less.h revision 191930
160814Sps/* $FreeBSD: head/contrib/less/less.h 191930 2009-05-09 01:35:27Z delphij $ */
260786Sps/*
3191930Sdelphij * Copyright (C) 1984-2008  Mark Nudelman
460786Sps *
560786Sps * You may distribute under the terms of either the GNU General Public
660786Sps * License or the Less License, as specified in the README file.
760786Sps *
860786Sps * For more information about less, or for information on how to
960786Sps * contact the author, see the README file.
1060786Sps */
1160786Sps
12170259Sdelphij#define NEWBOT 1
1360786Sps
1460786Sps/*
1560786Sps * Standard include file for "less".
1660786Sps */
1760786Sps
1860786Sps/*
1960786Sps * Defines for MSDOS_COMPILER.
2060786Sps */
2160786Sps#define	MSOFTC		1	/* Microsoft C */
2260786Sps#define	BORLANDC	2	/* Borland C */
2360786Sps#define	WIN32C		3	/* Windows (Borland C or Microsoft C) */
2460786Sps#define	DJGPPC		4	/* DJGPP C */
2560786Sps
2660786Sps/*
2760786Sps * Include the file of compile-time options.
2860786Sps * The <> make cc search for it in -I., not srcdir.
2960786Sps */
3060786Sps#include <defines.h>
3160786Sps
3260786Sps#ifdef _SEQUENT_
3360786Sps/*
3460786Sps * Kludge for Sequent Dynix systems that have sigsetmask, but
3560786Sps * it's not compatible with the way less calls it.
3660786Sps * {{ Do other systems need this? }}
3760786Sps */
3860786Sps#undef HAVE_SIGSETMASK
3960786Sps#endif
4060786Sps
4160786Sps/*
4260786Sps * Language details.
4360786Sps */
4460786Sps#if HAVE_VOID
4560786Sps#define	VOID_POINTER	void *
4660786Sps#else
4760786Sps#define	VOID_POINTER	char *
4860786Sps#define	void  int
4960786Sps#endif
5060786Sps#if HAVE_CONST
5160786Sps#define	constant	const
5260786Sps#else
5360786Sps#define	constant
5460786Sps#endif
5560786Sps
5660786Sps#define	public		/* PUBLIC FUNCTION */
5760786Sps
5860786Sps/* Library function declarations */
5960786Sps
6060786Sps#if HAVE_SYS_TYPES_H
6160786Sps#include <sys/types.h>
6260786Sps#endif
6360786Sps#if HAVE_STDIO_H
6460786Sps#include <stdio.h>
6560786Sps#endif
6660786Sps#if HAVE_FCNTL_H
6760786Sps#include <fcntl.h>
6860786Sps#endif
6960786Sps#if HAVE_UNISTD_H
7060786Sps#include <unistd.h>
7160786Sps#endif
7260786Sps#if HAVE_CTYPE_H
7360786Sps#include <ctype.h>
7460786Sps#endif
75173685Sdelphij#if HAVE_WCTYPE_H
76173685Sdelphij#include <wctype.h>
77173685Sdelphij#endif
7889022Sps#if HAVE_LIMITS_H
7989022Sps#include <limits.h>
8089022Sps#endif
8160786Sps#if HAVE_STDLIB_H
8260786Sps#include <stdlib.h>
8360786Sps#endif
8460786Sps#if HAVE_STRING_H
8560786Sps#include <string.h>
8660786Sps#endif
87161478Sdelphij
88161478Sdelphij/* OS-specific includes */
8960786Sps#ifdef _OSK
9060786Sps#include <modes.h>
9160786Sps#include <strings.h>
9260786Sps#endif
93161478Sdelphij
94161478Sdelphij#ifdef __TANDEM
95161478Sdelphij#include <floss.h>
96161478Sdelphij#endif
97161478Sdelphij
9889022Sps#if MSDOS_COMPILER==WIN32C || OS2
9960786Sps#include <io.h>
10060786Sps#endif
101161478Sdelphij
10260786Sps#if MSDOS_COMPILER==DJGPPC
10360786Sps#include <io.h>
10460786Sps#include <sys/exceptn.h>
10560786Sps#include <conio.h>
10660786Sps#include <pc.h>
10760786Sps#endif
10860786Sps
10960786Sps#if !HAVE_STDLIB_H
11060786Spschar *getenv();
11160786Spsoff_t lseek();
11260786SpsVOID_POINTER calloc();
11360786Spsvoid free();
11460786Sps#endif
11560786Sps
11660786Sps/*
11760786Sps * Simple lowercase test which can be used during option processing
11860786Sps * (before options are parsed which might tell us what charset to use).
11960786Sps */
120161478Sdelphij#define ASCII_IS_UPPER(c)	((c) >= 'A' && (c) <= 'Z')
121161478Sdelphij#define ASCII_IS_LOWER(c)	((c) >= 'a' && (c) <= 'z')
122161478Sdelphij#define	ASCII_TO_UPPER(c)	((c) - 'a' + 'A')
123161478Sdelphij#define	ASCII_TO_LOWER(c)	((c) - 'A' + 'a')
12460786Sps
125161478Sdelphij#undef IS_UPPER
126161478Sdelphij#undef IS_LOWER
127161478Sdelphij#undef TO_UPPER
128161478Sdelphij#undef TO_LOWER
129161478Sdelphij#undef IS_SPACE
130161478Sdelphij#undef IS_DIGIT
131161478Sdelphij
132173685Sdelphij#if HAVE_WCTYPE
133173685Sdelphij#define	IS_UPPER(c)	iswupper(c)
134173685Sdelphij#define	IS_LOWER(c)	iswlower(c)
135173685Sdelphij#define	TO_UPPER(c)	towupper(c)
136173685Sdelphij#define	TO_LOWER(c)	towlower(c)
137161478Sdelphij#else
138173685Sdelphij#if HAVE_UPPER_LOWER
139161478Sdelphij#define	IS_UPPER(c)	isupper((unsigned char) (c))
140161478Sdelphij#define	IS_LOWER(c)	islower((unsigned char) (c))
141161478Sdelphij#define	TO_UPPER(c)	toupper((unsigned char) (c))
142161478Sdelphij#define	TO_LOWER(c)	tolower((unsigned char) (c))
143173685Sdelphij#else
144173685Sdelphij#define	IS_UPPER(c)	ASCII_IS_UPPER(c)
145173685Sdelphij#define	IS_LOWER(c)	ASCII_IS_LOWER(c)
146173685Sdelphij#define	TO_UPPER(c)	ASCII_TO_UPPER(c)
147173685Sdelphij#define	TO_LOWER(c)	ASCII_TO_LOWER(c)
14860786Sps#endif
149173685Sdelphij#endif
15060786Sps
151161478Sdelphij#ifdef isspace
152161478Sdelphij#define IS_SPACE(c)	isspace((unsigned char)(c))
153161478Sdelphij#else
154161478Sdelphij#define IS_SPACE(c)	((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f')
155161478Sdelphij#endif
156161478Sdelphij
157161478Sdelphij#ifdef isdigit
158161478Sdelphij#define IS_DIGIT(c)	isdigit((unsigned char)(c))
159161478Sdelphij#else
160161478Sdelphij#define IS_DIGIT(c)	((c) >= '0' && (c) <= '9')
161161478Sdelphij#endif
162161478Sdelphij
163191930Sdelphij#define IS_CSI_START(c)	(((LWCHAR)(c)) == ESC || (((LWCHAR)(c)) == CSI))
164172471Sdelphij
16560786Sps#ifndef NULL
16660786Sps#define	NULL	0
16760786Sps#endif
16860786Sps
16960786Sps#ifndef TRUE
17060786Sps#define	TRUE		1
17160786Sps#endif
17260786Sps#ifndef FALSE
17360786Sps#define	FALSE		0
17460786Sps#endif
17560786Sps
17660786Sps#define	OPT_OFF		0
17760786Sps#define	OPT_ON		1
17860786Sps#define	OPT_ONPLUS	2
17960786Sps
18060786Sps#if !HAVE_MEMCPY
18160786Sps#ifndef memcpy
18260786Sps#define	memcpy(to,from,len)	bcopy((from),(to),(len))
18360786Sps#endif
18460786Sps#endif
18560786Sps
186161478Sdelphij#if HAVE_SNPRINTF
187161478Sdelphij#define SNPRINTF1(str, size, fmt, v1)             snprintf((str), (size), (fmt), (v1))
188161478Sdelphij#define SNPRINTF2(str, size, fmt, v1, v2)         snprintf((str), (size), (fmt), (v1), (v2))
189161478Sdelphij#define SNPRINTF3(str, size, fmt, v1, v2, v3)     snprintf((str), (size), (fmt), (v1), (v2), (v3))
190161478Sdelphij#define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) snprintf((str), (size), (fmt), (v1), (v2), (v3), (v4))
191161478Sdelphij#else
192161478Sdelphij/* Use unsafe sprintf if we don't have snprintf. */
193161478Sdelphij#define SNPRINTF1(str, size, fmt, v1)             sprintf((str), (fmt), (v1))
194161478Sdelphij#define SNPRINTF2(str, size, fmt, v1, v2)         sprintf((str), (fmt), (v1), (v2))
195161478Sdelphij#define SNPRINTF3(str, size, fmt, v1, v2, v3)     sprintf((str), (fmt), (v1), (v2), (v3))
196161478Sdelphij#define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) sprintf((str), (fmt), (v1), (v2), (v3), (v4))
197161478Sdelphij#endif
198161478Sdelphij
19960786Sps#define	BAD_LSEEK	((off_t)-1)
20060786Sps
201173685Sdelphij#ifndef SEEK_SET
202173685Sdelphij#define SEEK_SET 0
203173685Sdelphij#endif
204173685Sdelphij#ifndef SEEK_END
205173685Sdelphij#define SEEK_END 2
206173685Sdelphij#endif
207173685Sdelphij
20889022Sps#ifndef CHAR_BIT
20989022Sps#define CHAR_BIT 8
21089022Sps#endif
21189022Sps
21260786Sps/*
21389022Sps * Upper bound on the string length of an integer converted to string.
21489022Sps * 302 / 1000 is ceil (log10 (2.0)).  Subtract 1 for the sign bit;
21589022Sps * add 1 for integer division truncation; add 1 more for a minus sign.
21689022Sps */
21789022Sps#define INT_STRLEN_BOUND(t) ((sizeof(t) * CHAR_BIT - 1) * 302 / 1000 + 1 + 1)
21889022Sps
21989022Sps/*
22060786Sps * Special types and constants.
22160786Sps */
222161478Sdelphijtypedef unsigned long LWCHAR;
22360814Spstypedef off_t		POSITION;
224128348Stjrtypedef off_t		LINENUM;
225128348Stjr#define MIN_LINENUM_WIDTH  7	/* Min printing width of a line number */
226161478Sdelphij#define MAX_UTF_CHAR_LEN   6	/* Max bytes in one UTF-8 char */
22760786Sps
22860786Sps#define	NULL_POSITION	((POSITION)(-1))
22960786Sps
23060786Sps/*
23160786Sps * Flags for open()
23260786Sps */
23360786Sps#if MSDOS_COMPILER || OS2
23460786Sps#define	OPEN_READ	(O_RDONLY|O_BINARY)
23560786Sps#else
23660786Sps#ifdef _OSK
23760786Sps#define	OPEN_READ	(S_IREAD)
23860786Sps#else
23960786Sps#ifdef O_RDONLY
24060786Sps#define	OPEN_READ	(O_RDONLY)
24160786Sps#else
24260786Sps#define	OPEN_READ	(0)
24360786Sps#endif
24460786Sps#endif
24560786Sps#endif
24660786Sps
24760786Sps#if defined(O_WRONLY) && defined(O_APPEND)
24860786Sps#define	OPEN_APPEND	(O_APPEND|O_WRONLY)
24960786Sps#else
25060786Sps#ifdef _OSK
25160786Sps#define OPEN_APPEND	(S_IWRITE)
25260786Sps#else
25360786Sps#define	OPEN_APPEND	(1)
25460786Sps#endif
25560786Sps#endif
25660786Sps
25760786Sps/*
25860786Sps * Set a file descriptor to binary mode.
25960786Sps */
26060786Sps#if MSDOS_COMPILER==MSOFTC
26160786Sps#define	SET_BINARY(f)	_setmode(f, _O_BINARY);
26260786Sps#else
26389022Sps#if MSDOS_COMPILER || OS2
26460786Sps#define	SET_BINARY(f)	setmode(f, O_BINARY)
26560786Sps#else
26660786Sps#define	SET_BINARY(f)
26760786Sps#endif
26860786Sps#endif
26960786Sps
27060786Sps/*
27160786Sps * Does the shell treat "?" as a metacharacter?
27260786Sps */
27360786Sps#if MSDOS_COMPILER || OS2 || _OSK
27460786Sps#define	SHELL_META_QUEST 0
27560786Sps#else
27660786Sps#define	SHELL_META_QUEST 1
27760786Sps#endif
27860786Sps
27960786Sps#define	SPACES_IN_FILENAMES 1
28060786Sps
28160786Sps/*
28260786Sps * An IFILE represents an input file.
28360786Sps */
28460786Sps#define	IFILE		VOID_POINTER
28560786Sps#define	NULL_IFILE	((IFILE)NULL)
28660786Sps
28760786Sps/*
28860786Sps * The structure used to represent a "screen position".
28960786Sps * This consists of a file position, and a screen line number.
29060786Sps * The meaning is that the line starting at the given file
29160786Sps * position is displayed on the ln-th line of the screen.
29260786Sps * (Screen lines before ln are empty.)
29360786Sps */
29460786Spsstruct scrpos
29560786Sps{
29660786Sps	POSITION pos;
29760786Sps	int ln;
29860786Sps};
29960786Sps
30060786Spstypedef union parg
30160786Sps{
30260786Sps	char *p_string;
30360786Sps	int p_int;
304128348Stjr	LINENUM p_linenum;
30560786Sps} PARG;
30660786Sps
30760786Sps#define	NULL_PARG	((PARG *)NULL)
30860786Sps
30960786Spsstruct textlist
31060786Sps{
31160786Sps	char *string;
31260786Sps	char *endstring;
31360786Sps};
31460786Sps
31560786Sps#define	EOI		(-1)
31660786Sps
31760786Sps#define	READ_INTR	(-2)
31860786Sps
319170259Sdelphij/* A fraction is represented by an int n; the fraction is n/NUM_FRAC_DENOM */
320170259Sdelphij#define NUM_FRAC_DENOM			1000000
321170259Sdelphij#define NUM_LOG_FRAC_DENOM		6
322170259Sdelphij
32360786Sps/* How quiet should we be? */
32460786Sps#define	NOT_QUIET	0	/* Ring bell at eof and for errors */
32560786Sps#define	LITTLE_QUIET	1	/* Ring bell only for errors */
32660786Sps#define	VERY_QUIET	2	/* Never ring bell */
32760786Sps
32860786Sps/* How should we prompt? */
32960786Sps#define	PR_SHORT	0	/* Prompt with colon */
33060786Sps#define	PR_MEDIUM	1	/* Prompt with message */
33160786Sps#define	PR_LONG		2	/* Prompt with longer message */
33260786Sps
33360786Sps/* How should we handle backspaces? */
33460786Sps#define	BS_SPECIAL	0	/* Do special things for underlining and bold */
33560786Sps#define	BS_NORMAL	1	/* \b treated as normal char; actually output */
33660786Sps#define	BS_CONTROL	2	/* \b treated as control char; prints as ^H */
33760786Sps
33860786Sps/* How should we search? */
339191930Sdelphij#define	SRCH_FORW       (1 << 0)  /* Search forward from current position */
340191930Sdelphij#define	SRCH_BACK       (1 << 1)  /* Search backward from current position */
341191930Sdelphij#define SRCH_NO_MOVE    (1 << 2)  /* Highlight, but don't move */
342191930Sdelphij#define SRCH_FIND_ALL   (1 << 4)  /* Find and highlight all matches */
343191930Sdelphij#define SRCH_NO_MATCH   (1 << 8)  /* Search for non-matching lines */
344191930Sdelphij#define SRCH_PAST_EOF   (1 << 9)  /* Search past end-of-file, into next file */
345191930Sdelphij#define SRCH_FIRST_FILE (1 << 10) /* Search starting at the first file */
346191930Sdelphij#define SRCH_NO_REGEX   (1 << 12) /* Don't use regular expressions */
347191930Sdelphij#define SRCH_FILTER     (1 << 13) /* Search is for '&' (filter) command */
34860786Sps
34960786Sps#define	SRCH_REVERSE(t)	(((t) & SRCH_FORW) ? \
35060786Sps				(((t) & ~SRCH_FORW) | SRCH_BACK) : \
35160786Sps				(((t) & ~SRCH_BACK) | SRCH_FORW))
35260786Sps
35360786Sps/* */
35460786Sps#define	NO_MCA		0
35560786Sps#define	MCA_DONE	1
35660786Sps#define	MCA_MORE	2
35760786Sps
35860786Sps#define	CC_OK		0	/* Char was accepted & processed */
35960786Sps#define	CC_QUIT		1	/* Char was a request to abort current cmd */
36060786Sps#define	CC_ERROR	2	/* Char could not be accepted due to error */
36160786Sps#define	CC_PASS		3	/* Char was rejected (internal) */
36260786Sps
36360786Sps#define CF_QUIT_ON_ERASE 0001   /* Abort cmd if its entirely erased */
36460786Sps
365161478Sdelphij/* Special char bit-flags used to tell put_line() to do something special */
36660786Sps#define	AT_NORMAL	(0)
367161478Sdelphij#define	AT_UNDERLINE	(1 << 0)
368161478Sdelphij#define	AT_BOLD		(1 << 1)
369161478Sdelphij#define	AT_BLINK	(1 << 2)
370161478Sdelphij#define	AT_STANDOUT	(1 << 3)
371161478Sdelphij#define	AT_ANSI		(1 << 4)  /* Content-supplied "ANSI" escape sequence */
372161478Sdelphij#define	AT_BINARY	(1 << 5)  /* LESS*BINFMT representation */
373161478Sdelphij#define	AT_HILITE	(1 << 6)  /* Internal highlights (e.g., for search) */
37460786Sps
37589022Sps#if '0' == 240
37689022Sps#define IS_EBCDIC_HOST 1
37789022Sps#endif
37889022Sps
37960786Sps#if IS_EBCDIC_HOST
38060786Sps/*
38160786Sps * Long definition for EBCDIC.
38260786Sps * Since the argument is usually a constant, this macro normally compiles
38360786Sps * into a constant.
38460786Sps */
38560786Sps#define CONTROL(c) ( \
38660786Sps	(c)=='[' ? '\047' : \
38760786Sps	(c)=='a' ? '\001' : \
38860786Sps	(c)=='b' ? '\002' : \
38960786Sps	(c)=='c' ? '\003' : \
39060786Sps	(c)=='d' ? '\067' : \
39160786Sps	(c)=='e' ? '\055' : \
39260786Sps	(c)=='f' ? '\056' : \
39360786Sps	(c)=='g' ? '\057' : \
39460786Sps	(c)=='h' ? '\026' : \
39560786Sps	(c)=='i' ? '\005' : \
39660786Sps	(c)=='j' ? '\025' : \
39760786Sps	(c)=='k' ? '\013' : \
39860786Sps	(c)=='l' ? '\014' : \
39960786Sps	(c)=='m' ? '\015' : \
40060786Sps	(c)=='n' ? '\016' : \
40160786Sps	(c)=='o' ? '\017' : \
40260786Sps	(c)=='p' ? '\020' : \
40360786Sps	(c)=='q' ? '\021' : \
40460786Sps	(c)=='r' ? '\022' : \
40560786Sps	(c)=='s' ? '\023' : \
40660786Sps	(c)=='t' ? '\074' : \
40760786Sps	(c)=='u' ? '\075' : \
40860786Sps	(c)=='v' ? '\062' : \
40960786Sps	(c)=='w' ? '\046' : \
41060786Sps	(c)=='x' ? '\030' : \
41160786Sps	(c)=='y' ? '\031' : \
41260786Sps	(c)=='z' ? '\077' : \
41360786Sps	(c)=='A' ? '\001' : \
41460786Sps	(c)=='B' ? '\002' : \
41560786Sps	(c)=='C' ? '\003' : \
41660786Sps	(c)=='D' ? '\067' : \
41760786Sps	(c)=='E' ? '\055' : \
41860786Sps	(c)=='F' ? '\056' : \
41960786Sps	(c)=='G' ? '\057' : \
42060786Sps	(c)=='H' ? '\026' : \
42160786Sps	(c)=='I' ? '\005' : \
42260786Sps	(c)=='J' ? '\025' : \
42360786Sps	(c)=='K' ? '\013' : \
42460786Sps	(c)=='L' ? '\014' : \
42560786Sps	(c)=='M' ? '\015' : \
42660786Sps	(c)=='N' ? '\016' : \
42760786Sps	(c)=='O' ? '\017' : \
42860786Sps	(c)=='P' ? '\020' : \
42960786Sps	(c)=='Q' ? '\021' : \
43060786Sps	(c)=='R' ? '\022' : \
43160786Sps	(c)=='S' ? '\023' : \
43260786Sps	(c)=='T' ? '\074' : \
43360786Sps	(c)=='U' ? '\075' : \
43460786Sps	(c)=='V' ? '\062' : \
43560786Sps	(c)=='W' ? '\046' : \
43660786Sps	(c)=='X' ? '\030' : \
43760786Sps	(c)=='Y' ? '\031' : \
43860786Sps	(c)=='Z' ? '\077' : \
43960786Sps	(c)=='|' ? '\031' : \
44060786Sps	(c)=='\\' ? '\034' : \
44160786Sps	(c)=='^' ? '\036' : \
44260786Sps	(c)&077)
44360786Sps#else
44460786Sps#define	CONTROL(c)	((c)&037)
44560786Sps#endif /* IS_EBCDIC_HOST */
44660786Sps
44760786Sps#define	ESC		CONTROL('[')
448172471Sdelphij#define	CSI		((unsigned char)'\233')
44960786Sps
45060786Sps#if _OSK_MWC32
45160786Sps#define	LSIGNAL(sig,func)	os9_signal(sig,func)
45260786Sps#else
45360786Sps#define	LSIGNAL(sig,func)	signal(sig,func)
45460786Sps#endif
45560786Sps
45663131Sps#if HAVE_SIGPROCMASK
45763131Sps#if HAVE_SIGSET_T
45863131Sps#else
45963131Sps#undef HAVE_SIGPROCMASK
46063131Sps#endif
46163131Sps#endif
46263131Sps#if HAVE_SIGPROCMASK
46363131Sps#if HAVE_SIGEMPTYSET
46463131Sps#else
46563131Sps#undef  sigemptyset
46663131Sps#define sigemptyset(mp) *(mp) = 0
46763131Sps#endif
46863131Sps#endif
46963131Sps
47060786Sps#define	S_INTERRUPT	01
47160786Sps#define	S_STOP		02
47260786Sps#define S_WINCH		04
47360786Sps#define	ABORT_SIGS()	(sigs & (S_INTERRUPT|S_STOP))
47460786Sps
47560786Sps#define	QUIT_OK		0
47660786Sps#define	QUIT_ERROR	1
47760786Sps#define	QUIT_SAVED_STATUS (-1)
47860786Sps
479173685Sdelphij#define FOLLOW_DESC     0
480173685Sdelphij#define FOLLOW_NAME     1
481173685Sdelphij
48260786Sps/* filestate flags */
48360786Sps#define	CH_CANSEEK	001
48460786Sps#define	CH_KEEPOPEN	002
48560786Sps#define	CH_POPENED	004
48660786Sps#define	CH_HELPFILE	010
48760786Sps
48860786Sps#define	ch_zero()	((POSITION)0)
48960786Sps
49060786Sps#define	FAKE_HELPFILE	"@/\\less/\\help/\\file/\\@"
49160786Sps
49260786Sps#include "funcs.h"
49360786Sps
494128348Stjr/* Functions not included in funcs.h */
495128348Stjrvoid postoa();
496128348Stjrvoid linenumtoa();
497128348Stjrvoid inttoa();
498