annotate lwar/main.c @ 196:e7309c716059

Allow relative addressing to ignore a # before the operand (16 bit too!)
author lost
date Sun, 22 Mar 2009 17:58:43 +0000
parents bb2665c7005c
children bae1e3ecdce1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
171
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
1 /*
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
2 main.c
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
4
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
5 This file is part of LWAR.
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
6
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
7 LWAR is free software: you can redistribute it and/or modify it under the
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
10 version.
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
11
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
15 more details.
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
16
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
19
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
20
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
21 Implements the program startup code
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
22
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
23 */
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
24
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
26 #include "config.h"
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
27 #endif
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
28
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
29 #include <argp.h>
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
30 #include <errno.h>
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
31 #include <stdio.h>
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
32 #include <stdlib.h>
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
33 #include <sys/types.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
34 #include <sys/stat.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
35 #include <unistd.h>
171
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
36
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
37 #include "lwar.h"
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
38
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
39 // command line option handling
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
40 const char *argp_program_version = "LWAR from " PACKAGE_STRING;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
41 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
42
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
43 static error_t parse_opts(int key, char *arg, struct argp_state *state)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
44 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
45 switch (key)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
46 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
47 case 'd':
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
48 // debug
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
49 debug_level++;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
50 break;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
51
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
52 case 'a':
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
53 // add members
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
54 operation = LWAR_OP_ADD;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
55 break;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
56
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
57 case 'c':
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
58 // create archive
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
59 operation = LWAR_OP_CREATE;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
60 break;
174
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
61
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
62 case 'm':
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
63 mergeflag = 1;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
64 break;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
65
188
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
66 case 'r':
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
67 // replace members
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
68 operation = LWAR_OP_REPLACE;
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
69 break;
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
70
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
71 case 'l':
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
72 // list members
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
73 operation = LWAR_OP_LIST;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
74 break;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
75
188
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
76 case 'x':
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
77 // extract members
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
78 operation = LWAR_OP_EXTRACT;
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
79 break;
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
80
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
81 case ARGP_KEY_ARG:
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
82 if (archive_file)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
83 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
84 // add archive member to list
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
85 add_file_name(arg);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
86 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
87 else
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
88 archive_file = arg;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
89 break;
171
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
90
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
91 default:
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
92 return ARGP_ERR_UNKNOWN;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
93 }
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
94 return 0;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
95 }
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
96
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
97 static struct argp_option options[] =
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
98 {
188
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
99 { "replace", 'r', 0, 0,
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
100 "Add or replace archive members" },
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
101 { "extract", 'x', 0, 0,
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
102 "Extract members from the archive" },
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
103 { "add", 'a', 0, 0,
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
104 "Add members to the archive" },
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
105 { "list", 'l', 0, 0,
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
106 "List the contents of the archive" },
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
107 { "create", 'c', 0, 0,
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
108 "Create new archive (or truncate existing one)" },
174
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
109 { "merge", 'm', 0, 0,
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
110 "Add the contents of archive arguments instead of the archives themselves" },
171
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
111 { "debug", 'd', 0, 0,
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
112 "Set debug mode"},
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
113 { 0 }
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
114 };
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
115
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
116 static struct argp argp =
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
117 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
118 options,
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
119 parse_opts,
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
120 "<archive> [<file> ...]",
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
121 "LWAR, a library file manager for LWLINK"
171
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
122 };
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
123
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
124 extern void do_list(void);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
125 extern void do_add(void);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
126 extern void do_remove(void);
188
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
127 extern void do_replace(void);
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
128 extern void do_extract(void);
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
129
171
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
130 // main function; parse command line, set up assembler state, and run the
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
131 // assembler on the first file
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
132 int main(int argc, char **argv)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
133 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
134 argp_parse(&argp, argc, argv, 0, 0, NULL);
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
135 if (archive_file == NULL)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
136 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
137 fprintf(stderr, "You must specify an archive file.\n");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
138 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
139 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
140
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
141 if (operation == 0)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
142 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
143 fprintf(stderr, "You must specify an operation.\n");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
144 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
145 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
146
188
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
147 if (operation == LWAR_OP_LIST || operation == LWAR_OP_REMOVE || operation == LWAR_OP_EXTRACT)
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
148 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
149 struct stat stbuf;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
150 // make sure the archive exists
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
151 if (stat(archive_file, &stbuf) < 0)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
152 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
153 fprintf(stderr, "Cannot open archive file %s:\n", archive_file);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
154 perror("");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
155 exit(2);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
156 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
157 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
158 if (operation == LWAR_OP_CREATE)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
159 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
160 struct stat stbuf;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
161 // check if the archive exists
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
162 if (stat(archive_file, &stbuf) < 0)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
163 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
164 if (errno != ENOENT)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
165 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
166 fprintf(stderr, "Cannot create archive file %s:\n", archive_file);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
167 perror("");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
168 exit(2);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
169 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
170 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
171 else
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
172 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
173 if (unlink(archive_file) < 0)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
174 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
175 fprintf(stderr, "Cannot create archive file %s:\n", archive_file);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
176 perror("");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
177 exit(2);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
178 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
179
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
180 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
181 }
171
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
182
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
183 switch (operation)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
184 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
185 case LWAR_OP_LIST:
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
186 do_list();
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
187 break;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
188
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
189 case LWAR_OP_ADD:
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
190 case LWAR_OP_CREATE:
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
191 do_add();
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
192 break;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
193
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
194 case LWAR_OP_REMOVE:
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
195 do_remove();
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
196 break;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
197
188
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
198 case LWAR_OP_REPLACE:
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
199 do_replace();
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
200 break;
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
201
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
202 case LWAR_OP_EXTRACT:
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
203 do_extract();
bb2665c7005c Added --extract and --replace to lwar
lost
parents: 174
diff changeset
204 break;
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
205 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents: 171
diff changeset
206
171
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
207 exit(0);
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
208 }