comparison old-trunk/lwasm/main.c @ 339:eb230fa7d28e

Prepare for migration to hg
author lost
date Fri, 19 Mar 2010 02:54:14 +0000
parents
children
comparison
equal deleted inserted replaced
338:e7885b3ee266 339:eb230fa7d28e
1 /*
2 main.c
3 Copyright © 2009 William Astle
4
5 This file is part of LWASM.
6
7 LWASM is free software: you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation, either version 3 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
16
17 You should have received a copy of the GNU General Public License along with
18 this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 Implements the program startup code
22
23 */
24
25 #include <config.h>
26
27 #include <argp.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 #include "lwasm.h"
34
35 extern void pseudo_pragma_real(asmstate_t *as, void *cl, char **optr, int error);
36
37 // command line option handling
38 const char *argp_program_version = "LWASM from " PACKAGE_STRING;
39 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
40
41 static error_t parse_opts(int key, char *arg, struct argp_state *state)
42 {
43 asmstate_t *as = state -> input;
44 char *p;
45
46 switch (key)
47 {
48 case 'o':
49 // output
50 if (as -> outfile)
51 {
52 }
53 as -> outfile = arg;
54 break;
55
56 case 'd':
57 // debug
58 debug_level++;
59 break;
60
61 case 'l':
62 // list
63 if (arg)
64 as -> listfile = arg;
65 else
66 as -> listfile = "-";
67 break;
68
69 case 'b':
70 // decb output
71 as -> outformat = OUTPUT_DECB;
72 break;
73
74 case 'r':
75 // raw binary output
76 as -> outformat = OUTPUT_RAW;
77 break;
78
79 case 0x100:
80 // proprietary object format
81 as -> outformat = OUTPUT_OBJ;
82 break;
83
84 case 'f':
85 // output format
86 if (!strcasecmp(arg, "decb"))
87 as -> outformat = OUTPUT_DECB;
88 else if (!strcasecmp(arg, "raw"))
89 as -> outformat = OUTPUT_RAW;
90 else if (!strcasecmp(arg, "obj"))
91 as -> outformat = OUTPUT_OBJ;
92 else if (!strcasecmp(arg, "os9"))
93 {
94 as -> pragmas |= PRAGMA_DOLLARNOTLOCAL;
95 as -> outformat = OUTPUT_OS9;
96 }
97 else
98 {
99 fprintf(stderr, "Invalid output format: %s\n", arg);
100 exit(1);
101 }
102 break;
103
104 case 'p':
105 // pragmas
106 p = arg;
107 pseudo_pragma_real(as, NULL, &p, 2);
108 if (!p)
109 {
110 fprintf(stderr, "Invalid pragma string: %s\n", arg);
111 exit(1);
112 }
113 break;
114
115 case '9':
116 as -> no6309 = 1;
117 break;
118
119 case '3':
120 as -> no6309 = 0;
121 break;
122
123 case ARGP_KEY_END:
124 // done; sanity check
125 if (!as -> outfile)
126 as -> outfile = "a.out";
127 break;
128
129 case ARGP_KEY_ARG:
130 // non-option arg
131 if (as -> infile)
132 argp_usage(state);
133 as -> infile = arg;
134 break;
135
136 default:
137 return ARGP_ERR_UNKNOWN;
138 }
139 return 0;
140 }
141
142 static struct argp_option options[] =
143 {
144 { "output", 'o', "FILE", 0,
145 "Output to FILE"},
146 { "debug", 'd', 0, 0,
147 "Set debug mode"},
148 { "format", 'f', "TYPE", 0,
149 "Select output format: decb, raw, obj, os9"},
150 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL,
151 "Generate list [to FILE]"},
152 { "decb", 'b', 0, 0,
153 "Generate DECB .bin format output, equivalent of --format=decb"},
154 { "raw", 'r', 0, 0,
155 "Generate raw binary format output, equivalent of --format=raw"},
156 { "obj", 0x100, 0, 0,
157 "Generate proprietary object file format for later linking, equivalent of --format=obj" },
158 { "pragma", 'p', "PRAGMA", 0,
159 "Set an assembler pragma to any value understood by the \"pragma\" pseudo op"},
160 { "6809", '9', 0, 0,
161 "Set assembler to 6809 only mode" },
162 { "6309", '3', 0, 0,
163 "Set assembler to 6309 mode (default)" },
164 { 0 }
165 };
166
167 static struct argp argp =
168 {
169 options,
170 parse_opts,
171 "<input file>",
172 "LWASM, a HD6309 and MC6809 cross-assembler"
173 };
174
175 char *program_name;
176
177 // main function; parse command line, set up assembler state, and run the
178 // assembler on the first file
179 int main(int argc, char **argv)
180 {
181 asmstate_t asmstate;
182
183 program_name = argv[0];
184
185 argp_parse(&argp, argc, argv, 0, 0, &asmstate);
186
187 if (!asmstate.infile)
188 {
189 fprintf(stderr, "No input files specified.\n");
190 exit(1);
191 }
192
193 // lose the output file if it already exists...
194 unlink(asmstate.outfile);
195
196 exit(0);
197 }
198