Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / trmsbr.c
1 /* trmsbr.c - minor termcap support (load with -ltermlib) */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: trmsbr.c,v 2.5 1992/12/15 00:20:22 jromine Exp $";
4 #endif  /* lint */
5
6 #include "../h/mh.h"
7 #include <stdio.h>
8 #ifndef SYS5
9 #include <sgtty.h>
10 #if     defined(ULTRIX) && !defined(BSD43)
11 #undef  TIOCGWINSZ
12 #endif
13 #else   /* SYS5 */
14 #include <sys/types.h>
15 #include <termio.h>
16 #ifndef NOIOCTLH
17 #include <sys/ioctl.h>
18 #endif  /* NOIOCTLH */
19 #undef  TIOCGWINSZ
20 #endif  /* SYS5 */
21
22
23 #if     BUFSIZ<2048
24 #define TXTSIZ  2048
25 #else
26 #define TXTSIZ  BUFSIZ
27 #endif
28
29 #ifndef SYS5
30 extern char PC;
31 extern short    ospeed;
32 #else   /*   SYS5 */
33 char    PC;
34 short   ospeed;
35 #endif  /* SYS5 */
36
37 int     tgetent (), tgetnum ();
38 char   *tgetstr ();
39
40 /* \f */
41
42 static int  HC = 0;
43
44 static int  initLI = 0;
45 static int  LI = 40;
46 static int  initCO = 0;
47 static int  CO = 80;
48 static char *CL = NULL;
49 static char *SE = NULL;
50 static char *SO = NULL;
51
52 static char termcap[TXTSIZ];
53
54 /* \f */
55
56 static  read_termcap () {
57     register char  *bp,
58                    *term;
59     char   *cp,
60             myterm[TXTSIZ];
61 #ifndef SYS5
62     struct sgttyb   sg;
63 #else   /* SYS5 */
64     struct termio   sg;
65 #endif  /* SYS5 */
66     static int  inited = 0;
67
68     if (inited++)
69         return;
70
71     if ((term = getenv ("TERM")) == NULL || tgetent (myterm, term) <= OK)
72         return;
73
74 #ifndef SYS5
75     ospeed = ioctl (fileno (stdout), TIOCGETP, (char *) &sg) != NOTOK
76                 ? sg.sg_ospeed : 0;
77 #else   /* SYS5 */
78     ospeed = ioctl (fileno (stdout), TCGETA, &sg) != NOTOK
79                 ? sg.c_cflag & CBAUD : 0;
80 #endif  /* SYS5 */
81
82     HC = tgetflag ("hc");
83
84     if (!initCO && (CO = tgetnum ("co")) <= 0)
85         CO = 80;
86     if (!initLI && (LI = tgetnum ("li")) <= 0)
87         LI = 24;
88
89     cp = termcap;
90     CL = tgetstr ("cl", &cp);
91     if (bp = tgetstr ("pc", &cp))
92         PC = *bp;
93     if (tgetnum ("sg") <= 0) {
94         SE = tgetstr ("se", &cp);
95         SO = tgetstr ("so", &cp);
96     }
97 }
98
99 /* \f */
100
101 int     sc_width () {
102 #ifdef  TIOCGWINSZ
103     struct winsize win;
104     int width;
105
106     if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
107             && (width = win.ws_col) > 0) {
108         CO = width;
109         initCO++;
110     } else
111 #endif  /* TIOCGWINSZ */
112         read_termcap ();
113
114     return CO;
115 }
116
117
118 int     sc_length () {
119 #ifdef  TIOCGWINSZ
120     struct winsize win;
121
122     if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
123             && (LI = win.ws_row) > 0)
124         initLI++;
125     else
126 #endif  /* TIOCGWINSZ */
127         read_termcap ();
128
129     return LI;
130 }
131
132 /* \f */
133
134 static int  outc (c)
135 register char    c;
136 {
137     (void) putchar (c);
138 }
139
140
141 void clear_screen () {
142     read_termcap ();
143
144     if (CL && ospeed)
145         tputs (CL, LI, outc);
146     else {
147         printf ("\f");
148         if (ospeed)
149             printf ("\200");
150     }
151
152     (void) fflush (stdout);
153 }
154
155 /* \f */
156
157 /* VARARGS1 */
158
159 int     SOprintf (fmt, a, b, c, d, e, f)
160 char   *fmt,
161        *a,
162        *b,
163        *c,
164        *d,
165        *e,
166        *f;
167 {
168     read_termcap ();
169     if (SO == NULL || SE == NULL)
170         return NOTOK;
171
172     tputs (SO, 1, outc);
173     printf (fmt, a, b, c, d, e, f);
174     tputs (SE, 1, outc);
175
176     return OK;
177 }
178
179 /* \f */
180
181 int     sc_hardcopy () {
182     read_termcap ();
183
184     return HC;
185 }