/* Stubby - Generate stubs for dynamic-link libraries.
 * Copyright 1999 Tom Rothamel
 *
 * Stubby is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version. It is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * This program generates another computer program. I do not assert a      
 * copyright on the generated program that is the normal output of this    
 * program. This is _not_, however, an authorization for you to modify     
 * this program to cause it to output more of itself than necessary for    
 * its proper operation in order to circumvent my copyright.               
 */

/* This file contains the main routine for stubby, as well as some utility
 * functions called by it. */

  
#include <unistd.h>
#include "stubby.h"
#include "str.h"

char *dl_type = DLTYPE;
char *infile = "<stdin>";
char *libdir = LIBDIR;


/* Display a usage message, then quit. */
void usage() {
	printf("usage: stubby [-o outputfile] [-L libdir] [-T dltype] [inputfile]\n"
	       "       stubby -h\n"
	       "       stubby -V\n");
	exit(0);
}

/* Display a version message, then quit. */
void version() {
	printf("stubby %s\n"
	       "\tCopyright 1999 Tom Rothamel\n"
	       "\tThis is free software under the Gnu GPL. There is NO WARRANTY.\n",
	       VERSION);
	exit(0);
}

/* Copy the input to the output file, stopping once it sees a '%%'. */
void prefix(FILE *f) {
	char buf[256];

	while (fgets(buf, 256, f)) {
		yylineno++;
		if (buf[0] == '%' && buf[1] == '%') return;
		fprintf(of, "%s", buf);
	}

	error("EOF reached without seeing '%%%%'.");
	exit(-1);
}

/* This main option first takes care of parsing options, then calls the
 * input parser to process the c-like input file. */
int main(int argc, char **argv) {
	int c;

	of = stdout;
	yyin = stdin;

	yydebug = 0;
	yylineno = 1;

	while ((c = getopt(argc, argv, "o:L:T:yhV")) != -1) {
		switch (c) {
		case 'o':		       			
			of = fopen(optarg, "w");
			if (!of) {
				error("Couldn't open output file '%s'.", optarg);
				exit(-1);
			}
			break;
		case 'L':
			libdir = optarg;
			break;
		case 'T':
			dl_type = optarg;
			break;
		case 'y':
			yydebug = 1;
			break;
		case 'h':
			usage();
			break;
		case 'V':
			version();
			break;			
		case '?':
			error("Unknown option '%c'.", optopt);
			usage();
			exit(-1);
		}
	}

	if (optind < argc) {
		infile = argv[optind];
		yyin = fopen(infile, "r");
		if (!yyin) {
			error("Couldn't open input file.\n");
		}
	}	
	
	init_typehash();
	prefix(yyin);
	
	yyparse();
	finish_library();
	
	exit(0);
}
