Fixed build on Cygwin by adding configure check for ncurses/termcap.h.
[mmh] / uip / termsbr.c
1
2 /*
3  * termsbr.c -- termcap support
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  */
9
10 #include <h/mh.h>
11
12 #include <termios.h>
13
14 /* It might be better to tie this to the termcap_curses_order in
15    configure.ac.  It would be fine to check for ncurses/termcap.h
16    first on Linux, it's a symlink to termcap.h.  */
17 #ifdef HAVE_TERMCAP_H
18 # include <termcap.h>
19 #elif defined (HAVE_NCURSES_TERMCAP_H)
20 # include <ncurses/termcap.h>
21 #endif
22
23 /* <sys/ioctl.h> is need anyway for ioctl()
24 #ifdef GWINSZ_IN_SYS_IOCTL
25 */
26 # include <sys/ioctl.h>
27 /*
28 #endif
29 */
30
31 #ifdef WINSIZE_IN_PTEM
32 # include <sys/stream.h>
33 # include <sys/ptem.h>
34 #endif
35
36 #if BUFSIZ<2048
37 # define TXTSIZ 2048
38 #else
39 # define TXTSIZ BUFSIZ
40 #endif
41
42 #ifdef notdef_lyndon_posix
43
44 /*
45         XXX No current termcap should need this.  If your compile barfs,
46         email details to lyndon@orthanc.ca.  This code will vanish soon ...
47 */
48 /*
49  * These variables are sometimes defined in,
50  * and needed by the termcap library.
51  */
52 # ifdef HAVE_OSPEED
53 #  ifdef MUST_DEFINE_OSPEED
54 extern short ospeed;
55 extern char PC;
56 #  endif
57 # else
58 short ospeed;
59 char PC;
60 # endif
61
62 #endif /* notdef_lyndon_posix */
63
64 static long speedcode;
65
66 static int initLI = 0;
67 static int initCO = 0;
68
69 static int HC = 0;       /* are we on a hardcopy terminal?        */
70 static int LI = 40;      /* number of lines                       */
71 static int CO = 80;      /* number of colums                      */
72 static char *CL = NULL;  /* termcap string to clear screen        */
73 static char *SE = NULL;  /* termcap string to end standout mode   */
74 static char *SO = NULL;  /* termcap string to begin standout mode */
75
76 static char termcap[TXTSIZ];
77
78
79 static void
80 read_termcap(void)
81 {
82     char *bp, *cp;
83     char *term;
84
85 #ifndef TGETENT_ACCEPTS_NULL
86     char termbuf[TXTSIZ];
87 #endif
88
89     struct termios tio;
90     static int inited = 0;
91
92     if (inited++)
93         return;
94
95     if (!(term = getenv ("TERM")))
96         return;
97
98 /*
99  * If possible, we let tgetent allocate its own termcap buffer
100  */
101 #ifdef TGETENT_ACCEPTS_NULL
102     if (tgetent (NULL, term) != TGETENT_SUCCESS)
103         return;
104 #else
105     if (tgetent (termbuf, term) != TGETENT_SUCCESS)
106         return;
107 #endif
108
109     speedcode = cfgetospeed(&tio);
110
111     HC = tgetflag ("hc");
112
113     if (!initCO && (CO = tgetnum ("co")) <= 0)
114         CO = 80;
115     if (!initLI && (LI = tgetnum ("li")) <= 0)
116         LI = 24;
117
118     cp = termcap;
119     CL = tgetstr ("cl", &cp);
120     if ((bp = tgetstr ("pc", &cp)))
121         PC = *bp;
122     if (tgetnum ("sg") <= 0) {
123         SE = tgetstr ("se", &cp);
124         SO = tgetstr ("so", &cp);
125     }
126 }
127
128
129 int
130 sc_width (void)
131 {
132 #ifdef TIOCGWINSZ
133     struct winsize win;
134     int width;
135
136     if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
137             && (width = win.ws_col) > 0) {
138         CO = width;
139         initCO++;
140     } else
141 #endif /* TIOCGWINSZ */
142         read_termcap();
143
144     return CO;
145 }
146
147
148 int
149 sc_length (void)
150 {
151 #ifdef TIOCGWINSZ
152     struct winsize win;
153
154     if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
155             && (LI = win.ws_row) > 0)
156         initLI++;
157     else
158 #endif /* TIOCGWINSZ */
159         read_termcap();
160
161     return LI;
162 }
163
164
165 static int
166 outc (int c)
167 {
168     return putchar(c);
169 }
170
171
172 void
173 clear_screen (void)
174 {
175     read_termcap ();
176
177     if (CL && speedcode)
178         tputs (CL, LI, outc);
179     else {
180         printf ("\f");
181         if (speedcode)
182             printf ("\200");
183     }
184
185     fflush (stdout);
186 }
187
188
189 /*
190  * print in standout mode
191  */
192 int
193 SOprintf (char *fmt, ...)
194 {
195     va_list ap;
196
197     read_termcap ();
198     if (!(SO && SE))
199         return NOTOK;
200
201     tputs (SO, 1, outc);
202
203     va_start(ap, fmt);
204     vprintf (fmt, ap);
205     va_end(ap);
206
207     tputs (SE, 1, outc);
208
209     return OK;
210 }
211
212 /*
213  * Is this a hardcopy terminal?
214  */
215
216 int
217 sc_hardcopy(void)
218 {
219     read_termcap();
220     return HC;
221 }
222