File: main.c

package info (click to toggle)
lsfcc 0.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 324 kB
  • ctags: 217
  • sloc: ansic: 1,551; yacc: 408; sh: 337; lex: 85; makefile: 55
file content (58 lines) | stat: -rw-r--r-- 1,038 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sysexits.h>
#include <unistd.h>

void parse(int);								/* grammar.y */
extern FILE *yyin;

static char **av;
static int ac, input;

int main(int argc, char **argv) {
	int c, outfd;
	char *outpath = "lsf.out";
	
	while((c = getopt(argc, argv, "o:")) != -1) {
		switch(c) {
			case 'o':
				outpath = optarg;
				break;
			default:
				fprintf(stderr, "%s: unknown option character %c\n", argv[0], c);
				break;
		}
	}
	if(optind < argc)
		if(!(yyin = fopen(argv[optind], "r"))) {
			perror("fopen");
			exit(EX_NOINPUT);
		}
	av = argv;
	ac = argc;
	input = optind + 1;
	if((outfd = open(outpath, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
		perror("open");
		exit(EX_CANTCREAT);
	}
	parse(outfd);
	if(close(outfd)) {
		perror("close");
		exit(EX_OSERR);
	}
	exit(0);
}

int yywrap() {
	if(input >= ac)
		return 1;
	fclose(yyin);
	if(!(yyin = fopen(av[input++], "r"))) {
		perror("fopen");
		exit(EX_NOINPUT);
	}
	return 0;
}