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