home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / 68kdisassem / part2 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  6.1 KB  |  320 lines

  1. /*
  2.  *    SCCS:    @(#)main.c    1.2    11/2/84    14:19:31
  3.  *    Main routine etc.
  4.  *
  5.  ***********************************************************************
  6.  *    This software is copyright of
  7.  *
  8.  *        John M Collins
  9.  *        47 Cedarwood Drive
  10.  *        St Albans
  11.  *        Herts, AL4 0DN
  12.  *        England            +44 727 57267
  13.  *
  14.  *    and is released into the public domain on the following conditions:
  15.  *
  16.  *        1.  No free maintenance will be guaranteed.
  17.  *        2.  Nothing may be based on this software without
  18.  *            acknowledgement, including incorporation of this
  19.  *            notice.
  20.  *
  21.  *    Notwithstanding the above, the author welcomes correspondence and bug
  22.  *    fixes.
  23.  ***********************************************************************
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <fcntl.h>
  28. #include <a.out.h>
  29. #include <sys/var.h>
  30. #include "unc.h"
  31.  
  32. #define    LINELNG    70
  33.  
  34. void    inturdat(), intutext(), intudat(), intlsym();
  35. void    ptext(), pdata(), pabs(), pbss(), lscan();
  36.  
  37. ef_fids    mainfile;
  38.  
  39. int    par_entry, par_round;    /*  68000 parameters  */
  40. int    nmods;            /*  Number of modules it looks like  */
  41.  
  42. char    *tfnam = "split";
  43.  
  44. char    lsyms;            /*  Generate local symbols  */
  45. char    verbose;        /*  Tell the world what we are doing  */
  46. char    noabs;            /*  No non-global absolutes  */
  47. int    rel;            /*  File being analysed is relocatable  */
  48. int    lpos;
  49.  
  50. symbol    dosymb();
  51. struct    libit    *getfnam();
  52.  
  53. /*
  54.  *    Get hex characters, also allowing for 'k' and 'm'.
  55.  */
  56.  
  57. int    ghex(str)
  58. register  char    *str;
  59. {
  60.     register  int    result = 0;
  61.     register  int    lt;
  62.  
  63.     for  (;;)  {
  64.         lt = *str++;
  65.         switch  (lt)  {
  66.         default:
  67. err:            (void) fprintf(stderr, "Invalid hex digit \'%c\'\n", lt);
  68.             exit(1);
  69.             
  70.         case '\0':
  71.             return    result;
  72.             
  73.         case '0':case '1':case '2':case '3':case '4':
  74.         case '5':case '6':case '7':case '8':case '9':
  75.             result = (result << 4) + lt - '0';
  76.             continue;
  77.             
  78.         case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':
  79.             result = (result << 4) + lt - 'a' + 10;
  80.             continue;
  81.  
  82.         case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':
  83.             result = (result << 4) + lt - 'A' + 10;
  84.             continue;
  85.         
  86.         case 'k':case 'K':
  87.             if  (*str != '\0')
  88.                 goto  err;
  89.             return  result << 10;
  90.             
  91.         case 'm':case 'M':
  92.             if  (*str != '\0')
  93.                 goto  err;
  94.             return  result << 20;
  95.         }
  96.     }
  97. }
  98.  
  99. /*
  100.  *    Process entry line options.  Return number dealt with.
  101.  */
  102.  
  103. int    doopts(av)
  104. char    *av[];
  105. {
  106.     register  int    cnt = 0, lt;
  107.     register  char    *arg;
  108.     struct    var    vs;
  109.     
  110.     uvar(&vs);
  111.     par_entry = vs.v_ustart;
  112.     par_round = vs.v_txtrnd - 1;
  113.     
  114.     for  (;;)  {
  115.         arg = *++av;
  116.         if  (*arg++ != '-')
  117.             return    cnt;
  118.         cnt++;
  119.         
  120. nx:        switch  (lt = *arg++)  {
  121.         default:
  122.             (void) fprintf(stderr, "Bad option -%c\n", lt);
  123.             exit(1);
  124.             
  125.         case  '\0':
  126.             continue;
  127.             
  128.         case  'l':    /*  A file name  */
  129.         case  'L':
  130.             return    cnt - 1;
  131.             
  132.         case  's':
  133.             lsyms++;
  134.             goto  nx;
  135.             
  136.         case  'v':
  137.             verbose++;
  138.             goto  nx;
  139.             
  140.         case  'a':
  141.             noabs++;
  142.             goto  nx;
  143.  
  144.         case  'R':
  145.         case  'N':
  146.             if  (*arg == '\0')  {
  147.                 cnt++;
  148.                 arg = *++av;
  149.                 if  (arg == NULL)  {
  150. bo:                    (void) fprintf(stderr,"Bad -%c option\n",lt);
  151.                     exit(1);
  152.                 }
  153.             }
  154.             if  (lt == 'R')
  155.                 par_entry = ghex(arg);
  156.             else
  157.                 par_round = ghex(arg) - 1;
  158.             continue;
  159.             
  160.         case  't':
  161.             if  (*arg == '\0')  {
  162.                 cnt++;
  163.                 arg = *++av;
  164.                 if  (arg == NULL)
  165.                     goto  bo;
  166.             }
  167.             tfnam = arg;
  168.             continue;
  169.             
  170.         case  'o':
  171.             if  (*arg == '\0')  {
  172.                 cnt++;
  173.                 arg = *++av;
  174.                 if  (arg == NULL)
  175.                     goto  bo;
  176.             }
  177.             if  (freopen(arg, "w", stdout) == NULL)  {
  178.                 (void) fprintf(stderr, "Help! cant open %s\n", arg);
  179.                 exit(20);
  180.             }
  181.             continue;
  182.         }
  183.     }
  184. }
  185.     
  186. /*
  187.  *    Open binary files.  Arrange to erase them when finished.
  188.  */
  189.  
  190. void    bfopen(nam, fid)
  191. char    *nam;
  192. ef_fid    fid;
  193. {
  194.     char    fnam[80];
  195.     
  196.     (void) sprintf(fnam, "%s.tx", nam);
  197.     if  ((fid->ef_t = open(fnam, O_RDWR|O_CREAT, 0666)) < 0)  {
  198. efil:        (void) fprintf(stderr, "Help could not open %s\n", fnam);
  199.         exit(4);
  200.     }
  201.     (void) unlink(fnam);
  202.     (void) sprintf(fnam, "%s.dt", nam);
  203.     if  ((fid->ef_d = open(fnam, O_RDWR|O_CREAT, 0666)) < 0)
  204.         goto  efil;
  205.     (void) unlink(fnam);
  206. }
  207.  
  208. /*
  209.  *    Close binary files.  They should get zapped anyway.
  210.  */
  211.  
  212. void    bfclose(fid)
  213. ef_fid    fid;
  214. {
  215.     (void) close(fid->ef_t);
  216.     (void) close(fid->ef_d);
  217. }
  218.  
  219. /*
  220.  *    Main routine.
  221.  */
  222.  
  223. main(argc, argv)
  224. int    argc;
  225. char    *argv[];
  226. {
  227.     int    i;
  228.     char    *progname = argv[0];
  229.     char    *msg;
  230.     register  struct  libit  *lfd;
  231.     
  232.     i = doopts(argv);
  233.     argc -= i;
  234.     argv += i;
  235.     
  236.     if  (argc < 2)  {
  237.         (void) fprintf(stderr, "Usage: %s [ options ] file\n", progname);
  238.         exit(1);
  239.     }
  240.     
  241.     lfd = getfnam(argv[1]);
  242.     if  (lfd->lf_next > 0)  {
  243.         (void) fprintf(stderr, "Main file (%s) cannot be library\n", argv[1]);
  244.         exit(2);
  245.     }
  246.     
  247.     bfopen(tfnam, &mainfile);
  248.     if  (verbose)
  249.         (void) fprintf(stderr, "Scanning text\n");
  250.     if  (!rtext(lfd->lf_fd, lfd->lf_offset, &mainfile))  {
  251.         msg = "text";
  252. bf:        (void) fprintf(stderr, "Bad format input file - reading %s\n", msg);
  253.         exit(5);
  254.     }
  255.     if  (verbose)
  256.         (void) fprintf(stderr, "Scanning data\n");
  257.     if  (!rdata(lfd->lf_fd, lfd->lf_offset, &mainfile))  {
  258.         msg = "data";
  259.         goto  bf;
  260.     }
  261.     if  (verbose)
  262.         (void) fprintf(stderr, "Scanning symbols\n");
  263.     if  (!rsymb(lfd->lf_fd, lfd->lf_offset, dosymb, &mainfile))  {
  264.         msg = "symbols";
  265.         goto  bf;
  266.     }
  267.     if  (verbose)
  268.         (void) fprintf(stderr, "Scanning for relocation\n");
  269.     if  ((rel = rrel(lfd->lf_fd, lfd->lf_offset, &mainfile)) < 0)  {
  270.         msg = "reloc";
  271.         goto  bf;
  272.     }
  273.     
  274.     if  (rel)  {
  275.         if  (verbose)
  276.             (void) fprintf(stderr, "File is relocatable\n");
  277.         if  (argc > 2)
  278.             (void) fprintf(stderr, "Sorry - no scan on reloc files\n");
  279.     }
  280.     else
  281.         lscan(argc - 2, &argv[2]);
  282.  
  283.     if  (verbose)
  284.         (void) fprintf(stderr, "End of input\n");
  285.     
  286.     (void) close(lfd->lf_fd);
  287.     if  (nmods > 0)
  288.         (void) fprintf(stderr, "Warning: at least %d merged modules\n",
  289.             nmods + 1);
  290.  
  291.     if  (mainfile.ef_stvec != NULL)  {
  292.         free(mainfile.ef_stvec);
  293.         mainfile.ef_stvec = NULL;
  294.         mainfile.ef_stcnt = 0;
  295.     }
  296.     
  297.     if  (verbose)
  298.         (void) fprintf(stderr, "Text anal 1\n");
  299.     intutext();
  300.     if  (verbose)
  301.         (void) fprintf(stderr, "Data anal 1\n");
  302.     intudat(&mainfile);
  303.     if  (!rel)  {
  304.         if  (verbose)
  305.             (void) fprintf(stderr, "Data anal 2\n");
  306.         inturdat(&mainfile);
  307.     }
  308.     if  (lsyms)  {
  309.         if  (verbose)
  310.             (void) fprintf(stderr, "Local symbol scan\n");
  311.         intlsym();
  312.     }
  313.     pabs();
  314.     ptext(&mainfile);
  315.     pdata(&mainfile);
  316.     pbss(&mainfile);
  317.     bfclose(&mainfile);
  318.     exit(0);
  319. }
  320.