sysvars.pl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/perl
  2. use strict;
  3. use integer;
  4. use File::Basename;
  5. sub c_name($) {
  6. my($s) = @_;
  7. $s =~ s/[^A-Za-z0-9_]+/_/g;
  8. return $s;
  9. }
  10. die "Usage: $0 infile hfile cfile\n" unless (scalar @ARGV >= 3);
  11. my($infile, $hfile, $cfile) = @ARGV;
  12. my $err = 0;
  13. open(my $in, '<', $infile) or die "$0: $infile: $!\n";
  14. my $ns;
  15. my %vars;
  16. my %nsprefix;
  17. my $line = 0;
  18. while (defined(my $l = <$in>)) {
  19. $line++;
  20. $l =~ s/^\s+//;
  21. $l =~ s/\s+$//;
  22. next if ($l =~ /^(\#.*)?$/);
  23. if ($l =~ /^\@(\w+)(\s+([\w\.]+))?/) {
  24. $ns = $1;
  25. if (!defined($vars{$ns})) {
  26. $vars{$ns} = {};
  27. }
  28. $nsprefix{$ns} = $3;
  29. next;
  30. }
  31. if (!defined{$ns}) {
  32. print STDERR "$0:$infile:$line: variable without namespace\n";
  33. $err++;
  34. next;
  35. }
  36. my($var,$type,$defval) = split(/\s+/, $l);
  37. if (!defined($type)) {
  38. print STDERR "$0:$infile:$line: variable $var lacks type\n";
  39. $err++;
  40. next;
  41. }
  42. $vars{$ns}->{$var} = [$type, $defval];
  43. }
  44. close($in);
  45. exit 1 if ($err);
  46. my @nslist = sort keys(%vars);
  47. my %nsfirst;
  48. my %nscount;
  49. my @varname = (undef);
  50. my @varenum = ('sysvar_null');
  51. my %vartype = ('sysvar_null' => 'SYSVAR_NULLTYPE');
  52. my @vardef;
  53. my $cnt = 1;
  54. foreach my $ns (@nslist) {
  55. $nsfirst{$ns} = $cnt;
  56. foreach my $var (sort keys(%{$vars{$ns}})) {
  57. my($type, $defval) = @{$vars{$ns}->{$var}};
  58. push(@varname, $nsprefix{$ns}.$var);
  59. my $ename = c_name($ns.'.'.$var);
  60. push(@varenum, $ename);
  61. $vartype{$ename} = "SYSVAR_TYPE(_$type)";
  62. if (defined($defval)) {
  63. # Add mangling here if needed
  64. push(@vardef, "\t[$ename] = { .v_$type = $defval },\n");
  65. }
  66. $cnt++;
  67. }
  68. $nscount{$ns} = $cnt - $nsfirst{$ns};
  69. }
  70. open(my $h, '>', $hfile) or die "$0:$hfile: $!\n";
  71. my $htag = c_name(uc(basename($hfile)));
  72. print $h "#ifndef $htag\n";
  73. print $h "#define $htag\n\n";
  74. print $h "enum sysvar_ns_enum {\n";
  75. print $h map { "\tsysvar_ns_$_,\n" } @nslist;
  76. print $h "\tsysvar_nscount\n};\n\n";
  77. print $h "enum sysvar_enum {\n";
  78. print $h map { "\t$_,\n" } @varenum;
  79. print $h "\tsysvar_count\n};\n\n";
  80. push(@varenum, 'sysvar_count');
  81. print $h "\n";
  82. # Sometimes it is convenient to have this as a preprocessor symbol
  83. print $h "#define SYSVAR_COUNT $cnt\n\n";
  84. print $h "extern_c const char * const sysvar_name[sysvar_count];\n";
  85. print $h "extern_c const sysvar_type_t sysvar_types[sysvar_count];\n";
  86. print $h "extern_c const sysvar_t sysvar_defval[sysvar_count];\n";
  87. print $h "extern_c sysvar_t sysvar_val[sysvar_count];\n";
  88. print $h "extern_c bool sysvar_isset[sysvar_count];\n\n";
  89. print $h "static inline sysvar_type_t sysvar_type(size_t var)\n";
  90. print $h "{\n";
  91. print $h "\tif (!__builtin_constant_p(var)) {\n";
  92. print $h "\t\tif(var >= (size_t)sysvar_count)\n";
  93. print $h "\t\t\treturn SYSVAR_NULLTYPE;\n";
  94. print $h "\t\telse\n";
  95. print $h "\t\t\treturn sysvar_types[var];\n";
  96. print $h "\t}\n\n";
  97. print $h "\tswitch(var) {\n";
  98. foreach my $v (@varenum) {
  99. next unless (defined($vartype{$v}));
  100. print $h "\t\tcase ", $v, ":\n";
  101. print $h "\t\t\treturn ", $vartype{$v}, ";\n";
  102. }
  103. print $h "\t\tdefault:\n";
  104. print $h "\t\t\treturn SYSVAR_NULLTYPE;\n";
  105. print $h "\t};\n";
  106. print $h "}\n\n";
  107. print $h "#endif /* $htag */\n";
  108. close($h);
  109. open(my $c, '>', $cfile) or die "$0:$cfile: $!\n";
  110. print $c "#include \"sysvars.h\"\n\n";
  111. printf $c "const sysvar_ns_t sysvar_ns[%d] = {\n", scalar(@nslist)+1;
  112. foreach my $ns (@nslist) {
  113. printf $c "\t{ \"%s\", %d },\n", $ns, $nsfirst{$ns}, $nscount{$ns};
  114. }
  115. printf $c "\t{ NULL, %d }\n", $cnt;
  116. printf $c "};\n";
  117. print $c "const char * const sysvar_name[$cnt] = {\n";
  118. print $c "\tNULL,\n";
  119. foreach my $ns (@nslist) {
  120. print $c "\n\t/* ---- $ns ---- */\n";
  121. my $v_end = $nsfirst{$ns} + $nscount{$ns};
  122. for (my $v = $nsfirst{$ns}; $v < $v_end; $v++) {
  123. print $c "\t\"", $varname[$v], "\",\n";
  124. }
  125. }
  126. print $c "};\n";
  127. print $c "const sysvar_type_t sysvar_types[$cnt] = {\n";
  128. foreach my $v (@varenum) {
  129. next unless (defined($vartype{$v}));
  130. print $c "\t[$v] = ", $vartype{$v}, ",\n";
  131. }
  132. print $c "};\n";
  133. print $c "const sysvar_t sysvar_defval[$cnt] = {\n";
  134. print $c @vardef;
  135. print $c "};\n";
  136. print $c "sysvar_t sysvar_val[$cnt];\n";
  137. print $c "bool sysvar_isset[$cnt];\n";
  138. close($c);