xref: /freebsd/contrib/less/os.c (revision ccfd87fe2ac0e2e6aeb1911a7d7cce6712a8564f)
1 /*
2  * Copyright (C) 1984-2023  Mark Nudelman
3  *
4  * You may distribute under the terms of either the GNU General Public
5  * License or the Less License, as specified in the README file.
6  *
7  * For more information, see the README file.
8  */
9 
10 
11 /*
12  * Operating system dependent routines.
13  *
14  * Most of the stuff in here is based on Unix, but an attempt
15  * has been made to make things work on other operating systems.
16  * This will sometimes result in a loss of functionality, unless
17  * someone rewrites code specifically for the new operating system.
18  *
19  * The makefile provides defines to decide whether various
20  * Unix features are present.
21  */
22 
23 #include "less.h"
24 #include <signal.h>
25 #include <setjmp.h>
26 #if MSDOS_COMPILER==WIN32C
27 #include <windows.h>
28 #endif
29 #if HAVE_TIME_H
30 #include <time.h>
31 #endif
32 #if HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35 #if HAVE_VALUES_H
36 #include <values.h>
37 #endif
38 
39 #if defined(__APPLE__)
40 #include <sys/utsname.h>
41 #endif
42 
43 #if HAVE_POLL && !MSDOS_COMPILER
44 #define USE_POLL 1
45 static int use_poll = TRUE;
46 #else
47 #define USE_POLL 0
48 #endif
49 #if USE_POLL
50 #include <poll.h>
51 #endif
52 
53 /*
54  * BSD setjmp() saves (and longjmp() restores) the signal mask.
55  * This costs a system call or two per setjmp(), so if possible we clear the
56  * signal mask with sigsetmask(), and use _setjmp()/_longjmp() instead.
57  * On other systems, setjmp() doesn't affect the signal mask and so
58  * _setjmp() does not exist; we just use setjmp().
59  */
60 #if HAVE__SETJMP && HAVE_SIGSETMASK
61 #define SET_JUMP        _setjmp
62 #define LONG_JUMP       _longjmp
63 #else
64 #define SET_JUMP        setjmp
65 #define LONG_JUMP       longjmp
66 #endif
67 
68 public int reading;
69 public int waiting_for_data;
70 public int consecutive_nulls = 0;
71 
72 /* Milliseconds to wait for data before displaying "waiting for data" message. */
73 static int waiting_for_data_delay = 4000;
74 static jmp_buf read_label;
75 
76 extern int sigs;
77 extern int ignore_eoi;
78 extern int exit_F_on_close;
79 extern int follow_mode;
80 extern int scanning_eof;
81 extern char intr_char;
82 #if !MSDOS_COMPILER
83 extern int tty;
84 #endif
85 #if LESSTEST
86 extern char *ttyin_name;
87 #endif /*LESSTEST*/
88 
89 public void init_poll(void)
90 {
91     char *delay = lgetenv("LESS_DATA_DELAY");
92     int idelay = (delay == NULL) ? 0 : atoi(delay);
93     if (idelay > 0)
94         waiting_for_data_delay = idelay;
95 #if USE_POLL
96 #if defined(__APPLE__)
97 	/* In old versions of MacOS, poll() does not work with /dev/tty. */
98 	struct utsname uts;
99 	if (uname(&uts) < 0 || lstrtoi(uts.release, NULL, 10) < 20)
100 		use_poll = FALSE;
101 #endif
102 #endif
103 }
104 
105 #if USE_POLL
106 /*
107  * Check whether data is available, either from a file/pipe or from the tty.
108  * Return READ_AGAIN if no data currently available, but caller should retry later.
109  * Return READ_INTR to abort F command (forw_loop).
110  * Return 0 if safe to read from fd.
111  */
112 static int check_poll(int fd, int tty)
113 {
114 	struct pollfd poller[2] = { { fd, POLLIN, 0 }, { tty, POLLIN, 0 } };
115 	int timeout = (waiting_for_data && !(scanning_eof && follow_mode == FOLLOW_NAME)) ? -1 : waiting_for_data_delay;
116 	poll(poller, 2, timeout);
117 #if LESSTEST
118 	if (ttyin_name == NULL) /* Check for ^X only on a real tty. */
119 #endif /*LESSTEST*/
120 	{
121 		if (poller[1].revents & POLLIN)
122 		{
123 			LWCHAR ch = getchr();
124 			if (ch == intr_char)
125 				/* Break out of "waiting for data". */
126 				return (READ_INTR);
127 			ungetcc_back(ch);
128 		}
129 	}
130 	if (ignore_eoi && exit_F_on_close && (poller[0].revents & (POLLHUP|POLLIN)) == POLLHUP)
131 		/* Break out of F loop on HUP due to --exit-follow-on-close. */
132 		return (READ_INTR);
133 	if ((poller[0].revents & (POLLIN|POLLHUP|POLLERR)) == 0)
134 		/* No data available; let caller take action, then try again. */
135 		return (READ_AGAIN);
136 	/* There is data (or HUP/ERR) available. Safe to call read() without blocking. */
137 	return (0);
138 }
139 #endif /* USE_POLL */
140 
141 public int supports_ctrl_x(void)
142 {
143 #if USE_POLL
144 	return (use_poll);
145 #else
146 	return (FALSE);
147 #endif /* USE_POLL */
148 }
149 
150 /*
151  * Like read() system call, but is deliberately interruptible.
152  * A call to intread() from a signal handler will interrupt
153  * any pending iread().
154  */
155 public int iread(int fd, unsigned char *buf, unsigned int len)
156 {
157 	int n;
158 
159 start:
160 #if MSDOS_COMPILER==WIN32C
161 	if (ABORT_SIGS())
162 		return (READ_INTR);
163 #else
164 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
165 	if (kbhit())
166 	{
167 		int c;
168 
169 		c = getch();
170 		if (c == '\003')
171 			return (READ_INTR);
172 		ungetch(c);
173 	}
174 #endif
175 #endif
176 	if (!reading && SET_JUMP(read_label))
177 	{
178 		/*
179 		 * We jumped here from intread.
180 		 */
181 		reading = 0;
182 #if HAVE_SIGPROCMASK
183 		{
184 		  sigset_t mask;
185 		  sigemptyset(&mask);
186 		  sigprocmask(SIG_SETMASK, &mask, NULL);
187 		}
188 #else
189 #if HAVE_SIGSETMASK
190 		sigsetmask(0);
191 #else
192 #ifdef _OSK
193 		sigmask(~0);
194 #endif
195 #endif
196 #endif
197 		return (READ_INTR);
198 	}
199 
200 	flush();
201 	reading = 1;
202 #if MSDOS_COMPILER==DJGPPC
203 	if (isatty(fd))
204 	{
205 		/*
206 		 * Don't try reading from a TTY until a character is
207 		 * available, because that makes some background programs
208 		 * believe DOS is busy in a way that prevents those
209 		 * programs from working while "less" waits.
210          * {{ This code was added 12 Jan 2007; still needed? }}
211 		 */
212 		fd_set readfds;
213 
214 		FD_ZERO(&readfds);
215 		FD_SET(fd, &readfds);
216 		if (select(fd+1, &readfds, 0, 0, 0) == -1)
217 		{
218 			reading = 0;
219 			return (READ_ERR);
220 		}
221 	}
222 #endif
223 #if USE_POLL
224 	if (fd != tty && use_poll)
225 	{
226 		int ret = check_poll(fd, tty);
227 		if (ret != 0)
228 		{
229 			if (ret == READ_INTR)
230 				sigs |= S_INTERRUPT;
231 			reading = 0;
232 			return (ret);
233 		}
234 	}
235 #else
236 #if MSDOS_COMPILER==WIN32C
237 	if (win32_kbhit() && WIN32getch() == intr_char)
238 	{
239 		sigs |= S_INTERRUPT;
240 		reading = 0;
241 		return (READ_INTR);
242 	}
243 #endif
244 #endif
245 	n = read(fd, buf, len);
246 	reading = 0;
247 #if 1
248 	/*
249 	 * This is a kludge to workaround a problem on some systems
250 	 * where terminating a remote tty connection causes read() to
251 	 * start returning 0 forever, instead of -1.
252 	 */
253 	{
254 		if (!ignore_eoi)
255 		{
256 			if (n == 0)
257 				consecutive_nulls++;
258 			else
259 				consecutive_nulls = 0;
260 			if (consecutive_nulls > 20)
261 				quit(QUIT_ERROR);
262 		}
263 	}
264 #endif
265 	if (n < 0)
266 	{
267 #if HAVE_ERRNO
268 		/*
269 		 * Certain values of errno indicate we should just retry the read.
270 		 */
271 #if MUST_DEFINE_ERRNO
272 		extern int errno;
273 #endif
274 #ifdef EINTR
275 		if (errno == EINTR)
276 			goto start;
277 #endif
278 #ifdef EAGAIN
279 		if (errno == EAGAIN)
280 			goto start;
281 #endif
282 #endif
283 		return (READ_ERR);
284 	}
285 	return (n);
286 }
287 
288 /*
289  * Interrupt a pending iread().
290  */
291 public void intread(void)
292 {
293 	LONG_JUMP(read_label, 1);
294 }
295 
296 /*
297  * Return the current time.
298  */
299 #if HAVE_TIME
300 public time_type get_time(void)
301 {
302 	time_type t;
303 
304 	time(&t);
305 	return (t);
306 }
307 #endif
308 
309 
310 #if !HAVE_STRERROR
311 /*
312  * Local version of strerror, if not available from the system.
313  */
314 static char * strerror(int err)
315 {
316 	static char buf[INT_STRLEN_BOUND(int)+12];
317 #if HAVE_SYS_ERRLIST
318 	extern char *sys_errlist[];
319 	extern int sys_nerr;
320 
321 	if (err < sys_nerr)
322 		return sys_errlist[err];
323 #endif
324 	sprintf(buf, "Error %d", err);
325 	return buf;
326 }
327 #endif
328 
329 /*
330  * errno_message: Return an error message based on the value of "errno".
331  */
332 public char * errno_message(char *filename)
333 {
334 	char *p;
335 	char *m;
336 	int len;
337 #if HAVE_ERRNO
338 #if MUST_DEFINE_ERRNO
339 	extern int errno;
340 #endif
341 	p = strerror(errno);
342 #else
343 	p = "cannot open";
344 #endif
345 	len = (int) (strlen(filename) + strlen(p) + 3);
346 	m = (char *) ecalloc(len, sizeof(char));
347 	SNPRINTF2(m, len, "%s: %s", filename, p);
348 	return (m);
349 }
350 
351 /*
352  * Return a description of a signal.
353  * The return value is good until the next call to this function.
354  */
355 public char * signal_message(int sig)
356 {
357 	static char sigbuf[sizeof("Signal ") + INT_STRLEN_BOUND(sig) + 1];
358 #if HAVE_STRSIGNAL
359 	char *description = strsignal(sig);
360 	if (description)
361 		return description;
362 #endif
363 	sprintf(sigbuf, "Signal %d", sig);
364 	return sigbuf;
365 }
366 
367 /*
368  * Return (VAL * NUM) / DEN, where DEN is positive
369  * and min(VAL, NUM) <= DEN so the result cannot overflow.
370  * Round to the nearest integer, breaking ties by rounding to even.
371  */
372 public uintmax muldiv(uintmax val, uintmax num, uintmax den)
373 {
374 	/*
375 	 * Like round(val * (double) num / den), but without rounding error.
376 	 * Overflow cannot occur, so there is no need for floating point.
377 	 */
378 	uintmax q = val / den;
379 	uintmax r = val % den;
380 	uintmax qnum = q * num;
381 	uintmax rnum = r * num;
382 	uintmax quot = qnum + rnum / den;
383 	uintmax rem = rnum % den;
384 	return quot + (den / 2 < rem + (quot & ~den & 1));
385 }
386 
387 /*
388  * Return the ratio of two POSITIONS, as a percentage.
389  * {{ Assumes a POSITION is a long int. }}
390  */
391 public int percentage(POSITION num, POSITION den)
392 {
393 	return (int) muldiv(num,  (POSITION) 100, den);
394 }
395 
396 /*
397  * Return the specified percentage of a POSITION.
398  * Assume (0 <= POS && 0 <= PERCENT <= 100
399  *	   && 0 <= FRACTION < (PERCENT == 100 ? 1 : NUM_FRAC_DENOM)),
400  * so the result cannot overflow.  Round to even.
401  */
402 public POSITION percent_pos(POSITION pos, int percent, long fraction)
403 {
404 	/*
405 	 * Change from percent (parts per 100)
406 	 * to pctden (parts per 100 * NUM_FRAC_DENOM).
407 	 */
408 	POSITION pctden = (percent * NUM_FRAC_DENOM) + fraction;
409 
410 	return (POSITION) muldiv(pos, pctden, 100 * (POSITION) NUM_FRAC_DENOM);
411 }
412 
413 #if !HAVE_STRCHR
414 /*
415  * strchr is used by regexp.c.
416  */
417 char * strchr(char *s, char c)
418 {
419 	for ( ;  *s != '\0';  s++)
420 		if (*s == c)
421 			return (s);
422 	if (c == '\0')
423 		return (s);
424 	return (NULL);
425 }
426 #endif
427 
428 #if !HAVE_MEMCPY
429 void * memcpy(void *dst, void *src, int len)
430 {
431 	char *dstp = (char *) dst;
432 	char *srcp = (char *) src;
433 	int i;
434 
435 	for (i = 0;  i < len;  i++)
436 		dstp[i] = srcp[i];
437 	return (dst);
438 }
439 #endif
440 
441 #ifdef _OSK_MWC32
442 
443 /*
444  * This implements an ANSI-style intercept setup for Microware C 3.2
445  */
446 public int os9_signal(int type, RETSIGTYPE (*handler)())
447 {
448 	intercept(handler);
449 }
450 
451 #include <sgstat.h>
452 
453 int isatty(int f)
454 {
455 	struct sgbuf sgbuf;
456 
457 	if (_gs_opt(f, &sgbuf) < 0)
458 		return -1;
459 	return (sgbuf.sg_class == 0);
460 }
461 
462 #endif
463 
464 public void sleep_ms(int ms)
465 {
466 #if MSDOS_COMPILER==WIN32C
467 	Sleep(ms);
468 #else
469 #if HAVE_NANOSLEEP
470 	int sec = ms / 1000;
471 	struct timespec t = { sec, (ms - sec*1000) * 1000000 };
472 	nanosleep(&t, NULL);
473 #else
474 #if HAVE_USLEEP
475 	usleep(ms);
476 #else
477 	sleep(ms / 1000 + (ms % 1000 != 0));
478 #endif
479 #endif
480 #endif
481 }
482