src/mess/tools/imgtool/filtbas.c
| r2634 | r2635 | |
| 4 | 4 | |
| 5 | 5 | Filter for Microsoft-style tokenized BASIC files |
| 6 | 6 | |
| 7 | BASIC files typically follow the following format: |
| 8 | |
| 9 | int8 $ff |
| 10 | int16 <TOTAL LENGTH> |
| 11 | ... |
| 12 | int16 <PTR_NEXT_LINE> |
| 13 | int16 <LINE_NUM> |
| 14 | int8[] <TOKENISED_DATA> |
| 15 | int8 $00 End of line delimiter |
| 16 | |
| 7 | 17 | *****************************************************************************/ |
| 8 | 18 | |
| 9 | 19 | #include <string.h> |
| 10 | 20 | #include <stdarg.h> |
| 11 | 21 | #include <assert.h> |
| 22 | #include <ctype.h> |
| 23 | |
| 12 | 24 | #include "imgtool.h" |
| 13 | 25 | #include "utils.h" |
| 14 | 26 | |
| r2634 | r2635 | |
| 52 | 64 | IMPLEMENTATION |
| 53 | 65 | ***************************************************************************/ |
| 54 | 66 | |
| 67 | /*------------------------------------------------- |
| 68 | basic_readfile - reads a file and decodes |
| 69 | BASIC tokens into ASCII text |
| 70 | -------------------------------------------------*/ |
| 71 | |
| 55 | 72 | static imgtoolerr_t basic_readfile(const basictokens *tokens, |
| 56 | 73 | imgtool_partition *partition, const char *filename, |
| 57 | 74 | const char *fork, imgtool_stream *destf) |
| r2634 | r2635 | |
| 66 | 83 | const basictoken_tableent *token_table; |
| 67 | 84 | const char *token; |
| 68 | 85 | |
| 86 | /* open a memory stream */ |
| 69 | 87 | mem_stream = stream_open_mem(NULL, 0); |
| 70 | | if (!mem_stream) |
| 88 | if (mem_stream == NULL) |
| 71 | 89 | { |
| 72 | 90 | err = IMGTOOLERR_OUTOFMEMORY; |
| 73 | 91 | goto done; |
| r2634 | r2635 | |
| 78 | 96 | if (err) |
| 79 | 97 | goto done; |
| 80 | 98 | |
| 81 | | /* skip first bytes */ |
| 99 | /* skip first few bytes */ |
| 82 | 100 | stream_seek(mem_stream, tokens->skip_bytes, SEEK_SET); |
| 83 | 101 | |
| 102 | /* keep reading line headers */ |
| 84 | 103 | while(stream_read(mem_stream, line_header, sizeof(line_header)) == sizeof(line_header)) |
| 85 | 104 | { |
| 105 | /* pluck the address and line number out */ |
| 86 | 106 | if (tokens->be) |
| 87 | 107 | { |
| 88 | 108 | address = (UINT16) pick_integer_be(line_header, 0, 2); |
| r2634 | r2635 | |
| 94 | 114 | line_number = (UINT16) pick_integer_le(line_header, 2, 2); |
| 95 | 115 | } |
| 96 | 116 | |
| 117 | /* write the line number */ |
| 97 | 118 | stream_printf(destf, "%u ", (unsigned) line_number); |
| 98 | 119 | shift = 0x00; |
| 99 | 120 | |
| r2634 | r2635 | |
| 139 | 160 | } |
| 140 | 161 | |
| 141 | 162 | done: |
| 142 | | if (mem_stream) |
| 163 | if (mem_stream != NULL) |
| 143 | 164 | stream_close(mem_stream); |
| 144 | 165 | return err; |
| 145 | 166 | } |
| 146 | 167 | |
| 147 | 168 | |
| 148 | 169 | |
| 170 | /*------------------------------------------------- |
| 171 | basic_writefile - translates ASCII text to |
| 172 | BASIC tokens and writes it to a file |
| 173 | -------------------------------------------------*/ |
| 174 | |
| 149 | 175 | static imgtoolerr_t basic_writefile(const basictokens *tokens, |
| 150 | 176 | imgtool_partition *partition, const char *filename, |
| 151 | 177 | const char *fork, imgtool_stream *sourcef, option_resolution *opts) |
| 152 | 178 | { |
| 153 | | return IMGTOOLERR_UNIMPLEMENTED; |
| 179 | imgtoolerr_t err; |
| 180 | imgtool_stream *mem_stream; |
| 181 | char buf[1024]; |
| 182 | int eof = FALSE; |
| 183 | UINT32 len; |
| 184 | char c; |
| 185 | int i, j, pos, in_quotes; |
| 186 | UINT16 line_number; |
| 187 | UINT8 line_header[4]; |
| 188 | UINT8 file_header[3]; |
| 189 | const basictoken_tableent *token_table; |
| 190 | const char *token; |
| 191 | UINT8 token_shift, token_value; |
| 192 | UINT16 address; |
| 193 | |
| 194 | /* open a memory stream */ |
| 195 | mem_stream = stream_open_mem(NULL, 0); |
| 196 | if (mem_stream == NULL) |
| 197 | { |
| 198 | err = IMGTOOLERR_OUTOFMEMORY; |
| 199 | goto done; |
| 200 | } |
| 201 | |
| 202 | /* skip first few bytes */ |
| 203 | stream_fill(mem_stream, 0x00, tokens->skip_bytes); |
| 204 | |
| 205 | /* loop until the file is complete */ |
| 206 | while(!eof) |
| 207 | { |
| 208 | /* read a line */ |
| 209 | pos = 0; |
| 210 | while((len = stream_read(sourcef, &c, 1)) > 0) |
| 211 | { |
| 212 | /* break if at end of line */ |
| 213 | if ((c == '\r') || (c == '\n')) |
| 214 | break; |
| 215 | |
| 216 | if (pos <= ARRAY_LENGTH(buf) - 1) |
| 217 | { |
| 218 | buf[pos++] = c; |
| 219 | } |
| 220 | } |
| 221 | eof = (len == 0); |
| 222 | buf[pos] = '\0'; |
| 223 | |
| 224 | /* ignore lines that don't start with digits */ |
| 225 | if (isdigit(buf[0])) |
| 226 | { |
| 227 | /* start at beginning of line */ |
| 228 | pos = 0; |
| 229 | |
| 230 | /* read line number */ |
| 231 | line_number = 0; |
| 232 | while(isdigit(buf[pos])) |
| 233 | { |
| 234 | line_number *= 10; |
| 235 | line_number += (buf[pos++] - '0'); |
| 236 | } |
| 237 | |
| 238 | /* determine address */ |
| 239 | if (tokens->baseaddress != 0) |
| 240 | { |
| 241 | address = tokens->baseaddress + (UINT16)stream_size(mem_stream) + 4; |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | address = 0x0000; |
| 246 | } |
| 247 | |
| 248 | /* set up line header */ |
| 249 | memset(&line_header, 0, sizeof(line_header)); |
| 250 | if (tokens->be) |
| 251 | { |
| 252 | place_integer_be(line_header, 0, 2, address); |
| 253 | place_integer_be(line_header, 2, 2, line_number); |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | place_integer_be(line_header, 0, 2, address); |
| 258 | place_integer_be(line_header, 2, 2, line_number); |
| 259 | } |
| 260 | |
| 261 | /* emit line header */ |
| 262 | stream_write(mem_stream, line_header, sizeof(line_header)); |
| 263 | |
| 264 | /* skip spaces */ |
| 265 | while(isspace(buf[pos])) |
| 266 | pos++; |
| 267 | |
| 268 | /* when we start out, we are not within quotation marks */ |
| 269 | in_quotes = FALSE; |
| 270 | |
| 271 | /* read until end of line */ |
| 272 | while(buf[pos] != '\0') |
| 273 | { |
| 274 | token = NULL; |
| 275 | token_shift = 0; |
| 276 | token_value = 0; |
| 277 | |
| 278 | if (buf[pos] == '\"') |
| 279 | { |
| 280 | /* flip quotation status */ |
| 281 | in_quotes = !in_quotes; |
| 282 | } |
| 283 | else if (!in_quotes) |
| 284 | { |
| 285 | for (i = 0; (token == NULL) && (i < tokens->num_entries); i++) |
| 286 | { |
| 287 | token_table = &tokens->entries[i]; |
| 288 | for (j = 0; (token == NULL) && (j < token_table->num_tokens); j++) |
| 289 | { |
| 290 | if (!strncmp(&buf[pos], token_table->tokens[j], strlen(token_table->tokens[j]))) |
| 291 | { |
| 292 | token = token_table->tokens[j]; |
| 293 | token_shift = token_table->shift; |
| 294 | token_value = token_table->base + j; |
| 295 | pos += strlen(token); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /* did we find a token? */ |
| 302 | if (token != NULL) |
| 303 | { |
| 304 | /* emit the token */ |
| 305 | if (token_shift != 0) |
| 306 | stream_write(mem_stream, &token_shift, 1); |
| 307 | stream_write(mem_stream, &token_value, 1); |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | /* no token; emit the byte */ |
| 312 | stream_write(mem_stream, &buf[pos++], 1); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* emit line terminator */ |
| 317 | stream_fill(mem_stream, 0x00, 1); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /* emit program terminator */ |
| 322 | stream_fill(mem_stream, 0x00, 2); |
| 323 | |
| 324 | /* reset stream */ |
| 325 | stream_seek(mem_stream, 0, SEEK_SET); |
| 326 | |
| 327 | /* this is somewhat gross */ |
| 328 | if (tokens->skip_bytes >= 3) |
| 329 | { |
| 330 | if (tokens->be) |
| 331 | { |
| 332 | place_integer_be(file_header, 0, 1, 0xFF); |
| 333 | place_integer_be(file_header, 1, 2, stream_size(mem_stream)); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | place_integer_le(file_header, 0, 1, 0xFF); |
| 338 | place_integer_le(file_header, 1, 2, stream_size(mem_stream)); |
| 339 | } |
| 340 | stream_write(mem_stream, file_header, 3); |
| 341 | stream_seek(mem_stream, 0, SEEK_SET); |
| 342 | } |
| 343 | |
| 344 | /* write actual file */ |
| 345 | err = imgtool_partition_write_file(partition, filename, fork, mem_stream, opts, NULL); |
| 346 | if (err) |
| 347 | goto done; |
| 348 | |
| 349 | done: |
| 350 | if (mem_stream != NULL) |
| 351 | stream_close(mem_stream); |
| 352 | return err; |
| 154 | 353 | } |
| 155 | 354 | |
| 156 | 355 | |
| 157 | 356 | |
| 158 | | /* ----------------------------------------------------------------------- */ |
| 357 | /*************************************************************************** |
| 358 | TOKEN DEFINITIONS |
| 359 | ***************************************************************************/ |
| 159 | 360 | |
| 160 | 361 | static const char *const cocobas_statements[] = |
| 161 | 362 | { |
| r2634 | r2635 | |
| 2590 | 2791 | }; |
| 2591 | 2792 | |
| 2592 | 2793 | #endif |
| 2593 | | /* |
| 2594 | | int8 $ff |
| 2595 | | int16 <TOTAL LENGTH> |
| 2596 | | ... |
| 2597 | | int16 <PTR_NEXT_LINE> |
| 2598 | | int16 <LINE_NUM> |
| 2599 | | int8[] <TOKENISED_DATA> |
| 2600 | | int8 $00 End of line delimiter |
| 2601 | | */ |
| 2602 | 2794 | |
| 2603 | | /************************************* |
| 2604 | | * |
| 2605 | | * CoCo BASIC |
| 2606 | | * |
| 2607 | | *************************************/ |
| 2608 | 2795 | |
| 2796 | |
| 2797 | /*************************************************************************** |
| 2798 | COCO BASIC |
| 2799 | ***************************************************************************/ |
| 2800 | |
| 2609 | 2801 | static const basictoken_tableent cocobas_tokenents[] = |
| 2610 | 2802 | { |
| 2611 | 2803 | { 0x00, 0x80, cocobas_statements, sizeof(cocobas_statements) / sizeof(cocobas_statements[0]) }, |
| r2634 | r2635 | |
| 2646 | 2838 | |
| 2647 | 2839 | |
| 2648 | 2840 | |
| 2649 | | /************************************* |
| 2650 | | * |
| 2651 | | * Dragon BASIC |
| 2652 | | * |
| 2653 | | *************************************/ |
| 2841 | /*************************************************************************** |
| 2842 | DRAGON BASIC |
| 2843 | ***************************************************************************/ |
| 2654 | 2844 | |
| 2655 | 2845 | static const basictoken_tableent dragonbas_tokenents[] = |
| 2656 | 2846 | { |
| r2634 | r2635 | |
| 2690 | 2880 | } |
| 2691 | 2881 | } |
| 2692 | 2882 | |
| 2693 | | /************************************* |
| 2694 | | * |
| 2695 | | * VZBASIC |
| 2696 | | * |
| 2697 | | *************************************/ |
| 2698 | 2883 | |
| 2884 | |
| 2885 | /*************************************************************************** |
| 2886 | VZBASIC |
| 2887 | ***************************************************************************/ |
| 2888 | |
| 2699 | 2889 | static const basictoken_tableent vzbas_tokenents[] = |
| 2700 | 2890 | { |
| 2701 | | { 0x00, 0x80, vzbas, sizeof(vzbas) / sizeof(vzbas[0]) } |
| 2891 | { 0x00, 0x80, vzbas, sizeof(vzbas) / sizeof(vzbas[0]) } |
| 2702 | 2892 | }; |
| 2703 | 2893 | |
| 2894 | |
| 2895 | |
| 2704 | 2896 | static const basictokens vzbas_tokens = |
| 2705 | 2897 | { |
| 2706 | 2898 | 0x7ae9, |