ncurses 6.2 - patch 20201024
[ncurses.git] / progs / reset_cmd.c
1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 2016,2017 Free Software Foundation, Inc.                       *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *  Author: Thomas E. Dickey                                                *
32  ****************************************************************************/
33
34 #include <reset_cmd.h>
35 #include <tty_settings.h>
36
37 #include <errno.h>
38 #include <stdio.h>
39 #include <fcntl.h>
40
41 #if HAVE_SIZECHANGE
42 # if !defined(sun) || !TERMIOS
43 #  if HAVE_SYS_IOCTL_H
44 #   include <sys/ioctl.h>
45 #  endif
46 # endif
47 #endif
48
49 #if NEED_PTEM_H
50 /* they neglected to define struct winsize in termios.h -- it's only
51    in termio.h  */
52 #include <sys/stream.h>
53 #include <sys/ptem.h>
54 #endif
55
56 MODULE_ID("$Id: reset_cmd.c,v 1.23 2020/09/05 22:54:47 tom Exp $")
57
58 /*
59  * SCO defines TIOCGSIZE and the corresponding struct.  Other systems (SunOS,
60  * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
61  */
62 #ifdef TIOCGSIZE
63 # define IOCTL_GET_WINSIZE TIOCGSIZE
64 # define IOCTL_SET_WINSIZE TIOCSSIZE
65 # define STRUCT_WINSIZE struct ttysize
66 # define WINSIZE_ROWS(n) n.ts_lines
67 # define WINSIZE_COLS(n) n.ts_cols
68 #else
69 # ifdef TIOCGWINSZ
70 #  define IOCTL_GET_WINSIZE TIOCGWINSZ
71 #  define IOCTL_SET_WINSIZE TIOCSWINSZ
72 #  define STRUCT_WINSIZE struct winsize
73 #  define WINSIZE_ROWS(n) n.ws_row
74 #  define WINSIZE_COLS(n) n.ws_col
75 # endif
76 #endif
77
78 static FILE *my_file;
79
80 static bool use_reset = FALSE;  /* invoked as reset */
81 static bool use_init = FALSE;   /* invoked as init */
82
83 static void
84 failed(const char *msg)
85 {
86     int code = errno;
87
88     (void) fprintf(stderr, "%s: %s: %s\n", _nc_progname, msg, strerror(code));
89     restore_tty_settings();
90     (void) fprintf(my_file, "\n");
91     fflush(my_file);
92     ExitProgram(ErrSystem(code));
93     /* NOTREACHED */
94 }
95
96 static bool
97 cat_file(char *file)
98 {
99     FILE *fp;
100     size_t nr;
101     char buf[BUFSIZ];
102     bool sent = FALSE;
103
104     if (file != 0) {
105         if ((fp = fopen(file, "r")) == 0)
106             failed(file);
107
108         while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0) {
109             if (fwrite(buf, sizeof(char), nr, my_file) != nr) {
110                 failed(file);
111             }
112             sent = TRUE;
113         }
114         fclose(fp);
115     }
116     return sent;
117 }
118
119 static int
120 out_char(int c)
121 {
122     return putc(c, my_file);
123 }
124
125 /**************************************************************************
126  * Mode-setting logic
127  **************************************************************************/
128
129 /* some BSD systems have these built in, some systems are missing
130  * one or more definitions. The safest solution is to override unless the
131  * commonly-altered ones are defined.
132  */
133 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
134 #undef CEOF
135 #undef CERASE
136 #undef CINTR
137 #undef CKILL
138 #undef CLNEXT
139 #undef CRPRNT
140 #undef CQUIT
141 #undef CSTART
142 #undef CSTOP
143 #undef CSUSP
144 #endif
145
146 /* control-character defaults */
147 #ifndef CEOF
148 #define CEOF    CTRL('D')
149 #endif
150 #ifndef CERASE
151 #define CERASE  CTRL('H')
152 #endif
153 #ifndef CINTR
154 #define CINTR   127             /* ^? */
155 #endif
156 #ifndef CKILL
157 #define CKILL   CTRL('U')
158 #endif
159 #ifndef CLNEXT
160 #define CLNEXT  CTRL('v')
161 #endif
162 #ifndef CRPRNT
163 #define CRPRNT  CTRL('r')
164 #endif
165 #ifndef CQUIT
166 #define CQUIT   CTRL('\\')
167 #endif
168 #ifndef CSTART
169 #define CSTART  CTRL('Q')
170 #endif
171 #ifndef CSTOP
172 #define CSTOP   CTRL('S')
173 #endif
174 #ifndef CSUSP
175 #define CSUSP   CTRL('Z')
176 #endif
177
178 #if defined(_POSIX_VDISABLE)
179 #define DISABLED(val)   (((_POSIX_VDISABLE != -1) \
180                        && ((val) == _POSIX_VDISABLE)) \
181                       || ((val) <= 0))
182 #else
183 #define DISABLED(val)   ((int)(val) <= 0)
184 #endif
185
186 #define CHK(val, dft)   (unsigned char) (DISABLED(val) ? dft : val)
187
188 #define reset_char(item, value) \
189     tty_settings->c_cc[item] = CHK(tty_settings->c_cc[item], value)
190
191 /*
192  * Reset the terminal mode bits to a sensible state.  Very useful after
193  * a child program dies in raw mode.
194  */
195 void
196 reset_tty_settings(int fd, TTY * tty_settings)
197 {
198     GET_TTY(fd, tty_settings);
199
200 #ifdef TERMIOS
201 #if defined(VDISCARD) && defined(CDISCARD)
202     reset_char(VDISCARD, CDISCARD);
203 #endif
204     reset_char(VEOF, CEOF);
205     reset_char(VERASE, CERASE);
206 #if defined(VFLUSH) && defined(CFLUSH)
207     reset_char(VFLUSH, CFLUSH);
208 #endif
209     reset_char(VINTR, CINTR);
210     reset_char(VKILL, CKILL);
211 #if defined(VLNEXT) && defined(CLNEXT)
212     reset_char(VLNEXT, CLNEXT);
213 #endif
214     reset_char(VQUIT, CQUIT);
215 #if defined(VREPRINT) && defined(CRPRNT)
216     reset_char(VREPRINT, CRPRNT);
217 #endif
218 #if defined(VSTART) && defined(CSTART)
219     reset_char(VSTART, CSTART);
220 #endif
221 #if defined(VSTOP) && defined(CSTOP)
222     reset_char(VSTOP, CSTOP);
223 #endif
224 #if defined(VSUSP) && defined(CSUSP)
225     reset_char(VSUSP, CSUSP);
226 #endif
227 #if defined(VWERASE) && defined(CWERASE)
228     reset_char(VWERASE, CWERASE);
229 #endif
230
231     tty_settings->c_iflag &= ~((unsigned) (IGNBRK
232                                            | PARMRK
233                                            | INPCK
234                                            | ISTRIP
235                                            | INLCR
236                                            | IGNCR
237 #ifdef IUCLC
238                                            | IUCLC
239 #endif
240 #ifdef IXANY
241                                            | IXANY
242 #endif
243                                            | IXOFF));
244
245     tty_settings->c_iflag |= (BRKINT
246                               | IGNPAR
247                               | ICRNL
248                               | IXON
249 #ifdef IMAXBEL
250                               | IMAXBEL
251 #endif
252         );
253
254     tty_settings->c_oflag &= ~((unsigned) (0
255 #ifdef OLCUC
256                                            | OLCUC
257 #endif
258 #ifdef OCRNL
259                                            | OCRNL
260 #endif
261 #ifdef ONOCR
262                                            | ONOCR
263 #endif
264 #ifdef ONLRET
265                                            | ONLRET
266 #endif
267 #ifdef OFILL
268                                            | OFILL
269 #endif
270 #ifdef OFDEL
271                                            | OFDEL
272 #endif
273 #ifdef NLDLY
274                                            | NLDLY
275 #endif
276 #ifdef CRDLY
277                                            | CRDLY
278 #endif
279 #ifdef TABDLY
280                                            | TABDLY
281 #endif
282 #ifdef BSDLY
283                                            | BSDLY
284 #endif
285 #ifdef VTDLY
286                                            | VTDLY
287 #endif
288 #ifdef FFDLY
289                                            | FFDLY
290 #endif
291                                ));
292
293     tty_settings->c_oflag |= (OPOST
294 #ifdef ONLCR
295                               | ONLCR
296 #endif
297         );
298
299     tty_settings->c_cflag &= ~((unsigned) (CSIZE
300                                            | CSTOPB
301                                            | PARENB
302                                            | PARODD
303                                            | CLOCAL));
304     tty_settings->c_cflag |= (CS8 | CREAD);
305     tty_settings->c_lflag &= ~((unsigned) (ECHONL
306                                            | NOFLSH
307 #ifdef TOSTOP
308                                            | TOSTOP
309 #endif
310 #ifdef ECHOPTR
311                                            | ECHOPRT
312 #endif
313 #ifdef XCASE
314                                            | XCASE
315 #endif
316                                ));
317
318     tty_settings->c_lflag |= (ISIG
319                               | ICANON
320                               | ECHO
321                               | ECHOE
322                               | ECHOK
323 #ifdef ECHOCTL
324                               | ECHOCTL
325 #endif
326 #ifdef ECHOKE
327                               | ECHOKE
328 #endif
329         );
330 #endif
331
332     SET_TTY(fd, tty_settings);
333 }
334
335 /*
336  * Returns a "good" value for the erase character.  This is loosely based on
337  * the BSD4.4 logic.
338  */
339 static int
340 default_erase(void)
341 {
342     int result;
343
344     if (over_strike
345         && VALID_STRING(key_backspace)
346         && strlen(key_backspace) == 1) {
347         result = key_backspace[0];
348     } else {
349         result = CERASE;
350     }
351
352     return result;
353 }
354
355 /*
356  * Update the values of the erase, interrupt, and kill characters in the TTY
357  * parameter.
358  *
359  * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
360  * characters if they're unset, or if we specify them as options.  This differs
361  * from BSD 4.4 tset, which always sets erase.
362  */
363 void
364 set_control_chars(TTY * tty_settings, int my_erase, int my_intr, int my_kill)
365 {
366 #if defined(EXP_WIN32_DRIVER)
367     /* noop */
368 #else
369     if (DISABLED(tty_settings->c_cc[VERASE]) || my_erase >= 0) {
370         tty_settings->c_cc[VERASE] = UChar((my_erase >= 0)
371                                            ? my_erase
372                                            : default_erase());
373     }
374
375     if (DISABLED(tty_settings->c_cc[VINTR]) || my_intr >= 0) {
376         tty_settings->c_cc[VINTR] = UChar((my_intr >= 0)
377                                           ? my_intr
378                                           : CINTR);
379     }
380
381     if (DISABLED(tty_settings->c_cc[VKILL]) || my_kill >= 0) {
382         tty_settings->c_cc[VKILL] = UChar((my_kill >= 0)
383                                           ? my_kill
384                                           : CKILL);
385     }
386 #endif
387 }
388
389 /*
390  * Set up various conversions in the TTY parameter, including parity, tabs,
391  * returns, echo, and case, according to the termcap entry.
392  */
393 void
394 set_conversions(TTY * tty_settings)
395 {
396 #if defined(EXP_WIN32_DRIVER)
397     /* FIXME */
398 #else
399 #ifdef ONLCR
400     tty_settings->c_oflag |= ONLCR;
401 #endif
402     tty_settings->c_iflag |= ICRNL;
403     tty_settings->c_lflag |= ECHO;
404 #ifdef OXTABS
405     tty_settings->c_oflag |= OXTABS;
406 #endif /* OXTABS */
407
408     /* test used to be tgetflag("NL") */
409     if (VALID_STRING(newline) && newline[0] == '\n' && !newline[1]) {
410         /* Newline, not linefeed. */
411 #ifdef ONLCR
412         tty_settings->c_oflag &= ~((unsigned) ONLCR);
413 #endif
414         tty_settings->c_iflag &= ~((unsigned) ICRNL);
415     }
416 #ifdef OXTABS
417     /* test used to be tgetflag("pt") */
418     if (VALID_STRING(set_tab) && VALID_STRING(clear_all_tabs))
419         tty_settings->c_oflag &= ~OXTABS;
420 #endif /* OXTABS */
421     tty_settings->c_lflag |= (ECHOE | ECHOK);
422 #endif
423 }
424
425 static bool
426 sent_string(const char *s)
427 {
428     bool sent = FALSE;
429     if (VALID_STRING(s)) {
430         tputs(s, 0, out_char);
431         sent = TRUE;
432     }
433     return sent;
434 }
435
436 static bool
437 to_left_margin(void)
438 {
439     if (VALID_STRING(carriage_return)) {
440         sent_string(carriage_return);
441     } else {
442         out_char('\r');
443     }
444     return TRUE;
445 }
446
447 /*
448  * Set the hardware tabs on the terminal, using the 'ct' (clear all tabs),
449  * 'st' (set one tab) and 'ch' (horizontal cursor addressing) capabilities.
450  * This is done before 'if' and 'is', so they can recover in case of error.
451  *
452  * Return TRUE if we set any tab stops, FALSE if not.
453  */
454 static bool
455 reset_tabstops(int wide)
456 {
457     if ((init_tabs != 8)
458         && VALID_NUMERIC(init_tabs)
459         && VALID_STRING(set_tab)
460         && VALID_STRING(clear_all_tabs)) {
461         int c;
462
463         to_left_margin();
464         tputs(clear_all_tabs, 0, out_char);
465         if (init_tabs > 1) {
466             if (init_tabs > wide)
467                 init_tabs = (short) wide;
468             for (c = init_tabs; c < wide; c += init_tabs) {
469                 fprintf(my_file, "%*s", init_tabs, " ");
470                 tputs(set_tab, 0, out_char);
471             }
472             to_left_margin();
473         }
474         return (TRUE);
475     }
476     return (FALSE);
477 }
478
479 /* Output startup string. */
480 bool
481 send_init_strings(int fd GCC_UNUSED, TTY * old_settings)
482 {
483     int i;
484     bool need_flush = FALSE;
485
486     (void) old_settings;
487 #ifdef TAB3
488     if (old_settings != 0 &&
489         old_settings->c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
490         old_settings->c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
491         SET_TTY(fd, old_settings);
492     }
493 #endif
494     if (use_reset || use_init) {
495         if (VALID_STRING(init_prog)) {
496             IGNORE_RC(system(init_prog));
497         }
498
499         need_flush |= sent_string((use_reset && (reset_1string != 0))
500                                   ? reset_1string
501                                   : init_1string);
502
503         need_flush |= sent_string((use_reset && (reset_2string != 0))
504                                   ? reset_2string
505                                   : init_2string);
506
507         if (VALID_STRING(clear_margins)) {
508             need_flush |= sent_string(clear_margins);
509         } else
510 #if defined(set_lr_margin)
511         if (VALID_STRING(set_lr_margin)) {
512             need_flush |= sent_string(TIPARM_2(set_lr_margin, 0, columns - 1));
513         } else
514 #endif
515 #if defined(set_left_margin_parm) && defined(set_right_margin_parm)
516             if (VALID_STRING(set_left_margin_parm)
517                 && VALID_STRING(set_right_margin_parm)) {
518             need_flush |= sent_string(TIPARM_1(set_left_margin_parm, 0));
519             need_flush |= sent_string(TIPARM_1(set_right_margin_parm,
520                                                columns - 1));
521         } else
522 #endif
523             if (VALID_STRING(set_left_margin)
524                 && VALID_STRING(set_right_margin)) {
525             need_flush |= to_left_margin();
526             need_flush |= sent_string(set_left_margin);
527             if (VALID_STRING(parm_right_cursor)) {
528                 need_flush |= sent_string(TIPARM_1(parm_right_cursor,
529                                                    columns - 1));
530             } else {
531                 for (i = 0; i < columns - 1; i++) {
532                     out_char(' ');
533                     need_flush = TRUE;
534                 }
535             }
536             need_flush |= sent_string(set_right_margin);
537             need_flush |= to_left_margin();
538         }
539
540         need_flush |= reset_tabstops(columns);
541
542         need_flush |= cat_file((use_reset && reset_file) ? reset_file : init_file);
543
544         need_flush |= sent_string((use_reset && (reset_3string != 0))
545                                   ? reset_3string
546                                   : init_3string);
547     }
548
549     return need_flush;
550 }
551
552 /*
553  * Tell the user if a control key has been changed from the default value.
554  */
555 static void
556 show_tty_change(TTY * old_settings,
557                 TTY * new_settings,
558                 const char *name,
559                 int which,
560                 unsigned def)
561 {
562     unsigned older = 0, newer = 0;
563     char *p;
564
565 #if defined(EXP_WIN32_DRIVER)
566     /* noop */
567 #else
568     newer = new_settings->c_cc[which];
569     older = old_settings->c_cc[which];
570
571     if (older == newer && older == def)
572         return;
573 #endif
574     (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
575
576     if (DISABLED(newer)) {
577         (void) fprintf(stderr, "undef.\n");
578         /*
579          * Check 'delete' before 'backspace', since the key_backspace value
580          * is ambiguous.
581          */
582     } else if (newer == 0177) {
583         (void) fprintf(stderr, "delete.\n");
584     } else if ((p = key_backspace) != 0
585                && newer == (unsigned char) p[0]
586                && p[1] == '\0') {
587         (void) fprintf(stderr, "backspace.\n");
588     } else if (newer < 040) {
589         newer ^= 0100;
590         (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
591     } else
592         (void) fprintf(stderr, "%c.\n", UChar(newer));
593 }
594
595 /**************************************************************************
596  * Miscellaneous.
597  **************************************************************************/
598
599 void
600 reset_start(FILE *fp, bool is_reset, bool is_init)
601 {
602     my_file = fp;
603     use_reset = is_reset;
604     use_init = is_init;
605 }
606
607 void
608 reset_flush(void)
609 {
610     if (my_file != 0)
611         fflush(my_file);
612 }
613
614 void
615 print_tty_chars(TTY * old_settings, TTY * new_settings)
616 {
617 #if defined(EXP_WIN32_DRIVER)
618     /* noop */
619 #else
620     show_tty_change(old_settings, new_settings, "Erase", VERASE, CERASE);
621     show_tty_change(old_settings, new_settings, "Kill", VKILL, CKILL);
622     show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR);
623 #endif
624 }
625
626 #if HAVE_SIZECHANGE
627 /*
628  * Set window size if not set already, but update our copy of the values if the
629  * size was set.
630  */
631 void
632 set_window_size(int fd, short *high, short *wide)
633 {
634     STRUCT_WINSIZE win;
635     (void) ioctl(fd, IOCTL_GET_WINSIZE, &win);
636     if (WINSIZE_ROWS(win) == 0 &&
637         WINSIZE_COLS(win) == 0) {
638         if (*high > 0 && *wide > 0) {
639             WINSIZE_ROWS(win) = (unsigned short) *high;
640             WINSIZE_COLS(win) = (unsigned short) *wide;
641             (void) ioctl(fd, IOCTL_SET_WINSIZE, &win);
642         }
643     } else if (WINSIZE_ROWS(win) > 0 &&
644                WINSIZE_COLS(win) > 0) {
645         *high = (short) WINSIZE_ROWS(win);
646         *wide = (short) WINSIZE_COLS(win);
647     }
648 }
649 #endif