home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / pascal / px / int.c next >
Encoding:
C/C++ Source or Header  |  1991-04-16  |  5.7 KB  |  224 lines

  1. /*-
  2.  * Copyright (c) 1980 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)int.c    5.3 (Berkeley) 4/16/91";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * px - interpreter for Berkeley Pascal
  46.  * Version 3.0 Winter 1979
  47.  *
  48.  * Original version for the PDP 11/70 authored by:
  49.  * Bill Joy, Charles Haley, Ken Thompson
  50.  *
  51.  * Rewritten for VAX 11/780 by Kirk McKusick
  52.  */
  53.  
  54. #include    <signal.h>
  55. #include    "whoami.h"
  56. #include    "vars.h"
  57. #include    "libpc.h"
  58. #include    "objfmt.h"
  59.  
  60. /*
  61.  * New stuff for pdx
  62.  */
  63.  
  64. extern char *end;
  65. extern loopaddr();
  66. extern union progcntr pdx_pc;    /* address of interpreter program cntr */
  67. static void inittrap();
  68.  
  69. main(ac,av)
  70.  
  71.     int    ac;
  72.     char    **av;
  73.  
  74. {
  75.     register char *objprog, *file;
  76.     char *name;
  77.     register long bytesread, bytestoread, block;
  78.     register FILE *prog;
  79.     struct     pxhdr pxhd;
  80. #    define     pipe 3
  81.  
  82.     /*
  83.      * Initialize everything
  84.      */
  85.     _argc = ac;
  86.     _argv = av;
  87.     _nodump = FALSE;
  88.  
  89.     /*
  90.      * Determine how PX was invoked, and how to process the program 
  91.      */
  92.     file = _argv[1];
  93.     if (!strcmp(_argv[0], "pdx")) {
  94.         _mode = PDX;
  95.         _argv += 2; _argc -= 2;
  96.         name = _argv[0];
  97.     } else if (!strcmp(_argv[0], "pix")) {
  98.         _mode = PIX;
  99.         _argv++; _argc--;
  100.         name = _argv[0];
  101.     } else if (!strcmp(_argv[0], "pipe")) {
  102.         _mode = PIPE;
  103.         file = "PIPE";
  104.         _argv++; _argc--;
  105.         name = _argv[0];
  106.     } else {
  107.         _mode = PX;
  108.         if (_argc <= 1)
  109.             file = "obj";
  110.         name = file;
  111.     }
  112.  
  113.     /*
  114.      * kludge to check for old style objs.
  115.      */
  116.     if (_mode == PX && !strcmp(file, "-")) {
  117.         fprintf(stderr, "%s is obsolete and must be recompiled\n",
  118.             _argv[0]);
  119.         exit(1);
  120.     }
  121.     /*
  122.      * Process program header information
  123.      */
  124.     if (_mode == PIPE) {
  125.         read(pipe,&pxhd,sizeof(struct pxhdr));
  126.     } else {
  127.         prog = fopen(file,"r");
  128.         if (prog == NULL) {
  129.             perror(file);
  130.             exit(1);
  131.         }
  132.         fread(&pxhd,sizeof(struct pxhdr),1,prog);
  133.         if (pxhd.magicnum != MAGICNUM) {
  134.             fseek(prog,(long)(HEADER_BYTES-sizeof(struct pxhdr)),0);
  135.             fread(&pxhd,sizeof(struct pxhdr),1,prog);
  136.         }
  137.     }
  138.     if (pxhd.magicnum != MAGICNUM) {
  139.         fprintf(stderr,"%s is not a Pascal interpreter file\n",name);
  140.         exit(1);
  141.     }
  142.     if (pxhd.maketime < createtime) {
  143.         fprintf(stderr,"%s is obsolete and must be recompiled\n",name);
  144.         exit(1);
  145.     }
  146.  
  147.     /*
  148.      * Load program into memory
  149.      */
  150.     objprog = malloc((int)pxhd.objsize);
  151.     if (_mode == PIPE) {
  152.         bytestoread = pxhd.objsize;
  153.         bytesread = 0;
  154.         do    {
  155.             block = read(pipe,(int)(objprog+bytesread),bytestoread);
  156.             if (block > 0) {
  157.                 bytesread += block;
  158.                 bytestoread -= block;
  159.             }
  160.         } while (block > 0);
  161.     } else {
  162.         bytesread = fread(objprog,1,(int)pxhd.objsize,prog);
  163.         fclose(prog);
  164.     }
  165.     if (bytesread != pxhd.objsize) {
  166.         fprintf(stderr,"Read error occurred while loading %s\n",file);
  167.         exit(1);
  168.     }
  169.     if (_mode == PIX)
  170.         fputs("Execution begins...\n",stderr);
  171.     /*
  172.      * set interpreter to catch expected signals and begin interpretation
  173.      */
  174.     signal(SIGILL,syserr);
  175.     signal(SIGBUS,syserr);
  176.     signal(SIGSYS,syserr);
  177.     if (signal(SIGINT,SIG_IGN) != SIG_IGN)
  178.         signal(SIGINT,intr);
  179.     signal(SIGSEGV,memsize);
  180.     signal(SIGFPE,EXCEPT);
  181.     signal(SIGTRAP,liberr);
  182.  
  183.     /*
  184.      * See if we're being watched by the debugger, if so set a trap.
  185.      */
  186.     if (_mode == PDX || (_mode == PIX && pxhd.symtabsize > 0)) {
  187.         inittrap(&_display, &_dp, objprog, &pdx_pc, loopaddr);
  188.     }
  189.  
  190.     /*
  191.      * do it
  192.      */
  193.     interpreter(objprog);
  194.     /*
  195.      * reset signals, deallocate memory, and exit normally
  196.      */
  197.     signal(SIGINT,SIG_IGN);
  198.     signal(SIGSEGV,SIG_DFL);
  199.     signal(SIGFPE,SIG_DFL);
  200.     signal(SIGTRAP,SIG_DFL);
  201.     signal(SIGILL,SIG_DFL);
  202.     signal(SIGBUS,SIG_DFL);
  203.     signal(SIGSYS,SIG_DFL);
  204.     PFLUSH();
  205.     psexit(0);
  206. }
  207.  
  208. /*
  209.  * Generate an IOT trap to tell the debugger that the object code
  210.  * has been read in.  Parameters are there for debugger to look at,
  211.  * not the procedure.
  212.  */
  213.  
  214. static void
  215. inittrap(dispaddr, dpaddr, endaddr, pcaddr, loopaddrp)
  216. union disply *dispaddr;
  217. struct disp *dpaddr;
  218. char *endaddr;
  219. union progcntr *pcaddr;
  220. char **loopaddrp;
  221. {
  222.     kill(getpid(), SIGIOT);
  223. }
  224.