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