home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / (gcc-1.37.π) / shorten.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-01  |  5.0 KB  |  227 lines  |  [TEXT/KAHL]

  1. #define kNoChanges        0
  2. #define kChanges        1
  3. #define kBadArgument    2
  4. #define kIOErr            3
  5.  
  6. enum {FALSE, TRUE};
  7.  
  8. #include <types.h>
  9. #include <CType.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <CursorCtl.h>
  14. #include <Files.h>
  15.  
  16.  
  17. // #  70:        bra L#7
  18. // ### Warning 210 ### A short branch could be used here
  19. //     File ":test.c.a"; line 70
  20. // #  80:        bgt L#9
  21. // ### Warning 212 ### Short branch used
  22. //     File ":test.c.a"; line 80
  23.  
  24. char    *progname;
  25. char    *warning1 = "### Warning 210 ### A short branch could be used here";
  26. char    *warning2 = "### Warning 212 ### Short branch used";
  27. char    *semiLine = "; line ";
  28.  
  29. FILE    *errFile;
  30. FILE    *asmFile;
  31. FILE    *outFile;
  32.  
  33.  
  34. void    ShowUsage(void);
  35. int        ConvertToNumber(char *numString);
  36. FILE    *mpw_fopen(char *name, char *mode);
  37.  
  38.  
  39. int    main (int argc, char **argv)
  40. {
  41.     register int i;
  42.     char        *errFileName = 0;
  43.     char        *asmFileName = 0;
  44.     char        *outFileName = 0;
  45.     char        errBuf[250];
  46.     char        asmBuf[250];
  47.     char        *numPtr;
  48.     int            beforeLine = 1;
  49.     int            lineToFind;
  50.     int            numberOfChanges = 0;
  51.     Boolean        progress = FALSE;
  52.     Boolean        doNextLine = FALSE;
  53.     
  54.     progname = argv[0];
  55.     
  56.     /* Prepare to spin the beach ball. */
  57.     InitCursorCtl (nil);
  58.  
  59.     for (i = 1; i < argc; i++) {
  60.         if (argv[i][0] == '-' && argv[i][1] != 0) {
  61.             register char *str = argv[i] + 1;
  62.             if (!strcmp (str, "e"))
  63.                 errFileName = argv[++i];
  64.             else if (!strcmp (str, "a"))
  65.                 asmFileName = argv[++i];
  66.             else if (!strcmp (str, "o"))
  67.                 outFileName = argv[++i];
  68.             else if (!strcmp (str, "p"))
  69.                 progress = TRUE;
  70.             else {
  71.                 fprintf(stderr, "Invalid option `%s'", argv[i]);
  72.                 ShowUsage();
  73.                 exit(kBadArgument);
  74.             }
  75.         }
  76.         else {
  77.             fprintf(stderr, "### Error: Invalid option `%s'", argv[i]);
  78.             ShowUsage();
  79.             exit(kBadArgument);
  80.         }
  81.     }
  82.     
  83.     if (errFileName == 0) {
  84.         errFile = stdin;
  85.         errFileName = "stdin";
  86.     } else
  87.         errFile = fopen(errFileName, "r");
  88.     if (errFile == 0) {
  89.         fprintf(stderr, "### Error: Could not open file: %s.\n", errFileName);
  90.         exit(kIOErr);
  91.     }
  92.  
  93.     if (asmFileName == 0) {
  94.         fprintf(stderr, "### Error: must specify -a file for now.\n");
  95.         exit(kBadArgument);
  96.     } else
  97.         asmFile = fopen(asmFileName, "r");
  98.     if (asmFile == 0) {
  99.         fprintf(stderr, "### Error: Could not open file: %s.\n", asmFileName);
  100.         exit(kIOErr);
  101.     }
  102.     
  103.     if (outFileName == 0) {
  104.         outFile = stdout;
  105.         outFileName = "stdout";
  106.     } else
  107.         outFile = mpw_fopen(outFileName, "w");
  108.     if (outFile == 0) {
  109.         fprintf(stderr, "### Error: Could not open file: %s.\n", outFileName);
  110.         exit(kIOErr);
  111.     }
  112.  
  113.     if (progress) {
  114.         fprintf(stderr, "err file: %s.\n", errFileName);
  115.         fprintf(stderr, "asm file: %s.\n", asmFileName);
  116.         fprintf(stderr, "out file: %s.\n", outFileName);
  117.     }
  118.     
  119.     do {
  120.         SpinCursor(1);
  121.  
  122.         //
  123.         // Get a line from the error message file
  124.         //
  125.         if (fgets(errBuf, 250, errFile)) {
  126.             //
  127.             // We got one. Now see if we are looking for a message
  128.             // or a line number.
  129.             //
  130.             if (!doNextLine) {
  131.                 //
  132.                 // Looking for a message. Remove the carriage
  133.                 // return and compare the message against one of
  134.                 // the ones we are looking for. If we find a match,
  135.                 // set a flag that causes us to get the line number
  136.                 // from the next line.
  137.                 //
  138.                 if (errBuf[strlen(errBuf)-1] == '\n')
  139.                     errBuf[strlen(errBuf)-1] = 0;
  140.                 if (!strcmp(errBuf, warning1)) {
  141.                     doNextLine = TRUE;
  142.                 }
  143.             } else {
  144.                 //
  145.                 // This is a line that follows one of the warning
  146.                 // messages we are looking for. Find the line number
  147.                 // that is printed on it.
  148.                 //
  149.                 numPtr = strstr(errBuf, semiLine);
  150.                 if (numPtr) {
  151.                     //
  152.                     // Found "; Line ". Next convert the number after
  153.                     // it into an integer, scoot to that line in the
  154.                     // asm file, modify it, and write it to the output
  155.                     // file. All other lines between the current one
  156.                     // and the one we are looking for are echoed
  157.                     // without being changed.
  158.                     //
  159.                     lineToFind = ConvertToNumber(numPtr + strlen(semiLine));
  160.                     do {
  161.                         fgets(asmBuf, 250, asmFile);
  162.                         if (beforeLine == lineToFind) {
  163.                             memmove(asmBuf+6, asmBuf+4, strlen(asmBuf)-4+1);
  164.                             asmBuf[4] = '.';
  165.                             asmBuf[5] = 's';
  166.                             numberOfChanges++;
  167.                         }
  168.                         fputs(asmBuf, outFile);
  169.                         beforeLine++;
  170.                     } while (beforeLine <= lineToFind);
  171.                 }
  172.                 doNextLine = FALSE;
  173.             }
  174.         }
  175.     } while (!feof(errFile) && !ferror(errFile));
  176.     
  177.  
  178.     //
  179.     // Write out the rest of the file.
  180.     //
  181.     while (fgets(asmBuf, 250, asmFile)) {
  182.         SpinCursor(1);
  183.         fputs(asmBuf, outFile);
  184.     }
  185.  
  186.     return (numberOfChanges > 0) ? kChanges : kNoChanges;
  187. }
  188.  
  189.  
  190. void    ShowUsage(void)
  191. {
  192. }
  193.  
  194. int    ConvertToNumber(char *numString)
  195. {
  196.     int    numSoFar = 0;
  197.     
  198.     while (isdigit(*numString)) {
  199.         numSoFar = numSoFar * 10 + (*numString - '0');
  200.         numString++;
  201.     }
  202.     
  203.     return numSoFar;
  204. }
  205.  
  206. FILE    *mpw_fopen(char *name, char *mode)
  207. {
  208.     char *pname = (char *) malloc(strlen(name) + 2);
  209.     OSErr e;
  210.     FILE *fp;
  211.     struct FInfo fi;
  212.  
  213.     pname[0] = strlen(name);
  214.     strcpy(pname+1, name);
  215.     
  216.     fp = fopen(name, mode);
  217.     
  218.     e = GetFInfo(pname, 0, &fi);
  219.     /* should do spiffier error handling */
  220.     if (e != 0) printf("%d\n", e);
  221.     fi.fdType = (OSType) 'TEXT';
  222.     fi.fdCreator = (OSType) 'MPS ';
  223.     e = SetFInfo(pname, 0, &fi);
  224.     if (e != 0) printf("%d\n", e);
  225.     return fp;
  226. }
  227.