Commit | Line | Data |
---|---|---|
96964f3e JB |
1 | #include <stdlib.h> |
2 | #include <stdio.h> | |
3 | #include <string.h> | |
4b580abd | 4 | #include <stdbool.h> |
9ed84d89 | 5 | #include <getopt.h> |
e9a51b68 | 6 | |
9ed84d89 JB |
7 | #include "global_vars.h" |
8 | #include "lexical_analyzer.h" | |
9 | #include "syntactic_analyzer.h" | |
10 | #include "print_helper.h" | |
6a19b8fe | 11 | |
9ed84d89 JB |
12 | void do_lexical_analysis() { |
13 | c = fgetwc(source); // lecture du premier caractere | |
14 | do { | |
15 | scanner(); | |
f196dae5 | 16 | wprint_token(target); |
9ed84d89 JB |
17 | token[tokenFound].type = tokenTypestr[tokenType]; |
18 | tokenFound++; | |
19 | } while (tokenType != FIN); // tant que la fin du fichier n'est pas atteinte | |
4b580abd JB |
20 | } |
21 | ||
9ed84d89 | 22 | void do_syntactic_analysis() { |
48e4b4ad JB |
23 | fputws(L"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fr_FR\" lang=\"fr_FR\">\n", |
24 | target); | |
9ed84d89 | 25 | c = fgetwc(source); // lecture du premier caractere |
15ad4b5a | 26 | scanner(); |
e70feb8c | 27 | analyze_AXIOME(); |
48e4b4ad | 28 | fputws(L"</html>\n", target); |
4b580abd JB |
29 | } |
30 | ||
9ed84d89 JB |
31 | void print_usage(const char* name) { |
32 | fprintf(stdout,"Usage: %s [options]\n" | |
33 | "Where [options] are:\n" | |
f491c07a | 34 | " -h, --help: display this help message\n" |
9ed84d89 JB |
35 | " -l, --lexical-only: do only the lexical analysis\n" |
36 | " -i, --input<filename>: use <filename> as input file instead of standard input\n" | |
37 | " -o, --output<filename>: use <filename> as output file instead of standard output\n", | |
38 | name); | |
48e4b4ad | 39 | fflush(stdout); |
9ed84d89 | 40 | } |
4b580abd | 41 | |
9ed84d89 JB |
42 | int main(int argc, char **argv) { |
43 | /* In and out files name */ | |
44 | const char* in_file = NULL; | |
45 | const char* out_file = NULL; | |
46 | static int hflag = 0; | |
47 | static int lflag = 0; | |
48 | ||
49 | /* getopt_long stores the option index here. */ | |
50 | int option_index = 0; | |
51 | ||
52 | static struct option long_options[] = | |
53 | { | |
54 | {"help", no_argument, &hflag, 1}, | |
55 | {"input", optional_argument, NULL, 'i'}, | |
56 | {"lexical-only", no_argument, &lflag, 1}, | |
57 | {"output", optional_argument, NULL, 'o'}, | |
58 | {0, 0, 0, 0} | |
59 | }; | |
60 | ||
61 | int c_in; | |
62 | ||
63 | while ((c_in = getopt_long(argc, argv, "hi::lo::", long_options, | |
64 | &option_index)) != -1) { | |
65 | switch (c_in) { | |
66 | case 'h': | |
67 | hflag = 1; | |
68 | break; | |
69 | case 'i': | |
70 | if (optarg != NULL) { | |
71 | in_file = optarg; | |
72 | } | |
73 | break; | |
74 | case 'l': | |
75 | lflag = 1; | |
76 | break; | |
77 | case 'o': | |
78 | if (optarg != NULL) { | |
79 | out_file = optarg; | |
80 | } | |
81 | break; | |
82 | case 0: | |
83 | /* getopt_long() set a variable, just keep going */ | |
84 | break; | |
85 | case ':': | |
86 | /* missing option argument */ | |
87 | pr_error("%s: option '-%c' requires an argument\n", | |
25696723 | 88 | argv[0], optopt); |
9ed84d89 JB |
89 | break; |
90 | case '?': | |
91 | default: | |
92 | /* invalid option */ | |
93 | pr_error("%s: option '-%c' is invalid: ignored\n", | |
25696723 | 94 | argv[0], optopt); |
5c1346dd JB |
95 | /* print the help message for invalid options */ |
96 | hflag = 1; | |
9ed84d89 JB |
97 | break; |
98 | } | |
d3eb30ef | 99 | } |
4b580abd | 100 | |
9ed84d89 JB |
101 | if (in_file != NULL) { |
102 | // Ouvre le fichier source en lecture seulement (le fichier doit exister) : | |
103 | source = fopen(in_file, "r+"); | |
104 | if (source == NULL) { | |
224a9916 | 105 | pr_error("Fail to open file %s\n", in_file); |
9ed84d89 JB |
106 | return EXIT_FAILURE; |
107 | } | |
108 | } else { | |
109 | source = stdin; | |
4b580abd JB |
110 | } |
111 | ||
9ed84d89 JB |
112 | if (out_file != NULL) { |
113 | // Cree et ouvre le fichier cible en lecture/ecriture | |
114 | // avec suppression du contenu au prealable : | |
115 | target = fopen(out_file, "w+"); | |
116 | if (target == NULL) { | |
224a9916 | 117 | pr_error("Fail to open file %s\n", out_file); |
9ed84d89 JB |
118 | return EXIT_FAILURE; |
119 | } | |
120 | } else { | |
121 | target = stdout; | |
96964f3e JB |
122 | } |
123 | ||
9ed84d89 JB |
124 | if (hflag) { |
125 | print_usage(argv[0]); | |
f8c1adc1 | 126 | } else if (lflag) { |
9ed84d89 JB |
127 | do_lexical_analysis(); |
128 | } else { | |
129 | do_syntactic_analysis(); | |
96964f3e JB |
130 | } |
131 | ||
96964f3e JB |
132 | if (source != NULL) fclose(source); // fermeture du fichier source |
133 | if (target != NULL) fclose(target); // fermeture du fichier target | |
134 | ||
b5cd8f86 | 135 | return EXIT_SUCCESS; |
96964f3e | 136 | } |