Home | History | Annotate | Download | only in re2c
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include "globals.h"
      6 #include "parse.h"
      7 #include "dfa.h"
      8 #include "mbo_getopt.h"
      9 
     10 const char *fileName = 0;
     11 char *outputFileName = 0;
     12 int sFlag = 0;
     13 int bFlag = 0;
     14 int dFlag = 0;
     15 int iFlag = 0;
     16 int bUsedYYAccept = 0;
     17 unsigned int oline = 1;
     18 unsigned int maxFill = 1;
     19 int vFillIndexes = -1;
     20 unsigned char *vUsedLabels;
     21 unsigned int vUsedLabelAlloc = 1000;
     22 
     23 static char *opt_arg = NULL;
     24 static int opt_ind = 1;
     25 
     26 static const mbo_opt_struct OPTIONS[] = {
     27 	{'?', 0, "help"},
     28 	{'b', 0, "bit-vectors"},
     29 	{'d', 0, "debug-output"},
     30 	{'e', 0, "ecb"},
     31 	{'f', 0, "storable-state"},
     32 	{'h', 0, "help"},
     33 	{'i', 0, "no-debug-info"},
     34 	{'o', 1, "output"},
     35 	{'s', 0, "nested-ifs"},
     36 	{'v', 0, "version"},
     37         {'-', 0, NULL} /* end of args */
     38 };
     39 
     40 static void usage()
     41 {
     42     fprintf(stderr,
     43 	"usage: re2c [-esbvhd] file\n"
     44 	"\n"
     45 	"-? -h   --help          Display this info.\n"
     46 	"\n"
     47 	"-b      --bit-vectors   Implies -s. Use bit vectors as well in the attempt to\n"
     48 	"                        coax better code out of the compiler. Most useful for\n");
     49     fprintf(stderr,
     50 	"                        specifications with more than a few keywords (e.g. for\n"
     51 	"                        most programming languages).\n"
     52 	"\n"
     53 	"-e      --ecb           Cross-compile from an ASCII platform to\n"
     54 	"                        an EBCDIC one.\n"
     55 	"\n");
     56     fprintf(stderr,
     57 	"-s      --nested-ifs    Generate nested ifs for some switches. Many compilers\n"
     58 	"                        need this assist to generate better code.\n"
     59 	"\n"
     60 	"-f      --storable-state Generate a scanner with support for storable state\n"
     61 	"\n"
     62 	"-o      --output=output Specify the output file instead of stdout\n"
     63 	"\n");
     64     fprintf(stderr,
     65 	"-d      --debug-output  Creates a parser that dumps information during\n"
     66 	"                        about the current position and in which state the\n"
     67 	"                        parser is.\n"
     68 	"\n"
     69 	"-i      --no-debug-info Do not generate '#line' info (usefull for versioning).\n"
     70 	"\n"
     71 	"-v      --version       Show version information.\n"
     72 	"-V      --vernum        Show version as one number.\n");
     73 }
     74 
     75 char *
     76 mystrdup(const char *str)
     77 {
     78 	size_t len;
     79 	char *copy;
     80 
     81 	len = strlen(str) + 1;
     82 	copy = malloc(len);
     83 	memcpy(copy, str, len);
     84 	return (copy);
     85 }
     86 
     87 int main(int argc, char *argv[])
     88 {
     89     int c;
     90     FILE *f, *output;
     91 
     92     fileName = NULL;
     93 
     94     if(argc == 1) {
     95 	usage();
     96 	return 2;
     97     }
     98 
     99     while ((c = mbo_getopt(argc, argv, OPTIONS, &opt_arg, &opt_ind, 0))!=-1) {
    100 	switch (c) {
    101 	    case 'b':
    102 		sFlag = 1;
    103 		bFlag = 1;
    104 		break;
    105 	    case 'e':
    106 		xlat = asc2ebc;
    107 		talx = ebc2asc;
    108 		break;
    109 	    case 's':
    110 		sFlag = 1;
    111 		break;
    112 	    case 'd':
    113 		dFlag = 1;
    114 		break;
    115 	    case 'f':
    116 		vFillIndexes = 0;
    117 		break;
    118 	    case 'i':
    119 		iFlag = 1;
    120 		break;
    121 	    case 'o':
    122 		outputFileName = opt_arg;
    123 		break;
    124 	    case 'v':
    125 		fputs("re2c " PACKAGE_VERSION "\n", stdout);
    126 		break;
    127 	    case 'V': {
    128 		int v1, v2, v3;
    129 		sscanf(PACKAGE_VERSION, "%d.%d.%d", &v1, &v2, &v3);
    130 		fprintf(stdout, "%02d%02d%02d\n", v1, v2, v3);
    131 		return 2;
    132 	    }
    133 	    case 'h':
    134 	    case '?':
    135 	    default:
    136 		usage();
    137 		return 2;
    138  	}
    139     }
    140 
    141     if (argc == opt_ind + 1) {
    142 	fileName = argv[opt_ind];
    143     } else {
    144 	usage();
    145 	return 2;
    146     }
    147 
    148     vUsedLabels = calloc(vUsedLabelAlloc, 1);
    149     if (!vUsedLabels) {
    150 	fputs("Out of memory.\n", stderr);
    151 	return 1;
    152     }
    153 
    154     /* set up the input stream */
    155     if(fileName[0] == '-' && fileName[1] == '\0'){
    156 	fileName = "<stdin>";
    157 	f = stdin;
    158     } else {
    159 	if((f = fopen(fileName, "rt")) == NULL){
    160 	    fprintf(stderr, "can't open %s\n", fileName);
    161 	    return 1;
    162 	}
    163     }
    164 
    165     /* set up the output stream */
    166     if (outputFileName == 0 || (fileName[0] == '-' && fileName[1] == '\0')) {
    167 	outputFileName = mystrdup("<stdout>");
    168 	output = stdout;
    169     } else {
    170 	int len;
    171 	char *src, *dst, *tmp;
    172 
    173 	output = fopen(outputFileName, "wt");
    174 	if (!output) {
    175 	    fprintf(stderr, "can't open %s\n", outputFileName);
    176 	    return 1;
    177 	}
    178 
    179 	len = strlen(outputFileName);
    180 	tmp = (char*)malloc((len+1)*2);
    181 
    182 	for (src = outputFileName, dst = tmp; *src; ++src)
    183 	{
    184 	    if (*src == '\\')
    185 		*dst++ = *src;
    186 	    *dst++ = *src;
    187 	}
    188 	*dst = '\0';
    189 
    190 	outputFileName = tmp;
    191     }
    192 
    193     parse(f, output);
    194     free(outputFileName);
    195     return 0;
    196 }
    197