Reorder includes.
[TP_AL_C.git] / lexer / main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdbool.h>
5 #include <getopt.h>
6
7 #include "global_vars.h"
8 #include "print_helper.h"
9 #include "lexical_analyzer.h"
10 #include "syntactic_analyzer.h"
11
12 static void do_lexical_analysis() {
13 c = fgetwc(source); // lecture du premier caractere
14 do {
15 scanner();
16 wprint_token(target);
17 token[tokenFound].type = tokenTypestr[tokenType];
18 tokenFound++;
19 } while (tokenType != FIN); // tant que la fin du fichier n'est pas atteinte
20 }
21
22 static void do_syntactic_analysis() {
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);
25 c = fgetwc(source); // lecture du premier caractere
26 scanner();
27 analyze_AXIOME();
28 fputws(L"</html>\n", target);
29 }
30
31 static void print_usage(const char* name) {
32 fprintf(stdout,"Usage: %s [options]\n"
33 "Where [options] are:\n"
34 " -h, --help: display this help message\n"
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);
39 fflush(stdout);
40 }
41
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",
88 argv[0], optopt);
89 break;
90 case '?':
91 default:
92 /* invalid option */
93 pr_error("%s: option '-%c' is invalid: ignored\n",
94 argv[0], optopt);
95 /* print the help message for invalid options */
96 hflag = 1;
97 break;
98 }
99 }
100
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) {
105 pr_error("Fail to open file %s\n", in_file);
106 return EXIT_FAILURE;
107 }
108 } else {
109 source = stdin;
110 }
111
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) {
117 pr_error("Fail to open file %s\n", out_file);
118 return EXIT_FAILURE;
119 }
120 } else {
121 target = stdout;
122 }
123
124 if (hflag) {
125 print_usage(argv[0]);
126 } else if (lflag) {
127 do_lexical_analysis();
128 } else {
129 do_syntactic_analysis();
130 }
131
132 if (source != NULL) fclose(source); // fermeture du fichier source
133 if (target != NULL) fclose(target); // fermeture du fichier target
134
135 return EXIT_SUCCESS;
136 }