From d45b8c57b536dc2911cafb24e0aeac5479006eea Mon Sep 17 00:00:00 2001 From: Sergii K Date: Tue, 1 Oct 2024 19:03:56 +0300 Subject: [PATCH] Update pkglist.awk Changes Made: Functions Created: process_file: Handles file processing logic. extract_fmri: Extracts the FMRI from a given line. check_facet: Checks for the facet condition in the file. print_fmri: Outputs the FMRI and facet information. Variable Names: Improved readability by using more descriptive variable names and adding local scope where necessary. Control Flow: The do...while loops were replaced with standard while loops to improve clarity. Separation of Concerns: Each function is focused on a single task, making it easier to read and maintain. This refactoring should maintain the original functionality while making the code easier to understand and modify in the future. --- usr/src/pkg/scripts/pkglist.awk | 84 +++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 29 deletions(-) diff --git a/usr/src/pkg/scripts/pkglist.awk b/usr/src/pkg/scripts/pkglist.awk index 3cab39bd6b62..a60bb36b1e59 100644 --- a/usr/src/pkg/scripts/pkglist.awk +++ b/usr/src/pkg/scripts/pkglist.awk @@ -23,33 +23,59 @@ # BEGIN { - if (ARGC < 2) { - exit - } - retcode = 0 - for (i = 1; i < ARGC; i++) { - do { - e = getline f < ARGV[i] - } while ((e == 1) && (f !~ /name=pkg.fmri/)) - if (e == 1) { - l = split(f, a, "=") - fmri = a[l] - facet = 0 - do { - e = getline f < ARGV[i] - if (e == 1 && - f ~ /org.opensolaris.incorp-facet.*=true/) - facet = 1 - } while ((e == 1) && (f ~ /^set name=/)) - close(ARGV[i]) - printf("depend fmri=%s type=$(PKGDEP_TYPE)", fmri) - if (facet) - printf(" vlfacet=true") - print "" - } else { - print "no fmri in " ARGV[i] >> "/dev/stderr" - retcode = 2 - } - } - exit retcode + if (ARGC < 2) { + exit + } + + retcode = 0 + for (i = 1; i < ARGC; i++) { + process_file(ARGV[i]) + } + exit retcode +} + +function process_file(filename) { + local fmri, facet, line, e + facet = 0 + + while ((e = getline line < filename) > 0) { + if (line ~ /name=pkg.fmri/) { + fmri = extract_fmri(line) + if (fmri) { + facet = check_facet(filename) + print_fmri(fmri, facet) + } else { + print "no fmri in " filename >> "/dev/stderr" + retcode = 2 + } + break + } + } + close(filename) +} + +function extract_fmri(line) { + split(line, a, "=") + return a[length(a)] +} + +function check_facet(filename) { + local line, e + while ((e = getline line < filename) > 0) { + if (line ~ /org.opensolaris.incorp-facet.*=true/) { + return 1 + } + if (!(line ~ /^set name=/)) { + break + } + } + return 0 +} + +function print_fmri(fmri, facet) { + printf("depend fmri=%s type=$(PKGDEP_TYPE)", fmri) + if (facet) { + printf(" vlfacet=true") + } + print "" }