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