annotate lwbasic/main.c @ 159:8967eb907324

Trap negative sizes for [rz]m[bdq] and flag error
author lost@l-w.ca
date Sun, 28 Aug 2011 14:16:45 -0600
parents bcd532a90e53
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
1 /*
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
2 main.c
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
3
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
4 Copyright © 2011 William Astle
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
5
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
6 This file is part of LWTOOLS.
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
7
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
11 version.
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
12
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
16 more details.
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
17
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
20 */
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
21
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
22 /*
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
23 main program startup handling for lwbasic
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
24 */
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
25
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
26 #include <stdlib.h>
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
27 #include <stdio.h>
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
28 #include <stdarg.h>
22
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
29
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
30 #include <lw_cmdline.h>
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
31 #include <lw_string.h>
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
32 #include <lw_alloc.h>
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
33
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
34 #define __main_c_seen__
22
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
35 #include "lwbasic.h"
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
36
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
37 #define PROGVER "lwbasic from " PACKAGE_STRING
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
38
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
39 static struct lw_cmdline_options options[] =
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
40 {
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
41 { "output", 'o', "FILE", 0, "Output to FILE"},
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
42 { "debug", 'd', "LEVEL", lw_cmdline_opt_optional, "Set debug mode"},
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
43 { 0 }
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
44 };
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
45
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
46 static int parse_opts(int key, char *arg, void *data)
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
47 {
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
48 cstate *state = data;
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
49
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
50 switch (key)
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
51 {
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
52 case 'o':
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
53 if (state -> output_file)
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
54 lw_free(state -> output_file);
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
55 state -> output_file = lw_strdup(arg);
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
56 break;
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
57
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
58 case 'd':
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
59 if (!arg)
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
60 state -> debug_level = 50;
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
61 else
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
62 state -> debug_level = atoi(arg);
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
63 break;
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
64
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
65 case lw_cmdline_key_end:
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
66 return 0;
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
67
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
68 case lw_cmdline_key_arg:
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
69 if (state -> input_file)
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
70 {
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
71 fprintf(stderr, "Already have an input file; ignoring %s\n", arg);
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
72 }
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
73 else
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
74 {
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
75 state -> input_file = lw_strdup(arg);
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
76 }
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
77 break;
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
78
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
79 default:
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
80 return lw_cmdline_err_unknown;
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
81 }
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
82
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
83 return 0;
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
84 }
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
85
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
86 static struct lw_cmdline_parser cmdline_parser =
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
87 {
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
88 options,
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
89 parse_opts,
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
90 "INPUTFILE",
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
91 "lwbasic, a compiler for a dialect of Basic\vPlease report bugs to lost@l-w.ca.",
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
92 PROGVER
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
93 };
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
94
30
bcd532a90e53 Renamed "compiler" to "parser" for more consistent terminology
lost@l-w.ca
parents: 25
diff changeset
95 extern void parser(cstate *state);
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
96
22
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
97 int main(int argc, char **argv)
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
98 {
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
99 cstate state = { 0 };
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
100
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
101 lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, &state);
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
102
30
bcd532a90e53 Renamed "compiler" to "parser" for more consistent terminology
lost@l-w.ca
parents: 25
diff changeset
103 parser(&state);
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
104
22
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
105 exit(0);
7c35fa8dbc91 Added initial framework for lwbasic
lost@l-w.ca
parents:
diff changeset
106 }
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
107
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
108 void lwb_error(const char *fmt, ...)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
109 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
110 va_list args;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
111
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
112 va_start(args, fmt);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
113 vfprintf(stderr, fmt, args);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
114 va_end(args);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
115
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
116 exit(1);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 22
diff changeset
117 }