home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / miwm.zoo / miwm.c < prev    next >
C/C++ Source or Header  |  1991-12-07  |  3KB  |  169 lines

  1. #include <minimal.h>
  2. #include "screen.h"
  3.  
  4. /* defines for getting/setting terminal process groups */
  5. #define TIOCGPGRP    (('T'<< 8) | 6)
  6. #define TIOCSPGRP    (('T'<< 8) | 7)
  7.  
  8. SCREEN thescrn;
  9.  
  10. #define KEYFD -1        /* file descriptor for the keyboard */
  11. long pgrp;    /* process group for our children */
  12.  
  13. char *oldbase;
  14.  
  15. #define INBUFSIZ 1024
  16.  
  17. int
  18. main(argc, argv)
  19.     int argc;
  20.     char **argv;
  21. {
  22.     int i, work;
  23.     long c;
  24.     long s;
  25.     char *shell;
  26.     SCREEN *v, thescrn;
  27.     unsigned char cbuf[INBUFSIZ], *cb;
  28.     long readfds, reads;
  29.     extern char *getenv();
  30.  
  31.     oldbase = (char *)Physbase();
  32.     pgrp = Pgetpid();
  33.     if (argc > 1)
  34.         shell = argv[1];
  35.     else
  36.         shell = getenv("SHELL");
  37.  
  38.     if (!shell) {
  39.         Cconws("Usage: miwm shell\r\n");
  40.         Pterm(2);
  41.     }
  42.  
  43.     Cconws("\033f");
  44.  
  45.     screen[0] = &thescrn;
  46.     screen[0]->base = oldbase;
  47.     init(screen[0]);
  48.     run(screen[0], shell);
  49.     readfds = (1L << screen[0]->fd) | 1;
  50.  
  51.     screen[1] = (SCREEN *)Malloc((long)sizeof(*screen[1]));
  52.     screen[1]->base = oldbase+16000;
  53.     init(screen[1]);
  54.     run(screen[1], shell);
  55.     readfds |= (1L << screen[1]->fd);
  56.  
  57. /* OK, everything's set up; now let's just loop, reading from the keyboard
  58.    and writing to the current screen, and also checking the screens for
  59.    input
  60.  */
  61.     current = screen[0];
  62.     for(;;) {
  63.         work = 0;
  64.     /* check for screen output */
  65.         reads = readfds;
  66.         Fselect(0, &reads, 0L, 0L);
  67.  
  68.         for (i = 0; i < NUMSCREENS; i++) {
  69.             v = screen[i];
  70.             if (!(reads & (1L << v->fd)))
  71.                 continue;
  72.             s = Finstat(v->fd);
  73.             if (s > 0) {
  74.                 if (s > INBUFSIZ) s = INBUFSIZ;
  75.                 flash_off(v);
  76.                 cb = cbuf;
  77.                 s = Fread(v->fd, s, cb);
  78.                 while (s-- > 0) {
  79.                     (*(v->state))(v, *cb++);
  80.                 }
  81.                 work++;
  82.             }
  83.             else if (s < 0) {
  84.                 Cconws("Child exited??\r\n");
  85.                 quit(s);
  86.             }
  87.         }
  88.  
  89. /* check for keyboard input */
  90.         flash_on(current);
  91.         if ((reads & 1) && Finstat(KEYFD)) {
  92.             flash_off(current);
  93.             c = Fgetchar(KEYFD, 0);
  94.             if (c >= ALT_1 && c <= ALT_0) {
  95.                 if (c == ALT_1)
  96.                     current = screen[0];
  97.                 else if (c == ALT_2)
  98.                     current = screen[1];
  99.                 else if (c == ALT_0)
  100.                     quit(0);
  101.             }
  102.             else
  103.                 Fputchar(current->fd, c, 0);
  104.             flash_on(current);
  105.         }
  106.         if (!work)
  107.             Syield();
  108.     }
  109. }
  110.  
  111. quit(x)
  112.     int x;
  113. {
  114.     Pkill((int)-pgrp, SIGHUP);
  115.     Pterm(x);
  116. }
  117.  
  118. run(v, shell)
  119.     SCREEN *v;
  120.     char *shell;
  121. {
  122.     long pid;
  123.     long fd;
  124.     int oldtty;
  125.  
  126.     static char pty[] = "U:\\PIPE\\MWM.1";
  127.  
  128. /* create our handle */
  129.     fd = Fcreate(pty, FA_SYSTEM|FA_HIDDEN);
  130.     if (fd < 0) {
  131.         Cconws("couldn't create ");
  132.         Cconws(pty);
  133.         Cconws("\r\n");
  134.         quit(2);
  135.     }
  136.     v->fd = fd;
  137.  
  138. /* now create the child's handle */
  139.     fd = Fopen(pty, 2);
  140.  
  141.     oldtty = Fdup(-1);
  142.     if (oldtty < 0) {
  143.         Cconws("couldn't dup control terminal!\r\n");
  144.         quit(2);
  145.     }
  146.     Fforce(-1, fd);            /* set up new control terminal */
  147.     Fforce(0, fd);
  148.     Fforce(1, fd);
  149.     Fforce(2, fd);
  150.     pid = Pexec(100, shell, "", 0L); /* spawn child in background */
  151.     Fforce(-1, oldtty);
  152.     Fforce(0, oldtty);
  153.     Fforce(1, oldtty);
  154.     Fforce(2, oldtty);
  155.     if (pid < 0) {
  156.         Cconws("couldn't run ");
  157.         Cconws(shell);
  158.         Cconws("\r\n");
  159.         quit((int)pid);
  160.     }
  161.     v->pid = pid;
  162.     Psetpgrp((int)pid, (int)pgrp);    /* set the child's process group */
  163.     Fcntl(fd, &pgrp, TIOCSPGRP);    /* set the terminal process group */
  164.     Fclose(fd);            /* we won't need this side */
  165.     pty[12]++;            /* next pty name */
  166.     return 0;
  167. }
  168.  
  169.