4 * Copyright (C) 2015 Alexander Andrejevic <theflash AT sdf DOT lonestar DOT org>
5 * Copyright (C) 2015 Jason Self <j@jxself.org>
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.
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.
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/>
20 * SPDX-License-Identifier: AGPL-3.0-or-later
33 const int DEFAULT_ZVERSION = 6;
35 enum { ZVERSION = 11, ZORKID, ZSERIAL };
37 enum { FAIL = -1, OK = 0, NEED_RESTART = 1 };
39 static struct option const long_options[] =
41 { "help", no_argument, NULL, 'h' },
42 { "version", no_argument, NULL, 'V' },
43 { "output", required_argument, NULL, 'o' },
44 { "zversion", required_argument, NULL, ZVERSION },
45 { "zorkid", required_argument, NULL, ZORKID },
46 { "serial", required_argument, NULL, ZSERIAL },
56 int zversion; /* 0 - 8 */
57 int zorkid; /* 0 - 65535 */
58 char zserial[7]; /* YYMMDD */
59 Opcode_dict *opcode_dict;
62 void wrong_arg(const char *err, ...)
67 vfprintf(stderr, err, ap);
70 fprintf(stderr, "Try `" PACKAGE_NAME " --help' for more information.\n");
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"
85 void print_usage(int failed)
87 printf("Usage: " PACKAGE_NAME " [OPTION...] [FILES...]\n"
89 "--version Display program version and exit\n"
90 "--help Display this help\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",
101 void fill_zserial(void)
106 timeinfo = localtime(&t);
107 strftime (Config.zserial, sizeof(Config.zserial), "%y%m%d", timeinfo);
110 void fill_config(void)
112 bzero(&Config, sizeof(Config));
113 Config.zversion = DEFAULT_ZVERSION;
117 void parse_intarg(int *dest, const char name[], int min, int max, int defval)
123 int n = atoi(optarg);
124 if (n >= min && n <= max) {
128 wrong_arg("Wrong %s value %s, must be integer between %d and %d\n",
129 name, optarg, min, max);
132 void parse_zserial(void)
138 size_t n = strlen(optarg);
139 if (n == sizeof(Config.zserial) - 1) {
141 while (*p && isalnum(*p))
143 if (!*p) { /* ..optarg contains alphanumeric only? */
144 strncpy(Config.zserial, optarg, sizeof(Config.zserial));
148 wrong_arg("Wrong zserial value %s, must be 6 ascii characters\n", optarg);
151 void new_file_suffix(char *result, size_t maxlen, const char *src, const char *newsuffix)
153 strncpy(result, src, maxlen);
154 char *p = strrchr(result, '.');
155 if (p && strchr(p, '/'))
158 strncpy(p, newsuffix, maxlen - (p - result));
160 strncat(result, newsuffix, maxlen);
165 char *build_output_filename(const char basename[], const char *suffix)
167 int n = strlen(basename) + strlen(suffix);
168 char *ofile = malloc(n + 1); /* todo!!! check for NULL. free. */
169 new_file_suffix(ofile, n, basename, suffix);
173 int init_assembly(void)
185 int main(int argc, char *argv[], char *envp[])
187 const char *output_file = NULL;
193 while ((opt = getopt_long (argc, argv, "hVo:", long_options, NULL)) != -1) {
195 case 'h' : print_usage(0);
196 case 'V' : print_version();
197 case 'o' : if (output_file) wrong_arg("Output file must be given once\n");
198 output_file = optarg;
200 case ZVERSION: parse_intarg(&Config.zversion, "zversion", 1, 8, 1); break;
201 case ZORKID : parse_intarg(&Config.zorkid, "zorkid", 0, 0xFFFF, 0); break;
202 case ZSERIAL : parse_zserial(); break;
203 default : wrong_arg(0);
207 int first_input_file = optind;
208 if (first_input_file >= argc)
209 wrong_arg("Missing input file\n");
211 output_file = build_output_filename(argv[first_input_file], ".dat");
213 // TODO: Everything :)
215 printf("Input files:\n");
216 for (i = optind; i < argc; i++)
217 printf("\t%s\n", argv[i]);
219 printf("Output file: %s\n\n", output_file);
225 Config.zversion, Config.zorkid, Config.zserial
228 init_opcodes(Config.zversion, 0);
230 while(init_assembly() == OK && assembly() == NEED_RESTART);
232 /* TODO! List global symbols */
233 /* TODO! Find abbreviations */