1181053Srwatson/*
2180701Srwatson * Copyright (C) 1984-2012  Mark Nudelman
3155192Srwatson *
4155192Srwatson * You may distribute under the terms of either the GNU General Public
5155192Srwatson * License or the Less License, as specified in the README file.
6155192Srwatson *
7155192Srwatson * For more information, see the README file.
8155192Srwatson */
9155192Srwatson
10155192Srwatson#define NEWBOT 1
11155192Srwatson
12155192Srwatson/*
13180701Srwatson * Standard include file for "less".
14155192Srwatson */
15155192Srwatson
16155192Srwatson/*
17155192Srwatson * Defines for MSDOS_COMPILER.
18155192Srwatson */
19155192Srwatson#define	MSOFTC		1	/* Microsoft C */
20155192Srwatson#define	BORLANDC	2	/* Borland C */
21155192Srwatson#define	WIN32C		3	/* Windows (Borland C or Microsoft C) */
22155192Srwatson#define	DJGPPC		4	/* DJGPP C */
23155192Srwatson
24155192Srwatson/*
25155192Srwatson * Include the file of compile-time options.
26155192Srwatson * The <> make cc search for it in -I., not srcdir.
27155192Srwatson */
28155192Srwatson#include <defines.h>
29155192Srwatson
30155192Srwatson#ifdef _SEQUENT_
31155192Srwatson/*
32155192Srwatson * Kludge for Sequent Dynix systems that have sigsetmask, but
33155192Srwatson * it's not compatible with the way less calls it.
34155192Srwatson * {{ Do other systems need this? }}
35155192Srwatson */
36155192Srwatson#undef HAVE_SIGSETMASK
37156882Srwatson#endif
38178617Srwatson
39155192Srwatson/*
40155192Srwatson * Language details.
41155192Srwatson */
42155192Srwatson#if HAVE_VOID
43155192Srwatson#define	VOID_POINTER	void *
44155192Srwatson#else
45155192Srwatson#define	VOID_POINTER	char *
46155192Srwatson#define	void  int
47155192Srwatson#endif
48155192Srwatson#if HAVE_CONST
49155192Srwatson#define	constant	const
50155192Srwatson#else
51170196Srwatson#define	constant
52170196Srwatson#endif
53170196Srwatson
54170196Srwatson#define	public		/* PUBLIC FUNCTION */
55155192Srwatson
56155192Srwatson/* Library function declarations */
57155192Srwatson
58155192Srwatson#if HAVE_SYS_TYPES_H
59155192Srwatson#include <sys/types.h>
60155192Srwatson#endif
61155192Srwatson#if HAVE_STDIO_H
62155192Srwatson#include <stdio.h>
63155192Srwatson#endif
64155192Srwatson#if HAVE_FCNTL_H
65155192Srwatson#include <fcntl.h>
66155192Srwatson#endif
67161970Srwatson#if HAVE_UNISTD_H
68161970Srwatson#include <unistd.h>
69161970Srwatson#endif
70161970Srwatson#if HAVE_CTYPE_H
71161970Srwatson#include <ctype.h>
72161970Srwatson#endif
73161970Srwatson#if HAVE_WCTYPE_H
74161970Srwatson#include <wctype.h>
75161970Srwatson#endif
76161970Srwatson#if HAVE_LIMITS_H
77161970Srwatson#include <limits.h>
78161970Srwatson#endif
79161970Srwatson#if HAVE_STDLIB_H
80161970Srwatson#include <stdlib.h>
81161970Srwatson#endif
82161970Srwatson#if HAVE_STRING_H
83161970Srwatson#include <string.h>
84161970Srwatson#endif
85161970Srwatson
86161970Srwatson/* OS-specific includes */
87161970Srwatson#ifdef _OSK
88161970Srwatson#include <modes.h>
89161970Srwatson#include <strings.h>
90168688Scsjp#endif
91161970Srwatson
92161970Srwatson#ifdef __TANDEM
93161970Srwatson#include <floss.h>
94161970Srwatson#endif
95161970Srwatson
96161970Srwatson#if MSDOS_COMPILER==WIN32C || OS2
97161970Srwatson#include <io.h>
98161970Srwatson#endif
99161970Srwatson
100161970Srwatson#if MSDOS_COMPILER==DJGPPC
101161970Srwatson#include <io.h>
102161970Srwatson#include <sys/exceptn.h>
103161970Srwatson#include <conio.h>
104161970Srwatson#include <pc.h>
105161970Srwatson#endif
106161970Srwatson
107161970Srwatson#if !HAVE_STDLIB_H
108161970Srwatsonchar *getenv();
109161970Srwatsonoff_t lseek();
110161970SrwatsonVOID_POINTER calloc();
111161970Srwatsonvoid free();
112161970Srwatson#endif
113155192Srwatson
114161813Swsalamon/*
115161813Swsalamon * Simple lowercase test which can be used during option processing
116161813Swsalamon * (before options are parsed which might tell us what charset to use).
117161970Srwatson */
118161970Srwatson#define ASCII_IS_UPPER(c)	((c) >= 'A' && (c) <= 'Z')
119155192Srwatson#define ASCII_IS_LOWER(c)	((c) >= 'a' && (c) <= 'z')
120156889Srwatson#define	ASCII_TO_UPPER(c)	((c) - 'a' + 'A')
121156889Srwatson#define	ASCII_TO_LOWER(c)	((c) - 'A' + 'a')
122155192Srwatson
123155192Srwatson#undef IS_UPPER
124170196Srwatson#undef IS_LOWER
125170196Srwatson#undef TO_UPPER
126170196Srwatson#undef TO_LOWER
127155192Srwatson#undef IS_SPACE
128155192Srwatson#undef IS_DIGIT
129155192Srwatson
130155192Srwatson#if HAVE_WCTYPE
131155192Srwatson#define	IS_UPPER(c)	iswupper(c)
132156889Srwatson#define	IS_LOWER(c)	iswlower(c)
133156889Srwatson#define	TO_UPPER(c)	towupper(c)
134156889Srwatson#define	TO_LOWER(c)	towlower(c)
135156889Srwatson#else
136156889Srwatson#if HAVE_UPPER_LOWER
137156889Srwatson#define	IS_UPPER(c)	isupper((unsigned char) (c))
138156889Srwatson#define	IS_LOWER(c)	islower((unsigned char) (c))
139156889Srwatson#define	TO_UPPER(c)	toupper((unsigned char) (c))
140156889Srwatson#define	TO_LOWER(c)	tolower((unsigned char) (c))
141156889Srwatson#else
142156889Srwatson#define	IS_UPPER(c)	ASCII_IS_UPPER(c)
143156889Srwatson#define	IS_LOWER(c)	ASCII_IS_LOWER(c)
144156889Srwatson#define	TO_UPPER(c)	ASCII_TO_UPPER(c)
145156889Srwatson#define	TO_LOWER(c)	ASCII_TO_LOWER(c)
146156889Srwatson#endif
147156889Srwatson#endif
148156889Srwatson
149156889Srwatson#ifdef isspace
150156889Srwatson#define IS_SPACE(c)	isspace((unsigned char)(c))
151156889Srwatson#else
152156889Srwatson#define IS_SPACE(c)	((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f')
153156889Srwatson#endif
154156889Srwatson
155156889Srwatson#ifdef isdigit
156156889Srwatson#define IS_DIGIT(c)	isdigit((unsigned char)(c))
157160086Srwatson#else
158156889Srwatson#define IS_DIGIT(c)	((c) >= '0' && (c) <= '9')
159156889Srwatson#endif
160171066Scsjp
161156889Srwatson#define IS_CSI_START(c)	(((LWCHAR)(c)) == ESC || (((LWCHAR)(c)) == CSI))
162156889Srwatson
163156889Srwatson#ifndef NULL
164156889Srwatson#define	NULL	0
165156889Srwatson#endif
166156889Srwatson
167156889Srwatson#ifndef TRUE
168156889Srwatson#define	TRUE		1
169156889Srwatson#endif
170156889Srwatson#ifndef FALSE
171156889Srwatson#define	FALSE		0
172161813Swsalamon#endif
173161813Swsalamon
174156889Srwatson#define	OPT_OFF		0
175170407Srwatson#define	OPT_ON		1
176170407Srwatson#define	OPT_ONPLUS	2
177170407Srwatson
178170407Srwatson#if !HAVE_MEMCPY
179170407Srwatson#ifndef memcpy
180172995Scsjp#define	memcpy(to,from,len)	bcopy((from),(to),(len))
181156889Srwatson#endif
182156889Srwatson#endif
183155192Srwatson
184155192Srwatson#if HAVE_SNPRINTF
185195104Srwatson#define SNPRINTF1(str, size, fmt, v1)             snprintf((str), (size), (fmt), (v1))
186155192Srwatson#define SNPRINTF2(str, size, fmt, v1, v2)         snprintf((str), (size), (fmt), (v1), (v2))
187155192Srwatson#define SNPRINTF3(str, size, fmt, v1, v2, v3)     snprintf((str), (size), (fmt), (v1), (v2), (v3))
188195104Srwatson#define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) snprintf((str), (size), (fmt), (v1), (v2), (v3), (v4))
189195104Srwatson#else
190195104Srwatson/* Use unsafe sprintf if we don't have snprintf. */
191195104Srwatson#define SNPRINTF1(str, size, fmt, v1)             sprintf((str), (fmt), (v1))
192195104Srwatson#define SNPRINTF2(str, size, fmt, v1, v2)         sprintf((str), (fmt), (v1), (v2))
193156889Srwatson#define SNPRINTF3(str, size, fmt, v1, v2, v3)     sprintf((str), (fmt), (v1), (v2), (v3))
194155192Srwatson#define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) sprintf((str), (fmt), (v1), (v2), (v3), (v4))
195195104Srwatson#endif
196195104Srwatson
197195104Srwatson#define	BAD_LSEEK	((off_t)-1)
198195104Srwatson
199195104Srwatson#ifndef SEEK_SET
200195104Srwatson#define SEEK_SET 0
201195104Srwatson#endif
202195104Srwatson#ifndef SEEK_END
203195104Srwatson#define SEEK_END 2
204195104Srwatson#endif
205195104Srwatson
206195104Srwatson#ifndef CHAR_BIT
207195104Srwatson#define CHAR_BIT 8
208195104Srwatson#endif
209195104Srwatson
210195104Srwatson/*
211195104Srwatson * Upper bound on the string length of an integer converted to string.
212195104Srwatson * 302 / 1000 is ceil (log10 (2.0)).  Subtract 1 for the sign bit;
213195104Srwatson * add 1 for integer division truncation; add 1 more for a minus sign.
214195104Srwatson */
215195104Srwatson#define INT_STRLEN_BOUND(t) ((sizeof(t) * CHAR_BIT - 1) * 302 / 1000 + 1 + 1)
216195104Srwatson
217195104Srwatson/*
218195104Srwatson * Special types and constants.
219195104Srwatson */
220195104Srwatsontypedef unsigned long LWCHAR;
221195104Srwatsontypedef off_t		POSITION;
222195104Srwatsontypedef off_t		LINENUM;
223195104Srwatson#define MIN_LINENUM_WIDTH  7	/* Min printing width of a line number */
224195104Srwatson#define MAX_UTF_CHAR_LEN   6	/* Max bytes in one UTF-8 char */
225195104Srwatson
226195104Srwatson#define	NULL_POSITION	((POSITION)(-1))
227195104Srwatson
228195104Srwatson/*
229195104Srwatson * Flags for open()
230195104Srwatson */
231195104Srwatson#if MSDOS_COMPILER || OS2
232195104Srwatson#define	OPEN_READ	(O_RDONLY|O_BINARY)
233195104Srwatson#else
234195104Srwatson#ifdef _OSK
235195104Srwatson#define	OPEN_READ	(S_IREAD)
236195104Srwatson#else
237195104Srwatson#ifdef O_RDONLY
238195104Srwatson#define	OPEN_READ	(O_RDONLY)
239195104Srwatson#else
240195104Srwatson#define	OPEN_READ	(0)
241195104Srwatson#endif
242195104Srwatson#endif
243195104Srwatson#endif
244195104Srwatson
245195104Srwatson#if defined(O_WRONLY) && defined(O_APPEND)
246195104Srwatson#define	OPEN_APPEND	(O_APPEND|O_WRONLY)
247195104Srwatson#else
248195104Srwatson#ifdef _OSK
249195104Srwatson#define OPEN_APPEND	(S_IWRITE)
250195104Srwatson#else
251195104Srwatson#define	OPEN_APPEND	(1)
252195104Srwatson#endif
253195104Srwatson#endif
254195104Srwatson
255195104Srwatson/*
256195104Srwatson * Set a file descriptor to binary mode.
257195104Srwatson */
258195104Srwatson#if MSDOS_COMPILER==MSOFTC
259195104Srwatson#define	SET_BINARY(f)	_setmode(f, _O_BINARY);
260195104Srwatson#else
261195104Srwatson#if MSDOS_COMPILER || OS2
262195104Srwatson#define	SET_BINARY(f)	setmode(f, O_BINARY)
263195104Srwatson#else
264195104Srwatson#define	SET_BINARY(f)
265195104Srwatson#endif
266195104Srwatson#endif
267195104Srwatson
268195104Srwatson/*
269195104Srwatson * Does the shell treat "?" as a metacharacter?
270195104Srwatson */
271195104Srwatson#if MSDOS_COMPILER || OS2 || _OSK
272195104Srwatson#define	SHELL_META_QUEST 0
273195104Srwatson#else
274195104Srwatson#define	SHELL_META_QUEST 1
275195104Srwatson#endif
276195104Srwatson
277195104Srwatson#define	SPACES_IN_FILENAMES 1
278195104Srwatson
279195104Srwatson/*
280195104Srwatson * An IFILE represents an input file.
281195104Srwatson */
282195104Srwatson#define	IFILE		VOID_POINTER
283195104Srwatson#define	NULL_IFILE	((IFILE)NULL)
284195104Srwatson
285195104Srwatson/*
286195104Srwatson * The structure used to represent a "screen position".
287195104Srwatson * This consists of a file position, and a screen line number.
288195104Srwatson * The meaning is that the line starting at the given file
289195104Srwatson * position is displayed on the ln-th line of the screen.
290195104Srwatson * (Screen lines before ln are empty.)
291195104Srwatson */
292195104Srwatsonstruct scrpos
293195104Srwatson{
294195104Srwatson	POSITION pos;
295195104Srwatson	int ln;
296195104Srwatson};
297195104Srwatson
298195104Srwatsontypedef union parg
299195104Srwatson{
300195104Srwatson	char *p_string;
301195104Srwatson	int p_int;
302195104Srwatson	LINENUM p_linenum;
303195104Srwatson} PARG;
304195104Srwatson
305195104Srwatson#define	NULL_PARG	((PARG *)NULL)
306195104Srwatson
307195104Srwatsonstruct textlist
308195104Srwatson{
309195104Srwatson	char *string;
310195104Srwatson	char *endstring;
311195104Srwatson};
312195104Srwatson
313195104Srwatson#define	EOI		(-1)
314195104Srwatson
315195104Srwatson#define	READ_INTR	(-2)
316195104Srwatson
317195104Srwatson/* A fraction is represented by an int n; the fraction is n/NUM_FRAC_DENOM */
318195104Srwatson#define NUM_FRAC_DENOM			1000000
319195104Srwatson#define NUM_LOG_FRAC_DENOM		6
320195104Srwatson
321195104Srwatson/* How quiet should we be? */
322195104Srwatson#define	NOT_QUIET	0	/* Ring bell at eof and for errors */
323195104Srwatson#define	LITTLE_QUIET	1	/* Ring bell only for errors */
324195104Srwatson#define	VERY_QUIET	2	/* Never ring bell */
325195104Srwatson
326195104Srwatson/* How should we prompt? */
327195104Srwatson#define	PR_SHORT	0	/* Prompt with colon */
328195104Srwatson#define	PR_MEDIUM	1	/* Prompt with message */
329195104Srwatson#define	PR_LONG		2	/* Prompt with longer message */
330161970Srwatson
331155192Srwatson/* How should we handle backspaces? */
332155192Srwatson#define	BS_SPECIAL	0	/* Do special things for underlining and bold */
333155192Srwatson#define	BS_NORMAL	1	/* \b treated as normal char; actually output */
334156889Srwatson#define	BS_CONTROL	2	/* \b treated as control char; prints as ^H */
335155192Srwatson
336155192Srwatson/* How should we search? */
337155192Srwatson#define	SRCH_FORW       (1 << 0)  /* Search forward from current position */
338186822Srwatson#define	SRCH_BACK       (1 << 1)  /* Search backward from current position */
339186822Srwatson#define SRCH_NO_MOVE    (1 << 2)  /* Highlight, but don't move */
340155192Srwatson#define SRCH_FIND_ALL   (1 << 4)  /* Find and highlight all matches */
341161970Srwatson#define SRCH_NO_MATCH   (1 << 8)  /* Search for non-matching lines */
342189570Srwatson#define SRCH_PAST_EOF   (1 << 9)  /* Search past end-of-file, into next file */
343155192Srwatson#define SRCH_FIRST_FILE (1 << 10) /* Search starting at the first file */
344156889Srwatson#define SRCH_NO_REGEX   (1 << 12) /* Don't use regular expressions */
345155192Srwatson#define SRCH_FILTER     (1 << 13) /* Search is for '&' (filter) command */
346155192Srwatson#define SRCH_AFTER_TARGET (1 << 14) /* Start search after the target line */
347155192Srwatson
348155192Srwatson#define	SRCH_REVERSE(t)	(((t) & SRCH_FORW) ? \
349155192Srwatson				(((t) & ~SRCH_FORW) | SRCH_BACK) : \
350189570Srwatson				(((t) & ~SRCH_BACK) | SRCH_FORW))
351155192Srwatson
352156889Srwatson/* */
353155192Srwatson#define	NO_MCA		0
354155192Srwatson#define	MCA_DONE	1
355155192Srwatson#define	MCA_MORE	2
356195104Srwatson
357195104Srwatson#define	CC_OK		0	/* Char was accepted & processed */
358195104Srwatson#define	CC_QUIT		1	/* Char was a request to abort current cmd */
359195104Srwatson#define	CC_ERROR	2	/* Char could not be accepted due to error */
360195104Srwatson#define	CC_PASS		3	/* Char was rejected (internal) */
361195104Srwatson
362195104Srwatson#define CF_QUIT_ON_ERASE 0001   /* Abort cmd if its entirely erased */
363195104Srwatson
364195104Srwatson/* Special char bit-flags used to tell put_line() to do something special */
365195104Srwatson#define	AT_NORMAL	(0)
366195104Srwatson#define	AT_UNDERLINE	(1 << 0)
367195104Srwatson#define	AT_BOLD		(1 << 1)
368195104Srwatson#define	AT_BLINK	(1 << 2)
369195104Srwatson#define	AT_STANDOUT	(1 << 3)
370195104Srwatson#define	AT_ANSI		(1 << 4)  /* Content-supplied "ANSI" escape sequence */
371195104Srwatson#define	AT_BINARY	(1 << 5)  /* LESS*BINFMT representation */
372195104Srwatson#define	AT_HILITE	(1 << 6)  /* Internal highlights (e.g., for search) */
373195104Srwatson
374195104Srwatson#if '0' == 240
375195104Srwatson#define IS_EBCDIC_HOST 1
376195104Srwatson#endif
377195104Srwatson
378195104Srwatson#if IS_EBCDIC_HOST
379195104Srwatson/*
380195104Srwatson * Long definition for EBCDIC.
381195104Srwatson * Since the argument is usually a constant, this macro normally compiles
382195104Srwatson * into a constant.
383195104Srwatson */
384155192Srwatson#define CONTROL(c) ( \
385195104Srwatson	(c)=='[' ? '\047' : \
386195104Srwatson	(c)=='a' ? '\001' : \
387155192Srwatson	(c)=='b' ? '\002' : \
388195104Srwatson	(c)=='c' ? '\003' : \
389155192Srwatson	(c)=='d' ? '\067' : \
390155192Srwatson	(c)=='e' ? '\055' : \
391155192Srwatson	(c)=='f' ? '\056' : \
392156882Srwatson	(c)=='g' ? '\057' : \
393	(c)=='h' ? '\026' : \
394	(c)=='i' ? '\005' : \
395	(c)=='j' ? '\025' : \
396	(c)=='k' ? '\013' : \
397	(c)=='l' ? '\014' : \
398	(c)=='m' ? '\015' : \
399	(c)=='n' ? '\016' : \
400	(c)=='o' ? '\017' : \
401	(c)=='p' ? '\020' : \
402	(c)=='q' ? '\021' : \
403	(c)=='r' ? '\022' : \
404	(c)=='s' ? '\023' : \
405	(c)=='t' ? '\074' : \
406	(c)=='u' ? '\075' : \
407	(c)=='v' ? '\062' : \
408	(c)=='w' ? '\046' : \
409	(c)=='x' ? '\030' : \
410	(c)=='y' ? '\031' : \
411	(c)=='z' ? '\077' : \
412	(c)=='A' ? '\001' : \
413	(c)=='B' ? '\002' : \
414	(c)=='C' ? '\003' : \
415	(c)=='D' ? '\067' : \
416	(c)=='E' ? '\055' : \
417	(c)=='F' ? '\056' : \
418	(c)=='G' ? '\057' : \
419	(c)=='H' ? '\026' : \
420	(c)=='I' ? '\005' : \
421	(c)=='J' ? '\025' : \
422	(c)=='K' ? '\013' : \
423	(c)=='L' ? '\014' : \
424	(c)=='M' ? '\015' : \
425	(c)=='N' ? '\016' : \
426	(c)=='O' ? '\017' : \
427	(c)=='P' ? '\020' : \
428	(c)=='Q' ? '\021' : \
429	(c)=='R' ? '\022' : \
430	(c)=='S' ? '\023' : \
431	(c)=='T' ? '\074' : \
432	(c)=='U' ? '\075' : \
433	(c)=='V' ? '\062' : \
434	(c)=='W' ? '\046' : \
435	(c)=='X' ? '\030' : \
436	(c)=='Y' ? '\031' : \
437	(c)=='Z' ? '\077' : \
438	(c)=='|' ? '\031' : \
439	(c)=='\\' ? '\034' : \
440	(c)=='^' ? '\036' : \
441	(c)&077)
442#else
443#define	CONTROL(c)	((c)&037)
444#endif /* IS_EBCDIC_HOST */
445
446#define	ESC		CONTROL('[')
447#define	CSI		((unsigned char)'\233')
448
449#if _OSK_MWC32
450#define	LSIGNAL(sig,func)	os9_signal(sig,func)
451#else
452#define	LSIGNAL(sig,func)	signal(sig,func)
453#endif
454
455#if HAVE_SIGPROCMASK
456#if HAVE_SIGSET_T
457#else
458#undef HAVE_SIGPROCMASK
459#endif
460#endif
461#if HAVE_SIGPROCMASK
462#if HAVE_SIGEMPTYSET
463#else
464#undef  sigemptyset
465#define sigemptyset(mp) *(mp) = 0
466#endif
467#endif
468
469#define	S_INTERRUPT	01
470#define	S_STOP		02
471#define S_WINCH		04
472#define	ABORT_SIGS()	(sigs & (S_INTERRUPT|S_STOP))
473
474#define	QUIT_OK		0
475#define	QUIT_ERROR	1
476#define	QUIT_INTERRUPT	2
477#define	QUIT_SAVED_STATUS (-1)
478
479#define FOLLOW_DESC     0
480#define FOLLOW_NAME     1
481
482/* filestate flags */
483#define	CH_CANSEEK	001
484#define	CH_KEEPOPEN	002
485#define	CH_POPENED	004
486#define	CH_HELPFILE	010
487#define	CH_NODATA  	020	/* Special case for zero length files */
488
489
490#define	ch_zero()	((POSITION)0)
491
492#define	FAKE_HELPFILE	"@/\\less/\\help/\\file/\\@"
493#define FAKE_EMPTYFILE	"@/\\less/\\empty/\\file/\\@"
494
495/* Flags for cvt_text */
496#define	CVT_TO_LC	01	/* Convert upper-case to lower-case */
497#define	CVT_BS		02	/* Do backspace processing */
498#define	CVT_CRLF	04	/* Remove CR after LF */
499#define	CVT_ANSI	010	/* Remove ANSI escape sequences */
500
501#include "funcs.h"
502
503/* Functions not included in funcs.h */
504void postoa();
505void linenumtoa();
506void inttoa();
507