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