-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpl0dcMain.c
82 lines (77 loc) · 2.36 KB
/
pl0dcMain.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
75
76
77
78
79
80
81
82
/**
* @file pl0dcMain.c
*/
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#ifdef YACC
extern FILE *yyin;
int yylex(void);
void yyerror(const char* s);
# include "y.tab.c"
#else
# include "getSource.h"
#endif
int compile(FILE *fout);
#ifdef YACC
void yyerror(const char* s) {
fprintf(stderr, "error: %s\n", s);
}
#endif
static void printUsage(const char *argv0) {
#ifdef YACC
printf("Usage: %s [-h] [-o <output_file>] <input_file>\n", argv0);
#else
printf("Usage: %s [-g] [-h] [-o <output_file>] <input_file>\n", argv0);
#endif
}
int main(int argc, char *argv[]) {
char outFileName[FILENAME_MAX] = "a.out"; /* 出力プログラムファイル名 */
FILE *fout = NULL;
int opt;
/* コマンドラインオプションを解釈 */
#ifdef YACC
while ((opt = getopt(argc, argv, "o:h")) != -1) {
switch (opt) {
case 'o': strcpy(outFileName, optarg); break; /* 出力ファイル名取得 */
case 'h': printUsage(argv[0]); return 0; /* ヘルプ表示 */
default: printUsage(argv[0]); return 1; /* 無効なオプション */
}
}
#else
int outHtml = 0;
while ((opt = getopt(argc, argv, "o:gh")) != -1) {
switch (opt) {
case 'o': strcpy(outFileName, optarg); break; /* 出力ファイル名取得 */
case 'g': outHtml = 1; break;
case 'h': printUsage(argv[0]); return 0; /* ヘルプ表示 */
default: printUsage(argv[0]); return 1; /* 無効なオプション */
}
}
#endif
if (optind >= argc) {
fprintf(stderr, "%s: no input file\n", argv[0]);
printUsage(argv[0]);
return 1;
}
if ((fout = fopen(outFileName, "w")) == NULL) { /* 出力ファイルを作る */
printf("can't open %s\n", outFileName);
return 1;
}
#ifdef YACC
if ((yyin = fopen(argv[optind], "r")) == NULL) {
printf("can't open %s\n", argv[optind]);
return 1;
}
yyparse();
listCode(fout, 1);
fclose(yyin);
#else
if (!openSource(argv[optind], outHtml)) /* ソースプログラムファイルのopen */
return 1; /* openに失敗すれば終わり */
compile(fout); /* コンパイル */
closeSource(); /* ソースプログラムファイルのclose */
#endif
fclose(fout);
return 0;
}