409e16b186c62b70305371bdf747011fd984974c
[zilutils.git] / zilasm / main.c
1 /*
2  * main.c
3  *
4  * Copyright (C) 2015 Alexander Andrejevic <theflash AT sdf DOT lonestar DOT org>
5  * Copyright (C) 2015 Jason Self <j@jxself.org>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <getopt.h>
26 #include <time.h>
27 #include <ctype.h>
28
29 #include "config.h"
30
31 enum { ZVERSION = 11, ZORKID, ZSERIAL };
32
33 static struct option const long_options[] =
34 {
35     { "help",     no_argument,       NULL, 'h' },
36     { "version",  no_argument,       NULL, 'V' },
37     { "output",   required_argument, NULL, 'o' },
38     { "zversion", required_argument, NULL, ZVERSION },
39     { "zorkid",   required_argument, NULL, ZORKID   },
40     { "serial",   required_argument, NULL, ZSERIAL  },
41     { NULL, 0, NULL, 0 }
42 };
43
44 struct
45 {
46     int  zversion;     /* 0 - 8     */
47     int  zorkid;       /* 0 - 65535 */
48     char zserial[7];   /* YYMMDD    */
49 } Config;
50
51 void wrong_arg(const char *err, ...)
52 {
53     if (err)
54     {
55         va_list ap;
56         va_start(ap, err);
57         vfprintf(stderr, err, ap);
58         va_end(ap);
59     }
60     fprintf(stderr, "Try `" PACKAGE_NAME " --help' for more information.\n");
61     exit(1);
62 }
63
64 void print_version()
65 {
66     printf( PACKAGE_STRING "\n"
67             "License AGPLv3+: GNU AGPL version 3 or later\n"
68             "<http://gnu.org/licenses/agpl.html>\n"
69             "This is free software: you are free to change and redistribute it.\n"
70             "There is NO WARRANTY, to the extent permitted by law.\n"
71           );
72     exit(0);
73 }
74
75 void print_usage(int failed)
76 {
77     printf("Usage: " PACKAGE_NAME " [OPTION...] [FILES...]\n"
78            "\n"
79            "--version  Display program version and exit\n"
80            "--help     Display this help\n"
81            "\n"
82            "--zversion (accepts numbers 1 - 8, defaults to 6 if not specified)\n"
83            "--zorkid   (integer between 0 and 65535, defaults to 0 if not specified)\n"
84            "--serial   (six characters of ASCII, defaults to current date\n"
85            "            in the form YYMMDD if not specified)\n"
86           );
87     exit(failed);
88 }
89
90 void fill_zserial(void)
91 {
92     time_t t;
93     struct tm *timeinfo;
94     time (&t);
95     timeinfo = localtime(&t);
96     strftime (Config.zserial, sizeof(Config.zserial), "%y%m%d", timeinfo);
97 }
98
99 void fill_config(void)
100 {
101     Config.zversion = 6;
102     Config.zorkid   = 0;
103     fill_zserial();
104 }
105
106 void parse_intarg(int *dest, const char name[], int min, int max, int defval)
107 {
108     if (!optarg)
109     {
110         *dest = defval;
111         return;
112     }
113     int n = atoi(optarg);
114     if (n >= min && n <= max)
115     {
116         *dest = n;
117         return;
118     }
119     wrong_arg("Wrong %s value %s, must be integer between %d and %d\n",
120               name, optarg, min, max);
121 }
122
123 void parse_zserial(void)
124 {
125     if (!optarg)
126     {
127         fill_zserial();
128         return;
129     }
130     size_t n = strlen(optarg);
131     if (n == sizeof(Config.zserial) - 1)
132     {
133         char *p = optarg;
134         while (*p && isalnum(*p))
135             p++;
136         if (!*p)      /* ..optarg contains alphanumeric only? */
137         {
138             strncpy(Config.zserial, optarg, sizeof(Config.zserial));
139             return;
140         }
141     }
142     wrong_arg("Wrong zserial value %s, must be 6 ascii characters\n", optarg);
143 }
144
145 void new_file_suffix(char *result, size_t maxlen, const char *src, const char *newsuffix)
146 {
147     strncpy(result, src, maxlen);
148     char *p = strrchr(result, '.');
149     if (p && strchr(p, '/'))
150         p = NULL;
151     if (p)
152     {
153         strncpy(p, newsuffix, maxlen - (p - result));
154     }
155     else
156     {
157         strncat(result, newsuffix, maxlen);
158     }
159     result[maxlen] = 0;
160 }
161
162 char *build_output_filename(const char basename[], const char *suffix)
163 {
164     int n = strlen(basename) + strlen(suffix);
165     char *ofile = malloc(n + 1);  /* todo!!! check for NULL. free. */
166     new_file_suffix(ofile, n, basename, suffix);
167     return ofile;
168 }
169
170 int main(int argc, char *argv[], char *envp[])
171 {
172     const char *output_file = NULL;
173     int i;
174
175     fill_config();
176
177     int opt = 0;
178     while ((opt = getopt_long (argc, argv, "hVo:", long_options, NULL)) != -1)
179     {
180         switch(opt)
181         {
182         case 'h'     :
183             print_usage(0);
184         case 'V'     :
185             print_version();
186         case 'o'     :
187             if (output_file) wrong_arg("Output file must be given once\n");
188             output_file = optarg;
189             break;
190         case ZVERSION:
191             parse_intarg(&Config.zversion, "zversion", 1, 8,      1);
192             break;
193         case ZORKID  :
194             parse_intarg(&Config.zorkid,   "zorkid",   0, 0xFFFF, 0);
195             break;
196         case ZSERIAL :
197             parse_zserial();
198             break;
199         default      :
200             wrong_arg(0);
201         }
202     }
203
204     int first_input_file = optind;
205     if (first_input_file >= argc)
206         wrong_arg("Missing input file\n");
207     if (!output_file)
208         output_file = build_output_filename(argv[first_input_file], ".dat");
209
210     // TODO: Everything :)
211
212     printf("Input files:\n");
213     for (i = optind; i < argc; i++)
214         printf("\t%s\n", argv[i]);
215
216     printf("Output file: %s\n\n", output_file);
217
218     printf("Config:\n"
219            "- ZVersion: %d\n"
220            "- ZorkID:   %d\n"
221            "- ZSerial:  %s\n",
222            Config.zversion, Config.zorkid, Config.zserial
223           );
224
225     return 0;
226 }