#!/usr/bin/perl -w use strict; use Getopt::Std; use CGenerator; my %o; getopts('ch', \%o); my $cg = CGenerator::new("defaults"); my $parse_bool = " static void parse_bool(const char *s, bool *b) { if(!s) return; *b = (strcasecmp(\"no\",s) && strcasecmp(\"false\", s)); }"; $cg->cb(" static char * chomp(char *s) { if(!s) return s; while(isspace(*s)) s++; int n = strlen(s); while(n >= 0 && isspace(s[n - 1])) n--; s = strdup(s); s[n] = '\\0'; return s; }"); my ($y,$z); $cg->hinc("X11/Xlib.h"); $cg->hinc("colors.h", 1); $cg->cinc("config.h"); $cg->cinc("strings.h"); $cg->cinc("string.h"); $cg->cinc("assert.h"); $cg->cinc("ctype.h"); $cg->cinc("stdio.h"); $cg->cinc("mod.h", 1); $cg->fund("setup_defaults", "void", "Display *disp", "char *pname"); $cg->fun("setup_defaults","char *tmp;"); $cg->fund("defaults_print", "void"); $cg->fund("defaults_option", "void", "char *opt", "char *value"); $cg->fund("defaults_finalize", "void"); $cg->fun("defaults_option", "if(!value) return;"); foreach (<>) { next if /^\s*$/; next if /^\s*\#/; /([a-zA-Z_0-9]*)\:([a-z]+\:)?\s+(.*)\s*$/; my $b = CGenerator::cquote($3); my $a = CGenerator::cquote($1); $cg->dd("static const char DEF_${1}[]", $b); $cg->dd("char const *D_$1", "DEF_$1"); $cg->fun("setup_defaults","if(D_$1 == DEF_$1) {"); $cg->fun("setup_defaults"," tmp = XGetDefault(disp, pname, $a);"); $cg->fun("setup_defaults"," if(tmp) D_$1 = chomp(tmp);"); $cg->fun("setup_defaults","}"); $cg->fun("defaults_option","else if(!strcasecmp(\"$1\",opt))"); $cg->fun("defaults_option","D_$1 = chomp(value);"); $cg->fun("defaults_print","printf(\"$1: %s\\n\", D_$1);"); if(defined($2) and $2 eq "bool:") { $cg->hinc("stdbool.h"); $cg->cb($parse_bool); $cg->dd("bool DB_$1", $3); $cg->fun("defaults_finalize", "parse_bool(D_$1, &DB_$1);"); # $cg->fun("defaults_option","parse_bool(D_$1, &DB_$1);"); } if(defined($2) and $2 eq "color:") { #$z .= "\ts = XAllocNamedColor(disp, cmap, D_$1, &color, &color2);\n"; #$z .= "\tDC_$1 = color.pixel;\n"; #$z .= "\tif(!s)\n"; #$z .= "\t\tfprintf(stderr, \"BadColor - uterm.$1: '%s'\\n\", D_$1 );\n"; $cg->dd("struct color *DC_$1"); $cg->fun("defaults_finalize","DC_$1 = get_color(D_$1);") } if(defined($2) and $2 eq "key:") { $cg->dd("struct key DK_$1"); $cg->fun("defaults_finalize","parse_key(D_$1, &DK_$1);") } if(defined($2) and $2 eq "int:") { $cg->cinc("stdlib.h"); $cg->dd("int DI_$1", $3); $cg->fun("defaults_finalize", "DI_$1 = strtoul(D_$1, NULL, 0);"); } } $cg->fun("defaults_option","else fprintf(stderr, \"Unknown Option: %s\\n\", opt);"); $cg->show(\%o);