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/>
31 const int DEFAULT_ZVERSION = 6;
33 enum { ZVERSION = 11, ZORKID, ZSERIAL };
35 enum { FAIL = -1, OK = 0, NEED_RESTART = 1 };
37 static struct option const long_options[] =
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 },
54 int zversion; /* 0 - 8 */
55 int zorkid; /* 0 - 65535 */
56 char zserial[7]; /* YYMMDD */
57 Opcode_dict *opcode_dict;
60 void wrong_arg(const char *err, ...)
65 vfprintf(stderr, err, ap);
68 fprintf(stderr, "Try `" PACKAGE_NAME " --help' for more information.\n");
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"
83 void print_usage(int failed)
85 printf("Usage: " PACKAGE_NAME " [OPTION...] [FILES...]\n"
87 "--version Display program version and exit\n"
88 "--help Display this help\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",
99 void fill_zserial(void)
104 timeinfo = localtime(&t);
105 strftime (Config.zserial, sizeof(Config.zserial), "%y%m%d", timeinfo);
108 void fill_config(void)
110 bzero(&Config, sizeof(Config));
111 Config.zversion = DEFAULT_ZVERSION;
115 void parse_intarg(int *dest, const char name[], int min, int max, int defval)
121 int n = atoi(optarg);
122 if (n >= min && n <= max) {
126 wrong_arg("Wrong %s value %s, must be integer between %d and %d\n",
127 name, optarg, min, max);
130 void parse_zserial(void)
136 size_t n = strlen(optarg);
137 if (n == sizeof(Config.zserial) - 1) {
139 while (*p && isalnum(*p))
141 if (!*p) { /* ..optarg contains alphanumeric only? */
142 strncpy(Config.zserial, optarg, sizeof(Config.zserial));
146 wrong_arg("Wrong zserial value %s, must be 6 ascii characters\n", optarg);
149 void new_file_suffix(char *result, size_t maxlen, const char *src, const char *newsuffix)
151 strncpy(result, src, maxlen);
152 char *p = strrchr(result, '.');
153 if (p && strchr(p, '/'))
156 strncpy(p, newsuffix, maxlen - (p - result));
158 strncat(result, newsuffix, maxlen);
163 char *build_output_filename(const char basename[], const char *suffix)
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);
171 int init_assembly(void)
183 int main(int argc, char *argv[], char *envp[])
185 const char *output_file = NULL;
191 while ((opt = getopt_long (argc, argv, "hVo:", long_options, NULL)) != -1) {
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;
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);
205 int first_input_file = optind;
206 if (first_input_file >= argc)
207 wrong_arg("Missing input file\n");
209 output_file = build_output_filename(argv[first_input_file], ".dat");
211 // TODO: Everything :)
213 printf("Input files:\n");
214 for (i = optind; i < argc; i++)
215 printf("\t%s\n", argv[i]);
217 printf("Output file: %s\n\n", output_file);
223 Config.zversion, Config.zorkid, Config.zserial
226 init_opcodes(Config.zversion, 0);
228 while(init_assembly() == OK && assembly() == NEED_RESTART);
230 /* TODO! List global symbols */
231 /* TODO! Find abbreviations */