xref: /freebsd/contrib/less/less.h (revision c77c488926555ca344ae3a417544cf7a720e1de1)
1a5f0fb15SPaul Saab /*
2*c77c4889SXin LI  * Copyright (C) 1984-2024  Mark Nudelman
3a5f0fb15SPaul Saab  *
4a5f0fb15SPaul Saab  * You may distribute under the terms of either the GNU General Public
5a5f0fb15SPaul Saab  * License or the Less License, as specified in the README file.
6a5f0fb15SPaul Saab  *
796e55cc7SXin LI  * For more information, see the README file.
8a5f0fb15SPaul Saab  */
9a5f0fb15SPaul Saab 
10720c436cSXin LI #define NEWBOT 1
11a5f0fb15SPaul Saab 
12a5f0fb15SPaul Saab /*
13a5f0fb15SPaul Saab  * Standard include file for "less".
14a5f0fb15SPaul Saab  */
15a5f0fb15SPaul Saab 
16a5f0fb15SPaul Saab /*
17a5f0fb15SPaul Saab  * Defines for MSDOS_COMPILER.
18a5f0fb15SPaul Saab  */
19a5f0fb15SPaul Saab #define MSOFTC          1   /* Microsoft C */
20a5f0fb15SPaul Saab #define BORLANDC        2   /* Borland C */
21a5f0fb15SPaul Saab #define WIN32C          3   /* Windows (Borland C or Microsoft C) */
22a5f0fb15SPaul Saab #define DJGPPC          4   /* DJGPP C */
23a5f0fb15SPaul Saab 
24a5f0fb15SPaul Saab /*
25a5f0fb15SPaul Saab  * Include the file of compile-time options.
26a5f0fb15SPaul Saab  * The <> make cc search for it in -I., not srcdir.
27a5f0fb15SPaul Saab  */
28a5f0fb15SPaul Saab #include <defines.h>
29a5f0fb15SPaul Saab 
30a5f0fb15SPaul Saab #ifdef _SEQUENT_
31a5f0fb15SPaul Saab /*
32a5f0fb15SPaul Saab  * Kludge for Sequent Dynix systems that have sigsetmask, but
33a5f0fb15SPaul Saab  * it's not compatible with the way less calls it.
34a5f0fb15SPaul Saab  * {{ Do other systems need this? }}
35a5f0fb15SPaul Saab  */
36a5f0fb15SPaul Saab #undef HAVE_SIGSETMASK
37a5f0fb15SPaul Saab #endif
38a5f0fb15SPaul Saab 
39a5f0fb15SPaul Saab 
40a5f0fb15SPaul Saab /* Library function declarations */
41a5f0fb15SPaul Saab 
42a5f0fb15SPaul Saab #if HAVE_SYS_TYPES_H
43a5f0fb15SPaul Saab #include <sys/types.h>
44a5f0fb15SPaul Saab #endif
45a5f0fb15SPaul Saab #if HAVE_STDIO_H
46a5f0fb15SPaul Saab #include <stdio.h>
47a5f0fb15SPaul Saab #endif
48a5f0fb15SPaul Saab #if HAVE_FCNTL_H
49a5f0fb15SPaul Saab #include <fcntl.h>
50a5f0fb15SPaul Saab #endif
51a5f0fb15SPaul Saab #if HAVE_UNISTD_H
52a5f0fb15SPaul Saab #include <unistd.h>
53a5f0fb15SPaul Saab #endif
54a5f0fb15SPaul Saab #if HAVE_CTYPE_H
55a5f0fb15SPaul Saab #include <ctype.h>
56a5f0fb15SPaul Saab #endif
57423c5ce5SXin LI #if HAVE_WCTYPE_H
58423c5ce5SXin LI #include <wctype.h>
59423c5ce5SXin LI #endif
608fd4165cSPaul Saab #if HAVE_LIMITS_H
618fd4165cSPaul Saab #include <limits.h>
628fd4165cSPaul Saab #endif
63d713e089SXin LI #if HAVE_STDINT_H
64d713e089SXin LI #include <stdint.h>
65d713e089SXin LI #endif
66a5f0fb15SPaul Saab #if HAVE_STDLIB_H
67a5f0fb15SPaul Saab #include <stdlib.h>
68a5f0fb15SPaul Saab #endif
69a5f0fb15SPaul Saab #if HAVE_STRING_H
70a5f0fb15SPaul Saab #include <string.h>
71a5f0fb15SPaul Saab #endif
7289dd99dcSXin LI 
73d713e089SXin LI #if HAVE_STDCKDINT_H
74d713e089SXin LI #include <stdckdint.h>
75d713e089SXin LI #else
76d713e089SXin LI /*
77d713e089SXin LI  * These substitutes for C23 stdckdint macros do not set *R on overflow,
78d713e089SXin LI  * and they assume A and B are nonnegative.  That is good enough for us.
79d713e089SXin LI  */
80*c77c4889SXin LI #define ckd_add(r, a, b) help_ckd_add(r, (uintmax)(a), (uintmax)(b), sizeof *(r), signed_expr(*(r)))
81*c77c4889SXin LI #define ckd_mul(r, a, b) help_ckd_mul(r, (uintmax)(a), (uintmax)(b), sizeof *(r), signed_expr(*(r)))
82d713e089SXin LI /* True if the integer expression E, after promotion, is signed.  */
83d713e089SXin LI #define signed_expr(e) ((TRUE ? 0 : e) - 1 < 0)
84d713e089SXin LI #endif
85*c77c4889SXin LI #define muldiv(val,num,den) umuldiv((uintmax)(val), (uintmax)(num), (uintmax)(den))
86*c77c4889SXin LI 
87*c77c4889SXin LI #include "lang.h"
88d713e089SXin LI 
89d713e089SXin LI #if defined UINTMAX_MAX
90d713e089SXin LI typedef uintmax_t uintmax;
91d713e089SXin LI #elif defined ULLONG_MAX
92d713e089SXin LI typedef unsigned long long uintmax;
93d713e089SXin LI #else
94d713e089SXin LI typedef unsigned long uintmax;
95d713e089SXin LI #endif
96d713e089SXin LI 
9789dd99dcSXin LI /* OS-specific includes */
98a5f0fb15SPaul Saab #ifdef _OSK
99a5f0fb15SPaul Saab #include <modes.h>
100a5f0fb15SPaul Saab #include <strings.h>
101a5f0fb15SPaul Saab #endif
10289dd99dcSXin LI 
10389dd99dcSXin LI #ifdef __TANDEM
10489dd99dcSXin LI #include <floss.h>
10589dd99dcSXin LI #endif
10689dd99dcSXin LI 
1078fd4165cSPaul Saab #if MSDOS_COMPILER==WIN32C || OS2
108a5f0fb15SPaul Saab #include <io.h>
109a5f0fb15SPaul Saab #endif
11089dd99dcSXin LI 
111a5f0fb15SPaul Saab #if MSDOS_COMPILER==DJGPPC
112a5f0fb15SPaul Saab #include <io.h>
113a5f0fb15SPaul Saab #include <sys/exceptn.h>
114a5f0fb15SPaul Saab #include <conio.h>
115a5f0fb15SPaul Saab #include <pc.h>
116a5f0fb15SPaul Saab #endif
117a5f0fb15SPaul Saab 
118a5f0fb15SPaul Saab #if !HAVE_STDLIB_H
119a5f0fb15SPaul Saab char *getenv();
120a5f0fb15SPaul Saab off_t lseek();
121d713e089SXin LI void *calloc();
122a5f0fb15SPaul Saab void free();
123a5f0fb15SPaul Saab #endif
124a5f0fb15SPaul Saab 
125a5f0fb15SPaul Saab /*
126a5f0fb15SPaul Saab  * Simple lowercase test which can be used during option processing
127a5f0fb15SPaul Saab  * (before options are parsed which might tell us what charset to use).
128a5f0fb15SPaul Saab  */
12989dd99dcSXin LI #define ASCII_IS_UPPER(c)       ((c) >= 'A' && (c) <= 'Z')
13089dd99dcSXin LI #define ASCII_IS_LOWER(c)       ((c) >= 'a' && (c) <= 'z')
13189dd99dcSXin LI #define ASCII_TO_UPPER(c)       ((c) - 'a' + 'A')
13289dd99dcSXin LI #define ASCII_TO_LOWER(c)       ((c) - 'A' + 'a')
13389dd99dcSXin LI 
13489dd99dcSXin LI #undef IS_UPPER
13589dd99dcSXin LI #undef IS_LOWER
13689dd99dcSXin LI #undef TO_UPPER
13789dd99dcSXin LI #undef TO_LOWER
13889dd99dcSXin LI #undef IS_SPACE
13989dd99dcSXin LI #undef IS_DIGIT
140a5f0fb15SPaul Saab 
141423c5ce5SXin LI #if HAVE_WCTYPE
142*c77c4889SXin LI #define IS_UPPER(c)     iswupper((wint_t) (c))
143*c77c4889SXin LI #define IS_LOWER(c)     iswlower((wint_t) (c))
144*c77c4889SXin LI #define TO_UPPER(c)     towupper((wint_t) (c))
145*c77c4889SXin LI #define TO_LOWER(c)     towlower((wint_t) (c))
14689dd99dcSXin LI #else
147423c5ce5SXin LI #if HAVE_UPPER_LOWER
148*c77c4889SXin LI #define IS_UPPER(c)     (is_ascii_char(c) && isupper((unsigned char) (c)))
149*c77c4889SXin LI #define IS_LOWER(c)     (is_ascii_char(c) && islower((unsigned char) (c)))
150*c77c4889SXin LI #define TO_UPPER(c)     (is_ascii_char(c) ? toupper((unsigned char) (c)) : (c))
151*c77c4889SXin LI #define TO_LOWER(c)     (is_ascii_char(c) ? tolower((unsigned char) (c)) : (c))
152423c5ce5SXin LI #else
153*c77c4889SXin LI #define IS_UPPER(c)     (is_ascii_char(c) && ASCII_IS_UPPER(c))
154*c77c4889SXin LI #define IS_LOWER(c)     (is_ascii_char(c) && ASCII_IS_LOWER(c))
155*c77c4889SXin LI #define TO_UPPER(c)     (is_ascii_char(c) ? ASCII_TO_UPPER(c) : (c))
156*c77c4889SXin LI #define TO_LOWER(c)     (is_ascii_char(c) ? ASCII_TO_LOWER(c) : (c))
157423c5ce5SXin LI #endif
15889dd99dcSXin LI #endif
15989dd99dcSXin LI 
16089dd99dcSXin LI #ifdef isspace
16189dd99dcSXin LI #define IS_SPACE(c)     isspace((unsigned char)(c))
16289dd99dcSXin LI #else
16389dd99dcSXin LI #define IS_SPACE(c)     ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f')
16489dd99dcSXin LI #endif
16589dd99dcSXin LI 
16689dd99dcSXin LI #ifdef isdigit
16789dd99dcSXin LI #define IS_DIGIT(c)     isdigit((unsigned char)(c))
16889dd99dcSXin LI #else
16989dd99dcSXin LI #define IS_DIGIT(c)     ((c) >= '0' && (c) <= '9')
170a5f0fb15SPaul Saab #endif
171a5f0fb15SPaul Saab 
1727374caaaSXin LI #define IS_CSI_START(c) (((LWCHAR)(c)) == ESC || (((LWCHAR)(c)) == CSI))
17359a2d077SXin LI 
174a5f0fb15SPaul Saab #define OPT_OFF         0
175a5f0fb15SPaul Saab #define OPT_ON          1
176a5f0fb15SPaul Saab #define OPT_ONPLUS      2
177a5f0fb15SPaul Saab 
178a5f0fb15SPaul Saab #if !HAVE_MEMCPY
179a5f0fb15SPaul Saab #ifndef memcpy
180a5f0fb15SPaul Saab #define memcpy(to,from,len)     bcopy((from),(to),(len))
181a5f0fb15SPaul Saab #endif
182a5f0fb15SPaul Saab #endif
183a5f0fb15SPaul Saab 
18489dd99dcSXin LI #if HAVE_SNPRINTF
18589dd99dcSXin LI #define SNPRINTF1(str, size, fmt, v1)             snprintf((str), (size), (fmt), (v1))
18689dd99dcSXin LI #define SNPRINTF2(str, size, fmt, v1, v2)         snprintf((str), (size), (fmt), (v1), (v2))
18789dd99dcSXin LI #define SNPRINTF3(str, size, fmt, v1, v2, v3)     snprintf((str), (size), (fmt), (v1), (v2), (v3))
18889dd99dcSXin LI #define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) snprintf((str), (size), (fmt), (v1), (v2), (v3), (v4))
18989dd99dcSXin LI #else
19089dd99dcSXin LI /* Use unsafe sprintf if we don't have snprintf. */
19189dd99dcSXin LI #define SNPRINTF1(str, size, fmt, v1)             sprintf((str), (fmt), (v1))
19289dd99dcSXin LI #define SNPRINTF2(str, size, fmt, v1, v2)         sprintf((str), (fmt), (v1), (v2))
19389dd99dcSXin LI #define SNPRINTF3(str, size, fmt, v1, v2, v3)     sprintf((str), (fmt), (v1), (v2), (v3))
19489dd99dcSXin LI #define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) sprintf((str), (fmt), (v1), (v2), (v3), (v4))
19589dd99dcSXin LI #endif
19689dd99dcSXin LI 
197a5f0fb15SPaul Saab #define BAD_LSEEK       ((off_t)-1)
198a5f0fb15SPaul Saab 
199423c5ce5SXin LI #ifndef SEEK_SET
200423c5ce5SXin LI #define SEEK_SET 0
201423c5ce5SXin LI #endif
202423c5ce5SXin LI #ifndef SEEK_END
203423c5ce5SXin LI #define SEEK_END 2
204423c5ce5SXin LI #endif
205423c5ce5SXin LI 
2068fd4165cSPaul Saab #ifndef CHAR_BIT
2078fd4165cSPaul Saab #define CHAR_BIT 8
2088fd4165cSPaul Saab #endif
2098fd4165cSPaul Saab 
2108fd4165cSPaul Saab /*
2118fd4165cSPaul Saab  * Upper bound on the string length of an integer converted to string.
2128fd4165cSPaul Saab  * 302 / 1000 is ceil (log10 (2.0)).  Subtract 1 for the sign bit;
2138fd4165cSPaul Saab  * add 1 for integer division truncation; add 1 more for a minus sign.
2148fd4165cSPaul Saab  */
2158fd4165cSPaul Saab #define INT_STRLEN_BOUND(t) ((sizeof(t) * CHAR_BIT - 1) * 302 / 1000 + 1 + 1)
2168fd4165cSPaul Saab 
217a5f0fb15SPaul Saab /*
218a5f0fb15SPaul Saab  * Special types and constants.
219a5f0fb15SPaul Saab  */
22089dd99dcSXin LI typedef unsigned long LWCHAR;
221*c77c4889SXin LI #if defined(MINGW) || (defined(_MSC_VER) && _MSC_VER >= 1500)
222*c77c4889SXin LI typedef long long less_off_t;  /* __int64 */
223*c77c4889SXin LI typedef struct _stat64 less_stat_t;
224*c77c4889SXin LI #define less_fstat _fstat64
225*c77c4889SXin LI #define less_stat _stat64
226*c77c4889SXin LI #define less_lseek _lseeki64
227*c77c4889SXin LI #else
228*c77c4889SXin LI typedef off_t less_off_t;
229*c77c4889SXin LI typedef struct stat less_stat_t;
230*c77c4889SXin LI #define less_fstat fstat
231*c77c4889SXin LI #define less_stat stat
232*c77c4889SXin LI #define less_lseek lseek
233*c77c4889SXin LI #endif
234*c77c4889SXin LI typedef less_off_t      POSITION;
2351ede1615STim J. Robbins typedef off_t           LINENUM;
2362235c7feSXin LI #define MIN_LINENUM_WIDTH   7   /* Default min printing width of a line number */
2372235c7feSXin LI #define MAX_LINENUM_WIDTH   16  /* Max width of a line number */
2382235c7feSXin LI #define MAX_STATUSCOL_WIDTH 4   /* Max width of the status column */
23989dd99dcSXin LI #define MAX_UTF_CHAR_LEN    6   /* Max bytes in one UTF-8 char */
24095270f73SXin LI #define MAX_PRCHAR_LEN      31  /* Max chars in prchar() result */
241a5f0fb15SPaul Saab 
242a5f0fb15SPaul Saab #define NULL_POSITION   ((POSITION)(-1))
243a5f0fb15SPaul Saab 
244a5f0fb15SPaul Saab /*
245a5f0fb15SPaul Saab  * Flags for open()
246a5f0fb15SPaul Saab  */
247a5f0fb15SPaul Saab #if MSDOS_COMPILER || OS2
248a5f0fb15SPaul Saab #define OPEN_READ       (O_RDONLY|O_BINARY)
249a5f0fb15SPaul Saab #else
250a5f0fb15SPaul Saab #ifdef _OSK
251a5f0fb15SPaul Saab #define OPEN_READ       (S_IREAD)
252a5f0fb15SPaul Saab #else
253a5f0fb15SPaul Saab #ifdef O_RDONLY
254a5f0fb15SPaul Saab #define OPEN_READ       (O_RDONLY)
255a5f0fb15SPaul Saab #else
256a5f0fb15SPaul Saab #define OPEN_READ       (0)
257a5f0fb15SPaul Saab #endif
258a5f0fb15SPaul Saab #endif
259a5f0fb15SPaul Saab #endif
260a5f0fb15SPaul Saab 
261a5f0fb15SPaul Saab #if defined(O_WRONLY) && defined(O_APPEND)
262a5f0fb15SPaul Saab #define OPEN_APPEND     (O_APPEND|O_WRONLY)
263a5f0fb15SPaul Saab #else
264a5f0fb15SPaul Saab #ifdef _OSK
265a5f0fb15SPaul Saab #define OPEN_APPEND     (S_IWRITE)
266a5f0fb15SPaul Saab #else
267a5f0fb15SPaul Saab #define OPEN_APPEND     (1)
268a5f0fb15SPaul Saab #endif
269a5f0fb15SPaul Saab #endif
270a5f0fb15SPaul Saab 
271a5f0fb15SPaul Saab /*
272f80a33eaSXin LI  * Flags for creat()
273f80a33eaSXin LI  */
274f80a33eaSXin LI #if MSDOS_COMPILER
275f80a33eaSXin LI #define CREAT_RW        (S_IREAD|S_IWRITE)
276f80a33eaSXin LI #else
277f80a33eaSXin LI #define CREAT_RW        0644
278f80a33eaSXin LI #endif
279f80a33eaSXin LI 
280f80a33eaSXin LI /*
281a5f0fb15SPaul Saab  * Set a file descriptor to binary mode.
282a5f0fb15SPaul Saab  */
283a5f0fb15SPaul Saab #if MSDOS_COMPILER==MSOFTC
284a5f0fb15SPaul Saab #define SET_BINARY(f)   _setmode(f, _O_BINARY);
285a5f0fb15SPaul Saab #else
2868fd4165cSPaul Saab #if MSDOS_COMPILER || OS2
287a5f0fb15SPaul Saab #define SET_BINARY(f)   setmode(f, O_BINARY)
288a5f0fb15SPaul Saab #else
289a5f0fb15SPaul Saab #define SET_BINARY(f)
290a5f0fb15SPaul Saab #endif
291a5f0fb15SPaul Saab #endif
292a5f0fb15SPaul Saab 
293a5f0fb15SPaul Saab /*
294a5f0fb15SPaul Saab  * Does the shell treat "?" as a metacharacter?
295a5f0fb15SPaul Saab  */
296a5f0fb15SPaul Saab #if MSDOS_COMPILER || OS2 || _OSK
297a5f0fb15SPaul Saab #define SHELL_META_QUEST 0
298a5f0fb15SPaul Saab #else
299a5f0fb15SPaul Saab #define SHELL_META_QUEST 1
300a5f0fb15SPaul Saab #endif
301a5f0fb15SPaul Saab 
302a5f0fb15SPaul Saab #define SPACES_IN_FILENAMES 1
303a5f0fb15SPaul Saab 
304a5f0fb15SPaul Saab /*
305a5f0fb15SPaul Saab  * An IFILE represents an input file.
306a5f0fb15SPaul Saab  */
307d713e089SXin LI #define IFILE           void*
308a5f0fb15SPaul Saab #define NULL_IFILE      ((IFILE)NULL)
309a5f0fb15SPaul Saab 
310a5f0fb15SPaul Saab /*
311a5f0fb15SPaul Saab  * The structure used to represent a "screen position".
312a5f0fb15SPaul Saab  * This consists of a file position, and a screen line number.
313a5f0fb15SPaul Saab  * The meaning is that the line starting at the given file
314a5f0fb15SPaul Saab  * position is displayed on the ln-th line of the screen.
315a5f0fb15SPaul Saab  * (Screen lines before ln are empty.)
316a5f0fb15SPaul Saab  */
317a5f0fb15SPaul Saab struct scrpos
318a5f0fb15SPaul Saab {
319a5f0fb15SPaul Saab         POSITION pos;
320a5f0fb15SPaul Saab         int ln;
321a5f0fb15SPaul Saab };
322a5f0fb15SPaul Saab 
323a5f0fb15SPaul Saab typedef union parg
324a5f0fb15SPaul Saab {
325*c77c4889SXin LI         constant char *p_string;
326a5f0fb15SPaul Saab         int p_int;
3271ede1615STim J. Robbins         LINENUM p_linenum;
3282235c7feSXin LI         char p_char;
329a5f0fb15SPaul Saab } PARG;
330a5f0fb15SPaul Saab 
331a5f0fb15SPaul Saab #define NULL_PARG       ((PARG *)NULL)
332a5f0fb15SPaul Saab 
333a5f0fb15SPaul Saab struct textlist
334a5f0fb15SPaul Saab {
335a5f0fb15SPaul Saab         char *string;
336a5f0fb15SPaul Saab         char *endstring;
337a5f0fb15SPaul Saab };
338a5f0fb15SPaul Saab 
339a15691bfSXin LI struct wchar_range
340a15691bfSXin LI {
341a15691bfSXin LI         LWCHAR first, last;
342a15691bfSXin LI };
343a15691bfSXin LI 
344a15691bfSXin LI struct wchar_range_table
345a15691bfSXin LI {
346a15691bfSXin LI 	struct wchar_range *table;
347*c77c4889SXin LI 	unsigned int count;
348a15691bfSXin LI };
349a15691bfSXin LI 
350d713e089SXin LI #if HAVE_POLL
351d713e089SXin LI typedef short POLL_EVENTS;
352d713e089SXin LI #endif
353d713e089SXin LI 
354a5f0fb15SPaul Saab #define EOI             (-1)
355a5f0fb15SPaul Saab 
356d713e089SXin LI #define READ_ERR        (-1)
357a5f0fb15SPaul Saab #define READ_INTR       (-2)
358d713e089SXin LI #define READ_AGAIN      (-3)
359a5f0fb15SPaul Saab 
360d713e089SXin LI /*
361d713e089SXin LI  * A fraction is represented by a long n; the fraction is n/NUM_FRAC_DENOM.
362d713e089SXin LI  * To avoid overflow problems, 0 <= n < NUM_FRAC_DENUM <= LONG_MAX/100.
363d713e089SXin LI  */
364720c436cSXin LI #define NUM_FRAC_DENOM                  1000000
365720c436cSXin LI #define NUM_LOG_FRAC_DENOM              6
366720c436cSXin LI 
367a5f0fb15SPaul Saab /* How quiet should we be? */
368a5f0fb15SPaul Saab #define NOT_QUIET       0       /* Ring bell at eof and for errors */
369a5f0fb15SPaul Saab #define LITTLE_QUIET    1       /* Ring bell only for errors */
370a5f0fb15SPaul Saab #define VERY_QUIET      2       /* Never ring bell */
371a5f0fb15SPaul Saab 
372a5f0fb15SPaul Saab /* How should we prompt? */
373a5f0fb15SPaul Saab #define PR_SHORT        0       /* Prompt with colon */
374a5f0fb15SPaul Saab #define PR_MEDIUM       1       /* Prompt with message */
375a5f0fb15SPaul Saab #define PR_LONG         2       /* Prompt with longer message */
376a5f0fb15SPaul Saab 
377a5f0fb15SPaul Saab /* How should we handle backspaces? */
378a5f0fb15SPaul Saab #define BS_SPECIAL      0       /* Do special things for underlining and bold */
379a5f0fb15SPaul Saab #define BS_NORMAL       1       /* \b treated as normal char; actually output */
380a5f0fb15SPaul Saab #define BS_CONTROL      2       /* \b treated as control char; prints as ^H */
381a5f0fb15SPaul Saab 
382a5f0fb15SPaul Saab /* How should we search? */
38389dd99dcSXin LI #define SRCH_FORW       (1 << 0)  /* Search forward from current position */
38489dd99dcSXin LI #define SRCH_BACK       (1 << 1)  /* Search backward from current position */
38589dd99dcSXin LI #define SRCH_NO_MOVE    (1 << 2)  /* Highlight, but don't move */
3862235c7feSXin LI #define SRCH_INCR       (1 << 3)  /* Incremental search */
38789dd99dcSXin LI #define SRCH_FIND_ALL   (1 << 4)  /* Find and highlight all matches */
38889dd99dcSXin LI #define SRCH_NO_MATCH   (1 << 8)  /* Search for non-matching lines */
38989dd99dcSXin LI #define SRCH_PAST_EOF   (1 << 9)  /* Search past end-of-file, into next file */
39089dd99dcSXin LI #define SRCH_FIRST_FILE (1 << 10) /* Search starting at the first file */
39189dd99dcSXin LI #define SRCH_NO_REGEX   (1 << 12) /* Don't use regular expressions */
3927374caaaSXin LI #define SRCH_FILTER     (1 << 13) /* Search is for '&' (filter) command */
39333096f16SXin LI #define SRCH_AFTER_TARGET (1 << 14) /* Start search after the target line */
3942235c7feSXin LI #define SRCH_WRAP       (1 << 15) /* Wrap-around search (continue at BOF/EOF) */
395*c77c4889SXin LI #if OSC8_LINK
396*c77c4889SXin LI #define SRCH_OSC8       (1 << 16) /* */
397*c77c4889SXin LI #endif
398*c77c4889SXin LI #define SRCH_SUBSEARCH(i) (1 << (17+(i))) /* Search for subpattern */
399d713e089SXin LI /* {{ Depends on NUM_SEARCH_COLORS==5 }} */
400d713e089SXin LI #define SRCH_SUBSEARCH_ALL (SRCH_SUBSEARCH(1)|SRCH_SUBSEARCH(2)|SRCH_SUBSEARCH(3)|SRCH_SUBSEARCH(4)|SRCH_SUBSEARCH(5))
401a5f0fb15SPaul Saab 
402a5f0fb15SPaul Saab #define SRCH_REVERSE(t) (((t) & SRCH_FORW) ? \
403a5f0fb15SPaul Saab                                 (((t) & ~SRCH_FORW) | SRCH_BACK) : \
404a5f0fb15SPaul Saab                                 (((t) & ~SRCH_BACK) | SRCH_FORW))
405*c77c4889SXin LI /* Parsing position in an OSC8 link: "\e]8;PARAMS;URI\e\\" (final "\e\\" may be "\7") */
406*c77c4889SXin LI typedef enum osc8_state {
407*c77c4889SXin LI 	OSC8_NOT,     /* This is not an OSC8 link */
408*c77c4889SXin LI 	OSC8_PREFIX,  /* In the "\e]8;" */
409*c77c4889SXin LI 	OSC8_PARAMS,  /* In the parameters */
410*c77c4889SXin LI 	OSC8_URI,     /* In the URI */
411*c77c4889SXin LI 	OSC8_ST_ESC,  /* After the final \e */
412*c77c4889SXin LI 	OSC8_END,     /* At end */
413*c77c4889SXin LI } osc8_state;
414a5f0fb15SPaul Saab 
415a5f0fb15SPaul Saab /* */
416a5f0fb15SPaul Saab #define NO_MCA          0
417a5f0fb15SPaul Saab #define MCA_DONE        1
418a5f0fb15SPaul Saab #define MCA_MORE        2
419a5f0fb15SPaul Saab 
420a5f0fb15SPaul Saab #define CC_OK           0       /* Char was accepted & processed */
421a5f0fb15SPaul Saab #define CC_QUIT         1       /* Char was a request to abort current cmd */
422a5f0fb15SPaul Saab #define CC_ERROR        2       /* Char could not be accepted due to error */
423a5f0fb15SPaul Saab #define CC_PASS         3       /* Char was rejected (internal) */
424a5f0fb15SPaul Saab 
425a5f0fb15SPaul Saab #define CF_QUIT_ON_ERASE 0001   /* Abort cmd if its entirely erased */
426a5f0fb15SPaul Saab 
42789dd99dcSXin LI /* Special char bit-flags used to tell put_line() to do something special */
428a5f0fb15SPaul Saab #define AT_NORMAL       (0)
42989dd99dcSXin LI #define AT_UNDERLINE    (1 << 0)
43089dd99dcSXin LI #define AT_BOLD         (1 << 1)
43189dd99dcSXin LI #define AT_BLINK        (1 << 2)
43289dd99dcSXin LI #define AT_STANDOUT     (1 << 3)
43389dd99dcSXin LI #define AT_ANSI         (1 << 4)  /* Content-supplied "ANSI" escape sequence */
43489dd99dcSXin LI #define AT_BINARY       (1 << 5)  /* LESS*BINFMT representation */
43589dd99dcSXin LI #define AT_HILITE       (1 << 6)  /* Internal highlights (e.g., for search) */
436a5f0fb15SPaul Saab 
4372235c7feSXin LI #define AT_COLOR_SHIFT    8
4382235c7feSXin LI #define AT_NUM_COLORS     16
4392235c7feSXin LI #define AT_COLOR          ((AT_NUM_COLORS-1) << AT_COLOR_SHIFT)
4402235c7feSXin LI #define AT_COLOR_ATTN     (1 << AT_COLOR_SHIFT)
4412235c7feSXin LI #define AT_COLOR_BIN      (2 << AT_COLOR_SHIFT)
4422235c7feSXin LI #define AT_COLOR_CTRL     (3 << AT_COLOR_SHIFT)
4432235c7feSXin LI #define AT_COLOR_ERROR    (4 << AT_COLOR_SHIFT)
4442235c7feSXin LI #define AT_COLOR_LINENUM  (5 << AT_COLOR_SHIFT)
4452235c7feSXin LI #define AT_COLOR_MARK     (6 << AT_COLOR_SHIFT)
4462235c7feSXin LI #define AT_COLOR_PROMPT   (7 << AT_COLOR_SHIFT)
4472235c7feSXin LI #define AT_COLOR_RSCROLL  (8 << AT_COLOR_SHIFT)
448d713e089SXin LI #define AT_COLOR_HEADER   (9 << AT_COLOR_SHIFT)
449d713e089SXin LI #define AT_COLOR_SEARCH   (10 << AT_COLOR_SHIFT)
450d713e089SXin LI #define AT_COLOR_SUBSEARCH(i) ((10+(i)) << AT_COLOR_SHIFT)
451d713e089SXin LI #define NUM_SEARCH_COLORS (AT_NUM_COLORS-10-1)
4522235c7feSXin LI 
4532235c7feSXin LI typedef enum { CT_NULL, CT_4BIT, CT_6BIT } COLOR_TYPE;
4542235c7feSXin LI 
4552235c7feSXin LI typedef enum {
4562235c7feSXin LI 	CV_BLUE     = 1,
4572235c7feSXin LI 	CV_GREEN    = 2,
4582235c7feSXin LI 	CV_RED      = 4,
4592235c7feSXin LI 	CV_BRIGHT   = 8,
4602235c7feSXin LI 	CV_NOCHANGE = -2,
4612235c7feSXin LI 	CV_ERROR    = -1
4622235c7feSXin LI } COLOR_VALUE;
4632235c7feSXin LI 
464*c77c4889SXin LI typedef enum {
465*c77c4889SXin LI 	CATTR_NULL       = 0,
466*c77c4889SXin LI 	CATTR_STANDOUT   = (1 << 0),
467*c77c4889SXin LI 	CATTR_BOLD       = (1 << 1),
468*c77c4889SXin LI 	CATTR_UNDERLINE  = (1 << 2),
469*c77c4889SXin LI 	CATTR_BLINK      = (1 << 3),
470*c77c4889SXin LI } CHAR_ATTR;
471*c77c4889SXin LI 
4722235c7feSXin LI /* ANSI states */
473*c77c4889SXin LI typedef enum {
474*c77c4889SXin LI 	ANSI_NULL,
475*c77c4889SXin LI 	ANSI_MID,
476*c77c4889SXin LI 	ANSI_ERR,
477*c77c4889SXin LI 	ANSI_END,
478*c77c4889SXin LI } ansi_state;
4792235c7feSXin LI 
4808fd4165cSPaul Saab #if '0' == 240
4818fd4165cSPaul Saab #define IS_EBCDIC_HOST 1
4828fd4165cSPaul Saab #endif
4838fd4165cSPaul Saab 
484a5f0fb15SPaul Saab #if IS_EBCDIC_HOST
485a5f0fb15SPaul Saab /*
486a5f0fb15SPaul Saab  * Long definition for EBCDIC.
487a5f0fb15SPaul Saab  * Since the argument is usually a constant, this macro normally compiles
488a5f0fb15SPaul Saab  * into a constant.
489a5f0fb15SPaul Saab  */
490a5f0fb15SPaul Saab #define CONTROL(c) ( \
491a5f0fb15SPaul Saab         (c)=='[' ? '\047' : \
492a5f0fb15SPaul Saab         (c)=='a' ? '\001' : \
493a5f0fb15SPaul Saab         (c)=='b' ? '\002' : \
494a5f0fb15SPaul Saab         (c)=='c' ? '\003' : \
495a5f0fb15SPaul Saab         (c)=='d' ? '\067' : \
496a5f0fb15SPaul Saab         (c)=='e' ? '\055' : \
497a5f0fb15SPaul Saab         (c)=='f' ? '\056' : \
498a5f0fb15SPaul Saab         (c)=='g' ? '\057' : \
499a5f0fb15SPaul Saab         (c)=='h' ? '\026' : \
500a5f0fb15SPaul Saab         (c)=='i' ? '\005' : \
501a5f0fb15SPaul Saab         (c)=='j' ? '\025' : \
502a5f0fb15SPaul Saab         (c)=='k' ? '\013' : \
503a5f0fb15SPaul Saab         (c)=='l' ? '\014' : \
504a5f0fb15SPaul Saab         (c)=='m' ? '\015' : \
505a5f0fb15SPaul Saab         (c)=='n' ? '\016' : \
506a5f0fb15SPaul Saab         (c)=='o' ? '\017' : \
507a5f0fb15SPaul Saab         (c)=='p' ? '\020' : \
508a5f0fb15SPaul Saab         (c)=='q' ? '\021' : \
509a5f0fb15SPaul Saab         (c)=='r' ? '\022' : \
510a5f0fb15SPaul Saab         (c)=='s' ? '\023' : \
511a5f0fb15SPaul Saab         (c)=='t' ? '\074' : \
512a5f0fb15SPaul Saab         (c)=='u' ? '\075' : \
513a5f0fb15SPaul Saab         (c)=='v' ? '\062' : \
514a5f0fb15SPaul Saab         (c)=='w' ? '\046' : \
515a5f0fb15SPaul Saab         (c)=='x' ? '\030' : \
516a5f0fb15SPaul Saab         (c)=='y' ? '\031' : \
517a5f0fb15SPaul Saab         (c)=='z' ? '\077' : \
518a5f0fb15SPaul Saab         (c)=='A' ? '\001' : \
519a5f0fb15SPaul Saab         (c)=='B' ? '\002' : \
520a5f0fb15SPaul Saab         (c)=='C' ? '\003' : \
521a5f0fb15SPaul Saab         (c)=='D' ? '\067' : \
522a5f0fb15SPaul Saab         (c)=='E' ? '\055' : \
523a5f0fb15SPaul Saab         (c)=='F' ? '\056' : \
524a5f0fb15SPaul Saab         (c)=='G' ? '\057' : \
525a5f0fb15SPaul Saab         (c)=='H' ? '\026' : \
526a5f0fb15SPaul Saab         (c)=='I' ? '\005' : \
527a5f0fb15SPaul Saab         (c)=='J' ? '\025' : \
528a5f0fb15SPaul Saab         (c)=='K' ? '\013' : \
529a5f0fb15SPaul Saab         (c)=='L' ? '\014' : \
530a5f0fb15SPaul Saab         (c)=='M' ? '\015' : \
531a5f0fb15SPaul Saab         (c)=='N' ? '\016' : \
532a5f0fb15SPaul Saab         (c)=='O' ? '\017' : \
533a5f0fb15SPaul Saab         (c)=='P' ? '\020' : \
534a5f0fb15SPaul Saab         (c)=='Q' ? '\021' : \
535a5f0fb15SPaul Saab         (c)=='R' ? '\022' : \
536a5f0fb15SPaul Saab         (c)=='S' ? '\023' : \
537a5f0fb15SPaul Saab         (c)=='T' ? '\074' : \
538a5f0fb15SPaul Saab         (c)=='U' ? '\075' : \
539a5f0fb15SPaul Saab         (c)=='V' ? '\062' : \
540a5f0fb15SPaul Saab         (c)=='W' ? '\046' : \
541a5f0fb15SPaul Saab         (c)=='X' ? '\030' : \
542a5f0fb15SPaul Saab         (c)=='Y' ? '\031' : \
543a5f0fb15SPaul Saab         (c)=='Z' ? '\077' : \
544a5f0fb15SPaul Saab         (c)=='|' ? '\031' : \
545a5f0fb15SPaul Saab         (c)=='\\' ? '\034' : \
546a5f0fb15SPaul Saab         (c)=='^' ? '\036' : \
547a5f0fb15SPaul Saab         (c)&077)
548a5f0fb15SPaul Saab #else
549a5f0fb15SPaul Saab #define CONTROL(c)      ((c)&037)
550a5f0fb15SPaul Saab #endif /* IS_EBCDIC_HOST */
551a5f0fb15SPaul Saab 
552a5f0fb15SPaul Saab #define ESC             CONTROL('[')
553b7780dbeSXin LI #define ESCS            "\33"
55459a2d077SXin LI #define CSI             ((unsigned char)'\233')
555a5f0fb15SPaul Saab 
556a5f0fb15SPaul Saab #if _OSK_MWC32
557a5f0fb15SPaul Saab #define LSIGNAL(sig,func)       os9_signal(sig,func)
558a5f0fb15SPaul Saab #else
559a5f0fb15SPaul Saab #define LSIGNAL(sig,func)       signal(sig,func)
560a5f0fb15SPaul Saab #endif
561a5f0fb15SPaul Saab 
56215596da4SPaul Saab #if HAVE_SIGPROCMASK
56315596da4SPaul Saab #if HAVE_SIGSET_T
56415596da4SPaul Saab #else
56515596da4SPaul Saab #undef HAVE_SIGPROCMASK
56615596da4SPaul Saab #endif
56715596da4SPaul Saab #endif
56815596da4SPaul Saab #if HAVE_SIGPROCMASK
56915596da4SPaul Saab #if HAVE_SIGEMPTYSET
57015596da4SPaul Saab #else
57115596da4SPaul Saab #undef  sigemptyset
57215596da4SPaul Saab #define sigemptyset(mp) *(mp) = 0
57315596da4SPaul Saab #endif
57415596da4SPaul Saab #endif
57515596da4SPaul Saab 
576a5f0fb15SPaul Saab #define S_INTERRUPT     01
577a5f0fb15SPaul Saab #define S_STOP          02
578a5f0fb15SPaul Saab #define S_WINCH         04
579a5f0fb15SPaul Saab #define ABORT_SIGS()    (sigs & (S_INTERRUPT|S_STOP))
580a5f0fb15SPaul Saab 
581d713e089SXin LI #ifdef EXIT_SUCCESS
582d713e089SXin LI #define QUIT_OK         EXIT_SUCCESS
583d713e089SXin LI #else
584a5f0fb15SPaul Saab #define QUIT_OK         0
585d713e089SXin LI #endif
586d713e089SXin LI #ifdef EXIT_FAILURE
587d713e089SXin LI #define QUIT_ERROR      EXIT_FAILURE
588d713e089SXin LI #define QUIT_INTERRUPT  (EXIT_FAILURE+1)
589d713e089SXin LI #else
590a5f0fb15SPaul Saab #define QUIT_ERROR      1
59133096f16SXin LI #define QUIT_INTERRUPT  2
592d713e089SXin LI #endif
593a5f0fb15SPaul Saab #define QUIT_SAVED_STATUS (-1)
594a5f0fb15SPaul Saab 
595423c5ce5SXin LI #define FOLLOW_DESC     0
596423c5ce5SXin LI #define FOLLOW_NAME     1
597423c5ce5SXin LI 
598a5f0fb15SPaul Saab /* filestate flags */
599a5f0fb15SPaul Saab #define CH_CANSEEK      001
600a5f0fb15SPaul Saab #define CH_KEEPOPEN     002
601a5f0fb15SPaul Saab #define CH_POPENED      004
602a5f0fb15SPaul Saab #define CH_HELPFILE     010
60396e55cc7SXin LI #define CH_NODATA       020     /* Special case for zero length files */
604*c77c4889SXin LI #define CH_NOTRUSTSIZE  040     /* For files that claim 0 length size falsely */
60596e55cc7SXin LI 
606a5f0fb15SPaul Saab #define ch_zero()       ((POSITION)0)
607a5f0fb15SPaul Saab 
608a5f0fb15SPaul Saab #define FAKE_HELPFILE   "@/\\less/\\help/\\file/\\@"
60996e55cc7SXin LI #define FAKE_EMPTYFILE  "@/\\less/\\empty/\\file/\\@"
610a5f0fb15SPaul Saab 
611f0be0a1fSXin LI /* Flags for cvt_text */
612f0be0a1fSXin LI #define CVT_TO_LC       01      /* Convert upper-case to lower-case */
613f0be0a1fSXin LI #define CVT_BS          02      /* Do backspace processing */
614f0be0a1fSXin LI #define CVT_CRLF        04      /* Remove CR after LF */
615f0be0a1fSXin LI #define CVT_ANSI        010     /* Remove ANSI escape sequences */
616f0be0a1fSXin LI 
617a15691bfSXin LI #if HAVE_TIME_T
618a15691bfSXin LI #define time_type       time_t
619a15691bfSXin LI #else
620a15691bfSXin LI #define time_type       long
621a15691bfSXin LI #endif
622a15691bfSXin LI 
623b7780dbeSXin LI /* X11 mouse reporting definitions */
624b7780dbeSXin LI #define X11MOUSE_BUTTON1    0 /* Left button press */
625b7780dbeSXin LI #define X11MOUSE_BUTTON2    1 /* Middle button press */
626b7780dbeSXin LI #define X11MOUSE_BUTTON3    2 /* Right button press */
627b7780dbeSXin LI #define X11MOUSE_BUTTON_REL 3 /* Button release */
628b7780dbeSXin LI #define X11MOUSE_WHEEL_UP   0x40 /* Wheel scroll up */
629b7780dbeSXin LI #define X11MOUSE_WHEEL_DOWN 0x41 /* Wheel scroll down */
630b7780dbeSXin LI #define X11MOUSE_OFFSET     0x20 /* Added to button & pos bytes to create a char */
631b7780dbeSXin LI 
632*c77c4889SXin LI /* Security features. */
633*c77c4889SXin LI #define SF_EDIT             (1<<1)  /* Edit file (v) */
634*c77c4889SXin LI #define SF_EXAMINE          (1<<2)  /* Examine file (:e) */
635*c77c4889SXin LI #define SF_GLOB             (1<<3)  /* Expand file pattern */
636*c77c4889SXin LI #define SF_HISTORY          (1<<4)  /* History file */
637*c77c4889SXin LI #define SF_LESSKEY          (1<<5)  /* Lesskey files */
638*c77c4889SXin LI #define SF_LESSOPEN         (1<<6)  /* LESSOPEN */
639*c77c4889SXin LI #define SF_LOGFILE          (1<<7)  /* Log file (s, -o) */
640*c77c4889SXin LI #define SF_PIPE             (1<<8)  /* Pipe (|) */
641*c77c4889SXin LI #define SF_SHELL            (1<<9)  /* Shell command (!) */
642*c77c4889SXin LI #define SF_STOP             (1<<10) /* Stop signal */
643*c77c4889SXin LI #define SF_TAGS             (1<<11) /* Tags */
644*c77c4889SXin LI #define SF_OSC8_OPEN        (1<<12) /* OSC8 open */
645*c77c4889SXin LI 
646d713e089SXin LI #if LESSTEST
647d713e089SXin LI #define LESS_DUMP_CHAR CONTROL(']')
648d713e089SXin LI #endif
649d713e089SXin LI 
650f6b74a7dSXin LI struct mlist;
651f6b74a7dSXin LI struct loption;
652f6b74a7dSXin LI struct hilite_tree;
6532235c7feSXin LI struct ansi_state;
654f6b74a7dSXin LI #include "pattern.h"
65530a1828cSXin LI #include "xbuf.h"
656a5f0fb15SPaul Saab #include "funcs.h"
657a5f0fb15SPaul Saab 
6581ede1615STim J. Robbins /* Functions not included in funcs.h */
659d713e089SXin LI void postoa(POSITION, char*, int);
660d713e089SXin LI void linenumtoa(LINENUM, char*, int);
661d713e089SXin LI void inttoa(int, char*, int);
662d713e089SXin LI int lstrtoi(char*, char**, int);
663d713e089SXin LI POSITION lstrtopos(char*, char**, int);
664d713e089SXin LI unsigned long lstrtoul(char*, char**, int);
665*c77c4889SXin LI int lstrtoic(constant char*, constant char**, int);
666*c77c4889SXin LI POSITION lstrtoposc(constant char*, constant char**, int);
667*c77c4889SXin LI unsigned long lstrtoulc(constant char*, constant char**, int);
668b7780dbeSXin LI #if MSDOS_COMPILER==WIN32C
669d713e089SXin LI int pclose(FILE*);
670b7780dbeSXin LI #endif
671