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