Fix usage message.
[TP_AL_C.git] / lexer / main.c
CommitLineData
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
12void do_lexical_analysis() {
13 c = fgetwc(source); // lecture du premier caractere
14 do {
15 scanner();
16 if (tokenType == MOT || tokenType == MOTCLE) {
17 fwprintf(target, L"%20s: %ls\n", tokenTypestr[tokenType], token[tokenFound].value);
18 } else {
19 fwprintf(target, L"%20s\n", tokenTypestr[tokenType]);
4b580abd 20 }
9ed84d89
JB
21 token[tokenFound].type = tokenTypestr[tokenType];
22 tokenFound++;
23 } while (tokenType != FIN); // tant que la fin du fichier n'est pas atteinte
4b580abd
JB
24}
25
9ed84d89
JB
26void do_syntactic_analysis() {
27 c = fgetwc(source); // lecture du premier caractere
28 do {
29 analyze_AXIOME();
30 } while (tokenType != FIN);
4b580abd
JB
31}
32
9ed84d89
JB
33void print_usage(const char* name) {
34 fprintf(stdout,"Usage: %s [options]\n"
35 "Where [options] are:\n"
f491c07a 36 " -h, --help: display this help message\n"
9ed84d89
JB
37 " -l, --lexical-only: do only the lexical analysis\n"
38 " -i, --input<filename>: use <filename> as input file instead of standard input\n"
39 " -o, --output<filename>: use <filename> as output file instead of standard output\n",
40 name);
41}
4b580abd 42
9ed84d89
JB
43int main(int argc, char **argv) {
44 /* In and out files name */
45 const char* in_file = NULL;
46 const char* out_file = NULL;
47 static int hflag = 0;
48 static int lflag = 0;
49
50 /* getopt_long stores the option index here. */
51 int option_index = 0;
52
53 static struct option long_options[] =
54 {
55 {"help", no_argument, &hflag, 1},
56 {"input", optional_argument, NULL, 'i'},
57 {"lexical-only", no_argument, &lflag, 1},
58 {"output", optional_argument, NULL, 'o'},
59 {0, 0, 0, 0}
60 };
61
62 int c_in;
63
64 while ((c_in = getopt_long(argc, argv, "hi::lo::", long_options,
65 &option_index)) != -1) {
66 switch (c_in) {
67 case 'h':
68 hflag = 1;
69 break;
70 case 'i':
71 if (optarg != NULL) {
72 in_file = optarg;
73 }
74 break;
75 case 'l':
76 lflag = 1;
77 break;
78 case 'o':
79 if (optarg != NULL) {
80 out_file = optarg;
81 }
82 break;
83 case 0:
84 /* getopt_long() set a variable, just keep going */
85 break;
86 case ':':
87 /* missing option argument */
88 pr_error("%s: option '-%c' requires an argument\n",
89 argv[0], optopt);
90 break;
91 case '?':
92 default:
93 /* invalid option */
94 pr_error("%s: option '-%c' is invalid: ignored\n",
95 argv[0], optopt);
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]);
125 } else if (lflag){
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}