Annotation of src/usr.bin/sudo/mkdefaults, Revision 1.3
1.1 millert 1: #!/usr/bin/perl -w
2: #
3: # Generate sudo_defs_table and associated defines
4: #
5: # Input should be formatted thusly:
6: #
7: # var_name
8: # TYPE
9: # description (or NULL)
1.2 millert 10: # array of struct def_values if TYPE == T_TUPLE
1.1 millert 11:
12: # Deal with optional -o (output) argument
13: if ($#ARGV > 0 && $ARGV[0] eq "-o") {
14: shift;
15: $header = $cfile = shift;
16: $header .= '.h';
17: $cfile .= '.c';
18: }
1.2 millert 19: die "usage: $0 input_file\n" unless $#ARGV == 0;
1.1 millert 20:
21: $infile = $ARGV[0];
22: if (!defined($header)) {
23: $header = $infile;
24: $header =~ s/(\.in)?$/.h/;
25: }
26: if (!defined($cfile)) {
27: $cfile = $infile;
28: $cfile =~ s/(\.in)?$/.c/;
29: }
30:
31: open(IN, "<$infile") || die "$0: can't open $infile: $!\n";
32: open(HEADER, ">$header") || die "$0: can't open $header: $!\n";
33: open(CFILE, ">$cfile") || die "$0: can't open $cfile: $!\n";
34:
1.2 millert 35: $count = 0;
36: @tuple_values = ( "never" );
37: @records = ();
1.1 millert 38: while(<IN>) {
39: chomp;
1.2 millert 40: s/\s*#.*$//;
1.1 millert 41: next if /^\s*$/;
42:
43: if (/^\S/) {
1.2 millert 44: # Store previous record and begin new one
45: $records[$count++] = [$var, $type, $desc, $values, $callback] if defined($var);
1.1 millert 46:
47: $var = $_;
1.2 millert 48: $type = '';
49: $desc = undef;
50: $values = undef;
51: $callback = undef;
52: $field = 0;
1.1 millert 53: } else {
1.2 millert 54: $field++;
1.1 millert 55: s/^\s+//;
56: s/\s+$//;
1.2 millert 57: if ($field == 1) {
58: # type
59: $type = $_;
60: } elsif ($field == 2) {
61: # description
62: if ($_ eq "NULL") {
63: $desc = "NULL";
64: } else {
65: # Strip leading and trailing double quote and escape the rest
66: s/^"//;
67: s/"$//;
68: s/"/\\"/g;
69: $desc = "\"$_\"";
70: }
71: } elsif ($field == 3 || $field == 4) {
72: if (s/^\*//) {
73: $callback = $_;
74: } else {
75: die "$0: syntax error near line $.\n" if $type !~ /^T_TUPLE/;
76: $values = [ split ];
77: foreach $v (@$values) {
78: push(@tuple_values, $v) unless grep(/^$v$/, @tuple_values);
79: }
80: }
1.1 millert 81: } else {
1.2 millert 82: die "$0: syntax error near line $.\n";
83: }
84: }
85: }
86: $records[$count++] = [$var, $type, $desc, $values, $callback] if defined($var);
87:
88: # Print out value arrays
89: for ($i = 0; $i < $count; $i++) {
90: if (defined($records[$i]->[3])) {
91: die "Values list specified for non-tupple\n" unless
92: $records[$i]->[1] =~ /^T_TUPLE/;
93: printf CFILE "static struct def_values def_data_%s[] = {\n", $records[$i]->[0];
94: foreach (@{$records[$i]->[3]}) {
95: print CFILE " { \"$_\", $_ },\n";
1.1 millert 96: }
1.2 millert 97: print CFILE " { NULL, 0 },\n";
98: print CFILE "};\n\n";
1.1 millert 99: }
100: }
1.2 millert 101:
102: # Print each record
103: print CFILE "struct sudo_defs_types sudo_defs_table[] = {\n {\n";
104: for ($i = 0; $i < $count; $i++) {
105: &print_record($records[$i], $i);
106: }
1.1 millert 107: print CFILE "\tNULL, 0, NULL\n }\n};\n";
108:
1.2 millert 109: # Print out def_tuple
110: if (@tuple_values) {
111: print HEADER "\nenum def_tupple {\n";
1.3 ! millert 112: for ($i = 0; $i <= $#tuple_values; $i++) {
! 113: printf HEADER "\t%s%s\n", $tuple_values[$i],
! 114: $i != $#tuple_values ? "," : "";
1.2 millert 115: }
116: print HEADER "};\n";
117: }
118:
1.1 millert 119: close(IN);
120: close(HEADER);
121: close(CFILE);
122:
123: sub print_record {
1.2 millert 124: my ($rec, $recnum) = @_;
125: my ($i, $v, $defname);
126: # each variable gets a macro to access its value
127: for ($rec->[1]) {
128: if (/^T_U?INT/) { $v = "ival"; }
129: elsif (/^T_STR/) { $v = "str"; }
130: elsif (/^T_FLAG/) { $v = "flag"; }
131: elsif (/^T_MODE/) { $v = "mode"; }
132: elsif (/^T_LIST/) { $v = "list"; }
133: elsif (/^T_LOGFAC/) { $v = "ival"; }
134: elsif (/^T_LOGPRI/) { $v = "ival"; }
135: elsif (/^T_TUPLE/) { $v = "tuple"; }
136: else { die "$0: unknown defaults type: $type\n"; }
137: }
138: printf HEADER "#define %-23s (sudo_defs_table[$recnum].sd_un.${v})\n",
139: "def_$rec->[0]";
140:
141: $defname = "I_" . uc($rec->[0]);
142: printf HEADER "#define %-24s%d", $defname, $recnum;
143: #print HEADER "\t/* $rec->[2] */" if defined($rec->[2]);
1.1 millert 144: print HEADER "\n";
145:
1.2 millert 146: print CFILE "\t\"$rec->[0]\", $rec->[1],\n\t$rec->[2],\n";
147: if (defined($rec->[3])) {
148: printf CFILE "\tdef_data_$rec->[0],\n";
149: } else {
150: printf CFILE "\tNULL,\n";
151: }
152: printf CFILE "\t$rec->[4],\n" if defined($rec->[4]);
153: print CFILE " }, {\n";
1.1 millert 154: }