14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin
244887Schin /*
254887Schin * AT&T Research
264887Schin * return terminal rows and cols
274887Schin */
284887Schin
294887Schin #include <ast.h>
304887Schin #include <ast_tty.h>
314887Schin
324887Schin #if defined(__STDPP__directive) && defined(__STDPP__hide)
334887Schin __STDPP__directive pragma pp:hide ioctl sleep
344887Schin #else
354887Schin #define ioctl ______ioctl
364887Schin #define sleep ______sleep
374887Schin #endif
384887Schin
394887Schin #if _sys_ioctl
404887Schin #include <sys/ioctl.h>
414887Schin #endif
424887Schin
434887Schin #if defined(TIOCGWINSZ)
444887Schin #if _sys_stream && _sys_ptem
454887Schin #include <sys/stream.h>
464887Schin #include <sys/ptem.h>
474887Schin #endif
484887Schin #else
494887Schin #if !defined(TIOCGSIZE) && !defined(TIOCGWINSZ)
504887Schin #if _hdr_jioctl
514887Schin #define jwinsize winsize
524887Schin #include <jioctl.h>
534887Schin #else
544887Schin #if _sys_jioctl
554887Schin #define jwinsize winsize
564887Schin #include <sys/jioctl.h>
574887Schin #endif
584887Schin #endif
594887Schin #endif
604887Schin #endif
614887Schin
624887Schin #if defined(__STDPP__directive) && defined(__STDPP__hide)
634887Schin __STDPP__directive pragma pp:nohide ioctl sleep
644887Schin #else
654887Schin #undef ioctl
664887Schin #undef sleep
674887Schin #endif
684887Schin
694887Schin static int ttctl(int, int, void*);
704887Schin
714887Schin void
astwinsize(int fd,register int * rows,register int * cols)724887Schin astwinsize(int fd, register int* rows, register int* cols)
734887Schin {
744887Schin #ifdef TIOCGWINSZ
754887Schin #define NEED_ttctl
764887Schin struct winsize ws;
774887Schin
784887Schin if (!ttctl(fd, TIOCGWINSZ, &ws) && ws.ws_col > 0 && ws.ws_row > 0)
794887Schin {
804887Schin if (rows) *rows = ws.ws_row;
814887Schin if (cols) *cols = ws.ws_col;
824887Schin }
834887Schin else
844887Schin #else
854887Schin #ifdef TIOCGSIZE
864887Schin #define NEED_ttctl
874887Schin struct ttysize ts;
884887Schin
894887Schin if (!ttctl(fd, TIOCGSIZE, &ts) && ts.ts_lines > 0 && ts.ts_cols > 0)
904887Schin {
914887Schin if (rows) *rows = ts.ts_lines;
924887Schin if (cols) *cols = ts.ts_cols;
934887Schin }
944887Schin else
954887Schin #else
964887Schin #ifdef JWINSIZE
974887Schin #define NEED_ttctl
984887Schin struct winsize ws;
994887Schin
1004887Schin if (!ttctl(fd, JWINSIZE, &ws) && ws.bytesx > 0 && ws.bytesy > 0)
1014887Schin {
1024887Schin if (rows) *rows = ws.bytesy;
1034887Schin if (cols) *cols = ws.bytesx;
1044887Schin }
1054887Schin else
1064887Schin #endif
1074887Schin #endif
1084887Schin #endif
1094887Schin {
1104887Schin char* s;
1114887Schin
1124887Schin if (rows) *rows = (s = getenv("LINES")) ? strtol(s, NiL, 0) : 0;
1134887Schin if (cols) *cols = (s = getenv("COLUMNS")) ? strtol(s, NiL, 0) : 0;
1144887Schin }
1154887Schin }
1164887Schin
1174887Schin #ifdef NEED_ttctl
1184887Schin
1194887Schin /*
1204887Schin * tty ioctl() -- no cache
1214887Schin */
1224887Schin
1234887Schin static int
ttctl(register int fd,int op,void * tt)1244887Schin ttctl(register int fd, int op, void* tt)
1254887Schin {
1264887Schin register int v;
1274887Schin
1284887Schin if (fd < 0)
1294887Schin {
1304887Schin for (fd = 0; fd <= 2; fd++)
1314887Schin if (!ioctl(fd, op, tt)) return(0);
1324887Schin if ((fd = open("/dev/tty", O_RDONLY)) >= 0)
1334887Schin {
1344887Schin v = ioctl(fd, op, tt);
1354887Schin close(fd);
1364887Schin return(v);
1374887Schin }
1384887Schin }
1394887Schin else if (!ioctl(fd, op, tt)) return(0);
1404887Schin return(-1);
1414887Schin }
1424887Schin
1434887Schin #endif
144