Exemplo n.º 1
0
Arquivo: main.c Projeto: kalopa/dbow
/*
 * It all starts here...
 */
int
main(int argc, char *argv[])
{
	int i;
	char tmpfname[256], *cp, *ifile;

	opterr = nflag = mflag = 0;
	active = NULL;
	fofile = hofile = NULL;
	/*
	 * Deal with the command-line options.
	 */
	while ((i = getopt(argc, argv, "h:t:o:xNmv")) != -1) {
		switch (i) {
		case 'h':
			hofile = optarg;
			break;

		case 't':
			if ((active = findtype(optarg)) == NULL) {
				fprintf(stderr, "dbow: unsupported code type: %s\n", optarg); 
				exit(1);
			}
			break;

		case 'o':
			fofile = optarg;
			break;
#ifdef YYDEBUG
		case 'x':
			yydebug = 1;
			break;
#endif
		case 'm':
			mflag = 1;
			break;

		case 'N':
			nflag = 1;
			break;

		case 'v':
			printf("DBOW version %s\n", DBOW_VERSION);
			exit(0);
			break;

		default:
			usage();
			break;
		}
	}
	/*
	 * The default generator is for C code.
	 */
	if (active == NULL && (active = findtype("c")) == NULL) {
		fprintf(stderr, "dbow: can't find default generator type.\n");
		exit(1);
	}
	/*
	 * Make sure we have an input file, and call the lexical
	 * analyzer to open it.  Report an error if that doesn't work.
	 */
	if ((argc - optind) != 1)
		usage();
	ifile = strdup(argv[optind]);
	if (lexopen(ifile) < 0) {
		fprintf(stderr, "dbow: ");
		perror(ifile);
		exit(1);
	}
	/*
	 * If no output file has been specified, then carve an
	 * output file by changing the <file>.d to <file>.<fext>
	 * where 'fext' is the generators file extension.
	 */
	if (fofile == NULL) {
		strncpy(tmpfname, ifile, sizeof(tmpfname) - 5);
		if ((cp = strrchr(tmpfname, '.')) != NULL &&
					cp[1] == 'd' && cp[2] == '\0') {
			*cp = '\0';
		} else
			cp = strchr(tmpfname, '\0');
		*cp++ = '.';
		if (mflag)
			strcpy(cp, "m4");
		else
			strcpy(cp, active->fext);
		if ((cp = strrchr(tmpfname, '/')) != NULL)
			cp++;
		else
			cp = tmpfname;
		fofile = strdup(cp);
	}
	/*
	 * Open the pipe to the M4 command.
	 * XXX - don't use M4 when generating SQL (for now!).
	 */
	if (mflag || active->cdtype == CDT_DBASE) {
		if (strcmp(fofile, "-") == 0)
			fofp = stdout;
		else if ((fofp = fopen(fofile, "w")) == NULL) {
			fprintf(stderr, "dbow: cannot open file for writing: ");
			perror(fofile);
			exit(1);
		}
		hofile = NULL;
	} else
		fofp = m4open(fofile, active);
	linesync(ifile, 1, fofp);
	if (hofile == NULL)
		hofp = fofp;
	else {
		hofp = m4open(hofile, active);
		linesync(ifile, 1, hofp);
	}
	tofp = NULL;
	/*
	 * Parse the input file using YACC and close the input stream.
	 * If there are no errors, call each table generator.
	 */
	nerrors = 0;
	yyparse();
	lexclose();
	if (nerrors == 0) {
		/*
		 * Perform function optimizations and check all
		 * tables to make sure they have the basic quorum
		 * of functions.
		 */
		functioncleanup();
		/*
		 * If we've diverted the prolog to a separate file, then
		 * we'll need to include it in the main file.
		 */
		if (fofp != hofp)
			fileinc(hofile, fofp);
		doproto(ifile, 0);
		docode(NULL, 0);
		/*
		 * Emit the epilog code.
		 */
		genepilog(fofp);
		if (hofp != fofp)
			genepilog(hofp);
		if (tofp != NULL) {
			fclose(tofp);
			if ((tofp = fopen(tofile, "r")) != NULL) {
				while ((i = fgetc(tofp)) != EOF)
					fputc(i, fofp);
				fclose(tofp);
			}
			unlink(tofile);
		}
	}
	/*
	 * Close out the output file (and delete it if it's bad),
	 * then exit.
	 */
	pclose(fofp);
	if (hofp != fofp)
		pclose(hofp);
	exit(nerrors > 0 ? 1 : 0);
}
Exemplo n.º 2
0
int
main(int argc, char **argv)
{
	wchar_t t;
	int i, opt_end = 0;
	int sigs[] = {SIGHUP, SIGINT, SIGPIPE, 0};

#if defined(__lint)
	yydebug = 0;
#endif

	for (i = 0; sigs[i]; ++i) {
		if (signal(sigs[i], SIG_IGN) != SIG_IGN)
			(void) signal(sigs[i], catchsig);
	}
	tempfile = mktemp(tmp_name);
	(void) close(creat(tempfile, 0));

	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	if ((mb_cur_max = MB_CUR_MAX) > 1)
		wide = 1;

	procnam = argv[0];
	getflags(&argc, &argv, &opt_end);
	initalloc();

	setfname("-");
	if (argc > 1) {
		--argc;
		++argv;
		if (strcmp(argv[0], "-")) {
			ifile[ifx] = m4open(&argv, "r", &argc);
			setfname(argv[0]);
		}
	}

	for (;;) {
		token[0] = t = getchr();
		token[1] = EOS;

		if (t == WEOF) {
			if (ifx > 0) {
				(void) fclose(ifile[ifx]);
				ipflr = ipstk[--ifx];
				continue;
			}

			getflags(&argc, &argv, &opt_end);

			if (argc <= 1)
				/*
				 * If dowrap() has been called, the m4wrap
				 * macro has been processed, and a linked
				 * list of m4wrap strings has been created.
				 * The list starts at wrapstart.
				 */
				if (wrapstart) {
					/*
					 * Now that EOF has been processed,
					 * display the m4wrap strings.
					 */
					showwrap();
					continue;
				} else
					break;
			--argc;
			++argv;

			if (ifile[ifx] != stdin)
				(void) fclose(ifile[ifx]);

			if (strcmp(argv[0], "-"))
				ifile[ifx] = m4open(&argv, "r", &argc);
			else
				ifile[ifx] = stdin;

			setfname(argv[0]);
			continue;
		}

		if (is_alpha(t) || t == '_') {
			wchar_t	*tp = token+1;
			int tlim = toksize;
			struct nlist	*macadd;  /* temp variable */

			while ((*tp = getchr()) != WEOF &&
			    (is_alnum(*tp) || *tp == '_')) {
				tp++;
				if (--tlim <= 0)
					error2(gettext(
					    "more than %d chars in word"),
					    toksize);
			}
			putbak(*tp);
			*tp = EOS;

			macadd = lookup(token);
			*Ap = (wchar_t *)macadd;
			if (macadd->def) {
				if ((wchar_t *)(++Ap) >= astklm) {
					--Ap;
					error2(gettext(
					    "more than %d items on "
					    "argument stack"),
					    stksize);
				}

				if (Cp++ == NULL)
					Cp = callst;

				Cp->argp = Ap;
				*Ap++ = op;
				puttok(token);
				stkchr(EOS);
				t = getchr();
				putbak(t);

				if (t != '(')
					pbstr(L"()");
				else	/* try to fix arg count */
					*Ap++ = op;

				Cp->plev = 0;
			} else {
				puttok(token);
			}
		} else if (match(t, lquote)) {
			int	qlev = 1;

			for (;;) {
				token[0] = t = getchr();
				token[1] = EOS;

				if (match(t, rquote)) {
					if (--qlev > 0)
						puttok(token);
					else
						break;
				} else if (match(t, lquote)) {
					++qlev;
					puttok(token);
				} else {
					if (t == WEOF)
						error(gettext(
						"EOF in quote"));
					putchr(t);
				}
			}
		} else if (match(t, lcom) &&
		    ((lcom[0] != L'#' || lcom[1] != L'\0') ||
		    prev_char != '$')) {

			/*
			 * Don't expand commented macro (between lcom and
			 * rcom).
			 * What we know so far is that we have found the
			 * left comment char (lcom).
			 * Make sure we haven't found '#' (lcom) immediately
			 * preceded by '$' because we want to expand "$#".
			 */

			puttok(token);
			for (;;) {
				token[0] = t = getchr();
				token[1] = EOS;
				if (match(t, rcom)) {
					puttok(token);
					break;
				} else {
					if (t == WEOF)
						error(gettext(
						"EOF in comment"));
					putchr(t);
				}
			}
		} else if (Cp == NULL) {
			putchr(t);
		} else if (t == '(') {
			if (Cp->plev)
				stkchr(t);
			else {
				/* skip white before arg */
				while ((t = getchr()) != WEOF && is_space(t))
					;

				putbak(t);
			}

			++Cp->plev;
		} else if (t == ')') {
			--Cp->plev;

			if (Cp->plev == 0) {
				stkchr(EOS);
				expand(Cp->argp, Ap-Cp->argp-1);
				op = *Cp->argp;
				Ap = Cp->argp-1;

				if (--Cp < callst)
					Cp = NULL;
			} else
				stkchr(t);
		} else if (t == ',' && Cp->plev <= 1) {
			stkchr(EOS);
			*Ap = op;

			if ((wchar_t *)(++Ap) >= astklm) {
				--Ap;
				error2(gettext(
				    "more than %d items on argument stack"),
				    stksize);
			}

			while ((t = getchr()) != WEOF && is_space(t))
				;

			putbak(t);
		} else {
			stkchr(t);
		}
	}

	if (Cp != NULL)
		error(gettext(
		"EOF in argument list"));

	delexit(exitstat, 1);
	return (0);
}