Import of less version 394
[dragonfly.git] / contrib / less-394 / less.h
1 /*
2  * Copyright (C) 1984-2005  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 about less, or for information on how to 
8  * contact the author, see the README file.
9  */
10
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_LIMITS_H
74 #include <limits.h>
75 #endif
76 #if HAVE_STDLIB_H
77 #include <stdlib.h>
78 #endif
79 #if HAVE_STRING_H
80 #include <string.h>
81 #endif
82
83 /* OS-specific includes */
84 #ifdef _OSK
85 #include <modes.h>
86 #include <strings.h>
87 #endif
88
89 #ifdef __TANDEM
90 #include <floss.h>
91 #endif
92
93 #if MSDOS_COMPILER==WIN32C || OS2
94 #include <io.h>
95 #endif
96
97 #if MSDOS_COMPILER==DJGPPC
98 #include <io.h>
99 #include <sys/exceptn.h>
100 #include <conio.h>
101 #include <pc.h>
102 #endif
103
104 #if !HAVE_STDLIB_H
105 char *getenv();
106 off_t lseek();
107 VOID_POINTER calloc();
108 void free();
109 #endif
110
111 /*
112  * Simple lowercase test which can be used during option processing
113  * (before options are parsed which might tell us what charset to use).
114  */
115 #define ASCII_IS_UPPER(c)       ((c) >= 'A' && (c) <= 'Z')
116 #define ASCII_IS_LOWER(c)       ((c) >= 'a' && (c) <= 'z')
117 #define ASCII_TO_UPPER(c)       ((c) - 'a' + 'A')
118 #define ASCII_TO_LOWER(c)       ((c) - 'A' + 'a')
119
120 #undef IS_UPPER
121 #undef IS_LOWER
122 #undef TO_UPPER
123 #undef TO_LOWER
124 #undef IS_SPACE
125 #undef IS_DIGIT
126
127 #if !HAVE_UPPER_LOWER
128 #define IS_UPPER(c)     ASCII_IS_UPPER(c)
129 #define IS_LOWER(c)     ASCII_IS_LOWER(c)
130 #define TO_UPPER(c)     ASCII_TO_UPPER(c)
131 #define TO_LOWER(c)     ASCII_TO_LOWER(c)
132 #else
133 #define IS_UPPER(c)     isupper((unsigned char) (c))
134 #define IS_LOWER(c)     islower((unsigned char) (c))
135 #define TO_UPPER(c)     toupper((unsigned char) (c))
136 #define TO_LOWER(c)     tolower((unsigned char) (c))
137 #endif
138
139 #ifdef isspace
140 #define IS_SPACE(c)     isspace((unsigned char)(c))
141 #else
142 #define IS_SPACE(c)     ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f')
143 #endif
144
145 #ifdef isdigit
146 #define IS_DIGIT(c)     isdigit((unsigned char)(c))
147 #else
148 #define IS_DIGIT(c)     ((c) >= '0' && (c) <= '9')
149 #endif
150
151 #ifndef NULL
152 #define NULL    0
153 #endif
154
155 #ifndef TRUE
156 #define TRUE            1
157 #endif
158 #ifndef FALSE
159 #define FALSE           0
160 #endif
161
162 #define OPT_OFF         0
163 #define OPT_ON          1
164 #define OPT_ONPLUS      2
165
166 #if !HAVE_MEMCPY
167 #ifndef memcpy
168 #define memcpy(to,from,len)     bcopy((from),(to),(len))
169 #endif
170 #endif
171
172 #if HAVE_SNPRINTF
173 #define SNPRINTF1(str, size, fmt, v1)             snprintf((str), (size), (fmt), (v1))
174 #define SNPRINTF2(str, size, fmt, v1, v2)         snprintf((str), (size), (fmt), (v1), (v2))
175 #define SNPRINTF3(str, size, fmt, v1, v2, v3)     snprintf((str), (size), (fmt), (v1), (v2), (v3))
176 #define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) snprintf((str), (size), (fmt), (v1), (v2), (v3), (v4))
177 #else
178 /* Use unsafe sprintf if we don't have snprintf. */
179 #define SNPRINTF1(str, size, fmt, v1)             sprintf((str), (fmt), (v1))
180 #define SNPRINTF2(str, size, fmt, v1, v2)         sprintf((str), (fmt), (v1), (v2))
181 #define SNPRINTF3(str, size, fmt, v1, v2, v3)     sprintf((str), (fmt), (v1), (v2), (v3))
182 #define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) sprintf((str), (fmt), (v1), (v2), (v3), (v4))
183 #endif
184
185 #define BAD_LSEEK       ((off_t)-1)
186
187 #ifndef CHAR_BIT
188 #define CHAR_BIT 8
189 #endif
190
191 /*
192  * Upper bound on the string length of an integer converted to string.
193  * 302 / 1000 is ceil (log10 (2.0)).  Subtract 1 for the sign bit;
194  * add 1 for integer division truncation; add 1 more for a minus sign.
195  */
196 #define INT_STRLEN_BOUND(t) ((sizeof(t) * CHAR_BIT - 1) * 302 / 1000 + 1 + 1)
197
198 /*
199  * Special types and constants.
200  */
201 typedef unsigned long LWCHAR;
202 typedef off_t           POSITION;
203 typedef off_t           LINENUM;
204 #define MIN_LINENUM_WIDTH  7    /* Min printing width of a line number */
205 #define MAX_UTF_CHAR_LEN   6    /* Max bytes in one UTF-8 char */
206
207 #define NULL_POSITION   ((POSITION)(-1))
208
209 /*
210  * Flags for open()
211  */
212 #if MSDOS_COMPILER || OS2
213 #define OPEN_READ       (O_RDONLY|O_BINARY)
214 #else
215 #ifdef _OSK
216 #define OPEN_READ       (S_IREAD)
217 #else
218 #ifdef O_RDONLY
219 #define OPEN_READ       (O_RDONLY)
220 #else
221 #define OPEN_READ       (0)
222 #endif
223 #endif
224 #endif
225
226 #if defined(O_WRONLY) && defined(O_APPEND)
227 #define OPEN_APPEND     (O_APPEND|O_WRONLY)
228 #else
229 #ifdef _OSK
230 #define OPEN_APPEND     (S_IWRITE)
231 #else
232 #define OPEN_APPEND     (1)
233 #endif
234 #endif
235
236 /*
237  * Set a file descriptor to binary mode.
238  */
239 #if MSDOS_COMPILER==MSOFTC
240 #define SET_BINARY(f)   _setmode(f, _O_BINARY);
241 #else
242 #if MSDOS_COMPILER || OS2
243 #define SET_BINARY(f)   setmode(f, O_BINARY)
244 #else
245 #define SET_BINARY(f)
246 #endif
247 #endif
248
249 /*
250  * Does the shell treat "?" as a metacharacter?
251  */
252 #if MSDOS_COMPILER || OS2 || _OSK
253 #define SHELL_META_QUEST 0
254 #else
255 #define SHELL_META_QUEST 1
256 #endif
257
258 #define SPACES_IN_FILENAMES 1
259
260 /*
261  * An IFILE represents an input file.
262  */
263 #define IFILE           VOID_POINTER
264 #define NULL_IFILE      ((IFILE)NULL)
265
266 /*
267  * The structure used to represent a "screen position".
268  * This consists of a file position, and a screen line number.
269  * The meaning is that the line starting at the given file
270  * position is displayed on the ln-th line of the screen.
271  * (Screen lines before ln are empty.)
272  */
273 struct scrpos
274 {
275         POSITION pos;
276         int ln;
277 };
278
279 typedef union parg
280 {
281         char *p_string;
282         int p_int;
283         LINENUM p_linenum;
284 } PARG;
285
286 #define NULL_PARG       ((PARG *)NULL)
287
288 struct textlist
289 {
290         char *string;
291         char *endstring;
292 };
293
294 #define EOI             (-1)
295
296 #define READ_INTR       (-2)
297
298 /* How quiet should we be? */
299 #define NOT_QUIET       0       /* Ring bell at eof and for errors */
300 #define LITTLE_QUIET    1       /* Ring bell only for errors */
301 #define VERY_QUIET      2       /* Never ring bell */
302
303 /* How should we prompt? */
304 #define PR_SHORT        0       /* Prompt with colon */
305 #define PR_MEDIUM       1       /* Prompt with message */
306 #define PR_LONG         2       /* Prompt with longer message */
307
308 /* How should we handle backspaces? */
309 #define BS_SPECIAL      0       /* Do special things for underlining and bold */
310 #define BS_NORMAL       1       /* \b treated as normal char; actually output */
311 #define BS_CONTROL      2       /* \b treated as control char; prints as ^H */
312
313 /* How should we search? */
314 #define SRCH_FORW       (1 << 0)  /* Search forward from current position */
315 #define SRCH_BACK       (1 << 1)  /* Search backward from current position */
316 #define SRCH_NO_MOVE    (1 << 2)  /* Highlight, but don't move */
317 #define SRCH_FIND_ALL   (1 << 4)  /* Find and highlight all matches */
318 #define SRCH_NO_MATCH   (1 << 8)  /* Search for non-matching lines */
319 #define SRCH_PAST_EOF   (1 << 9)  /* Search past end-of-file, into next file */
320 #define SRCH_FIRST_FILE (1 << 10) /* Search starting at the first file */
321 #define SRCH_NO_REGEX   (1 << 12) /* Don't use regular expressions */
322
323 #define SRCH_REVERSE(t) (((t) & SRCH_FORW) ? \
324                                 (((t) & ~SRCH_FORW) | SRCH_BACK) : \
325                                 (((t) & ~SRCH_BACK) | SRCH_FORW))
326
327 /* */
328 #define NO_MCA          0
329 #define MCA_DONE        1
330 #define MCA_MORE        2
331
332 #define CC_OK           0       /* Char was accepted & processed */
333 #define CC_QUIT         1       /* Char was a request to abort current cmd */
334 #define CC_ERROR        2       /* Char could not be accepted due to error */
335 #define CC_PASS         3       /* Char was rejected (internal) */
336
337 #define CF_QUIT_ON_ERASE 0001   /* Abort cmd if its entirely erased */
338
339 /* Special char bit-flags used to tell put_line() to do something special */
340 #define AT_NORMAL       (0)
341 #define AT_UNDERLINE    (1 << 0)
342 #define AT_BOLD         (1 << 1)
343 #define AT_BLINK        (1 << 2)
344 #define AT_STANDOUT     (1 << 3)
345 #define AT_ANSI         (1 << 4)  /* Content-supplied "ANSI" escape sequence */
346 #define AT_BINARY       (1 << 5)  /* LESS*BINFMT representation */
347 #define AT_HILITE       (1 << 6)  /* Internal highlights (e.g., for search) */
348
349 #if '0' == 240
350 #define IS_EBCDIC_HOST 1
351 #endif
352
353 #if IS_EBCDIC_HOST
354 /*
355  * Long definition for EBCDIC.
356  * Since the argument is usually a constant, this macro normally compiles
357  * into a constant.
358  */
359 #define CONTROL(c) ( \
360         (c)=='[' ? '\047' : \
361         (c)=='a' ? '\001' : \
362         (c)=='b' ? '\002' : \
363         (c)=='c' ? '\003' : \
364         (c)=='d' ? '\067' : \
365         (c)=='e' ? '\055' : \
366         (c)=='f' ? '\056' : \
367         (c)=='g' ? '\057' : \
368         (c)=='h' ? '\026' : \
369         (c)=='i' ? '\005' : \
370         (c)=='j' ? '\025' : \
371         (c)=='k' ? '\013' : \
372         (c)=='l' ? '\014' : \
373         (c)=='m' ? '\015' : \
374         (c)=='n' ? '\016' : \
375         (c)=='o' ? '\017' : \
376         (c)=='p' ? '\020' : \
377         (c)=='q' ? '\021' : \
378         (c)=='r' ? '\022' : \
379         (c)=='s' ? '\023' : \
380         (c)=='t' ? '\074' : \
381         (c)=='u' ? '\075' : \
382         (c)=='v' ? '\062' : \
383         (c)=='w' ? '\046' : \
384         (c)=='x' ? '\030' : \
385         (c)=='y' ? '\031' : \
386         (c)=='z' ? '\077' : \
387         (c)=='A' ? '\001' : \
388         (c)=='B' ? '\002' : \
389         (c)=='C' ? '\003' : \
390         (c)=='D' ? '\067' : \
391         (c)=='E' ? '\055' : \
392         (c)=='F' ? '\056' : \
393         (c)=='G' ? '\057' : \
394         (c)=='H' ? '\026' : \
395         (c)=='I' ? '\005' : \
396         (c)=='J' ? '\025' : \
397         (c)=='K' ? '\013' : \
398         (c)=='L' ? '\014' : \
399         (c)=='M' ? '\015' : \
400         (c)=='N' ? '\016' : \
401         (c)=='O' ? '\017' : \
402         (c)=='P' ? '\020' : \
403         (c)=='Q' ? '\021' : \
404         (c)=='R' ? '\022' : \
405         (c)=='S' ? '\023' : \
406         (c)=='T' ? '\074' : \
407         (c)=='U' ? '\075' : \
408         (c)=='V' ? '\062' : \
409         (c)=='W' ? '\046' : \
410         (c)=='X' ? '\030' : \
411         (c)=='Y' ? '\031' : \
412         (c)=='Z' ? '\077' : \
413         (c)=='|' ? '\031' : \
414         (c)=='\\' ? '\034' : \
415         (c)=='^' ? '\036' : \
416         (c)&077)
417 #else
418 #define CONTROL(c)      ((c)&037)
419 #endif /* IS_EBCDIC_HOST */
420
421 #define ESC             CONTROL('[')
422
423 #if _OSK_MWC32
424 #define LSIGNAL(sig,func)       os9_signal(sig,func)
425 #else
426 #define LSIGNAL(sig,func)       signal(sig,func)
427 #endif
428
429 #if HAVE_SIGPROCMASK
430 #if HAVE_SIGSET_T
431 #else
432 #undef HAVE_SIGPROCMASK
433 #endif
434 #endif
435 #if HAVE_SIGPROCMASK
436 #if HAVE_SIGEMPTYSET
437 #else
438 #undef  sigemptyset
439 #define sigemptyset(mp) *(mp) = 0
440 #endif
441 #endif
442
443 #define S_INTERRUPT     01
444 #define S_STOP          02
445 #define S_WINCH         04
446 #define ABORT_SIGS()    (sigs & (S_INTERRUPT|S_STOP))
447
448 #define QUIT_OK         0
449 #define QUIT_ERROR      1
450 #define QUIT_SAVED_STATUS (-1)
451
452 /* filestate flags */
453 #define CH_CANSEEK      001
454 #define CH_KEEPOPEN     002
455 #define CH_POPENED      004
456 #define CH_HELPFILE     010
457
458 #define ch_zero()       ((POSITION)0)
459
460 #define FAKE_HELPFILE   "@/\\less/\\help/\\file/\\@"
461
462 #include "funcs.h"
463
464 /* Functions not included in funcs.h */
465 void postoa();
466 void linenumtoa();
467 void inttoa();