160814Sps/* $FreeBSD$ */ 260786Sps/* 3240121Sdelphij * Copyright (C) 1984-2012 Mark Nudelman 460786Sps * 560786Sps * You may distribute under the terms of either the GNU General Public 660786Sps * License or the Less License, as specified in the README file. 760786Sps * 8240121Sdelphij * For more information, see the README file. 960786Sps */ 1060786Sps 11170259Sdelphij#define NEWBOT 1 1260786Sps 1360786Sps/* 1460786Sps * Standard include file for "less". 1560786Sps */ 1660786Sps 1760786Sps/* 1860786Sps * Defines for MSDOS_COMPILER. 1960786Sps */ 2060786Sps#define MSOFTC 1 /* Microsoft C */ 2160786Sps#define BORLANDC 2 /* Borland C */ 2260786Sps#define WIN32C 3 /* Windows (Borland C or Microsoft C) */ 2360786Sps#define DJGPPC 4 /* DJGPP C */ 2460786Sps 2560786Sps/* 2660786Sps * Include the file of compile-time options. 2760786Sps * The <> make cc search for it in -I., not srcdir. 2860786Sps */ 2960786Sps#include <defines.h> 3060786Sps 3160786Sps#ifdef _SEQUENT_ 3260786Sps/* 3360786Sps * Kludge for Sequent Dynix systems that have sigsetmask, but 3460786Sps * it's not compatible with the way less calls it. 3560786Sps * {{ Do other systems need this? }} 3660786Sps */ 3760786Sps#undef HAVE_SIGSETMASK 3860786Sps#endif 3960786Sps 4060786Sps/* 4160786Sps * Language details. 4260786Sps */ 4360786Sps#if HAVE_VOID 4460786Sps#define VOID_POINTER void * 4560786Sps#else 4660786Sps#define VOID_POINTER char * 4760786Sps#define void int 4860786Sps#endif 4960786Sps#if HAVE_CONST 5060786Sps#define constant const 5160786Sps#else 5260786Sps#define constant 5360786Sps#endif 5460786Sps 5560786Sps#define public /* PUBLIC FUNCTION */ 5660786Sps 5760786Sps/* Library function declarations */ 5860786Sps 5960786Sps#if HAVE_SYS_TYPES_H 6060786Sps#include <sys/types.h> 6160786Sps#endif 6260786Sps#if HAVE_STDIO_H 6360786Sps#include <stdio.h> 6460786Sps#endif 6560786Sps#if HAVE_FCNTL_H 6660786Sps#include <fcntl.h> 6760786Sps#endif 6860786Sps#if HAVE_UNISTD_H 6960786Sps#include <unistd.h> 7060786Sps#endif 7160786Sps#if HAVE_CTYPE_H 7260786Sps#include <ctype.h> 7360786Sps#endif 74173685Sdelphij#if HAVE_WCTYPE_H 75173685Sdelphij#include <wctype.h> 76173685Sdelphij#endif 7789022Sps#if HAVE_LIMITS_H 7889022Sps#include <limits.h> 7989022Sps#endif 8060786Sps#if HAVE_STDLIB_H 8160786Sps#include <stdlib.h> 8260786Sps#endif 8360786Sps#if HAVE_STRING_H 8460786Sps#include <string.h> 8560786Sps#endif 86161478Sdelphij 87161478Sdelphij/* OS-specific includes */ 8860786Sps#ifdef _OSK 8960786Sps#include <modes.h> 9060786Sps#include <strings.h> 9160786Sps#endif 92161478Sdelphij 93161478Sdelphij#ifdef __TANDEM 94161478Sdelphij#include <floss.h> 95161478Sdelphij#endif 96161478Sdelphij 9789022Sps#if MSDOS_COMPILER==WIN32C || OS2 9860786Sps#include <io.h> 9960786Sps#endif 100161478Sdelphij 10160786Sps#if MSDOS_COMPILER==DJGPPC 10260786Sps#include <io.h> 10360786Sps#include <sys/exceptn.h> 10460786Sps#include <conio.h> 10560786Sps#include <pc.h> 10660786Sps#endif 10760786Sps 10860786Sps#if !HAVE_STDLIB_H 10960786Spschar *getenv(); 11060786Spsoff_t lseek(); 11160786SpsVOID_POINTER calloc(); 11260786Spsvoid free(); 11360786Sps#endif 11460786Sps 11560786Sps/* 11660786Sps * Simple lowercase test which can be used during option processing 11760786Sps * (before options are parsed which might tell us what charset to use). 11860786Sps */ 119161478Sdelphij#define ASCII_IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z') 120161478Sdelphij#define ASCII_IS_LOWER(c) ((c) >= 'a' && (c) <= 'z') 121161478Sdelphij#define ASCII_TO_UPPER(c) ((c) - 'a' + 'A') 122161478Sdelphij#define ASCII_TO_LOWER(c) ((c) - 'A' + 'a') 12360786Sps 124161478Sdelphij#undef IS_UPPER 125161478Sdelphij#undef IS_LOWER 126161478Sdelphij#undef TO_UPPER 127161478Sdelphij#undef TO_LOWER 128161478Sdelphij#undef IS_SPACE 129161478Sdelphij#undef IS_DIGIT 130161478Sdelphij 131173685Sdelphij#if HAVE_WCTYPE 132173685Sdelphij#define IS_UPPER(c) iswupper(c) 133173685Sdelphij#define IS_LOWER(c) iswlower(c) 134173685Sdelphij#define TO_UPPER(c) towupper(c) 135173685Sdelphij#define TO_LOWER(c) towlower(c) 136161478Sdelphij#else 137173685Sdelphij#if HAVE_UPPER_LOWER 138161478Sdelphij#define IS_UPPER(c) isupper((unsigned char) (c)) 139161478Sdelphij#define IS_LOWER(c) islower((unsigned char) (c)) 140161478Sdelphij#define TO_UPPER(c) toupper((unsigned char) (c)) 141161478Sdelphij#define TO_LOWER(c) tolower((unsigned char) (c)) 142173685Sdelphij#else 143173685Sdelphij#define IS_UPPER(c) ASCII_IS_UPPER(c) 144173685Sdelphij#define IS_LOWER(c) ASCII_IS_LOWER(c) 145173685Sdelphij#define TO_UPPER(c) ASCII_TO_UPPER(c) 146173685Sdelphij#define TO_LOWER(c) ASCII_TO_LOWER(c) 14760786Sps#endif 148173685Sdelphij#endif 14960786Sps 150161478Sdelphij#ifdef isspace 151161478Sdelphij#define IS_SPACE(c) isspace((unsigned char)(c)) 152161478Sdelphij#else 153161478Sdelphij#define IS_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f') 154161478Sdelphij#endif 155161478Sdelphij 156161478Sdelphij#ifdef isdigit 157161478Sdelphij#define IS_DIGIT(c) isdigit((unsigned char)(c)) 158161478Sdelphij#else 159161478Sdelphij#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9') 160161478Sdelphij#endif 161161478Sdelphij 162191930Sdelphij#define IS_CSI_START(c) (((LWCHAR)(c)) == ESC || (((LWCHAR)(c)) == CSI)) 163172471Sdelphij 16460786Sps#ifndef NULL 16560786Sps#define NULL 0 16660786Sps#endif 16760786Sps 16860786Sps#ifndef TRUE 16960786Sps#define TRUE 1 17060786Sps#endif 17160786Sps#ifndef FALSE 17260786Sps#define FALSE 0 17360786Sps#endif 17460786Sps 17560786Sps#define OPT_OFF 0 17660786Sps#define OPT_ON 1 17760786Sps#define OPT_ONPLUS 2 17860786Sps 17960786Sps#if !HAVE_MEMCPY 18060786Sps#ifndef memcpy 18160786Sps#define memcpy(to,from,len) bcopy((from),(to),(len)) 18260786Sps#endif 18360786Sps#endif 18460786Sps 185161478Sdelphij#if HAVE_SNPRINTF 186161478Sdelphij#define SNPRINTF1(str, size, fmt, v1) snprintf((str), (size), (fmt), (v1)) 187161478Sdelphij#define SNPRINTF2(str, size, fmt, v1, v2) snprintf((str), (size), (fmt), (v1), (v2)) 188161478Sdelphij#define SNPRINTF3(str, size, fmt, v1, v2, v3) snprintf((str), (size), (fmt), (v1), (v2), (v3)) 189161478Sdelphij#define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) snprintf((str), (size), (fmt), (v1), (v2), (v3), (v4)) 190161478Sdelphij#else 191161478Sdelphij/* Use unsafe sprintf if we don't have snprintf. */ 192161478Sdelphij#define SNPRINTF1(str, size, fmt, v1) sprintf((str), (fmt), (v1)) 193161478Sdelphij#define SNPRINTF2(str, size, fmt, v1, v2) sprintf((str), (fmt), (v1), (v2)) 194161478Sdelphij#define SNPRINTF3(str, size, fmt, v1, v2, v3) sprintf((str), (fmt), (v1), (v2), (v3)) 195161478Sdelphij#define SNPRINTF4(str, size, fmt, v1, v2, v3, v4) sprintf((str), (fmt), (v1), (v2), (v3), (v4)) 196161478Sdelphij#endif 197161478Sdelphij 19860786Sps#define BAD_LSEEK ((off_t)-1) 19960786Sps 200173685Sdelphij#ifndef SEEK_SET 201173685Sdelphij#define SEEK_SET 0 202173685Sdelphij#endif 203173685Sdelphij#ifndef SEEK_END 204173685Sdelphij#define SEEK_END 2 205173685Sdelphij#endif 206173685Sdelphij 20789022Sps#ifndef CHAR_BIT 20889022Sps#define CHAR_BIT 8 20989022Sps#endif 21089022Sps 21160786Sps/* 21289022Sps * Upper bound on the string length of an integer converted to string. 21389022Sps * 302 / 1000 is ceil (log10 (2.0)). Subtract 1 for the sign bit; 21489022Sps * add 1 for integer division truncation; add 1 more for a minus sign. 21589022Sps */ 21689022Sps#define INT_STRLEN_BOUND(t) ((sizeof(t) * CHAR_BIT - 1) * 302 / 1000 + 1 + 1) 21789022Sps 21889022Sps/* 21960786Sps * Special types and constants. 22060786Sps */ 221161478Sdelphijtypedef unsigned long LWCHAR; 22260814Spstypedef off_t POSITION; 223128348Stjrtypedef off_t LINENUM; 224128348Stjr#define MIN_LINENUM_WIDTH 7 /* Min printing width of a line number */ 225161478Sdelphij#define MAX_UTF_CHAR_LEN 6 /* Max bytes in one UTF-8 char */ 22660786Sps 22760786Sps#define NULL_POSITION ((POSITION)(-1)) 22860786Sps 22960786Sps/* 23060786Sps * Flags for open() 23160786Sps */ 23260786Sps#if MSDOS_COMPILER || OS2 23360786Sps#define OPEN_READ (O_RDONLY|O_BINARY) 23460786Sps#else 23560786Sps#ifdef _OSK 23660786Sps#define OPEN_READ (S_IREAD) 23760786Sps#else 23860786Sps#ifdef O_RDONLY 23960786Sps#define OPEN_READ (O_RDONLY) 24060786Sps#else 24160786Sps#define OPEN_READ (0) 24260786Sps#endif 24360786Sps#endif 24460786Sps#endif 24560786Sps 24660786Sps#if defined(O_WRONLY) && defined(O_APPEND) 24760786Sps#define OPEN_APPEND (O_APPEND|O_WRONLY) 24860786Sps#else 24960786Sps#ifdef _OSK 25060786Sps#define OPEN_APPEND (S_IWRITE) 25160786Sps#else 25260786Sps#define OPEN_APPEND (1) 25360786Sps#endif 25460786Sps#endif 25560786Sps 25660786Sps/* 25760786Sps * Set a file descriptor to binary mode. 25860786Sps */ 25960786Sps#if MSDOS_COMPILER==MSOFTC 26060786Sps#define SET_BINARY(f) _setmode(f, _O_BINARY); 26160786Sps#else 26289022Sps#if MSDOS_COMPILER || OS2 26360786Sps#define SET_BINARY(f) setmode(f, O_BINARY) 26460786Sps#else 26560786Sps#define SET_BINARY(f) 26660786Sps#endif 26760786Sps#endif 26860786Sps 26960786Sps/* 27060786Sps * Does the shell treat "?" as a metacharacter? 27160786Sps */ 27260786Sps#if MSDOS_COMPILER || OS2 || _OSK 27360786Sps#define SHELL_META_QUEST 0 27460786Sps#else 27560786Sps#define SHELL_META_QUEST 1 27660786Sps#endif 27760786Sps 27860786Sps#define SPACES_IN_FILENAMES 1 27960786Sps 28060786Sps/* 28160786Sps * An IFILE represents an input file. 28260786Sps */ 28360786Sps#define IFILE VOID_POINTER 28460786Sps#define NULL_IFILE ((IFILE)NULL) 28560786Sps 28660786Sps/* 28760786Sps * The structure used to represent a "screen position". 28860786Sps * This consists of a file position, and a screen line number. 28960786Sps * The meaning is that the line starting at the given file 29060786Sps * position is displayed on the ln-th line of the screen. 29160786Sps * (Screen lines before ln are empty.) 29260786Sps */ 29360786Spsstruct scrpos 29460786Sps{ 29560786Sps POSITION pos; 29660786Sps int ln; 29760786Sps}; 29860786Sps 29960786Spstypedef union parg 30060786Sps{ 30160786Sps char *p_string; 30260786Sps int p_int; 303128348Stjr LINENUM p_linenum; 30460786Sps} PARG; 30560786Sps 30660786Sps#define NULL_PARG ((PARG *)NULL) 30760786Sps 30860786Spsstruct textlist 30960786Sps{ 31060786Sps char *string; 31160786Sps char *endstring; 31260786Sps}; 31360786Sps 31460786Sps#define EOI (-1) 31560786Sps 31660786Sps#define READ_INTR (-2) 31760786Sps 318170259Sdelphij/* A fraction is represented by an int n; the fraction is n/NUM_FRAC_DENOM */ 319170259Sdelphij#define NUM_FRAC_DENOM 1000000 320170259Sdelphij#define NUM_LOG_FRAC_DENOM 6 321170259Sdelphij 32260786Sps/* How quiet should we be? */ 32360786Sps#define NOT_QUIET 0 /* Ring bell at eof and for errors */ 32460786Sps#define LITTLE_QUIET 1 /* Ring bell only for errors */ 32560786Sps#define VERY_QUIET 2 /* Never ring bell */ 32660786Sps 32760786Sps/* How should we prompt? */ 32860786Sps#define PR_SHORT 0 /* Prompt with colon */ 32960786Sps#define PR_MEDIUM 1 /* Prompt with message */ 33060786Sps#define PR_LONG 2 /* Prompt with longer message */ 33160786Sps 33260786Sps/* How should we handle backspaces? */ 33360786Sps#define BS_SPECIAL 0 /* Do special things for underlining and bold */ 33460786Sps#define BS_NORMAL 1 /* \b treated as normal char; actually output */ 33560786Sps#define BS_CONTROL 2 /* \b treated as control char; prints as ^H */ 33660786Sps 33760786Sps/* How should we search? */ 338191930Sdelphij#define SRCH_FORW (1 << 0) /* Search forward from current position */ 339191930Sdelphij#define SRCH_BACK (1 << 1) /* Search backward from current position */ 340191930Sdelphij#define SRCH_NO_MOVE (1 << 2) /* Highlight, but don't move */ 341191930Sdelphij#define SRCH_FIND_ALL (1 << 4) /* Find and highlight all matches */ 342191930Sdelphij#define SRCH_NO_MATCH (1 << 8) /* Search for non-matching lines */ 343191930Sdelphij#define SRCH_PAST_EOF (1 << 9) /* Search past end-of-file, into next file */ 344191930Sdelphij#define SRCH_FIRST_FILE (1 << 10) /* Search starting at the first file */ 345191930Sdelphij#define SRCH_NO_REGEX (1 << 12) /* Don't use regular expressions */ 346191930Sdelphij#define SRCH_FILTER (1 << 13) /* Search is for '&' (filter) command */ 347221715Sdelphij#define SRCH_AFTER_TARGET (1 << 14) /* Start search after the target line */ 34860786Sps 34960786Sps#define SRCH_REVERSE(t) (((t) & SRCH_FORW) ? \ 35060786Sps (((t) & ~SRCH_FORW) | SRCH_BACK) : \ 35160786Sps (((t) & ~SRCH_BACK) | SRCH_FORW)) 35260786Sps 35360786Sps/* */ 35460786Sps#define NO_MCA 0 35560786Sps#define MCA_DONE 1 35660786Sps#define MCA_MORE 2 35760786Sps 35860786Sps#define CC_OK 0 /* Char was accepted & processed */ 35960786Sps#define CC_QUIT 1 /* Char was a request to abort current cmd */ 36060786Sps#define CC_ERROR 2 /* Char could not be accepted due to error */ 36160786Sps#define CC_PASS 3 /* Char was rejected (internal) */ 36260786Sps 36360786Sps#define CF_QUIT_ON_ERASE 0001 /* Abort cmd if its entirely erased */ 36460786Sps 365161478Sdelphij/* Special char bit-flags used to tell put_line() to do something special */ 36660786Sps#define AT_NORMAL (0) 367161478Sdelphij#define AT_UNDERLINE (1 << 0) 368161478Sdelphij#define AT_BOLD (1 << 1) 369161478Sdelphij#define AT_BLINK (1 << 2) 370161478Sdelphij#define AT_STANDOUT (1 << 3) 371161478Sdelphij#define AT_ANSI (1 << 4) /* Content-supplied "ANSI" escape sequence */ 372161478Sdelphij#define AT_BINARY (1 << 5) /* LESS*BINFMT representation */ 373161478Sdelphij#define AT_HILITE (1 << 6) /* Internal highlights (e.g., for search) */ 37460786Sps 37589022Sps#if '0' == 240 37689022Sps#define IS_EBCDIC_HOST 1 37789022Sps#endif 37889022Sps 37960786Sps#if IS_EBCDIC_HOST 38060786Sps/* 38160786Sps * Long definition for EBCDIC. 38260786Sps * Since the argument is usually a constant, this macro normally compiles 38360786Sps * into a constant. 38460786Sps */ 38560786Sps#define CONTROL(c) ( \ 38660786Sps (c)=='[' ? '\047' : \ 38760786Sps (c)=='a' ? '\001' : \ 38860786Sps (c)=='b' ? '\002' : \ 38960786Sps (c)=='c' ? '\003' : \ 39060786Sps (c)=='d' ? '\067' : \ 39160786Sps (c)=='e' ? '\055' : \ 39260786Sps (c)=='f' ? '\056' : \ 39360786Sps (c)=='g' ? '\057' : \ 39460786Sps (c)=='h' ? '\026' : \ 39560786Sps (c)=='i' ? '\005' : \ 39660786Sps (c)=='j' ? '\025' : \ 39760786Sps (c)=='k' ? '\013' : \ 39860786Sps (c)=='l' ? '\014' : \ 39960786Sps (c)=='m' ? '\015' : \ 40060786Sps (c)=='n' ? '\016' : \ 40160786Sps (c)=='o' ? '\017' : \ 40260786Sps (c)=='p' ? '\020' : \ 40360786Sps (c)=='q' ? '\021' : \ 40460786Sps (c)=='r' ? '\022' : \ 40560786Sps (c)=='s' ? '\023' : \ 40660786Sps (c)=='t' ? '\074' : \ 40760786Sps (c)=='u' ? '\075' : \ 40860786Sps (c)=='v' ? '\062' : \ 40960786Sps (c)=='w' ? '\046' : \ 41060786Sps (c)=='x' ? '\030' : \ 41160786Sps (c)=='y' ? '\031' : \ 41260786Sps (c)=='z' ? '\077' : \ 41360786Sps (c)=='A' ? '\001' : \ 41460786Sps (c)=='B' ? '\002' : \ 41560786Sps (c)=='C' ? '\003' : \ 41660786Sps (c)=='D' ? '\067' : \ 41760786Sps (c)=='E' ? '\055' : \ 41860786Sps (c)=='F' ? '\056' : \ 41960786Sps (c)=='G' ? '\057' : \ 42060786Sps (c)=='H' ? '\026' : \ 42160786Sps (c)=='I' ? '\005' : \ 42260786Sps (c)=='J' ? '\025' : \ 42360786Sps (c)=='K' ? '\013' : \ 42460786Sps (c)=='L' ? '\014' : \ 42560786Sps (c)=='M' ? '\015' : \ 42660786Sps (c)=='N' ? '\016' : \ 42760786Sps (c)=='O' ? '\017' : \ 42860786Sps (c)=='P' ? '\020' : \ 42960786Sps (c)=='Q' ? '\021' : \ 43060786Sps (c)=='R' ? '\022' : \ 43160786Sps (c)=='S' ? '\023' : \ 43260786Sps (c)=='T' ? '\074' : \ 43360786Sps (c)=='U' ? '\075' : \ 43460786Sps (c)=='V' ? '\062' : \ 43560786Sps (c)=='W' ? '\046' : \ 43660786Sps (c)=='X' ? '\030' : \ 43760786Sps (c)=='Y' ? '\031' : \ 43860786Sps (c)=='Z' ? '\077' : \ 43960786Sps (c)=='|' ? '\031' : \ 44060786Sps (c)=='\\' ? '\034' : \ 44160786Sps (c)=='^' ? '\036' : \ 44260786Sps (c)&077) 44360786Sps#else 44460786Sps#define CONTROL(c) ((c)&037) 44560786Sps#endif /* IS_EBCDIC_HOST */ 44660786Sps 44760786Sps#define ESC CONTROL('[') 448172471Sdelphij#define CSI ((unsigned char)'\233') 44960786Sps 45060786Sps#if _OSK_MWC32 45160786Sps#define LSIGNAL(sig,func) os9_signal(sig,func) 45260786Sps#else 45360786Sps#define LSIGNAL(sig,func) signal(sig,func) 45460786Sps#endif 45560786Sps 45663131Sps#if HAVE_SIGPROCMASK 45763131Sps#if HAVE_SIGSET_T 45863131Sps#else 45963131Sps#undef HAVE_SIGPROCMASK 46063131Sps#endif 46163131Sps#endif 46263131Sps#if HAVE_SIGPROCMASK 46363131Sps#if HAVE_SIGEMPTYSET 46463131Sps#else 46563131Sps#undef sigemptyset 46663131Sps#define sigemptyset(mp) *(mp) = 0 46763131Sps#endif 46863131Sps#endif 46963131Sps 47060786Sps#define S_INTERRUPT 01 47160786Sps#define S_STOP 02 47260786Sps#define S_WINCH 04 47360786Sps#define ABORT_SIGS() (sigs & (S_INTERRUPT|S_STOP)) 47460786Sps 47560786Sps#define QUIT_OK 0 47660786Sps#define QUIT_ERROR 1 477221715Sdelphij#define QUIT_INTERRUPT 2 47860786Sps#define QUIT_SAVED_STATUS (-1) 47960786Sps 480173685Sdelphij#define FOLLOW_DESC 0 481173685Sdelphij#define FOLLOW_NAME 1 482173685Sdelphij 48360786Sps/* filestate flags */ 48460786Sps#define CH_CANSEEK 001 48560786Sps#define CH_KEEPOPEN 002 48660786Sps#define CH_POPENED 004 48760786Sps#define CH_HELPFILE 010 488240121Sdelphij#define CH_NODATA 020 /* Special case for zero length files */ 48960786Sps 490240121Sdelphij 49160786Sps#define ch_zero() ((POSITION)0) 49260786Sps 49360786Sps#define FAKE_HELPFILE "@/\\less/\\help/\\file/\\@" 494240121Sdelphij#define FAKE_EMPTYFILE "@/\\less/\\empty/\\file/\\@" 49560786Sps 496195941Sdelphij/* Flags for cvt_text */ 497195941Sdelphij#define CVT_TO_LC 01 /* Convert upper-case to lower-case */ 498195941Sdelphij#define CVT_BS 02 /* Do backspace processing */ 499195941Sdelphij#define CVT_CRLF 04 /* Remove CR after LF */ 500195941Sdelphij#define CVT_ANSI 010 /* Remove ANSI escape sequences */ 501195941Sdelphij 50260786Sps#include "funcs.h" 50360786Sps 504128348Stjr/* Functions not included in funcs.h */ 505128348Stjrvoid postoa(); 506128348Stjrvoid linenumtoa(); 507128348Stjrvoid inttoa(); 508