# # Global variables: # g_debug: debuggin on? # g_filename: Filename to output. # g_entries: # of entries in the table. # g_max_common_entries: # of entries without a special table. # g_emitted_nc boolean, have we emitted a non-comment line yet? # g_orig_is_enabled: boolean, is the original template line enabled? # # line_emit: boolean, emit the current line? # line_parsed: boolean, has the current line been modified? # line_isenabled: boolean, is it enabled? # line_line: The actual line. # # a_*: Table of all line statuses. # a_type: What type is the entry. # a_enable: Is the entry enabled? # a_wline: Source config line for the entry. # a_arg{1,2,3}: arguments 1, 2 and 3 # # is one of dev, pdev, opts, fs. # t__entries # of entries in the _arg# tables. # t__arg{1,2,3} Argument tables. # t__index # # function emit_init() { printf("# Config file autogenerated.\n") >g_filename line_emit = 1; } function emit_line(is_enabled, theline) { # Actually emit a line if (is_enabled == 0) printf("#%s\n", theline) >>g_filename; else printf("%s\n", theline) >>g_filename; } function emit_source_build_lines(j, found) { for (j = 0 ; j < g_entries ; j++) { if (a_type[j] == T_SOURCE) { emit_line(a_enable[j], sprintf("source %s", a_arg1[j])); a_used[j] = 1; found = 1; } if (a_type[j] == T_BUILD) { emit_line(a_enable[j], sprintf("build %s", a_arg1[j])); a_used[j] = 1; found = 1; } } if (found == 1) emit_line(1, ""); } function output_line(theline, is_enabled, is_parsed) { # Figure if (is_parsed == 0) { # We didn't change this line. # Use the original from the template file. if (g_emitted_nc == 0) { if (g_orig_is_enabled == 1) { g_emitted_nc = 1; emit_source_build_lines(); } } printf("%s\n", g_origline) >>g_filename; } else { # Spit out the modified line. if (is_enabled == 1) { # If this is the first enabled line, emit source and build. if (g_emitted_nc == 0) { g_emitted_nc = 1; emit_source_build_lines(); } printf("%s\n", theline) >>g_filename; } else { printf("#%s\n", theline) >>g_filename; } } } # # File starts with # of entries. # Each entry is 6 lines: # wline: Line from the source file. Uncommented. # type: Type of line # enable: Is it enabled? # arg1: arguments. # arg2 # arg3 function read_table(table_name, temp_type, temp_j, j) { getline g_entries = 1) { print "Processing ", thetype, " line."; } # XXX blah. Assume this line is the original template line # XXX and use the enable status. is_enabled = g_orig_is_enabled; # Also assume leading spaces and #s have been stripped. pline = theline; # Split the line into parts. if (split_regexp != "") { # Allow a splitting pattern to be passed in. # This is to aproximate quotation of arguments. XXX nparts = split(pline, part_array, split_regexp); for (j = 1 ; j <= nparts ; j++) { gsub(/^[[:space:]]+/, "", part_array[j]); gsub(/[[:space:]]+$/, "", part_array[j]); } } else { nparts = split(pline, part_array); } # Figure out which part(s) of the line to use. if (index1 > 0) { t1 = part_array[index1]; } else { t1 = ""; } if (index2 > 0) { t2 = part_array[index2]; } else { t2 = ""; } if (index3 > 0) { t3 = part_array[index3]; } else { t3 = ""; } # Check it against the table. if (g_debug >= 2) printf("T> %s\nTemplate: type:%s On:%d Arg1:%s Arg2:%s Arg3:%s\n", theline, thetype, is_enabled, t1, t2, t3); lookup_in_table(theline, thetype, is_enabled, t1, t2, t3); } # Need to figure out if the template line is enabled. { g_origline = $0; # See if this line is on. if (/^[[:space:]]*#/) g_orig_is_enabled = 0; else g_orig_is_enabled = 1; # Strip leading spaces and #s gsub(/^[[:space:]]*(#+[[:space:]]*)+/, ""); } # ---- snip ---- # XXX XXX This should go in a common file. # # "foo" " " "at" " " "bar" /^[^[:space:]]+[[:space:]]+at[[:space:]]+[^[:space:]]+/ { process_line($0, T_DEV, 1, 3, -1); } #"pseudo-device" " " "foo" /^pseudo-device[[:space:]]+[^[:space:]]+/ { process_line($0, T_PDEV, 2, 3, -1); } # XXX: This needs some work to handle options set to a value. # XXX: This needs some work to handle multiple options to a line. #"options" " " "FOO" /^options[[:space:]]+[^[:space:]]+/ { process_line($0, T_OPTIONS, 2, -1, -1); } #"file-system" " " "FOO" /^file-system[[:space:]]+[^[:space:]]+/ { process_line($0, T_FS, 2, -1, -1); } #"makeoptions" " " "FOO" /^makeoptions[[:space:]]+[^[:space:]]+/ { process_line($0, T_MAKEOPT, 2, -1, -1); } #"config" " " "netbsd" " " "root" " " "on" " " # "sd0a" " " "type" " " "ffs" /^config[[:space:]]+[^[:space:]]+[[:space:]]+root[[:space:]]+on[[:space:]]+[^[:space:]]+[[:space:]]+type[[:space:]]+[^[:space:]]+/ { process_line($0, T_CONFIG, 2, 5, 7); } #"source" " " "/foo/bar" /^source[[:space:]]+[^[:space:]]+/ { process_line($0, T_SOURCE, 2, -1, -1); } #"build" " " "/foo/bar" /^build[[:space:]]+[^[:space:]]+/ { process_line($0, T_BUILD, 2, -1, -1); } #"maxusers" " " "123" /^maxusers[[:space:]]+[[:digit:]]+/ { process_line($0, T_MAXUSERS, 2, -1, -1); } #"ident" " " \"foo\" /^ident[[:space:]]+"[^"]*"/ { # XXX not quite right action wrt quoted info. process_line($0, T_IDENT, 2, -1, -1, "\[\"\]\+"); } # ---- snip ---- # After matching the line and potentially rewriting it, output it. # any rewriting should have set line_line. { if (line_emit == 1) output_line(line_line, line_isenabled, line_parsed); line_emit = 1; line_parsed = 0; line_isenabled = 0; line_line = "DEADBEEF"; } END { if (g_newlines > 0) { print "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" print "New lines in the config:" for (j = 0 ; j < g_newlines ; j++) { print g_newline[j]; } } found_old = 0; for (j = 0 ; j < g_entries ; j++) { if (a_used[j] == 0) { if (found_old == 0) { print "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" print "Unused lines from old config:" found_old = 1; } print a_wline[j] } } }