Home | History | Annotate | Line # | Download | only in dist
skel2c revision 1.1.1.3
      1      1.1  christos # vile: awkmode
      2      1.1  christos function noident(given) {
      3      1.1  christos 	gsub(/\$/,"@", given);
      4      1.1  christos 	return given;
      5      1.1  christos }
      6      1.1  christos BEGIN { havesection = 0;
      7  1.1.1.3  christos 	version = "Id: skel2c,v 1.3 2014/04/06 19:48:04 tom Exp ";
      8      1.1  christos 	nsec = 0;
      9      1.1  christos 	ifdef = "";
     10      1.1  christos 	printf  "/* This file generated automatically using\n * %s\n */\n\n",
     11      1.1  christos 		noident(version);
     12      1.1  christos       }
     13      1.1  christos /[$]Id[:][^$]*[$]/ {
     14      1.1  christos 	printf "%s\n", noident($0);
     15      1.1  christos 	next;
     16      1.1  christos       }
     17      1.1  christos /^%% *insert *VERSION *here/ {
     18      1.1  christos 	printf "    CONCAT1(\"#define YYMAJOR \", YYMAJOR),\n";
     19      1.1  christos 	printf "    CONCAT1(\"#define YYMINOR \", YYMINOR),\n";
     20      1.1  christos 	printf "#ifdef YYPATCH\n";
     21      1.1  christos 	printf "    CONCAT1(\"#define YYPATCH \", YYPATCH),\n";
     22      1.1  christos 	printf "#endif\n";
     23      1.1  christos 	next;
     24      1.1  christos       }
     25      1.1  christos /^%%ifdef/ {
     26      1.1  christos 	if (NF >= 2) {
     27      1.1  christos 	    printf "#if defined(%s)\n", $2;
     28      1.1  christos 	    printf "    \"#if %s\",\n", $2;
     29      1.1  christos 	} else {
     30      1.1  christos 	    _abort_exit = 1;
     31      1.1  christos 	    printf "skel2c: ill-formed %%ifdef in skeleton file on line %d\n", FNR > "/dev/stderr";
     32      1.1  christos 	    exit 2;
     33      1.1  christos 	}
     34      1.1  christos 	if (ifdef != "") {
     35      1.1  christos 	    printf "skel2c: nested %%ifdef in skeleton file on line %d\n", FNR > "/dev/stderr";
     36      1.1  christos 	    exit 2;
     37      1.1  christos 	}
     38      1.1  christos 	ifdef = $2;
     39      1.1  christos 	next;
     40      1.1  christos       }
     41      1.1  christos /^%%endif/ {
     42      1.1  christos 	if (ifdef != "") {
     43      1.1  christos 	    if (NF >= 2) {
     44      1.1  christos 		printf "    \"#endif /* %s */\",\n", $2;
     45      1.1  christos 		printf "#endif /* defined(%s) */\n", $2;
     46      1.1  christos 	    } else {
     47      1.1  christos 		printf "    \"#endif /* %s */\",\n", ifdef;
     48      1.1  christos 		printf "#endif /* defined(%s) */\n", ifdef;
     49      1.1  christos 	    }
     50      1.1  christos 	    ifdef = "";
     51      1.1  christos 	} else {
     52      1.1  christos 	    printf "    \"#endif\",\n";
     53      1.1  christos 	    printf "#endif\n";
     54      1.1  christos 	    printf "skel2c: unmatched %endif in skeleton file on line %d\n", FNR > "/dev/stderr";
     55      1.1  christos 	    exit 2;
     56      1.1  christos 	}
     57      1.1  christos 	next;
     58      1.1  christos       }
     59      1.1  christos /^%%/ { if (havesection) {
     60      1.1  christos 	    printf "    0\n};\n\n";
     61      1.1  christos 	}
     62      1.1  christos 	if (NF >= 2) {
     63      1.1  christos 	    havesection = 1;
     64      1.1  christos 	    section = $2;
     65      1.1  christos 	    seclist[nsec] = section;
     66      1.1  christos 	    nsec = nsec + 1;
     67      1.1  christos 	    printf "const char *const %s[] =\n{\n", $2;
     68      1.1  christos 	} else {
     69      1.1  christos 	    havesection = 0;
     70      1.1  christos 	}
     71      1.1  christos 	next;
     72      1.1  christos       }
     73      1.1  christos       { if (havesection) {
     74      1.1  christos 	    # Could use 'gsub(/\\/, "\\\\")' instead of the following
     75      1.1  christos 	    # two lines, but there's a bug in mawk and the original
     76      1.1  christos 	    # awk (not in gawk) which is triggered by that.
     77      1.1  christos 	    gsub(/\\/, "\\\1");
     78      1.1  christos 	    gsub(/\1/, "\\");
     79      1.1  christos #	    gsub(/\t/, "\\t"); # change '\t' to "\\t"
     80      1.1  christos 	    gsub(/\"/, "\\\"");
     81      1.1  christos 	    printf "    \"%s\",\n", $0;
     82      1.1  christos 	} else {
     83      1.1  christos 	    print $0;
     84      1.1  christos 	}
     85      1.1  christos       }
     86      1.1  christos END   { if (_abort_exit)
     87      1.1  christos 	    exit 2;
     88      1.1  christos 	if (havesection) {
     89      1.1  christos 	    print "    0\n};\n";
     90      1.1  christos 	}
     91      1.1  christos 	if (nsec > 0) {
     92      1.1  christos 	    print "void";
     93      1.1  christos 	    print "write_section(FILE * fp, const char *const section[])";
     94      1.1  christos 	    print "{";
     95      1.1  christos 	    print "    int i;";
     96      1.1  christos 	    print "    const char *s;\n";
     97      1.1  christos 	    print "    for (i = 0; (s = section[i]) != 0; ++i)";
     98      1.1  christos 	    print "    {";
     99      1.1  christos 	    print "\tif (fp == code_file)";
    100      1.1  christos 	    print "\t    ++outline;";
    101      1.1  christos 	    print "\tfprintf(fp, \"%s\\n\", s);";
    102      1.1  christos 	    print "    }";
    103      1.1  christos 	    print "}";
    104      1.1  christos 	} else {
    105      1.1  christos 	    print "skel2c: no sections defined in skeleton file" > "/dev/stderr";
    106      1.1  christos 	    exit 2;
    107      1.1  christos 	}
    108      1.1  christos 	if (ifdef != "") {
    109      1.1  christos 	    printf "skel2c: unmatched %%ifdef %s at end of skeleton file\n", $ifdef > "/dev/stderr";
    110      1.1  christos 	    exit 2;
    111      1.1  christos 	}
    112      1.1  christos       }
    113