5dbd24020e15bcf663f48b0f5eaea3e57c45d1eb
[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 const int DEFAULT_ZVERSION = 6;
32
33 enum { ZVERSION = 11, ZORKID, ZSERIAL };
34
35 enum { FAIL = -1, OK = 0, NEED_RESTART = 1 };
36
37 static struct option const long_options[] =
38 {
39     { "help",     no_argument,       NULL, 'h' },
40     { "version",  no_argument,       NULL, 'V' },
41     { "output",   required_argument, NULL, 'o' },
42     { "zversion", required_argument, NULL, ZVERSION },
43     { "zorkid",   required_argument, NULL, ZORKID   },
44     { "serial",   required_argument, NULL, ZSERIAL  },
45     { NULL, 0, NULL, 0 }
46 };
47
48 typedef struct
49 {
50     int todo;
51 } Opcode_dict;
52
53 struct
54 {
55     int  zversion;     /* 0 - 8     */
56     int  zorkid;       /* 0 - 65535 */
57     char zserial[7];   /* YYMMDD    */
58     Opcode_dict *opcode_dict;
59 } Config;
60
61 void wrong_arg(const char *err, ...)
62 {
63     if (err)
64     {
65         va_list ap;
66         va_start(ap, err);
67         vfprintf(stderr, err, ap);
68         va_end(ap);
69     }
70     fprintf(stderr, "Try `" PACKAGE_NAME " --help' for more information.\n");
71     exit(1);
72 }
73
74 void print_version()
75 {
76     printf( PACKAGE_STRING "\n"
77             "License AGPLv3+: GNU AGPL version 3 or later\n"
78             "<http://gnu.org/licenses/agpl.html>\n"
79             "This is free software: you are free to change and redistribute it.\n"
80             "There is NO WARRANTY, to the extent permitted by law.\n"
81           );
82     exit(0);
83 }
84
85 void print_usage(int failed)
86 {
87     printf("Usage: " PACKAGE_NAME " [OPTION...] [FILES...]\n"
88            "\n"
89            "--version  Display program version and exit\n"
90            "--help     Display this help\n"
91            "\n"
92            "--zversion (accepts numbers 1 - 8, defaults to %d if not specified)\n"
93            "--zorkid   (integer between 0 and 65535, defaults to 0 if not specified)\n"
94            "--serial   (six characters of ASCII, defaults to current date\n"
95            "            in the form YYMMDD if not specified)\n",
96            DEFAULT_ZVERSION
97           );
98     exit(failed);
99 }
100
101 void fill_zserial(void)
102 {
103     time_t t;
104     struct tm *timeinfo;
105     time (&t);
106     timeinfo = localtime(&t);
107     strftime (Config.zserial, sizeof(Config.zserial), "%y%m%d", timeinfo);
108 }
109
110 void fill_config(void)
111 {
112     bzero(&Config, sizeof(Config));
113     Config.zversion = DEFAULT_ZVERSION;
114     fill_zserial();
115 }
116
117 void parse_intarg(int *dest, const char name[], int min, int max, int defval)
118 {
119     if (!optarg)
120     {
121         *dest = defval;
122         return;
123     }
124     int n = atoi(optarg);
125     if (n >= min && n <= max)
126     {
127         *dest = n;
128         return;
129     }
130     wrong_arg("Wrong %s value %s, must be integer between %d and %d\n",
131               name, optarg, min, max);
132 }
133
134 void parse_zserial(void)
135 {
136     if (!optarg)
137     {
138         fill_zserial();
139         return;
140     }
141     size_t n = strlen(optarg);
142     if (n == sizeof(Config.zserial) - 1)
143     {
144         char *p = optarg;
145         while (*p && isalnum(*p))
146             p++;
147         if (!*p)      /* ..optarg contains alphanumeric only? */
148         {
149             strncpy(Config.zserial, optarg, sizeof(Config.zserial));
150             return;
151         }
152     }
153     wrong_arg("Wrong zserial value %s, must be 6 ascii characters\n", optarg);
154 }
155
156 void new_file_suffix(char *result, size_t maxlen, const char *src, const char *newsuffix)
157 {
158     strncpy(result, src, maxlen);
159     char *p = strrchr(result, '.');
160     if (p && strchr(p, '/'))
161         p = NULL;
162     if (p)
163     {
164         strncpy(p, newsuffix, maxlen - (p - result));
165     }
166     else
167     {
168         strncat(result, newsuffix, maxlen);
169     }
170     result[maxlen] = 0;
171 }
172
173 char *build_output_filename(const char basename[], const char *suffix)
174 {
175     int n = strlen(basename) + strlen(suffix);
176     char *ofile = malloc(n + 1);  /* todo!!! check for NULL. free. */
177     new_file_suffix(ofile, n, basename, suffix);
178     return ofile;
179 }
180
181 void build_opcode_dict(void)
182 {
183     /* TODO */
184 }
185
186 int init_assembly(void)
187 {
188     /* TODO */
189     return OK;
190 }
191
192 int assembly(void)
193 {
194     /* TODO */
195     return OK;
196 }
197
198 int main(int argc, char *argv[], char *envp[])
199 {
200     const char *output_file = NULL;
201     int i;
202
203     fill_config();
204
205     int opt = 0;
206     while ((opt = getopt_long (argc, argv, "hVo:", long_options, NULL)) != -1)
207     {
208         switch(opt)
209         {
210         case 'h'     :
211             print_usage(0);
212         case 'V'     :
213             print_version();
214         case 'o'     :
215             if (output_file) wrong_arg("Output file must be given once\n");
216             output_file = optarg;
217             break;
218         case ZVERSION:
219             parse_intarg(&Config.zversion, "zversion", 1, 8,      1);
220             break;
221         case ZORKID  :
222             parse_intarg(&Config.zorkid,   "zorkid",   0, 0xFFFF, 0);
223             break;
224         case ZSERIAL :
225             parse_zserial();
226             break;
227         default      :
228             wrong_arg(0);
229         }
230     }
231
232     int first_input_file = optind;
233     if (first_input_file >= argc)
234         wrong_arg("Missing input file\n");
235     if (!output_file)
236         output_file = build_output_filename(argv[first_input_file], ".dat");
237
238     // TODO: Everything :)
239
240     printf("Input files:\n");
241     for (i = optind; i < argc; i++)
242         printf("\t%s\n", argv[i]);
243
244     printf("Output file: %s\n\n", output_file);
245
246     printf("Config:\n"
247            "- ZVersion: %d\n"
248            "- ZorkID:   %d\n"
249            "- ZSerial:  %s\n",
250            Config.zversion, Config.zorkid, Config.zserial
251           );
252
253     build_opcode_dict();  /* ..fills Config.opcode_dict */
254
255     while(init_assembly() == OK && assembly() == NEED_RESTART);
256
257     return 0;
258 }