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,5 +1,9 @@
/* 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 */
@@ -29,36 +33,67 @@ int main(int argc, char **argv) {
int size1,size2; int size1,size2;
int line1,line2; int line1,line2;
int link1,link2,link; int link1,link2,link;
int debug=0; int debug=0,print_link=0;
int offset=0x801; int offset=0x801;
int c;
FILE *fff;
/* Check command line arguments */
while ((c = getopt (argc, argv,"dl"))!=-1) {
switch (c) {
case 'd':
debug=1;
break;
case 'l':
print_link=1;
break;
}
}
/* No file specified, used stdin */
if (optind==argc) {
fff=stdin;
}
else {
fff=fopen(argv[optind],"r");
if (fff==NULL) {
fprintf(stderr,"Error, could not open %s\n",argv[optind]);
return -1;
}
}
/* read size, first two bytes */ /* read size, first two bytes */
size1=fgetc(stdin); size1=fgetc(fff);
size2=fgetc(stdin); size2=fgetc(fff);
if (debug) fprintf(stderr,"File size: %x %x\n",size1,size2); if (debug) fprintf(stderr,"File size: %x %x\n",size1,size2);
while(!feof(stdin)) { while(!feof(fff)) {
/* link points to the next line */ /* link points to the next line */
/* assumes asoft program starts at address $801 */ /* assumes asoft program starts at address $801 */
link1=fgetc(stdin); link1=fgetc(fff);
link2=fgetc(stdin); link2=fgetc(fff);
link=(link2<<8)|link1; link=(link2<<8)|link1;
offset+=2; offset+=2;
if (print_link) printf("%04X:",link);
/* link==0 indicates EOF */ /* link==0 indicates EOF */
if (link==0) goto the_end; if (link==0) goto the_end;
/* line number is little endian 16-bit value */ /* line number is little endian 16-bit value */
line1=fgetc(stdin); line1=fgetc(fff);
line2=fgetc(stdin); line2=fgetc(fff);
if (feof(stdin)) goto the_end; if (feof(fff)) goto the_end;
printf("%4d ",((line2)<<8)+line1); printf("%4d ",((line2)<<8)+line1);
offset+=2; offset+=2;
/* repeat until EOL character (0) */ /* repeat until EOL character (0) */
while( (ch1=fgetc(stdin))!=0 ) { while( (ch1=fgetc(fff))!=0 ) {
offset++; offset++;
/* if > 0x80 it's a token */ /* if > 0x80 it's a token */
if (ch1>=0x80) { if (ch1>=0x80) {
@@ -72,12 +107,12 @@ int main(int argc, char **argv) {
else { else {
fputc(ch1,stdout); fputc(ch1,stdout);
} }
} }
offset++; offset++;
printf("\n"); printf("\n");
if (link!=offset) fprintf(stderr,"WARNING! link!=offset %x %x\n", if (link!=offset) {
link,offset); fprintf(stderr,"WARNING! link!=offset %x %x\n",link,offset);
}
} }
the_end:; the_end:;
return 0; return 0;