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