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