Coding style fixlet.
[TP_AL_C.git] / lexer / main.c
index 8b137891791fe96927ad78e64b0aad7bded08bdc..f2b6df47660f77bb7c43c49b4752a7ea52582966 100644 (file)
@@ -1 +1,135 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+#include <getopt.h>
 
+#include "global_vars.h"
+#include "lexical_analyzer.h"
+#include "syntactic_analyzer.h"
+#include "print_helper.h"
+
+void do_lexical_analysis() {
+    c = fgetwc(source); // lecture du premier caractere
+    do {
+        scanner();
+        wprint_token_target();
+        token[tokenFound].type = tokenTypestr[tokenType];
+        tokenFound++;
+    } while (tokenType != FIN); // tant que la fin du fichier n'est pas atteinte
+}
+
+void do_syntactic_analysis() {
+    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",
+           target);
+    c = fgetwc(source); // lecture du premier caractere
+    analyze_AXIOME();
+    fputws(L"</html>\n", target);
+}
+
+void print_usage(const char* name) {
+    fprintf(stdout,"Usage: %s [options]\n"
+                   "Where [options] are:\n"
+                   " -h, --help: display this help message\n"
+                   " -l, --lexical-only: do only the lexical analysis\n"
+                   " -i, --input<filename>: use <filename> as input file instead of standard input\n"
+                   " -o, --output<filename>: use <filename> as output file instead of standard output\n",
+            name);
+    fflush(stdout);
+}
+
+int main(int argc, char **argv) {
+    /* In and out files name */
+    const char* in_file = NULL;
+    const char* out_file = NULL;
+    static int hflag = 0;
+    static int lflag = 0;
+
+    /* getopt_long stores the option index here. */
+    int option_index = 0;
+
+    static struct option long_options[] =
+    {
+        {"help", no_argument, &hflag, 1},
+        {"input", optional_argument, NULL, 'i'},
+        {"lexical-only", no_argument, &lflag, 1},
+        {"output", optional_argument, NULL, 'o'},
+        {0, 0, 0, 0}
+    };
+
+    int c_in;
+
+    while ((c_in = getopt_long(argc, argv, "hi::lo::", long_options,
+                               &option_index)) != -1) {
+        switch (c_in) {
+            case 'h':
+                hflag = 1;
+                break;
+            case 'i':
+                if (optarg != NULL) {
+                    in_file = optarg;
+                }
+                break;
+            case 'l':
+                lflag = 1;
+                break;
+            case 'o':
+                if (optarg != NULL) {
+                    out_file = optarg;
+                }
+                break;
+            case 0:
+                /* getopt_long() set a variable, just keep going */
+                break;
+            case ':':
+                /* missing option argument */
+                pr_error("%s: option '-%c' requires an argument\n",
+                         argv[0], optopt);
+                break;
+            case '?':
+            default:
+                /* invalid option */
+                pr_error("%s: option '-%c' is invalid: ignored\n",
+                         argv[0], optopt);
+                /* print the help message for invalid options */
+                hflag = 1;
+                break;
+        }
+    }
+
+    if (in_file != NULL) {
+        // Ouvre le fichier source en lecture seulement (le fichier doit exister) :
+        source = fopen(in_file, "r+");
+        if (source == NULL) {
+            pr_error("Fail to open file %s\n", in_file);
+            return EXIT_FAILURE;
+        }
+    } else {
+        source = stdin;
+    }
+
+    if (out_file != NULL) {
+        // Cree et ouvre le fichier cible en lecture/ecriture
+        // avec suppression du contenu au prealable :
+        target = fopen(out_file, "w+");
+        if (target == NULL) {
+            pr_error("Fail to open file %s\n", out_file);
+            return EXIT_FAILURE;
+        }
+    } else {
+        target = stdout;
+    }
+
+    if (hflag) {
+        print_usage(argv[0]);
+    } else if (lflag) {
+        do_lexical_analysis();
+    } else {
+        do_syntactic_analysis();
+    }
+
+    if (source != NULL) fclose(source); // fermeture du fichier source
+    if (target != NULL) fclose(target); // fermeture du fichier target
+
+    return EXIT_SUCCESS;
+}