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