home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / STVI369G.ZIP / MINIX.C < prev    next >
C/C++ Source or Header  |  1990-05-01  |  5KB  |  329 lines

  1. /* $Header: /nw/tony/src/stevie/src/RCS/minix.c,v 1.5 89/07/11 21:24:18 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. /*
  11.  * inchar() - get a character from the keyboard
  12.  */
  13. int
  14. inchar()
  15. {
  16.     char    c;
  17.  
  18.     flushbuf();        /* flush any pending output */
  19.  
  20.     while (read(0, &c, 1) != 1)
  21.         ;
  22.  
  23.     got_int = FALSE;
  24.     return c;
  25. }
  26.  
  27. #define    BSIZE    2048
  28. static    char    outbuf[BSIZE];
  29. static    int    bpos = 0;
  30.  
  31. void
  32. flushbuf()
  33. {
  34.     if (bpos != 0)
  35.         write(1, outbuf, bpos);
  36.     bpos = 0;
  37. }
  38.  
  39. /*
  40.  * Macro to output a character. Used within this file for speed.
  41.  */
  42. #define    outone(c)    outbuf[bpos++] = c; if (bpos >= BSIZE) flushbuf()
  43.  
  44. /*
  45.  * Function version for use outside this file.
  46.  */
  47. void
  48. outchar(c)
  49. register char    c;
  50. {
  51.     outbuf[bpos++] = c;
  52.     if (bpos >= BSIZE)
  53.         flushbuf();
  54. }
  55.  
  56. void
  57. outstr(s)
  58. register char    *s;
  59. {
  60.     while (*s) {
  61.         outone(*s++);
  62.     }
  63. }
  64.  
  65. void
  66. beep()
  67. {
  68.     if ( P(P_VB) )
  69.         vbeep();
  70.     else
  71.         outone('\007');
  72. }
  73.  
  74. /*
  75.  * remove(file) - remove a file
  76.  */
  77. void
  78. remove(file)
  79. char *file;
  80. {
  81.     unlink(file);
  82. }
  83.  
  84. /*
  85.  * rename(of, nf) - rename existing file 'of' to 'nf'
  86.  */
  87. void
  88. rename(of, nf)
  89. char    *of, *nf;
  90. {
  91.     unlink(nf);
  92.     link(of, nf);
  93.     unlink(of);
  94. }
  95.  
  96. void
  97. pause()
  98. {
  99.     sleep (1);
  100. }
  101.  
  102. static    struct    sgttyb    ostate;
  103.  
  104. /*
  105.  * Go into cbreak mode
  106.  */
  107. void
  108. set_tty()
  109. {
  110.     struct    sgttyb    nstate;
  111.  
  112.      ioctl(0, TIOCGETP, &ostate);
  113.      nstate = ostate;
  114.      nstate.sg_flags &= ~(XTABS|ECHO);
  115.      nstate.sg_flags |= CBREAK;
  116.      ioctl(0, TIOCSETP, &nstate);
  117. }
  118.  
  119. /*
  120.  * Restore original terminal modes
  121.  */
  122. void
  123. reset_tty()
  124. {
  125.     ioctl(0, TIOCSETP, &ostate);
  126. }
  127.  
  128. void
  129. sig()
  130. {
  131.     signal(SIGINT, sig);
  132.     signal(SIGQUIT, sig);
  133.  
  134.     got_int = TRUE;
  135. }
  136.  
  137. void
  138. windinit()
  139. {
  140. #ifdef    TERMCAP
  141.     if (t_init() != 1) {
  142.         fprintf(stderr, "unknown terminal type\n");
  143.         exit(1);
  144.     }
  145. #else
  146.     Columns = 80;
  147.     P(P_LI) = Rows = 25;
  148. #endif
  149.     /*
  150.      * The code here makes sure that there isn't a window during which
  151.      * we could get interrupted and exit with the tty in a weird state.
  152.      */
  153.     signal(SIGINT, sig);
  154.     signal(SIGQUIT, sig);
  155.  
  156.     set_tty();
  157.  
  158.     if (got_int)
  159.         windexit(0);
  160. }
  161.  
  162. void
  163. windexit(r)
  164. int r;
  165. {
  166.     reset_tty();
  167.     exit(r);
  168. }
  169.  
  170. void
  171. windgoto(r, c)
  172. register int    r, c;
  173. {
  174. #ifdef    TERMCAP
  175.     char    *tgoto();
  176. #else
  177.     r += 1;
  178.     c += 1;
  179. #endif
  180.  
  181.     /*
  182.      * Check for overflow once, to save time.
  183.      */
  184.     if (bpos + 8 >= BSIZE)
  185.         flushbuf();
  186.  
  187. #ifdef    TERMCAP
  188.     outstr(tgoto(T_CM, c, r));
  189. #else
  190.     outbuf[bpos++] = '\033';
  191.     outbuf[bpos++] = '[';
  192.     if (r >= 10)
  193.         outbuf[bpos++] = r/10 + '0';
  194.     outbuf[bpos++] = r%10 + '0';
  195.     outbuf[bpos++] = ';';
  196.     if (c >= 10)
  197.         outbuf[bpos++] = c/10 + '0';
  198.     outbuf[bpos++] = c%10 + '0';
  199.     outbuf[bpos++] = 'H';
  200. #endif
  201. }
  202.  
  203. FILE *
  204. fopenb(fname, mode)
  205. char    *fname;
  206. char    *mode;
  207. {
  208.     return fopen(fname, mode);
  209. }
  210.  
  211. char *
  212. strchr(s, c)
  213. char    *s;
  214. char    c;
  215. {
  216.     char *index();
  217.  
  218.     return index(s, c);
  219. }
  220.  
  221. char *
  222. fixname(s)
  223. char    *s;
  224. {
  225.     return s;
  226. }
  227.  
  228. /*
  229.  * doshell() - run a command or an interactive shell
  230.  */
  231. void
  232. doshell(cmd)
  233. char    *cmd;
  234. {
  235.     char    *cp, *getenv();
  236.     char    cline[128];
  237.  
  238.     outstr("\r\n");
  239.     flushbuf();
  240.  
  241.     if (cmd == NULL) {
  242.         if ((cmd = getenv("SHELL")) == NULL)
  243.             cmd = "/bin/sh";
  244.         sprintf(cline, "%s -i", cmd);
  245.         cmd = cline;
  246.     }
  247.  
  248.     reset_tty();
  249.     system(cmd);
  250.     set_tty();
  251.  
  252.     wait_return();
  253. }
  254.  
  255. /*
  256.  *    FILL IT IN, FOR YOUR SYSTEM, AND SHARE IT!
  257.  *
  258.  *    The next couple of functions do system-specific stuff.
  259.  *    They currently do nothing; I'm not familiar enough with
  260.  *    system-specific programming on this system.
  261.  *    If you fill it in for your system, please post the results
  262.  *    and share with the rest of us.
  263.  */
  264.  
  265.  
  266. setcolor (c)
  267. /*
  268.  * Set the color to c, using the local system convention for numbering
  269.  * colors or video attributes.
  270.  *
  271.  * If you implement this, remember to note the original color in
  272.  * windinit(), before you do any setcolor() commands, and
  273.  * do a setcolor() back to the original as part of windexit().
  274.  */
  275.   int c:
  276. {
  277. }
  278.  
  279.  
  280. setrows (r)
  281. /*
  282.  * Set the number of lines to r, if possible.  Otherwise
  283.  * "do the right thing".  Return the number of lines actually set.
  284.  *
  285.  * If you implement this, remember to note the original number of rows
  286.  * in windinit(), before you do any setrows() commands, and
  287.  * do a setrows() back to the original as part of windexit().
  288.  */
  289.   int r;
  290. {
  291.     /* Since we do nothing, just return the current number of lines */
  292.     return ( P(P_LI) );
  293. }
  294.  
  295.  
  296. vbeep ()
  297. /*
  298.  * Do a "visual bell".  This generally consists of flashing the screen
  299.  * once in inverse video.
  300.  */
  301. {
  302.     int    color, revco;
  303.  
  304.     color = P( P_CO );        /* get current color */
  305.     revco = reverse_color (color);    /* system-specific */
  306.     setcolor (revco);
  307.     flushbuf ();
  308.     pause ();
  309.     setcolor (color);
  310.     windgoto (Cursrow, Curscol);
  311.     flushbuf ();
  312. }
  313.  
  314. reverse_color (co)
  315. /*
  316.  * Returns the inverse video attribute or color of co.
  317.  * The existing code below is VERY simple-minded.
  318.  * Replace it with proper code for your system.
  319.  */
  320.  int co;
  321. {
  322.     if (co)        return (0);
  323.     else        return (1);
  324. }
  325.  
  326.  
  327. /********** End of do-it-yourself kit **********************/
  328.  
  329.