1 /* $FreeBSD$ */ 2 /* 3 * Copyright (C) 1984-2005 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 84 /* OS-specific includes */ 85 #ifdef _OSK 86 #include <modes.h> 87 #include <strings.h> 88 #endif 89 90 #ifdef __TANDEM 91 #include <floss.h> 92 #endif 93 94 #if MSDOS_COMPILER==WIN32C || OS2 95 #include <io.h> 96 #endif 97 98 #if MSDOS_COMPILER==DJGPPC 99 #include <io.h> 100 #include <sys/exceptn.h> 101 #include <conio.h> 102 #include <pc.h> 103 #endif 104 105 #if !HAVE_STDLIB_H 106 char *getenv(); 107 off_t lseek(); 108 VOID_POINTER calloc(); 109 void free(); 110 #endif 111 112 /* 113 * Simple lowercase test which can be used during option processing 114 * (before options are parsed which might tell us what charset to use). 115 */ 116 #define ASCII_IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z') 117 #define ASCII_IS_LOWER(c) ((c) >= 'a' && (c) <= 'z') 118 #define ASCII_TO_UPPER(c) ((c) - 'a' + 'A') 119 #define ASCII_TO_LOWER(c) ((c) - 'A' + 'a') 120 121 #undef IS_UPPER 122 #undef IS_LOWER 123 #undef TO_UPPER 124 #undef TO_LOWER 125 #undef IS_SPACE 126 #undef IS_DIGIT 127 128 #if !HAVE_UPPER_LOWER 129 #define IS_UPPER(c) ASCII_IS_UPPER(c) 130 #define IS_LOWER(c) ASCII_IS_LOWER(c) 131 #define TO_UPPER(c) ASCII_TO_UPPER(c) 132 #define TO_LOWER(c) ASCII_TO_LOWER(c) 133 #else 134 #define IS_UPPER(c) isupper((unsigned char) (c)) 135 #define IS_LOWER(c) islower((unsigned char) (c)) 136 #define TO_UPPER(c) toupper((unsigned char) (c)) 137 #define TO_LOWER(c) tolower((unsigned char) (c)) 138 #endif 139 140 #ifdef isspace 141 #define IS_SPACE(c) isspace((unsigned char)(c)) 142 #else 143 #define IS_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f') 144 #endif 145 146 #ifdef isdigit 147 #define IS_DIGIT(c) isdigit((unsigned char)(c)) 148 #else 149 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9') 150 #endif 151 152 #ifndef NULL 153 #define NULL 0 154 #endif 155 156 #ifndef TRUE 157 #define TRUE 1 158 #endif 159 #ifndef FALSE 160 #define FALSE 0 161 #endif 162 163 #define OPT_OFF 0 164 #define OPT_ON 1 165 #define OPT_ONPLUS 2 166 167 #if !HAVE_MEMCPY 168 #ifndef memcpy 169 #define memcpy(to,from,len) bcopy((from),(to),(len)) 170 #endif 171 #endif 172 173 #if HAVE_SNPRINTF 174 #define SNPRINTF1(str, size, fmt, v1) snprintf((str), (size), (fmt), (v1)) 175 #define SNPRINTF2(str, size, fmt, v1, v2) snprintf((str), (size), (fmt), (v1), (v2)) 176 #define SNPRINTF3(str, size, fmt, v1, v2, v3) snprintf((str), (size), (fmt), (v1), (v2), (v3)) 177 #define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) snprintf((str), (size), (fmt), (v1), (v2), (v3), (v4)) 178 #else 179 /* Use unsafe sprintf if we don't have snprintf. */ 180 #define SNPRINTF1(str, size, fmt, v1) sprintf((str), (fmt), (v1)) 181 #define SNPRINTF2(str, size, fmt, v1, v2) sprintf((str), (fmt), (v1), (v2)) 182 #define SNPRINTF3(str, size, fmt, v1, v2, v3) sprintf((str), (fmt), (v1), (v2), (v3)) 183 #define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) sprintf((str), (fmt), (v1), (v2), (v3), (v4)) 184 #endif 185 186 #define BAD_LSEEK ((off_t)-1) 187 188 #ifndef CHAR_BIT 189 #define CHAR_BIT 8 190 #endif 191 192 /* 193 * Upper bound on the string length of an integer converted to string. 194 * 302 / 1000 is ceil (log10 (2.0)). Subtract 1 for the sign bit; 195 * add 1 for integer division truncation; add 1 more for a minus sign. 196 */ 197 #define INT_STRLEN_BOUND(t) ((sizeof(t) * CHAR_BIT - 1) * 302 / 1000 + 1 + 1) 198 199 /* 200 * Special types and constants. 201 */ 202 typedef unsigned long LWCHAR; 203 typedef off_t POSITION; 204 typedef off_t LINENUM; 205 #define MIN_LINENUM_WIDTH 7 /* Min printing width of a line number */ 206 #define MAX_UTF_CHAR_LEN 6 /* Max bytes in one UTF-8 char */ 207 208 #define NULL_POSITION ((POSITION)(-1)) 209 210 /* 211 * Flags for open() 212 */ 213 #if MSDOS_COMPILER || OS2 214 #define OPEN_READ (O_RDONLY|O_BINARY) 215 #else 216 #ifdef _OSK 217 #define OPEN_READ (S_IREAD) 218 #else 219 #ifdef O_RDONLY 220 #define OPEN_READ (O_RDONLY) 221 #else 222 #define OPEN_READ (0) 223 #endif 224 #endif 225 #endif 226 227 #if defined(O_WRONLY) && defined(O_APPEND) 228 #define OPEN_APPEND (O_APPEND|O_WRONLY) 229 #else 230 #ifdef _OSK 231 #define OPEN_APPEND (S_IWRITE) 232 #else 233 #define OPEN_APPEND (1) 234 #endif 235 #endif 236 237 /* 238 * Set a file descriptor to binary mode. 239 */ 240 #if MSDOS_COMPILER==MSOFTC 241 #define SET_BINARY(f) _setmode(f, _O_BINARY); 242 #else 243 #if MSDOS_COMPILER || OS2 244 #define SET_BINARY(f) setmode(f, O_BINARY) 245 #else 246 #define SET_BINARY(f) 247 #endif 248 #endif 249 250 /* 251 * Does the shell treat "?" as a metacharacter? 252 */ 253 #if MSDOS_COMPILER || OS2 || _OSK 254 #define SHELL_META_QUEST 0 255 #else 256 #define SHELL_META_QUEST 1 257 #endif 258 259 #define SPACES_IN_FILENAMES 1 260 261 /* 262 * An IFILE represents an input file. 263 */ 264 #define IFILE VOID_POINTER 265 #define NULL_IFILE ((IFILE)NULL) 266 267 /* 268 * The structure used to represent a "screen position". 269 * This consists of a file position, and a screen line number. 270 * The meaning is that the line starting at the given file 271 * position is displayed on the ln-th line of the screen. 272 * (Screen lines before ln are empty.) 273 */ 274 struct scrpos 275 { 276 POSITION pos; 277 int ln; 278 }; 279 280 typedef union parg 281 { 282 char *p_string; 283 int p_int; 284 LINENUM p_linenum; 285 } PARG; 286 287 #define NULL_PARG ((PARG *)NULL) 288 289 struct textlist 290 { 291 char *string; 292 char *endstring; 293 }; 294 295 #define EOI (-1) 296 297 #define READ_INTR (-2) 298 299 /* How quiet should we be? */ 300 #define NOT_QUIET 0 /* Ring bell at eof and for errors */ 301 #define LITTLE_QUIET 1 /* Ring bell only for errors */ 302 #define VERY_QUIET 2 /* Never ring bell */ 303 304 /* How should we prompt? */ 305 #define PR_SHORT 0 /* Prompt with colon */ 306 #define PR_MEDIUM 1 /* Prompt with message */ 307 #define PR_LONG 2 /* Prompt with longer message */ 308 309 /* How should we handle backspaces? */ 310 #define BS_SPECIAL 0 /* Do special things for underlining and bold */ 311 #define BS_NORMAL 1 /* \b treated as normal char; actually output */ 312 #define BS_CONTROL 2 /* \b treated as control char; prints as ^H */ 313 314 /* How should we search? */ 315 #define SRCH_FORW (1 << 0) /* Search forward from current position */ 316 #define SRCH_BACK (1 << 1) /* Search backward from current position */ 317 #define SRCH_NO_MOVE (1 << 2) /* Highlight, but don't move */ 318 #define SRCH_FIND_ALL (1 << 4) /* Find and highlight all matches */ 319 #define SRCH_NO_MATCH (1 << 8) /* Search for non-matching lines */ 320 #define SRCH_PAST_EOF (1 << 9) /* Search past end-of-file, into next file */ 321 #define SRCH_FIRST_FILE (1 << 10) /* Search starting at the first file */ 322 #define SRCH_NO_REGEX (1 << 12) /* Don't use regular expressions */ 323 324 #define SRCH_REVERSE(t) (((t) & SRCH_FORW) ? \ 325 (((t) & ~SRCH_FORW) | SRCH_BACK) : \ 326 (((t) & ~SRCH_BACK) | SRCH_FORW)) 327 328 /* */ 329 #define NO_MCA 0 330 #define MCA_DONE 1 331 #define MCA_MORE 2 332 333 #define CC_OK 0 /* Char was accepted & processed */ 334 #define CC_QUIT 1 /* Char was a request to abort current cmd */ 335 #define CC_ERROR 2 /* Char could not be accepted due to error */ 336 #define CC_PASS 3 /* Char was rejected (internal) */ 337 338 #define CF_QUIT_ON_ERASE 0001 /* Abort cmd if its entirely erased */ 339 340 /* Special char bit-flags used to tell put_line() to do something special */ 341 #define AT_NORMAL (0) 342 #define AT_UNDERLINE (1 << 0) 343 #define AT_BOLD (1 << 1) 344 #define AT_BLINK (1 << 2) 345 #define AT_STANDOUT (1 << 3) 346 #define AT_ANSI (1 << 4) /* Content-supplied "ANSI" escape sequence */ 347 #define AT_BINARY (1 << 5) /* LESS*BINFMT representation */ 348 #define AT_HILITE (1 << 6) /* Internal highlights (e.g., for search) */ 349 350 #if '0' == 240 351 #define IS_EBCDIC_HOST 1 352 #endif 353 354 #if IS_EBCDIC_HOST 355 /* 356 * Long definition for EBCDIC. 357 * Since the argument is usually a constant, this macro normally compiles 358 * into a constant. 359 */ 360 #define CONTROL(c) ( \ 361 (c)=='[' ? '\047' : \ 362 (c)=='a' ? '\001' : \ 363 (c)=='b' ? '\002' : \ 364 (c)=='c' ? '\003' : \ 365 (c)=='d' ? '\067' : \ 366 (c)=='e' ? '\055' : \ 367 (c)=='f' ? '\056' : \ 368 (c)=='g' ? '\057' : \ 369 (c)=='h' ? '\026' : \ 370 (c)=='i' ? '\005' : \ 371 (c)=='j' ? '\025' : \ 372 (c)=='k' ? '\013' : \ 373 (c)=='l' ? '\014' : \ 374 (c)=='m' ? '\015' : \ 375 (c)=='n' ? '\016' : \ 376 (c)=='o' ? '\017' : \ 377 (c)=='p' ? '\020' : \ 378 (c)=='q' ? '\021' : \ 379 (c)=='r' ? '\022' : \ 380 (c)=='s' ? '\023' : \ 381 (c)=='t' ? '\074' : \ 382 (c)=='u' ? '\075' : \ 383 (c)=='v' ? '\062' : \ 384 (c)=='w' ? '\046' : \ 385 (c)=='x' ? '\030' : \ 386 (c)=='y' ? '\031' : \ 387 (c)=='z' ? '\077' : \ 388 (c)=='A' ? '\001' : \ 389 (c)=='B' ? '\002' : \ 390 (c)=='C' ? '\003' : \ 391 (c)=='D' ? '\067' : \ 392 (c)=='E' ? '\055' : \ 393 (c)=='F' ? '\056' : \ 394 (c)=='G' ? '\057' : \ 395 (c)=='H' ? '\026' : \ 396 (c)=='I' ? '\005' : \ 397 (c)=='J' ? '\025' : \ 398 (c)=='K' ? '\013' : \ 399 (c)=='L' ? '\014' : \ 400 (c)=='M' ? '\015' : \ 401 (c)=='N' ? '\016' : \ 402 (c)=='O' ? '\017' : \ 403 (c)=='P' ? '\020' : \ 404 (c)=='Q' ? '\021' : \ 405 (c)=='R' ? '\022' : \ 406 (c)=='S' ? '\023' : \ 407 (c)=='T' ? '\074' : \ 408 (c)=='U' ? '\075' : \ 409 (c)=='V' ? '\062' : \ 410 (c)=='W' ? '\046' : \ 411 (c)=='X' ? '\030' : \ 412 (c)=='Y' ? '\031' : \ 413 (c)=='Z' ? '\077' : \ 414 (c)=='|' ? '\031' : \ 415 (c)=='\\' ? '\034' : \ 416 (c)=='^' ? '\036' : \ 417 (c)&077) 418 #else 419 #define CONTROL(c) ((c)&037) 420 #endif /* IS_EBCDIC_HOST */ 421 422 #define ESC CONTROL('[') 423 424 #if _OSK_MWC32 425 #define LSIGNAL(sig,func) os9_signal(sig,func) 426 #else 427 #define LSIGNAL(sig,func) signal(sig,func) 428 #endif 429 430 #if HAVE_SIGPROCMASK 431 #if HAVE_SIGSET_T 432 #else 433 #undef HAVE_SIGPROCMASK 434 #endif 435 #endif 436 #if HAVE_SIGPROCMASK 437 #if HAVE_SIGEMPTYSET 438 #else 439 #undef sigemptyset 440 #define sigemptyset(mp) *(mp) = 0 441 #endif 442 #endif 443 444 #define S_INTERRUPT 01 445 #define S_STOP 02 446 #define S_WINCH 04 447 #define ABORT_SIGS() (sigs & (S_INTERRUPT|S_STOP)) 448 449 #define QUIT_OK 0 450 #define QUIT_ERROR 1 451 #define QUIT_SAVED_STATUS (-1) 452 453 /* filestate flags */ 454 #define CH_CANSEEK 001 455 #define CH_KEEPOPEN 002 456 #define CH_POPENED 004 457 #define CH_HELPFILE 010 458 459 #define ch_zero() ((POSITION)0) 460 461 #define FAKE_HELPFILE "@/\\less/\\help/\\file/\\@" 462 463 #include "funcs.h" 464 465 /* Functions not included in funcs.h */ 466 void postoa(); 467 void linenumtoa(); 468 void inttoa(); 469