Changed msg_style and msg_delim to be file static to m_getfld.c
[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 static long speedcode;
43
44 static int initLI = 0;
45 static int initCO = 0;
46
47 static int HC = 0;       /* are we on a hardcopy terminal?        */
48 static int LI = 40;      /* number of lines                       */
49 static int CO = 80;      /* number of colums                      */
50 static char *CL = NULL;  /* termcap string to clear screen        */
51 static char *SE = NULL;  /* termcap string to end standout mode   */
52 static char *SO = NULL;  /* termcap string to begin standout mode */
53
54 static char termcap[TXTSIZ];
55
56
57 static void
58 read_termcap(void)
59 {
60     char *cp;
61     char *term;
62
63 #ifndef TGETENT_ACCEPTS_NULL
64     char termbuf[TXTSIZ];
65 #endif
66
67     struct termios tio;
68     static int inited = 0;
69
70     if (inited++)
71         return;
72
73     if (!(term = getenv ("TERM")))
74         return;
75
76 /*
77  * If possible, we let tgetent allocate its own termcap buffer
78  */
79 #ifdef TGETENT_ACCEPTS_NULL
80     if (tgetent (NULL, term) != TGETENT_SUCCESS)
81         return;
82 #else
83     if (tgetent (termbuf, term) != TGETENT_SUCCESS)
84         return;
85 #endif
86
87     speedcode = cfgetospeed(&tio);
88
89     HC = tgetflag ("hc");
90
91     if (!initCO && (CO = tgetnum ("co")) <= 0)
92         CO = 80;
93     if (!initLI && (LI = tgetnum ("li")) <= 0)
94         LI = 24;
95
96     cp = termcap;
97     CL = tgetstr ("cl", &cp);
98     if (tgetnum ("sg") <= 0) {
99         SE = tgetstr ("se", &cp);
100         SO = tgetstr ("so", &cp);
101     }
102 }
103
104
105 int
106 sc_width (void)
107 {
108 #ifdef TIOCGWINSZ
109     struct winsize win;
110     int width;
111
112     if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
113             && (width = win.ws_col) > 0) {
114         CO = width;
115         initCO++;
116     } else
117 #endif /* TIOCGWINSZ */
118         read_termcap();
119
120     return CO;
121 }
122
123
124 int
125 sc_length (void)
126 {
127 #ifdef TIOCGWINSZ
128     struct winsize win;
129
130     if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
131             && (LI = win.ws_row) > 0)
132         initLI++;
133     else
134 #endif /* TIOCGWINSZ */
135         read_termcap();
136
137     return LI;
138 }
139
140
141 static int
142 outc (int c)
143 {
144     return putchar(c);
145 }
146
147
148 void
149 clear_screen (void)
150 {
151     read_termcap ();
152
153     if (CL && speedcode)
154         tputs (CL, LI, outc);
155     else {
156         printf ("\f");
157         if (speedcode)
158             printf ("\200");
159     }
160
161     fflush (stdout);
162 }
163
164
165 /*
166  * print in standout mode
167  */
168 int
169 SOprintf (char *fmt, ...)
170 {
171     va_list ap;
172
173     read_termcap ();
174     if (!(SO && SE))
175         return NOTOK;
176
177     tputs (SO, 1, outc);
178
179     va_start(ap, fmt);
180     vprintf (fmt, ap);
181     va_end(ap);
182
183     tputs (SE, 1, outc);
184
185     return OK;
186 }
187
188 /*
189  * Is this a hardcopy terminal?
190  */
191
192 int
193 sc_hardcopy(void)
194 {
195     read_termcap();
196     return HC;
197 }
198