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