home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-3 / SYMP.C < prev    next >
Text File  |  2000-06-30  |  3KB  |  152 lines

  1. /*
  2.     SYMP.C
  3.     Written by Leor Zolman, 3/82
  4.  
  5.     Given a SYM file put out by ASM86, this aplphabetizes the
  6.     symbols, eliminates all symbols starting with "L0", and writes
  7.     the result into T.SYM.
  8. */
  9.  
  10. #include <bdscio.h>
  11.  
  12. char iobuf[BUFSIZ];
  13. int strcmp();
  14.  
  15. main(argc,argv)
  16. char **argv;
  17. {
  18.     char iname[30];
  19.     int i,j,c;
  20.     unsigned valtab[1000], newval;
  21.     char *namtab[1000], newnam[30];
  22.     int count;
  23.     int ncols, colno;
  24.  
  25.     if (argc == 1) exit(puts("Usage:\nsymp <symnane> [ncols]\n"));
  26.     ncols = 80;
  27.     if (isdigit(argv[2][0])) ncols = atoi(argv[2]);
  28.  
  29.     strcpy(iname,argv[1]);
  30.     strcat(iname,".SYM");
  31.  
  32.     if (fopen(iname,iobuf) == ERROR) exit(puts("Can't open input file"));
  33.  
  34.     count = 0;
  35.  
  36.     while (1) {
  37.         while (isspace(c = getc(iobuf)))   /* skip leading space */
  38.             ;
  39.         if (c == EOF || c == CPMEOF)
  40.             break;
  41.  
  42.         if (!ishexd(c)) {
  43.             printf("Bad 1st char of value: %c\n",c);
  44.             printf("Read %d symbols, last was: %s\n",
  45.                     count, namtab[count-1]);
  46.             exit();
  47.         }
  48.  
  49.         newval = 0;
  50.                 /* accumulate new hex value */
  51.         do {
  52.            newval = newval * 16 + (c < ':' ? (c - '0') : (c - '7'));
  53.            c = getc(iobuf);
  54.         } while (ishexd(c));
  55.  
  56.             /* skip white space between value and symbol */
  57.         while (isspace(c)) c = getc(iobuf);
  58.  
  59.                 /* collect up new symbol name */
  60.         i = 0;
  61.         do {
  62.             newnam[i++] = c;
  63.         } while (!isspace(c = getc(iobuf)));
  64.         newnam[i] = '\0';
  65.  
  66.         valtab[count] = newval;
  67.         namtab[count] = sbrk(strlen(newnam) + 1);
  68.         strcpy(namtab[count++],newnam);
  69.     }
  70.  
  71.     qsort(namtab,count,2,&strcmp,valtab);    /* alphabetize */
  72.  
  73.     fclose(iobuf);
  74.     if (fcreat("T.SYM",iobuf) == ERROR) exit(puts("Can't creat T.SYM"));
  75.  
  76.     colno = 1;
  77.     for (i = 0; i < count; i++) {
  78.         if (namtab[i][0] == 'L' && namtab[i][1] == '0')
  79.             continue;
  80.         fprintf(iobuf, "%04x %-8s  ",valtab[i],namtab[i]);
  81.         colno += 15;
  82.         if (colno + 15 >= ncols) {
  83.             fputs("\n",iobuf);
  84.             colno = 1;
  85.         }
  86.     }
  87.     fputs("\n",iobuf);
  88.     putc(CPMEOF,iobuf);
  89.     fflush(iobuf);
  90.     fclose(iobuf);
  91.     puts("T.SYM is ready for action...\n");
  92. }
  93.  
  94. isspace(c)
  95. char c;
  96. {
  97.     return (c == ' ' || c == '\t' || c == 0x0d || c == 0x0a);
  98. }
  99.  
  100. qsort(base, nel, width, compar,base2)
  101. char *base; int (*compar)();
  102. unsigned width,nel;
  103. {    int i, j;
  104.     unsigned gap, ngap, t1;
  105.     int jd, t2;
  106.  
  107.     t1 = nel * width;
  108.     for (ngap = nel / 2; ngap > 0; ngap /= 2) {
  109.        gap = ngap * width;
  110.        t2 = gap + width;
  111.        jd = base + gap;
  112.        for (i = t2; i <= t1; i += width)
  113.           for (j =  i - t2; j >= 0; j -= gap) {
  114.         if ((*compar)(base+j, jd+j) <=0) break;
  115.              _swp(base+j, jd+j);
  116.              _swp(base2+j,base2+gap+j);
  117.           }
  118.     }
  119. }
  120.  
  121. _swp(a,b)
  122. int *a, *b;
  123. {
  124.     int temp;
  125.     temp = *a;
  126.     *a = *b;
  127.     *b = temp;    
  128. }
  129.  
  130.  
  131.  
  132.  
  133. int strcmp(s,t)
  134. char **s, **t;
  135. {
  136.     char *s1, *t1;
  137.     s1 = *s;
  138.     t1 = *t;
  139.     int i;
  140.     i = 0;
  141.     while (s1[i] == t1[i])
  142.         if (s1[i++] == '\0')
  143.             return 0;
  144.     return s1[i] - t1[i];
  145. }
  146.  
  147. int ishexd(c)
  148. char c;
  149. {
  150.     return isdigit(c) || (c >= 'A' && c <= 'F');
  151. }
  152.