]> git.draconx.ca Git - liblbx.git/blob - src/lbxtool.c
9c4b57bbf623d6dafaa45f76084b964db07703d8
[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 (C) 2006-2010 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 static const char *progname;
43 #define errmsg(fmt, ...) (\
44         fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
45 )
46
47 enum {
48         MODE_NONE,
49         MODE_LIST,
50         MODE_EXTRACT,
51 };
52
53 int filematch(char **argv, const char *name)
54 {
55         int i;
56
57         for (i = 0; argv[i]; i++) {
58                 switch(fnmatch(argv[i], name, 0)) {
59                 case 0:
60                         return 0;
61                 case FNM_NOMATCH:
62                         break;
63                 default:
64                         errmsg("error matching glob: %s.\n", argv[i]);
65                         return 1;
66                 }
67         }
68
69         return i ? -1: 0;
70 }
71
72 int list(LBX *lbx, int verbose, char **argv) {
73         size_t nfiles;
74         unsigned int i;
75
76         nfiles = lbx_numfiles(lbx);
77         if (verbose) {
78                 printf("Files in archive: %zu\n", nfiles);
79         }
80
81         for (i = 0; i < nfiles; i++) {
82                 struct lbx_statbuf stat;
83
84                 lbx_file_stat(lbx, i, &stat);
85
86                 switch (filematch(argv, stat.name)) {
87                 case -1: continue;
88                 case  0: break;
89                 default: return EXIT_FAILURE;
90                 }
91
92                 printf("%s", stat.name);
93                 if (verbose) {
94                         printf(" size=%zu bytes", stat.size);
95                 }
96
97                 putchar('\n');
98         }
99
100         return EXIT_SUCCESS;
101 }
102
103 int extract_file(LBXfile *f, const struct lbx_statbuf *stat)
104 {
105         int ret = -1;
106         size_t rc;
107         FILE *of;
108
109         of = fopen(stat->name, "wb");
110         if (!of) {
111                 errmsg("%s: fopen: %s\n",
112                         stat->name, strerror(errno));
113                 return -1;
114         }
115
116         while (1) {
117                 unsigned char buf[1024];
118
119                 rc = lbx_file_read(f, buf, sizeof buf);
120                 if (rc == 0) {
121                         if (lbx_file_eof(f))
122                                 ret = 0;
123                         break;
124                 }
125
126                 if (fwrite(buf, rc, 1, of) != 1) {
127                         errmsg("%s: fwrite: %s\n", stat->name, strerror(errno));
128                         break;
129                 }
130
131                 if (rc < sizeof buf) {
132                         if (lbx_file_eof(f))
133                                 ret = 0;
134                         break;
135                 }
136         }
137
138         if (fclose(of) == EOF) {
139                 errmsg("%s: fclose: %s\n", stat->name, strerror(errno));
140                 return -1;
141         }
142
143         return ret;
144 }
145
146 int extract(LBX *lbx, int verbose, char **argv) {
147         size_t nfiles;
148         unsigned int i;
149
150         nfiles = lbx_numfiles(lbx);
151         if (verbose) {
152                 printf("Files in archive: %zu\n", nfiles);
153         }
154
155         for (i = 0; i < nfiles; i++) {
156                 struct lbx_statbuf stat;
157                 LBXfile *file;
158
159                 lbx_file_stat(lbx, i, &stat);
160
161                 switch (filematch(argv, stat.name)) {
162                 case -1: continue;
163                 case  0: break;
164                 default: return EXIT_FAILURE;
165                 }
166
167                 file = lbx_file_open(lbx, i);
168                 if (!file) {
169                         errmsg("%s: %s.\n", stat.name, lbx_errmsg());
170                         continue;
171                 }
172
173                 if (extract_file(file, &stat) == -1)
174                         return EXIT_FAILURE;
175                 lbx_file_close(file);
176         }
177
178         return EXIT_SUCCESS;
179 }
180
181 int main(int argc, char **argv)
182 {
183         int mode = MODE_NONE, verbose = 0, opt, rc = EXIT_FAILURE;
184         struct lbx_pipe_state stdin_handle = { .f = stdin };
185         const char *file = NULL;
186         LBX *lbx;
187
188         static const char         *sopts   = "lxf:i:vV";
189         static const struct option lopts[] = {
190                 { "list",    0, NULL, 'l' },
191                 { "extract", 0, NULL, 'x' },
192
193                 { "file",    1, NULL, 'f' },
194                 { "index",   1, NULL, 'i' },
195
196                 { "verbose", 0, NULL, 'v' },
197
198                 { "version", 0, NULL, 'V' },
199                 { "usage",   0, NULL, 'U' },
200                 { "help",    0, NULL, 'H' },
201
202                 { 0 }
203         };
204
205         progname = "lbxtool"; /* argv[0]; */
206         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
207                 switch(opt) {
208                 case 'l':
209                         mode = MODE_LIST;
210                         break;
211                 case 'x':
212                         mode = MODE_EXTRACT;
213                         break;
214                 case 'f':
215                         file = optarg;
216                         break;
217                 case 'i':
218                         /* FIXME: Add index file support. */
219                         break;
220                 case 'v':
221                         verbose = 1;
222                         break;
223                 case 'V':
224                         puts(VERSION_BOILERPLATE("lbxtool"));
225                         return EXIT_SUCCESS;
226                 case 'U':
227                         printusage();
228                         return EXIT_SUCCESS;
229                 case 'H':
230                         printhelp();
231                         return EXIT_SUCCESS;
232                 default:
233                         return EXIT_FAILURE;
234                 }
235         }
236
237         if (file)
238                 lbx = lbx_fopen(file);
239         else
240                 lbx = lbx_open(&stdin_handle, &lbx_pipe_fops, NULL, "stdin");
241
242         if (!lbx) {
243                 errmsg("%s: %s.\n", file ? file : "stdin", lbx_errmsg());
244                 return EXIT_FAILURE;
245         }
246
247         switch (mode) {
248         case MODE_LIST:
249                 rc = list(lbx, verbose, &argv[optind]);
250                 break;
251         case MODE_EXTRACT:
252                 rc = extract(lbx, verbose, &argv[optind]);
253                 break;
254         default:
255                 fprintf(stderr, "%s: you must specify a mode.\n", progname);
256         }
257
258         lbx_close(lbx);
259         return rc;
260 }