xref: /src/contrib/ncurses/progs/reset_cmd.c (revision 68ad2b0d7af2a3571c4abac9afa712f9b09b721c)
1 /****************************************************************************
2  * Copyright 2019-2024,2025 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.42 2025/11/22 21:38:31 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 #define set_flags(target, mask)    target |= mask
79 #define clear_flags(target, mask)  target &= ~((unsigned)(mask))
80 
81 static FILE *my_file;
82 
83 static bool use_reset = FALSE;	/* invoked as reset */
84 static bool use_init = FALSE;	/* invoked as init */
85 
86 static GCC_NORETURN void
failed(const char * msg)87 failed(const char *msg)
88 {
89     int code = errno;
90 
91     (void) fprintf(stderr, "%s: %s: %s\n", _nc_progname, msg, strerror(code));
92     restore_tty_settings();
93     (void) fprintf(my_file, "\n");
94     fflush(my_file);
95     ExitProgram(ErrSystem(code));
96     /* NOTREACHED */
97 }
98 
99 static bool
cat_file(char * file)100 cat_file(char *file)
101 {
102     FILE *fp;
103     size_t nr;
104     char buf[BUFSIZ];
105     bool sent = FALSE;
106 
107     if (file != NULL) {
108 	if ((fp = safe_fopen(file, "r")) == NULL)
109 	    failed(file);
110 
111 	while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0) {
112 	    if (fwrite(buf, sizeof(char), nr, my_file) != nr) {
113 		failed(file);
114 	    }
115 	    sent = TRUE;
116 	}
117 	fclose(fp);
118     }
119     return sent;
120 }
121 
122 static int
out_char(int c)123 out_char(int c)
124 {
125     return putc(c, my_file);
126 }
127 
128 /**************************************************************************
129  * Mode-setting logic
130  **************************************************************************/
131 
132 /* some BSD systems have these built in, some systems are missing
133  * one or more definitions. The safest solution is to override unless the
134  * commonly-altered ones are defined.
135  */
136 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
137 #undef CEOF
138 #undef CERASE
139 #undef CINTR
140 #undef CKILL
141 #undef CLNEXT
142 #undef CRPRNT
143 #undef CQUIT
144 #undef CSTART
145 #undef CSTOP
146 #undef CSUSP
147 #endif
148 
149 /* control-character defaults */
150 #ifndef CEOF
151 #define CEOF	CTRL('D')
152 #endif
153 #ifndef CERASE
154 #define CERASE	CTRL('H')
155 #endif
156 #ifndef CINTR
157 #define CINTR	127		/* ^? */
158 #endif
159 #ifndef CKILL
160 #define CKILL	CTRL('U')
161 #endif
162 #ifndef CLNEXT
163 #define CLNEXT  CTRL('v')
164 #endif
165 #ifndef CRPRNT
166 #define CRPRNT  CTRL('r')
167 #endif
168 #ifndef CQUIT
169 #define CQUIT	CTRL('\\')
170 #endif
171 #ifndef CSTART
172 #define CSTART	CTRL('Q')
173 #endif
174 #ifndef CSTOP
175 #define CSTOP	CTRL('S')
176 #endif
177 #ifndef CSUSP
178 #define CSUSP	CTRL('Z')
179 #endif
180 
181 #if defined(_POSIX_VDISABLE)
182 #define DISABLED(val)   (((_POSIX_VDISABLE != -1) \
183 		       && ((val) == _POSIX_VDISABLE)) \
184 		      || ((val) <= 0))
185 #else
186 #define DISABLED(val)   ((int)(val) <= 0)
187 #endif
188 
189 #define CHK(val, dft)   (unsigned char) (DISABLED(val) ? dft : val)
190 
191 #define reset_char(item, value) \
192     tty_settings->c_cc[item] = CHK(tty_settings->c_cc[item], value)
193 
194 /*
195  * Simplify ifdefs
196  */
197 #ifndef BSDLY
198 #define BSDLY 0
199 #endif
200 #ifndef CRDLY
201 #define CRDLY 0
202 #endif
203 #ifndef ECHOCTL
204 #define ECHOCTL 0
205 #endif
206 #ifndef ECHOKE
207 #define ECHOKE 0
208 #endif
209 #ifndef ECHOPRT
210 #define ECHOPRT 0
211 #endif
212 #ifndef FFDLY
213 #define FFDLY 0
214 #endif
215 #ifndef IMAXBEL
216 #define IMAXBEL 0
217 #endif
218 #ifndef IUCLC
219 #define IUCLC 0
220 #endif
221 #ifndef IXANY
222 #define IXANY 0
223 #endif
224 #ifndef NLDLY
225 #define NLDLY 0
226 #endif
227 #ifndef OCRNL
228 #define OCRNL 0
229 #endif
230 #ifndef OFDEL
231 #define OFDEL 0
232 #endif
233 #ifndef OFILL
234 #define OFILL 0
235 #endif
236 #ifndef OLCUC
237 #define OLCUC 0
238 #endif
239 #ifndef ONLCR
240 #define ONLCR 0
241 #endif
242 #ifndef ONLRET
243 #define ONLRET 0
244 #endif
245 #ifndef ONOCR
246 #define ONOCR 0
247 #endif
248 #ifndef OXTABS
249 #define OXTABS 0
250 #endif
251 #ifndef TAB3
252 #define TAB3 0
253 #endif
254 #ifndef TABDLY
255 #define TABDLY 0
256 #endif
257 #ifndef TOSTOP
258 #define TOSTOP 0
259 #endif
260 #ifndef VTDLY
261 #define VTDLY 0
262 #endif
263 #ifndef XCASE
264 #define XCASE 0
265 #endif
266 
267 /*
268  * Reset the terminal mode bits to a sensible state.  Very useful after
269  * a child program dies in raw mode.
270  */
271 void
reset_tty_settings(int fd,TTY * tty_settings,int noset)272 reset_tty_settings(int fd, TTY * tty_settings, int noset)
273 {
274 #ifdef TERMIOS
275     unsigned mask;
276 #ifdef TIOCMGET
277     int modem_bits;
278 #endif
279 
280     GET_TTY(fd, tty_settings);
281 
282 #if defined(VDISCARD) && defined(CDISCARD)
283     reset_char(VDISCARD, CDISCARD);
284 #endif
285     reset_char(VEOF, CEOF);
286     reset_char(VERASE, CERASE);
287 #if defined(VERASE2) && defined(CERASE2)
288     reset_char(VERASE2, CERASE2);
289 #endif
290 #if defined(VFLUSH) && defined(CFLUSH)
291     reset_char(VFLUSH, CFLUSH);
292 #endif
293     reset_char(VINTR, CINTR);
294     reset_char(VKILL, CKILL);
295 #if defined(VLNEXT) && defined(CLNEXT)
296     reset_char(VLNEXT, CLNEXT);
297 #endif
298     reset_char(VQUIT, CQUIT);
299 #if defined(VREPRINT) && defined(CRPRNT)
300     reset_char(VREPRINT, CRPRNT);
301 #endif
302 #if defined(VSTART) && defined(CSTART)
303     reset_char(VSTART, CSTART);
304 #endif
305 #if defined(VSTOP) && defined(CSTOP)
306     reset_char(VSTOP, CSTOP);
307 #endif
308 #if defined(VSUSP) && defined(CSUSP)
309     reset_char(VSUSP, CSUSP);
310 #endif
311 #if defined(VWERASE) && defined(CWERASE)
312     reset_char(VWERASE, CWERASE);
313 #endif
314 
315     clear_flags(tty_settings->c_iflag, (IGNBRK
316 					| PARMRK
317 					| INPCK
318 					| ISTRIP
319 					| INLCR
320 					| IGNCR
321 					| IUCLC
322 					| IXANY
323 					| IXOFF));
324 
325     set_flags(tty_settings->c_iflag, (BRKINT
326 				      | IGNPAR
327 				      | ICRNL
328 				      | IXON
329 				      | IMAXBEL));
330 
331     clear_flags(tty_settings->c_oflag, (0
332 					| OLCUC
333 					| OCRNL
334 					| ONOCR
335 					| ONLRET
336 					| OFILL
337 					| OFDEL
338 					| NLDLY
339 					| CRDLY
340 					| TABDLY
341 					| BSDLY
342 					| VTDLY
343 					| FFDLY));
344 
345     set_flags(tty_settings->c_oflag, (OPOST
346 				      | ONLCR));
347 
348     mask = (CSIZE | CSTOPB | PARENB | PARODD);
349 #ifdef TIOCMGET
350     /* leave clocal alone if this appears to use a modem */
351     if (ioctl(fd, TIOCMGET, &modem_bits) == -1)
352 	mask |= CLOCAL;
353 #else
354     /* cannot check - use the behavior from tset */
355     mask |= CLOCAL;
356 #endif
357     clear_flags(tty_settings->c_cflag, mask);
358 
359     set_flags(tty_settings->c_cflag, (CS8 | CREAD));
360     clear_flags(tty_settings->c_lflag, (ECHONL
361 					| NOFLSH
362 					| TOSTOP
363 					| ECHOPRT
364 					| XCASE));
365 
366     set_flags(tty_settings->c_lflag, (ISIG
367 				      | ICANON
368 				      | ECHO
369 				      | ECHOE
370 				      | ECHOK
371 				      | ECHOCTL
372 				      | ECHOKE));
373 #elif defined(_NC_WINDOWS)
374     /* reference:
375        https://learn.microsoft.com/en-us/windows/console/setconsolemode
376        https://learn.microsoft.com/en-us/windows/console/high-level-console-modes
377      */
378     GET_TTY(fd, tty_settings);
379     /* do not change ENABLE_VIRTUAL_TERMINAL_INPUT */
380     /* do not change ENABLE_WINDOW_INPUT */
381     tty_settings->dwFlagIn &= ~(
382 				   ENABLE_INSERT_MODE |
383 				   ENABLE_QUICK_EDIT_MODE);
384     tty_settings->dwFlagIn |= (
385 				  ENABLE_EXTENDED_FLAGS |
386 				  ENABLE_MOUSE_INPUT |
387 				  ENABLE_LINE_INPUT |	/* like ICANON */
388 				  ENABLE_ECHO_INPUT |	/* like ECHO */
389 				  ENABLE_PROCESSED_INPUT	/* like BRKINT */
390 	);
391 #ifdef ENABLED_PROCESSED_OUTPUT
392     tty_settings->dwFlagOut |= ENABLED_PROCESSED_OUTPUT;
393 #endif
394     tty_settings->dwFlagOut |= ENABLE_WRAP_AT_EOL_OUTPUT;
395 #endif /* TERMIOS */
396 
397     if (!noset) {
398 	SET_TTY(fd, tty_settings);
399     }
400 }
401 
402 /*
403  * Returns a "good" value for the erase character.  This is loosely based on
404  * the BSD4.4 logic.
405  */
406 #ifdef TERMIOS
407 static int
default_erase(void)408 default_erase(void)
409 {
410     int result;
411 
412     if (over_strike
413 	&& VALID_STRING(key_backspace)
414 	&& strlen(key_backspace) == 1) {
415 	result = key_backspace[0];
416     } else {
417 	result = CERASE;
418     }
419 
420     return result;
421 }
422 #endif
423 
424 /*
425  * Update the values of the erase, interrupt, and kill characters in the TTY
426  * parameter.
427  *
428  * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
429  * characters if they're unset, or if we specify them as options.  This differs
430  * from BSD 4.4 tset, which always sets erase.
431  */
432 void
set_control_chars(TTY * tty_settings,int my_erase,int my_intr,int my_kill)433 set_control_chars(TTY * tty_settings, int my_erase, int my_intr, int my_kill)
434 {
435 #ifdef TERMIOS
436     if (DISABLED(tty_settings->c_cc[VERASE]) || my_erase >= 0) {
437 	tty_settings->c_cc[VERASE] = UChar((my_erase >= 0)
438 					   ? my_erase
439 					   : default_erase());
440     }
441 
442     if (DISABLED(tty_settings->c_cc[VINTR]) || my_intr >= 0) {
443 	tty_settings->c_cc[VINTR] = UChar((my_intr >= 0)
444 					  ? my_intr
445 					  : CINTR);
446     }
447 
448     if (DISABLED(tty_settings->c_cc[VKILL]) || my_kill >= 0) {
449 	tty_settings->c_cc[VKILL] = UChar((my_kill >= 0)
450 					  ? my_kill
451 					  : CKILL);
452     }
453 #elif defined(_NC_WINDOWS)
454     /* noop */
455     (void) tty_settings;
456     (void) my_erase;
457     (void) my_intr;
458     (void) my_kill;
459 #endif /* TERMIOS */
460 }
461 
462 /*
463  * Set up various conversions in the TTY parameter, including parity, tabs,
464  * returns, echo, and case, according to the termcap entry.
465  */
466 void
set_conversions(TTY * tty_settings)467 set_conversions(TTY * tty_settings)
468 {
469 #ifdef TERMIOS
470     set_flags(tty_settings->c_oflag, ONLCR);
471     set_flags(tty_settings->c_iflag, ICRNL);
472     set_flags(tty_settings->c_lflag, ECHO);
473     set_flags(tty_settings->c_oflag, OXTABS);
474 
475     /* test used to be tgetflag("NL") */
476     if (VALID_STRING(newline) && newline[0] == '\n' && !newline[1]) {
477 	/* Newline, not linefeed. */
478 	clear_flags(tty_settings->c_oflag, ONLCR);
479 	clear_flags(tty_settings->c_iflag, ICRNL);
480     }
481 #if OXTABS
482     /* test used to be tgetflag("pt") */
483     if (VALID_STRING(set_tab) && VALID_STRING(clear_all_tabs))
484 	clear_flags(tty_settings->c_oflag, OXTABS);
485 #endif /* OXTABS */
486     set_flags(tty_settings->c_lflag, (ECHOE | ECHOK));
487 #elif defined(_NC_WINDOWS)
488     (void) tty_settings;
489 #endif /* TERMIOS */
490 }
491 
492 static bool
sent_string(const char * s)493 sent_string(const char *s)
494 {
495     bool sent = FALSE;
496     if (VALID_STRING(s)) {
497 	tputs(s, 0, out_char);
498 	sent = TRUE;
499     }
500     return sent;
501 }
502 
503 static bool
to_left_margin(void)504 to_left_margin(void)
505 {
506     if (VALID_STRING(carriage_return)) {
507 	sent_string(carriage_return);
508     } else {
509 	out_char('\r');
510     }
511     return TRUE;
512 }
513 
514 /*
515  * Set the hardware tabs on the terminal, using the 'ct' (clear all tabs),
516  * 'st' (set one tab) and 'ch' (horizontal cursor addressing) capabilities.
517  * This is done before 'if' and 'is', so they can recover in case of error.
518  *
519  * Return TRUE if we set any tab stops, FALSE if not.
520  */
521 static bool
reset_tabstops(int wide)522 reset_tabstops(int wide)
523 {
524     if ((init_tabs != 8)
525 	&& VALID_NUMERIC(init_tabs)
526 	&& VALID_STRING(set_tab)
527 	&& VALID_STRING(clear_all_tabs)) {
528 	int c;
529 
530 	to_left_margin();
531 	tputs(clear_all_tabs, 0, out_char);
532 	if (init_tabs > 1) {
533 	    if (init_tabs > wide)
534 		init_tabs = (short) wide;
535 	    for (c = init_tabs; c < wide; c += init_tabs) {
536 		fprintf(my_file, "%*s", init_tabs, " ");
537 		tputs(set_tab, 0, out_char);
538 	    }
539 	    to_left_margin();
540 	}
541 	return (TRUE);
542     }
543     return (FALSE);
544 }
545 
546 /* Output startup string. */
547 bool
send_init_strings(int fd GCC_UNUSED,TTY * old_settings)548 send_init_strings(int fd GCC_UNUSED, TTY * old_settings)
549 {
550     int i;
551     bool need_flush = FALSE;
552 
553     (void) old_settings;
554 #if TAB3
555     if (old_settings != NULL &&
556 	old_settings->c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
557 	old_settings->c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
558 	SET_TTY(fd, old_settings);
559     }
560 #endif
561     if (use_reset || use_init) {
562 	if (VALID_STRING(init_prog)) {
563 	    IGNORE_RC(system(init_prog));
564 	}
565 
566 	need_flush |= sent_string((use_reset && (reset_1string != NULL))
567 				  ? reset_1string
568 				  : init_1string);
569 
570 	need_flush |= sent_string((use_reset && (reset_2string != NULL))
571 				  ? reset_2string
572 				  : init_2string);
573 
574 	if (VALID_STRING(clear_margins)) {
575 	    need_flush |= sent_string(clear_margins);
576 	}
577 #if defined(set_lr_margin)
578 	else if (VALID_STRING(set_lr_margin)) {
579 	    need_flush |= sent_string(TIPARM_2(set_lr_margin, 0, columns - 1));
580 	}
581 #endif
582 #if defined(set_left_margin_parm) && defined(set_right_margin_parm)
583 	else if (VALID_STRING(set_left_margin_parm)
584 		 && VALID_STRING(set_right_margin_parm)) {
585 	    need_flush |= sent_string(TIPARM_1(set_left_margin_parm, 0));
586 	    need_flush |= sent_string(TIPARM_1(set_right_margin_parm,
587 					       columns - 1));
588 	}
589 #endif
590 	else if (VALID_STRING(set_left_margin)
591 		 && VALID_STRING(set_right_margin)) {
592 	    need_flush |= to_left_margin();
593 	    need_flush |= sent_string(set_left_margin);
594 	    if (VALID_STRING(parm_right_cursor)) {
595 		need_flush |= sent_string(TIPARM_1(parm_right_cursor,
596 						   columns - 1));
597 	    } else {
598 		for (i = 0; i < columns - 1; i++) {
599 		    out_char(' ');
600 		    need_flush = TRUE;
601 		}
602 	    }
603 	    need_flush |= sent_string(set_right_margin);
604 	    need_flush |= to_left_margin();
605 	}
606 
607 	need_flush |= reset_tabstops(columns);
608 
609 	need_flush |= cat_file((use_reset && reset_file) ? reset_file : init_file);
610 
611 	need_flush |= sent_string((use_reset && (reset_3string != NULL))
612 				  ? reset_3string
613 				  : init_3string);
614     }
615 
616     return need_flush;
617 }
618 
619 #ifdef TERMIOS
620 /*
621  * Tell the user if a control key has been changed from the default value.
622  */
623 static void
show_tty_change(TTY * old_settings,TTY * new_settings,const char * name,int which,unsigned def)624 show_tty_change(TTY * old_settings,
625 		TTY * new_settings,
626 		const char *name,
627 		int which,
628 		unsigned def)
629 {
630     unsigned older = 0, newer = 0;
631     char *p;
632 
633     newer = new_settings->c_cc[which];
634     older = old_settings->c_cc[which];
635 
636     if (older == newer && older == def)
637 	return;
638     (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
639 
640     if (DISABLED(newer)) {
641 	(void) fprintf(stderr, "undef.\n");
642 	/*
643 	 * Check 'delete' before 'backspace', since the key_backspace value
644 	 * is ambiguous.
645 	 */
646     } else if (newer == 0177) {
647 	(void) fprintf(stderr, "delete.\n");
648     } else if ((p = key_backspace) != NULL
649 	       && newer == (unsigned char) p[0]
650 	       && p[1] == '\0') {
651 	(void) fprintf(stderr, "backspace.\n");
652     } else if (newer < 040) {
653 	newer ^= 0100;
654 	(void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
655     } else
656 	(void) fprintf(stderr, "%c.\n", UChar(newer));
657 }
658 #endif /* TERMIOS */
659 
660 /**************************************************************************
661  * Miscellaneous.
662  **************************************************************************/
663 
664 void
reset_start(FILE * fp,bool is_reset,bool is_init)665 reset_start(FILE *fp, bool is_reset, bool is_init)
666 {
667     my_file = fp;
668     use_reset = is_reset;
669     use_init = is_init;
670 }
671 
672 void
reset_flush(void)673 reset_flush(void)
674 {
675     if (my_file != NULL)
676 	fflush(my_file);
677 }
678 
679 void
print_tty_chars(TTY * old_settings,TTY * new_settings)680 print_tty_chars(TTY * old_settings, TTY * new_settings)
681 {
682 #ifdef TERMIOS
683     show_tty_change(old_settings, new_settings, "Erase", VERASE, CERASE);
684     show_tty_change(old_settings, new_settings, "Kill", VKILL, CKILL);
685     show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR);
686 #elif defined(_NC_WINDOWS)
687     (void) old_settings;
688     (void) new_settings;
689 #endif /* TERMIOS */
690 }
691 
692 #if HAVE_SIZECHANGE
693 /*
694  * Set window size if not set already, but update our copy of the values if the
695  * size was set.
696  */
697 void
set_window_size(int fd,NCURSES_INT2 * high,NCURSES_INT2 * wide)698 set_window_size(int fd, NCURSES_INT2 *high, NCURSES_INT2 *wide)
699 {
700     STRUCT_WINSIZE win;
701     (void) ioctl(fd, IOCTL_GET_WINSIZE, &win);
702     if (WINSIZE_ROWS(win) == 0 &&
703 	WINSIZE_COLS(win) == 0) {
704 	if (*high > 0 && *wide > 0) {
705 	    WINSIZE_ROWS(win) = (unsigned short) *high;
706 	    WINSIZE_COLS(win) = (unsigned short) *wide;
707 	    (void) ioctl(fd, IOCTL_SET_WINSIZE, &win);
708 	}
709     } else if (WINSIZE_ROWS(win) > 0 &&
710 	       WINSIZE_COLS(win) > 0) {
711 	*high = (short) WINSIZE_ROWS(win);
712 	*wide = (short) WINSIZE_COLS(win);
713     }
714 }
715 #endif
716