1 /*
2      ATP QWK MAIL READER FOR READING AND REPLYING TO QWK MAIL PACKETS.
3      Copyright (C) 1992, 1993, 1997  Thomas McWilliams
4      Copyright (C) 1990  Rene Cougnenc
5 
6      This program is free software; you can redistribute it and/or modify
7      it under the terms of the GNU General Public License as published by
8      the Free Software Foundation; either version 2, or (at your option)
9      any later version.
10 
11      This program is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14      GNU General Public License for more details.
15 
16      You should have received a copy of the GNU General Public License
17      along with this program; if not, write to the Free Software
18      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 /*
22 ansi.c
23 */
24 
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include "reader.h"
29 #include "readlib.h"
30 #include "ansi.h"
31 
32 /* define the "escape" character */
33 
34 #undef ESC
35 static const char ESC = (char)27 ;
36 
37 #ifdef HAVE_LIBTERMCAP
38 #if 0 && defined(NEED_TCVARS)  /* not needed at present */
39 char PC, *UP, *BC;
40 short ospeed = 0;
41 /* this is a place holder for alignment purposes only */
42 short atp_tc_dumb = 0;
43 #endif
44 
45 /*
46  * outc, wrapper for use by tputs().
47  */
48 static int
outc(int c)49 outc(int c)
50 {
51     int r;
52     r = putchar(c);
53     return r;
54 }
55 
56 #define tc_out(s) tputs( s, 1, outc )
57 #endif
58 
59 /*
60  * graph_togl - visually check if display supports VT102 line graphics.
61  */
62 atp_BOOL_T
graph_togl(atp_BOOL_T flag)63 graph_togl(atp_BOOL_T flag)
64 {
65     if (flag) {
66 	flag = FALSE;
67 	printf("\n\t\t +------------------------------+\n");
68 	printf("\t\t |                              |\n");
69 	printf("\t\t |  VT102 graphics are now OFF  |\n");
70 	printf("\t\t |                              |\n");
71 	printf("\t\t +------------------------------+\n\n");
72     } else {
73 	flag = TRUE;
74 	printf("\n\016\t\t lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk\n");
75 	printf("\t\t xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax\n");
76 	printf("\t\t xaa\017 VT102 graphics are now ON\016 ax\n");
77 	printf("\t\t xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax\n");
78 	printf("\t\t mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj\n\n\017");
79     }
80     return flag;
81 }
82 
83 /* termcap support provided for cls(), clear(), high(), blink(), reverse() */
84 
85 /*
86  * cls - clear screen and home cursor.
87  */
88 void
cls(void)89 cls(void)
90 {
91     if (ansi)
92 	printf("%c[2J%c[H", ESC, ESC);
93 #ifdef HAVE_LIBTERMCAP
94     else if (clear_scr)
95 	tc_out(clear_scr);
96     /* tputs (clear_scr, 1, putchar ) ; */
97 #endif
98 #if defined(DJGPP) || defined(__TURBOC__)
99     else {
100 	gotoxy(1, 1);
101 	clrscr();
102     }
103 #endif
104 }
105 
106 /*
107  * clear - reset ansi screen color and highlighting attributes.
108  */
109 void
clear(void)110 clear(void)
111 {
112     if (ansi)
113 	printf("%c[0m", ESC);
114 #ifdef HAVE_LIBTERMCAP
115     else if (clear_attr)
116 	tc_out(clear_attr);
117 #endif
118 }
119 
120 /*
121  * high - ansi highlighting/boldface.
122  */
123 void
high(void)124 high(void)
125 {				/* HigLitht  (or BoldFace )  */
126     if (ansi)
127 	printf("%c[1m", ESC);
128 #ifdef HAVE_LIBTERMCAP
129     else if (bold_on)
130 	tc_out(bold_on);
131 #endif
132 }
133 
134 /*
135  * blink - invoke ansi blink mode.
136  */
137 void
blink(void)138 blink(void)
139 {
140     if (ansi)
141 	printf("%c[5m", ESC);
142 #ifdef HAVE_LIBTERMCAP
143     else if (blink_on)
144 	tc_out(blink_on);
145 #endif
146 }
147 
148 /*
149  * deleol - ansi del to end of line.
150  */
151 void
deleol(void)152 deleol(void)
153 {
154     if (ansi)
155 	printf("%c[K", ESC);
156 #ifdef HAVE_LIBTERMCAP
157     else if (del_eol)
158 	tc_out(del_eol);
159 #endif
160 #if defined(DJGPP) || defined(__TURBOC__)
161     else
162 	clreol();
163 #endif
164 }
165 
166 #ifdef ATPUNUSED
167 void
reverse()168 reverse()
169 {				/* Reverse video mode         */
170     if (ansi)
171 	printf("%c[7m", ESC);
172 #ifdef HAVE_LIBTERMCAP
173     else if (rev_on)
174 	tc_out(rev_on);
175 #endif
176 }
177 
178 /*------------- Foreground colors ---------------------*/
179 void
black()180 black()
181 {
182     if (color && ansi)
183 	printf("%c[30m", ESC);
184 }
185 #endif
186 /*
187  * red - ansi color.
188  */
189 void
red(void)190 red(void)
191 {
192     if (color && ansi)
193 	printf("%c[31m", ESC);
194 }
195 
196 /*
197  * green - ansi color.
198  */
199 void
green(void)200 green(void)
201 {
202     if (color && ansi)
203 	printf("%c[32m", ESC);
204 }
205 
206 /*
207  * yellow - ansi color.
208  */
209 void
yellow(void)210 yellow(void)
211 {
212     if (color && ansi)
213 	printf("%c[33m", ESC);
214 }
215 
216 /*
217  * blue - ansi color.
218  */
219 void
blue(void)220 blue(void)
221 {
222     if (color && ansi)
223 	printf("%c[34m", ESC);
224 }
225 
226 /*
227  * magenta - ansi color.
228  */
229 void
magenta(void)230 magenta(void)
231 {
232     if (color && ansi)
233 	printf("%c[35m", ESC);
234 }
235 
236 /*
237  * cyan - ansi color.
238  */
239 void
cyan(void)240 cyan(void)
241 {
242     if (color && ansi)
243 	printf("%c[36m", ESC);
244 }
245 
246 /*
247  * white - ansi color.
248  */
249 void
white()250 white()
251 {
252     if (color && ansi)
253 	printf("%c[37m", ESC);
254 }
255 
256 /*------------- BackGround colors ---------------------*/
257 
258 /*
259  * black - ansi background color.
260  */
261 void
bblack()262 bblack()
263 {
264     if (color && ansi)
265 	printf("%c[40m", ESC);
266 }
267 
268 #ifdef ATPUNUSED
269 void
bred()270 bred()
271 {
272     if (color && ansi)
273 	printf("%c[41m", ESC);
274 }
275 void
bgreen()276 bgreen()
277 {
278     if (color && ansi)
279 	printf("%c[42m", ESC);
280 }
281 void
byellow()282 byellow()
283 {
284     if (color && ansi)
285 	printf("%c[43m", ESC);
286 }
287 void
bblue()288 bblue()
289 {
290     if (color && ansi)
291 	printf("%c[44m", ESC);
292 }
293 void
bmagenta()294 bmagenta()
295 {
296     if (color && ansi)
297 	printf("%c[45m", ESC);
298 }
299 void
bcyan()300 bcyan()
301 {
302     if (color && ansi)
303 	printf("%c[46m", ESC);
304 }
305 void
bwhite()306 bwhite()
307 {
308     if (color && ansi)
309 	printf("%c[47m", ESC);
310 }
311 #endif
312 
313 #ifdef ATPUNUSED
314 /*----------- Cursor position ---------------------------*/
315 
316 void
locate(x,y)317 locate(x, y)
318 int x, y;
319 {
320     if (ansi)
321 	printf("%c[%d;%dH", ESC, y, x);
322 }
323 
324 void
up(nb)325 up(nb)				/* Cursor Up default 1 */
326 int nb;
327 {
328     if (ansi) {
329 	if (!nb)
330 	    nb = 1;
331 	printf("%c[%dA", ESC, nb);
332     }
333 }
334 
335 void
dn(nb)336 dn(nb)				/* Cursor down default 1 */
337 int nb;
338 {
339     if (ansi) {
340 	if (!nb)
341 	    nb = 1;
342 	printf("%c[%dB", ESC, nb);
343     }
344 }
345 
346 void
right(nb)347 right(nb)			/* Cursor right default 1 */
348 int nb;
349 {
350     if (ansi) {
351 	if (!nb)
352 	    nb = 1;
353 	printf("%c[%dC", ESC, nb);
354     }
355 }
356 
357 void
left(nb)358 left(nb)			/* Cursor left default 1 */
359 int nb;
360 {
361     if (ansi) {
362 	if (!nb)
363 	    nb = 1;
364 	printf("%c[%dD", ESC, nb);
365     }
366 }
367 #endif
368 
369 #ifdef HAVE_LIBTERMCAP
370 
371 #define o2bSIZE 200
372 static int o2b_ct;
373 static char *o2b_p;
374 static char o2b_bf[o2bSIZE + 1];
375 
376 /*
377  * out_2_buf, expand termcap attribute in buffer.
378  * passed to tputs() to expand a termcap attribute string into a buffer
379  */
380 static int
out_2_buf(int n)381 out_2_buf(int n)
382 {
383     if (o2bSIZE <= o2b_ct) {
384 	o2b_bf[o2bSIZE] = NUL_CHAR;
385 	n = EOF;
386     } else {
387 	*o2b_p = (char) n;
388 	o2b_ct++;
389 	o2b_p++;
390 	*o2b_p = NUL_CHAR;
391     }
392     return n;
393 }
394 
395 /*
396  * tputs_ptr - returns a pointer to the expanded attribute.
397  */
398 char *
tputs_ptr(char * attr)399 tputs_ptr(char *attr)
400 {
401     o2b_bf[0] = NUL_CHAR;
402     o2b_p = o2b_bf;
403     o2b_ct = 0;
404     /*@-strictops */
405     if (attr != NULL && *attr != NUL_CHAR ) /*@=strictops */
406 	tputs(attr, 1, out_2_buf);
407     return (char *) o2b_bf;
408 }
409 
410 /*
411  * tputs_cpy - copies a termcap attribute to a string buffer.
412  */
413 int
tputs_cpy(char * bfr,char * attr)414 tputs_cpy(char *bfr, char *attr)
415 {
416     strcpy(bfr, tputs_ptr(attr));
417     return strlen(o2b_bf);
418 }
419 
420 /*
421  * tputs_cat - strcats a termcap attribute to a string buffer.
422  */
423 int
tputs_cat(char * bfr,char * attr)424 tputs_cat(char *bfr, char *attr)
425 {
426     strcat(bfr, tputs_ptr(attr));
427     return strlen(o2b_bf);
428 }
429 
430 /* tputs_dup not currently used in ATP */
431 #if 0
432 /*
433 ** tputs_dup - strdups a termcap attribute.
434  */
435 char *
436 tputs_dup(char *attr)
437 {
438     char *t;
439     t = strdup(tputs_ptr(attr));
440     return t;
441 }
442 #endif
443 #endif
444 
445 /*-----------------------------------------------------------------------*/
446 /*         Below we have the signal related functions.                   */
447 
448 
449 
450 #ifdef HAVE_SIGACTION
451 static struct sigaction sdefault, signore, stpfind;
452 #if defined(USE_FORK)
453 static struct sigaction new_sinter;
454 #endif
455 #if defined(TIOCGWINSZ) && defined(SIGWINCH)
456 static struct sigaction new_swinch;
457 #endif
458 #endif
459 
460 #if defined(TIOCGWINSZ) && defined(SIGWINCH)
461 
462 /*
463  * sig_chwin - set-up handler for SIGWINCH.
464  */
465 void
sig_chwin(void)466 sig_chwin(void)
467 {
468 #if defined(HAVE_SIGACTION)
469     sigaction(SIGWINCH, &new_swinch, NULL);
470 #else
471     signal(SIGWINCH, getwinders);
472 #endif
473 }
474 
475 #endif				/* TIOCGWINSZ */
476 
477 
478 
479 /*
480  * sig_init - initialize sigaction structures.
481  */
482 #ifdef HAVE_SIGACTION
483 void
sig_init(void)484 sig_init(void)
485 {
486     sigset_t sigst;
487     sigemptyset(&sigst);
488 #if defined(TIOCGWINSZ) && defined(SIGWINCH)
489     new_swinch.sa_handler = getwinders;
490     new_swinch.sa_mask = sigst;
491     new_swinch.sa_flags = 0;
492 #endif
493 #if defined(USE_FORK)
494     new_sinter.sa_handler = kill_child;
495     new_sinter.sa_mask = sigst;
496     new_sinter.sa_flags = 0;
497 #endif
498     sdefault.sa_handler = SIG_DFL;
499     signore.sa_handler = SIG_IGN;
500     stpfind.sa_handler = stopfind;
501     sdefault.sa_mask = signore.sa_mask = stpfind.sa_mask = sigst;
502     sdefault.sa_flags = signore.sa_flags = stpfind.sa_flags = 0;
503 }
504 
505 #endif				/* HAVE_SIGACTION */
506 
507 
508 
509 #if defined(USE_FORK) && defined(SIGINT)
510 
511 /*
512  * sig_chint - set-up handler for SIGINT.
513  */
514 void
sig_chint(void)515 sig_chint(void)
516 {
517 #if defined(HAVE_SIGACTION)
518     sigaction(SIGINT, &new_sinter, NULL);
519 #else
520     signal(SIGINT, kill_child);
521 #endif
522 }
523 
524 #endif
525 
526 
527 /*
528  * sig_stopfind - set-up SIGINT handler for findtxt().
529  */
530 void
sig_stopfind(void)531 sig_stopfind(void)
532 {
533 #if defined(SIGINT)
534 #if defined(HAVE_SIGACTION)
535     sigaction(SIGINT, &stpfind, NULL);
536 #else
537     signal(SIGINT, stopfind);
538 #endif
539 #endif
540 }
541 
542 
543 #if 0
544 void
545 sig_default(int signo)
546 {
547 #ifdef HAVE_SIGACTION
548     sigaction(signo, &sdefault, NULL);
549 #else
550     signal(signo, SIG_DFL);
551 #endif
552 }
553 #endif
554 
555 /*
556  * sig_ignore - ignore a signal.
557  */
558 void
sig_ignore(int signo)559 sig_ignore(int signo)
560 {
561 #ifdef HAVE_SIGACTION
562     sigaction(signo, &signore, NULL);
563 #else
564     signal(signo, SIG_IGN);
565 #endif
566 }
567 
568 /*-----------------------------------------------------------------------*/
569 /*-- ALL CODE BELOW IS UNUSED BY ATP ------------------------------------*/
570 
571 #if 0 && defined(LISTDIR)
572 #include "editline/editline.h"
573 extern char *Screen;
574 extern size_t ScreenSize;
575 
576 static void
577 list_dir(char *path)
578 {
579     char **av;
580     int ac = 5;
581 
582     ac = rl_list_possib((char *) path, (char ***) &av);
583     if (ac) {
584 	ScreenSize = SCREEN_INC;
585 	Screen = NEW(char, ScreenSize);
586 	columns(ac, av);
587 	TTYflush();
588 	DISPOSE(Screen);
589 	sleep(3);
590 	while (--ac >= 0)
591 	    free(av[ac]);
592 	free(av);
593     }
594 }
595 #endif
596