home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / dino / dino_bot.1 / source / shell / format.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-10  |  6.5 KB  |  304 lines

  1. /* Copyright, 1990, Regents of the University of Colorado */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <strings.h>
  5. #include <fcntl.h>
  6. #include "shell.h"
  7.  
  8. BOOL warnings = TRUE;    /* Display warning messages. */
  9. BOOL comments = FALSE;    /* Display comment messages. */
  10. BOOL notes = FALSE;        /* Display note messages. */
  11. BOOL aladin = FALSE;        /* Display Attribute Grammar line numbers. */
  12. /*BOOL full = FALSE;        /* Display complete source file. */
  13. BOOL lnumb = FALSE;        /* Display source file line numbers. */
  14. BOOL order = TRUE;    /*output messages in the order out of compiler*/
  15.  
  16.  
  17. int sourcefile;
  18. char sourcename[FILENAME] = "\0";
  19.  
  20. enum level {note = 0,comment,warning,fatal,deadly};
  21. char *(msg_type[]) = {"NOTE", "COMMENT", "WARNING", "FATAL", "DEADLY"};
  22. enum level max_err = note;
  23.  
  24. typedef struct segment{
  25.         char file[FILENAME];
  26.         int phys;
  27.         int logical;
  28.         } segment;
  29.  
  30. typedef struct msg{
  31.         int line;
  32.         int col;
  33.         int ala;
  34.         enum level type; 
  35.         char mess[MESSLEN];
  36.         char source[MESSLEN];
  37.         struct msg *pt;
  38.         segment *seg;
  39.         } msg;
  40.  
  41. msg *msgs, *cur;
  42.  
  43. segment *seglist;
  44. int nsegs;
  45.  
  46. char line_file_name[FILENAME];
  47.  
  48. initsegs(){
  49.     int line_file;
  50.     char *line;
  51.     char *pt, *q1, *q2;
  52.     int i;
  53.  
  54.     if ((line_file = open(line_file_name, O_RDONLY)) == -1)
  55.         {
  56.         fprintf(stderr, "\nformat: Unable to open file %s\n\n",line_file_name);
  57.         exit(1);
  58.        }
  59.     line = get_line(line_file);
  60.     nsegs = strtol(line, &pt, 10);
  61.     if((seglist = (segment*) malloc (sizeof(segment)*nsegs)) == NULL)
  62.         {fprintf(stderr,"format:malloc segment\n"); exit(1);}
  63.     for (i=0; i<nsegs; i++){
  64.         line = get_line(line_file);
  65.         seglist[i].phys = strtol(line, &pt, 10);
  66.         seglist[i].logical = strtol(pt, &q2, 10);
  67.         q1 = strchr(q2,'"');
  68.         q2 = strchr(q1+1,'"');
  69.         strncpy(seglist[i].file, q1, q2-q1+1);
  70.         seglist[i].file[(q2-q1)+2] = '\0';
  71.         }
  72.     }    
  73.  
  74. #define MAX_ERROR 60
  75. msg error_list[MAX_ERROR];
  76. int next_error = 0;
  77.  
  78. init(argc, argv)
  79.     int argc;
  80.     char *argv[];
  81.     {
  82.     int in;
  83.  
  84.     for (in = 1; in < argc; in++)
  85.         if (argv[in][0] != '-'){ 
  86.             strcpy(sourcename, argv[in]);
  87.             }
  88.         else switch(argv[in][1])
  89.             {
  90.             case 'F' : strcpy(line_file_name, &argv[in][2]); break;
  91.             case 'a' : aladin = TRUE; break;
  92.             case 'w' : warnings = FALSE; break;
  93.             case 'c' : comments = TRUE; break;
  94.             case 'n' : notes = TRUE; break;
  95. /*            case 'f' : full = TRUE; break;*/
  96.             case 'l' : lnumb = TRUE; break;
  97.             case 'e' : order = FALSE;break;
  98.             default : ;
  99.             };
  100. /*    if (full && !order){
  101.         fprintf(stderr,"format: f and e flags not allowed together\n");
  102.         order = TRUE;
  103.         }*/
  104.     msgs = error_list;
  105.     next_error++;
  106.     msgs -> line = 0;
  107.     msgs -> col = 0;
  108.     msgs -> ala = 0;
  109.     msgs -> mess[0] = '\0';
  110.     msgs -> pt = 0;
  111.     msgs -> seg = 0;
  112.     initsegs();
  113.     };
  114.  
  115. add_last()
  116.     {
  117.     msg *p;
  118.     static msg end = {0,0,0,note};
  119.     for (p=msgs; p->pt != NULL; p = p->pt);
  120.     p->pt = &end;
  121.     end.pt = NULL;
  122.     };
  123.  
  124. store(str)
  125.     char *str;
  126.     {
  127.     char *comma = index(str, ',');
  128.     int line, col, aladin;
  129.     enum level type;
  130.     char *pt1 = comma +7, *pt2 = comma + 7;
  131.     msg *new, *search, *prev;
  132.     BOOL found;
  133.     char c;
  134.     int i;
  135.  
  136.     if (next_error>=MAX_ERROR-2) return;
  137.     if (comma == NULL) return;
  138.     if (strncmp(comma,", line ",7) != 0) return;
  139.  
  140.     pt1 = pt2 = comma +7;
  141.     line = strtol(pt1,&pt2,10);
  142.     if (pt1 == pt2) return;
  143.  
  144.     pt1 = ++pt2;
  145.     col = strtol(pt1,&pt2,10);
  146.     if (pt1 == pt2) return;
  147.  
  148.     pt1 = ++ pt2;
  149.     if (0== strncmp(pt1, "NOTE:",5)) {type = note; pt1 +=5;}
  150.     else if (0== strncmp(pt1, "COMMENT:",8)) {type = comment; pt1 +=8;}
  151.     else if (0== strncmp(pt1, "WARNING:",8)) {type = warning; pt1 +=8;}
  152.     else if (0== strncmp(pt1, "FATAL:",6)) {type = fatal; pt1 +=6;}
  153.     else if (0== strncmp(pt1, "DEADLY:",7)) {type = deadly; pt1 +=7;}
  154.     else return;
  155.  
  156.     for (pt2 = pt1, c = *pt2;
  157.         (c != '\0') && (c != '\n') && ((c < '0') || (c > '9'));
  158.         pt2++, c = *pt2);
  159.     if ((c == '\0') || (c == '\n')) aladin = 0;
  160.         else {
  161.             aladin = strtol(pt2, (char**)NULL,10);
  162.             pt2 -=3;}
  163.     *pt2 = '\0';
  164.     if (max_err < type)
  165.         max_err = type;
  166.  
  167.     new = &error_list[next_error++];
  168.     new -> line = line;
  169.     new -> col = col;
  170.     new -> ala = aladin;
  171.     new -> type = type;
  172.     for (i=0; i<nsegs && line<=seglist[i].phys; i++);
  173.     new -> seg = &seglist[i];
  174.     strcpy(new ->mess, pt1); /* pt2-pt1*/
  175.  
  176.     if(order){
  177.         for (prev = msgs, search = msgs->pt, found = FALSE; 
  178.                 !(found || (search == NULL)); 
  179.                 )
  180.             {
  181.             if (line < search->line)
  182.                 found = TRUE;
  183.             else if(line == search->line)
  184.                 if (col < search->col)
  185.                     found = TRUE;
  186.             if (!found){
  187.                 prev = search; 
  188.                 search = search->pt;
  189.                 }
  190.             };
  191.         new ->pt = search;
  192.         prev ->pt = new;
  193.         }
  194.     else {
  195.         new->pt = NULL;
  196.         error_list[next_error-2].pt = new;
  197.         }
  198.     }
  199.  
  200. dump_msgs(){
  201.     int line = 0;
  202.     msg *cur;
  203.     char out[MESSLEN];
  204.     BOOL print;
  205.  
  206.     for(cur=msgs->pt; cur->pt != NULL; cur = cur->pt){
  207.         switch(cur->type){
  208.             case note : print = notes; break;
  209.             case comment : print = comments; break;
  210.             case warning : print = warnings; break;
  211.             case fatal :
  212.             case deadly : print = TRUE;
  213.             }
  214.         if (print){
  215.             int i,carat;
  216.  
  217.             if (!order || line != cur->line){
  218.                 if (lnumb) printf("%d\t",cur->line);
  219.                 printf("%s",cur->source);
  220.                 }
  221.             line = cur->line;
  222.             if (lnumb )
  223.                 carat = cur->col + 7;
  224.                 else carat = cur->col -1;
  225.             for (i=0; i<carat; i++) out[i] = '-';
  226.             out[i] = '^';
  227.             out[i+1] = '\0';
  228.             printf("%s\n%s %s %d %s\n",
  229.                 out,
  230.                 msg_type[cur->type],cur->seg->file,
  231.                 (cur->line) - (cur->seg->phys) + (cur->seg->logical),
  232.                 cur->mess);
  233.             if (aladin && cur->ala >0)
  234.                 printf("\tLine %d in aladin file\n\n",cur->ala);
  235.             }
  236.         }
  237.     }
  238.  
  239. dump(str)
  240.     char *str;
  241.     {
  242.     static int line = 1;
  243.     BOOL print = FALSE;
  244.     msg *grp;
  245.     char out[MESSLEN];
  246.  
  247. /*    print = full;*/
  248.     print = FALSE;
  249.     if (order){
  250.         for(grp = cur; (print == FALSE) && (grp->line == line); grp = grp ->pt)
  251.             switch(grp->type){
  252.                 case note : print = notes; break;
  253.                 case comment : print = comments; break;
  254.                 case warning : print = warnings; break;
  255.                 case fatal :
  256.                 case deadly : print = TRUE;
  257.                 }
  258.         if (print)
  259.             strcpy(cur->source, str);
  260.         for(; (cur->line == line) && (cur->pt != NULL); cur = cur ->pt);
  261.         }
  262.     else
  263.         for( grp = msgs->pt; grp != NULL; grp = grp ->pt)
  264.             if (grp->line == line)
  265.                 strcpy(grp->source, str);
  266.     line++;
  267.     }
  268.  
  269. void main(argc, argv)
  270.     int argc;
  271.     char *argv[];
  272.     {
  273.     int done = FALSE;
  274.     char *line;
  275.  
  276.     init(argc,argv);
  277.     while (!done){
  278.         line = get_line(0);
  279.         if (*line != '\0')
  280.             store(line);
  281.             else done = TRUE;
  282.         }
  283.     add_last();
  284.     if ((sourcefile = open(sourcename, O_RDONLY)) == -1)
  285.         {
  286.         fprintf(stderr, "\nformat: Unable to open file `%s' as source.\n\n",
  287.             sourcename);
  288.         exit(1);
  289.        }
  290.     cur = msgs->pt;
  291.     done = FALSE;
  292.     buf_count = 0;
  293.     eof = 0;
  294.     while (!done){
  295.         line = get_line(sourcefile);
  296.         if (*line != '\0')
  297.             dump(line);
  298.             else done = TRUE;
  299.         }
  300.     dump(" ");
  301.     dump_msgs();
  302.     exit(max_err+2);
  303.     };
  304.