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