Assembly loop added, calling empty routines.
[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 enum { FAIL = -1, OK = 0, NEED_RESTART = 1 };
34
35 static struct option const long_options[] =
36 {
37     { "help",     no_argument,       NULL, 'h' },
38     { "version",  no_argument,       NULL, 'V' },
39     { "output",   required_argument, NULL, 'o' },
40     { "zversion", required_argument, NULL, ZVERSION },
41     { "zorkid",   required_argument, NULL, ZORKID   },
42     { "serial",   required_argument, NULL, ZSERIAL  },
43     { NULL, 0, NULL, 0 }
44 };
45
46 typedef struct
47 {
48     int todo;
49 } Opcode_dict;
50
51 struct
52 {
53     int  zversion;     /* 0 - 8     */
54     int  zorkid;       /* 0 - 65535 */
55     char zserial[7];   /* YYMMDD    */
56     Opcode_dict *opcode_dict;
57 } Config;
58
59 void wrong_arg(const char *err, ...)
60 {
61     if (err)
62     {
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 6 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           );
95     exit(failed);
96 }
97
98 void fill_zserial(void)
99 {
100     time_t t;
101     struct tm *timeinfo;
102     time (&t);
103     timeinfo = localtime(&t);
104     strftime (Config.zserial, sizeof(Config.zserial), "%y%m%d", timeinfo);
105 }
106
107 void fill_config(void)
108 {
109     Config.zversion = 6;
110     Config.zorkid   = 0;
111     fill_zserial();
112 }
113
114 void parse_intarg(int *dest, const char name[], int min, int max, int defval)
115 {
116     if (!optarg)
117     {
118         *dest = defval;
119         return;
120     }
121     int n = atoi(optarg);
122     if (n >= min && n <= max)
123     {
124         *dest = n;
125         return;
126     }
127     wrong_arg("Wrong %s value %s, must be integer between %d and %d\n",
128               name, optarg, min, max);
129 }
130
131 void parse_zserial(void)
132 {
133     if (!optarg)
134     {
135         fill_zserial();
136         return;
137     }
138     size_t n = strlen(optarg);
139     if (n == sizeof(Config.zserial) - 1)
140     {
141         char *p = optarg;
142         while (*p && isalnum(*p))
143             p++;
144         if (!*p)      /* ..optarg contains alphanumeric only? */
145         {
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     {
161         strncpy(p, newsuffix, maxlen - (p - result));
162     }
163     else
164     {
165         strncat(result, newsuffix, maxlen);
166     }
167     result[maxlen] = 0;
168 }
169
170 char *build_output_filename(const char basename[], const char *suffix)
171 {
172     int n = strlen(basename) + strlen(suffix);
173     char *ofile = malloc(n + 1);  /* todo!!! check for NULL. free. */
174     new_file_suffix(ofile, n, basename, suffix);
175     return ofile;
176 }
177
178 void build_opcode_dict(void)
179 {
180     /* TODO */
181 }
182
183 int init_assembly(void)
184 {
185     /* TODO */
186     return OK;
187 }
188
189 int assembly(void)
190 {
191     /* TODO */
192     return OK;
193 }
194
195 int main(int argc, char *argv[], char *envp[])
196 {
197     const char *output_file = NULL;
198     int i;
199
200     fill_config();
201
202     int opt = 0;
203     while ((opt = getopt_long (argc, argv, "hVo:", long_options, NULL)) != -1)
204     {
205         switch(opt)
206         {
207         case 'h'     :
208             print_usage(0);
209         case 'V'     :
210             print_version();
211         case 'o'     :
212             if (output_file) wrong_arg("Output file must be given once\n");
213             output_file = optarg;
214             break;
215         case ZVERSION:
216             parse_intarg(&Config.zversion, "zversion", 1, 8,      1);
217             break;
218         case ZORKID  :
219             parse_intarg(&Config.zorkid,   "zorkid",   0, 0xFFFF, 0);
220             break;
221         case ZSERIAL :
222             parse_zserial();
223             break;
224         default      :
225             wrong_arg(0);
226         }
227     }
228
229     int first_input_file = optind;
230     if (first_input_file >= argc)
231         wrong_arg("Missing input file\n");
232     if (!output_file)
233         output_file = build_output_filename(argv[first_input_file], ".dat");
234
235     // TODO: Everything :)
236
237     printf("Input files:\n");
238     for (i = optind; i < argc; i++)
239         printf("\t%s\n", argv[i]);
240
241     printf("Output file: %s\n\n", output_file);
242
243     printf("Config:\n"
244            "- ZVersion: %d\n"
245            "- ZorkID:   %d\n"
246            "- ZSerial:  %s\n",
247            Config.zversion, Config.zorkid, Config.zserial
248           );
249
250     build_opcode_dict();  /* ..fills Config.opcode_dict */
251
252     while(init_assembly() == OK && assembly() == NEED_RESTART);
253
254     return 0;
255 }