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