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