less.h revision 172471
1321964Ssjg/* $FreeBSD: head/contrib/less/less.h 172471 2007-10-08 16:17:42Z delphij $ */
2246149Ssjg/*
3246149Ssjg * Copyright (C) 1984-2007  Mark Nudelman
4246149Ssjg *
5246149Ssjg * You may distribute under the terms of either the GNU General Public
6246149Ssjg * License or the Less License, as specified in the README file.
7246149Ssjg *
8246149Ssjg * For more information about less, or for information on how to
9246149Ssjg * contact the author, see the README file.
10321964Ssjg */
11246149Ssjg
12246149Ssjg#define NEWBOT 1
13246149Ssjg
14246149Ssjg/*
15246149Ssjg * Standard include file for "less".
16246149Ssjg */
17246149Ssjg
18246149Ssjg/*
19246149Ssjg * Defines for MSDOS_COMPILER.
20246149Ssjg */
21246149Ssjg#define	MSOFTC		1	/* Microsoft C */
22246149Ssjg#define	BORLANDC	2	/* Borland C */
23321964Ssjg#define	WIN32C		3	/* Windows (Borland C or Microsoft C) */
24321964Ssjg#define	DJGPPC		4	/* DJGPP C */
25246149Ssjg
26246149Ssjg/*
27246149Ssjg * Include the file of compile-time options.
28246149Ssjg * The <> make cc search for it in -I., not srcdir.
29246149Ssjg */
30246149Ssjg#include <defines.h>
31246149Ssjg
32246149Ssjg#ifdef _SEQUENT_
33246149Ssjg/*
34246149Ssjg * Kludge for Sequent Dynix systems that have sigsetmask, but
35246149Ssjg * it's not compatible with the way less calls it.
36246149Ssjg * {{ Do other systems need this? }}
37246149Ssjg */
38321964Ssjg#undef HAVE_SIGSETMASK
39246149Ssjg#endif
40246149Ssjg
41246149Ssjg/*
42246149Ssjg * Language details.
43246149Ssjg */
44246149Ssjg#if HAVE_VOID
45246149Ssjg#define	VOID_POINTER	void *
46246149Ssjg#else
47246149Ssjg#define	VOID_POINTER	char *
48246149Ssjg#define	void  int
49246149Ssjg#endif
50246149Ssjg#if HAVE_CONST
51246149Ssjg#define	constant	const
52246149Ssjg#else
53246149Ssjg#define	constant
54246149Ssjg#endif
55246149Ssjg
56246149Ssjg#define	public		/* PUBLIC FUNCTION */
57246149Ssjg
58246149Ssjg/* Library function declarations */
59246149Ssjg
60246149Ssjg#if HAVE_SYS_TYPES_H
61246149Ssjg#include <sys/types.h>
62246149Ssjg#endif
63246149Ssjg#if HAVE_STDIO_H
64246149Ssjg#include <stdio.h>
65246149Ssjg#endif
66246149Ssjg#if HAVE_FCNTL_H
67246149Ssjg#include <fcntl.h>
68246149Ssjg#endif
69246149Ssjg#if HAVE_UNISTD_H
70246149Ssjg#include <unistd.h>
71246149Ssjg#endif
72246149Ssjg#if HAVE_CTYPE_H
73246149Ssjg#include <ctype.h>
74246149Ssjg#endif
75246149Ssjg#if HAVE_LIMITS_H
76246149Ssjg#include <limits.h>
77246149Ssjg#endif
78246149Ssjg#if HAVE_STDLIB_H
79246149Ssjg#include <stdlib.h>
80246149Ssjg#endif
81246149Ssjg#if HAVE_STRING_H
82321964Ssjg#include <string.h>
83246149Ssjg#endif
84246149Ssjg
85246149Ssjg/* OS-specific includes */
86246149Ssjg#ifdef _OSK
87246149Ssjg#include <modes.h>
88246149Ssjg#include <strings.h>
89246149Ssjg#endif
90246149Ssjg
91246149Ssjg#ifdef __TANDEM
92246149Ssjg#include <floss.h>
93253883Ssjg#endif
94246149Ssjg
95246149Ssjg#if MSDOS_COMPILER==WIN32C || OS2
96246149Ssjg#include <io.h>
97246149Ssjg#endif
98246149Ssjg
99246149Ssjg#if MSDOS_COMPILER==DJGPPC
100246149Ssjg#include <io.h>
101246149Ssjg#include <sys/exceptn.h>
102246149Ssjg#include <conio.h>
103246149Ssjg#include <pc.h>
104246149Ssjg#endif
105246149Ssjg
106246149Ssjg#if !HAVE_STDLIB_H
107246149Ssjgchar *getenv();
108246149Ssjgoff_t lseek();
109246149SsjgVOID_POINTER calloc();
110246149Ssjgvoid free();
111246149Ssjg#endif
112246149Ssjg
113246149Ssjg/*
114246149Ssjg * Simple lowercase test which can be used during option processing
115246149Ssjg * (before options are parsed which might tell us what charset to use).
116246149Ssjg */
117246149Ssjg#define ASCII_IS_UPPER(c)	((c) >= 'A' && (c) <= 'Z')
118246149Ssjg#define ASCII_IS_LOWER(c)	((c) >= 'a' && (c) <= 'z')
119246149Ssjg#define	ASCII_TO_UPPER(c)	((c) - 'a' + 'A')
120321964Ssjg#define	ASCII_TO_LOWER(c)	((c) - 'A' + 'a')
121246149Ssjg
122246149Ssjg#undef IS_UPPER
123246149Ssjg#undef IS_LOWER
124246149Ssjg#undef TO_UPPER
125246149Ssjg#undef TO_LOWER
126246149Ssjg#undef IS_SPACE
127246149Ssjg#undef IS_DIGIT
128246149Ssjg
129246149Ssjg#if !HAVE_UPPER_LOWER
130246149Ssjg#define	IS_UPPER(c)	ASCII_IS_UPPER(c)
131246149Ssjg#define	IS_LOWER(c)	ASCII_IS_LOWER(c)
132321964Ssjg#define	TO_UPPER(c)	ASCII_TO_UPPER(c)
133246149Ssjg#define	TO_LOWER(c)	ASCII_TO_LOWER(c)
134246149Ssjg#else
135292068Ssjg#define	IS_UPPER(c)	isupper((unsigned char) (c))
136246149Ssjg#define	IS_LOWER(c)	islower((unsigned char) (c))
137246149Ssjg#define	TO_UPPER(c)	toupper((unsigned char) (c))
138246149Ssjg#define	TO_LOWER(c)	tolower((unsigned char) (c))
139246149Ssjg#endif
140246149Ssjg
141246149Ssjg#ifdef isspace
142246149Ssjg#define IS_SPACE(c)	isspace((unsigned char)(c))
143246149Ssjg#else
144246149Ssjg#define IS_SPACE(c)	((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f')
145246149Ssjg#endif
146292068Ssjg
147246149Ssjg#ifdef isdigit
148321964Ssjg#define IS_DIGIT(c)	isdigit((unsigned char)(c))
149321964Ssjg#else
150321964Ssjg#define IS_DIGIT(c)	((c) >= '0' && (c) <= '9')
151246149Ssjg#endif
152246149Ssjg
153246149Ssjg#define IS_CSI_START(c)	((c) == ESC || ((unsigned char)(c)) == CSI)
154246149Ssjg
155246149Ssjg#ifndef NULL
156246149Ssjg#define	NULL	0
157246149Ssjg#endif
158246149Ssjg
159246149Ssjg#ifndef TRUE
160321964Ssjg#define	TRUE		1
161246149Ssjg#endif
162246149Ssjg#ifndef FALSE
163246149Ssjg#define	FALSE		0
164246149Ssjg#endif
165246149Ssjg
166246149Ssjg#define	OPT_OFF		0
167246149Ssjg#define	OPT_ON		1
168246149Ssjg#define	OPT_ONPLUS	2
169246149Ssjg
170246149Ssjg#if !HAVE_MEMCPY
171246149Ssjg#ifndef memcpy
172246149Ssjg#define	memcpy(to,from,len)	bcopy((from),(to),(len))
173246149Ssjg#endif
174246149Ssjg#endif
175246149Ssjg
176246149Ssjg#if HAVE_SNPRINTF
177246149Ssjg#define SNPRINTF1(str, size, fmt, v1)             snprintf((str), (size), (fmt), (v1))
178246149Ssjg#define SNPRINTF2(str, size, fmt, v1, v2)         snprintf((str), (size), (fmt), (v1), (v2))
179246149Ssjg#define SNPRINTF3(str, size, fmt, v1, v2, v3)     snprintf((str), (size), (fmt), (v1), (v2), (v3))
180246149Ssjg#define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) snprintf((str), (size), (fmt), (v1), (v2), (v3), (v4))
181246149Ssjg#else
182246149Ssjg/* Use unsafe sprintf if we don't have snprintf. */
183246149Ssjg#define SNPRINTF1(str, size, fmt, v1)             sprintf((str), (fmt), (v1))
184246149Ssjg#define SNPRINTF2(str, size, fmt, v1, v2)         sprintf((str), (fmt), (v1), (v2))
185246149Ssjg#define SNPRINTF3(str, size, fmt, v1, v2, v3)     sprintf((str), (fmt), (v1), (v2), (v3))
186246149Ssjg#define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) sprintf((str), (fmt), (v1), (v2), (v3), (v4))
187246149Ssjg#endif
188246149Ssjg
189246149Ssjg#define	BAD_LSEEK	((off_t)-1)
190321964Ssjg
191321964Ssjg#ifndef CHAR_BIT
192321964Ssjg#define CHAR_BIT 8
193321964Ssjg#endif
194246149Ssjg
195246149Ssjg/*
196246149Ssjg * Upper bound on the string length of an integer converted to string.
197246149Ssjg * 302 / 1000 is ceil (log10 (2.0)).  Subtract 1 for the sign bit;
198246149Ssjg * add 1 for integer division truncation; add 1 more for a minus sign.
199246149Ssjg */
200246149Ssjg#define INT_STRLEN_BOUND(t) ((sizeof(t) * CHAR_BIT - 1) * 302 / 1000 + 1 + 1)
201246149Ssjg
202246149Ssjg/*
203246149Ssjg * Special types and constants.
204246149Ssjg */
205246149Ssjgtypedef unsigned long LWCHAR;
206246149Ssjgtypedef off_t		POSITION;
207246149Ssjgtypedef off_t		LINENUM;
208246149Ssjg#define MIN_LINENUM_WIDTH  7	/* Min printing width of a line number */
209246149Ssjg#define MAX_UTF_CHAR_LEN   6	/* Max bytes in one UTF-8 char */
210246149Ssjg
211246149Ssjg#define	NULL_POSITION	((POSITION)(-1))
212246149Ssjg
213246149Ssjg/*
214246149Ssjg * Flags for open()
215246149Ssjg */
216246149Ssjg#if MSDOS_COMPILER || OS2
217246149Ssjg#define	OPEN_READ	(O_RDONLY|O_BINARY)
218246149Ssjg#else
219246149Ssjg#ifdef _OSK
220246149Ssjg#define	OPEN_READ	(S_IREAD)
221246149Ssjg#else
222246149Ssjg#ifdef O_RDONLY
223246149Ssjg#define	OPEN_READ	(O_RDONLY)
224246149Ssjg#else
225246149Ssjg#define	OPEN_READ	(0)
226246149Ssjg#endif
227246149Ssjg#endif
228246149Ssjg#endif
229246149Ssjg
230246149Ssjg#if defined(O_WRONLY) && defined(O_APPEND)
231246149Ssjg#define	OPEN_APPEND	(O_APPEND|O_WRONLY)
232246149Ssjg#else
233246149Ssjg#ifdef _OSK
234246149Ssjg#define OPEN_APPEND	(S_IWRITE)
235246149Ssjg#else
236246149Ssjg#define	OPEN_APPEND	(1)
237246149Ssjg#endif
238246149Ssjg#endif
239246149Ssjg
240246149Ssjg/*
241246149Ssjg * Set a file descriptor to binary mode.
242246149Ssjg */
243246149Ssjg#if MSDOS_COMPILER==MSOFTC
244246149Ssjg#define	SET_BINARY(f)	_setmode(f, _O_BINARY);
245246149Ssjg#else
246246149Ssjg#if MSDOS_COMPILER || OS2
247246149Ssjg#define	SET_BINARY(f)	setmode(f, O_BINARY)
248246149Ssjg#else
249246149Ssjg#define	SET_BINARY(f)
250246149Ssjg#endif
251246149Ssjg#endif
252246149Ssjg
253246149Ssjg/*
254321964Ssjg * Does the shell treat "?" as a metacharacter?
255321964Ssjg */
256321964Ssjg#if MSDOS_COMPILER || OS2 || _OSK
257321964Ssjg#define	SHELL_META_QUEST 0
258321964Ssjg#else
259321964Ssjg#define	SHELL_META_QUEST 1
260321964Ssjg#endif
261321964Ssjg
262246149Ssjg#define	SPACES_IN_FILENAMES 1
263321964Ssjg
264321964Ssjg/*
265321964Ssjg * An IFILE represents an input file.
266321964Ssjg */
267321964Ssjg#define	IFILE		VOID_POINTER
268321964Ssjg#define	NULL_IFILE	((IFILE)NULL)
269321964Ssjg
270/*
271 * The structure used to represent a "screen position".
272 * This consists of a file position, and a screen line number.
273 * The meaning is that the line starting at the given file
274 * position is displayed on the ln-th line of the screen.
275 * (Screen lines before ln are empty.)
276 */
277struct scrpos
278{
279	POSITION pos;
280	int ln;
281};
282
283typedef union parg
284{
285	char *p_string;
286	int p_int;
287	LINENUM p_linenum;
288} PARG;
289
290#define	NULL_PARG	((PARG *)NULL)
291
292struct textlist
293{
294	char *string;
295	char *endstring;
296};
297
298#define	EOI		(-1)
299
300#define	READ_INTR	(-2)
301
302/* A fraction is represented by an int n; the fraction is n/NUM_FRAC_DENOM */
303#define NUM_FRAC_DENOM			1000000
304#define NUM_LOG_FRAC_DENOM		6
305
306/* How quiet should we be? */
307#define	NOT_QUIET	0	/* Ring bell at eof and for errors */
308#define	LITTLE_QUIET	1	/* Ring bell only for errors */
309#define	VERY_QUIET	2	/* Never ring bell */
310
311/* How should we prompt? */
312#define	PR_SHORT	0	/* Prompt with colon */
313#define	PR_MEDIUM	1	/* Prompt with message */
314#define	PR_LONG		2	/* Prompt with longer message */
315
316/* How should we handle backspaces? */
317#define	BS_SPECIAL	0	/* Do special things for underlining and bold */
318#define	BS_NORMAL	1	/* \b treated as normal char; actually output */
319#define	BS_CONTROL	2	/* \b treated as control char; prints as ^H */
320
321/* How should we search? */
322#define	SRCH_FORW	(1 << 0)  /* Search forward from current position */
323#define	SRCH_BACK	(1 << 1)  /* Search backward from current position */
324#define	SRCH_NO_MOVE	(1 << 2)  /* Highlight, but don't move */
325#define	SRCH_FIND_ALL	(1 << 4)  /* Find and highlight all matches */
326#define	SRCH_NO_MATCH	(1 << 8)  /* Search for non-matching lines */
327#define	SRCH_PAST_EOF	(1 << 9)  /* Search past end-of-file, into next file */
328#define	SRCH_FIRST_FILE	(1 << 10) /* Search starting at the first file */
329#define	SRCH_NO_REGEX	(1 << 12) /* Don't use regular expressions */
330
331#define	SRCH_REVERSE(t)	(((t) & SRCH_FORW) ? \
332				(((t) & ~SRCH_FORW) | SRCH_BACK) : \
333				(((t) & ~SRCH_BACK) | SRCH_FORW))
334
335/* */
336#define	NO_MCA		0
337#define	MCA_DONE	1
338#define	MCA_MORE	2
339
340#define	CC_OK		0	/* Char was accepted & processed */
341#define	CC_QUIT		1	/* Char was a request to abort current cmd */
342#define	CC_ERROR	2	/* Char could not be accepted due to error */
343#define	CC_PASS		3	/* Char was rejected (internal) */
344
345#define CF_QUIT_ON_ERASE 0001   /* Abort cmd if its entirely erased */
346
347/* Special char bit-flags used to tell put_line() to do something special */
348#define	AT_NORMAL	(0)
349#define	AT_UNDERLINE	(1 << 0)
350#define	AT_BOLD		(1 << 1)
351#define	AT_BLINK	(1 << 2)
352#define	AT_STANDOUT	(1 << 3)
353#define	AT_ANSI		(1 << 4)  /* Content-supplied "ANSI" escape sequence */
354#define	AT_BINARY	(1 << 5)  /* LESS*BINFMT representation */
355#define	AT_HILITE	(1 << 6)  /* Internal highlights (e.g., for search) */
356
357#if '0' == 240
358#define IS_EBCDIC_HOST 1
359#endif
360
361#if IS_EBCDIC_HOST
362/*
363 * Long definition for EBCDIC.
364 * Since the argument is usually a constant, this macro normally compiles
365 * into a constant.
366 */
367#define CONTROL(c) ( \
368	(c)=='[' ? '\047' : \
369	(c)=='a' ? '\001' : \
370	(c)=='b' ? '\002' : \
371	(c)=='c' ? '\003' : \
372	(c)=='d' ? '\067' : \
373	(c)=='e' ? '\055' : \
374	(c)=='f' ? '\056' : \
375	(c)=='g' ? '\057' : \
376	(c)=='h' ? '\026' : \
377	(c)=='i' ? '\005' : \
378	(c)=='j' ? '\025' : \
379	(c)=='k' ? '\013' : \
380	(c)=='l' ? '\014' : \
381	(c)=='m' ? '\015' : \
382	(c)=='n' ? '\016' : \
383	(c)=='o' ? '\017' : \
384	(c)=='p' ? '\020' : \
385	(c)=='q' ? '\021' : \
386	(c)=='r' ? '\022' : \
387	(c)=='s' ? '\023' : \
388	(c)=='t' ? '\074' : \
389	(c)=='u' ? '\075' : \
390	(c)=='v' ? '\062' : \
391	(c)=='w' ? '\046' : \
392	(c)=='x' ? '\030' : \
393	(c)=='y' ? '\031' : \
394	(c)=='z' ? '\077' : \
395	(c)=='A' ? '\001' : \
396	(c)=='B' ? '\002' : \
397	(c)=='C' ? '\003' : \
398	(c)=='D' ? '\067' : \
399	(c)=='E' ? '\055' : \
400	(c)=='F' ? '\056' : \
401	(c)=='G' ? '\057' : \
402	(c)=='H' ? '\026' : \
403	(c)=='I' ? '\005' : \
404	(c)=='J' ? '\025' : \
405	(c)=='K' ? '\013' : \
406	(c)=='L' ? '\014' : \
407	(c)=='M' ? '\015' : \
408	(c)=='N' ? '\016' : \
409	(c)=='O' ? '\017' : \
410	(c)=='P' ? '\020' : \
411	(c)=='Q' ? '\021' : \
412	(c)=='R' ? '\022' : \
413	(c)=='S' ? '\023' : \
414	(c)=='T' ? '\074' : \
415	(c)=='U' ? '\075' : \
416	(c)=='V' ? '\062' : \
417	(c)=='W' ? '\046' : \
418	(c)=='X' ? '\030' : \
419	(c)=='Y' ? '\031' : \
420	(c)=='Z' ? '\077' : \
421	(c)=='|' ? '\031' : \
422	(c)=='\\' ? '\034' : \
423	(c)=='^' ? '\036' : \
424	(c)&077)
425#else
426#define	CONTROL(c)	((c)&037)
427#endif /* IS_EBCDIC_HOST */
428
429#define	ESC		CONTROL('[')
430#define	CSI		((unsigned char)'\233')
431
432#if _OSK_MWC32
433#define	LSIGNAL(sig,func)	os9_signal(sig,func)
434#else
435#define	LSIGNAL(sig,func)	signal(sig,func)
436#endif
437
438#if HAVE_SIGPROCMASK
439#if HAVE_SIGSET_T
440#else
441#undef HAVE_SIGPROCMASK
442#endif
443#endif
444#if HAVE_SIGPROCMASK
445#if HAVE_SIGEMPTYSET
446#else
447#undef  sigemptyset
448#define sigemptyset(mp) *(mp) = 0
449#endif
450#endif
451
452#define	S_INTERRUPT	01
453#define	S_STOP		02
454#define S_WINCH		04
455#define	ABORT_SIGS()	(sigs & (S_INTERRUPT|S_STOP))
456
457#define	QUIT_OK		0
458#define	QUIT_ERROR	1
459#define	QUIT_SAVED_STATUS (-1)
460
461/* filestate flags */
462#define	CH_CANSEEK	001
463#define	CH_KEEPOPEN	002
464#define	CH_POPENED	004
465#define	CH_HELPFILE	010
466
467#define	ch_zero()	((POSITION)0)
468
469#define	FAKE_HELPFILE	"@/\\less/\\help/\\file/\\@"
470
471#include "funcs.h"
472
473/* Functions not included in funcs.h */
474void postoa();
475void linenumtoa();
476void inttoa();
477