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

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