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