xref: /openbsd-src/usr.bin/less/less.h (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /*
2  * Copyright (C) 1984-2012  Mark Nudelman
3  * Modified for use with illumos by Garrett D'Amore.
4  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
5  *
6  * You may distribute under the terms of either the GNU General Public
7  * License or the Less License, as specified in the README file.
8  *
9  * For more information, see the README file.
10  */
11 
12 /*
13  * Standard include file for "less".
14  */
15 
16 #include "defines.h"
17 
18 #include <sys/types.h>
19 
20 #include <ctype.h>
21 #include <fcntl.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <wctype.h>
29 
30 /*
31  * Simple lowercase test which can be used during option processing
32  * (before options are parsed which might tell us what charset to use).
33  */
34 
35 #undef IS_SPACE
36 #undef IS_DIGIT
37 
38 #define	IS_SPACE(c)	isspace((unsigned char)(c))
39 #define	IS_DIGIT(c)	isdigit((unsigned char)(c))
40 
41 #define	IS_CSI_START(c)	(((LWCHAR)(c)) == ESC || (((LWCHAR)(c)) == CSI))
42 
43 #ifndef TRUE
44 #define	TRUE		1
45 #endif
46 #ifndef FALSE
47 #define	FALSE		0
48 #endif
49 
50 #define	OPT_OFF		0
51 #define	OPT_ON		1
52 #define	OPT_ONPLUS	2
53 
54 /*
55  * Special types and constants.
56  */
57 typedef unsigned long LWCHAR;
58 #define	MIN_LINENUM_WIDTH  7	/* Min printing width of a line number */
59 #define	MAX_UTF_CHAR_LEN   6	/* Max bytes in one UTF-8 char */
60 
61 #define	SHELL_META_QUEST 1
62 
63 /*
64  * An IFILE represents an input file.
65  */
66 #define	IFILE		void *
67 
68 /*
69  * The structure used to represent a "screen position".
70  * This consists of a file position, and a screen line number.
71  * The meaning is that the line starting at the given file
72  * position is displayed on the ln-th line of the screen.
73  * (Screen lines before ln are empty.)
74  */
75 struct scrpos {
76 	off_t pos;
77 	int ln;
78 };
79 
80 typedef union parg {
81 	char *p_string;
82 	int p_int;
83 	off_t p_linenum;
84 } PARG;
85 
86 struct textlist {
87 	char *string;
88 	char *endstring;
89 };
90 
91 #define	EOI		(-1)
92 
93 #define	READ_INTR	(-2)
94 
95 /* A fraction is represented by an int n; the fraction is n/NUM_FRAC_DENOM */
96 #define	NUM_FRAC_DENOM			1000000
97 #define	NUM_LOG_FRAC_DENOM		6
98 
99 /* How quiet should we be? */
100 #define	NOT_QUIET	0	/* Ring bell at eof and for errors */
101 #define	LITTLE_QUIET	1	/* Ring bell only for errors */
102 #define	VERY_QUIET	2	/* Never ring bell */
103 
104 /* How should we prompt? */
105 #define	PR_SHORT	0	/* Prompt with colon */
106 #define	PR_MEDIUM	1	/* Prompt with message */
107 #define	PR_LONG		2	/* Prompt with longer message */
108 
109 /* How should we handle backspaces? */
110 #define	BS_SPECIAL	0	/* Do special things for underlining and bold */
111 #define	BS_NORMAL	1	/* \b treated as normal char; actually output */
112 #define	BS_CONTROL	2	/* \b treated as control char; prints as ^H */
113 
114 /* How should we search? */
115 #define	SRCH_FORW	(1 << 0)  /* Search forward from current position */
116 #define	SRCH_BACK	(1 << 1)  /* Search backward from current position */
117 #define	SRCH_NO_MOVE	(1 << 2)  /* Highlight, but don't move */
118 #define	SRCH_FIND_ALL	(1 << 4)  /* Find and highlight all matches */
119 #define	SRCH_NO_MATCH	(1 << 8)  /* Search for non-matching lines */
120 #define	SRCH_PAST_EOF	(1 << 9)  /* Search past end-of-file, into next file */
121 #define	SRCH_FIRST_FILE	(1 << 10) /* Search starting at the first file */
122 #define	SRCH_NO_REGEX	(1 << 12) /* Don't use regular expressions */
123 #define	SRCH_FILTER	(1 << 13) /* Search is for '&' (filter) command */
124 #define	SRCH_AFTER_TARGET (1 << 14) /* Start search after the target line */
125 
126 #define	SRCH_REVERSE(t)	(((t) & SRCH_FORW) ? \
127 				(((t) & ~SRCH_FORW) | SRCH_BACK) : \
128 				(((t) & ~SRCH_BACK) | SRCH_FORW))
129 
130 /* */
131 #define	NO_MCA		0
132 #define	MCA_DONE	1
133 #define	MCA_MORE	2
134 
135 #define	CC_OK		0	/* Char was accepted & processed */
136 #define	CC_QUIT		1	/* Char was a request to abort current cmd */
137 #define	CC_ERROR	2	/* Char could not be accepted due to error */
138 #define	CC_PASS		3	/* Char was rejected (internal) */
139 
140 #define	CF_QUIT_ON_ERASE 0001   /* Abort cmd if its entirely erased */
141 
142 /* Special char bit-flags used to tell put_line() to do something special */
143 #define	AT_NORMAL	(0)
144 #define	AT_UNDERLINE	(1 << 0)
145 #define	AT_BOLD		(1 << 1)
146 #define	AT_BLINK	(1 << 2)
147 #define	AT_STANDOUT	(1 << 3)
148 #define	AT_ANSI		(1 << 4)  /* Content-supplied "ANSI" escape sequence */
149 #define	AT_BINARY	(1 << 5)  /* LESS*BINFMT representation */
150 #define	AT_HILITE	(1 << 6)  /* Internal highlights (e.g., for search) */
151 #define	AT_INDET	(1 << 7)  /* Indeterminate: either bold or underline */
152 
153 #define	CONTROL(c)	((c)&037)
154 
155 #define	ESC		CONTROL('[')
156 #define	CSI		((unsigned char)'\233')
157 
158 #define	S_INTERRUPT	01
159 #define	S_STOP		02
160 #define	S_WINCH		04
161 #define	ABORT_SIGS()	(sigs & (S_INTERRUPT|S_STOP))
162 
163 #define	QUIT_OK		0
164 #define	QUIT_ERROR	1
165 #define	QUIT_INTERRUPT	2
166 #define	QUIT_SAVED_STATUS (-1)
167 
168 #define	FOLLOW_DESC	0
169 #define	FOLLOW_NAME	1
170 
171 /* filestate flags */
172 #define	CH_CANSEEK	001
173 #define	CH_KEEPOPEN	002
174 #define	CH_POPENED	004
175 #define	CH_HELPFILE	010
176 #define	CH_NODATA	020	/* Special case for zero length files */
177 
178 
179 #define	ch_zero()	(0)
180 
181 #define	FAKE_EMPTYFILE	"@/\\less/\\empty/\\file/\\@"
182 
183 /* Flags for cvt_text */
184 #define	CVT_TO_LC	01	/* Convert upper-case to lower-case */
185 #define	CVT_BS		02	/* Do backspace processing */
186 #define	CVT_CRLF	04	/* Remove CR after LF */
187 #define	CVT_ANSI	010	/* Remove ANSI escape sequences */
188 
189 #include "funcs.h"
190 
191 /* Functions not included in funcs.h */
192 void postoa(off_t, char *, size_t);
193 void inttoa(int, char *, size_t);
194