-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquish_main.c
74 lines (64 loc) · 1.34 KB
/
squish_main.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "squish_run.h"
static int
printHelp(char *progname)
{
printf("%s <options> [ <files> ]\n", progname);
printf("\n");
printf("Run scripts from files, or stdin if no file specified\n");
printf("\n");
printf("Options:\n");
printf("-o <file> : place output in <file>\n");
printf("-v : be more verbose\n");
printf("\n");
exit (1);
}
int
main(int argc, char **argv)
{
FILE *ofp = stdout;
FILE *promptfp = stdout;
int verbosity = 0;
int status = 0;
int i, ch;
while ((ch = getopt(argc, argv, "hvo:")) != -1) {
switch (ch) {
case 'v':
verbosity++;
break;
case 'o':
if ((ofp = fopen(optarg, "w")) == NULL) {
(void) fprintf(stderr,
"failed opening output file '%s' : %s\n",
optarg, strerror(errno));
exit(-1);
}
break;
case '?':
case 'h':
default:
printHelp(argv[0]);
break;
}
}
/*
* skip arguments processed by getopt -- note that the first
* remaining item in argv is now in position 0
*/
argc -= optind;
argv += optind;
if (argc > 0) {
for (i = 0; i < argc; i++) {
status = runScriptFile(ofp, promptfp, argv[i], verbosity);
if (status != 0) exit (status);
}
exit (0);
}
if (runScript(ofp, promptfp, stdin, "stdin", verbosity) < 0)
return (-1);
return 0;
}