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 |
e70feb8c | 26 | analyze_AXIOME(); |
48e4b4ad | 27 | fputws(L"</html>\n", target); |
4b580abd JB |
28 | } |
29 | ||
9ed84d89 JB |
30 | void print_usage(const char* name) { |
31 | fprintf(stdout,"Usage: %s [options]\n" | |
32 | "Where [options] are:\n" | |
f491c07a | 33 | " -h, --help: display this help message\n" |
9ed84d89 JB |
34 | " -l, --lexical-only: do only the lexical analysis\n" |
35 | " -i, --input<filename>: use <filename> as input file instead of standard input\n" | |
36 | " -o, --output<filename>: use <filename> as output file instead of standard output\n", | |
37 | name); | |
48e4b4ad | 38 | fflush(stdout); |
9ed84d89 | 39 | } |
4b580abd | 40 | |
9ed84d89 JB |
41 | int main(int argc, char **argv) { |
42 | /* In and out files name */ | |
43 | const char* in_file = NULL; | |
44 | const char* out_file = NULL; | |
45 | static int hflag = 0; | |
46 | static int lflag = 0; | |
47 | ||
48 | /* getopt_long stores the option index here. */ | |
49 | int option_index = 0; | |
50 | ||
51 | static struct option long_options[] = | |
52 | { | |
53 | {"help", no_argument, &hflag, 1}, | |
54 | {"input", optional_argument, NULL, 'i'}, | |
55 | {"lexical-only", no_argument, &lflag, 1}, | |
56 | {"output", optional_argument, NULL, 'o'}, | |
57 | {0, 0, 0, 0} | |
58 | }; | |
59 | ||
60 | int c_in; | |
61 | ||
62 | while ((c_in = getopt_long(argc, argv, "hi::lo::", long_options, | |
63 | &option_index)) != -1) { | |
64 | switch (c_in) { | |
65 | case 'h': | |
66 | hflag = 1; | |
67 | break; | |
68 | case 'i': | |
69 | if (optarg != NULL) { | |
70 | in_file = optarg; | |
71 | } | |
72 | break; | |
73 | case 'l': | |
74 | lflag = 1; | |
75 | break; | |
76 | case 'o': | |
77 | if (optarg != NULL) { | |
78 | out_file = optarg; | |
79 | } | |
80 | break; | |
81 | case 0: | |
82 | /* getopt_long() set a variable, just keep going */ | |
83 | break; | |
84 | case ':': | |
85 | /* missing option argument */ | |
86 | pr_error("%s: option '-%c' requires an argument\n", | |
25696723 | 87 | argv[0], optopt); |
9ed84d89 JB |
88 | break; |
89 | case '?': | |
90 | default: | |
91 | /* invalid option */ | |
92 | pr_error("%s: option '-%c' is invalid: ignored\n", | |
25696723 | 93 | argv[0], optopt); |
5c1346dd JB |
94 | /* print the help message for invalid options */ |
95 | hflag = 1; | |
9ed84d89 JB |
96 | break; |
97 | } | |
d3eb30ef | 98 | } |
4b580abd | 99 | |
9ed84d89 JB |
100 | if (in_file != NULL) { |
101 | // Ouvre le fichier source en lecture seulement (le fichier doit exister) : | |
102 | source = fopen(in_file, "r+"); | |
103 | if (source == NULL) { | |
224a9916 | 104 | pr_error("Fail to open file %s\n", in_file); |
9ed84d89 JB |
105 | return EXIT_FAILURE; |
106 | } | |
107 | } else { | |
108 | source = stdin; | |
4b580abd JB |
109 | } |
110 | ||
9ed84d89 JB |
111 | if (out_file != NULL) { |
112 | // Cree et ouvre le fichier cible en lecture/ecriture | |
113 | // avec suppression du contenu au prealable : | |
114 | target = fopen(out_file, "w+"); | |
115 | if (target == NULL) { | |
224a9916 | 116 | pr_error("Fail to open file %s\n", out_file); |
9ed84d89 JB |
117 | return EXIT_FAILURE; |
118 | } | |
119 | } else { | |
120 | target = stdout; | |
96964f3e JB |
121 | } |
122 | ||
9ed84d89 JB |
123 | if (hflag) { |
124 | print_usage(argv[0]); | |
f8c1adc1 | 125 | } else if (lflag) { |
9ed84d89 JB |
126 | do_lexical_analysis(); |
127 | } else { | |
128 | do_syntactic_analysis(); | |
96964f3e JB |
129 | } |
130 | ||
96964f3e JB |
131 | if (source != NULL) fclose(source); // fermeture du fichier source |
132 | if (target != NULL) fclose(target); // fermeture du fichier target | |
133 | ||
b5cd8f86 | 134 | return EXIT_SUCCESS; |
96964f3e | 135 | } |