xref: /netbsd-src/external/bsd/less/dist/less.h (revision 20006a0bde522c99e03e2f0935b37976c345030a)
1 /*	$NetBSD	*/
2 
3 /*
4  * Copyright (C) 1984-2011  Mark Nudelman
5  *
6  * You may distribute under the terms of either the GNU General Public
7  * License or the Less License, as specified in the README file.
8  *
9  * For more information about less, or for information on how to
10  * contact the author, see the README file.
11  */
12 
13 #define NEWBOT 1
14 
15 /*
16  * Standard include file for "less".
17  */
18 
19 /*
20  * Defines for MSDOS_COMPILER.
21  */
22 #define	MSOFTC		1	/* Microsoft C */
23 #define	BORLANDC	2	/* Borland C */
24 #define	WIN32C		3	/* Windows (Borland C or Microsoft C) */
25 #define	DJGPPC		4	/* DJGPP C */
26 
27 /*
28  * Include the file of compile-time options.
29  * The <> make cc search for it in -I., not srcdir.
30  */
31 #include <defines.h>
32 
33 #ifdef _SEQUENT_
34 /*
35  * Kludge for Sequent Dynix systems that have sigsetmask, but
36  * it's not compatible with the way less calls it.
37  * {{ Do other systems need this? }}
38  */
39 #undef HAVE_SIGSETMASK
40 #endif
41 
42 /*
43  * Language details.
44  */
45 #if HAVE_VOID
46 #define	VOID_POINTER	void *
47 #else
48 #define	VOID_POINTER	char *
49 #define	void  int
50 #endif
51 #if HAVE_CONST
52 #define	constant	const
53 #else
54 #define	constant
55 #endif
56 
57 #define	public		/* PUBLIC FUNCTION */
58 
59 /* Library function declarations */
60 
61 #if HAVE_SYS_TYPES_H
62 #include <sys/types.h>
63 #endif
64 #if HAVE_STDIO_H
65 #include <stdio.h>
66 #endif
67 #if HAVE_FCNTL_H
68 #include <fcntl.h>
69 #endif
70 #if HAVE_UNISTD_H
71 #include <unistd.h>
72 #endif
73 #if HAVE_CTYPE_H
74 #include <ctype.h>
75 #endif
76 #if HAVE_WCTYPE_H
77 #include <wctype.h>
78 #endif
79 #if HAVE_LIMITS_H
80 #include <limits.h>
81 #endif
82 #if HAVE_STDLIB_H
83 #include <stdlib.h>
84 #endif
85 #if HAVE_STRING_H
86 #include <string.h>
87 #endif
88 
89 /* OS-specific includes */
90 #ifdef _OSK
91 #include <modes.h>
92 #include <strings.h>
93 #endif
94 
95 #ifdef __TANDEM
96 #include <floss.h>
97 #endif
98 
99 #if MSDOS_COMPILER==WIN32C || OS2
100 #include <io.h>
101 #endif
102 
103 #if MSDOS_COMPILER==DJGPPC
104 #include <io.h>
105 #include <sys/exceptn.h>
106 #include <conio.h>
107 #include <pc.h>
108 #endif
109 
110 #if !HAVE_STDLIB_H
111 char *getenv();
112 off_t lseek();
113 VOID_POINTER calloc();
114 void free();
115 #endif
116 
117 /*
118  * Simple lowercase test which can be used during option processing
119  * (before options are parsed which might tell us what charset to use).
120  */
121 #define ASCII_IS_UPPER(c)	((c) >= 'A' && (c) <= 'Z')
122 #define ASCII_IS_LOWER(c)	((c) >= 'a' && (c) <= 'z')
123 #define	ASCII_TO_UPPER(c)	((c) - 'a' + 'A')
124 #define	ASCII_TO_LOWER(c)	((c) - 'A' + 'a')
125 
126 #undef IS_UPPER
127 #undef IS_LOWER
128 #undef TO_UPPER
129 #undef TO_LOWER
130 #undef IS_SPACE
131 #undef IS_DIGIT
132 
133 #if HAVE_WCTYPE
134 #define	IS_UPPER(c)	iswupper(c)
135 #define	IS_LOWER(c)	iswlower(c)
136 #define	TO_UPPER(c)	towupper(c)
137 #define	TO_LOWER(c)	towlower(c)
138 #else
139 #if HAVE_UPPER_LOWER
140 #define	IS_UPPER(c)	isupper((unsigned char) (c))
141 #define	IS_LOWER(c)	islower((unsigned char) (c))
142 #define	TO_UPPER(c)	toupper((unsigned char) (c))
143 #define	TO_LOWER(c)	tolower((unsigned char) (c))
144 #else
145 #define	IS_UPPER(c)	ASCII_IS_UPPER(c)
146 #define	IS_LOWER(c)	ASCII_IS_LOWER(c)
147 #define	TO_UPPER(c)	ASCII_TO_UPPER(c)
148 #define	TO_LOWER(c)	ASCII_TO_LOWER(c)
149 #endif
150 #endif
151 
152 #ifdef isspace
153 #define IS_SPACE(c)	isspace((unsigned char)(c))
154 #else
155 #define IS_SPACE(c)	((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f')
156 #endif
157 
158 #ifdef isdigit
159 #define IS_DIGIT(c)	isdigit((unsigned char)(c))
160 #else
161 #define IS_DIGIT(c)	((c) >= '0' && (c) <= '9')
162 #endif
163 
164 #define IS_CSI_START(c)	(((LWCHAR)(c)) == ESC || (((LWCHAR)(c)) == CSI))
165 
166 #ifndef NULL
167 #define	NULL	0
168 #endif
169 
170 #ifndef TRUE
171 #define	TRUE		1
172 #endif
173 #ifndef FALSE
174 #define	FALSE		0
175 #endif
176 
177 #define	OPT_OFF		0
178 #define	OPT_ON		1
179 #define	OPT_ONPLUS	2
180 
181 #if !HAVE_MEMCPY
182 #ifndef memcpy
183 #define	memcpy(to,from,len)	bcopy((from),(to),(len))
184 #endif
185 #endif
186 
187 #if HAVE_SNPRINTF
188 #define SNPRINTF1(str, size, fmt, v1)             snprintf((str), (size), (fmt), (v1))
189 #define SNPRINTF2(str, size, fmt, v1, v2)         snprintf((str), (size), (fmt), (v1), (v2))
190 #define SNPRINTF3(str, size, fmt, v1, v2, v3)     snprintf((str), (size), (fmt), (v1), (v2), (v3))
191 #define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) snprintf((str), (size), (fmt), (v1), (v2), (v3), (v4))
192 #else
193 /* Use unsafe sprintf if we don't have snprintf. */
194 #define SNPRINTF1(str, size, fmt, v1)             sprintf((str), (fmt), (v1))
195 #define SNPRINTF2(str, size, fmt, v1, v2)         sprintf((str), (fmt), (v1), (v2))
196 #define SNPRINTF3(str, size, fmt, v1, v2, v3)     sprintf((str), (fmt), (v1), (v2), (v3))
197 #define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) sprintf((str), (fmt), (v1), (v2), (v3), (v4))
198 #endif
199 
200 #define	BAD_LSEEK	((off_t)-1)
201 
202 #ifndef SEEK_SET
203 #define SEEK_SET 0
204 #endif
205 #ifndef SEEK_END
206 #define SEEK_END 2
207 #endif
208 
209 #ifndef CHAR_BIT
210 #define CHAR_BIT 8
211 #endif
212 
213 /*
214  * Upper bound on the string length of an integer converted to string.
215  * 302 / 1000 is ceil (log10 (2.0)).  Subtract 1 for the sign bit;
216  * add 1 for integer division truncation; add 1 more for a minus sign.
217  */
218 #define INT_STRLEN_BOUND(t) ((sizeof(t) * CHAR_BIT - 1) * 302 / 1000 + 1 + 1)
219 
220 /*
221  * Special types and constants.
222  */
223 typedef unsigned long LWCHAR;
224 typedef off_t		POSITION;
225 typedef off_t		LINENUM;
226 #define MIN_LINENUM_WIDTH  7	/* Min printing width of a line number */
227 #define MAX_UTF_CHAR_LEN   6	/* Max bytes in one UTF-8 char */
228 
229 #define	NULL_POSITION	((POSITION)(-1))
230 
231 /*
232  * Flags for open()
233  */
234 #if MSDOS_COMPILER || OS2
235 #define	OPEN_READ	(O_RDONLY|O_BINARY)
236 #else
237 #ifdef _OSK
238 #define	OPEN_READ	(S_IREAD)
239 #else
240 #ifdef O_RDONLY
241 #define	OPEN_READ	(O_RDONLY)
242 #else
243 #define	OPEN_READ	(0)
244 #endif
245 #endif
246 #endif
247 
248 #if defined(O_WRONLY) && defined(O_APPEND)
249 #define	OPEN_APPEND	(O_APPEND|O_WRONLY)
250 #else
251 #ifdef _OSK
252 #define OPEN_APPEND	(S_IWRITE)
253 #else
254 #define	OPEN_APPEND	(1)
255 #endif
256 #endif
257 
258 /*
259  * Set a file descriptor to binary mode.
260  */
261 #if MSDOS_COMPILER==MSOFTC
262 #define	SET_BINARY(f)	_setmode(f, _O_BINARY);
263 #else
264 #if MSDOS_COMPILER || OS2
265 #define	SET_BINARY(f)	setmode(f, O_BINARY)
266 #else
267 #define	SET_BINARY(f)
268 #endif
269 #endif
270 
271 /*
272  * Does the shell treat "?" as a metacharacter?
273  */
274 #if MSDOS_COMPILER || OS2 || _OSK
275 #define	SHELL_META_QUEST 0
276 #else
277 #define	SHELL_META_QUEST 1
278 #endif
279 
280 #define	SPACES_IN_FILENAMES 1
281 
282 /*
283  * An IFILE represents an input file.
284  */
285 #define	IFILE		VOID_POINTER
286 #define	NULL_IFILE	((IFILE)NULL)
287 
288 /*
289  * The structure used to represent a "screen position".
290  * This consists of a file position, and a screen line number.
291  * The meaning is that the line starting at the given file
292  * position is displayed on the ln-th line of the screen.
293  * (Screen lines before ln are empty.)
294  */
295 struct scrpos
296 {
297 	POSITION pos;
298 	int ln;
299 };
300 
301 typedef union parg
302 {
303 	char *p_string;
304 	int p_int;
305 	LINENUM p_linenum;
306 } PARG;
307 
308 #define	NULL_PARG	((PARG *)NULL)
309 
310 struct textlist
311 {
312 	char *string;
313 	char *endstring;
314 };
315 
316 #define	EOI		(-1)
317 
318 #define	READ_INTR	(-2)
319 
320 /* A fraction is represented by an int n; the fraction is n/NUM_FRAC_DENOM */
321 #define NUM_FRAC_DENOM			1000000
322 #define NUM_LOG_FRAC_DENOM		6
323 
324 /* How quiet should we be? */
325 #define	NOT_QUIET	0	/* Ring bell at eof and for errors */
326 #define	LITTLE_QUIET	1	/* Ring bell only for errors */
327 #define	VERY_QUIET	2	/* Never ring bell */
328 
329 /* How should we prompt? */
330 #define	PR_SHORT	0	/* Prompt with colon */
331 #define	PR_MEDIUM	1	/* Prompt with message */
332 #define	PR_LONG		2	/* Prompt with longer message */
333 
334 /* How should we handle backspaces? */
335 #define	BS_SPECIAL	0	/* Do special things for underlining and bold */
336 #define	BS_NORMAL	1	/* \b treated as normal char; actually output */
337 #define	BS_CONTROL	2	/* \b treated as control char; prints as ^H */
338 
339 /* How should we search? */
340 #define	SRCH_FORW       (1 << 0)  /* Search forward from current position */
341 #define	SRCH_BACK       (1 << 1)  /* Search backward from current position */
342 #define SRCH_NO_MOVE    (1 << 2)  /* Highlight, but don't move */
343 #define SRCH_FIND_ALL   (1 << 4)  /* Find and highlight all matches */
344 #define SRCH_NO_MATCH   (1 << 8)  /* Search for non-matching lines */
345 #define SRCH_PAST_EOF   (1 << 9)  /* Search past end-of-file, into next file */
346 #define SRCH_FIRST_FILE (1 << 10) /* Search starting at the first file */
347 #define SRCH_NO_REGEX   (1 << 12) /* Don't use regular expressions */
348 #define SRCH_FILTER     (1 << 13) /* Search is for '&' (filter) command */
349 #define SRCH_AFTER_TARGET (1 << 14) /* Start search after the target line */
350 
351 #define	SRCH_REVERSE(t)	(((t) & SRCH_FORW) ? \
352 				(((t) & ~SRCH_FORW) | SRCH_BACK) : \
353 				(((t) & ~SRCH_BACK) | SRCH_FORW))
354 
355 /* */
356 #define	NO_MCA		0
357 #define	MCA_DONE	1
358 #define	MCA_MORE	2
359 
360 #define	CC_OK		0	/* Char was accepted & processed */
361 #define	CC_QUIT		1	/* Char was a request to abort current cmd */
362 #define	CC_ERROR	2	/* Char could not be accepted due to error */
363 #define	CC_PASS		3	/* Char was rejected (internal) */
364 
365 #define CF_QUIT_ON_ERASE 0001   /* Abort cmd if its entirely erased */
366 
367 /* Special char bit-flags used to tell put_line() to do something special */
368 #define	AT_NORMAL	(0)
369 #define	AT_UNDERLINE	(1 << 0)
370 #define	AT_BOLD		(1 << 1)
371 #define	AT_BLINK	(1 << 2)
372 #define	AT_STANDOUT	(1 << 3)
373 #define	AT_ANSI		(1 << 4)  /* Content-supplied "ANSI" escape sequence */
374 #define	AT_BINARY	(1 << 5)  /* LESS*BINFMT representation */
375 #define	AT_HILITE	(1 << 6)  /* Internal highlights (e.g., for search) */
376 
377 #if '0' == 240
378 #define IS_EBCDIC_HOST 1
379 #endif
380 
381 #if IS_EBCDIC_HOST
382 /*
383  * Long definition for EBCDIC.
384  * Since the argument is usually a constant, this macro normally compiles
385  * into a constant.
386  */
387 #define CONTROL(c) ( \
388 	(c)=='[' ? '\047' : \
389 	(c)=='a' ? '\001' : \
390 	(c)=='b' ? '\002' : \
391 	(c)=='c' ? '\003' : \
392 	(c)=='d' ? '\067' : \
393 	(c)=='e' ? '\055' : \
394 	(c)=='f' ? '\056' : \
395 	(c)=='g' ? '\057' : \
396 	(c)=='h' ? '\026' : \
397 	(c)=='i' ? '\005' : \
398 	(c)=='j' ? '\025' : \
399 	(c)=='k' ? '\013' : \
400 	(c)=='l' ? '\014' : \
401 	(c)=='m' ? '\015' : \
402 	(c)=='n' ? '\016' : \
403 	(c)=='o' ? '\017' : \
404 	(c)=='p' ? '\020' : \
405 	(c)=='q' ? '\021' : \
406 	(c)=='r' ? '\022' : \
407 	(c)=='s' ? '\023' : \
408 	(c)=='t' ? '\074' : \
409 	(c)=='u' ? '\075' : \
410 	(c)=='v' ? '\062' : \
411 	(c)=='w' ? '\046' : \
412 	(c)=='x' ? '\030' : \
413 	(c)=='y' ? '\031' : \
414 	(c)=='z' ? '\077' : \
415 	(c)=='A' ? '\001' : \
416 	(c)=='B' ? '\002' : \
417 	(c)=='C' ? '\003' : \
418 	(c)=='D' ? '\067' : \
419 	(c)=='E' ? '\055' : \
420 	(c)=='F' ? '\056' : \
421 	(c)=='G' ? '\057' : \
422 	(c)=='H' ? '\026' : \
423 	(c)=='I' ? '\005' : \
424 	(c)=='J' ? '\025' : \
425 	(c)=='K' ? '\013' : \
426 	(c)=='L' ? '\014' : \
427 	(c)=='M' ? '\015' : \
428 	(c)=='N' ? '\016' : \
429 	(c)=='O' ? '\017' : \
430 	(c)=='P' ? '\020' : \
431 	(c)=='Q' ? '\021' : \
432 	(c)=='R' ? '\022' : \
433 	(c)=='S' ? '\023' : \
434 	(c)=='T' ? '\074' : \
435 	(c)=='U' ? '\075' : \
436 	(c)=='V' ? '\062' : \
437 	(c)=='W' ? '\046' : \
438 	(c)=='X' ? '\030' : \
439 	(c)=='Y' ? '\031' : \
440 	(c)=='Z' ? '\077' : \
441 	(c)=='|' ? '\031' : \
442 	(c)=='\\' ? '\034' : \
443 	(c)=='^' ? '\036' : \
444 	(c)&077)
445 #else
446 #define	CONTROL(c)	((c)&037)
447 #endif /* IS_EBCDIC_HOST */
448 
449 #define	ESC		CONTROL('[')
450 #define	CSI		((unsigned char)'\233')
451 
452 #if _OSK_MWC32
453 #define	LSIGNAL(sig,func)	os9_signal(sig,func)
454 #else
455 #define	LSIGNAL(sig,func)	signal(sig,func)
456 #endif
457 
458 #if HAVE_SIGPROCMASK
459 #if HAVE_SIGSET_T
460 #else
461 #undef HAVE_SIGPROCMASK
462 #endif
463 #endif
464 #if HAVE_SIGPROCMASK
465 #if HAVE_SIGEMPTYSET
466 #else
467 #undef  sigemptyset
468 #define sigemptyset(mp) *(mp) = 0
469 #endif
470 #endif
471 
472 #define	S_INTERRUPT	01
473 #define	S_STOP		02
474 #define S_WINCH		04
475 #define	ABORT_SIGS()	(sigs & (S_INTERRUPT|S_STOP))
476 
477 #define	QUIT_OK		0
478 #define	QUIT_ERROR	1
479 #define	QUIT_INTERRUPT	2
480 #define	QUIT_SAVED_STATUS (-1)
481 
482 #define FOLLOW_DESC     0
483 #define FOLLOW_NAME     1
484 
485 /* filestate flags */
486 #define	CH_CANSEEK	001
487 #define	CH_KEEPOPEN	002
488 #define	CH_POPENED	004
489 #define	CH_HELPFILE	010
490 
491 #define	ch_zero()	((POSITION)0)
492 
493 #define	FAKE_HELPFILE	"@/\\less/\\help/\\file/\\@"
494 
495 /* Flags for cvt_text */
496 #define	CVT_TO_LC	01	/* Convert upper-case to lower-case */
497 #define	CVT_BS		02	/* Do backspace processing */
498 #define	CVT_CRLF	04	/* Remove CR after LF */
499 #define	CVT_ANSI	010	/* Remove ANSI escape sequences */
500 
501 #include "funcs.h"
502 
503 /* Functions not included in funcs.h */
504 void postoa();
505 void linenumtoa();
506 void inttoa();
507