home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Reference Guide / C-C++ Interactive Reference Guide.iso / c_ref / csource3 / 110_01 / symbug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1984-03-04  |  3.6 KB  |  107 lines

  1. /*
  2.     Symbol Table Sort
  3.  
  4.     Written by
  5.     Bob Pasky
  6.     36 Wiswall Rd.
  7.     Newton Centre, MA 02159
  8.  
  9.     Makes BDS-C .SYM file compatible with VBUG symbol table.
  10.  
  11.     VBUG is mnemonic & symbolic debugger for Z-80(tm, Zilog Inc.)
  12.     micros which I wrote. It displays its trace addresses as
  13.     a symbol plus offset, and allows the user to reference a
  14.     location in the same manner. So that the trace runs as fast as
  15.     possible, the symbol table is ordered by the address of the
  16.     symbol, and vbug does a linear search of the addresses until
  17.     it finds one which is greater than the address to be displayed
  18.     and backs off to the previous symbol. It will then print that
  19.     symbol and a hex byte for the difference between the actual
  20.     address and the symbol's address, e.g., "FOOBAR +0A:".
  21.     In vbug, as in my assembler, only the first 7 characters are
  22.     significant -- I must take in to account that fact when creating
  23.     symbol names in my C programs.
  24.  
  25.     The format for VBUG's symbol file is:
  26.         2 bytes: address (low byte-high byte order),
  27.         1 byte: symbol length + 3,
  28.         1-7 bytes: symbol characters;
  29.     Addresses must be sorted in ascending order.
  30.     An address field of 0xFFFF ends table.
  31.  
  32.     The program reads the .sym file produced by "Clink", storing
  33.     the symbols and their addresses in "sym" and "ad" arrays,
  34.     respectively. The addresses and an index into "sym" array
  35.     are sorted using the "qsort" function. The .sym file is
  36.     renamed to .csy, and a new .sym file is opened (vbug uses the
  37.     .sym type also). Finally, the symbols and their addresses are
  38.     written to this file in the format described above.
  39.  
  40.     The program is called by:
  41.         A>symbug [<symbolfile> [<symbolfile> ...] ]
  42.     where <symbolfile> is the name of the .sym file produced by
  43.     "Clink". If no name is given at invocation of the program,
  44.     a prompt will be printed requesting a <symbolfile> name.
  45.     Entering simply <CR> at the prompt will cause the program
  46.     to exit().
  47.  
  48.     This program may not be DIRECTLY useable by anyone else, but
  49.     it demonstrates some simple string manipulation functions.
  50.     One feature which should be noted is that the string data
  51.     are not moved around in memory; only a pointer to the
  52.     string (actually the index number into the array) is moved.
  53.     When the sorting is complete, reading the index numbers
  54.     sequentially from the array "ad" gives the string array index in
  55.     the order desired. (Remember, this program outputs by ascending
  56.     address field; the symbols already happen to be in alphabetical
  57.     order.)
  58.  
  59. */
  60.  
  61. #include "bdscio.h"
  62.  
  63. #define CTRLQ 0x11    /* quit key: control-q */
  64.             /* YOU may want to change this to control-c */
  65.  
  66. #define MAXSYM 200    /* max number of symbols expected in file */
  67. #define MAXSYMLEN 10    /* max symbol length expected from input */
  68. #define SYMLEN 7    /* max symbol length to be output */
  69.  
  70. struct adtab {
  71.     int v;        /* value */
  72.     int o;        /* offset into sym */
  73.     } ad[MAXSYM];    /* max number of symbols expected */
  74.  
  75. char sym[MAXSYM][MAXSYMLEN];    /*array of symbol names */
  76.  
  77. main(argc,argv)
  78. char **argv;
  79. {
  80.     int i, j, fd;
  81.     int total;
  82.     char fnbuf[30], *fname;        /* filename buffer & ptr */
  83.     char onbuf[30], *oname;        /* output filename buf & ptr */
  84.     char ibuf[BUFSIZ];        /* buffered input buffer */
  85.     char *gets();
  86.     char tempstr[80];        /* size of line produced by CLINK */
  87.     int cmpadd();
  88.  
  89.     while (1)
  90.     {
  91.         oname = onbuf;
  92.         if (argc-1)
  93.         {
  94.             fname = *++argv;
  95.             argc--;
  96.         }
  97.         else
  98.         {
  99.             printf ("\nEnter SYM file name: ");
  100.             if (!*(fname = gets(fnbuf))) break;
  101.         }
  102.         strcpy (oname,fname);
  103.         strcat (fname,".SYM");
  104.  
  105.         if ((fd = fopen(fname,ibuf)) == ERROR)
  106.         {
  107.             printf("Can't open %s\n",fname);
  108.             continue;
  109.         }
  110.         else    printf("\nReading %-13s\n",fname