annotate lwar/main.c @ 273:88bbffa404ea 2.5 2.5

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