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