home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / misc / aterminfo.lha / lib_setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  4.8 KB  |  241 lines

  1.  
  2. /* This work is copyrighted. See COPYRIGHT.OLD & COPYRIGHT.NEW for   *
  3. *  details. If they are missing then this copy is in violation of    *
  4. *  the copyright conditions.                                        */
  5.  
  6. /*
  7.  *    setupterm(termname, filedes, errret)
  8.  *
  9.  *    Find and read the appropriate object file for the terminal
  10.  *    Make cur_term point to the structure.
  11.  *    Turn off the XTABS bit in the tty structure if it was on
  12.  *    If XTABS was on, remove the tab and backtab capabilities.
  13.  *
  14.  */
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #ifndef sun
  19. /* SUN's sys/ioctl.h clash with termios.h */
  20. #include <sys/ioctl.h>
  21. #endif
  22. #include "term.h"
  23.  
  24. extern int read_entry(char *, TERMINAL*);
  25.  
  26. int LINES, COLS;
  27.  
  28. #define ret_error(code, fmt, arg)    if (errret) {\
  29.                         *errret = code;\
  30.                         return(code);\
  31.                     } else {\
  32.                         fprintf(stderr, fmt, arg);\
  33.                         exit(1);\
  34.                     }
  35.  
  36. #define ret_error0(code, msg)        if (errret) {\
  37.                         *errret = code;\
  38.                         return(code);\
  39.                     } else {\
  40.                         fprintf(stderr, msg);\
  41.                         exit(1);\
  42.                     }
  43.  
  44.  
  45. static void do_prototype(void);
  46.  
  47. static int _use_env = TRUE;
  48.  
  49. void use_env(char f)
  50. {
  51.     _use_env = f;
  52. }
  53.  
  54.  
  55. static int resize(int fd)
  56. {
  57. #ifndef AMIGA
  58.  
  59. struct winsize size;
  60.  
  61.     if (ioctl(fd, TIOCGWINSZ, &size) < 0) {
  62.         perror("TIOCGWINSZ");
  63.         return 1;
  64.     }
  65.     LINES = size.ws_row;
  66.     COLS = size.ws_col;
  67.     return 0;
  68. #else
  69.     return(1);
  70. #endif
  71. }
  72.  
  73. char ttytype[NAMESIZE];
  74.  
  75. int setupterm(char *termname, int filedes, int *errret)
  76. {
  77. static int    _been_here = FALSE;
  78. char        filename1[1024];
  79. char        filename2[1024];
  80. char        *directory = SRCDIR;
  81. char        *terminfo;
  82. struct term    *term_ptr;
  83. char         *rows, *cols;
  84.  
  85.     if (_been_here == FALSE) {
  86.         _been_here = TRUE;
  87. #ifdef TRACE
  88.         _init_trace();
  89.         if (_tracing)
  90.                 _tracef("setupterm(%s,%d,%x) called", termname, filedes, errret);
  91. #endif
  92.  
  93.         if (termname == NULL) {
  94.                 termname = getenv("TERM");
  95.                 if (termname == NULL)
  96.                 ret_error0(-1, "TERM environment variable not set.\n");
  97.         }
  98.  
  99.                 term_ptr = (struct term *) malloc(sizeof(struct term));
  100.  
  101.         if (term_ptr == NULL)
  102.                 ret_error0(-1, "Not enough memory to create terminal structure.\n") ;
  103.  
  104.         if ((terminfo = getenv("TERMINFO")) != NULL)
  105.                 directory = terminfo;
  106.  
  107. #ifndef _AMIGA
  108.         sprintf(filename1, "%s/%c/%s", directory, termname[0], termname);
  109.         sprintf(filename2, "%s/%c/%s", SRCDIR, termname[0], termname);
  110. #else
  111.         sprintf(filename1, "%s%c/%s", directory, termname[0], termname);
  112.         sprintf(filename2, "%s%c/%s", SRCDIR, termname[0], termname);
  113. #endif
  114.  
  115.         if (read_entry(filename1, term_ptr) < 0 &&  read_entry(filename2, term_ptr) < 0)
  116.                 ret_error(0, "'%s': Unknown terminal type.\n", termname);
  117.  
  118.         cur_term = term_ptr;
  119.         if (command_character  &&  getenv("CC"))
  120.                 do_prototype();
  121.  
  122.         strncpy(ttytype, cur_term->term_names, NAMESIZE - 1);
  123.         ttytype[NAMESIZE - 1] = '\0';
  124.         cur_term->Filedes = filedes;
  125.  
  126.         /* figure out the size of the screen */
  127.  
  128.         rows = getenv("LINES");
  129.         if (rows != (char *)NULL)
  130.             LINES = atoi(rows);
  131.  
  132.         cols = getenv("COLUMNS");
  133.         if (cols != (char *)NULL)
  134.             COLS = atoi(cols);
  135.  
  136.         /* if _use_env is false then override the environment */
  137.  
  138.         if (_use_env == FALSE)
  139.             if (lines > 0 && columns > 0) {
  140.                 LINES = lines;
  141.                 COLS  = columns;
  142.             }
  143.  
  144.         /* If _use_env is true but environment is undefined:
  145.            try lines/columns, else use window size,
  146.            else give up.
  147.         */
  148.  
  149.         if (LINES <= 0 || COLS <= 0) {
  150.             if (lines > 0 && columns > 0) {
  151.                 LINES = lines;
  152.                 COLS  = columns;
  153.             } else if (resize(filedes) == 1) {
  154.                 fprintf(stderr, "can't find the screen size");
  155.                 exit(1);
  156.             }
  157.         }
  158.         lines = LINES;
  159.         columns = COLS;
  160.  
  161. #ifdef TRACE
  162.         _tracef("screen size is %dx%d and %dx%d", COLS, LINES, columns, lines);
  163. #endif
  164.  
  165. #ifndef AMIGA
  166.  
  167. #ifdef TERMIOS
  168. #ifndef BSD
  169. #define tabs XTABS
  170. #else
  171. #define tabs OXTAB
  172. #endif
  173.  
  174.  #ifndef linux
  175.          if((tcdrain(filedes)) == -1) {
  176.              perror("setupterm() tcdrain() failed:");
  177.              exit(1);
  178.          }
  179.  #endif
  180.          if((tcgetattr(filedes, &cur_term->Ottyb)) == -1) {
  181.              perror("setupterm() tcgetattr() failed:");
  182.              exit(1);
  183.          }
  184.  
  185.         if (cur_term->Ottyb.c_oflag & tabs)
  186.             tab = back_tab = NULL;
  187.  
  188.         cur_term->Nttyb = cur_term->Ottyb;
  189.         cur_term->Nttyb.c_oflag &= ~tabs;
  190. #else
  191.         gtty(filedes, &cur_term->Ottyb);
  192.         if (cur_term->Ottyb.sg_flags & XTABS)
  193.                 tab = back_tab = NULL;
  194.  
  195.         cur_term->Nttyb = cur_term->Ottyb;
  196.         cur_term->Nttyb.sg_flags &= ~XTABS;
  197. #endif
  198.  
  199.         def_prog_mode();
  200. #endif
  201.  
  202.  
  203.  
  204.     }
  205.     if (errret)
  206.         *errret = 1;
  207.     return(1);
  208.  
  209. }
  210.  
  211.  
  212. /*
  213. **    do_prototype()
  214. **
  215. **    Take the real command character out of the CC environment variable
  216. **    and substitute it in for the prototype given in 'command_character'.
  217. **
  218. */
  219.  
  220. static void
  221. do_prototype()
  222. {
  223. int    i, j;
  224. char    CC;
  225. char    proto;
  226. char    *tmp;
  227.  
  228.     tmp = getenv("CC");
  229.     CC = *tmp;
  230.     proto = *command_character;
  231.  
  232.     for (i=0; i < STRCOUNT; i++) {
  233.             j = 0;
  234.             while (cur_term->Strings[i][j]) {
  235.             if (cur_term->Strings[i][j] == proto)
  236.                     cur_term->Strings[i][j] = CC;
  237.             j++;
  238.             }
  239.     }
  240. }
  241.