4 * Copyright (C) 2015 Alexander Andrejevic <theflash AT sdf DOT lonestar DOT org>
5 * Copyright (C) 2015, 2019, 2020 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
50 const int DEFAULT_ZVERSION = 6;
53 { ZVERSION = 11, ZORKID, ZSERIAL };
56 { FAIL = -1, OK = 0, NEED_RESTART = 1 };
58 static struct option const long_options[] = {
59 {"help", no_argument, NULL, 'h'},
60 {"version", no_argument, NULL, 'V'},
61 {"output", required_argument, NULL, 'o'},
62 {"zversion", required_argument, NULL, ZVERSION},
63 {"zorkid", required_argument, NULL, ZORKID},
64 {"serial", required_argument, NULL, ZSERIAL},
77 int zversion; /* 0 - 8 */
78 int zorkid; /* 0 - 65535 */
79 char zserial[7]; /* YYMMDD */
80 Opcode_dict *opcode_dict;
84 struct String_Table_Elem
86 string value; // value in ASCII format
96 void fill_config (void);
97 void get_arguments (int argc, char *argv[], char *envp[]);
99 char *get_output_file_name ();
102 list < String_Table_Elem > m_string_table;
106 char *build_output_filename (const char basename[], const char *suffix);
107 void fill_zserial (void);
108 void new_file_suffix (char *result, size_t maxlen, const char *src,
109 const char *newsuffix);
111 void output_code_section ();
113 void parse_intarg (int *dest, const char name[], int min, int max,
115 void parse_zserial (void);
116 void print_usage (int failed);
117 void print_version ();
118 void wrong_arg (const char *err, ...);
123 CMain::CMain ():m_output_file (NULL)
130 FILE *file = fopen (m_output_file, "wb");
133 program_header_reset (6);
134 int size = sizeof (Program_header);
136 Program_header.mode = 0;
137 Program_header.release = 1; // game version
139 int code_start_offset = 64;
140 Program_header.startPC = code_start_offset >> 2;
143 ZMemblock *zmem_code = zmem_init (65536);
145 /// write zero number of local variables
146 zmem_putbyte (zmem_code, 0); // number of local variables
149 // write instructions' codes
150 for (int i = 0; i < g_number_of_instructions; ++i)
152 for (int j = 0; j < g_codes[i]->used_size; ++j)
154 zmem_putbyte (zmem_code, g_codes[i]->contents[j]);
160 m_code_size += 8 - (m_code_size & 7);
162 Program_header.dynamic_size = 8;
164 //Program_header.h_file_size = 33; //sizeof(Program_header) + zmb->used_size;
168 Word stringTableOffset = m_code_size;
169 Program_header.H_STRINGS_OFFSET = (64 + stringTableOffset) >> 3;
171 int stringTableSize = 64;
172 Program_header.h_file_size =
173 (code_start_offset + m_code_size + stringTableSize) >> 3;
174 ZMemblock *zmb = zmem_init (Program_header.h_file_size * 8);
176 for (int i = 0; i < m_code_size; ++i)
177 zmem_putbyte (zmb, zmem_code->contents[i]);
179 zmem_destroy (zmem_code);
181 //zmem_putbyte(zmb, 0); // number of local variables
182 //zmem_putbyte(zmb, 141); // print addr command
185 //zmem_putbyte(zmb, (offset >> 8) & 255);
186 //zmem_putbyte(zmb, offset & 255);
188 //zmem_putbyte(zmb, 186); // quit command
190 // output zeros until string table begins
191 while (zmb->used_size < stringTableOffset)
192 zmem_putbyte (zmb, 0);
194 //// fill string table with one string
195 //add_string_to_string_table("Hello, World!", zmb);
197 outputToFile (&Program_header, file);
198 fwrite (zmb->contents, zmb->allocated_size, 1, file);
207 CMain::get_output_file_name ()
209 return m_output_file;
214 CMain::get_arguments (int argc, char *argv[], char *envp[])
217 while ((opt = getopt_long (argc, argv, "hVo:", long_options, NULL)) != -1)
227 wrong_arg ("Output file must be given once\n");
228 m_output_file = optarg;
231 parse_intarg (&Config.zversion, "zversion", 1, 8, 1);
234 parse_intarg (&Config.zorkid, "zorkid", 0, 0xFFFF, 0);
244 int first_input_file = optind;
245 if (first_input_file >= argc)
246 wrong_arg ("Missing input file\n");
248 m_output_file = build_output_filename (argv[first_input_file], ".dat");
250 // TODO: Everything :)
253 printf ("Input files:\n");
254 for (int i = optind; i < argc; i++)
255 printf ("\t%s\n", argv[i]);
257 printf ("Output file: %s\n\n", m_output_file);
262 "- ZSerial: %s\n", Config.zversion, Config.zorkid, Config.zserial);
268 CMain::wrong_arg (const char *err, ...)
274 vfprintf (stderr, err, ap);
277 fprintf (stderr, "Try `" PACKAGE_NAME " --help' for more information.\n");
283 CMain::print_version ()
285 printf (PACKAGE_STRING "\n"
286 "License AGPLv3+: GNU AGPL version 3 or later\n"
287 "<http://gnu.org/licenses/agpl.html>\n"
288 "This is free software: you are free to change and redistribute it.\n"
289 "There is NO WARRANTY, to the extent permitted by law.\n");
295 CMain::print_usage (int failed)
297 printf ("Usage: " PACKAGE_NAME " [OPTION...] [FILES...]\n"
299 "--version Display program version and exit\n"
300 "--help Display this help\n"
302 "--zversion (accepts numbers 1 - 8, defaults to %d if not specified)\n"
303 "--zorkid (integer between 0 and 65535, defaults to 0 if not specified)\n"
304 "--serial (six characters of ASCII, defaults to current date\n"
305 " in the form YYMMDD if not specified)\n",
311 CMain::fill_zserial (void)
316 timeinfo = localtime (&t);
317 strftime (Config.zserial, sizeof (Config.zserial), "%y%m%d", timeinfo);
322 CMain::fill_config (void)
324 bzero (&Config, sizeof (Config));
325 Config.zversion = DEFAULT_ZVERSION;
331 CMain::parse_intarg (int *dest, const char name[], int min, int max,
339 int n = atoi (optarg);
340 if (n >= min && n <= max)
345 wrong_arg ("Wrong %s value %s, must be integer between %d and %d\n",
346 name, optarg, min, max);
351 CMain::parse_zserial (void)
358 size_t n = strlen (optarg);
359 if (n == sizeof (Config.zserial) - 1)
362 while (*p && isalnum (*p))
365 { /* ..optarg contains alphanumeric only? */
366 strncpy (Config.zserial, optarg, sizeof (Config.zserial));
370 wrong_arg ("Wrong zserial value %s, must be 6 ascii characters\n", optarg);
375 CMain::new_file_suffix (char *result, size_t maxlen, const char *src,
376 const char *newsuffix)
378 strncpy (result, src, maxlen);
379 char *p = strrchr (result, '.');
380 if (p && strchr (p, '/'))
384 strncpy (p, newsuffix, maxlen - (p - result));
388 strncat (result, newsuffix, maxlen);
395 CMain::build_output_filename (const char basename[], const char *suffix)
397 int n = strlen (basename) + strlen (suffix);
398 char *ofile = (char *) malloc (n + 1); /* todo!!! check for NULL. free. */
399 new_file_suffix (ofile, n, basename, suffix);
413 CMain::output_code_section ()
421 main (int argc, char *argv[], char *envp[])
426 main.get_arguments (argc, argv, envp);
427 init_opcodes (Config.zversion, 0);
431 //for (int i = optind; i < argc; i++)
432 // parse_file(argv[i]);
434 string file_name = argv[optind];
438 char delimeter = '\\';
440 char delimeter = '/';
443 if (file_name.rfind(delimeter) == string::npos)
445 pc.current_directory = "";
446 pc.current_file_name = file_name;
450 pc.current_directory = file_name.substr(0, file_name.rfind(delimeter)+1);
451 pc.current_file_name = file_name.substr(file_name.rfind(delimeter)+1);
453 g_parsing_contexts.push(pc);
455 parse_file();// argv[optind]);
459 /* TODO! List global symbols */
460 /* TODO! Find abbreviations */