Mercurial > hg-old > index.cgi
annotate src/main.c @ 12:9ef54d49febc 1.0
Bumped version number
author | lost |
---|---|
date | Sun, 04 Jan 2009 05:45:30 +0000 |
parents | 287a6905a63c |
children | 05d4115b4860 |
rev | line source |
---|---|
0 | 1 /* |
2 main.c | |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
3 Copyright © 2008 William Astle |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
4 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
5 This file is part of LWASM. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
6 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
10 version. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
11 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
15 more details. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
16 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
19 |
0 | 20 |
21 Implements the program startup code | |
22 | |
23 */ | |
24 | |
5
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
25 #include "config.h" |
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
26 |
0 | 27 #include <argp.h> |
28 #include <errno.h> | |
29 #include <stdio.h> | |
30 #include <stdlib.h> | |
31 | |
32 #include "lwasm.h" | |
33 | |
34 // external declarations | |
35 extern void resolve_phasing(asmstate_t *as); | |
36 extern void generate_code(asmstate_t *as); | |
37 extern void list_code(asmstate_t *as); | |
38 extern void write_code(asmstate_t *as); | |
39 | |
40 | |
41 // command line option handling | |
5
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
42 const char *argp_program_version = PACKAGE_STRING; |
287a6905a63c
Moved config.h to src/ and switched version strings to use autotools version
lost
parents:
4
diff
changeset
|
43 const char *argp_program_bug_address = PACKAGE_BUGREPORT; |
0 | 44 |
45 static error_t parse_opts(int key, char *arg, struct argp_state *state) | |
46 { | |
47 asmstate_t *as = state -> input; | |
48 | |
49 switch (key) | |
50 { | |
51 case 'o': | |
52 // output | |
53 if (as -> outfile) | |
54 { | |
55 } | |
56 as -> outfile = arg; | |
57 break; | |
58 | |
59 case 'd': | |
60 // debug | |
61 as -> debug++; | |
62 break; | |
63 | |
64 case 'l': | |
65 // list | |
66 if (arg) | |
67 as -> listfile = arg; | |
68 else | |
69 as -> listfile = "-"; | |
70 break; | |
71 | |
72 case 'b': | |
73 // decb output | |
74 as -> outformat = OUTPUT_DECB; | |
75 break; | |
76 | |
77 case 'r': | |
78 // raw binary output | |
79 as -> outformat = OUTPUT_RAW; | |
80 break; | |
81 | |
82 case ARGP_KEY_END: | |
83 // done; sanity check | |
84 if (!as -> outfile) | |
85 as -> outfile = "a.out"; | |
86 break; | |
87 | |
88 case ARGP_KEY_ARG: | |
89 // non-option arg | |
90 if (as -> infile) | |
91 argp_usage(state); | |
92 as -> infile = arg; | |
93 break; | |
94 | |
95 default: | |
96 return ARGP_ERR_UNKNOWN; | |
97 } | |
98 return 0; | |
99 } | |
100 | |
101 static struct argp_option options[] = | |
102 { | |
103 { "output", 'o', "FILE", 0, | |
104 "Output to FILE"}, | |
105 { "debug", 'd', 0, 0, | |
106 "Set debug mode"}, | |
107 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL, | |
108 "Generate list [to FILE]"}, | |
109 { "decb", 'b', 0, 0, | |
110 "Generate DECB .bin format output"}, | |
111 { "raw", 'r', 0, 0, | |
112 "Generate raw binary format output"}, | |
113 { "rawrel", 0, 0, 0, | |
114 "Generate raw binary respecing ORG statements as offsets from the start of the file"}, | |
115 { 0 } | |
116 }; | |
117 | |
118 static struct argp argp = | |
119 { | |
120 options, | |
121 parse_opts, | |
122 "<input file>", | |
123 "LWASM, a HD6309 and MC6809 cross-assembler" | |
124 }; | |
125 | |
126 // main function; parse command line, set up assembler state, and run the | |
127 // assembler on the first file | |
128 int main(int argc, char **argv) | |
129 { | |
130 // assembler state | |
131 asmstate_t asmstate = { 0 }; | |
132 | |
133 argp_parse(&argp, argc, argv, 0, 0, &asmstate); | |
134 if (!asmstate.listfile) | |
135 asmstate.listfile = "-"; | |
136 | |
137 // printf("Assembling %s to %s; list to %s\n", asmstate.infile, asmstate.outfile, asmstate.listfile); | |
138 | |
139 /* pass 1 - collect the symbols and assign addresses where possible */ | |
140 /* pass 1 also resolves included files, etc. */ | |
141 /* that means files are read exactly once unless included multiple times */ | |
142 lwasm_read_file(&asmstate, (char *)(asmstate.infile)); | |
143 | |
144 // pass 2: actually generate the code; if any phasing errors appear | |
145 // at this stage, we have a bug | |
146 generate_code(&asmstate); | |
147 | |
148 // now make a pretty listing | |
149 list_code(&asmstate); | |
150 | |
151 // now write the code out to the output file | |
152 write_code(&asmstate); | |
153 | |
154 if (asmstate.errorcount > 0) | |
155 exit(1); | |
156 | |
157 exit(0); | |
158 } | |
159 |