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