]> git.sur5r.net Git - cc65/blob - src/sp65/main.c
c14bf38c007cfff73182d9f10e2ecab0b9a5582b
[cc65] / src / sp65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*            Main program of the sp65 sprite and bitmap utility             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2012,      Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40
41 /* common */
42 #include "abend.h"
43 #include "cmdline.h"
44 #include "print.h"
45 #include "version.h"
46
47 /* sp65 */
48 #include "attr.h"
49 #include "convert.h"
50 #include "error.h"
51 #include "input.h"
52 #include "output.h"
53
54
55
56 /*****************************************************************************/
57 /*                                   Data                                    */
58 /*****************************************************************************/
59
60
61
62 /* Bitmap first read */
63 static Bitmap* B;
64
65 /* Bitmap working copy */
66 static Bitmap* C;
67
68 /* Output data from convertion */
69 static StrBuf* D;
70
71
72
73 /*****************************************************************************/
74 /*                                   Code                                    */
75 /*****************************************************************************/
76
77
78
79 static void Usage (void)
80 /* Print usage information and exit */
81 {
82     printf (
83             "Usage: %s [options] file [options] [file]\n"
84             "Short options:\n"
85             "  -V\t\t\t\tPrint the version number and exit\n"
86             "  -c fmt[,attrlist]\t\tConvert into target format\n"
87             "  -h\t\t\t\tHelp (this text)\n"
88             "  -lc\t\t\t\tList all possible conversions\n"
89             "  -r file[,attrlist]\t\tRead an input file\n"
90             "  -v\t\t\t\tIncrease verbosity\n"
91             "  -w file[,attrlist]\t\tWrite the output to a file\n"
92             "\n"
93             "Long options:\n"
94             "  --convert-to fmt[,attrlist]\tConvert into target format\n"
95             "  --help\t\t\tHelp (this text)\n"
96             "  --list-conversions\t\tList all possible conversions\n"
97             "  --pop\t\t\t\tRestore the original loaded image\n"
98             "  --read file[,attrlist]\tRead an input file\n"
99             "  --slice x,y,w,h\t\tGenerate a slice from the loaded bitmap\n"
100             "  --verbose\t\t\tIncrease verbosity\n"
101             "  --version\t\t\tPrint the version number and exit\n"
102             "  --write file[,attrlist]\tWrite the output to a file\n",
103             ProgName);
104 }
105
106
107
108 static void SetWorkBitmap (Bitmap* N)
109 /* Delete an old working bitmap and set a new one. The new one may be NULL
110  * to clear it.
111  */
112 {
113     /* If we have a distinct work bitmap, delete it */
114     if (C != 0 && C != B) {
115         FreeBitmap (C);
116     }
117
118     /* Set the new one */
119     C = N;
120 }
121
122
123
124 static void SetOutputData (StrBuf* N)
125 /* Delete the old output data and replace it by the given one. The new one
126  * may be NULL to clear it.
127  */
128 {
129     /* Delete the old output data */
130     if (D != 0) {
131         FreeStrBuf (D);
132     }
133
134     /* Set the new one */
135     D = N;
136 }
137
138
139
140 static void OptConvertTo (const char* Opt attribute ((unused)), const char* Arg)
141 /* Convert the bitmap into a target format */
142 {
143     static const char* NameList[] = {
144         "format"
145     };
146
147     /* Parse the argument */
148     Collection* A = ParseAttrList (Arg, NameList, 2);
149
150     /* We must have a bitmap */
151     if (C == 0) {
152         Error ("No bitmap to convert");
153     }
154
155     /* Convert the bitmap */
156     SetOutputData (ConvertTo (C, A));
157
158     /* Delete the attribute list */
159     FreeCollection (A);
160 }
161
162
163
164 static void OptHelp (const char* Opt attribute ((unused)),
165                      const char* Arg attribute ((unused)))
166 /* Print usage information and exit */
167 {
168     Usage ();
169     exit (EXIT_SUCCESS);
170 }
171
172
173
174 static void OptListConversions (const char* Opt attribute ((unused)),
175                                 const char* Arg attribute ((unused)))
176 /* Print a list of all conversions */
177 {
178     ListConversionTargets (stdout);
179     exit (EXIT_SUCCESS);
180 }
181
182
183
184 static void OptPop (const char* Opt attribute ((unused)),
185                     const char* Arg attribute ((unused)))
186 /* Restore the original image */
187 {
188     /* C and B must differ and we must have an original */
189     if (B == 0 || C == 0 || C == B) {
190         Error ("Nothing to pop");
191     }
192
193     /* Delete the changed image and restore the original one */
194     SetWorkBitmap (B);
195 }
196
197
198
199 static void OptRead (const char* Opt, const char* Arg)
200 /* Read an input file */
201 {
202     static const char* NameList[] = {
203         "name", "format"
204     };
205
206
207     /* Parse the argument */
208     Collection* A = ParseAttrList (Arg, NameList, 2);
209
210     /* Clear the working copy */
211     SetWorkBitmap (0);
212
213     /* Delete the original */
214     FreeBitmap (B);
215
216     /* Read the file and use it as original and as working copy */
217     B = C = ReadInputFile (A);
218
219     /* Delete the attribute list */
220     FreeCollection (A);
221 }
222
223
224
225 static void OptSlice (const char* Opt attribute ((unused)), const char* Arg)
226 /* Generate a slice of a bitmap */
227 {
228     unsigned X, Y, W, H;
229     unsigned char T;
230
231     /* We must have a bitmap otherwise we cannot slice */
232     if (C == 0) {
233         Error ("Nothing to slice");
234     }
235
236     /* The argument is X,Y,W,H */
237     if (sscanf (Arg, "%u,%u,%u,%u,%c", &X, &Y, &W, &H, &T) != 4) {
238         Error ("Invalid argument. Slice must be given as X,Y,W,H");
239     }
240
241     /* Check the coordinates to be within the original bitmap */
242     if (W > BM_MAX_WIDTH || H > BM_MAX_HEIGHT ||
243         X + W > GetBitmapWidth (C) ||
244         Y + H > GetBitmapHeight (C)) {
245         Error ("Invalid slice coordinates and/or size");
246     }
247
248     /* Create the slice */
249     SetWorkBitmap (SliceBitmap (C, X, Y, W, H));
250 }
251
252
253
254 static void OptVerbose (const char* Opt attribute ((unused)),
255                         const char* Arg attribute ((unused)))
256 /* Increase versbosity */
257 {
258     ++Verbosity;
259 }
260
261
262
263 static void OptVersion (const char* Opt attribute ((unused)),
264                         const char* Arg attribute ((unused)))
265 /* Print the assembler version */
266 {
267     fprintf (stderr,
268              "%s V%s - (C) Copyright 2012, Ullrich von Bassewitz\n",
269              ProgName, GetVersionAsString ());
270 }
271
272
273
274 static void OptWrite (const char* Opt attribute ((unused)), const char* Arg)
275 /* Write an output file */
276 {
277     static const char* NameList[] = {
278         "name", "format"
279     };
280
281
282     /* Parse the argument */
283     Collection* A = ParseAttrList (Arg, NameList, 2);
284
285     /* Write the file */
286     WriteOutputFile (D, A);
287
288     /* Delete the attribute list */
289     FreeCollection (A);
290 }
291
292
293
294 int main (int argc, char* argv [])
295 /* sp65 main program */
296 {
297     /* Program long options */
298     static const LongOpt OptTab[] = {
299         { "--convert-to",       1,      OptConvertTo            },
300         { "--help",             0,      OptHelp                 },
301         { "--list-conversions", 0,      OptListConversions      },
302         { "--pop",              0,      OptPop                  },
303         { "--read",             1,      OptRead                 },
304         { "--slice",            1,      OptSlice                },
305         { "--verbose",          0,      OptVerbose              },
306         { "--version",          0,      OptVersion              },
307         { "--write",            1,      OptWrite                },
308     };
309
310     unsigned I;
311
312     /* Initialize the cmdline module */
313     InitCmdLine (&argc, &argv, "sp65");
314
315     /* Check the parameters */
316     I = 1;
317     while (I < ArgCount) {
318
319         /* Get the argument */
320         const char* Arg = ArgVec[I];
321
322         /* Check for an option */
323         if (Arg[0] == '-') {
324             switch (Arg[1]) {
325
326                 case '-':
327                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
328                     break;
329
330                 case 'V':
331                     OptVersion (Arg, 0);
332                     break;
333
334                 case 'c':
335                     OptConvertTo (Arg, GetArg (&I, 2));
336                     break;
337
338                 case 'h':
339                     OptHelp (Arg, 0);
340                     break;
341
342                 case 'l':
343                     if (Arg[2] == 'c') {
344                         OptListConversions (Arg, 0);
345                     } else {
346                         UnknownOption (Arg);
347                     }
348                     break;
349
350                 case 'r':
351                     OptRead (Arg, GetArg (&I, 2));
352                     break;
353
354                 case 'v':
355                     OptVerbose (Arg, 0);
356                     break;
357
358                 case 'w':
359                     OptWrite (Arg, GetArg (&I, 2));
360                     break;
361
362                 default:
363                     UnknownOption (Arg);
364                     break;
365
366             }
367         } else {
368             /* We don't accept anything else */
369             AbEnd ("Don't know what to do with `%s'", Arg);
370         }
371
372         /* Next argument */
373         ++I;
374     }
375
376     /* Success */
377     return EXIT_SUCCESS;
378 }
379
380
381