Home | History | Annotate | Line # | Download | only in dist
os.c revision 1.1.1.2
      1  1.1.1.2  tron /*	$NetBSD: os.c,v 1.1.1.2 2013/09/04 19:35:04 tron Exp $	*/
      2      1.1  tron 
      3      1.1  tron /*
      4  1.1.1.2  tron  * Copyright (C) 1984-2012  Mark Nudelman
      5      1.1  tron  *
      6      1.1  tron  * You may distribute under the terms of either the GNU General Public
      7      1.1  tron  * License or the Less License, as specified in the README file.
      8      1.1  tron  *
      9  1.1.1.2  tron  * For more information, see the README file.
     10      1.1  tron  */
     11      1.1  tron 
     12      1.1  tron 
     13      1.1  tron /*
     14      1.1  tron  * Operating system dependent routines.
     15      1.1  tron  *
     16      1.1  tron  * Most of the stuff in here is based on Unix, but an attempt
     17      1.1  tron  * has been made to make things work on other operating systems.
     18      1.1  tron  * This will sometimes result in a loss of functionality, unless
     19      1.1  tron  * someone rewrites code specifically for the new operating system.
     20      1.1  tron  *
     21      1.1  tron  * The makefile provides defines to decide whether various
     22      1.1  tron  * Unix features are present.
     23      1.1  tron  */
     24      1.1  tron 
     25      1.1  tron #include "less.h"
     26      1.1  tron #include <signal.h>
     27      1.1  tron #include <setjmp.h>
     28      1.1  tron #if HAVE_TIME_H
     29      1.1  tron #include <time.h>
     30      1.1  tron #endif
     31      1.1  tron #if HAVE_ERRNO_H
     32      1.1  tron #include <errno.h>
     33      1.1  tron #endif
     34      1.1  tron #if HAVE_VALUES_H
     35      1.1  tron #include <values.h>
     36      1.1  tron #endif
     37      1.1  tron 
     38      1.1  tron #if HAVE_TIME_T
     39      1.1  tron #define time_type	time_t
     40      1.1  tron #else
     41      1.1  tron #define	time_type	long
     42      1.1  tron #endif
     43      1.1  tron 
     44      1.1  tron /*
     45      1.1  tron  * BSD setjmp() saves (and longjmp() restores) the signal mask.
     46      1.1  tron  * This costs a system call or two per setjmp(), so if possible we clear the
     47      1.1  tron  * signal mask with sigsetmask(), and use _setjmp()/_longjmp() instead.
     48      1.1  tron  * On other systems, setjmp() doesn't affect the signal mask and so
     49      1.1  tron  * _setjmp() does not exist; we just use setjmp().
     50      1.1  tron  */
     51      1.1  tron #if HAVE__SETJMP && HAVE_SIGSETMASK
     52      1.1  tron #define SET_JUMP	_setjmp
     53      1.1  tron #define LONG_JUMP	_longjmp
     54      1.1  tron #else
     55      1.1  tron #define SET_JUMP	setjmp
     56      1.1  tron #define LONG_JUMP	longjmp
     57      1.1  tron #endif
     58      1.1  tron 
     59      1.1  tron public int reading;
     60      1.1  tron 
     61      1.1  tron static jmp_buf read_label;
     62      1.1  tron 
     63      1.1  tron extern int sigs;
     64      1.1  tron 
     65      1.1  tron /*
     66      1.1  tron  * Like read() system call, but is deliberately interruptible.
     67      1.1  tron  * A call to intread() from a signal handler will interrupt
     68      1.1  tron  * any pending iread().
     69      1.1  tron  */
     70      1.1  tron 	public int
     71      1.1  tron iread(fd, buf, len)
     72      1.1  tron 	int fd;
     73      1.1  tron 	char *buf;
     74      1.1  tron 	unsigned int len;
     75      1.1  tron {
     76      1.1  tron 	register int n;
     77      1.1  tron 
     78      1.1  tron start:
     79      1.1  tron #if MSDOS_COMPILER==WIN32C
     80      1.1  tron 	if (ABORT_SIGS())
     81      1.1  tron 		return (READ_INTR);
     82      1.1  tron #else
     83      1.1  tron #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
     84      1.1  tron 	if (kbhit())
     85      1.1  tron 	{
     86      1.1  tron 		int c;
     87      1.1  tron 
     88      1.1  tron 		c = getch();
     89      1.1  tron 		if (c == '\003')
     90      1.1  tron 			return (READ_INTR);
     91      1.1  tron 		ungetch(c);
     92      1.1  tron 	}
     93      1.1  tron #endif
     94      1.1  tron #endif
     95      1.1  tron 	if (SET_JUMP(read_label))
     96      1.1  tron 	{
     97      1.1  tron 		/*
     98      1.1  tron 		 * We jumped here from intread.
     99      1.1  tron 		 */
    100      1.1  tron 		reading = 0;
    101      1.1  tron #if HAVE_SIGPROCMASK
    102      1.1  tron 		{
    103      1.1  tron 		  sigset_t mask;
    104      1.1  tron 		  sigemptyset(&mask);
    105      1.1  tron 		  sigprocmask(SIG_SETMASK, &mask, NULL);
    106      1.1  tron 		}
    107      1.1  tron #else
    108      1.1  tron #if HAVE_SIGSETMASK
    109      1.1  tron 		sigsetmask(0);
    110      1.1  tron #else
    111      1.1  tron #ifdef _OSK
    112      1.1  tron 		sigmask(~0);
    113      1.1  tron #endif
    114      1.1  tron #endif
    115      1.1  tron #endif
    116      1.1  tron 		return (READ_INTR);
    117      1.1  tron 	}
    118      1.1  tron 
    119      1.1  tron 	flush();
    120      1.1  tron 	reading = 1;
    121      1.1  tron #if MSDOS_COMPILER==DJGPPC
    122      1.1  tron 	if (isatty(fd))
    123      1.1  tron 	{
    124      1.1  tron 		/*
    125      1.1  tron 		 * Don't try reading from a TTY until a character is
    126      1.1  tron 		 * available, because that makes some background programs
    127      1.1  tron 		 * believe DOS is busy in a way that prevents those
    128      1.1  tron 		 * programs from working while "less" waits.
    129      1.1  tron 		 */
    130      1.1  tron 		fd_set readfds;
    131      1.1  tron 
    132      1.1  tron 		FD_ZERO(&readfds);
    133      1.1  tron 		FD_SET(fd, &readfds);
    134      1.1  tron 		if (select(fd+1, &readfds, 0, 0, 0) == -1)
    135      1.1  tron 			return (-1);
    136      1.1  tron 	}
    137      1.1  tron #endif
    138      1.1  tron 	n = read(fd, buf, len);
    139      1.1  tron #if 1
    140      1.1  tron 	/*
    141      1.1  tron 	 * This is a kludge to workaround a problem on some systems
    142      1.1  tron 	 * where terminating a remote tty connection causes read() to
    143      1.1  tron 	 * start returning 0 forever, instead of -1.
    144      1.1  tron 	 */
    145      1.1  tron 	{
    146      1.1  tron 		extern int ignore_eoi;
    147      1.1  tron 		if (!ignore_eoi)
    148      1.1  tron 		{
    149      1.1  tron 			static int consecutive_nulls = 0;
    150      1.1  tron 			if (n == 0)
    151      1.1  tron 				consecutive_nulls++;
    152      1.1  tron 			else
    153      1.1  tron 				consecutive_nulls = 0;
    154      1.1  tron 			if (consecutive_nulls > 20)
    155      1.1  tron 				quit(QUIT_ERROR);
    156      1.1  tron 		}
    157      1.1  tron 	}
    158      1.1  tron #endif
    159      1.1  tron 	reading = 0;
    160      1.1  tron 	if (n < 0)
    161      1.1  tron 	{
    162      1.1  tron #if HAVE_ERRNO
    163      1.1  tron 		/*
    164      1.1  tron 		 * Certain values of errno indicate we should just retry the read.
    165      1.1  tron 		 */
    166      1.1  tron #if MUST_DEFINE_ERRNO
    167      1.1  tron 		extern int errno;
    168      1.1  tron #endif
    169      1.1  tron #ifdef EINTR
    170      1.1  tron 		if (errno == EINTR)
    171      1.1  tron 			goto start;
    172      1.1  tron #endif
    173      1.1  tron #ifdef EAGAIN
    174      1.1  tron 		if (errno == EAGAIN)
    175      1.1  tron 			goto start;
    176      1.1  tron #endif
    177      1.1  tron #endif
    178      1.1  tron 		return (-1);
    179      1.1  tron 	}
    180      1.1  tron 	return (n);
    181      1.1  tron }
    182      1.1  tron 
    183      1.1  tron /*
    184      1.1  tron  * Interrupt a pending iread().
    185      1.1  tron  */
    186      1.1  tron 	public void
    187      1.1  tron intread()
    188      1.1  tron {
    189      1.1  tron 	LONG_JUMP(read_label, 1);
    190      1.1  tron }
    191      1.1  tron 
    192      1.1  tron /*
    193      1.1  tron  * Return the current time.
    194      1.1  tron  */
    195      1.1  tron #if HAVE_TIME
    196      1.1  tron 	public long
    197      1.1  tron get_time()
    198      1.1  tron {
    199      1.1  tron 	time_type t;
    200      1.1  tron 
    201      1.1  tron 	time(&t);
    202      1.1  tron 	return (t);
    203      1.1  tron }
    204      1.1  tron #endif
    205      1.1  tron 
    206      1.1  tron 
    207      1.1  tron #if !HAVE_STRERROR
    208      1.1  tron /*
    209      1.1  tron  * Local version of strerror, if not available from the system.
    210      1.1  tron  */
    211      1.1  tron 	static char *
    212      1.1  tron strerror(err)
    213      1.1  tron 	int err;
    214      1.1  tron {
    215      1.1  tron #if HAVE_SYS_ERRLIST
    216      1.1  tron 	static char buf[16];
    217      1.1  tron 	extern char *sys_errlist[];
    218      1.1  tron 	extern int sys_nerr;
    219      1.1  tron 
    220      1.1  tron 	if (err < sys_nerr)
    221      1.1  tron 		return sys_errlist[err];
    222      1.1  tron 	sprintf(buf, "Error %d", err);
    223      1.1  tron 	return buf;
    224      1.1  tron #else
    225      1.1  tron 	return ("cannot open");
    226      1.1  tron #endif
    227      1.1  tron }
    228      1.1  tron #endif
    229      1.1  tron 
    230      1.1  tron /*
    231      1.1  tron  * errno_message: Return an error message based on the value of "errno".
    232      1.1  tron  */
    233      1.1  tron 	public char *
    234      1.1  tron errno_message(filename)
    235      1.1  tron 	char *filename;
    236      1.1  tron {
    237      1.1  tron 	register char *p;
    238      1.1  tron 	register char *m;
    239      1.1  tron 	int len;
    240      1.1  tron #if HAVE_ERRNO
    241      1.1  tron #if MUST_DEFINE_ERRNO
    242      1.1  tron 	extern int errno;
    243      1.1  tron #endif
    244      1.1  tron 	p = strerror(errno);
    245      1.1  tron #else
    246      1.1  tron 	p = "cannot open";
    247      1.1  tron #endif
    248      1.1  tron 	len = strlen(filename) + strlen(p) + 3;
    249      1.1  tron 	m = (char *) ecalloc(len, sizeof(char));
    250      1.1  tron 	SNPRINTF2(m, len, "%s: %s", filename, p);
    251      1.1  tron 	return (m);
    252      1.1  tron }
    253      1.1  tron 
    254      1.1  tron /* #define HAVE_FLOAT 0 */
    255      1.1  tron 
    256      1.1  tron 	static POSITION
    257      1.1  tron muldiv(val, num, den)
    258      1.1  tron 	POSITION val, num, den;
    259      1.1  tron {
    260      1.1  tron #if HAVE_FLOAT
    261      1.1  tron 	double v = (((double) val) * num) / den;
    262      1.1  tron 	return ((POSITION) (v + 0.5));
    263      1.1  tron #else
    264      1.1  tron 	POSITION v = ((POSITION) val) * num;
    265      1.1  tron 
    266      1.1  tron 	if (v / num == val)
    267      1.1  tron 		/* No overflow */
    268      1.1  tron 		return (POSITION) (v / den);
    269      1.1  tron 	else
    270      1.1  tron 		/* Above calculation overflows;
    271      1.1  tron 		 * use a method that is less precise but won't overflow. */
    272      1.1  tron 		return (POSITION) (val / (den / num));
    273      1.1  tron #endif
    274      1.1  tron }
    275      1.1  tron 
    276      1.1  tron /*
    277      1.1  tron  * Return the ratio of two POSITIONS, as a percentage.
    278      1.1  tron  * {{ Assumes a POSITION is a long int. }}
    279      1.1  tron  */
    280      1.1  tron 	public int
    281      1.1  tron percentage(num, den)
    282      1.1  tron 	POSITION num, den;
    283      1.1  tron {
    284      1.1  tron 	return (int) muldiv(num,  (POSITION) 100, den);
    285      1.1  tron }
    286      1.1  tron 
    287      1.1  tron /*
    288      1.1  tron  * Return the specified percentage of a POSITION.
    289      1.1  tron  */
    290      1.1  tron 	public POSITION
    291      1.1  tron percent_pos(pos, percent, fraction)
    292      1.1  tron 	POSITION pos;
    293      1.1  tron 	int percent;
    294      1.1  tron 	long fraction;
    295      1.1  tron {
    296      1.1  tron 	/* Change percent (parts per 100) to perden (parts per NUM_FRAC_DENOM). */
    297      1.1  tron 	POSITION perden = (percent * (NUM_FRAC_DENOM / 100)) + (fraction / 100);
    298      1.1  tron 
    299      1.1  tron 	if (perden == 0)
    300      1.1  tron 		return (0);
    301      1.1  tron 	return (POSITION) muldiv(pos, perden, (POSITION) NUM_FRAC_DENOM);
    302      1.1  tron }
    303      1.1  tron 
    304      1.1  tron #if !HAVE_STRCHR
    305      1.1  tron /*
    306      1.1  tron  * strchr is used by regexp.c.
    307      1.1  tron  */
    308      1.1  tron 	char *
    309      1.1  tron strchr(s, c)
    310      1.1  tron 	char *s;
    311      1.1  tron 	int c;
    312      1.1  tron {
    313      1.1  tron 	for ( ;  *s != '\0';  s++)
    314      1.1  tron 		if (*s == c)
    315      1.1  tron 			return (s);
    316      1.1  tron 	if (c == '\0')
    317      1.1  tron 		return (s);
    318      1.1  tron 	return (NULL);
    319      1.1  tron }
    320      1.1  tron #endif
    321      1.1  tron 
    322      1.1  tron #if !HAVE_MEMCPY
    323      1.1  tron 	VOID_POINTER
    324      1.1  tron memcpy(dst, src, len)
    325      1.1  tron 	VOID_POINTER dst;
    326      1.1  tron 	VOID_POINTER src;
    327      1.1  tron 	int len;
    328      1.1  tron {
    329      1.1  tron 	char *dstp = (char *) dst;
    330      1.1  tron 	char *srcp = (char *) src;
    331      1.1  tron 	int i;
    332      1.1  tron 
    333      1.1  tron 	for (i = 0;  i < len;  i++)
    334      1.1  tron 		dstp[i] = srcp[i];
    335      1.1  tron 	return (dst);
    336      1.1  tron }
    337      1.1  tron #endif
    338      1.1  tron 
    339      1.1  tron #ifdef _OSK_MWC32
    340      1.1  tron 
    341      1.1  tron /*
    342      1.1  tron  * This implements an ANSI-style intercept setup for Microware C 3.2
    343      1.1  tron  */
    344      1.1  tron 	public int
    345      1.1  tron os9_signal(type, handler)
    346      1.1  tron 	int type;
    347      1.1  tron 	RETSIGTYPE (*handler)();
    348      1.1  tron {
    349      1.1  tron 	intercept(handler);
    350      1.1  tron }
    351      1.1  tron 
    352      1.1  tron #include <sgstat.h>
    353      1.1  tron 
    354      1.1  tron 	int
    355      1.1  tron isatty(f)
    356      1.1  tron 	int f;
    357      1.1  tron {
    358      1.1  tron 	struct sgbuf sgbuf;
    359      1.1  tron 
    360      1.1  tron 	if (_gs_opt(f, &sgbuf) < 0)
    361      1.1  tron 		return -1;
    362      1.1  tron 	return (sgbuf.sg_class == 0);
    363      1.1  tron }
    364      1.1  tron 
    365      1.1  tron #endif
    366