Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / local / lbl / sbr / trmsbr.c
1 /* trmsbr.c - minor termcap support (load with -ltermlib) */
2
3 #include "../h/mh.h"
4 #include <stdio.h>
5 #ifndef SYS5
6 #include <sgtty.h>
7 #else   SYS5
8 #include <sys/types.h>
9 #include <termio.h>
10 #include <sys/ioctl.h>
11 #endif  SYS5
12 #include <ctype.h>
13
14
15 #if     BUFSIZ<2048
16 #define TXTSIZ  2048
17 #else
18 #define TXTSIZ  BUFSIZ
19 #endif
20
21 char    PC;
22 short   ospeed;
23
24 int     tgetent (), tgetnum ();
25 char   *tgetstr ();
26
27 /* \f */
28
29 static int  TCinited = 0;
30 static int  TCavailable = 0;
31 static int  initLI = 0;
32 static int  LI = 24;
33 static int  initCO = 0;
34 static int  CO = 80;
35 static char *CL = NULL;
36 static char *SE = NULL;
37 static char *SO = NULL;
38
39 static char termcap[TXTSIZ];
40 static char area[TXTSIZ];
41
42 /* \f */
43
44 static void
45 read_termcap()
46 {
47     register char  *bp,
48                    *term;
49     register int i;
50     char   *cp;
51 #ifndef SYS5
52     struct sgttyb   sg;
53 #else   SYS5
54     struct termio   sg;
55 #endif  SYS5
56
57     if (TCinited)
58         return;
59
60     ++TCinited;
61
62     if (!isatty(fileno(stdout)))
63         return;
64
65     if ((term = getenv ("TERM")) == NULL || tgetent (termcap, term) <= OK)
66         return;
67
68     ++TCavailable;
69
70 #ifndef SYS5
71     ospeed = ioctl(fileno (stdout), TIOCGETP, (char *) &sg) != NOTOK
72                 ? sg.sg_ospeed : 0;
73 #else   SYS5
74     ospeed = ioctl(fileno (stdout), TCGETA, &sg) != NOTOK
75                 ? sg.c_cflag & CBAUD : 0;
76 #endif  SYS5
77
78     if (!initCO && (i = tgetnum ("co")) > 0)
79         CO = i;
80     if (!initLI && (i = tgetnum ("li")) > 0)
81         LI = i;
82
83     cp = area;
84     CL = tgetstr ("cl", &cp);
85     if (bp = tgetstr ("pc", &cp))
86         PC = *bp;
87     if (tgetnum ("sg") <= 0) {
88         SE = tgetstr ("se", &cp);
89         SO = tgetstr ("so", &cp);
90     }
91 }
92
93 /* \f */
94
95 int
96 sc_width()
97 {
98 #ifdef  TIOCGWINSZ
99     struct winsize win;
100
101     if (ioctl(fileno (stderr), TIOCGWINSZ, &win) != NOTOK && win.ws_col > 0) {
102         CO = win.ws_col;
103         initCO++;
104     } else
105 #endif  TIOCGWINSZ
106         read_termcap();
107
108     return CO;
109 }
110
111
112 int
113 sc_length()
114 {
115 #ifdef  TIOCGWINSZ
116     struct winsize win;
117
118     if (ioctl(fileno (stderr), TIOCGWINSZ, &win) != NOTOK && win.ws_row > 0) {
119         LI = win.ws_row;
120         initLI++;
121     } else
122 #endif  TIOCGWINSZ
123         read_termcap();
124
125     return LI;
126 }
127
128 /* \f */
129
130 static int
131 outc(c)
132 register char    c;
133 {
134     (void) putchar (c);
135 }
136
137
138 void
139 clear_screen()
140 {
141     read_termcap();
142
143     if (CL && ospeed)
144         tputs (CL, LI, outc);
145     else {
146         printf ("\f");
147         if (ospeed)
148             printf ("\200");
149     }
150
151     (void) fflush (stdout);
152 }
153
154 /* \f */
155
156 /* VARARGS1 */
157
158 SOprintf(fmt, a, b, c, d, e, f)
159 char   *fmt,
160        *a,
161        *b,
162        *c,
163        *d,
164        *e,
165        *f;
166 {
167     read_termcap();
168     if (SO == NULL || SE == NULL)
169         return NOTOK;
170
171     tputs (SO, 1, outc);
172     printf (fmt, a, b, c, d, e, f);
173     tputs (SE, 1, outc);
174
175     return OK;
176 }
177
178 #define MAXTCENT 32
179 static int nTCent;
180 static char *TCid[MAXTCENT];
181 static char *TCent[MAXTCENT];
182
183 char *
184 get_termcap(id)
185         char *id;
186 {
187         register int i;
188         char *cp;
189         static char tcbuf[1024];
190         static char *tcptr = tcbuf;
191
192         for (i = 0; i < nTCent; ++i)
193                 if (strcmp(TCid[i], id) == 0)
194                         return (TCent[i]);
195
196         if (nTCent >= MAXTCENT)
197                 return ("");
198
199         TCid[nTCent] = getcpy(id);
200
201         if (TCinited == 0)
202                 read_termcap();
203
204         if (!TCavailable)
205                 return ("");
206
207         cp = tgetstr(id, &tcptr);
208         if (cp == 0) {
209                 /* check for some alternates for the entry */
210                 if (strcmp(id, "mb") == 0 ||
211                     strcmp(id, "md") == 0 ||
212                     strcmp(id, "mh") == 0 ||
213                     strcmp(id, "mr") == 0 ||
214                     strcmp(id, "us") == 0)
215                         id = "so";
216                 else if (strcmp(id, "me") == 0 ||
217                          strcmp(id, "ue") == 0)
218                         id = "se";
219                 cp = tgetstr(id, &tcptr);
220                 if (cp == 0)
221                         cp = "";
222         }
223
224         /* skip leading delay spec */
225         while (isdigit(*cp))
226                 ++cp;
227         if (*cp == '.')
228                 if (isdigit(*++cp))
229                         ++cp;
230         if (*cp == '*')
231                 ++cp;
232
233         TCent[nTCent] = cp;
234         ++nTCent;
235         return (cp);
236 }