]> git.draconx.ca Git - liblbx.git/blob - src/lbxtool.c
tools: Move progname handling into a common source file.
[liblbx.git] / src / lbxtool.c
1 /*
2  *  2ooM: The Master of Orion II Reverse Engineering Project
3  *  Simple command-line tool to extract LBX archive files.
4  *  Copyright © 2006-2011, 2013 Nick Bowler
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <getopt.h>
25 #include <fnmatch.h>
26
27 #include "tools.h"
28 #include "lbx.h"
29 #include "error.h"
30
31 static void printusage(void)
32 {
33         puts("usage: lbxtool [-l|-x] [-v] [-f path] [file ...]");
34 }
35
36 static void printhelp(void)
37 {
38         printusage();
39         puts("For now, see the man page for detailed help.");
40 }
41
42 enum {
43         MODE_NONE,
44         MODE_LIST,
45         MODE_EXTRACT,
46 };
47
48 int filematch(char **argv, const char *name)
49 {
50         int i;
51
52         for (i = 0; argv[i]; i++) {
53                 switch(fnmatch(argv[i], name, 0)) {
54                 case 0:
55                         return 0;
56                 case FNM_NOMATCH:
57                         break;
58                 default:
59                         errmsg("error matching glob: %s.\n", argv[i]);
60                         return 1;
61                 }
62         }
63
64         return i ? -1: 0;
65 }
66
67 int list(struct lbx *lbx, int verbose, char **argv)
68 {
69         unsigned int i;
70
71         if (verbose) {
72                 printf("Files in archive: %u\n", lbx->nfiles);
73         }
74
75         for (i = 0; i < lbx->nfiles; i++) {
76                 struct lbx_statbuf stat;
77
78                 lbx_file_stat(lbx, i, &stat);
79
80                 switch (filematch(argv, stat.name)) {
81                 case -1: continue;
82                 case  0: break;
83                 default: return EXIT_FAILURE;
84                 }
85
86                 printf("%s", stat.name);
87                 if (verbose) {
88                         printf(" size=%zu bytes", stat.size);
89                 }
90
91                 putchar('\n');
92         }
93
94         return EXIT_SUCCESS;
95 }
96
97 int extract_file(LBXfile *f, const struct lbx_statbuf *stat)
98 {
99         int ret = -1;
100         size_t rc;
101         FILE *of;
102
103         of = fopen(stat->name, "wb");
104         if (!of) {
105                 errmsg("%s: fopen: %s\n",
106                         stat->name, strerror(errno));
107                 return -1;
108         }
109
110         while (1) {
111                 unsigned char buf[1024];
112
113                 rc = lbx_file_read(f, buf, sizeof buf);
114                 if (rc == 0) {
115                         if (lbx_file_eof(f))
116                                 ret = 0;
117                         break;
118                 }
119
120                 if (fwrite(buf, rc, 1, of) != 1) {
121                         errmsg("%s: fwrite: %s\n", stat->name, strerror(errno));
122                         break;
123                 }
124
125                 if (rc < sizeof buf) {
126                         if (lbx_file_eof(f))
127                                 ret = 0;
128                         break;
129                 }
130         }
131
132         if (fclose(of) == EOF) {
133                 errmsg("%s: fclose: %s\n", stat->name, strerror(errno));
134                 return -1;
135         }
136
137         return ret;
138 }
139
140 int extract(struct lbx *lbx, int verbose, char **argv)
141 {
142         unsigned int i;
143
144         if (verbose) {
145                 printf("Files in archive: %u\n", lbx->nfiles);
146         }
147
148         for (i = 0; i < lbx->nfiles; i++) {
149                 struct lbx_statbuf stat;
150                 LBXfile *file;
151
152                 lbx_file_stat(lbx, i, &stat);
153
154                 switch (filematch(argv, stat.name)) {
155                 case -1: continue;
156                 case  0: break;
157                 default: return EXIT_FAILURE;
158                 }
159
160                 file = lbx_file_open(lbx, i);
161                 if (!file) {
162                         errmsg("%s: %s.\n", stat.name, lbx_errmsg());
163                         continue;
164                 }
165
166                 if (extract_file(file, &stat) == -1)
167                         return EXIT_FAILURE;
168                 lbx_file_close(file);
169         }
170
171         return EXIT_SUCCESS;
172 }
173
174 int main(int argc, char **argv)
175 {
176         int mode = MODE_NONE, verbose = 0, opt, rc = EXIT_FAILURE;
177         struct lbx_pipe_state stdin_handle = { .f = stdin };
178         const char *file = NULL;
179         struct lbx *lbx;
180
181         static const char         *sopts   = "lxf:i:vV";
182         static const struct option lopts[] = {
183                 { "list",    0, NULL, 'l' },
184                 { "extract", 0, NULL, 'x' },
185
186                 { "file",    1, NULL, 'f' },
187                 { "index",   1, NULL, 'i' },
188
189                 { "verbose", 0, NULL, 'v' },
190
191                 { "version", 0, NULL, 'V' },
192                 { "usage",   0, NULL, 'U' },
193                 { "help",    0, NULL, 'H' },
194
195                 { 0 }
196         };
197
198         tool_init("lbxtool", argc, argv);
199         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
200                 switch(opt) {
201                 case 'l':
202                         mode = MODE_LIST;
203                         break;
204                 case 'x':
205                         mode = MODE_EXTRACT;
206                         break;
207                 case 'f':
208                         file = optarg;
209                         break;
210                 case 'i':
211                         /* FIXME: Add index file support. */
212                         break;
213                 case 'v':
214                         verbose = 1;
215                         break;
216                 case 'V':
217                         tool_version();
218                         return EXIT_SUCCESS;
219                 case 'U':
220                         printusage();
221                         return EXIT_SUCCESS;
222                 case 'H':
223                         printhelp();
224                         return EXIT_SUCCESS;
225                 default:
226                         return EXIT_FAILURE;
227                 }
228         }
229
230         if (file)
231                 lbx = lbx_fopen(file);
232         else
233                 lbx = lbx_open(&stdin_handle, &lbx_pipe_fops, NULL, "stdin");
234
235         if (!lbx) {
236                 errmsg("%s: %s.\n", file ? file : "stdin", lbx_errmsg());
237                 return EXIT_FAILURE;
238         }
239
240         switch (mode) {
241         case MODE_LIST:
242                 rc = list(lbx, verbose, &argv[optind]);
243                 break;
244         case MODE_EXTRACT:
245                 rc = extract(lbx, verbose, &argv[optind]);
246                 break;
247         default:
248                 errmsg("you must specify a mode.\n", 0);
249         }
250
251         lbx_close(lbx);
252         return rc;
253 }