home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / memacs / ue311c.arc / ATARI.C < prev    next >
C/C++ Source or Header  |  1990-08-16  |  11KB  |  478 lines

  1. /*    ATARI.C:    Operating specific I/O and Spawning functions
  2.             for the ATARI ST operating system (GEMDOS)
  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    ATARI
  11. #include        "edef.h"
  12. #include    "elang.h"
  13. #include    "osbind.h"
  14. #include    "stat.h"    /* DMABUFFER is here */
  15. #include    "errno.h"
  16.  
  17. /****    ST Internals definitions        *****/
  18.  
  19. /*    BIOS calls */
  20.  
  21. #define    BCONSTAT    1    /* return input device status */
  22. #define    CONIN        2    /* read character from device */
  23. #define    BCONOUT        3    /* write character to device */
  24.  
  25. /*    XBIOS calls */
  26.  
  27. #define    INITMOUS    0    /* initialize the mouse */
  28. #define    GETREZ        4    /* get current resolution */
  29. #define    SETSCREEN    5    /* set screen resolution */
  30. #define    SETPALETTE    6    /* set the color pallette */
  31. #define    SETCOLOR    7    /* set or read a color */
  32. #define    CURSCONF    21    /* set cursor configuration */
  33. #define    IKBDWS        25    /* intelligent keyboard send command */
  34. #define    KBDVBASE    34    /* get keyboard table base */
  35.  
  36. /*    GEMDOS calls */
  37.  
  38. #define    EXEC        0x4b    /* Exec off a process */
  39.  
  40. #define    CON        2    /* CON: Keyboard and screen device */
  41.  
  42. /*
  43.  * This function is called once to set up the terminal device streams.
  44.  * On VMS, it translates TT until it finds the terminal, then assigns
  45.  * a channel to it and sets it raw. On CPM it is a no-op.
  46.  */
  47. ttopen()
  48. {
  49.     /* on all screens we are not sure of the initial position
  50.        of the cursor                    */
  51.     ttrow = 999;
  52.     ttcol = 999;
  53. }
  54.  
  55. /*
  56.  * This function gets called just before we go back home to the command
  57.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  58.  * Another no-operation on CPM.
  59.  */
  60. ttclose()
  61. {
  62. }
  63.  
  64. /*
  65.  * Write a character to the display. On VMS, terminal output is buffered, and
  66.  * we just put the characters in the big array, after checking for overflow.
  67.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  68.  * MS-DOS (use the very very raw console output routine).
  69.  */
  70. ttputc(c)
  71.  
  72. char c;
  73.  
  74. {
  75. }
  76.  
  77. /*
  78.  * Flush terminal buffer. Does real work where the terminal output is buffered
  79.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  80.  */
  81. ttflush()
  82. {
  83. }
  84.  
  85. /*
  86.  * Read a character from the terminal, performing no editing and doing no echo
  87.  * at all. More complex in VMS that almost anyplace else, which figures. Very
  88.  * simple on CPM, because the system can do exactly what you want.
  89.  */
  90. ttgetc()
  91. {
  92. }
  93.  
  94. #if    TYPEAH
  95. typahead()
  96.  
  97. {
  98.     int rval;    /* return value from BIOS call */
  99.  
  100.     /* get the status of the console */
  101.     rval = bios(BCONSTAT, CON);
  102.  
  103.     /* end return the results */
  104.     if (rval == 0)
  105.         return(FALSE);
  106.     else
  107.         return(TRUE);
  108. }
  109. #endif
  110.  
  111. /*
  112.  * Create a subjob with a copy of the command intrepreter in it. When the
  113.  * command interpreter exits, mark the screen as garbage so that you do a full
  114.  * repaint. Bound to "^X C". The message at the start in VMS puts out a newline.
  115.  * Under some (unknown) condition, you don't get one free when DCL starts up.
  116.  */
  117. spawncli(f, n)
  118. {
  119.     /* don't allow this command if restricted */
  120.     if (restflag)
  121.         return(resterr());
  122.  
  123. #if     MWC
  124.     mlerase();    /* clear the message line */
  125.         TTflush();
  126.     TTkclose();
  127.     system("msh.prg");
  128.     TTkopen();
  129.         sgarbf = TRUE;
  130.         return(TRUE);
  131. #endif
  132. }
  133.  
  134. /*
  135.  * Run a one-liner in a subjob. When the command returns, wait for a single
  136.  * character to be typed, then mark the screen as garbage so a full repaint is
  137.  * done. Bound to "C-X !".
  138.  */
  139. spawn(f, n)
  140. {
  141.         register int    s;
  142.         char            line[NLINE];
  143.  
  144.     /* don't allow this command if restricted */
  145.     if (restflag)
  146.         return(resterr());
  147.  
  148. #if     MWC
  149.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  150.                 return(s);
  151.     mlerase();
  152.     TTkclose();
  153.         system(line);
  154.     TTkopen();
  155.     /* if we are interactive, pause here */
  156.     if (clexec == FALSE) {
  157.             mlputs(TEXT6);
  158. /*                     "\r\n\n[End]" */
  159.             tgetc();
  160.         }
  161.         sgarbf = TRUE;
  162.         return (TRUE);
  163. #endif
  164. }
  165.  
  166. /*
  167.  * Run an external program with arguments. When it returns, wait for a single
  168.  * character to be typed, then mark the screen as garbage so a full repaint is
  169.  * done. Bound to "C-X $".
  170.  */
  171.  
  172. execprg(f, n)
  173.  
  174. {
  175.         register int    s;
  176.         char            line[NLINE];
  177.  
  178.     /* don't allow this command if restricted */
  179.     if (restflag)
  180.         return(resterr());
  181.  
  182. #if     MWC
  183.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  184.                 return(s);
  185.     mlerase();
  186.     TTkclose();
  187.         system(line);
  188.     TTkopen();
  189.     /* if we are interactive, pause here */
  190.     if (clexec == FALSE) {
  191.             mlputs(TEXT6);
  192. /*                     "\r\n\n[End]" */
  193.             tgetc();
  194.         }
  195.         sgarbf = TRUE;
  196.         return (TRUE);
  197. #endif
  198. }
  199.  
  200. /*
  201.  * Pipe a one line command into a window
  202.  * Bound to ^X @
  203.  */
  204. pipecmd(f, n)
  205. {
  206.         register int    s;    /* return status from CLI */
  207.     register WINDOW *wp;    /* pointer to new window */
  208.     register BUFFER *bp;    /* pointer to buffer to zot */
  209.         char    line[NLINE];    /* command line send to shell */
  210.     static char bname[] = "command";
  211.     FILE *fp;
  212.  
  213.     static char filnam[NSTRING] = "command";
  214.  
  215.     /* don't allow this command if restricted */
  216.     if (restflag)
  217.         return(resterr());
  218.  
  219.     /* get the command to pipe in */
  220.         if ((s=mlreply("@", line, NLINE)) != TRUE)
  221.                 return(s);
  222.  
  223.     /* get rid of the command output buffer if it exists */
  224.         if ((bp=bfind(bname, FALSE, 0)) != FALSE) {
  225.         /* try to make sure we are off screen */
  226.         wp = wheadp;
  227.         while (wp != NULL) {
  228.             if (wp->w_bufp == bp) {
  229.                 onlywind(FALSE, 1);
  230.                 break;
  231.             }
  232.             wp = wp->w_wndp;
  233.         }
  234.         if (zotbuf(bp) != TRUE)
  235.  
  236.             return(FALSE);
  237.     }
  238.  
  239. #if     MWC
  240.     strcat(line," >>");
  241.     strcat(line,filnam);
  242.     movecursor(term.t_nrow - 1, 0);
  243.     TTkclose();
  244.     system(line);
  245.     TTkopen();
  246.         sgarbf = TRUE;
  247.     if ((fp = fopen(filnam, "r")) == NULL) {
  248.         s = FALSE;
  249.     } else {
  250.         fclose(fp);
  251.         s = TRUE;
  252.     }
  253. #endif
  254.  
  255.     if (s != TRUE)
  256.         return(s);
  257.  
  258.     /* split the current window to make room for the command output */
  259.     if (splitwind(FALSE, 1) == FALSE)
  260.             return(FALSE);
  261.  
  262.     /* and read the stuff in */
  263.     if (getfile(filnam, FALSE) == FALSE)
  264.         return(FALSE);
  265.  
  266.     /* make this window in VIEW mode, update all mode lines */
  267.     curwp->w_bufp->b_mode |= MDVIEW;
  268.     wp = wheadp;
  269.     while (wp != NULL) {
  270.         wp->w_flag |= WFMODE;
  271.         wp = wp->w_wndp;
  272.     }
  273.  
  274.     /* and get rid of the temporary file */
  275.     unlink(filnam);
  276.     return(TRUE);
  277. }
  278.  
  279. /*
  280.  * filter a buffer through an external DOS program
  281.  * Bound to ^X #
  282.  */
  283. filter(f, n)
  284.  
  285. {
  286.         register int    s;    /* return status from CLI */
  287.     register BUFFER *bp;    /* pointer to buffer to zot */
  288.         char line[NLINE];    /* command line send to shell */
  289.     char tmpnam[NFILEN];    /* place to store real file name */
  290.     static char bname1[] = "fltinp";
  291.  
  292.     static char filnam1[] = "fltinp";
  293.     static char filnam2[] = "fltout";
  294.  
  295.     /* don't allow this command if restricted */
  296.     if (restflag)
  297.         return(resterr());
  298.  
  299.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  300.         return(rdonly());    /* we are in read only mode    */
  301.  
  302.     /* get the filter name and its args */
  303.         if ((s=mlreply("#", line, NLINE)) != TRUE)
  304.                 return(s);
  305.  
  306.     /* setup the proper file names */
  307.     bp = curbp;
  308.     strcpy(tmpnam, bp->b_fname);    /* save the original name */
  309.     strcpy(bp->b_fname, bname1);    /* set it to our new one */
  310.  
  311.     /* write it out, checking for errors */
  312.     if (writeout(filnam1, "w") != TRUE) {
  313.         mlwrite(TEXT2);
  314. /*                      "[Cannot write filter file]" */
  315.         strcpy(bp->b_fname, tmpnam);
  316.         return(FALSE);
  317.     }
  318.  
  319. #if     MWC
  320.     strcat(line," <fltinp >fltout");
  321.     movecursor(term.t_nrow - 1, 0);
  322.     TTkclose();
  323.     system(line);
  324.     TTkopen();
  325.         sgarbf = TRUE;
  326.     s = TRUE;
  327. #endif
  328.  
  329.     /* on failure, escape gracefully */
  330.     if (s != TRUE || (readin(filnam2,FALSE) == FALSE)) {
  331.         mlwrite(TEXT3);
  332. /*                      "[Execution failed]" */
  333.         strcpy(bp->b_fname, tmpnam);
  334.         unlink(filnam1);
  335.         unlink(filnam2);
  336.         return(s);
  337.     }
  338.  
  339.     /* reset file name */
  340.     strcpy(bp->b_fname, tmpnam);    /* restore name */
  341.     bp->b_flag |= BFCHG;        /* flag it as changed */
  342.  
  343.     /* and get rid of the temporary file */
  344.     unlink(filnam1);
  345.     unlink(filnam2);
  346.     return(TRUE);
  347. }
  348.  
  349. rename(oldname, newname)    /* rename a file */
  350.  
  351. char *oldname;        /* original file name */
  352. char *newname;        /* new file name */
  353.  
  354. {
  355.     Frename(0, oldname, newname);
  356. }
  357.  
  358. /* return a system dependant string with the current time */
  359.  
  360. char *PASCAL NEAR timeset()
  361.  
  362. {
  363.     register char *sp;    /* temp string pointer */
  364.     char buf[16];        /* time data buffer */
  365.  
  366.     time(buf);
  367.     sp = ctime(buf);
  368.     sp[strlen(sp)-1] = 0;
  369.     return(sp);
  370. }
  371.  
  372. /*    FILE Directory routines        */
  373.  
  374. static DMABUFFER info;        /* Info about the file */
  375. char path[NFILEN];        /* path of file to find */
  376. char rbuf[NFILEN];        /* return file buffer */
  377.  
  378. /*    do a wild card directory search (for file name completion) */
  379.  
  380. char *PASCAL NEAR getffile(fspec)
  381.  
  382. char *fspec;    /* file to match */
  383.  
  384. {
  385.     register int index;        /* index into various strings */
  386.     register int point;        /* index into other strings */
  387.     register int extflag;        /* does the file have an extention? */
  388.     char fname[NFILEN];        /* file/path for DOS call */
  389.  
  390.     /* first parse the file path off the file spec */
  391.     strcpy(path, fspec);
  392.     index = strlen(path) - 1;
  393.     while (index >= 0 && (path[index] != '/' &&
  394.                 path[index] != '\\' && path[index] != ':'))
  395.         --index;
  396.     path[index+1] = 0;
  397.  
  398.     /* check for an extension */
  399.     point = strlen(fspec) - 1;
  400.     extflag = FALSE;
  401.     while (point >= 0) {
  402.         if (fspec[point] == '.') {
  403.             extflag = TRUE;
  404.             break;
  405.         }
  406.         point--;
  407.     }
  408.  
  409.     /* construct the composite wild card spec */
  410.     strcpy(fname, path);
  411.     strcat(fname, &fspec[index+1]);
  412.     strcat(fname, "*");
  413.     if (extflag == FALSE)
  414.         strcat(fname, ".*");
  415.  
  416.     /* and call for the first file */
  417.  
  418.     Fsetdta(&info);        /* Initialize buffer for our search */
  419.     if (Fsfirst(fname, 0xF7) != AE_OK)
  420.         return(NULL);
  421.  
  422.     /* return the first file name! */
  423.     strcpy(rbuf, path);
  424.     strcat(rbuf, info.d_fname);
  425.     mklower(rbuf);
  426.     return(rbuf);
  427. }
  428.  
  429. char *PASCAL NEAR getnfile()
  430.  
  431. {
  432.  
  433.     /* and call for the first file */
  434.     if (Fsnext() != AE_OK)
  435.         return(NULL);
  436.  
  437.     /* return the first file name! */
  438.     strcpy(rbuf, path);
  439.     strcat(rbuf, info.d_fname);
  440.     mklower(rbuf);
  441.     return(rbuf);
  442. }
  443.  
  444. #if    LATTICE
  445. system(cmd)    /* call the system to execute a new program */
  446.  
  447. char *cmd;    /* command to execute */
  448.  
  449. {
  450.     char *pptr;            /* pointer into program name */
  451.     char pname[NSTRING];        /* name of program to execute */
  452.     char tail[NSTRING];        /* command tail */
  453.  
  454.     /* scan off program name.... */
  455.     pptr = pname;
  456.     while (*cmd && (*cmd != ' ' && *cmd != '\t'))
  457.         *pptr++ = *cmd++;
  458.     *pptr = 0;
  459.  
  460.     /* create program name length/string */
  461.     tail[0] = strlen(cmd);
  462.     strcpy(&tail[1], cmd);
  463.  
  464.     /* go do it! */
  465.     return(gemdos(        (int)EXEC,
  466.                 (int)0,
  467.                 (char *)pname,
  468.                 (char *)tail,
  469.                 (char *)NULL));
  470. }
  471. #endif
  472.  
  473. #else
  474. atarihello()
  475. {
  476. }
  477. #endif
  478.