1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
|
/* zmakebas - convert a text file containing a speccy Basic program
* into an actual program file loadable on a speccy.
*
* Public domain by Russell Marks, 1998.
*/
/* warning: this is probably the least structured program ever.
* I guess that's what comes from hacking something into existence... :-/
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_GETOPT
#include <unistd.h>
#endif
#include <math.h>
#include <ctype.h>
#if defined(__TURBOC__) && !defined(MSDOS)
#define MSDOS
#endif
#define DEFAULT_OUTPUT "out.tap"
#define REM_TOKEN_NUM 234
#define BIN_TOKEN_NUM 196
#define VAL_TOKEN_NUM 176
#define VALSTR_TOKEN_NUM 174
#define DEFFN_TOKEN_NUM 206
/* tokens are stored (and looked for) in reverse speccy-char-set order,
* to avoid def fn/fn and go to/to screwups. There are two entries for
* each token - this is to allow for things like "go to" which can
* actually be entered (thank ghod) as "goto". The one extension to
* this I've made is that randomize can be entered with -ize or -ise.
*
* One exception to the above - VAL/VAL$ positions are flipped here
* so that we do VAL$ first, and swapped after tokenising.
*/
char *tokens[]=
{
"copy", "",
"return", "",
"clear", "",
"draw", "",
"cls", "",
"if", "",
"randomize", "randomise",
"save", "",
"run", "",
"plot", "",
"print", "",
"poke", "",
"next", "",
"pause", "",
"let", "",
"list", "",
"load", "",
"input", "",
"go sub", "gosub",
"go to", "goto",
"for", "",
"rem", "",
"dim", "",
"continue", "",
"border", "",
"new", "",
"restore", "",
"data", "",
"read", "",
"stop", "",
"llist", "",
"lprint", "",
"out", "",
"over", "",
"inverse", "",
"bright", "",
"flash", "",
"paper", "",
"ink", "",
"circle", "",
"beep", "",
"verify", "",
"merge", "",
"close #", "close#",
"open #", "open#",
"erase", "",
"move", "",
"format", "",
"cat", "",
"def fn", "deffn",
"step", "",
"to", "",
"then", "",
"line", "",
"<>", "",
">=", "",
"<=", "",
"and", "",
"or", "",
"bin", "",
"not", "",
"chr$", "",
"str$", "",
"usr", "",
"in", "",
"peek", "",
"abs", "",
"sgn", "",
"sqr", "",
"int", "",
"exp", "",
"ln", "",
"atn", "",
"acs", "",
"asn", "",
"tan", "",
"cos", "",
"sin", "",
"len", "",
"val$", "",
"code", "",
"val", "",
"tab", "",
"at", "",
"attr", "",
"screen$", "",
"point", "",
"fn", "",
"pi", "",
"inkey$", "",
"rnd", "",
"play", "",
"spectrum", "",
NULL
};
/* the whole raw basic file is written to filebuf; no output is generated
* until the whole program has been converted. This is to allow a TAP
* to be output on a non-seekable file (stdout).
*/
#ifdef MSDOS
#define MAX_LABELS 500
unsigned char filebuf[32768];
char infile[256],outfile[256];
#else
#define MAX_LABELS 2000
unsigned char filebuf[49152];
char infile[1024],outfile[1024];
#endif
#define MAX_LABEL_LEN 16
/* this is needed for tap files too: */
unsigned char headerbuf[17];
int output_tap=1,use_labels=0;
unsigned int startline=0x8000;
int autostart=10,autoincr=2;
char speccy_filename[11];
int labelend=0;
unsigned char labels[MAX_LABELS][MAX_LABEL_LEN+1];
int label_lines[MAX_LABELS];
unsigned char startlabel[MAX_LABEL_LEN+1];
#ifndef HAVE_GETOPT
/* ok, well here's a crude getopt clone I wrote a few years ago.
* It's not great, but it's good enough for zmakebas at least.
*/
int optopt=0,opterr=0,optind=1;
char *optarg=NULL;
/* holds offset in current argv[] value */
static int optpos=1;
/* This routine assumes that the caller is pretty sane and doesn't
* try passing an invalid 'optstring' or varying argc/argv.
*/
int getopt(int argc,char *argv[],char *optstring)
{
char *ptr;
/* check for end of arg list */
if(optind==argc || *(argv[optind])!='-' || strlen(argv[optind])<=1)
return(-1);
if((ptr=strchr(optstring,argv[optind][optpos]))==NULL)
return('?'); /* error: unknown option */
else
{
optopt=*ptr;
if(ptr[1]==':')
{
if(optind==argc-1) return(':'); /* error: missing option */
optarg=argv[optind+1];
optpos=1;
optind+=2;
return(optopt); /* return early, avoiding the normal increment */
}
}
/* now increment position ready for next time.
* no checking is done for the end of args yet - this is done on
* the next call.
*/
optpos++;
if(optpos>=strlen(argv[optind]))
{
optpos=1;
optind++;
}
return(optopt); /* return the found option */
}
#endif /* !HAVE_GETOPT */
/* dbl2spec() converts a double to an inline-basic-style speccy FP number.
*
* usage: dbl2spec(num,&exp,&man);
*
* num is double to convert.
* pexp is an int * to where to return the exponent byte.
* pman is an unsigned long * to where to return the 4 mantissa bytes.
* bit 31 is bit 7 of the 0th (first) mantissa byte, and bit 0 is bit 0 of
* the 3rd (last). As such, the unsigned long returned *must* be written
* to whatever file in big-endian format to make any sense to a speccy.
*
* returns 1 if ok, 0 if exponent too big.
*/
int dbl2spec(double num,int *pexp,unsigned long *pman)
{
int exp;
unsigned long man;
/* check for small integers */
if(num==(long)num && num>=-65535.0 && num<=65535.0)
{
/* ignores sign - see below, which applies to ints too. */
long tmp=(long)fabs(num);
exp=0;
man=((tmp%256)<<16)|((tmp>>8)<<8);
}
else
{
int f;
/* It appears that the sign bit is always left as 0 when floating-point
* numbers are embedded in programs, and the speccy appears to use the
* '-' character to detemine negativity - tests confirm this.
* As such, we *completely ignore* the sign of the number.
* exp is 0x80+exponent.
*/
num=fabs(num);
/* binary standard form goes from 0.50000... to 0.9999...(dec), like
* decimal which goes from 0.10000... to 0.9999....
*/
/* as such, if the number is >=1, it gets divided by 2, and exp++.
* And if the number is <0.5, it gets multiplied by 2, and exp--.
*/
exp=0;
while(num>=1.0)
{
num/=2.0; exp++;
}
while(num<0.5)
{
num*=2.0; exp--;
}
/* so now the number is in binary standard form in exp and num.
* we check the range of exp... -128 <= exp <= 127.
* (if outside, we return error (i.e. 0))
*/
if(exp<-128 || exp>127) return(0);
exp=128+exp;
/* so now all we need to do is roll the bits off the mantissa in `num'.
* we start at the 0.5ths bit at bit 0, and shift left 1 each time
* round the loop.
*/
num*=2.0; /* make it so that the 0.5ths bit is the integer part, */
/* and the rest is the fractional (0.xxx) part. */
man=0;
for(f=0;f<32;f++)
{
man<<=1;
man|=(int)num;
num-=(int)num;
num*=2.0;
}
/* Now, if (int)num is non-zero (well, 1) then we should generally
* round up 1. We don't do this if it would cause an overflow in the
* mantissa, though.
*/
if((int)num && man!=0xFFFFFFFF) man++;
/* finally, zero out the top bit */
man&=0x7FFFFFFF;
}
/* done */
*pexp=exp; *pman=man;
return(1);
}
unsigned long grok_hex(unsigned char **ptrp,int textlinenum)
{
static char *hexits="0123456789abcdefABCDEF",*lookup;
unsigned char *ptr=*ptrp;
unsigned long v=0,n;
/* we know the number starts with "0x" and we're pointing to it */
ptr+=2;
if(strchr(hexits,*ptr)==NULL)
{
fprintf(stderr,"line %d: bad BIN 0x... number\n",textlinenum);
exit(1);
}
while(*ptr && (lookup=strchr(hexits,*ptr))!=NULL)
{
n=lookup-hexits;
if(n>15) n-=6;
v=v*16+n;
ptr++;
}
*ptrp=ptr;
return(v);
}
unsigned long grok_binary(unsigned char **ptrp,int textlinenum)
{
unsigned long v=0;
unsigned char *ptr=*ptrp;
while(isspace(*ptr)) ptr++;
if(*ptr!='0' && *ptr!='1')
{
fprintf(stderr,"line %d: bad BIN number\n",textlinenum);
exit(1);
}
if(ptr[1]=='x' || ptr[1]=='X')
{
*ptrp=ptr;
return(grok_hex(ptrp,textlinenum));
}
while(*ptr=='0' || *ptr=='1')
{
v*=2;
v+=*ptr-'0';
ptr++;
}
*ptrp=ptr;
return(v);
}
void usage_help()
{
printf("zmakebas 1.2b - public domain by Russell Marks.\n\n");
printf("usage: zmakebas [-hlr] [-a line] [-i incr] [-n speccy_filename]\n");
printf(" [-o output_file] [-s line] [input_file]\n\n");
printf(" -a set auto-start line of basic file (default none).\n");
printf(" -h give this usage help.\n");
printf(" -i in labels mode, set line number incr. (default 2).\n");
printf(" -l use labels rather than line numbers.\n");
printf(" -n set Spectrum filename (to be given in tape header).");
printf("\n -o specify output file (default `%s').\n",
DEFAULT_OUTPUT);
printf(" -r output raw headerless file (default is .tap file).\n");
printf(" -s in labels mode, set starting line number ");
printf("(default 10).\n");
}
/* cmdline option parsing routine.
*/
void parse_options(int argc,char *argv[])
{
int done=0;
done=0;
opterr=0;
startlabel[0]=0;
do
switch(getopt(argc,argv,"a:hi:ln:o:rs:"))
{
case 'a':
if(*optarg=='@')
if(strlen(optarg+1)>MAX_LABEL_LEN)
fprintf(stderr,"Auto-start label too long\n"),exit(1);
else
strcpy(startlabel,optarg+1);
else
{
startline=(unsigned int)atoi(optarg);
if(startline>9999)
fprintf(stderr,"Auto-start line must be in the range 0 to 9999.\n"),
exit(1);
}
break;
case 'h': /* usage help */
usage_help();
exit(1);
case 'i':
autoincr=(int)atoi(optarg);
/* this is unnecessarily restrictive but keeps things a bit sane */
if(autoincr<1 || autoincr>1000)
fprintf(stderr,"Label line incr. must be in the range 1 to 1000.\n"),
exit(1);
break;
case 'l':
use_labels=1;
break;
case 'n':
strncpy(speccy_filename,optarg,10);
speccy_filename[10]=0;
break;
case 'o':
if(strlen(optarg)>sizeof(outfile)-1)
fprintf(stderr,"Filename too long\n"),exit(1);
else
strcpy(outfile,optarg);
break;
case 'r': /* output raw file */
output_tap=0; break;
case 's':
autostart=(int)atoi(optarg);
if(autostart<0 || autostart>9999)
fprintf(stderr,"Label start line must be in the range 0 to 9999.\n"),
exit(1);
break;
case '?':
switch(optopt)
{
case 'a':
fprintf(stderr,"The `a' option takes a line number arg.\n");
break;
case 'i':
fprintf(stderr,"The `i' option takes a line incr. arg.\n");
break;
case 'n':
fprintf(stderr,"The `n' option takes a Spectrum filename arg.\n");
break;
case 'o':
fprintf(stderr,"The `o' option takes a filename arg.\n");
break;
case 's':
fprintf(stderr,"The `s' option takes a line number arg.\n");
break;
default:
fprintf(stderr,"Option `%c' not recognised.\n",optopt);
}
exit(1);
case -1:
done=1;
}
while(!done);
if(optind<argc-1) /* two or more remaining args */
usage_help(),exit(1);
if(optind==argc-1) /* one remaining arg */
{
if(strlen(argv[optind])>sizeof(infile)-1)
fprintf(stderr,"Filename too long\n"),exit(1);
else
strcpy(infile,argv[optind]);
}
}
int grok_block(unsigned char *ptr,int textlinenum)
{
static char *lookup[]=
{
" ", " '", "' ", "''", " .", " :", "'.", "':",
". ", ".'", ": ", ":'", "..", ".:", ":.", "::",
NULL
};
char **lptr;
int f=128,v=-1;
for(lptr=lookup;*lptr!=NULL;lptr++,f++)
{
if(strncmp(ptr+1,*lptr,2)==0)
{
v=f;
break;
}
}
if(v==-1)
{
fprintf(stderr,"line %d: invalid block graphics escape\n",textlinenum);
exit(1);
}
return(v);
}
int main(int argc,char *argv[])
{
#ifdef MSDOS
static unsigned char buf[512],lcasebuf[512],outbuf[1024];
#else
/* deliberately very large buffers, to allow e.g. embedding of m/c
* programs in REM statements. 48k*8 should cover all eventualities,
* and not generally be a problem at this point. I mean, it's not
* even a megabyte in total, what's that between friends? :-)
*
* Note that these need to be large even if line-continuation
* backslashes are used, as the line is constructed in buf[]. outbuf
* is in the Spectrum's tokenised format, and can be much smaller.
*/
static unsigned char buf[8*49152],lcasebuf[8*49152];
static unsigned char outbuf[49152];
#endif
int f,toknum,toklen,linenum,linelen,in_quotes,in_rem,in_deffn,lastline;
char **tarrptr;
unsigned char *ptr,*ptr2,*linestart,*outptr,*remptr,*fileptr,*asciiptr;
double num;
int num_exp;
unsigned long num_mantissa,num_ascii;
int textlinenum;
int chk=0;
int alttok;
int passnum=1;
FILE *in=stdin,*out=stdout;
strcpy(speccy_filename,"");
strcpy(infile,"-");
strcpy(outfile,DEFAULT_OUTPUT);
parse_options(argc,argv);
if(strcmp(infile,"-")!=0 && (in=fopen(infile,"r"))==NULL)
fprintf(stderr,"Couldn't open input file.\n"),exit(1);
fileptr=filebuf;
linenum=-1; /* to set lastline */
/* we make one pass if using line numbers, two if using labels */
do
{
if(use_labels) linenum=autostart-autoincr;
textlinenum=0;
if(passnum>1 && fseek(in,0L,SEEK_SET)!=0)
{
fprintf(stderr,"Need seekable input for label support\n");
exit(1);
}
while(fgets(buf+1,sizeof(buf)-1,in)!=NULL)
{
buf[0]=32; /* just in case, for all the ptr[-1] stuff */
textlinenum++;
lastline=linenum;
if(buf[strlen(buf)-1]=='\n') buf[strlen(buf)-1]=0;
/* allow for (shell-style) comments which don't appear in the program,
* and also ignore blank lines.
*/
if(buf[1]==0 || buf[1]=='#') continue;
/* check for line continuation; the "*buf" checks here aren't strictly
* necessary (see buf[0] above) but I'm just happier this way. :-)
*/
while(*buf && buf[strlen(buf)-1]=='\\' && !feof(in))
{
f=strlen(buf)-1;
if(fgets(buf+f,sizeof(buf)-f,in))
textlinenum++;
else
{
if(*buf) buf[strlen(buf)-1]=0; /* remove backslash on EOF */
}
if(*buf && buf[strlen(buf)-1]=='\n') buf[strlen(buf)-1]=0;
}
if(strlen(buf)>=sizeof(buf)-MAX_LABEL_LEN-1)
{
/* this is nasty, but given how the label substitution works it's
* probably the safest thing to do.
*/
fprintf(stderr,"line %d: line too big for input buffer\n",textlinenum);
exit(1);
}
/* get line number (or assign one) */
if(use_labels)
{
linestart=buf;
/* assign a line number */
linenum+=autoincr;
if(linenum>9999)
fprintf(stderr,"Generated line number is >9999 - %s\n",
(autostart>1 || autoincr>1)?"try using `-s 1 -i 1'"
:"too many lines!"),
exit(1);
}
else
{
ptr=buf;
while(isspace(*ptr)) ptr++;
if(!isdigit(*ptr))
{
fprintf(stderr,"line %d: missing line number\n",textlinenum);
exit(1);
}
linenum=(int)strtol(ptr,(char **)&linestart,10);
if(linenum<=lastline)
{
fprintf(stderr,"line %d: line no. not greater than previous one\n",
textlinenum);
exit(1);
}
}
if(linenum<0 || linenum>9999)
{
fprintf(stderr,"line %d: line no. out of range\n",textlinenum);
exit(1);
}
/* lose remaining spaces */
while(isspace(*linestart)) linestart++;
/* check there's no line numbers on label-using programs */
if(use_labels && isdigit(*linestart))
{
fprintf(stderr,"line %d: line number used in labels mode\n",
textlinenum);
exit(1);
}
if(use_labels && *linestart=='@')
{
if((ptr=strchr(linestart,':'))==NULL)
{
fprintf(stderr,"line %d: incomplete token definition\n",textlinenum);
exit(1);
}
if(ptr-linestart-1>MAX_LABEL_LEN)
{
fprintf(stderr,"line %d: token too long\n",textlinenum);
exit(1);
}
if(passnum==1)
{
*ptr=0;
label_lines[labelend]=linenum;
strcpy(labels[labelend++],linestart+1);
if(labelend>=MAX_LABELS)
{
fprintf(stderr,"line %d: too many labels\n",textlinenum);
exit(1);
}
for(f=0;f<labelend-1;f++)
if(strcmp(linestart+1,labels[f])==0)
{
fprintf(stderr,"iine %d: attempt to redefine label\n",textlinenum);
exit(1);
}
*ptr=':';
}
linestart=ptr+1;
while(isspace(*linestart)) linestart++;
/* if now blank, don't bother inserting an actual line here;
* instead, fiddle linenum so the next line will have the
* same number.
*/
if(*linestart==0)
{
linenum-=autoincr;
continue;
}
}
if(use_labels && passnum==1) continue;
/* make token comparison copy of line. this has lowercase letters and
* blanked-out strings.
*/
ptr=linestart; in_quotes=0;
ptr2=lcasebuf;
while(*ptr)
{
if(*ptr=='"') in_quotes=!in_quotes;
if(in_quotes && *ptr!='"')
*ptr2++=32;
else
*ptr2++=tolower(*ptr);
ptr++;
}
*ptr2=0;
/* now convert any token without letters either side to the correct
* speccy token number. (Any space this leaves in the string is replaced
* by 0x01 chars.)
*
* However, we need to check for REM first. If found, no token/num stuff
* is performed on the line after that point.
*/
remptr=NULL;
if((ptr=strstr(lcasebuf,"rem"))!=NULL &&
!isalpha(ptr[-1]) && !isalpha(ptr[3]))
{
ptr2=linestart+(ptr-lcasebuf);
/* endpoint for checks must be here, then. */
remptr=ptr2;
*remptr=*ptr=0;
/* the zero will be replaced with the REM token later */
ptr2[1]=ptr[1]=ptr2[2]=ptr[2]=1;
/* absorb at most one trailing space */
if(ptr[3]==' ') ptr2[3]=ptr[3]=1;
}
toknum=256; alttok=1;
for(tarrptr=tokens;*tarrptr!=NULL;tarrptr++)
{
if(alttok) toknum--;
alttok=!alttok;
switch(toknum)
{
case VAL_TOKEN_NUM: toknum=VALSTR_TOKEN_NUM; break;
case VALSTR_TOKEN_NUM: toknum=VAL_TOKEN_NUM;
}
if(**tarrptr==0) continue;
toklen=strlen(*tarrptr);
ptr=lcasebuf;
while((ptr=strstr(ptr,*tarrptr))!=NULL)
{
/* check it's not in the middle of a word etc., except for
* <>, <=, >=.
*/
if((*tarrptr)[0]=='<' || (*tarrptr)[1]=='=' ||
(!isalpha(ptr[-1]) && !isalpha(ptr[toklen])))
{
ptr2=linestart+(ptr-lcasebuf);
/* the token text is overwritten in the lcase copy too, to
* avoid problems with e.g. go to/to.
*/
*ptr2=*ptr=toknum;
for(f=1;f<toklen;f++) ptr2[f]=ptr[f]=1;
/* absorb trailing spaces too */
while(ptr2[f]==' ')
ptr2[f++]=1;
/* for BIN, we need the token right before the number. */
if(toknum==BIN_TOKEN_NUM)
{
*ptr2=*ptr=1;
ptr2[f-1]=ptr[f-1]=toknum;
}
}
ptr+=toklen;
}
}
if(use_labels)
{
/* replace @label with matching number.
* this expands labels in strings too, since:
* 1. it seems reasonable to assume you might want this;
* 2. it makes the code a bit simpler :-) ;
* 3. you can use the escape `\@' to get a literal `@' anyway.
*/
ptr=linestart;
while((ptr=strchr(ptr,'@'))!=NULL)
{
if(ptr[-1]=='\\')
{
ptr++;
continue;
}
/* the easiest way to spot them is to try matching against
* each label in turn. It's gross, but at least it's sane
* and doesn't restrict what you can have as a label.
* We also test that the char after a match is not a printable
* ascii char (other than space or colon), to prevent matches
* against the shorter of two possibilities.
*/
ptr++;
for(f=0;f<labelend;f++)
{
int len=strlen(labels[f]);
if(memcmp(labels[f],ptr,len)==0 &&
(ptr[len]<33 || ptr[len]>126 || ptr[len]==':'))
{
static unsigned char numbuf[64];
/* this could be optimised to use a single memmove(), but
* at least this way it's clear(er) what's happening.
*/
/* switch text for label. first, remove text */
memmove(ptr-1,ptr+len,strlen(ptr+len)+1);
/* make number string */
sprintf(numbuf,"%d",label_lines[f]);
len=strlen(numbuf);
/* insert room for number string */
ptr--;
memmove(ptr+len,ptr,strlen(ptr)+1);
memcpy(ptr,numbuf,len);
ptr+=len;
break;
}
}
if(f==labelend)
{
fprintf(stderr,"line %d: undefined label\n",textlinenum);
exit(1);
}
}
}
if(remptr) *remptr=REM_TOKEN_NUM;
/* remove 0x01s, deal with backslash things, and add numbers */
ptr=linestart;
outptr=outbuf;
in_rem=in_deffn=in_quotes=0;
while(*ptr)
{
if(outptr>outbuf+sizeof(outbuf)-10)
{
fprintf(stderr,"line %d: line too big\n",textlinenum);
exit(1);
}
if(*ptr=='"') in_quotes=!in_quotes;
/* as well as 0x01 chars, we skip tabs. */
if(*ptr==1 || *ptr==9 || (!in_quotes && !in_rem && *ptr==' '))
{
ptr++;
continue;
}
if(*ptr==DEFFN_TOKEN_NUM) in_deffn=1;
if(*ptr==REM_TOKEN_NUM) in_rem=1;
if(*ptr=='\\')
{
if(isalpha(ptr[1]) && strchr("VWXYZvwxyz",ptr[1])==NULL)
*outptr++=144+tolower(ptr[1])-'a';
else
switch(ptr[1])
{
case '\\': *outptr++='\\'; break;
case '@': *outptr++='@'; break;
case '*': *outptr++=127; break; /* copyright symbol */
case '\'': case '.': case ':': case ' ': /* block graphics char */
*outptr++=grok_block(ptr,textlinenum);
ptr++;
break;
case '{': /* directly specify output code */
/* find end of number */
asciiptr=strchr(ptr+2,'}');
if(asciiptr==NULL)
{
fprintf(stderr,
"line %d: unclosed brace in eight-bit character code\n",
textlinenum);
exit(1);
}
/* parse number in decimal, octal or hex */
num_ascii=strtoul(ptr+2, NULL, 0);
if(num_ascii<0 || num_ascii>255)
{
fprintf(stderr,
"line %d: eight-bit character code out of range\n",
textlinenum);
exit(1);
}
*outptr++=(char)num_ascii;
/* set pointer to the second char from the end, so we're in the
* right place when we skip forward two chars below
*/
ptr=asciiptr-1;
break;
default:
fprintf(stderr,
"line %d: warning: unknown escape `%c', inserting literally\n",
textlinenum,ptr[1]);
*outptr++=ptr[1];
}
ptr+=2;
continue;
}
/* spot any numbers (because we have to add the inline FP
* representation). We do this largely by relying on strtod(),
* so that we only have to find the start - i.e. a non-alpha char,
* an optional `-' or `+', an optional `.', then a digit.
*/
if(!in_rem && !in_quotes && !isalpha(ptr[-1]) &&
(isdigit(*ptr) ||
((*ptr=='-' || *ptr=='+' || *ptr=='.') && isdigit(ptr[1])) ||
((*ptr=='-' || *ptr=='+') && ptr[1]=='.' && isdigit(ptr[2]))))
{
if(ptr[-1]!=BIN_TOKEN_NUM)
/* we have a number. parse with strtod(). */
num=strtod(ptr,(char **)&ptr2);
else
{
/* however, if the number was after a BIN token, the inline
* number must match the binary number, e.g. BIN 1001 would be
* followed by an inline 9.
*/
ptr2=ptr;
num=(double)grok_binary(&ptr2,textlinenum);
}
/* output text of number */
memcpy(outptr,ptr,ptr2-ptr);
outptr+=ptr2-ptr;
*outptr++=0x0e;
if(!dbl2spec(num,&num_exp,&num_mantissa))
{
fprintf(stderr,"line %d: exponent out of range (number too big)\n",
textlinenum);
exit(1);
}
*outptr++=num_exp;
*outptr++=(num_mantissa>>24);
*outptr++=(num_mantissa>>16);
*outptr++=(num_mantissa>>8);
*outptr++=(num_mantissa&255);
ptr=ptr2;
}
else
{
/* special def fn case */
if(in_deffn)
{
if(*ptr=='=')
in_deffn=0;
else
{
if(*ptr==',' || *ptr==')')
{
*outptr++=0x0e;
*outptr++=0;
*outptr++=0;
*outptr++=0;
*outptr++=0;
*outptr++=0;
*outptr++=*ptr++;
}
if(*ptr!=' ')
{
if(*ptr=='=')
in_deffn=0;
*outptr++=*ptr++;
}
}
}
else
{
/* if not number, just output char */
*outptr++=*ptr++;
}
}
}
*outptr++=0x0d; /* add terminating CR */
/* output line */
linelen=outptr-outbuf;
if(fileptr+4+linelen>=filebuf+sizeof(filebuf))
{
/* the program would be too big to load into a speccy long before
* you'd get this error, but FWIW...
*/
fprintf(stderr,"program too big!\n");
exit(1);
}
*fileptr++=(linenum>>8);
*fileptr++=(linenum&255);
*fileptr++=(linelen&255);
*fileptr++=(linelen>>8);
memcpy(fileptr,outbuf,linelen);
fileptr+=linelen;
} /* end of pass-making while() */
passnum++;
} /* end of do..while() pass loop */
while(use_labels && passnum<=2);
if(in!=stdin) fclose(in);
/* we only need to do this if outputting a .tap, but might as well do
* it to check the label's ok anyway.
*/
if(*startlabel)
{
/* this is nearly a can't happen error, but FWIW... */
if(!use_labels)
fprintf(stderr,"Auto-start label specified, but not using labels!\n"),
exit(1);
for(f=0;f<labelend;f++)
if(strcmp(startlabel,labels[f])==0)
{
startline=label_lines[f]; break;
}
if(f==labelend)
fprintf(stderr,"Auto-start label is undefined\n"),exit(1);
}
/* write output file */
if(strcmp(outfile,"-")!=0 && (out=fopen(outfile,"wb"))==NULL)
fprintf(stderr,"Couldn't open output file.\n"),exit(1);
if(output_tap)
{
unsigned int siz=fileptr-filebuf;
/* make header */
headerbuf[0]=0;
for(f=strlen(speccy_filename);f<10;f++)
speccy_filename[f]=32;
strncpy(headerbuf+1,speccy_filename,10);
headerbuf[11]=(siz&255);
headerbuf[12]=(siz/256);
headerbuf[13]=(startline&255);
headerbuf[14]=(startline/256);
headerbuf[15]=(siz&255);
headerbuf[16]=(siz/256);
/* write header */
fprintf(out,"%c%c%c",19,0,chk=0);
for(f=0;f<17;f++) chk^=headerbuf[f];
fwrite(headerbuf,1,17,out);
fputc(chk,out);
/* write (most of) tap bit for data block */
fprintf(out,"%c%c%c",(siz+2)&255,(siz+2)>>8,chk=255);
for(f=0;f<siz;f++) chk^=filebuf[f];
}
fwrite(filebuf,1,fileptr-filebuf,out);
if(output_tap)
fputc(chk,out);
if(out!=stdout) fclose(out);
exit(0);
}
|