home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / UE311C.ZIP / WMCS.C < prev    next >
C/C++ Source or Header  |  1990-08-16  |  10KB  |  374 lines

  1. /*    WMCS.C:    Operating specific I/O and Spawning functions
  2.         for the WICAT computer/operating system
  3.         for MicroEMACS 3.10
  4.         (C)Copyright 1988 by Daniel M. Lawrence
  5. */
  6.  
  7. #include        <stdio.h>
  8. #include    "estruct.h"
  9. #include    "eproto.h"
  10. #if    WMCS
  11. #include        "edef.h"
  12. #include    "elang.h"
  13.  
  14. #include    "/sysincl.sys/devtdisp.h"
  15. #include    "/sysincl.sys/dstatdisp.h"
  16.  
  17. devicetable    dtable;
  18. devicestatus    dstat;
  19. ushort    xmdstat = 0x019d;
  20. ushort    xmdsave;
  21. ushort    xmdmask = 0xfe62;
  22.  
  23. /*
  24.  * This function is called once to set up the terminal device streams.
  25.  * On VMS, it translates TT until it finds the terminal, then assigns
  26.  * a channel to it and sets it raw. On CPM it is a no-op.
  27.  */
  28. ttopen()
  29. {
  30.     /* set device status */
  31.     _giodst(1,&dtable,sizeof(dtable),&dstat);
  32.     xmdsave = dstat.class.tty.dstyflags1;
  33.     dstat.class.tty.dstyflags1 &= xmdmask;    /* clear the bits we need to use */
  34.     dstat.class.tty.dstyflags1 |= xmdstat;    /* set our status bits */
  35.     _siodst(1,&dstat);    /* set device status */
  36.  
  37.     /* on all screens we are not sure of the initial position
  38.        of the cursor                    */
  39.     ttrow = 999;
  40.     ttcol = 999;
  41. }
  42.  
  43. /*
  44.  * This function gets called just before we go back home to the command
  45.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  46.  * Another no-operation on CPM.
  47.  */
  48. ttclose()
  49. {
  50.     /* reset device status back to saved values */
  51.     dstat.class.tty.dstyflags1 = xmdsave;
  52.     _siodst(1,&dstat);
  53. }
  54.  
  55. /*
  56.  * Write a character to the display. On VMS, terminal output is buffered, and
  57.  * we just put the characters in the big array, after checking for overflow.
  58.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  59.  * MS-DOS (use the very very raw console output routine).
  60.  */
  61. ttputc(c)
  62. {
  63.         fputc(c, stdout);
  64. }
  65.  
  66. /*
  67.  * Flush terminal buffer. Does real work where the terminal output is buffered
  68.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  69.  */
  70. ttflush()
  71. {
  72.         fflush(stdout);
  73. }
  74.  
  75. /*
  76.  * Read a character from the terminal, performing no editing and doing no echo
  77.  * at all. More complex in VMS that almost anyplace else, which figures. Very
  78.  * simple on CPM, because the system can do exactly what you want.
  79.  */
  80. ttgetc()
  81. {
  82.     char c;
  83.     long trns;
  84.     _read(1,-1,0,-1,&c,1,&trns);
  85.     return((int)(c & 0x7f));
  86. }
  87.  
  88. #if    TERMCAP
  89. /* get a character with timeout */
  90. mttgetc()
  91. {
  92.     char c;
  93.     long trns,status;
  94.     
  95.     status = _read(1,-1,0,4,&c,1,&trns);
  96.     if(status) return(-1);
  97.     return((int)(c & 0x7f));
  98. }
  99. #endif
  100.  
  101. #if    TYPEAH
  102. /* typahead:    Check to see if any characters are already in the
  103.         keyboard buffer
  104. */
  105.  
  106. typahead()
  107.  
  108. {
  109. /*  This doesnt seem to work even though it should
  110.     So I leave it commented out...
  111.  
  112.     devicetable    xdtable;
  113.     devicestatus    xdstat;
  114.     
  115.     _giodst(1,&xdtable,sizeof(xdtable),&xdstat);
  116.     return(dstat.class.tty.dstyinputcnt);
  117. */
  118.     return(FALSE);
  119. }
  120. #endif
  121.  
  122. /*
  123.  * Create a subjob with a copy of the command intrepreter in it. When the
  124.  * command interpreter exits, mark the screen as garbage so that you do a full
  125.  * repaint. Bound to "^X C". The message at the start in VMS puts out a newline.
  126.  * Under some (unknown) condition, you don't get one free when DCL starts up.
  127.  */
  128. spawncli(f, n)
  129. {
  130.         register char *cp;
  131.         char *getenv();
  132.  
  133.     /* don't allow this command if restricted */
  134.     if (restflag)
  135.         return(resterr());
  136.  
  137.         movecursor(term.t_nrow, 0);             /* Seek to last line.   */
  138.         TTflush();
  139.         TTclose();                              /* stty to old settings */
  140.     if ((cp = getenv("SYS$CIP")) != NULL && *cp != '\0')
  141.                 system(cp);
  142.         else
  143.         system("CIP");
  144.  
  145.         sgarbf = TRUE;
  146.     sleep(2);
  147.         TTopen();
  148.         return(TRUE);
  149. }
  150.  
  151. /*
  152.  * Run a one-liner in a subjob. When the command returns, wait for a single
  153.  * character to be typed, then mark the screen as garbage so a full repaint is
  154.  * done. Bound to "C-X !".
  155.  */
  156. spawn(f, n)
  157. {
  158.         register int    s;
  159.         char            line[NLINE];
  160.  
  161.     /* don't allow this command if restricted */
  162.     if (restflag)
  163.         return(resterr());
  164.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  165.                 return (s);
  166.         TTputc('\n');                /* Already have '\r'    */
  167.         TTflush();
  168.         TTclose();                              /* stty to old modes    */
  169.         system(line);
  170.         TTopen();
  171.         mlputs(TEXT188);                        /* Pause.               */
  172. /*             "[End]" */
  173.         TTflush();
  174.         while ((s = tgetc()) != '\r' && s != ' ')
  175.                 ;
  176.         sgarbf = TRUE;
  177.         return (TRUE);
  178. }
  179.  
  180. /*
  181.  * Run an external program with arguments. When it returns, wait for a single
  182.  * character to be typed, then mark the screen as garbage so a full repaint is
  183.  * done. Bound to "C-X $".
  184.  */
  185.  
  186. execprg(f, n)
  187.  
  188. {
  189.         register int    s;
  190.         char            line[NLINE];
  191.  
  192.     /* don't allow this command if restricted */
  193.     if (restflag)
  194.         return(resterr());
  195.  
  196.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  197.                 return (s);
  198.         TTputc('\n');                /* Already have '\r'    */
  199.         TTflush();
  200.         TTclose();                              /* stty to old modes    */
  201.         system(line);
  202.         TTopen();
  203.         mlputs(TEXT188);                        /* Pause.               */
  204. /*             "[End]" */
  205.         TTflush();
  206.         while ((s = tgetc()) != '\r' && s != ' ')
  207.                 ;
  208.         sgarbf = TRUE;
  209.         return (TRUE);
  210. }
  211.  
  212. /*
  213.  * Pipe a one line command into a window
  214.  * Bound to ^X @
  215.  */
  216. pipecmd(f, n)
  217. {
  218.         register int    s;    /* return status from CLI */
  219.     register WINDOW *wp;    /* pointer to new window */
  220.     register BUFFER *bp;    /* pointer to buffer to zot */
  221.         char    line[NLINE];    /* command line send to shell */
  222.     static char bname[] = "command";
  223.  
  224.     static char filnam[NSTRING] = "command";
  225.  
  226.     /* don't allow this command if restricted */
  227.     if (restflag)
  228.         return(resterr());
  229.  
  230.     /* get the command to pipe in */
  231.         if ((s=mlreply("@", line, NLINE)) != TRUE)
  232.                 return(s);
  233.  
  234.     /* get rid of the command output buffer if it exists */
  235.         if ((bp=bfind(bname, FALSE, 0)) != FALSE) {
  236.         /* try to make sure we are off screen */
  237.         wp = wheadp;
  238.         while (wp != NULL) {
  239.             if (wp->w_bufp == bp) {
  240.                 onlywind(FALSE, 1);
  241.                 break;
  242.             }
  243.             wp = wp->w_wndp;
  244.         }
  245.         if (zotbuf(bp) != TRUE)
  246.  
  247.             return(FALSE);
  248.     }
  249.  
  250.         TTputc('\n');                /* Already have '\r'    */
  251.         TTflush();
  252.         TTclose();                              /* stty to old modes    */
  253.     strcat(line,">");
  254.     strcat(line,filnam);
  255.         system(line);
  256.         TTopen();
  257.         TTflush();
  258.         sgarbf = TRUE;
  259.         s = TRUE;
  260.  
  261.     if (s != TRUE)
  262.         return(s);
  263.  
  264.     /* split the current window to make room for the command output */
  265.     if (splitwind(FALSE, 1) == FALSE)
  266.             return(FALSE);
  267.  
  268.     /* and read the stuff in */
  269.     if (getfile(filnam, FALSE) == FALSE)
  270.         return(FALSE);
  271.  
  272.     /* make this window in VIEW mode, update all mode lines */
  273.     curwp->w_bufp->b_mode |= MDVIEW;
  274.     wp = wheadp;
  275.     while (wp != NULL) {
  276.         wp->w_flag |= WFMODE;
  277.         wp = wp->w_wndp;
  278.     }
  279.  
  280.     /* and get rid of the temporary file */
  281.     unlink(filnam);
  282.     return(TRUE);
  283. }
  284.  
  285. /*
  286.  * filter a buffer through an external DOS program
  287.  * Bound to ^X #
  288.  */
  289. filter(f, n)
  290.  
  291. {
  292.         register int    s;    /* return status from CLI */
  293.     register BUFFER *bp;    /* pointer to buffer to zot */
  294.         char line[NLINE];    /* command line send to shell */
  295.     char tmpnam[NFILEN];    /* place to store real file name */
  296.     static char bname1[] = "fltinp";
  297.  
  298.     static char filnam1[] = "fltinp";
  299.     static char filnam2[] = "fltout";
  300.  
  301.     /* don't allow this command if restricted */
  302.     if (restflag)
  303.         return(resterr());
  304.  
  305.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  306.         return(rdonly());    /* we are in read only mode    */
  307.  
  308.     /* get the filter name and its args */
  309.         if ((s=mlreply("#", line, NLINE)) != TRUE)
  310.                 return(s);
  311.  
  312.     /* setup the proper file names */
  313.     bp = curbp;
  314.     strcpy(tmpnam, bp->b_fname);    /* save the original name */
  315.     strcpy(bp->b_fname, bname1);    /* set it to our new one */
  316.  
  317.     /* write it out, checking for errors */
  318.     if (writeout(filnam1, "w") != TRUE) {
  319.         mlwrite(TEXT2);
  320. /*                      "[Cannot write filter file]" */
  321.         strcpy(bp->b_fname, tmpnam);
  322.         return(FALSE);
  323.     }
  324.  
  325.         TTputc('\n');                /* Already have '\r'    */
  326.         TTflush();
  327.         TTclose();                              /* stty to old modes    */
  328.     strcat(line," <fltinp >fltout");
  329.         system(line);
  330.         TTopen();
  331.         TTflush();
  332.         sgarbf = TRUE;
  333.         s = TRUE;
  334.  
  335.     /* on failure, escape gracefully */
  336.     if (s != TRUE || (readin(filnam2,FALSE) == FALSE)) {
  337.         mlwrite(TEXT3);
  338. /*                      "[Execution failed]" */
  339.         strcpy(bp->b_fname, tmpnam);
  340.         unlink(filnam1);
  341.         unlink(filnam2);
  342.         return(s);
  343.     }
  344.  
  345.     /* reset file name */
  346.     strcpy(bp->b_fname, tmpnam);    /* restore name */
  347.     bp->b_flag |= BFCHG;        /* flag it as changed */
  348.  
  349.     /* and get rid of the temporary file */
  350.     unlink(filnam1);
  351.     unlink(filnam2);
  352.     return(TRUE);
  353. }
  354.  
  355. /* return a system dependant string with the current time */
  356.  
  357. char *PASCAL NEAR timeset()
  358.  
  359. {
  360.     register char *sp;    /* temp string pointer */
  361.     char buf[16];        /* time data buffer */
  362.     extern char *ctime();
  363.  
  364.     time(buf);
  365.     sp = ctime(buf);
  366.     sp[strlen(sp)-1] = 0;
  367.     return(sp);
  368. }
  369. #else
  370. wmcshello()
  371. {
  372. }
  373. #endif
  374.