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