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