home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 313_01 / minix.c < prev    next >
C/C++ Source or Header  |  1990-04-22  |  4KB  |  268 lines

  1. /* $Header: /r3/tony/src/stevie/src/RCS/minix.c,v 1.6 90/01/22 19:08:35 tony Exp $
  2.  *
  3.  * System-dependent routines for Minix-ST
  4.  */
  5.  
  6. #include "stevie.h"
  7. #include <sgtty.h>
  8. #include <signal.h>
  9.  
  10. #ifdef    TERMCAP
  11. extern    int    ospeed;
  12. #endif
  13.  
  14. /*
  15.  * inchar() - get a character from the keyboard
  16.  */
  17. int
  18. inchar()
  19. {
  20.     char    c;
  21.  
  22.     flushbuf();        /* flush any pending output */
  23.  
  24.     while (read(0, &c, 1) != 1)
  25.         ;
  26.  
  27.     got_int = FALSE;
  28.     return c;
  29. }
  30.  
  31. #define    BSIZE    2048
  32. static    char    outbuf[BSIZE];
  33. static    int    bpos = 0;
  34.  
  35. void
  36. flushbuf()
  37. {
  38.     if (bpos != 0)
  39.         write(1, outbuf, bpos);
  40.     bpos = 0;
  41. }
  42.  
  43. /*
  44.  * Macro to output a character. Used within this file for speed.
  45.  */
  46. #define    outone(c)    outbuf[bpos++] = c; if (bpos >= BSIZE) flushbuf()
  47.  
  48. /*
  49.  * Function version for use outside this file.
  50.  */
  51. void
  52. outchar(c)
  53. register char    c;
  54. {
  55.     outbuf[bpos++] = c;
  56.     if (bpos >= BSIZE)
  57.         flushbuf();
  58. }
  59.  
  60. void
  61. outstr(s)
  62. register char    *s;
  63. {
  64.     while (*s) {
  65.         outone(*s++);
  66.     }
  67. }
  68.  
  69. #ifdef    TERMCAP
  70. void
  71. outcstr(s)
  72. char    *s;
  73. {
  74.     tputs(s, 1, outchar);
  75. }
  76. #endif
  77.  
  78. void
  79. beep()
  80. {
  81.     outone('\007');
  82. }
  83.  
  84. /*
  85.  * remove(file) - remove a file
  86.  */
  87. void
  88. remove(file)
  89. char *file;
  90. {
  91.     unlink(file);
  92. }
  93.  
  94. /*
  95.  * rename(of, nf) - rename existing file 'of' to 'nf'
  96.  */
  97. void
  98. rename(of, nf)
  99. char    *of, *nf;
  100. {
  101.     unlink(nf);
  102.     link(of, nf);
  103.     unlink(of);
  104. }
  105.  
  106. void
  107. delay()
  108. {
  109.     /* not implemented */
  110. }
  111.  
  112. static    struct    sgttyb    ostate;
  113.  
  114. /*
  115.  * Go into cbreak mode
  116.  */
  117. void
  118. set_tty()
  119. {
  120.     struct    sgttyb    nstate;
  121.  
  122.     ioctl(0, TIOCGETP, &ostate);
  123.     nstate = ostate;
  124.     nstate.sg_flags &= ~(XTABS|ECHO);
  125.     nstate.sg_flags |= CBREAK;
  126.     ioctl(0, TIOCSETP, &nstate);
  127.  
  128. #ifdef    TERMCAP
  129.     ospeed = nstate.sg_ospeed;
  130. #endif
  131. }
  132.  
  133. /*
  134.  * Restore original terminal modes
  135.  */
  136. void
  137. reset_tty()
  138. {
  139.     ioctl(0, TIOCSETP, &ostate);
  140. }
  141.  
  142. void
  143. sig()
  144. {
  145.     signal(SIGINT, sig);
  146.     signal(SIGQUIT, sig);
  147.  
  148.     got_int = TRUE;
  149. }
  150.  
  151. void
  152. windinit()
  153. {
  154. #ifdef    TERMCAP
  155.     if (t_init() != 1) {
  156.         fprintf(stderr, "unknown terminal type\n");
  157.         exit(1);
  158.     }
  159. #else
  160.     Columns = 80;
  161.     P(P_LI) = Rows = 25;
  162. #endif
  163.     /*
  164.      * The code here makes sure that there isn't a window during which
  165.      * we could get interrupted and exit with the tty in a weird state.
  166.      */
  167.     signal(SIGINT, sig);
  168.     signal(SIGQUIT, sig);
  169.  
  170.     set_tty();
  171.  
  172.     if (got_int)
  173.         windexit(0);
  174. }
  175.  
  176. void
  177. windexit(r)
  178. int r;
  179. {
  180.     reset_tty();
  181.     exit(r);
  182. }
  183.  
  184. void
  185. windgoto(r, c)
  186. register int    r, c;
  187. {
  188. #ifdef    TERMCAP
  189.     char    *tgoto();
  190. #else
  191.     r += 1;
  192.     c += 1;
  193. #endif
  194.  
  195.     /*
  196.      * Check for overflow once, to save time.
  197.      */
  198.     if (bpos + 8 >= BSIZE)
  199.         flushbuf();
  200.  
  201. #ifdef    TERMCAP
  202.     outcstr(tgoto(T_CM, c, r));
  203. #else
  204.     outbuf[bpos++] = '\033';
  205.     outbuf[bpos++] = '[';
  206.     if (r >= 10)
  207.         outbuf[bpos++] = r/10 + '0';
  208.     outbuf[bpos++] = r%10 + '0';
  209.     outbuf[bpos++] = ';';
  210.     if (c >= 10)
  211.         outbuf[bpos++] = c/10 + '0';
  212.     outbuf[bpos++] = c%10 + '0';
  213.     outbuf[bpos++] = 'H';
  214. #endif
  215. }
  216.  
  217. FILE *
  218. fopenb(fname, mode)
  219. char    *fname;
  220. char    *mode;
  221. {
  222.     return fopen(fname, mode);
  223. }
  224.  
  225. char *
  226. strchr(s, c)
  227. char    *s;
  228. char    c;
  229. {
  230.     char *index();
  231.  
  232.     return index(s, c);
  233. }
  234.  
  235. char *
  236. fixname(s)
  237. char    *s;
  238. {
  239.     return s;
  240. }
  241.  
  242. /*
  243.  * doshell() - run a command or an interactive shell
  244.  */
  245. void
  246. doshell(cmd)
  247. char    *cmd;
  248. {
  249.     char    *cp, *getenv();
  250.     char    cline[128];
  251.  
  252.     outstr("\r\n");
  253.     flushbuf();
  254.  
  255.     if (cmd == NULL) {
  256.         if ((cmd = getenv("SHELL")) == NULL)
  257.             cmd = "/bin/sh";
  258.         sprintf(cline, "%s -i", cmd);
  259.         cmd = cline;
  260.     }
  261.  
  262.     reset_tty();
  263.     system(cmd);
  264.     set_tty();
  265.  
  266.     wait_return();
  267. }
  268.