asoft_detoken: clean up whitespace, add command line args

This commit is contained in:
Vince Weaver 2017-04-23 22:43:35 -04:00
parent 18a66c1427
commit 60322f95cb

View File

@ -1,10 +1,14 @@
/* asoft_detoken: detokenize an applesoft BASIC program */
/* by Vince Weaver (vince@deater.net) */
#include <stdio.h> #include <stdio.h>
#include <string.h> /* strlen() */ #include <string.h> /* strlen() */
#include <unistd.h> /* getopt() */
#include "version.h" #include "version.h"
/* Starting at 0x80 */ /* Starting at 0x80 */
char applesoft_tokens[][8]={ char applesoft_tokens[][8]={
/* 80 */ "END","FOR","NEXT","DATA","INPUT","DEL","DIM","READ", /* 80 */ "END","FOR","NEXT","DATA","INPUT","DEL","DIM","READ",
/* 88 */ "GR","TEXT","PR #","IN #","CALL","PLOT","HLIN","VLIN", /* 88 */ "GR","TEXT","PR #","IN #","CALL","PLOT","HLIN","VLIN",
/* 90 */ "HGR2","HGR","HCOLOR=","HPLOT","DRAW","XDRAW","HTAB","HOME", /* 90 */ "HGR2","HGR","HCOLOR=","HPLOT","DRAW","XDRAW","HTAB","HOME",
@ -24,61 +28,92 @@ char applesoft_tokens[][8]={
}; };
int main(int argc, char **argv) { int main(int argc, char **argv) {
int ch1,i;
int size1,size2;
int line1,line2;
int link1,link2,link;
int debug=0;
int offset=0x801;
/* read size, first two bytes */
size1=fgetc(stdin);
size2=fgetc(stdin);
if (debug) fprintf(stderr,"File size: %x %x\n",size1,size2);
while(!feof(stdin)) { int ch1,i;
int size1,size2;
/* link points to the next line */ int line1,line2;
/* assumes asoft program starts at address $801 */ int link1,link2,link;
link1=fgetc(stdin); int debug=0,print_link=0;
link2=fgetc(stdin); int offset=0x801;
link=(link2<<8)|link1; int c;
offset+=2; FILE *fff;
/* link==0 indicates EOF */ /* Check command line arguments */
if (link==0) goto the_end; while ((c = getopt (argc, argv,"dl"))!=-1) {
switch (c) {
/* line number is little endian 16-bit value */
line1=fgetc(stdin); case 'd':
line2=fgetc(stdin); debug=1;
if (feof(stdin)) goto the_end; break;
printf("%4d ",((line2)<<8)+line1); case 'l':
offset+=2; print_link=1;
break;
/* repeat until EOL character (0) */ }
while( (ch1=fgetc(stdin))!=0 ) { }
offset++;
/* if > 0x80 it's a token */ /* No file specified, used stdin */
if (ch1>=0x80) { if (optind==argc) {
fputc(' ',stdout); fff=stdin;
for(i=0;i<strlen(applesoft_tokens[ch1-0x80]);i++) { }
fputc(applesoft_tokens[ch1-0x80][i],stdout); else {
} fff=fopen(argv[optind],"r");
fputc(' ',stdout); if (fff==NULL) {
} fprintf(stderr,"Error, could not open %s\n",argv[optind]);
/* otherwise it is an ascii char */ return -1;
else { }
fputc(ch1,stdout);
} }
}
offset++; /* read size, first two bytes */
printf("\n"); size1=fgetc(fff);
if (link!=offset) fprintf(stderr,"WARNING! link!=offset %x %x\n", size2=fgetc(fff);
link,offset);
} if (debug) fprintf(stderr,"File size: %x %x\n",size1,size2);
the_end:;
return 0; while(!feof(fff)) {
/* link points to the next line */
/* assumes asoft program starts at address $801 */
link1=fgetc(fff);
link2=fgetc(fff);
link=(link2<<8)|link1;
offset+=2;
if (print_link) printf("%04X:",link);
/* link==0 indicates EOF */
if (link==0) goto the_end;
/* line number is little endian 16-bit value */
line1=fgetc(fff);
line2=fgetc(fff);
if (feof(fff)) goto the_end;
printf("%4d ",((line2)<<8)+line1);
offset+=2;
/* repeat until EOL character (0) */
while( (ch1=fgetc(fff))!=0 ) {
offset++;
/* if > 0x80 it's a token */
if (ch1>=0x80) {
fputc(' ',stdout);
for(i=0;i<strlen(applesoft_tokens[ch1-0x80]);i++) {
fputc(applesoft_tokens[ch1-0x80][i],stdout);
}
fputc(' ',stdout);
}
/* otherwise it is an ascii char */
else {
fputc(ch1,stdout);
}
}
offset++;
printf("\n");
if (link!=offset) {
fprintf(stderr,"WARNING! link!=offset %x %x\n",link,offset);
}
}
the_end:;
return 0;
} }