339
|
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 #include <config.h>
|
|
26
|
|
27 #include <argp.h>
|
|
28 #include <errno.h>
|
|
29 #include <stdio.h>
|
|
30 #include <stdlib.h>
|
|
31 #include <sys/types.h>
|
|
32 #include <sys/stat.h>
|
|
33 #include <unistd.h>
|
|
34
|
|
35 #include "lwar.h"
|
|
36
|
|
37 // command line option handling
|
|
38 const char *argp_program_version = "LWAR from " PACKAGE_STRING;
|
|
39 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
|
|
40 char *program_name;
|
|
41
|
|
42 static error_t parse_opts(int key, char *arg, struct argp_state *state)
|
|
43 {
|
|
44 switch (key)
|
|
45 {
|
|
46 case 'd':
|
|
47 // debug
|
|
48 debug_level++;
|
|
49 break;
|
|
50
|
|
51 case 'a':
|
|
52 // add members
|
|
53 operation = LWAR_OP_ADD;
|
|
54 break;
|
|
55
|
|
56 case 'c':
|
|
57 // create archive
|
|
58 operation = LWAR_OP_CREATE;
|
|
59 break;
|
|
60
|
|
61 case 'm':
|
|
62 mergeflag = 1;
|
|
63 break;
|
|
64
|
|
65 case 'r':
|
|
66 // replace members
|
|
67 operation = LWAR_OP_REPLACE;
|
|
68 break;
|
|
69
|
|
70 case 'l':
|
|
71 // list members
|
|
72 operation = LWAR_OP_LIST;
|
|
73 break;
|
|
74
|
|
75 case 'x':
|
|
76 // extract members
|
|
77 operation = LWAR_OP_EXTRACT;
|
|
78 break;
|
|
79
|
|
80 case ARGP_KEY_ARG:
|
|
81 if (archive_file)
|
|
82 {
|
|
83 // add archive member to list
|
|
84 add_file_name(arg);
|
|
85 }
|
|
86 else
|
|
87 archive_file = arg;
|
|
88 break;
|
|
89
|
|
90 default:
|
|
91 return ARGP_ERR_UNKNOWN;
|
|
92 }
|
|
93 return 0;
|
|
94 }
|
|
95
|
|
96 static struct argp_option options[] =
|
|
97 {
|
|
98 { "replace", 'r', 0, 0,
|
|
99 "Add or replace archive members" },
|
|
100 { "extract", 'x', 0, 0,
|
|
101 "Extract members from the archive" },
|
|
102 { "add", 'a', 0, 0,
|
|
103 "Add members to the archive" },
|
|
104 { "list", 'l', 0, 0,
|
|
105 "List the contents of the archive" },
|
|
106 { "create", 'c', 0, 0,
|
|
107 "Create new archive (or truncate existing one)" },
|
|
108 { "merge", 'm', 0, 0,
|
|
109 "Add the contents of archive arguments instead of the archives themselves" },
|
|
110 { "debug", 'd', 0, 0,
|
|
111 "Set debug mode"},
|
|
112 { 0 }
|
|
113 };
|
|
114
|
|
115 static struct argp argp =
|
|
116 {
|
|
117 options,
|
|
118 parse_opts,
|
|
119 "<archive> [<file> ...]",
|
|
120 "LWAR, a library file manager for LWLINK"
|
|
121 };
|
|
122
|
|
123 extern void do_list(void);
|
|
124 extern void do_add(void);
|
|
125 extern void do_remove(void);
|
|
126 extern void do_replace(void);
|
|
127 extern void do_extract(void);
|
|
128
|
|
129 // main function; parse command line, set up assembler state, and run the
|
|
130 // assembler on the first file
|
|
131 int main(int argc, char **argv)
|
|
132 {
|
|
133 program_name = argv[0];
|
|
134 argp_parse(&argp, argc, argv, 0, 0, NULL);
|
|
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
|
|
147 if (operation == LWAR_OP_LIST || operation == LWAR_OP_REMOVE || operation == LWAR_OP_EXTRACT)
|
|
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 }
|
|
182
|
|
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
|
|
198 case LWAR_OP_REPLACE:
|
|
199 do_replace();
|
|
200 break;
|
|
201
|
|
202 case LWAR_OP_EXTRACT:
|
|
203 do_extract();
|
|
204 break;
|
|
205 }
|
|
206
|
|
207 exit(0);
|
|
208 }
|