Removed the -clear switch from mhl.
[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 initLI = 0;
61 static int initCO = 0;
62
63 static int LI = 40;      /* number of lines */
64 static int CO = 80;      /* number of colums */
65 static char *SE = NULL;  /* termcap string to end standout mode   */
66 static char *SO = NULL;  /* termcap string to begin standout mode */
67
68 static char termcap[TXTSIZ];
69
70
71 static void
72 read_termcap(void)
73 {
74         char *bp, *cp;
75         char *term;
76
77 #ifndef TGETENT_ACCEPTS_NULL
78         char termbuf[TXTSIZ];
79 #endif
80
81 #ifdef HAVE_TERMIOS_H
82         struct termios tio;
83 #else
84 # ifdef HAVE_TERMIO_H
85         struct termio tio;
86 # else
87         struct sgttyb tio;
88 # endif
89 #endif
90
91         static int inited = 0;
92
93         if (inited++)
94                 return;
95
96         if (!(term = getenv("TERM")))
97                 return;
98
99 /*
100 ** If possible, we let tgetent allocate its own termcap buffer
101 */
102 #ifdef TGETENT_ACCEPTS_NULL
103         if (tgetent(NULL, term) != TGETENT_SUCCESS)
104                 return;
105 #else
106         if (tgetent(termbuf, term) != TGETENT_SUCCESS)
107                 return;
108 #endif
109
110 #ifdef HAVE_TERMIOS_H
111         speedcode = cfgetospeed(&tio);
112 #else
113 # ifdef HAVE_TERMIO_H
114         speedcode = ioctl(fileno(stdout), TCGETA, &tio) != NOTOK ?
115                         tio.c_cflag & CBAUD : 0;
116 # else
117         speedcode = ioctl(fileno(stdout), TIOCGETP, (char *) &tio) != NOTOK ?
118                         tio.sg_ospeed : 0;
119 # endif
120 #endif
121
122         if (!initCO && (CO = tgetnum("co")) <= 0)
123                 CO = 80;
124         if (!initLI && (LI = tgetnum("li")) <= 0)
125                 LI = 24;
126
127         cp = termcap;
128         if ((bp = tgetstr("pc", &cp)))
129                 PC = *bp;
130         if (tgetnum("sg") <= 0) {
131                 SE = tgetstr("se", &cp);
132                 SO = tgetstr("so", &cp);
133         }
134 }
135
136
137 int
138 sc_width(void)
139 {
140 #ifdef TIOCGWINSZ
141         struct winsize win;
142         int width;
143
144         if (ioctl(fileno(stderr), TIOCGWINSZ, &win) != NOTOK
145                         && (width = win.ws_col) > 0) {
146                 CO = width;
147                 initCO++;
148         } else
149 #endif /* TIOCGWINSZ */
150                 read_termcap();
151
152         return CO;
153 }
154
155
156 int
157 sc_length(void)
158 {
159 #ifdef TIOCGWINSZ
160         struct winsize win;
161
162         if (ioctl(fileno(stderr), TIOCGWINSZ, &win) != NOTOK
163                         && (LI = win.ws_row) > 0)
164                 initLI++;
165         else
166 #endif /* TIOCGWINSZ */
167                 read_termcap();
168
169         return LI;
170 }
171
172
173 /*
174 ** print in standout mode
175 */
176 int
177 SOprintf(char *fmt, ...)
178 {
179         va_list ap;
180
181         read_termcap();
182         if (!(SO && SE))
183                 return NOTOK;
184
185         tputs(SO, 1, putchar);
186
187         va_start(ap, fmt);
188         vprintf(fmt, ap);
189         va_end(ap);
190
191         tputs(SE, 1, putchar);
192
193         return OK;
194 }