resize.c revision 8f44fb3b
1/* $XTermId: resize.c,v 1.145 2021/03/21 20:03:17 tom Exp $ */
2
3/*
4 * Copyright 2003-2020,2021 by Thomas E. Dickey
5 *
6 *                         All Rights Reserved
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
23 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Except as contained in this notice, the name(s) of the above copyright
28 * holders shall not be used in advertising or otherwise to promote the
29 * sale, use or other dealings in this Software without prior written
30 * authorization.
31 *
32 *
33 * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
34 *
35 *                         All Rights Reserved
36 *
37 * Permission to use, copy, modify, and distribute this software and its
38 * documentation for any purpose and without fee is hereby granted,
39 * provided that the above copyright notice appear in all copies and that
40 * both that copyright notice and this permission notice appear in
41 * supporting documentation, and that the name of Digital Equipment
42 * Corporation not be used in advertising or publicity pertaining to
43 * distribution of the software without specific, written prior permission.
44 *
45 *
46 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
47 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
48 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
49 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
50 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
51 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
52 * SOFTWARE.
53 */
54
55/* resize.c */
56
57#include <stdio.h>
58#include <ctype.h>
59
60#ifdef RESIZE_ONLY
61#include "resize.h"
62#else
63#include <xterm.h>
64#include <version.h>
65#include <xstrings.h>
66#include <xtermcap.h>
67#include <xterm_io.h>
68#endif
69
70#ifndef USE_TERMINFO		/* avoid conflict with configure script */
71#if defined(__QNX__) || defined(__SCO__) || defined(linux) || defined(__OpenBSD__) || defined(__UNIXWARE__)
72#define USE_TERMINFO
73#endif
74#endif
75
76#if defined(__QNX__)
77#include <unix.h>
78#endif
79
80/*
81 * Some OS's may want to use both, like SCO for example.  We catch here anyone
82 * who hasn't decided what they want.
83 */
84#if !defined(USE_TERMCAP) && !defined(USE_TERMINFO)
85#define USE_TERMINFO
86#endif
87
88#include <signal.h>
89#include <pwd.h>
90
91#ifdef USE_IGNORE_RC
92int ignore_unused;
93#endif
94
95#ifdef __MVS__
96#define ESCAPE(string) "\047" string
97#else
98#define ESCAPE(string) "\033" string
99#endif
100
101#define	EMULATIONS	2
102#define	SUN		1
103#define	VT100		0
104
105#define	TIMEOUT		10
106
107#define	SHELL_UNKNOWN	0
108#define	SHELL_C		1
109#define	SHELL_BOURNE	2
110/* *INDENT-OFF* */
111static struct {
112    const char *name;
113    int type;
114} shell_list[] = {
115    { "csh",	SHELL_C },	/* vanilla cshell */
116    { "jcsh",   SHELL_C },
117    { "tcsh",   SHELL_C },
118    { "sh",	SHELL_BOURNE }, /* vanilla Bourne shell */
119    { "ash",    SHELL_BOURNE },
120    { "bash",	SHELL_BOURNE }, /* GNU Bourne again shell */
121    { "dash",	SHELL_BOURNE },
122    { "jsh",    SHELL_BOURNE },
123    { "ksh",	SHELL_BOURNE }, /* Korn shell (from AT&T toolchest) */
124    { "ksh-i",	SHELL_BOURNE }, /* another name for Korn shell */
125    { "ksh93",	SHELL_BOURNE }, /* Korn shell */
126    { "mksh",   SHELL_BOURNE },
127    { "pdksh",  SHELL_BOURNE },
128    { "zsh",    SHELL_BOURNE },
129    { NULL,	SHELL_BOURNE }	/* default (same as xterm's) */
130};
131/* *INDENT-ON* */
132
133static const char *const emuname[EMULATIONS] =
134{
135    "VT100",
136    "Sun",
137};
138static char *myname;
139static int shell_type = SHELL_UNKNOWN;
140static const char *const getsize[EMULATIONS] =
141{
142    ESCAPE("7") ESCAPE("[r") ESCAPE("[9999;9999H") ESCAPE("[6n"),
143    ESCAPE("[18t"),
144};
145#if defined(USE_STRUCT_WINSIZE)
146static const char *const getwsize[EMULATIONS] =
147{				/* size in pixels */
148    0,
149    ESCAPE("[14t"),
150};
151#endif /* USE_STRUCT_WINSIZE */
152static const char *const restore[EMULATIONS] =
153{
154    ESCAPE("8"),
155    0,
156};
157static const char *const setsize[EMULATIONS] =
158{
159    0,
160    ESCAPE("[8;%s;%st"),
161};
162
163#ifdef USE_ANY_SYSV_TERMIO
164static struct termio tioorig;
165#elif defined(USE_TERMIOS)
166static struct termios tioorig;
167#else
168static struct sgttyb sgorig;
169#endif /* USE_ANY_SYSV_TERMIO/USE_TERMIOS */
170
171static const char *const size[EMULATIONS] =
172{
173    ESCAPE("[%d;%dR"),
174    ESCAPE("[8;%d;%dt"),
175};
176static const char sunname[] = "sunsize";
177static int tty;
178static FILE *ttyfp;
179
180#if defined(USE_STRUCT_WINSIZE)
181static const char *wsize[EMULATIONS] =
182{
183    0,
184    ESCAPE("[4;%hd;%hdt"),
185};
186#endif /* USE_STRUCT_WINSIZE */
187
188static GCC_NORETURN void failed(const char *);
189static GCC_NORETURN void onintr(int);
190static GCC_NORETURN void resize_timeout(int);
191static GCC_NORETURN void Usage(void);
192
193static void
194failed(const char *s)
195{
196    int save = errno;
197    IGNORE_RC(write(2, myname, strlen(myname)));
198    IGNORE_RC(write(2, ": ", (size_t) 2));
199    errno = save;
200    perror(s);
201    exit(EXIT_FAILURE);
202}
203
204/* ARGSUSED */
205static void
206onintr(int sig GCC_UNUSED)
207{
208#ifdef USE_ANY_SYSV_TERMIO
209    (void) ioctl(tty, TCSETAW, &tioorig);
210#elif defined(USE_TERMIOS)
211    (void) tcsetattr(tty, TCSADRAIN, &tioorig);
212#else /* not USE_TERMIOS */
213    (void) ioctl(tty, TIOCSETP, &sgorig);
214#endif /* USE_ANY_SYSV_TERMIO/USE_TERMIOS */
215    exit(EXIT_FAILURE);
216}
217
218static void
219resize_timeout(int sig)
220{
221    fprintf(stderr, "\n%s: Time out occurred\r\n", myname);
222    onintr(sig);
223}
224
225static void
226Usage(void)
227{
228    fprintf(stderr, strcmp(myname, sunname) == 0 ?
229	    "Usage: %s [rows cols]\n" :
230	    "Usage: %s [-v] [-u] [-c] [-s [rows cols]]\n", myname);
231    exit(EXIT_FAILURE);
232}
233
234#ifdef USE_TERMCAP
235static void
236print_termcap(const char *termcap)
237{
238    int ch;
239
240    putchar('\'');
241    while ((ch = *termcap++) != '\0') {
242	switch (ch & 0xff) {
243	case 127:		/* undo bug in GNU termcap */
244	    printf("^?");
245	    break;
246	case '\'':		/* must escape anyway (unlikely) */
247	    /* FALLTHRU */
248	case '!':		/* must escape for SunOS csh */
249	    putchar('\\');
250	    /* FALLTHRU */
251	default:
252	    putchar(ch);
253	    break;
254	}
255    }
256    putchar('\'');
257}
258#endif /* USE_TERMCAP */
259
260static int
261checkdigits(char *str)
262{
263    while (*str) {
264	if (!isdigit(CharOf(*str)))
265	    return (0);
266	str++;
267    }
268    return (1);
269}
270
271static void
272readstring(FILE *fp, char *buf, const char *str)
273{
274    int last, c;
275#if !defined(USG) && !defined(__minix)
276    /* What is the advantage of setitimer() over alarm()? */
277    struct itimerval it;
278#endif
279
280    signal(SIGALRM, resize_timeout);
281#if defined(USG) || defined(__minix)
282    alarm(TIMEOUT);
283#else
284    memset((char *) &it, 0, sizeof(struct itimerval));
285    it.it_value.tv_sec = TIMEOUT;
286    setitimer(ITIMER_REAL, &it, (struct itimerval *) NULL);
287#endif
288    if ((c = getc(fp)) == 0233) {	/* meta-escape, CSI */
289	c = ESCAPE("")[0];
290	*buf++ = (char) c;
291	*buf++ = '[';
292    } else {
293	*buf++ = (char) c;
294    }
295    if (c != *str) {
296	fprintf(stderr, "%s: unknown character, exiting.\r\n", myname);
297	onintr(0);
298    }
299    last = str[strlen(str) - 1];
300    while ((*buf++ = (char) getc(fp)) != last) {
301	;
302    }
303#if defined(USG) || defined(__minix)
304    alarm(0);
305#else
306    memset((char *) &it, 0, sizeof(struct itimerval));
307    setitimer(ITIMER_REAL, &it, (struct itimerval *) NULL);
308#endif
309    *buf = 0;
310}
311
312/*
313   resets termcap string to reflect current screen size
314 */
315int
316main(int argc, char **argv ENVP_ARG)
317{
318#ifdef USE_TERMCAP
319    char *env;
320#endif
321    char *ptr;
322    int emu = VT100;
323    char *shell;
324    int i;
325    int rc;
326    int rows, cols;
327#ifdef USE_ANY_SYSV_TERMIO
328    struct termio tio;
329#elif defined(USE_TERMIOS)
330    struct termios tio;
331#else
332    struct sgttyb sg;
333#endif /* USE_ANY_SYSV_TERMIO/USE_TERMIOS */
334#ifdef USE_TERMCAP
335    int ok_tcap = 1;
336    char termcap[TERMCAP_SIZE];
337    char newtc[TERMCAP_SIZE];
338#endif /* USE_TERMCAP */
339    char buf[BUFSIZ];
340#ifdef TTYSIZE_STRUCT
341    TTYSIZE_STRUCT ts;
342#endif
343    char *name_of_tty;
344#ifdef CANT_OPEN_DEV_TTY
345    extern char *ttyname();
346#endif
347    const char *setname = "";
348
349    myname = x_basename(argv[0]);
350    if (strcmp(myname, sunname) == 0)
351	emu = SUN;
352    for (argv++, argc--; argc > 0 && **argv == '-'; argv++, argc--) {
353	switch ((*argv)[1]) {
354	case 's':		/* Sun emulation */
355	    if (emu == SUN)
356		Usage();	/* Never returns */
357	    emu = SUN;
358	    break;
359	case 'u':		/* Bourne (Unix) shell */
360	    shell_type = SHELL_BOURNE;
361	    break;
362	case 'c':		/* C shell */
363	    shell_type = SHELL_C;
364	    break;
365	case 'v':
366	    printf("%s\n", xtermVersion());
367	    exit(EXIT_SUCCESS);
368	default:
369	    Usage();		/* Never returns */
370	}
371    }
372
373    if (SHELL_UNKNOWN == shell_type) {
374	/* Find out what kind of shell this user is running.
375	 * This is the same algorithm that xterm uses.
376	 */
377	if ((ptr = x_getenv("SHELL")) == NULL) {
378	    uid_t uid = getuid();
379	    struct passwd pw;
380
381	    if (x_getpwuid(uid, &pw)) {
382		(void) x_getlogin(uid, &pw);
383	    }
384	    if (!OkPasswd(&pw)
385		|| *(ptr = pw.pw_shell) == 0) {
386		/* this is the same default that xterm uses */
387		ptr = x_strdup("/bin/sh");
388	    }
389	}
390
391	shell = x_basename(ptr);
392
393	/* now that we know, what kind is it? */
394	for (i = 0; shell_list[i].name; i++) {
395	    if (!strcmp(shell_list[i].name, shell)) {
396		break;
397	    }
398	}
399	shell_type = shell_list[i].type;
400    }
401
402    if (argc == 2) {
403	if (!setsize[emu]) {
404	    fprintf(stderr,
405		    "%s: Can't set window size under %s emulation\n",
406		    myname, emuname[emu]);
407	    exit(EXIT_FAILURE);
408	}
409	if (!checkdigits(argv[0]) || !checkdigits(argv[1])) {
410	    Usage();		/* Never returns */
411	}
412    } else if (argc != 0) {
413	Usage();		/* Never returns */
414    }
415#ifdef CANT_OPEN_DEV_TTY
416    if ((name_of_tty = ttyname(fileno(stderr))) == NULL)
417#endif
418	name_of_tty = x_strdup("/dev/tty");
419
420    if ((ttyfp = fopen(name_of_tty, "r+")) == NULL) {
421	fprintf(stderr, "%s:  can't open terminal %s\n",
422		myname, name_of_tty);
423	exit(EXIT_FAILURE);
424    }
425    tty = fileno(ttyfp);
426#ifdef USE_TERMCAP
427    if ((env = x_getenv("TERM")) == 0) {
428	env = x_strdup(DFT_TERMTYPE);
429	if (SHELL_BOURNE == shell_type) {
430	    setname = "TERM=" DFT_TERMTYPE ";\nexport TERM;\n";
431	} else {
432	    setname = "setenv TERM " DFT_TERMTYPE ";\n";
433	}
434    }
435    termcap[0] = 0;		/* ...just in case we've accidentally gotten terminfo */
436    if (tgetent(termcap, env) <= 0 || termcap[0] == 0) {
437	ok_tcap = 0;
438    }
439#endif /* USE_TERMCAP */
440#ifdef USE_TERMINFO
441    if (x_getenv("TERM") == 0) {
442	if (SHELL_BOURNE == shell_type) {
443	    setname = "TERM=" DFT_TERMTYPE ";\nexport TERM;\n";
444	} else {
445	    setname = "setenv TERM " DFT_TERMTYPE ";\n";
446	}
447    }
448#endif /* USE_TERMINFO */
449
450#ifdef USE_ANY_SYSV_TERMIO
451    rc = ioctl(tty, TCGETA, &tioorig);
452    tio = tioorig;
453    UIntClr(tio.c_iflag, (ICRNL | IUCLC));
454    UIntClr(tio.c_lflag, (ICANON | ECHO));
455    tio.c_cflag |= CS8;
456    tio.c_cc[VMIN] = 6;
457    tio.c_cc[VTIME] = 1;
458#elif defined(USE_TERMIOS)
459    rc = tcgetattr(tty, &tioorig);
460    tio = tioorig;
461    UIntClr(tio.c_iflag, ICRNL);
462    UIntClr(tio.c_lflag, (ICANON | ECHO));
463    tio.c_cflag |= CS8;
464    tio.c_cc[VMIN] = 6;
465    tio.c_cc[VTIME] = 1;
466#else /* not USE_TERMIOS */
467    rc = ioctl(tty, TIOCGETP, &sgorig);
468    sg = sgorig;
469    sg.sg_flags |= RAW;
470    UIntClr(sg.sg_flags, ECHO);
471#endif /* USE_ANY_SYSV_TERMIO/USE_TERMIOS */
472    if (rc != 0)
473	failed("get tty settings");
474
475    signal(SIGINT, onintr);
476    signal(SIGQUIT, onintr);
477    signal(SIGTERM, onintr);
478
479#ifdef USE_ANY_SYSV_TERMIO
480    rc = ioctl(tty, TCSETAW, &tio);
481#elif defined(USE_TERMIOS)
482    rc = tcsetattr(tty, TCSADRAIN, &tio);
483#else /* not USE_TERMIOS */
484    rc = ioctl(tty, TIOCSETP, &sg);
485#endif /* USE_ANY_SYSV_TERMIO/USE_TERMIOS */
486    if (rc != 0)
487	failed("set tty settings");
488
489    if (argc == 2) {		/* look for optional parameters of "-s" */
490	char *tmpbuf = TypeMallocN(char,
491				   strlen(setsize[emu]) +
492				   strlen(argv[0]) +
493				   strlen(argv[1]) +
494				   1);
495	if (tmpbuf == 0) {
496	    fprintf(stderr, "%s: Cannot query size\n", myname);
497	    onintr(0);
498	} else {
499	    sprintf(tmpbuf, setsize[emu], argv[0], argv[1]);
500	    IGNORE_RC(write(tty, tmpbuf, strlen(tmpbuf)));
501	    free(tmpbuf);
502	}
503    }
504    IGNORE_RC(write(tty, getsize[emu], strlen(getsize[emu])));
505    readstring(ttyfp, buf, size[emu]);
506    if (sscanf(buf, size[emu], &rows, &cols) != 2) {
507	fprintf(stderr, "%s: Can't get rows and columns\r\n", myname);
508	onintr(0);
509    }
510    if (restore[emu])
511	IGNORE_RC(write(tty, restore[emu], strlen(restore[emu])));
512#if defined(USE_STRUCT_WINSIZE)
513    /* finally, set the tty's window size */
514    if (getwsize[emu]) {
515	/* get the window size in pixels */
516	IGNORE_RC(write(tty, getwsize[emu], strlen(getwsize[emu])));
517	readstring(ttyfp, buf, wsize[emu]);
518	if (sscanf(buf, wsize[emu], &ts.ws_xpixel, &ts.ws_ypixel) != 2) {
519	    fprintf(stderr, "%s: Can't get window size\r\n", myname);
520	    onintr(0);
521	}
522	setup_winsize(ts, rows, cols, 0, 0);
523	SET_TTYSIZE(tty, ts);
524    } else if (ioctl(tty, TIOCGWINSZ, &ts) != -1) {
525	/* we don't have any way of directly finding out
526	   the current height & width of the window in pixels.  We try
527	   our best by computing the font height and width from the "old"
528	   window-size values, and multiplying by these ratios... */
529#define scaled(old,new,len) (old)?((unsigned)(new)*(len)/(old)):(len)
530	setup_winsize(ts, rows, cols,
531		      scaled(TTYSIZE_ROWS(ts), rows, ts.ws_ypixel),
532		      scaled(TTYSIZE_COLS(ts), cols, ts.ws_xpixel));
533	SET_TTYSIZE(tty, ts);
534    }
535#endif /* USE_STRUCT_WINSIZE */
536
537#ifdef USE_ANY_SYSV_TERMIO
538    rc = ioctl(tty, TCSETAW, &tioorig);
539#elif defined(USE_TERMIOS)
540    rc = tcsetattr(tty, TCSADRAIN, &tioorig);
541#else /* not USE_TERMIOS */
542    rc = ioctl(tty, TIOCSETP, &sgorig);
543#endif /* USE_ANY_SYSV_TERMIO/USE_TERMIOS */
544    if (rc != 0)
545	failed("set tty settings");
546
547    signal(SIGINT, SIG_DFL);
548    signal(SIGQUIT, SIG_DFL);
549    signal(SIGTERM, SIG_DFL);
550
551#ifdef USE_TERMCAP
552    if (ok_tcap) {
553	/* update termcap string */
554	/* first do columns */
555	if ((ptr = x_strindex(termcap, "co#")) == NULL) {
556	    fprintf(stderr, "%s: No `co#'\n", myname);
557	    exit(EXIT_FAILURE);
558	}
559
560	i = (int) (ptr - termcap) + 3;
561	strncpy(newtc, termcap, (size_t) i);
562	sprintf(newtc + i, "%d", cols);
563	if ((ptr = strchr(ptr, ':')) != 0)
564	    strcat(newtc, ptr);
565
566	/* now do lines */
567	if ((ptr = x_strindex(newtc, "li#")) == NULL) {
568	    fprintf(stderr, "%s: No `li#'\n", myname);
569	    exit(EXIT_FAILURE);
570	}
571
572	i = (int) (ptr - newtc) + 3;
573	strncpy(termcap, newtc, (size_t) i);
574	sprintf(termcap + i, "%d", rows);
575	if ((ptr = strchr(ptr, ':')) != 0)
576	    strcat(termcap, ptr);
577    }
578#endif /* USE_TERMCAP */
579
580    if (SHELL_BOURNE == shell_type) {
581
582#ifdef USE_TERMCAP
583	if (ok_tcap) {
584	    printf("%sTERMCAP=", setname);
585	    print_termcap(termcap);
586	    printf(";\nexport TERMCAP;\n");
587	}
588#endif /* USE_TERMCAP */
589#ifdef USE_TERMINFO
590	printf("%sCOLUMNS=%d;\nLINES=%d;\nexport COLUMNS LINES;\n",
591	       setname, cols, rows);
592#endif /* USE_TERMINFO */
593
594    } else {			/* not Bourne shell */
595
596#ifdef USE_TERMCAP
597	if (ok_tcap) {
598	    printf("set noglob;\n%ssetenv TERMCAP ", setname);
599	    print_termcap(termcap);
600	    printf(";\nunset noglob;\n");
601	}
602#endif /* USE_TERMCAP */
603#ifdef USE_TERMINFO
604	printf("set noglob;\n%ssetenv COLUMNS '%d';\nsetenv LINES '%d';\nunset noglob;\n",
605	       setname, cols, rows);
606#endif /* USE_TERMINFO */
607    }
608    exit(EXIT_SUCCESS);
609}
610