1 /* 2 ** termsbr.c -- termcap support 3 ** 4 ** This code is Copyright (c) 2002, by the authors of nmh. See the 5 ** COPYRIGHT file in the root directory of the nmh distribution for 6 ** complete copyright information. 7 */ 8 9 #include <h/mh.h> 10 11 #include <termios.h> 12 13 #ifdef HAVE_TERMCAP_H 14 # include <termcap.h> 15 #endif 16 17 /* <sys/ioctl.h> is need anyway for ioctl() 18 #ifdef GWINSZ_IN_SYS_IOCTL 19 */ 20 # include <sys/ioctl.h> 21 /* 22 #endif 23 */ 24 25 #ifdef WINSIZE_IN_PTEM 26 # include <sys/stream.h> 27 # include <sys/ptem.h> 28 #endif 29 30 #if BUFSIZ<2048 31 # define TXTSIZ 2048 32 #else 33 # define TXTSIZ BUFSIZ 34 #endif 35 36 static long speedcode; 37 38 static int initCO = 0; 39 40 static int CO = 80; /* number of colums */ 41 42 43 static void read_termcap(void)44read_termcap(void) 45 { 46 char *term; 47 48 #ifndef TGETENT_ACCEPTS_NULL 49 char termbuf[TXTSIZ]; 50 #endif 51 52 struct termios tio; 53 static int inited = 0; 54 55 if (inited++) 56 return; 57 58 if (!(term = getenv("TERM"))) 59 return; 60 61 /* 62 ** If possible, we let tgetent allocate its own termcap buffer 63 */ 64 #ifdef TGETENT_ACCEPTS_NULL 65 if (tgetent(NULL, term) != TGETENT_SUCCESS) 66 return; 67 #else 68 if (tgetent(termbuf, term) != TGETENT_SUCCESS) 69 return; 70 #endif 71 72 speedcode = cfgetospeed(&tio); 73 74 if (!initCO && (CO = tgetnum("co")) <= 0) 75 CO = 80; 76 } 77 78 79 int sc_width(void)80sc_width(void) 81 { 82 #ifdef TIOCGWINSZ 83 struct winsize win; 84 int width; 85 86 if (ioctl(fileno(stderr), TIOCGWINSZ, &win) != NOTOK 87 && (width = win.ws_col) > 0) { 88 CO = width; 89 initCO++; 90 } else 91 #endif /* TIOCGWINSZ */ 92 read_termcap(); 93 94 return CO; 95 } 96