annotate lwlink/trunk/src/main.c @ 112:a567dbb3f1d4

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