Add a small Hello World program to be assembled
[zilutils.git] / zilc / main.c
1 /*
2  * main.c
3  *
4  * Copyright (C) 2015 Alexander Andrejevic <theflash AT sdf DOT lonestar DOT org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as
8  * published by the Free Software Foundation, either version 3 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>
18  *
19  * SPDX-License-Identifier: AGPL-3.0-or-later
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <getopt.h>
25
26 int main(int argc, char *argv[], char *envp[])
27 {
28     int verbose = 0;
29     extern FILE *yyin;
30
31     while (1)
32     {
33         int option_index;
34         static struct option long_options[] =
35         {
36             { "verbose", no_argument, NULL, 'v'},
37             { NULL,      0,           NULL, 0  }
38         };
39
40         int choice = getopt_long(argc, argv, "v", long_options, &option_index);
41         if (choice == -1) break;
42
43         switch (choice)
44         {
45         case 'v':
46             verbose++;
47             break;
48         }
49     }
50
51     if (optind == argc)
52     {
53         fprintf(stderr, "No input files specified.\n");
54         return EXIT_FAILURE;
55     }
56
57     while (optind < argc)
58     {
59         char *filename = argv[optind++];
60
61         yyin = fopen(filename, "r");
62         if (yyin == NULL)
63         {
64             fprintf(stderr, "Cannot open input file: %s\n", filename);
65             continue;
66         }
67
68         switch (yyparse())
69         {
70         case 0:
71             if (verbose) fprintf(stdout, "%s: Done parsing.\n", filename);
72             break;
73
74         case 1:
75             fprintf(stderr, "%s: errors encountered.", filename);
76             break;
77
78         case 2:
79             fprintf(stderr, "%s: out of memory.\n", filename);
80             break;
81         }
82     }
83
84     return EXIT_SUCCESS;
85 }