home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / telecom / 107 / c / aspiff.c next >
Encoding:
C/C++ Source or Header  |  1987-02-20  |  14.3 KB  |  289 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Module:      aspiff.c - Program to spiff up assembly source files        */
  4. /*                         generated by D/R (Alcyon) C Compiler.            */
  5. /*                         Re-aligns spacing, and changes Alcyon register   */
  6. /*                         notation from R0-R15 to D0-D7 and A0-A7          */
  7. /*                                                                          */
  8. /* Programmer:  George R. Woodside                                          */
  9. /*                                                                          */
  10. /* Date:        January 17, 1987                                            */
  11. /*                                                                          */
  12. /* Flags:       -i file     Specify name of input file.                     */
  13. /*                          Default is stdin.                               */
  14. /*                          (If a parameter appears with no flag, it        */
  15. /*                          is accepted as the name of the input file.)     */
  16. /*              -o file     Specify name of output file.                    */
  17. /*                          Default is stdout.                              */
  18. /*                                                                          */
  19. /****************************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <osbind.h>
  23. #include <ctype.h>
  24.  
  25. #define  MAXLINE  255
  26.  
  27. extern  char  *optarg;                  /* command line argument            */
  28. extern  int   optind;                   /* command line argument index      */
  29.  
  30. int  i;                                 /* counter                          */
  31.  
  32. int  i_flag = 0;                        /* input file specified             */
  33. int  o_flag = 0;                        /* output file specified            */
  34.  
  35. char line[MAXLINE+1];                   /* input line                       */
  36. char *infile;                           /* input file name                  */
  37. char *outfile;                          /* output file name                 */
  38. char *pname = "aspiff";                 /* default program name             */
  39.  
  40. /****************************************************************************/
  41. /*                                                                          */
  42. /* Check command line for specified file names                              */
  43. /*                                                                          */
  44. /****************************************************************************/
  45.  
  46. main(argc,argv)
  47. int  argc;
  48. char *argv[];
  49. {
  50.   FILE  *fp_in;                         /* input file                       */
  51.  
  52.   register  int  c;                     /* input byte                       */
  53.  
  54.  /***************************************************************************/
  55.  /*                                                                         */
  56.  /* If unix system, save program name.                                      */
  57.  /*                                                                         */
  58.  /***************************************************************************/
  59.  
  60. #ifdef UNIX
  61.   pname = argv[0];                      /* name is always first parameter.  */
  62. #endif
  63.  
  64.   if(argc < 2)
  65.     {
  66.       printf("Usage: %s -i file -o file\n",pname);
  67.       printf("\t-i file   = input file name\n");
  68.       printf("\t-o file   = output file name\n");
  69.       exit(1);
  70.     }
  71.  
  72.   while((c=getopt(argc,argv,"I:O:i:o:")) != EOF)
  73.     {
  74.       switch (c)
  75.         {
  76.           case('I'):
  77.           case('i'):
  78.             i_flag = 1;                 /* show file named                  */
  79.             infile = optarg;            /* input file name                  */
  80.             break;
  81.  
  82.           case('O'):
  83.           case('o'):
  84.             o_flag = 1;                 /* show name is set                 */
  85.             outfile = optarg;           /* input file name                  */
  86.             break;
  87.  
  88.         }                               /* end switch                       */
  89.       }                                 /* end while                        */
  90.  
  91.       while(optind != argc)             /* if leftover arguments,           */
  92.         {
  93.           if(i_flag == 0)
  94.             {
  95.               infile = argv[optind++];
  96.               i_flag = 1;
  97.             }
  98.  
  99.           else
  100.             printf("%s: Unknown argument: %s\n",pname,argv[optind++]);
  101.  
  102.         }                               /* end while                        */
  103.  
  104.       if(i_flag)
  105.         {
  106.           if( (fp_in = fopen(infile,"r") ) == NULL)
  107.             {
  108.               printf("%s: Unable to open input file %s\n",pname,infile);
  109.               exit(1);
  110.             }                           /* end open error                   */
  111.           else
  112.             spiffo(fp_in);              /* do the file                      */
  113.  
  114.         }                               /* end if i_flag                    */
  115.       else
  116.         spiffo(stdin);                  /* do standard in                   */
  117.  
  118. }                                       /* end main                         */
  119.  
  120. /****************************************************************************/
  121. /*                                                                          */
  122. /* Now we try to open the output file if needed                             */
  123. /*                                                                          */
  124. /****************************************************************************/
  125.  
  126. spiffo(fin)
  127. FILE  *fin;
  128. {
  129.   FILE  *fout;
  130.  
  131.   if(o_flag)
  132.     {
  133.       if( (fout = fopen(outfile,"w") ) == NULL)
  134.         {
  135.           printf("%s: Unable to open output file %s\n",pname,outfile);
  136.           exit(1);
  137.         }                               /* end open error                   */
  138.       else
  139.         spiff(fin,fout);                /* do the file                      */
  140.     }                                   /* end if o_flag                    */
  141.   else
  142.     spiff(fin,stdout);                  /* do standard out                  */
  143.  
  144. }
  145.  
  146. /****************************************************************************/
  147. /*                                                                          */
  148. /* Now we process the file                                                  */
  149. /*                                                                          */
  150. /****************************************************************************/
  151.  
  152. spiff(ifp,ofp)
  153. FILE  *ifp;
  154. FILE  *ofp;
  155. {
  156.   register  int  flag;                  /* label flag                       */
  157.   register  int  start;                 /* start of code in line            */
  158.   register  int  c;                     /* data character                   */
  159.  
  160.   while( (fgets(line,MAXLINE,ifp) ) != NULL)
  161.     {
  162.       flag  = 0;                        /* not in a label                   */
  163.       start = 0;                        /* no code yet                      */
  164.       i     = 0;                        /* at column 0                      */
  165.  
  166.       if(line[0] == '*')                /* if this line is a comment,       */
  167.         fprintf(ofp,"%s",line);         /* print unchanged                  */
  168.       else
  169.         {    
  170.           while(line[i])                /* scan length of line              */
  171.             {
  172.               if(line[i++] == ':')      /* if we find a separator,          */
  173.                 flag = 1;               /* we have a label                  */
  174.             }                           /* end scan for label               */
  175.  
  176.           /******************************************************************/
  177.           /* If there is a label, print it on a line alone.                 */
  178.           /* Then bump the line start pointer past them.                    */
  179.           /******************************************************************/
  180.  
  181.           if(flag)                      /* if we found a label              */
  182.             {
  183.               while(line[start] != ':') /* get byte until label symbol      */
  184.                 fprintf(ofp,"%c",line[start++]); /* and write it            */
  185.  
  186.               fprintf(ofp,":\n");       /* add on the separator             */
  187.               start++;                  /* skip the colon                   */
  188.             }                           /* end found a label                */
  189.  
  190.           /******************************************************************/
  191.           /* If there is anything left on the line,                         */
  192.           /* tab over, and copy it out.                                     */
  193.           /******************************************************************/
  194.  
  195.           i=0;                          /* reset byte counter               */
  196.           while(isspace(line[start]))   /* skip spaces                      */
  197.             start++;                    /* move toward data, if any         */
  198.  
  199.           if(line[start])               /* if there was some data,          */
  200.             {
  201.               dotab(ofp);               /* first, tab in                    */
  202.  
  203.               rereg(&line[start]);      /* revise register notation         */
  204.               while( c = line[start++] )/* for the rest of the line,        */
  205.                 {
  206.                   if( ( c == ' ') || ( c == '\t') ) /* if white space,      */
  207.                     dotab(ofp);         /* then tab over                    */
  208.                   else                  /* if text,                         */
  209.                     {
  210.                       if( c != '\n' )   /* if not a newline,                */
  211.                         {
  212.                           fprintf(ofp,"%c",c); /* copy the text byte        */
  213.                           i++;          /* and count the putput             */
  214.                         }               /* end not newline                  */
  215.                     }                   /* end text byte                    */
  216.                 }                       /* end rest of line                 */
  217.               if(i)                     /* if there was any data,           */
  218.                 fprintf(ofp,"\n");      /* then new line                    */
  219.             }                           /* end data left in line            */
  220.         }                               /* end of line with data            */
  221.     }                                   /* end of all lines                 */
  222.   return(0);                            /* worked ok                        */
  223. }                                       /* end spiff one line               */
  224.  
  225. /****************************************************************************/
  226. /* Tab over to next stop (modulo 8)                                         */
  227. /****************************************************************************/
  228.  
  229. dotab(ofp)
  230. FILE  *ofp;
  231. {
  232.   fprintf(ofp,"%c",' ');                /* insure one space                 */
  233.   i++;                                  /* count it                         */
  234.   while(i&7)                            /* until we hit the tab,            */
  235.     {
  236.       fprintf(ofp,"%c",' ');            /* space again                      */
  237.       i++;                              /* and count it                     */
  238.     }                                   /* end space until mod 8            */
  239. }                                       /* end dotab                        */
  240.  
  241. /****************************************************************************/
  242. /* Redefine register labels from R0-R15 to D0-D7 and A0-A7                  */
  243. /****************************************************************************/
  244. rereg(data)
  245. char  *data;
  246. {
  247.   register  int   c;                    /* current character                */
  248.   register  int   r;                    /* current character                */
  249.   register  int   tens;                 /* high register number             */
  250.   register  int   ones;                 /* low register number              */
  251.  
  252.   register  char  *from = data;         /* picking up here                  */
  253.   register  char  *to   = data;         /* putting it back here             */
  254.  
  255.   while(*from)                          /* until end of line                */
  256.     {
  257.       if( (c = *from++) != 'R')         /* if not a register,               */
  258.         *to++ = c;                      /* just copy it                     */
  259.       else                              /* it may be a register             */
  260.         {
  261.           tens = *from;                 /* get next byte                    */
  262.           if(! isdigit(tens) )          /* if not a digit,                  */
  263.             *to++ = c;                  /* treat the R as data              */
  264.           else                          /* we have a register               */
  265.             {
  266.               ones = *++from;           /* get next character               */
  267.               if(isdigit(ones) )        /* if second one is digit,          */
  268.                 {
  269.                   from++;               /* skip past it                     */
  270.                   r = (tens - '0') * 10 + (ones - '0'); /* convert number   */
  271.                 }                       /* end two digit parse              */
  272.               else
  273.                 r = (tens - '0');       /* single digit register            */
  274.  
  275.               if(r > 7)                 /* if an address register,          */
  276.                 {
  277.                   r -= 8;               /* adjust address register number   */
  278.                   *to++ = 'A';          /* write the A                      */
  279.                 }                       /* end got address register         */
  280.               else                      /* must be a data register          */
  281.                 *to++ = 'D';            /* so write D                       */
  282.  
  283.               *to++ = r + '0';          /* write register number            */
  284.             }                           /* end of digit parse               */
  285.         }                               /* end of a register assignemnt     */
  286.     }                                   /* end of line                      */
  287.   *to = '\0';                           /* be sure terminated               */
  288. }                                       /* end rereg                        */
  289.