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

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Module:     cmerge.c - Merge Alcyon C source and .s files                */
  4. /*                                                                          */
  5. /* Programmer: George R. Woodside                                           */
  6. /*                                                                          */
  7. /* Date:       January 18, 1987                                             */
  8. /*                                                                          */
  9. /*             argv[1] must be the name of the .c file, and                 */
  10. /*             argv[2] must be the name of the .s file.                     */
  11. /*             Output is to stdout.                                         */
  12. /*                                                                          */
  13. /****************************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <osbind.h>
  18.  
  19. int   clno = 0;                         /* current line in .c file          */
  20. int   slno = 0;                         /* current line in .s file          */
  21. int   seek = 0;                         /* .c file line to seek             */
  22.  
  23. FILE  *fp_c;                            /* .c file pointer                  */
  24. FILE  *fp_s;                            /* .s file pointer                  */
  25.  
  26. char  cline[256];                       /* text line from .c file           */
  27. char  sline[256];                       /* text line from .s file           */
  28. char  junk[128];                        /* throw away buffer for sscanf     */
  29.  
  30. char  *flag = "*line ";                 /* line number reference            */
  31. int   flen;                             /* length to compare                */
  32.  
  33. main(argc,argv)
  34. int  argc;
  35. char *argv[];
  36. {
  37.   register  int  s_stat = 1;            /* .s file line read status         */
  38.   register  int  c_stat = 1;            /* .c file line read status         */
  39.  
  40.   flen  = strlen(flag);                 /* length to compare                */
  41.  
  42.   if( (fp_c = fopen(argv[1],"r") ) == NULL) /* if unable to open .c file,   */
  43.     {
  44.       printf("cmerge: Unable to open %s\n",argv[1]);
  45.       exit(1);                          /* punt                             */
  46.     }
  47.  
  48.   if( (fp_s = fopen(argv[2],"r") ) == NULL) /* if unable to open .s file,   */
  49.     {
  50.       printf("cmerge: Unable to open %s\n",argv[2]);
  51.       exit(1);                          /* punt                             */
  52.     }
  53.  
  54.   /**************************************************************************/
  55.   /*                                                                        */
  56.   /* Determine the first line number reference in the .s file               */
  57.   /*                                                                        */
  58.   /**************************************************************************/
  59.  
  60.   while(s_stat == 1)                    /* while non-line number line,      */
  61.     s_stat = getsline();                /* read another                     */
  62.  
  63.   if(s_stat == 0)                       /* if end of file,                  */
  64.     {
  65.       printf("cmerge: No line numbers in .s file\n"); /* flag error         */
  66.       exit(1);                          /* and punt                         */
  67.     }
  68.  
  69.   sscanf(sline,"%s %d",&junk,&seek);    /* get line number to seek          */
  70.  
  71.   fclose(fp_s);                         /* close the .s file                */
  72.  
  73.   if( (fp_s = fopen(argv[2],"r") ) == NULL) /* re-open .s file              */
  74.     {
  75.       printf("cmerge: Unable to re-open %s\n",argv[2]);
  76.       exit(1);                          /* punt                             */
  77.     }
  78.  
  79.   while(s_stat || c_stat)               /* while either file left open      */
  80.     {
  81.       if( (clno < seek) || (s_stat == 0) ) /* if we need the .c file        */
  82.         {
  83.           c_stat = getcline();          /* read a line                      */
  84.  
  85.           if(c_stat)                    /* if not end of file,              */
  86.             printf("%s",cline);         /* print it                         */
  87.         }                               /* end need .c line                 */
  88.       else
  89.         {
  90.           s_stat = getsline();          /* read a line                      */
  91.           if(s_stat == 1)               /* if a text line,                  */
  92.             printf("......%s",sline);   /* print it                         */
  93.           if(s_stat == 2)               /* if a line number line,           */
  94.             sscanf(sline,"%s %d",&junk,&seek); /* get line to seek          */
  95.         }                               /* end need .s line                 */
  96.     }                                   /* end either file open             */
  97. }                                       /* end main                         */
  98.  
  99. /****************************************************************************/
  100. /*                                                                          */
  101. /* Read a line from the .s file.                                            */
  102. /*   Return 0 if end of file.                                               */
  103. /*   Return 1 if data line.                                                 */
  104. /*   Return 2 if a line number line.                                        */
  105. /*                                                                          */
  106. /****************************************************************************/
  107.  
  108. getsline()
  109. {
  110.   register char *ptr;
  111.  
  112.   ptr = fgets(sline,255,fp_s);          /* read a line                      */
  113.  
  114.   if( ptr == NULL )                     /* if end of file,                  */
  115.     return(0);                          /* say so                           */
  116.  
  117.   if(strncmp(flag,sline,flen) == 0)     /* if a line number flag            */
  118.     return(2);                          /* indicate a number found          */
  119.   else                                  /* otherwise,                       */
  120.     return(1);                          /* regular data line                */
  121. }
  122.  
  123. /****************************************************************************/
  124. /*                                                                          */
  125. /* Read a line from the .c file.                                            */
  126. /* Update clno.                                                             */
  127. /* Return 0 if end of file.                                                 */
  128. /*                                                                          */
  129. /****************************************************************************/
  130.  
  131. getcline()
  132. {
  133.   register char  *ptr;
  134.  
  135.   ptr = fgets(cline,255,fp_c);          /* read a line                      */
  136.  
  137.   clno++;                               /* count it                         */
  138.  
  139.   if( ptr == NULL )                     /* if end of file,                  */
  140.     return(0);                          /* say so                           */
  141.   else                                  /* otherwise,                       */
  142.     return(1);                          /* regular data line                */
  143. }
  144.  
  145. /****************************************************************************/
  146. /* Kludge to cure alcyon library flaw                                       */
  147. /****************************************************************************/
  148. never()
  149. {
  150.   register char  *crud;
  151.   register int   i;
  152.  
  153.   i = atoi(crud);                       /* someday they may fix this        */
  154. }
  155.