home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / memacs / ue311c.arc / NECDOS.C < prev    next >
C/C++ Source or Header  |  1991-03-12  |  28KB  |  1,110 lines

  1. /*    NECDOS.C:    Operating specific I/O and Spawning functions
  2.             under the MSDOS operating system
  3.             on the NEC PC-9801 series computer
  4.             for MicroEMACS 3.10
  5.             (C)Copyright 1990 by Daniel M. Lawrence
  6. */
  7.  
  8. #include        <stdio.h>
  9. #include    "estruct.h"
  10. #include    "eproto.h"
  11.  
  12. #ifdef    MSDOS
  13. #include        "edef.h"
  14. #include    "elang.h"
  15.  
  16. /* The Mouse driver only works with typeahead defined */
  17. #if    MOUSE
  18. #undef    TYPEAH
  19. #define    TYPEAH    1
  20. #endif
  21.  
  22. #if  TURBO
  23. #include <conio.h>
  24. #include <dir.h>
  25. #include <dos.h>
  26. #include <bios.h>
  27.  
  28. struct ffblk fileblock;    /* structure for directory searches */
  29. #endif
  30. #if    MSC
  31. #include <dos.h>
  32.  
  33. struct find_t fileblock;    /* structure for directory searches */
  34. #endif
  35.  
  36. #if     LATTICE | MSC | DTL | TURBO | AZTEC | MWC
  37. union REGS rg;        /* cpu register for use of DOS calls */
  38. struct SREGS segreg;    /* cpu segment registers         */
  39. int nxtchar = -1;    /* character held from type ahead    */
  40. #endif
  41.  
  42. #if    MSC | TURBO
  43. #include    <process.h>
  44. #endif
  45.  
  46. /*    Some global variable    */
  47. #define INBUFSIZ    40
  48. static int mexist;    /* is the mouse driver installed? */
  49. static int nbuttons;    /* number of buttons on the mouse */
  50. static int oldright;    /* old right button status */
  51. static int oldleft;    /* old left button status */
  52.  
  53. PASCAL NEAR execprog(char *cmd);
  54.  
  55. /*    input buffers and pointers    */
  56.  
  57. #define    IBUFSIZE    64    /* this must be a power of 2 */
  58.  
  59. unsigned char in_buf[IBUFSIZE];    /* input character buffer */
  60. int in_next = 0;        /* pos to retrieve next input character */
  61. int in_last = 0;        /* pos to place most recent input character */
  62.  
  63. in_init()    /* initialize the input buffer */
  64.  
  65. {
  66.     in_next = in_last = 0;
  67. }
  68.  
  69. in_check()    /* is the input buffer non-empty? */
  70.  
  71. {
  72.     if (in_next == in_last)
  73.         return(FALSE);
  74.     else
  75.         return(TRUE);
  76. }
  77.  
  78. in_put(event)
  79.  
  80. int event;    /* event to enter into the input buffer */
  81.  
  82. {
  83.     in_buf[in_last++] = event;
  84.     in_last &= (IBUFSIZE - 1);
  85. }
  86.  
  87. int in_get()    /* get an event from the input buffer */
  88.  
  89. {
  90.     register int event;    /* event to return */
  91.  
  92.     event = in_buf[in_next++];
  93.     in_next &= (IBUFSIZE - 1);
  94.     return(event);
  95. }
  96.  
  97. /*
  98.  * This function is called once to set up the terminal device streams.
  99.  */
  100.  
  101. PASCAL NEAR ttopen()
  102.  
  103. {
  104. #if    MOUSE
  105.     long miaddr;    /* mouse interupt routine address */
  106. #endif
  107.  
  108. #if     (HP150 == 0) & LATTICE
  109.     /* kill the ctrl-break interupt */
  110.     rg.h.ah = 0x33;        /* control-break check dos call */
  111.     rg.h.al = 1;        /* set the current state */
  112.     rg.h.dl = 0;        /* set it OFF */
  113.     intdos(&rg, &rg);    /* go for it! */
  114. #endif
  115.     /* on all screens we are not sure of the initial position
  116.        of the cursor                    */
  117.     ttrow = 999;
  118.     ttcol = 999;
  119.  
  120. #if    MOUSE
  121.     /* check if the mouse drive exists first */
  122.     rg.x.ax = 0x3533;    /* look at the interrupt 33 address */
  123.  
  124. #if    MSC | TURBO | DTL | LATTICE | MWC
  125.     int86x(0x21, &rg, &rg, &segreg);
  126.     miaddr = (((long)segreg.es) << 16) + (long)rg.x.bx;
  127.     if (miaddr == 0 || *(char * far)miaddr == 0xcf) {
  128. #endif
  129. #if    AZTEC
  130.     sysint(0x21, &rg, &rg);
  131.     miaddr = (((long)rg.x.es) << 16) + (long)rg.x.bx;
  132.     if (miaddr == 0 || *(char *)miaddr == 0xcf) {
  133. #endif
  134.         mexist = FALSE;
  135.         return;
  136.     }
  137.  
  138.     /* and then check for the mouse itself */
  139.     rg.x.ax = 0;        /* mouse status flag */
  140.     int86(0x33, &rg, &rg);    /* check for the mouse interupt */
  141.     mexist = (rg.x.ax != 0);
  142.  
  143.     /* override this missing value from the NEC */
  144.     nbuttons = 2;
  145.  
  146.     /* initialize our character input queue */
  147.     in_init();
  148.     if (mexist == FALSE)
  149.         return;
  150.  
  151.     /* if the mouse exists.. get it in the upper right corner */
  152.     rg.x.ax = 4;        /* set mouse cursor position */
  153.     rg.x.cx = (term.t_ncol - 1) << 3;    /* last col of display */
  154.     rg.x.dx = 0;        /* top row */
  155.     int86(0x33, &rg, &rg);
  156.  
  157.     /* set the display plane for the mouse */
  158.     rg.x.ax = 18;        /* set screen for displaying cursor */
  159.     rg.x.bx = 0;        /* in GRAY */
  160.     int86(0x33, &rg, &rg);
  161.  
  162.     /* and set its attributes */
  163.     rg.x.ax = 10;        /* set text cursor */
  164.     rg.x.bx = 0;        /* software text cursor please */
  165.     rg.x.cx = 0x77ff;    /* screen mask */
  166.     rg.x.dx = 0x7700;    /* cursor mask */
  167.     int86(0x33, &rg, &rg);
  168. #else    /* !MOUSE */
  169.     mexist = 0;
  170. #endif    /* !MOUSE */
  171. }
  172.  
  173. maxlines(lines)        /* set number of vertical rows for mouse */
  174.  
  175. int lines;    /* # of vertical lines */
  176.  
  177. {
  178. #if    MOUSE
  179.     if (mexist) {
  180. #endif
  181.     }
  182. /* #endif*/
  183. }
  184.  
  185. /*
  186.  * This function gets called just before we go back home to the command
  187.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  188.  * Another no-operation on CPM.
  189.  */
  190. PASCAL NEAR ttclose()
  191. {
  192. #if     (HP150 == 0) & LATTICE
  193.     /* restore the ctrl-break interrupt */
  194.     rg.h.ah = 0x33;        /* control-break check dos call */
  195.     rg.h.al = 1;        /* set the current state */
  196.     rg.h.dl = 1;        /* set it ON */
  197.     intdos(&rg, &rg);    /* go for it! */
  198. #endif
  199. }
  200.  
  201. /*
  202.  * Write a character to the display. On VMS, terminal output is buffered, and
  203.  * we just put the characters in the big array, after checking for overflow.
  204.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  205.  * MS-DOS (use the very very raw console output routine).
  206.  */
  207.  
  208. PASCAL NEAR ttputc(c)
  209.  
  210. int c;
  211.  
  212. {
  213. #if     MWC
  214.         putcnb(c);
  215. #endif
  216.  
  217. #if    (LATTICE | AZTEC | TURBO | MSC) & ~IBMPC
  218.     bdos(6, c, 0);
  219. #endif
  220. }
  221.  
  222. /*
  223.  * Flush terminal buffer. Does real work where the terminal output is buffered
  224.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  225.  */
  226. PASCAL NEAR ttflush()
  227. {
  228. }
  229.  
  230. int doschar()    /* call the dos to get a char */
  231.  
  232. {
  233.  
  234.     register unsigned int c;    /* extended character to return */ 
  235.  
  236.     rg.h.ah = 7;        /* dos Direct Console Input call */
  237.     intdos(&rg, &rg);
  238.     if (rg.h.al == 0x1d) {    /* function key!! */
  239.         rg.h.ah = 7;    /* get the next character */
  240.         intdos(&rg, &rg);
  241.         c = extcode(rg.h.al);
  242.         in_put(c >> 8);        /* prefix byte */
  243.         in_put(c & 255);    /* event code byte */
  244.         return(0);        /* extended escape sequence */
  245.     }
  246.     return(rg.h.al & 255);
  247. }
  248.  
  249. /*
  250.  * Read a character from the terminal, performing no editing and doing no echo
  251.  * at all. Also mouse events are forced into the input stream here.
  252.  */
  253. PASCAL NEAR ttgetc()
  254.  
  255. {
  256.     register int c;        /* character read */
  257.  
  258. ttc:    /* return any keystrokes waiting in the
  259.        type ahead buffer */
  260.     if (in_check())
  261.         return(in_get());
  262.  
  263. #if    TYPEAH
  264.     if (typahead())
  265.         return(doschar());
  266.  
  267.     /* with no mouse, this is a simple get char routine */
  268.     if (mexist == FALSE || mouseflag == FALSE)
  269.         return(doschar());
  270.  
  271. #if    MOUSE
  272.     /* turn the mouse cursor on */
  273.     rg.x.ax = 1;    /* Show Cursor */
  274.     int86(0x33, &rg, &rg);
  275.  
  276.     /* loop waiting for something to happen */
  277.     while (TRUE) {
  278.         if (typahead())
  279.             break;
  280.         if (checkmouse())
  281.             break;
  282.     }
  283.  
  284.     /* turn the mouse cursor back off */
  285.     rg.x.ax = 2;    /* Hide Cursor */
  286.     int86(0x33, &rg, &rg);
  287.  
  288.     goto ttc;
  289. #endif    /* MOUSE */
  290. #else    /* TYPEAH */
  291.     return(doschar());
  292. #endif    /* TYPEAH */
  293. }
  294.  
  295. #if    MOUSE
  296. checkmouse()
  297.  
  298. {
  299.     register int k;        /* current bit/button of mouse */
  300.     register int event;    /* encoded mouse event */
  301.     int mousecol;        /* current mouse column */
  302.     int mouserow;        /* current mouse row */
  303.     int sstate;        /* current shift key status */
  304.     int leftbutton;        /* status of the left mouse button */
  305.     int rightbutton;    /* status of the right mouse button */
  306.  
  307.     /* check to see if any mouse buttons are different */
  308.     rg.x.ax = 3;    /* Get button status and mouse position */
  309.     int86(0x33, &rg, &rg);
  310.     leftbutton = rg.x.ax;
  311.     rightbutton = rg.x.bx;
  312.  
  313.     mousecol = rg.x.cx >> 3;
  314.     mouserow = rg.x.dx >> 4;
  315.  
  316.     if ((rightbutton == oldright) &&
  317.         (leftbutton == oldleft))
  318.              return(FALSE);
  319.  
  320.     /* get the shift key status as well */
  321.     rg.h.ah = 2;    /* return current shift status */
  322.     int86(0x18, &rg, &rg);
  323.     sstate = rg.h.al;
  324.  
  325.     if (rightbutton != oldright) {
  326.         /* the right button changed, generate an event */
  327.         in_put(0);
  328.         in_put(MOUS >> 8);
  329.         in_put(mousecol);
  330.         in_put(mouserow);
  331.  
  332.         event = ((rightbutton != 0) ? 0 : 1);    /* up or down? */
  333.         event += 4;            /* right button */
  334.         if (sstate & 1)            /* shifted */
  335.             event += 'A';
  336.         else if (sstate & 16)        /* controled? */
  337.             event += 1;
  338.         else
  339.             event += 'a';        /* plain */
  340.         in_put(event);
  341.         oldright = rightbutton;
  342.         return(TRUE);
  343.     }
  344.  
  345.     if (leftbutton != oldleft) {
  346.         /* the left button changed, generate an event */
  347.         in_put(0);
  348.         in_put(MOUS >> 8);
  349.         in_put(mousecol);
  350.         in_put(mouserow);
  351.  
  352.         event = ((leftbutton != 0) ? 0 : 1);    /* up or down? */
  353.         if (sstate & 1)            /* shifted */
  354.             event += 'A';
  355.         else if (sstate & 16)        /* controled? */
  356.             event += 1;
  357.         else
  358.             event += 'a';        /* plain */
  359.         in_put(event);
  360.         oldleft = leftbutton;
  361.         return(TRUE);
  362.     }
  363.     return(FALSE);
  364. }
  365. #endif
  366.  
  367. #if    TYPEAH
  368. /* typahead:    Check to see if any characters are already in the
  369.         keyboard buffer
  370. */
  371.  
  372. PASCAL NEAR typahead()
  373.  
  374. {
  375.     int flags;    /* cpu flags from dos call */
  376.  
  377.     rg.x.ax = 0x4406;    /* IOCTL input status */
  378.     rg.x.bx = 0;        /* File handle = stdin */
  379. #if    MSC | DTL
  380.     int86(0x21,&rg,&rg);
  381.     flags = rg.h.al;
  382. #else
  383. #if    LATTICE | AZTEC | TURBO
  384.     flags = intdos(&rg, &rg);
  385. #else
  386.     intcall(&rg, &rg, 0x21);
  387.     flags = rg.x.flags;
  388. #endif
  389. #endif
  390.     if (flags & 1)        /* AL = 0xFF if ready */
  391.         return(TRUE);
  392.     else
  393.         return(FALSE);
  394. }
  395. #endif
  396.  
  397. /*
  398.  * Create a subjob with a copy of the command intrepreter in it. When the
  399.  * command interpreter exits, mark the screen as garbage so that you do a full
  400.  * repaint. Bound to "^X C".
  401.  */
  402.  
  403. PASCAL NEAR spawncli(f, n)
  404.  
  405. int f, n;
  406.  
  407. {
  408.     /* don't allow this command if restricted */
  409.     if (restflag)
  410.         return(resterr());
  411.  
  412.         movecursor(term.t_nrow, 0);             /* Seek to last line.   */
  413.         TTflush();
  414.     TTkclose();
  415.     shellprog("");
  416.     TTkopen();
  417.         sgarbf = TRUE;
  418.         return(TRUE);
  419. }
  420.  
  421. /*
  422.  * Run a one-liner in a subjob. When the command returns, wait for a single
  423.  * character to be typed, then mark the screen as garbage so a full repaint is
  424.  * done. Bound to "C-X !".
  425.  */
  426. PASCAL NEAR spawn(f, n)
  427.  
  428. int f, n;
  429.  
  430. {
  431.         register int s;
  432.         char line[NLINE];
  433.  
  434.     /* don't allow this command if restricted */
  435.     if (restflag)
  436.         return(resterr());
  437.  
  438.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  439.                 return(s);
  440.     movecursor(term.t_nrow - 1, 0);
  441.     TTkclose();
  442.         shellprog(line);
  443.     TTkopen();
  444.     /* if we are interactive, pause here */
  445.     if (clexec == FALSE) {
  446.             mlputs(TEXT6);
  447. /*                     "\r\n\n[End]" */
  448.             tgetc();
  449.         }
  450.         sgarbf = TRUE;
  451.         return (TRUE);
  452. }
  453.  
  454. /*
  455.  * Run an external program with arguments. When it returns, wait for a single
  456.  * character to be typed, then mark the screen as garbage so a full repaint is
  457.  * done. Bound to "C-X $".
  458.  */
  459.  
  460. PASCAL NEAR execprg(f, n)
  461.  
  462. {
  463.         register int s;
  464.         char line[NLINE];
  465.  
  466.     /* don't allow this command if restricted */
  467.     if (restflag)
  468.         return(resterr());
  469.  
  470.         if ((s=mlreply("$", line, NLINE)) != TRUE)
  471.                 return(s);
  472.     movecursor(term.t_nrow - 1, 0);
  473.     TTkclose();
  474.         execprog(line);
  475.     TTkopen();
  476.     /* if we are interactive, pause here */
  477.     if (clexec == FALSE) {
  478.             mlputs(TEXT6);
  479. /*                     "\r\n\n[End]" */
  480.             tgetc();
  481.         }
  482.         sgarbf = TRUE;
  483.         return (TRUE);
  484. }
  485.  
  486. /*
  487.  * Pipe a one line command into a window
  488.  * Bound to ^X @
  489.  */
  490. PASCAL NEAR pipecmd(f, n)
  491.  
  492. int f, n;
  493.  
  494. {
  495.     register WINDOW *wp;    /* pointer to new window */
  496.     register BUFFER *bp;    /* pointer to buffer to zot */
  497.     register char *tmp;    /* ptr to TMP DOS environment variable */
  498.     FILE *fp;
  499.         char line[NLINE];    /* command line send to shell */
  500.     static char bname[] = "command";
  501.     static char filnam[NSTRING] = "command";
  502.     char *getenv();
  503.  
  504.     /* don't allow this command if restricted */
  505.     if (restflag)
  506.         return(resterr());
  507.  
  508.     if ((tmp = getenv("TMP")) == NULL)
  509.         filnam[0] = 0;
  510.     else {
  511.         strcpy(filnam, tmp);
  512.         if (filnam[strlen(filnam) - 1] != '\\')
  513.             strcat(filnam, "\\");
  514.         }
  515.     strcat(filnam,"command");
  516.  
  517.     /* get the command to pipe in */
  518.         if (mlreply("@", line, NLINE) != TRUE)
  519.                 return(FALSE);
  520.  
  521.     /* get rid of the command output buffer if it exists */
  522.         if ((bp=bfind(bname, FALSE, 0)) != FALSE) {
  523.         /* try to make sure we are off screen */
  524.         wp = wheadp;
  525.         while (wp != NULL) {
  526.             if (wp->w_bufp == bp) {
  527.                 onlywind(FALSE, 1);
  528.                 break;
  529.             }
  530.             wp = wp->w_wndp;
  531.         }
  532.         /* get rid of the existing command buffer */
  533.         if (zotbuf(bp) != TRUE)
  534.             return(FALSE);
  535.     }
  536.  
  537.     /* redirect the command output to the output file */
  538.     strcat(line, " >>");
  539.     strcat(line, filnam);
  540.     movecursor(term.t_nrow - 1, 0);
  541.  
  542.     /* execute the command */
  543.     TTkclose();
  544.         shellprog(line);
  545.     TTkopen();
  546.         sgarbf = TRUE;
  547.  
  548.         /* did the output file get generated? */
  549.     if ((fp = fopen(filnam, "r")) == NULL)
  550.         return(FALSE);
  551.     fclose(fp);
  552.  
  553.     /* split the current window to make room for the command output */
  554.     if (splitwind(FALSE, 1) == FALSE)
  555.             return(FALSE);
  556.  
  557.     /* and read the stuff in */
  558.     if (getfile(filnam, FALSE) == FALSE)
  559.         return(FALSE);
  560.  
  561.     /* make this window in VIEW mode, update all mode lines */
  562.     curwp->w_bufp->b_mode |= MDVIEW;
  563.     wp = wheadp;
  564.     while (wp != NULL) {
  565.         wp->w_flag |= WFMODE;
  566.         wp = wp->w_wndp;
  567.     }
  568.  
  569.     /* and get rid of the temporary file */
  570.     unlink(filnam);
  571.     return(TRUE);
  572. }
  573.  
  574. /*
  575.  * filter a buffer through an external DOS program
  576.  * Bound to ^X #
  577.  */
  578. PASCAL NEAR filter(f, n)
  579.  
  580. int f, n;
  581.  
  582. {
  583.         register int    s;    /* return status from CLI */
  584.     register BUFFER *bp;    /* pointer to buffer to zot */
  585.         char line[NLINE];    /* command line send to shell */
  586.     char tmpnam[NFILEN];    /* place to store real file name */
  587.     static char bname1[] = "fltinp";
  588.  
  589.     static char filnam1[] = "fltinp";
  590.     static char filnam2[] = "fltout";
  591.  
  592.     /* don't allow this command if restricted */
  593.     if (restflag)
  594.         return(resterr());
  595.  
  596.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  597.         return(rdonly());    /* we are in read only mode    */
  598.  
  599.     /* get the filter name and its args */
  600.         if ((s=mlreply("#", line, NLINE)) != TRUE)
  601.                 return(s);
  602.  
  603.     /* setup the proper file names */
  604.     bp = curbp;
  605.     strcpy(tmpnam, bp->b_fname);    /* save the original name */
  606.     strcpy(bp->b_fname, bname1);    /* set it to our new one */
  607.  
  608.     /* write it out, checking for errors */
  609.     if (writeout(filnam1, "w") != TRUE) {
  610.         mlwrite(TEXT2);
  611. /*                      "[Cannot write filter file]" */
  612.         strcpy(bp->b_fname, tmpnam);
  613.         return(FALSE);
  614.     }
  615.  
  616.     strcat(line," <fltinp >fltout");
  617.     movecursor(term.t_nrow - 1, 0);
  618.     TTkclose();
  619.         shellprog(line);
  620.     TTkopen();
  621.         sgarbf = TRUE;
  622.     s = TRUE;
  623.  
  624.     /* on failure, escape gracefully */
  625.     if (s != TRUE || (readin(filnam2,FALSE) == FALSE)) {
  626.         mlwrite(TEXT3);
  627. /*                      "[Execution failed]" */
  628.         strcpy(bp->b_fname, tmpnam);
  629.         unlink(filnam1);
  630.         unlink(filnam2);
  631.         return(s);
  632.     }
  633.  
  634.     /* reset file name */
  635.     strcpy(bp->b_fname, tmpnam);    /* restore name */
  636.     bp->b_flag |= BFCHG;        /* flag it as changed */
  637.  
  638.     /* and get rid of the temporary file */
  639.     unlink(filnam1);
  640.     unlink(filnam2);
  641.     return(TRUE);
  642. }
  643.  
  644. #if    LATTICE
  645. extern int _oserr;
  646. #endif
  647.  
  648. #if    AZTEC | MWC
  649. extern int errno;
  650. #endif
  651.  
  652. #if    MSC
  653. extern int _doserrno;
  654. #endif
  655.  
  656. /*    SHELLPROG: Execute a command in a subshell        */
  657.  
  658. PASCAL NEAR shellprog(cmd)
  659.  
  660. char *cmd;    /*  Incoming command line to execute  */
  661.  
  662. {
  663.     char *shell;        /* Name of system command processor */
  664.     char swchar;        /* switch character to use */
  665.     union REGS regs;    /* parameters for dos call */
  666.     char comline[NSTRING];    /* constructed command line */
  667.     char *getenv();
  668.  
  669.     /*  detect current switch character and set us up to use it */
  670.     regs.h.ah = 0x37;    /*  get setting data  */
  671.     regs.h.al = 0x00;    /*  get switch character  */
  672.     intdos(®s, ®s);
  673.     swchar = (char)regs.h.dl;
  674.  
  675.     /*  get name of system shell  */
  676.     if ((shell = getenv("COMSPEC")) == NULL) {
  677.         return(FALSE);        /*  No shell located  */
  678.     }
  679.  
  680.     /* trim leading whitespace off the command */
  681.     while (*cmd == ' ' || *cmd == '\t')    /*  find out if null command */
  682.         cmd++;
  683.  
  684.     /**  If the command line is not empty, bring up the shell  **/
  685.     /**  and execute the command.  Otherwise, bring up the     **/
  686.     /**  shell in interactive mode.   **/
  687.  
  688.     if (*cmd) {
  689.         strcpy(comline, shell);
  690.         strcat(comline, " ");
  691.         comline[strlen(comline) + 1] = 0;
  692.         comline[strlen(comline)] = swchar;
  693.         strcat(comline, "c ");
  694.         strcat(comline, cmd);
  695.         return(execprog(comline));
  696.     } else
  697.         return(execprog(shell));
  698. }
  699.  
  700. /*    EXECPROG:    A function to execute a named program
  701.             with arguments
  702. */
  703.  
  704. #if    LATTICE | AZTEC | MWC
  705. #define    CFLAG    1
  706. #endif
  707.  
  708. PASCAL NEAR execprog(cmd)
  709.  
  710. char *cmd;    /*  Incoming command line to execute  */
  711.  
  712. {
  713.     char *sp;        /* temporary string pointer */
  714.     char f1[38];        /* FCB1 area (not initialized */
  715.     char f2[38];        /* FCB2 area (not initialized */
  716.     char prog[NSTRING];    /* program filespec */
  717.     char tail[NSTRING];    /* command tail with length byte */
  718.     union REGS regs;    /* parameters for dos call  */
  719. #if    MWC == 0
  720.     struct SREGS segreg;    /* segment registers for dis call */
  721. #endif
  722.     struct Pblock {        /* EXEC parameter block */
  723.         short envptr;    /* 2 byte pointer to environment string */
  724.         char *cline;    /* 4 byte pointer to command line */
  725.         char *fcb1;    /* 4 byte pointer to FCB at PSP+5Ch */
  726.         char *fcb2;    /* 4 byte pointer to FCB at PSP+6Ch */
  727.     } pblock;
  728.  
  729.     /* parse the command name from the command line */
  730.     sp = prog;
  731.     while (*cmd && (*cmd != ' ') && (*cmd != '\t'))
  732.         *sp++ = *cmd++;
  733.     *sp = 0;
  734.  
  735.     /* and parse out the command tail */
  736.     while (*cmd && ((*cmd == ' ') || (*cmd == '\t')))
  737.         ++cmd;
  738.     *tail = (char)(strlen(cmd)); /* record the byte length */
  739.     strcpy(&tail[1], cmd);
  740.     strcat(&tail[1], "\r");
  741.  
  742.     /* look up the program on the path trying various extentions */
  743.     if ((sp = flook(prog, TRUE)) == NULL)
  744.         if ((sp = flook(strcat(prog, ".exe"), TRUE)) == NULL) {
  745.             strcpy(&prog[strlen(prog)-4], ".com");
  746.             if ((sp = flook(prog, TRUE)) == NULL)
  747.                 return(FALSE);
  748.         }
  749.     strcpy(prog, sp);
  750.  
  751. #if    MWC == 0
  752.     /* get a pointer to this PSPs environment segment number */
  753.     segread(&segreg);
  754. #endif
  755.  
  756.     /* set up the EXEC parameter block */
  757.     pblock.envptr = 0;    /* make the child inherit the parents env */
  758.     pblock.fcb1 = f1;        /* point to a blank FCB */
  759.     pblock.fcb2 = f2;        /* point to a blank FCB */
  760.         pblock.cline = tail;        /* parameter line pointer */
  761.  
  762.     /* and make the call */
  763.     regs.h.ah = 0x4b;    /* EXEC Load or Execute a Program */
  764.     regs.h.al = 0x00;    /* load end execute function subcode */
  765. #if    AZTEC | MWC
  766.     regs.x.ds = ((unsigned long)(prog) >> 16);    /* program name ptr */
  767.     regs.x.dx = (unsigned int)(prog);
  768.     regs.x.es = regs.x.ds;
  769.     /*regs.x.es = ((unsigned long)(&pblock) >> 16);    * set up param block ptr */
  770.     regs.x.bx = (unsigned int)(&pblock);
  771. #endif
  772. #if    LATTICE | MSC | TURBO | DTL
  773.     segreg.ds = ((unsigned long)(prog) >> 16);    /* program name ptr */
  774.     regs.x.dx = (unsigned int)(prog);
  775.     segreg.es = ((unsigned long)(&pblock) >> 16);    /* set up param block ptr */
  776.     regs.x.bx = (unsigned int)(&pblock);
  777. #endif
  778.  
  779. #if    NOVELL
  780.     rval = execpr(prog, &pblock);
  781. #endif
  782.     
  783. #if    LATTICE && (NOVELL == 0)
  784.     if ((intdosx(®s, ®s, &segreg) & CFLAG) == 0) {
  785.         regs.h.ah = 0x4d;    /* get child process return code */
  786.         intdos(®s, ®s);    /* go do it */
  787.         rval = regs.x.ax;    /* save child's return code */
  788.     } else
  789.         rval = -_oserr;        /* failed child call */
  790. #endif
  791. #if    AZTEC && (NOVELL == 0)
  792.     if ((sysint(0x21, ®s, ®s) & CFLAG) == 0) {
  793.         regs.h.ah = 0x4d;    /* get child process return code */
  794.         sysint(0x21, ®s, ®s);    /* go do it */
  795.         rval = regs.x.ax;    /* save child's return code */
  796.     } else
  797.         rval = -errno;        /* failed child call */
  798. #endif
  799. #if    MWC && (NOVELL == 0)
  800.     intcall(®s, ®s, DOSINT);
  801.     if ((regs.x.flags & CFLAG) == 0) {
  802.         regs.h.ah = 0x4d;    /* get child process return code */
  803.         intcall(®s, ®s, DOSINT);    /* go do it */
  804.         rval = regs.x.ax;    /* save child's return code */
  805.     } else
  806.         rval = -errno;        /* failed child call */
  807. #endif
  808. #if    (TURBO | MSC | DTL) && (NOVELL == 0)
  809.     intdosx(®s, ®s, &segreg);
  810.     if (regs.x.cflag == 0) {
  811.         regs.h.ah = 0x4d;    /* get child process return code */
  812.         intdos(®s, ®s);    /* go do it */
  813.         rval = regs.x.ax;    /* save child's return code */
  814.     } else
  815.         rval = -_doserrno;    /* failed child call */
  816. #endif
  817.     return((rval < 0) ? FALSE : TRUE);
  818. }
  819.  
  820. /* return a system dependant string with the current time */
  821.  
  822. char *PASCAL NEAR timeset()
  823.  
  824. {
  825. #if    MWC | TURBO | MSC
  826.     register char *sp;    /* temp string pointer */
  827.     char buf[16];        /* time data buffer */
  828.     extern char *ctime();
  829.  
  830.     time(buf);
  831.     sp = ctime(buf);
  832.     sp[strlen(sp)-1] = 0;
  833.     return(sp);
  834. #else
  835.     return(errorm);
  836. #endif
  837. }
  838.  
  839. #if    HP150 == 0
  840. /*    extcode:    resolve MSDOS extended character codes
  841.             encoding the proper sequences into emacs
  842.             printable character specifications
  843. */
  844.  
  845. int extcode(c)
  846.  
  847. unsigned c;    /* byte following a zero extended char byte */
  848.  
  849. {
  850.     /* function keys 1 through 9 */
  851.     if (c >= 59 && c < 68)
  852.         return(SPEC | c - 58 + '0');
  853.  
  854.     /* function key 10 */
  855.     if (c == 68)
  856.         return(SPEC | '0');
  857.  
  858.     /* shifted function keys */
  859.     if (c >= 84 && c < 93)
  860.         return(SPEC | SHFT | c - 83 + '0');
  861.     if (c == 93)
  862.         return(SPEC | SHFT | '0');
  863.  
  864.     /* control function keys */
  865.     if (c >= 94 && c < 103)
  866.         return(SPEC | CTRL | c - 93 + '0');
  867.     if (c == 103)
  868.         return(SPEC | CTRL | '0');
  869.  
  870.     /* ALTed function keys */
  871.     if (c >= 104 && c < 113)
  872.         return(SPEC | ALTD | c - 103 + '0');
  873.     if (c == 113)
  874.         return(SPEC | ALTD | '0');
  875.  
  876.     /* ALTed number keys */
  877.     if (c >= 120 && c < 129)
  878.         return(ALTD | c - 119 + '0');
  879.     if (c == 129)
  880.         return(ALTD | '0');
  881.  
  882.     /* some others as well */
  883.     switch (c) {
  884.         case 3:        return(0);        /* null */
  885.         case 15:    return(SHFT | CTRL | 'I');    /* backtab */
  886.  
  887.         case 16:    return(ALTD | 'Q');
  888.         case 17:    return(ALTD | 'W');
  889.         case 18:    return(ALTD | 'E');
  890.         case 19:    return(ALTD | 'R');
  891.         case 20:    return(ALTD | 'T');
  892.         case 21:    return(ALTD | 'Y');
  893.         case 22:    return(ALTD | 'U');
  894.         case 23:    return(ALTD | 'I');
  895.         case 24:    return(ALTD | 'O');
  896.         case 25:    return(ALTD | 'P');
  897.  
  898.         case 30:    return(ALTD | 'A');
  899.         case 31:    return(ALTD | 'S');
  900.         case 32:    return(ALTD | 'D');
  901.         case 33:    return(ALTD | 'F');
  902.         case 34:    return(ALTD | 'G');
  903.         case 35:    return(ALTD | 'H');
  904.         case 36:    return(ALTD | 'J');
  905.         case 37:    return(ALTD | 'K');
  906.         case 38:    return(ALTD | 'L');
  907.  
  908.         case 44:    return(ALTD | 'Z');
  909.         case 45:    return(ALTD | 'X');
  910.         case 46:    return(ALTD | 'C');
  911.         case 47:    return(ALTD | 'V');
  912.         case 48:    return(ALTD | 'B');
  913.         case 49:    return(ALTD | 'N');
  914.         case 50:    return(ALTD | 'M');
  915.  
  916.         case 71:    return(SPEC | '<');    /* HOME */
  917.         case 72:    return(SPEC | 'P');    /* cursor up */
  918.         case 73:    return(SPEC | 'Z');    /* page up */
  919.         case 75:    return(SPEC | 'B');    /* cursor left */
  920.         case 77:    return(SPEC | 'F');    /* cursor right */
  921.         case 79:    return(SPEC | '>');    /* end */
  922.         case 80:    return(SPEC | 'N');    /* cursor down */
  923.         case 81:    return(SPEC | 'V');    /* page down */
  924.         case 82:    return(SPEC | 'C');    /* insert */
  925.         case 83:    return(SPEC | 'D');    /* delete */
  926.         case 115:    return(SPEC | CTRL | 'B');    /* control left */
  927.         case 116:    return(SPEC | CTRL | 'F');    /* control right */
  928.         case 117:    return(SPEC | CTRL | '>');    /* control END */
  929.         case 118:    return(SPEC | CTRL | 'V');    /* control page down */
  930.         case 119:    return(SPEC | CTRL | '<');    /* control HOME */
  931.         case 132:    return(SPEC | CTRL | 'Z');    /* control page up */
  932.     }
  933.  
  934.     return(ALTD | c);
  935. }
  936. #endif
  937.  
  938. #if    TURBO
  939. /*    FILE Directory routines        */
  940.  
  941. char path[NFILEN];    /* path of file to find */
  942. char rbuf[NFILEN];    /* return file buffer */
  943.  
  944. /*    do a wild card directory search (for file name completion) */
  945.  
  946. char *PASCAL NEAR getffile(fspec)
  947.  
  948. char *fspec;    /* pattern to match */
  949.  
  950. {
  951.     register int index;        /* index into various strings */
  952.     register int point;        /* index into other strings */
  953.     register int extflag;        /* does the file have an extention? */
  954.     char fname[NFILEN];        /* file/path for DOS call */
  955.  
  956.     /* first parse the file path off the file spec */
  957.     strcpy(path, fspec);
  958.     index = strlen(path) - 1;
  959.     while (index >= 0 && (path[index] != '/' &&
  960.                 path[index] != '\\' && path[index] != ':'))
  961.         --index;
  962.     path[index+1] = 0;
  963.  
  964.     /* check for an extension */
  965.     point = strlen(fspec) - 1;
  966.     extflag = FALSE;
  967.     while (point > index) {
  968.         if (fspec[point] == '.') {
  969.             extflag = TRUE;
  970.             break;
  971.         }
  972.         point--;
  973.     }
  974.  
  975.     /* construct the composite wild card spec */
  976.     strcpy(fname, path);
  977.     strcat(fname, &fspec[index+1]);
  978.     strcat(fname, "*");
  979.     if (extflag == FALSE)
  980.         strcat(fname, ".*");
  981.  
  982.     /* and call for the first file */
  983.     if (findfirst(fname, &fileblock, FA_DIREC) == -1)
  984.         return(NULL);
  985.  
  986.     /* return the first file name! */
  987.     strcpy(rbuf, path);
  988.     strcat(rbuf, fileblock.ff_name);
  989.     mklower(rbuf);
  990.     if (fileblock.ff_attrib == 16)
  991.         strcat(rbuf, DIRSEPSTR);
  992.     return(rbuf);
  993. }
  994.  
  995. char *PASCAL NEAR getnfile()
  996.  
  997. {
  998.     register int index;        /* index into various strings */
  999.     register int point;        /* index into other strings */
  1000.     register int extflag;        /* does the file have an extention? */
  1001.     char fname[NFILEN];        /* file/path for DOS call */
  1002.  
  1003.     /* and call for the first file */
  1004.     if (findnext(&fileblock) == -1)
  1005.         return(NULL);
  1006.  
  1007.     /* return the first file name! */
  1008.     strcpy(rbuf, path);
  1009.     strcat(rbuf, fileblock.ff_name);
  1010.     mklower(rbuf);
  1011.     if (fileblock.ff_attrib == 16)
  1012.         strcat(rbuf, DIRSEPSTR);
  1013.     return(rbuf);
  1014. }
  1015. #else
  1016. #if    MSC
  1017. /*    FILE Directory routines        */
  1018.  
  1019. char path[NFILEN];    /* path of file to find */
  1020. char rbuf[NFILEN];    /* return file buffer */
  1021.  
  1022. /*    do a wild card directory search (for file name completion) */
  1023.  
  1024. char *PASCAL NEAR getffile(fspec)
  1025.  
  1026. char *fspec;    /* pattern to match */
  1027.  
  1028. {
  1029.     register int index;        /* index into various strings */
  1030.     register int point;        /* index into other strings */
  1031.     register int extflag;        /* does the file have an extention? */
  1032.     char fname[NFILEN];        /* file/path for DOS call */
  1033.  
  1034.     /* first parse the file path off the file spec */
  1035.     strcpy(path, fspec);
  1036.     index = strlen(path) - 1;
  1037.     while (index >= 0 && (path[index] != '/' &&
  1038.                 path[index] != '\\' && path[index] != ':'))
  1039.         --index;
  1040.     path[index+1] = 0;
  1041.  
  1042.     /* check for an extension */
  1043.     point = strlen(fspec) - 1;
  1044.     extflag = FALSE;
  1045.     while (point > index) {
  1046.         if (fspec[point] == '.') {
  1047.             extflag = TRUE;
  1048.             break;
  1049.         }
  1050.         point--;
  1051.     }
  1052.  
  1053.     /* construct the composite wild card spec */
  1054.     strcpy(fname, path);
  1055.     strcat(fname, &fspec[index+1]);
  1056.     strcat(fname, "*");
  1057.     if (extflag == FALSE)
  1058.         strcat(fname, ".*");
  1059.  
  1060.     /* and call for the first file */
  1061.     if (_dos_findfirst(fname, _A_NORMAL|_A_SUBDIR, &fileblock) != 0)
  1062.         return(NULL);
  1063.  
  1064.     /* return the first file name! */
  1065.     strcpy(rbuf, path);
  1066.     strcat(rbuf, fileblock.name);
  1067.     mklower(rbuf);
  1068.     if (fileblock.attrib == 16)
  1069.         strcat(rbuf, DIRSEPSTR);
  1070.     return(rbuf);
  1071. }
  1072.  
  1073. char *PASCAL NEAR getnfile()
  1074.  
  1075. {
  1076.     register int index;        /* index into various strings */
  1077.     register int point;        /* index into other strings */
  1078.     register int extflag;        /* does the file have an extention? */
  1079.     char fname[NFILEN];        /* file/path for DOS call */
  1080.  
  1081.     /* and call for the first file */
  1082.     if (_dos_findnext(&fileblock) != 0)
  1083.         return(NULL);
  1084.  
  1085.     /* return the first file name! */
  1086.     strcpy(rbuf, path);
  1087.     strcat(rbuf, fileblock.name);
  1088.     mklower(rbuf);
  1089.     if (fileblock.attrib == 16)
  1090.         strcat(rbuf, DIRSEPSTR);
  1091.     return(rbuf);
  1092. }
  1093. #else
  1094. char *PASCAL NEAR getffile(fspec)
  1095.  
  1096. char *fspec;    /* file to match */
  1097.  
  1098. {
  1099.     return(NULL);
  1100. }
  1101.  
  1102. char *PASCAL NEAR getnfile()
  1103.  
  1104. {
  1105.     return(NULL);
  1106. }
  1107. #endif
  1108. #endif
  1109. #endif
  1110.