Removed the now unused SOprint().
[mmh] / uip / termsbr.c
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 #ifdef HAVE_TERMIOS_H
12 # include <termios.h>
13 #else
14 # ifdef HAVE_TERMIO_H
15 #  include <termio.h>
16 # else
17 #  include <sgtty.h>
18 # endif
19 #endif
20
21 #ifdef HAVE_TERMCAP_H
22 # include <termcap.h>
23 #endif
24
25 /* <sys/ioctl.h> is need anyway for ioctl()
26 #ifdef GWINSZ_IN_SYS_IOCTL
27 */
28 # include <sys/ioctl.h>
29 /*
30 #endif
31 */
32
33 #ifdef WINSIZE_IN_PTEM
34 # include <sys/stream.h>
35 # include <sys/ptem.h>
36 #endif
37
38 #if BUFSIZ<2048
39 # define TXTSIZ 2048
40 #else
41 # define TXTSIZ BUFSIZ
42 #endif
43
44 /*
45 ** These variables are sometimes defined in,
46 ** and needed by the termcap library.
47 */
48 #ifdef HAVE_OSPEED
49 # ifdef MUST_DEFINE_OSPEED
50 extern short ospeed;
51 extern char PC;
52 # endif
53 #else
54 short ospeed;
55 char PC;
56 #endif
57
58 static long speedcode;
59
60 static int initCO = 0;
61
62 static int CO = 80;      /* number of colums */
63
64 static char termcap[TXTSIZ];
65
66
67 static void
68 read_termcap(void)
69 {
70         char *bp, *cp;
71         char *term;
72
73 #ifndef TGETENT_ACCEPTS_NULL
74         char termbuf[TXTSIZ];
75 #endif
76
77 #ifdef HAVE_TERMIOS_H
78         struct termios tio;
79 #else
80 # ifdef HAVE_TERMIO_H
81         struct termio tio;
82 # else
83         struct sgttyb tio;
84 # endif
85 #endif
86
87         static int inited = 0;
88
89         if (inited++)
90                 return;
91
92         if (!(term = getenv("TERM")))
93                 return;
94
95 /*
96 ** If possible, we let tgetent allocate its own termcap buffer
97 */
98 #ifdef TGETENT_ACCEPTS_NULL
99         if (tgetent(NULL, term) != TGETENT_SUCCESS)
100                 return;
101 #else
102         if (tgetent(termbuf, term) != TGETENT_SUCCESS)
103                 return;
104 #endif
105
106 #ifdef HAVE_TERMIOS_H
107         speedcode = cfgetospeed(&tio);
108 #else
109 # ifdef HAVE_TERMIO_H
110         speedcode = ioctl(fileno(stdout), TCGETA, &tio) != NOTOK ?
111                         tio.c_cflag & CBAUD : 0;
112 # else
113         speedcode = ioctl(fileno(stdout), TIOCGETP, (char *) &tio) != NOTOK ?
114                         tio.sg_ospeed : 0;
115 # endif
116 #endif
117
118         if (!initCO && (CO = tgetnum("co")) <= 0)
119                 CO = 80;
120
121         cp = termcap;
122         if ((bp = tgetstr("pc", &cp)))
123                 PC = *bp;
124 }
125
126
127 int
128 sc_width(void)
129 {
130 #ifdef TIOCGWINSZ
131         struct winsize win;
132         int width;
133
134         if (ioctl(fileno(stderr), TIOCGWINSZ, &win) != NOTOK
135                         && (width = win.ws_col) > 0) {
136                 CO = width;
137                 initCO++;
138         } else
139 #endif /* TIOCGWINSZ */
140                 read_termcap();
141
142         return CO;
143 }