annotate src/main.c @ 294:0f488febdc2b

command line options
author lost
date Sat, 17 Jan 2009 16:55:53 +0000
parents f14e82afdac7
children 14d835cf02d9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
294
0f488febdc2b command line options
lost
parents: 293
diff changeset
1 /*
0f488febdc2b command line options
lost
parents: 293
diff changeset
2 main.c
0f488febdc2b command line options
lost
parents: 293
diff changeset
3 Copyright © 2008 William Astle
0f488febdc2b command line options
lost
parents: 293
diff changeset
4
0f488febdc2b command line options
lost
parents: 293
diff changeset
5 This file is part of LWLINK.
0f488febdc2b command line options
lost
parents: 293
diff changeset
6
0f488febdc2b command line options
lost
parents: 293
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
0f488febdc2b command line options
lost
parents: 293
diff changeset
8 terms of the GNU General Public License as published by the Free Software
0f488febdc2b command line options
lost
parents: 293
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
0f488febdc2b command line options
lost
parents: 293
diff changeset
10 version.
0f488febdc2b command line options
lost
parents: 293
diff changeset
11
0f488febdc2b command line options
lost
parents: 293
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
0f488febdc2b command line options
lost
parents: 293
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0f488febdc2b command line options
lost
parents: 293
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
0f488febdc2b command line options
lost
parents: 293
diff changeset
15 more details.
0f488febdc2b command line options
lost
parents: 293
diff changeset
16
0f488febdc2b command line options
lost
parents: 293
diff changeset
17 You should have received a copy of the GNU General Public License along with
0f488febdc2b command line options
lost
parents: 293
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
0f488febdc2b command line options
lost
parents: 293
diff changeset
19
0f488febdc2b command line options
lost
parents: 293
diff changeset
20
0f488febdc2b command line options
lost
parents: 293
diff changeset
21 Implements the program startup code
0f488febdc2b command line options
lost
parents: 293
diff changeset
22
0f488febdc2b command line options
lost
parents: 293
diff changeset
23 */
0f488febdc2b command line options
lost
parents: 293
diff changeset
24
0f488febdc2b command line options
lost
parents: 293
diff changeset
25 #ifdef HAVE_CONFIG_H
0f488febdc2b command line options
lost
parents: 293
diff changeset
26 #include "config.h"
0f488febdc2b command line options
lost
parents: 293
diff changeset
27 #endif
0f488febdc2b command line options
lost
parents: 293
diff changeset
28
0f488febdc2b command line options
lost
parents: 293
diff changeset
29 #include <argp.h>
0f488febdc2b command line options
lost
parents: 293
diff changeset
30 #include <errno.h>
0f488febdc2b command line options
lost
parents: 293
diff changeset
31 #include <stdio.h>
0f488febdc2b command line options
lost
parents: 293
diff changeset
32 #include <stdlib.h>
0f488febdc2b command line options
lost
parents: 293
diff changeset
33
0f488febdc2b command line options
lost
parents: 293
diff changeset
34 #include "lwlink.h"
0f488febdc2b command line options
lost
parents: 293
diff changeset
35
0f488febdc2b command line options
lost
parents: 293
diff changeset
36 // command line option handling
0f488febdc2b command line options
lost
parents: 293
diff changeset
37 const char *argp_program_version = PACKAGE_STRING;
0f488febdc2b command line options
lost
parents: 293
diff changeset
38 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
0f488febdc2b command line options
lost
parents: 293
diff changeset
39
0f488febdc2b command line options
lost
parents: 293
diff changeset
40 static error_t parse_opts(int key, char *arg, struct argp_state *state)
0f488febdc2b command line options
lost
parents: 293
diff changeset
41 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
42 switch (key)
0f488febdc2b command line options
lost
parents: 293
diff changeset
43 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
44 case 'o':
0f488febdc2b command line options
lost
parents: 293
diff changeset
45 // output
0f488febdc2b command line options
lost
parents: 293
diff changeset
46 if (outfile)
0f488febdc2b command line options
lost
parents: 293
diff changeset
47 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
48 }
0f488febdc2b command line options
lost
parents: 293
diff changeset
49 outfile = arg;
0f488febdc2b command line options
lost
parents: 293
diff changeset
50 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
51
0f488febdc2b command line options
lost
parents: 293
diff changeset
52 case 'd':
0f488febdc2b command line options
lost
parents: 293
diff changeset
53 // debug
0f488febdc2b command line options
lost
parents: 293
diff changeset
54 debug_level++;
0f488febdc2b command line options
lost
parents: 293
diff changeset
55 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
56
0f488febdc2b command line options
lost
parents: 293
diff changeset
57 case 'b':
0f488febdc2b command line options
lost
parents: 293
diff changeset
58 // decb output
0f488febdc2b command line options
lost
parents: 293
diff changeset
59 outformat = OUTPUT_DECB;
0f488febdc2b command line options
lost
parents: 293
diff changeset
60 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
61
0f488febdc2b command line options
lost
parents: 293
diff changeset
62 case 'r':
0f488febdc2b command line options
lost
parents: 293
diff changeset
63 // raw binary output
0f488febdc2b command line options
lost
parents: 293
diff changeset
64 outformat = OUTPUT_RAW;
0f488febdc2b command line options
lost
parents: 293
diff changeset
65 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
66
0f488febdc2b command line options
lost
parents: 293
diff changeset
67 case 'f':
0f488febdc2b command line options
lost
parents: 293
diff changeset
68 // output format
0f488febdc2b command line options
lost
parents: 293
diff changeset
69 if (!strcasecmp(arg, "decb"))
0f488febdc2b command line options
lost
parents: 293
diff changeset
70 outformat = OUTPUT_DECB;
0f488febdc2b command line options
lost
parents: 293
diff changeset
71 else if (!strcasecmp(arg, "raw"))
0f488febdc2b command line options
lost
parents: 293
diff changeset
72 outformat = OUTPUT_RAW;
0f488febdc2b command line options
lost
parents: 293
diff changeset
73 else
0f488febdc2b command line options
lost
parents: 293
diff changeset
74 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
75 fprintf(stderr, "Invalid output format: %s\n", arg);
0f488febdc2b command line options
lost
parents: 293
diff changeset
76 exit(1);
0f488febdc2b command line options
lost
parents: 293
diff changeset
77 }
0f488febdc2b command line options
lost
parents: 293
diff changeset
78 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
79 case ARGP_KEY_END:
0f488febdc2b command line options
lost
parents: 293
diff changeset
80 // done; sanity check
0f488febdc2b command line options
lost
parents: 293
diff changeset
81 if (!outfile)
0f488febdc2b command line options
lost
parents: 293
diff changeset
82 outfile = "a.out";
0f488febdc2b command line options
lost
parents: 293
diff changeset
83 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
84
0f488febdc2b command line options
lost
parents: 293
diff changeset
85 case ARGP_KEY_ARG:
0f488febdc2b command line options
lost
parents: 293
diff changeset
86 // FIXME: input files!
0f488febdc2b command line options
lost
parents: 293
diff changeset
87 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
88
0f488febdc2b command line options
lost
parents: 293
diff changeset
89 default:
0f488febdc2b command line options
lost
parents: 293
diff changeset
90 return ARGP_ERR_UNKNOWN;
0f488febdc2b command line options
lost
parents: 293
diff changeset
91 }
0f488febdc2b command line options
lost
parents: 293
diff changeset
92 return 0;
0f488febdc2b command line options
lost
parents: 293
diff changeset
93 }
0f488febdc2b command line options
lost
parents: 293
diff changeset
94
0f488febdc2b command line options
lost
parents: 293
diff changeset
95 static struct argp_option options[] =
0f488febdc2b command line options
lost
parents: 293
diff changeset
96 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
97 { "output", 'o', "FILE", 0,
0f488febdc2b command line options
lost
parents: 293
diff changeset
98 "Output to FILE"},
0f488febdc2b command line options
lost
parents: 293
diff changeset
99 { "debug", 'd', 0, 0,
0f488febdc2b command line options
lost
parents: 293
diff changeset
100 "Set debug mode"},
0f488febdc2b command line options
lost
parents: 293
diff changeset
101 { "format", 'f', "TYPE", 0,
0f488febdc2b command line options
lost
parents: 293
diff changeset
102 "Select output format: decb, raw, obj"},
0f488febdc2b command line options
lost
parents: 293
diff changeset
103 { "decb", 'b', 0, 0,
0f488febdc2b command line options
lost
parents: 293
diff changeset
104 "Generate DECB .bin format output, equivalent of --format=decb"},
0f488febdc2b command line options
lost
parents: 293
diff changeset
105 { "raw", 'r', 0, 0,
0f488febdc2b command line options
lost
parents: 293
diff changeset
106 "Generate raw binary format output, equivalent of --format=raw"},
0f488febdc2b command line options
lost
parents: 293
diff changeset
107 { 0 }
0f488febdc2b command line options
lost
parents: 293
diff changeset
108 };
0f488febdc2b command line options
lost
parents: 293
diff changeset
109
0f488febdc2b command line options
lost
parents: 293
diff changeset
110 static struct argp argp =
0f488febdc2b command line options
lost
parents: 293
diff changeset
111 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
112 options,
0f488febdc2b command line options
lost
parents: 293
diff changeset
113 parse_opts,
0f488febdc2b command line options
lost
parents: 293
diff changeset
114 "<input file> ...",
0f488febdc2b command line options
lost
parents: 293
diff changeset
115 "LWLINK, a HD6309 and MC6809 cross-linker"
0f488febdc2b command line options
lost
parents: 293
diff changeset
116 };
0f488febdc2b command line options
lost
parents: 293
diff changeset
117
0f488febdc2b command line options
lost
parents: 293
diff changeset
118 // main function; parse command line, set up assembler state, and run the
0f488febdc2b command line options
lost
parents: 293
diff changeset
119 // assembler on the first file
0f488febdc2b command line options
lost
parents: 293
diff changeset
120 int main(int argc, char **argv)
0f488febdc2b command line options
lost
parents: 293
diff changeset
121 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
122 argp_parse(&argp, argc, argv, 0, 0, NULL);
0f488febdc2b command line options
lost
parents: 293
diff changeset
123
0f488febdc2b command line options
lost
parents: 293
diff changeset
124 exit(0);
0f488febdc2b command line options
lost
parents: 293
diff changeset
125 }