171
|
1 /*
|
|
2 main.c
|
|
3 Copyright © 2009 William Astle
|
|
4
|
|
5 This file is part of LWAR.
|
|
6
|
|
7 LWAR 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 #ifdef HAVE_CONFIG_H
|
|
26 #include "config.h"
|
|
27 #endif
|
|
28
|
|
29 #include <argp.h>
|
|
30 #include <errno.h>
|
|
31 #include <stdio.h>
|
|
32 #include <stdlib.h>
|
172
|
33 #include <sys/types.h>
|
|
34 #include <sys/stat.h>
|
|
35 #include <unistd.h>
|
171
|
36
|
|
37 #include "lwar.h"
|
|
38
|
|
39 // command line option handling
|
|
40 const char *argp_program_version = "LWAR from " PACKAGE_STRING;
|
|
41 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
|
|
42
|
|
43 static error_t parse_opts(int key, char *arg, struct argp_state *state)
|
|
44 {
|
|
45 switch (key)
|
|
46 {
|
|
47 case 'd':
|
|
48 // debug
|
|
49 debug_level++;
|
|
50 break;
|
|
51
|
172
|
52 case 'a':
|
|
53 // add members
|
|
54 operation = LWAR_OP_ADD;
|
|
55 break;
|
|
56
|
|
57 case 'c':
|
|
58 // create archive
|
|
59 operation = LWAR_OP_CREATE;
|
|
60 break;
|
174
|
61
|
|
62 case 'm':
|
|
63 mergeflag = 1;
|
|
64 break;
|
|
65
|
188
|
66 case 'r':
|
|
67 // replace members
|
|
68 operation = LWAR_OP_REPLACE;
|
|
69 break;
|
172
|
70
|
|
71 case 'l':
|
|
72 // list members
|
|
73 operation = LWAR_OP_LIST;
|
|
74 break;
|
|
75
|
188
|
76 case 'x':
|
|
77 // extract members
|
|
78 operation = LWAR_OP_EXTRACT;
|
|
79 break;
|
|
80
|
172
|
81 case ARGP_KEY_ARG:
|
|
82 if (archive_file)
|
|
83 {
|
|
84 // add archive member to list
|
|
85 add_file_name(arg);
|
|
86 }
|
|
87 else
|
|
88 archive_file = arg;
|
|
89 break;
|
171
|
90
|
|
91 default:
|
|
92 return ARGP_ERR_UNKNOWN;
|
|
93 }
|
|
94 return 0;
|
|
95 }
|
|
96
|
|
97 static struct argp_option options[] =
|
|
98 {
|
188
|
99 { "replace", 'r', 0, 0,
|
|
100 "Add or replace archive members" },
|
|
101 { "extract", 'x', 0, 0,
|
|
102 "Extract members from the archive" },
|
172
|
103 { "add", 'a', 0, 0,
|
|
104 "Add members to the archive" },
|
|
105 { "list", 'l', 0, 0,
|
|
106 "List the contents of the archive" },
|
|
107 { "create", 'c', 0, 0,
|
|
108 "Create new archive (or truncate existing one)" },
|
174
|
109 { "merge", 'm', 0, 0,
|
|
110 "Add the contents of archive arguments instead of the archives themselves" },
|
171
|
111 { "debug", 'd', 0, 0,
|
|
112 "Set debug mode"},
|
|
113 { 0 }
|
|
114 };
|
|
115
|
|
116 static struct argp argp =
|
|
117 {
|
|
118 options,
|
|
119 parse_opts,
|
172
|
120 "<archive> [<file> ...]",
|
|
121 "LWAR, a library file manager for LWLINK"
|
171
|
122 };
|
|
123
|
172
|
124 extern void do_list(void);
|
|
125 extern void do_add(void);
|
|
126 extern void do_remove(void);
|
188
|
127 extern void do_replace(void);
|
|
128 extern void do_extract(void);
|
172
|
129
|
171
|
130 // main function; parse command line, set up assembler state, and run the
|
|
131 // assembler on the first file
|
|
132 int main(int argc, char **argv)
|
|
133 {
|
|
134 argp_parse(&argp, argc, argv, 0, 0, NULL);
|
172
|
135 if (archive_file == NULL)
|
|
136 {
|
|
137 fprintf(stderr, "You must specify an archive file.\n");
|
|
138 exit(1);
|
|
139 }
|
|
140
|
|
141 if (operation == 0)
|
|
142 {
|
|
143 fprintf(stderr, "You must specify an operation.\n");
|
|
144 exit(1);
|
|
145 }
|
|
146
|
188
|
147 if (operation == LWAR_OP_LIST || operation == LWAR_OP_REMOVE || operation == LWAR_OP_EXTRACT)
|
172
|
148 {
|
|
149 struct stat stbuf;
|
|
150 // make sure the archive exists
|
|
151 if (stat(archive_file, &stbuf) < 0)
|
|
152 {
|
|
153 fprintf(stderr, "Cannot open archive file %s:\n", archive_file);
|
|
154 perror("");
|
|
155 exit(2);
|
|
156 }
|
|
157 }
|
|
158 if (operation == LWAR_OP_CREATE)
|
|
159 {
|
|
160 struct stat stbuf;
|
|
161 // check if the archive exists
|
|
162 if (stat(archive_file, &stbuf) < 0)
|
|
163 {
|
|
164 if (errno != ENOENT)
|
|
165 {
|
|
166 fprintf(stderr, "Cannot create archive file %s:\n", archive_file);
|
|
167 perror("");
|
|
168 exit(2);
|
|
169 }
|
|
170 }
|
|
171 else
|
|
172 {
|
|
173 if (unlink(archive_file) < 0)
|
|
174 {
|
|
175 fprintf(stderr, "Cannot create archive file %s:\n", archive_file);
|
|
176 perror("");
|
|
177 exit(2);
|
|
178 }
|
|
179
|
|
180 }
|
|
181 }
|
171
|
182
|
172
|
183 switch (operation)
|
|
184 {
|
|
185 case LWAR_OP_LIST:
|
|
186 do_list();
|
|
187 break;
|
|
188
|
|
189 case LWAR_OP_ADD:
|
|
190 case LWAR_OP_CREATE:
|
|
191 do_add();
|
|
192 break;
|
|
193
|
|
194 case LWAR_OP_REMOVE:
|
|
195 do_remove();
|
|
196 break;
|
|
197
|
188
|
198 case LWAR_OP_REPLACE:
|
|
199 do_replace();
|
|
200 break;
|
|
201
|
|
202 case LWAR_OP_EXTRACT:
|
|
203 do_extract();
|
|
204 break;
|
172
|
205 }
|
|
206
|
171
|
207 exit(0);
|
|
208 }
|