1 // Copyright (c) 1993-2011 PrBoom developers (see AUTHORS)
2 // Licence: GPLv2 or later (see COPYING)
3 
4 // Main program, parse command line arguments
5 
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <ctype.h>
10 
11 #include "rd_util.h"
12 #include "rd_output.h"
13 #include "rd_sound.h"
14 #include "rd_palette.h"
15 #include "rd_graphic.h"
16 
17 enum argtype
18 {
19   ARG_NONE,
20   ARG_OUTPUT,
21   ARG_INCLUDE,
22   ARG_PALETTE,
23   ARG_MARKER,
24   ARG_LUMP,
25   ARG_GRAPHIC,
26   ARG_SOUND,
27   ARG_SPRITE,
28 };
29 
usage(int exitcode)30 static void ATTR((noreturn)) usage(int exitcode)
31 {
32   FILE *f = exitcode ? stderr : stdout;
33   fprintf(f, "Usage: rdatawad <options...>\n"
34           "  -o <output filename>\n"
35           "  -I <search directory>\n"
36           "  -palette <rgbfile>\n"
37           "  -marker <lumpname>\n"
38           "  -lumps <file>...\n"
39           "  -graphics <ppmfile>...\n"
40           "  -sounds <wavfile>...\n"
41           "  -sprites <x,y,ppmfile>...\n");
42   exit(exitcode);
43 }
44 
main(int argc,char ** argv)45 int main(int argc, char **argv)
46 {
47   enum argtype argtype = ARG_NONE;
48   const char *output = NULL;
49 
50   if (argc <= 1)
51     usage(0);
52 
53   while (*(++argv))
54   {
55     char *arg = *argv;
56 
57     if (*arg == '-')
58     {
59       if (*(arg+1) == '-') // allow both -switch and --switch
60         arg++;
61 
62       if (!strcmp(arg, "-o"))
63         argtype = ARG_OUTPUT;
64       else if (!strcmp(arg, "-I"))
65         argtype = ARG_INCLUDE;
66       else if (!strcmp(arg, "-palette"))
67         argtype = ARG_PALETTE;
68       else if (!strcmp(arg, "-marker"))
69         argtype = ARG_MARKER;
70       else if (!strcmp(arg, "-lumps"))
71         argtype = ARG_LUMP;
72       else if (!strcmp(arg, "-graphics"))
73         argtype = ARG_GRAPHIC;
74       else if (!strcmp(arg, "-sounds"))
75         argtype = ARG_SOUND;
76       else if (!strcmp(arg, "-sprites"))
77         argtype = ARG_SPRITE;
78       else if (!strcmp(arg, "-help") || !strcmp(arg, "-version"))
79         usage(0);
80       else
81         usage(1);
82     }
83     else
84       switch (argtype)
85       {
86       case ARG_NONE:
87         usage(1);
88         break;
89 
90       case ARG_OUTPUT:
91         output = arg;
92         argtype = ARG_NONE;
93         break;
94 
95       case ARG_INCLUDE:
96         search_path(arg);
97         break;
98 
99       case ARG_PALETTE:
100         palette_init(arg);
101         argtype = ARG_NONE;
102         break;
103 
104       case ARG_MARKER:
105         output_add(arg, NULL, 0);
106         break;
107 
108       case ARG_LUMP:
109         {
110           void *data = NULL;
111           size_t size = read_or_die(&data, arg);
112           output_add(arg, data, size);
113         }
114         break;
115 
116       case ARG_GRAPHIC:
117         {
118           void *data = NULL;
119           size_t size = ppm_to_patch(&data, arg, 0, 0);
120           output_add(arg, data, size);
121         }
122         break;
123 
124       case ARG_SOUND:
125         {
126           void *data = NULL;
127           size_t size = wav_to_doom(&data, arg);
128           output_add(arg, data, size);
129         }
130         break;
131 
132       case ARG_SPRITE:
133         {
134           void *data = NULL;
135           char *pos = arg;
136           size_t size;
137           int x, y;
138 
139           x = strtol(pos, &pos, 0);
140           if (*pos == ',') pos++;
141           else if (!isspace(*pos) && !isdigit(*pos)) usage(1);
142 
143           y = strtol(pos, &pos, 0);
144           if (*pos == ',') pos++;
145           else if (!isspace(*pos) && !isdigit(*pos)) usage(1);
146 
147           size = ppm_to_patch(&data, pos, x, y);
148           output_add(pos, data, size);
149         }
150         break;
151       }
152   }
153 
154   if (output)
155     output_write(output);
156   else
157     die("No output file specified\n");
158 
159   return 0;
160 }
161