home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / letters / term.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-02  |  3.6 KB  |  162 lines

  1. /*
  2.  * this program will attempt to draw random stuff on the screen by using the
  3.  * termcap library.  Much of this code was evolved from term.c from the nn
  4.  * sources.
  5.  *
  6.  * Larry Moss (lm03_cif@uhura.cc.rochester.edu)
  7.  * Kim Storm deserves a fair amount of credit for this.  It may not look
  8.  *   too much like his code, but his code was at least more understandable
  9.  *   than the documentation I was trying to work from and I'd like to give
  10.  *   credit for making it easier for me to write.
  11.  *
  12.  * I wrote a fair amount of this based on man pages, then used Kim's code as
  13.  * a reference to make things work.  Some of this actually ended up looking
  14.  * more like his code than I planned on.  I hope I haven't gone overboard.
  15.  */
  16.  
  17. #include <stdio.h>
  18. #ifndef NEXT
  19. # ifdef AMIGA
  20. #  include <stdlib.h>
  21. #  include "amitermcap.h"
  22. # else
  23. #  include <termio.h>
  24. # endif
  25. #endif
  26. #include <string.h>
  27. #include "term.h"
  28.  
  29. char    *tgoto();
  30. char    PC, *BC, *UP;
  31.  
  32. char        *term_name;
  33. char    XBC[64], XUP[64];
  34. char    bell_str[256] = "\007";
  35. char     cursor_home[64];
  36. char     clear_screen[64];
  37. char     cursor_address[128];
  38. char     enter_standout_mode[64], exit_standout_mode[64];
  39. char     enter_underline_mode[64], exit_underline_mode[64];
  40.  
  41. outc(c)
  42. {
  43.     putchar(c);
  44. }
  45.  
  46. bell() {
  47.     putp(bell_str);
  48. }
  49.  
  50. quiet() {
  51. }
  52.  
  53. int Lines, Columns;    /* screen size */
  54. int garbage_size;    /* number of garbage chars left from so */
  55. int double_garbage;    /* space needed to enter&exit standout mode */
  56. int STANDOUT;        /* terminal got standout mode */
  57. int TWRAP;        /* terminal got automatic margins */
  58.  
  59. /*
  60.  * used to get the actual terminal control string.
  61.  */
  62. opt_cap(cap, buf)
  63. char           *cap, *buf;
  64. {
  65.     char    *tgetstr();
  66.  
  67.     *buf = '\0';
  68.     return tgetstr(cap, &buf) != NULL;
  69. }
  70.  
  71. /*
  72.  * call opt_cap to get control string.  report if the terminal lacks that
  73.  * capability.
  74.  */
  75. get_cap(cap, buf)
  76. char *cap, *buf;
  77. {
  78.     if (!opt_cap(cap, buf))
  79.         fprintf(stderr, "TERMCAP entry for %s has no '%s' capability\n",
  80.             term_name, cap);
  81. }
  82.  
  83. /*
  84.  * set everythign up.  find the necessary strings to control the terminal.
  85.  */
  86. init_term()
  87. {
  88.     char            tbuf[1024];
  89.  
  90. #ifndef AMIGA
  91.     /*
  92.      * get terminal type from the environment or have the user enter it
  93.      */
  94.     if ((term_name = (char *)getenv("TERM")) == NULL) {
  95.         fprintf(stderr, "No TERM variable in environment\n");
  96.         fprintf(stderr, "Enter terminal type to use: ");
  97.         scanf("%s", term_name = (char *)malloc(30 * sizeof(char)));
  98.     }
  99. #endif
  100.  
  101.     /*
  102.      * get the termcap entry for the terminal above
  103.      */
  104.     if (tgetent(tbuf, term_name) <= 0) {
  105.         fprintf(stderr, "Unknown terminal type: %s\n", term_name);
  106.         exit(1);
  107.     }
  108.  
  109.     /*
  110.      * get the padding character for the terminal
  111.      */
  112.     opt_cap("pc", cursor_address);    /* temp. usage */
  113.     PC = cursor_address[0];
  114.  
  115.     get_cap("cm", cursor_address);
  116.     if (!opt_cap("ho", cursor_home))
  117.         strcpy(cursor_home, tgoto(cursor_address, 0, 0));
  118.  
  119.     get_cap("cl", clear_screen);
  120.  
  121.     Lines = tgetnum("li");
  122.     Columns = tgetnum("co");
  123.  
  124.     opt_cap("so", enter_standout_mode);
  125.     opt_cap("se", exit_standout_mode);
  126.  
  127.     opt_cap("us", enter_underline_mode);
  128.     opt_cap("ue", exit_underline_mode);
  129.  
  130.     garbage_size = tgetnum("sg");
  131.  
  132.     TWRAP = tgetflag("am");
  133.  
  134.     STANDOUT = HAS_CAP(enter_standout_mode);
  135.     if (STANDOUT) {
  136.         if (garbage_size < 0)
  137.             garbage_size = 0;
  138.         double_garbage = 2 * garbage_size;
  139.     } else
  140.         garbage_size = double_garbage = 0;
  141. }
  142.  
  143. underline(on)
  144. {
  145.     if (garbage_size)
  146.         return 0;
  147.     if (!HAS_CAP(enter_underline_mode))
  148.         return 0;
  149.     putp(on ? enter_underline_mode : exit_underline_mode);
  150.     return 1;
  151. }
  152.  
  153. highlight(on)
  154. {
  155.     if (garbage_size)
  156.         return 0;
  157.     if (!HAS_CAP(enter_standout_mode))
  158.         return 0;
  159.     putp(on ? enter_standout_mode : exit_standout_mode);
  160.     return 1;
  161. }
  162.