]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/reset_cmd.c
ncurses 6.3 - patch 20211030
[ncurses.git] / progs / reset_cmd.c
1 /****************************************************************************
2  * Copyright 2019-2020,2021 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 is 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.28 2021/10/02 18:08:44 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 GCC_NORETURN 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 = safe_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, int noset)
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     if (!noset) {
333         SET_TTY(fd, tty_settings);
334     }
335 }
336
337 /*
338  * Returns a "good" value for the erase character.  This is loosely based on
339  * the BSD4.4 logic.
340  */
341 static int
342 default_erase(void)
343 {
344     int result;
345
346     if (over_strike
347         && VALID_STRING(key_backspace)
348         && strlen(key_backspace) == 1) {
349         result = key_backspace[0];
350     } else {
351         result = CERASE;
352     }
353
354     return result;
355 }
356
357 /*
358  * Update the values of the erase, interrupt, and kill characters in the TTY
359  * parameter.
360  *
361  * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
362  * characters if they're unset, or if we specify them as options.  This differs
363  * from BSD 4.4 tset, which always sets erase.
364  */
365 void
366 set_control_chars(TTY * tty_settings, int my_erase, int my_intr, int my_kill)
367 {
368 #if defined(EXP_WIN32_DRIVER)
369     /* noop */
370     (void) tty_settings;
371     (void) my_erase;
372     (void) my_intr;
373     (void) my_kill;
374 #else
375     if (DISABLED(tty_settings->c_cc[VERASE]) || my_erase >= 0) {
376         tty_settings->c_cc[VERASE] = UChar((my_erase >= 0)
377                                            ? my_erase
378                                            : default_erase());
379     }
380
381     if (DISABLED(tty_settings->c_cc[VINTR]) || my_intr >= 0) {
382         tty_settings->c_cc[VINTR] = UChar((my_intr >= 0)
383                                           ? my_intr
384                                           : CINTR);
385     }
386
387     if (DISABLED(tty_settings->c_cc[VKILL]) || my_kill >= 0) {
388         tty_settings->c_cc[VKILL] = UChar((my_kill >= 0)
389                                           ? my_kill
390                                           : CKILL);
391     }
392 #endif
393 }
394
395 /*
396  * Set up various conversions in the TTY parameter, including parity, tabs,
397  * returns, echo, and case, according to the termcap entry.
398  */
399 void
400 set_conversions(TTY * tty_settings)
401 {
402 #if defined(EXP_WIN32_DRIVER)
403     /* FIXME */
404 #else
405 #ifdef ONLCR
406     tty_settings->c_oflag |= ONLCR;
407 #endif
408     tty_settings->c_iflag |= ICRNL;
409     tty_settings->c_lflag |= ECHO;
410 #ifdef OXTABS
411     tty_settings->c_oflag |= OXTABS;
412 #endif /* OXTABS */
413
414     /* test used to be tgetflag("NL") */
415     if (VALID_STRING(newline) && newline[0] == '\n' && !newline[1]) {
416         /* Newline, not linefeed. */
417 #ifdef ONLCR
418         tty_settings->c_oflag &= ~((unsigned) ONLCR);
419 #endif
420         tty_settings->c_iflag &= ~((unsigned) ICRNL);
421     }
422 #ifdef OXTABS
423     /* test used to be tgetflag("pt") */
424     if (VALID_STRING(set_tab) && VALID_STRING(clear_all_tabs))
425         tty_settings->c_oflag &= ~OXTABS;
426 #endif /* OXTABS */
427     tty_settings->c_lflag |= (ECHOE | ECHOK);
428 #endif
429 }
430
431 static bool
432 sent_string(const char *s)
433 {
434     bool sent = FALSE;
435     if (VALID_STRING(s)) {
436         tputs(s, 0, out_char);
437         sent = TRUE;
438     }
439     return sent;
440 }
441
442 static bool
443 to_left_margin(void)
444 {
445     if (VALID_STRING(carriage_return)) {
446         sent_string(carriage_return);
447     } else {
448         out_char('\r');
449     }
450     return TRUE;
451 }
452
453 /*
454  * Set the hardware tabs on the terminal, using the 'ct' (clear all tabs),
455  * 'st' (set one tab) and 'ch' (horizontal cursor addressing) capabilities.
456  * This is done before 'if' and 'is', so they can recover in case of error.
457  *
458  * Return TRUE if we set any tab stops, FALSE if not.
459  */
460 static bool
461 reset_tabstops(int wide)
462 {
463     if ((init_tabs != 8)
464         && VALID_NUMERIC(init_tabs)
465         && VALID_STRING(set_tab)
466         && VALID_STRING(clear_all_tabs)) {
467         int c;
468
469         to_left_margin();
470         tputs(clear_all_tabs, 0, out_char);
471         if (init_tabs > 1) {
472             if (init_tabs > wide)
473                 init_tabs = (short) wide;
474             for (c = init_tabs; c < wide; c += init_tabs) {
475                 fprintf(my_file, "%*s", init_tabs, " ");
476                 tputs(set_tab, 0, out_char);
477             }
478             to_left_margin();
479         }
480         return (TRUE);
481     }
482     return (FALSE);
483 }
484
485 /* Output startup string. */
486 bool
487 send_init_strings(int fd GCC_UNUSED, TTY * old_settings)
488 {
489     int i;
490     bool need_flush = FALSE;
491
492     (void) old_settings;
493 #ifdef TAB3
494     if (old_settings != 0 &&
495         old_settings->c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
496         old_settings->c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
497         SET_TTY(fd, old_settings);
498     }
499 #endif
500     if (use_reset || use_init) {
501         if (VALID_STRING(init_prog)) {
502             IGNORE_RC(system(init_prog));
503         }
504
505         need_flush |= sent_string((use_reset && (reset_1string != 0))
506                                   ? reset_1string
507                                   : init_1string);
508
509         need_flush |= sent_string((use_reset && (reset_2string != 0))
510                                   ? reset_2string
511                                   : init_2string);
512
513         if (VALID_STRING(clear_margins)) {
514             need_flush |= sent_string(clear_margins);
515         } else
516 #if defined(set_lr_margin)
517         if (VALID_STRING(set_lr_margin)) {
518             need_flush |= sent_string(TIPARM_2(set_lr_margin, 0, columns - 1));
519         } else
520 #endif
521 #if defined(set_left_margin_parm) && defined(set_right_margin_parm)
522             if (VALID_STRING(set_left_margin_parm)
523                 && VALID_STRING(set_right_margin_parm)) {
524             need_flush |= sent_string(TIPARM_1(set_left_margin_parm, 0));
525             need_flush |= sent_string(TIPARM_1(set_right_margin_parm,
526                                                columns - 1));
527         } else
528 #endif
529             if (VALID_STRING(set_left_margin)
530                 && VALID_STRING(set_right_margin)) {
531             need_flush |= to_left_margin();
532             need_flush |= sent_string(set_left_margin);
533             if (VALID_STRING(parm_right_cursor)) {
534                 need_flush |= sent_string(TIPARM_1(parm_right_cursor,
535                                                    columns - 1));
536             } else {
537                 for (i = 0; i < columns - 1; i++) {
538                     out_char(' ');
539                     need_flush = TRUE;
540                 }
541             }
542             need_flush |= sent_string(set_right_margin);
543             need_flush |= to_left_margin();
544         }
545
546         need_flush |= reset_tabstops(columns);
547
548         need_flush |= cat_file((use_reset && reset_file) ? reset_file : init_file);
549
550         need_flush |= sent_string((use_reset && (reset_3string != 0))
551                                   ? reset_3string
552                                   : init_3string);
553     }
554
555     return need_flush;
556 }
557
558 /*
559  * Tell the user if a control key has been changed from the default value.
560  */
561 static void
562 show_tty_change(TTY * old_settings,
563                 TTY * new_settings,
564                 const char *name,
565                 int which,
566                 unsigned def)
567 {
568     unsigned older = 0, newer = 0;
569     char *p;
570
571 #if defined(EXP_WIN32_DRIVER)
572     /* noop */
573     (void) old_settings;
574     (void) new_settings;
575     (void) name;
576     (void) which;
577     (void) def;
578 #else
579     newer = new_settings->c_cc[which];
580     older = old_settings->c_cc[which];
581
582     if (older == newer && older == def)
583         return;
584 #endif
585     (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
586
587     if (DISABLED(newer)) {
588         (void) fprintf(stderr, "undef.\n");
589         /*
590          * Check 'delete' before 'backspace', since the key_backspace value
591          * is ambiguous.
592          */
593     } else if (newer == 0177) {
594         (void) fprintf(stderr, "delete.\n");
595     } else if ((p = key_backspace) != 0
596                && newer == (unsigned char) p[0]
597                && p[1] == '\0') {
598         (void) fprintf(stderr, "backspace.\n");
599     } else if (newer < 040) {
600         newer ^= 0100;
601         (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
602     } else
603         (void) fprintf(stderr, "%c.\n", UChar(newer));
604 }
605
606 /**************************************************************************
607  * Miscellaneous.
608  **************************************************************************/
609
610 void
611 reset_start(FILE *fp, bool is_reset, bool is_init)
612 {
613     my_file = fp;
614     use_reset = is_reset;
615     use_init = is_init;
616 }
617
618 void
619 reset_flush(void)
620 {
621     if (my_file != 0)
622         fflush(my_file);
623 }
624
625 void
626 print_tty_chars(TTY * old_settings, TTY * new_settings)
627 {
628 #if defined(EXP_WIN32_DRIVER)
629     /* noop */
630 #else
631     show_tty_change(old_settings, new_settings, "Erase", VERASE, CERASE);
632     show_tty_change(old_settings, new_settings, "Kill", VKILL, CKILL);
633     show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR);
634 #endif
635 }
636
637 #if HAVE_SIZECHANGE
638 /*
639  * Set window size if not set already, but update our copy of the values if the
640  * size was set.
641  */
642 void
643 set_window_size(int fd, short *high, short *wide)
644 {
645     STRUCT_WINSIZE win;
646     (void) ioctl(fd, IOCTL_GET_WINSIZE, &win);
647     if (WINSIZE_ROWS(win) == 0 &&
648         WINSIZE_COLS(win) == 0) {
649         if (*high > 0 && *wide > 0) {
650             WINSIZE_ROWS(win) = (unsigned short) *high;
651             WINSIZE_COLS(win) = (unsigned short) *wide;
652             (void) ioctl(fd, IOCTL_SET_WINSIZE, &win);
653         }
654     } else if (WINSIZE_ROWS(win) > 0 &&
655                WINSIZE_COLS(win) > 0) {
656         *high = (short) WINSIZE_ROWS(win);
657         *wide = (short) WINSIZE_COLS(win);
658     }
659 }
660 #endif