home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Module: aspiff.c - Program to spiff up assembly source files */
- /* generated by D/R (Alcyon) C Compiler. */
- /* Re-aligns spacing, and changes Alcyon register */
- /* notation from R0-R15 to D0-D7 and A0-A7 */
- /* */
- /* Programmer: George R. Woodside */
- /* */
- /* Date: January 17, 1987 */
- /* */
- /* Flags: -i file Specify name of input file. */
- /* Default is stdin. */
- /* (If a parameter appears with no flag, it */
- /* is accepted as the name of the input file.) */
- /* -o file Specify name of output file. */
- /* Default is stdout. */
- /* */
- /****************************************************************************/
-
- #include <stdio.h>
- #include <osbind.h>
- #include <ctype.h>
-
- #define MAXLINE 255
-
- extern char *optarg; /* command line argument */
- extern int optind; /* command line argument index */
-
- int i; /* counter */
-
- int i_flag = 0; /* input file specified */
- int o_flag = 0; /* output file specified */
-
- char line[MAXLINE+1]; /* input line */
- char *infile; /* input file name */
- char *outfile; /* output file name */
- char *pname = "aspiff"; /* default program name */
-
- /****************************************************************************/
- /* */
- /* Check command line for specified file names */
- /* */
- /****************************************************************************/
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- FILE *fp_in; /* input file */
-
- register int c; /* input byte */
-
- /***************************************************************************/
- /* */
- /* If unix system, save program name. */
- /* */
- /***************************************************************************/
-
- #ifdef UNIX
- pname = argv[0]; /* name is always first parameter. */
- #endif
-
- if(argc < 2)
- {
- printf("Usage: %s -i file -o file\n",pname);
- printf("\t-i file = input file name\n");
- printf("\t-o file = output file name\n");
- exit(1);
- }
-
- while((c=getopt(argc,argv,"I:O:i:o:")) != EOF)
- {
- switch (c)
- {
- case('I'):
- case('i'):
- i_flag = 1; /* show file named */
- infile = optarg; /* input file name */
- break;
-
- case('O'):
- case('o'):
- o_flag = 1; /* show name is set */
- outfile = optarg; /* input file name */
- break;
-
- } /* end switch */
- } /* end while */
-
- while(optind != argc) /* if leftover arguments, */
- {
- if(i_flag == 0)
- {
- infile = argv[optind++];
- i_flag = 1;
- }
-
- else
- printf("%s: Unknown argument: %s\n",pname,argv[optind++]);
-
- } /* end while */
-
- if(i_flag)
- {
- if( (fp_in = fopen(infile,"r") ) == NULL)
- {
- printf("%s: Unable to open input file %s\n",pname,infile);
- exit(1);
- } /* end open error */
- else
- spiffo(fp_in); /* do the file */
-
- } /* end if i_flag */
- else
- spiffo(stdin); /* do standard in */
-
- } /* end main */
-
- /****************************************************************************/
- /* */
- /* Now we try to open the output file if needed */
- /* */
- /****************************************************************************/
-
- spiffo(fin)
- FILE *fin;
- {
- FILE *fout;
-
- if(o_flag)
- {
- if( (fout = fopen(outfile,"w") ) == NULL)
- {
- printf("%s: Unable to open output file %s\n",pname,outfile);
- exit(1);
- } /* end open error */
- else
- spiff(fin,fout); /* do the file */
- } /* end if o_flag */
- else
- spiff(fin,stdout); /* do standard out */
-
- }
-
- /****************************************************************************/
- /* */
- /* Now we process the file */
- /* */
- /****************************************************************************/
-
- spiff(ifp,ofp)
- FILE *ifp;
- FILE *ofp;
- {
- register int flag; /* label flag */
- register int start; /* start of code in line */
- register int c; /* data character */
-
- while( (fgets(line,MAXLINE,ifp) ) != NULL)
- {
- flag = 0; /* not in a label */
- start = 0; /* no code yet */
- i = 0; /* at column 0 */
-
- if(line[0] == '*') /* if this line is a comment, */
- fprintf(ofp,"%s",line); /* print unchanged */
- else
- {
- while(line[i]) /* scan length of line */
- {
- if(line[i++] == ':') /* if we find a separator, */
- flag = 1; /* we have a label */
- } /* end scan for label */
-
- /******************************************************************/
- /* If there is a label, print it on a line alone. */
- /* Then bump the line start pointer past them. */
- /******************************************************************/
-
- if(flag) /* if we found a label */
- {
- while(line[start] != ':') /* get byte until label symbol */
- fprintf(ofp,"%c",line[start++]); /* and write it */
-
- fprintf(ofp,":\n"); /* add on the separator */
- start++; /* skip the colon */
- } /* end found a label */
-
- /******************************************************************/
- /* If there is anything left on the line, */
- /* tab over, and copy it out. */
- /******************************************************************/
-
- i=0; /* reset byte counter */
- while(isspace(line[start])) /* skip spaces */
- start++; /* move toward data, if any */
-
- if(line[start]) /* if there was some data, */
- {
- dotab(ofp); /* first, tab in */
-
- rereg(&line[start]); /* revise register notation */
- while( c = line[start++] )/* for the rest of the line, */
- {
- if( ( c == ' ') || ( c == '\t') ) /* if white space, */
- dotab(ofp); /* then tab over */
- else /* if text, */
- {
- if( c != '\n' ) /* if not a newline, */
- {
- fprintf(ofp,"%c",c); /* copy the text byte */
- i++; /* and count the putput */
- } /* end not newline */
- } /* end text byte */
- } /* end rest of line */
- if(i) /* if there was any data, */
- fprintf(ofp,"\n"); /* then new line */
- } /* end data left in line */
- } /* end of line with data */
- } /* end of all lines */
- return(0); /* worked ok */
- } /* end spiff one line */
-
- /****************************************************************************/
- /* Tab over to next stop (modulo 8) */
- /****************************************************************************/
-
- dotab(ofp)
- FILE *ofp;
- {
- fprintf(ofp,"%c",' '); /* insure one space */
- i++; /* count it */
- while(i&7) /* until we hit the tab, */
- {
- fprintf(ofp,"%c",' '); /* space again */
- i++; /* and count it */
- } /* end space until mod 8 */
- } /* end dotab */
-
- /****************************************************************************/
- /* Redefine register labels from R0-R15 to D0-D7 and A0-A7 */
- /****************************************************************************/
- rereg(data)
- char *data;
- {
- register int c; /* current character */
- register int r; /* current character */
- register int tens; /* high register number */
- register int ones; /* low register number */
-
- register char *from = data; /* picking up here */
- register char *to = data; /* putting it back here */
-
- while(*from) /* until end of line */
- {
- if( (c = *from++) != 'R') /* if not a register, */
- *to++ = c; /* just copy it */
- else /* it may be a register */
- {
- tens = *from; /* get next byte */
- if(! isdigit(tens) ) /* if not a digit, */
- *to++ = c; /* treat the R as data */
- else /* we have a register */
- {
- ones = *++from; /* get next character */
- if(isdigit(ones) ) /* if second one is digit, */
- {
- from++; /* skip past it */
- r = (tens - '0') * 10 + (ones - '0'); /* convert number */
- } /* end two digit parse */
- else
- r = (tens - '0'); /* single digit register */
-
- if(r > 7) /* if an address register, */
- {
- r -= 8; /* adjust address register number */
- *to++ = 'A'; /* write the A */
- } /* end got address register */
- else /* must be a data register */
- *to++ = 'D'; /* so write D */
-
- *to++ = r + '0'; /* write register number */
- } /* end of digit parse */
- } /* end of a register assignemnt */
- } /* end of line */
- *to = '\0'; /* be sure terminated */
- } /* end rereg */
-