17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*23a1cceaSRoger A. Faulkner * Common Development and Distribution License (the "License"). 6*23a1cceaSRoger A. Faulkner * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate /* 23*23a1cceaSRoger A. Faulkner * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 26*23a1cceaSRoger A. Faulkner /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27*23a1cceaSRoger A. Faulkner /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Editor 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <crypt.h> 347c478bd9Sstevel@tonic-gate #include <libgen.h> 357c478bd9Sstevel@tonic-gate #include <wait.h> 367c478bd9Sstevel@tonic-gate #include <string.h> 377c478bd9Sstevel@tonic-gate #include <sys/types.h> 387c478bd9Sstevel@tonic-gate #include <locale.h> 397c478bd9Sstevel@tonic-gate #include <regexpr.h> 407c478bd9Sstevel@tonic-gate #include <regex.h> 417c478bd9Sstevel@tonic-gate #include <errno.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate static const char *msgtab[] = 447c478bd9Sstevel@tonic-gate { 457c478bd9Sstevel@tonic-gate "write or open on pipe failed", /* 0 */ 467c478bd9Sstevel@tonic-gate "warning: expecting `w'", /* 1 */ 477c478bd9Sstevel@tonic-gate "mark not lower case ascii", /* 2 */ 487c478bd9Sstevel@tonic-gate "Cannot open input file", /* 3 */ 497c478bd9Sstevel@tonic-gate "PWB spec problem", /* 4 */ 507c478bd9Sstevel@tonic-gate "nothing to undo", /* 5 */ 517c478bd9Sstevel@tonic-gate "restricted shell", /* 6 */ 527c478bd9Sstevel@tonic-gate "cannot create output file", /* 7 */ 537c478bd9Sstevel@tonic-gate "filesystem out of space!", /* 8 */ 547c478bd9Sstevel@tonic-gate "cannot open file", /* 9 */ 557c478bd9Sstevel@tonic-gate "cannot link", /* 10 */ 567c478bd9Sstevel@tonic-gate "Range endpoint too large", /* 11 */ 577c478bd9Sstevel@tonic-gate "unknown command", /* 12 */ 587c478bd9Sstevel@tonic-gate "search string not found", /* 13 */ 597c478bd9Sstevel@tonic-gate "-", /* 14 */ 607c478bd9Sstevel@tonic-gate "line out of range", /* 15 */ 617c478bd9Sstevel@tonic-gate "bad number", /* 16 */ 627c478bd9Sstevel@tonic-gate "bad range", /* 17 */ 637c478bd9Sstevel@tonic-gate "Illegal address count", /* 18 */ 647c478bd9Sstevel@tonic-gate "incomplete global expression", /* 19 */ 657c478bd9Sstevel@tonic-gate "illegal suffix", /* 20 */ 667c478bd9Sstevel@tonic-gate "illegal or missing filename", /* 21 */ 677c478bd9Sstevel@tonic-gate "no space after command", /* 22 */ 687c478bd9Sstevel@tonic-gate "fork failed - try again", /* 23 */ 697c478bd9Sstevel@tonic-gate "maximum of 64 characters in file names", /* 24 */ 707c478bd9Sstevel@tonic-gate "`\\digit' out of range", /* 25 */ 717c478bd9Sstevel@tonic-gate "interrupt", /* 26 */ 727c478bd9Sstevel@tonic-gate "line too long", /* 27 */ 737c478bd9Sstevel@tonic-gate "illegal character in input file", /* 28 */ 747c478bd9Sstevel@tonic-gate "write error", /* 29 */ 757c478bd9Sstevel@tonic-gate "out of memory for append", /* 30 */ 767c478bd9Sstevel@tonic-gate "temp file too big", /* 31 */ 777c478bd9Sstevel@tonic-gate "I/O error on temp file", /* 32 */ 787c478bd9Sstevel@tonic-gate "multiple globals not allowed", /* 33 */ 797c478bd9Sstevel@tonic-gate "global too long", /* 34 */ 807c478bd9Sstevel@tonic-gate "no match", /* 35 */ 817c478bd9Sstevel@tonic-gate "illegal or missing delimiter", /* 36 */ 827c478bd9Sstevel@tonic-gate "-", /* 37 */ 837c478bd9Sstevel@tonic-gate "replacement string too long", /* 38 */ 847c478bd9Sstevel@tonic-gate "illegal move destination", /* 39 */ 857c478bd9Sstevel@tonic-gate "-", /* 40 */ 867c478bd9Sstevel@tonic-gate "no remembered search string", /* 41 */ 877c478bd9Sstevel@tonic-gate "'\\( \\)' imbalance", /* 42 */ 887c478bd9Sstevel@tonic-gate "Too many `\\(' s", /* 43 */ 897c478bd9Sstevel@tonic-gate "more than 2 numbers given", /* 44 */ 907c478bd9Sstevel@tonic-gate "'\\}' expected", /* 45 */ 917c478bd9Sstevel@tonic-gate "first number exceeds second", /* 46 */ 927c478bd9Sstevel@tonic-gate "incomplete substitute", /* 47 */ 937c478bd9Sstevel@tonic-gate "newline unexpected", /* 48 */ 947c478bd9Sstevel@tonic-gate "'[ ]' imbalance", /* 49 */ 957c478bd9Sstevel@tonic-gate "regular expression overflow", /* 50 */ 967c478bd9Sstevel@tonic-gate "regular expression error", /* 51 */ 977c478bd9Sstevel@tonic-gate "command expected", /* 52 */ 987c478bd9Sstevel@tonic-gate "a, i, or c not allowed in G", /* 53 */ 997c478bd9Sstevel@tonic-gate "end of line expected", /* 54 */ 1007c478bd9Sstevel@tonic-gate "no remembered replacement string", /* 55 */ 1017c478bd9Sstevel@tonic-gate "no remembered command", /* 56 */ 1027c478bd9Sstevel@tonic-gate "illegal redirection", /* 57 */ 1037c478bd9Sstevel@tonic-gate "possible concurrent update", /* 58 */ 1047c478bd9Sstevel@tonic-gate "-", /* 59 */ 1057c478bd9Sstevel@tonic-gate "the x command has become X (upper case)", /* 60 */ 1067c478bd9Sstevel@tonic-gate "Warning: 'w' may destroy input file " 1077c478bd9Sstevel@tonic-gate "(due to `illegal char' read earlier)", 1087c478bd9Sstevel@tonic-gate /* 61 */ 1097c478bd9Sstevel@tonic-gate "Caution: 'q' may lose data in buffer;" 1107c478bd9Sstevel@tonic-gate " 'w' may destroy input file", 1117c478bd9Sstevel@tonic-gate /* 62 */ 1127c478bd9Sstevel@tonic-gate "Encryption of string failed", /* 63 */ 1137c478bd9Sstevel@tonic-gate "Encryption facility not available", /* 64 */ 1147c478bd9Sstevel@tonic-gate "Cannot encrypt temporary file", /* 65 */ 1157c478bd9Sstevel@tonic-gate "Enter key:", /* 66 */ 1167c478bd9Sstevel@tonic-gate "Illegal byte sequence", /* 67 */ 1177c478bd9Sstevel@tonic-gate "File does not exist", /* 68 */ 1187c478bd9Sstevel@tonic-gate "tempnam failed", /* 69 */ 1197c478bd9Sstevel@tonic-gate "Cannot open temporary file", /* 70 */ 1207c478bd9Sstevel@tonic-gate 0 1217c478bd9Sstevel@tonic-gate }; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate #include <stdlib.h> 1247c478bd9Sstevel@tonic-gate #include <limits.h> 1257c478bd9Sstevel@tonic-gate #include <stdio.h> 1267c478bd9Sstevel@tonic-gate #include <signal.h> 1277c478bd9Sstevel@tonic-gate #include <sys/types.h> 1287c478bd9Sstevel@tonic-gate #include <sys/stat.h> 1297c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 1307c478bd9Sstevel@tonic-gate #include <unistd.h> 1317c478bd9Sstevel@tonic-gate #include <termio.h> 1327c478bd9Sstevel@tonic-gate #include <ctype.h> 1337c478bd9Sstevel@tonic-gate #include <setjmp.h> 1347c478bd9Sstevel@tonic-gate #include <fcntl.h> 1357c478bd9Sstevel@tonic-gate #include <wchar.h> /* I18N */ 1367c478bd9Sstevel@tonic-gate #include <wctype.h> /* I18N */ 1377c478bd9Sstevel@tonic-gate #include <widec.h> /* I18N */ 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate #define FTYPE(A) (A.st_mode) 1407c478bd9Sstevel@tonic-gate #define FMODE(A) (A.st_mode) 1417c478bd9Sstevel@tonic-gate #define IDENTICAL(A, B) (A.st_dev == B.st_dev && A.st_ino == B.st_ino) 1427c478bd9Sstevel@tonic-gate #define ISBLK(A) ((A.st_mode & S_IFMT) == S_IFBLK) 1437c478bd9Sstevel@tonic-gate #define ISCHR(A) ((A.st_mode & S_IFMT) == S_IFCHR) 1447c478bd9Sstevel@tonic-gate #define ISDIR(A) ((A.st_mode & S_IFMT) == S_IFDIR) 1457c478bd9Sstevel@tonic-gate #define ISFIFO(A) ((A.st_mode & S_IFMT) == S_IFIFO) 1467c478bd9Sstevel@tonic-gate #define ISREG(A) ((A.st_mode & S_IFMT) == S_IFREG) 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate #define PUTM() if (xcode >= 0) puts(gettext(msgtab[xcode])) 1497c478bd9Sstevel@tonic-gate #define UNGETC(c) (peekc = c) 1507c478bd9Sstevel@tonic-gate #define FNSIZE PATH_MAX 1517c478bd9Sstevel@tonic-gate #define LBSIZE LINE_MAX 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* size of substitution replacement pattern buffer */ 1547c478bd9Sstevel@tonic-gate #define RHSIZE (LINE_MAX*2) 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate #define KSIZE 8 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate #define READ 0 1597c478bd9Sstevel@tonic-gate #define WRITE 1 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate extern char *optarg; /* Value of argument */ 1627c478bd9Sstevel@tonic-gate extern int optind; /* Indicator of argument */ 1637c478bd9Sstevel@tonic-gate extern int __xpg4; /* defined in xpg4.c; 0 if not xpg4-compiled program */ 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate struct Fspec { 1667c478bd9Sstevel@tonic-gate char Ftabs[22]; 1677c478bd9Sstevel@tonic-gate char Fdel; 1687c478bd9Sstevel@tonic-gate unsigned char Flim; 1697c478bd9Sstevel@tonic-gate char Fmov; 1707c478bd9Sstevel@tonic-gate char Ffill; 1717c478bd9Sstevel@tonic-gate }; 1727c478bd9Sstevel@tonic-gate static struct Fspec fss; 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate static char *fsp; 1757c478bd9Sstevel@tonic-gate static int fsprtn; 1767c478bd9Sstevel@tonic-gate static char line[70]; 1777c478bd9Sstevel@tonic-gate static char *linp = line; 1787c478bd9Sstevel@tonic-gate static int sig; 1797c478bd9Sstevel@tonic-gate static int Xqt = 0; 1807c478bd9Sstevel@tonic-gate static int lastc; 1817c478bd9Sstevel@tonic-gate static char savedfile[FNSIZE]; 1827c478bd9Sstevel@tonic-gate static char file[FNSIZE]; 1837c478bd9Sstevel@tonic-gate static char funny[FNSIZE]; 1847c478bd9Sstevel@tonic-gate static int funlink = 0; 1857c478bd9Sstevel@tonic-gate static char linebuf[LBSIZE]; 1867c478bd9Sstevel@tonic-gate static char *tstring = linebuf; 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate static char *expbuf; 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate static char rhsbuf[RHSIZE]; 1917c478bd9Sstevel@tonic-gate struct lin { 1927c478bd9Sstevel@tonic-gate long cur; 1937c478bd9Sstevel@tonic-gate long sav; 1947c478bd9Sstevel@tonic-gate }; 1957c478bd9Sstevel@tonic-gate typedef struct lin *LINE; 1967c478bd9Sstevel@tonic-gate static LINE zero; 1977c478bd9Sstevel@tonic-gate static LINE dot; 1987c478bd9Sstevel@tonic-gate static LINE dol; 1997c478bd9Sstevel@tonic-gate static LINE endcore; 2007c478bd9Sstevel@tonic-gate static LINE fendcore; 2017c478bd9Sstevel@tonic-gate static LINE addr1; 2027c478bd9Sstevel@tonic-gate static LINE addr2; 2037c478bd9Sstevel@tonic-gate static LINE savdol, savdot; 2047c478bd9Sstevel@tonic-gate static int globflg; 2057c478bd9Sstevel@tonic-gate static int initflg; 2067c478bd9Sstevel@tonic-gate static char genbuf[LBSIZE]; 2077c478bd9Sstevel@tonic-gate static long count; 2087c478bd9Sstevel@tonic-gate static int numpass; /* Number of passes thru dosub(). */ 2097c478bd9Sstevel@tonic-gate static int gsubf; /* Occurrence value. LBSIZE-1=all. */ 2107c478bd9Sstevel@tonic-gate static int ocerr1; /* Allows lines NOT changed by dosub() to NOT be put */ 2117c478bd9Sstevel@tonic-gate /* out. Retains last line changed as current line. */ 2127c478bd9Sstevel@tonic-gate static int ocerr2; /* Flags if ANY line changed by substitute(). 0=nc. */ 2137c478bd9Sstevel@tonic-gate static char *nextip; 2147c478bd9Sstevel@tonic-gate static char *linebp; 2157c478bd9Sstevel@tonic-gate static int ninbuf; 2167c478bd9Sstevel@tonic-gate static int peekc; 2177c478bd9Sstevel@tonic-gate static int io; 2187c478bd9Sstevel@tonic-gate static void (*oldhup)(), (*oldintr)(); 2197c478bd9Sstevel@tonic-gate static void (*oldquit)(), (*oldpipe)(); 2207c478bd9Sstevel@tonic-gate static void quit(int); 2217c478bd9Sstevel@tonic-gate static int vflag = 1; 2227c478bd9Sstevel@tonic-gate static int xflag; 2237c478bd9Sstevel@tonic-gate static int xtflag; 2247c478bd9Sstevel@tonic-gate static int kflag; 2257c478bd9Sstevel@tonic-gate static int crflag; 2267c478bd9Sstevel@tonic-gate /* Flag for determining if file being read is encrypted */ 2277c478bd9Sstevel@tonic-gate static int hflag; 2287c478bd9Sstevel@tonic-gate static int xcode = -1; 2297c478bd9Sstevel@tonic-gate static char crbuf[LBSIZE]; 2307c478bd9Sstevel@tonic-gate static int perm[2]; 2317c478bd9Sstevel@tonic-gate static int tperm[2]; 2327c478bd9Sstevel@tonic-gate static int permflag; 2337c478bd9Sstevel@tonic-gate static int tpermflag; 2347c478bd9Sstevel@tonic-gate static int col; 2357c478bd9Sstevel@tonic-gate static char *globp; 2367c478bd9Sstevel@tonic-gate static int tfile = -1; 2377c478bd9Sstevel@tonic-gate static int tline; 2387c478bd9Sstevel@tonic-gate static char *tfname; 2397c478bd9Sstevel@tonic-gate extern char *locs; 2407c478bd9Sstevel@tonic-gate static char ibuff[LBSIZE]; 2417c478bd9Sstevel@tonic-gate static int iblock = -1; 2427c478bd9Sstevel@tonic-gate static char obuff[LBSIZE]; 2437c478bd9Sstevel@tonic-gate static int oblock = -1; 2447c478bd9Sstevel@tonic-gate static int ichanged; 2457c478bd9Sstevel@tonic-gate static int nleft; 2467c478bd9Sstevel@tonic-gate static long savnames[26], names[26]; 2477c478bd9Sstevel@tonic-gate static int anymarks; 2487c478bd9Sstevel@tonic-gate static long subnewa; 2497c478bd9Sstevel@tonic-gate static int fchange; 2507c478bd9Sstevel@tonic-gate static int nline; 2517c478bd9Sstevel@tonic-gate static int fflg, shflg; 2527c478bd9Sstevel@tonic-gate static char prompt[16] = "*"; 2537c478bd9Sstevel@tonic-gate static int rflg; 2547c478bd9Sstevel@tonic-gate static int readflg; 2557c478bd9Sstevel@tonic-gate static int eflg; 2567c478bd9Sstevel@tonic-gate static int qflg = 0; 2577c478bd9Sstevel@tonic-gate static int ncflg; 2587c478bd9Sstevel@tonic-gate static int listn; 2597c478bd9Sstevel@tonic-gate static int listf; 2607c478bd9Sstevel@tonic-gate static int pflag; 2617c478bd9Sstevel@tonic-gate static int flag28 = 0; /* Prevents write after a partial read */ 2627c478bd9Sstevel@tonic-gate static int save28 = 0; /* Flag whether buffer empty at start of read */ 2637c478bd9Sstevel@tonic-gate static long savtime; 2647c478bd9Sstevel@tonic-gate static char *name = "SHELL"; 2657c478bd9Sstevel@tonic-gate static char *rshell = "/usr/lib/rsh"; 2667c478bd9Sstevel@tonic-gate static char *shpath; /* pointer to correct shell for execution */ 2677c478bd9Sstevel@tonic-gate /* of execlp() */ 2687c478bd9Sstevel@tonic-gate static char *val; 2697c478bd9Sstevel@tonic-gate static char *home; 2707c478bd9Sstevel@tonic-gate static int nodelim; 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate int makekey(int *); 2737c478bd9Sstevel@tonic-gate int _mbftowc(char *, wchar_t *, int (*)(), int *); 2747c478bd9Sstevel@tonic-gate static int error(int code); 2757c478bd9Sstevel@tonic-gate static void tlist(struct Fspec *); 2767c478bd9Sstevel@tonic-gate static void tstd(struct Fspec *); 2777c478bd9Sstevel@tonic-gate static void gdelete(void); 2787c478bd9Sstevel@tonic-gate static void delete(void); 2797c478bd9Sstevel@tonic-gate static void exfile(void); 2807c478bd9Sstevel@tonic-gate static void filename(int comm); 2817c478bd9Sstevel@tonic-gate static void newline(void); 2827c478bd9Sstevel@tonic-gate static int gettty(void); 2837c478bd9Sstevel@tonic-gate static void commands(void); 2847c478bd9Sstevel@tonic-gate static void undo(void); 2857c478bd9Sstevel@tonic-gate static void save(void); 2867c478bd9Sstevel@tonic-gate static void strcopy(char *source, char *dest); 2877c478bd9Sstevel@tonic-gate static int strequal(char **scan1, char *str); 2887c478bd9Sstevel@tonic-gate static int stdtab(char *, char *); 2897c478bd9Sstevel@tonic-gate static int lenchk(char *, struct Fspec *); 2907c478bd9Sstevel@tonic-gate static void clear(struct Fspec *); 2917c478bd9Sstevel@tonic-gate static int expnd(char *, char *, int *, struct Fspec *); 2927c478bd9Sstevel@tonic-gate static void tincr(int, struct Fspec *); 2937c478bd9Sstevel@tonic-gate static void targ(struct Fspec *); 2947c478bd9Sstevel@tonic-gate static int numb(void); 2957c478bd9Sstevel@tonic-gate static int fspec(char *, struct Fspec *, int); 2967c478bd9Sstevel@tonic-gate static void red(char *); 2977c478bd9Sstevel@tonic-gate static void newtime(void); 2987c478bd9Sstevel@tonic-gate static void chktime(void); 2997c478bd9Sstevel@tonic-gate static void getime(void); 3007c478bd9Sstevel@tonic-gate static void mkfunny(void); 3017c478bd9Sstevel@tonic-gate static int eopen(char *, int); 3027c478bd9Sstevel@tonic-gate static void eclose(int f); 3037c478bd9Sstevel@tonic-gate static void globaln(int); 3047c478bd9Sstevel@tonic-gate static char *getkey(const char *); 3057c478bd9Sstevel@tonic-gate static int execute(int, LINE); 3067c478bd9Sstevel@tonic-gate static void error1(int); 3077c478bd9Sstevel@tonic-gate static int getcopy(void); 3087c478bd9Sstevel@tonic-gate static void move(int); 3097c478bd9Sstevel@tonic-gate static void dosub(void); 3107c478bd9Sstevel@tonic-gate static int getsub(void); 3117c478bd9Sstevel@tonic-gate static int compsub(void); 3127c478bd9Sstevel@tonic-gate static void substitute(int); 3137c478bd9Sstevel@tonic-gate static void join(void); 3147c478bd9Sstevel@tonic-gate static void global(int); 3157c478bd9Sstevel@tonic-gate static void init(void); 3167c478bd9Sstevel@tonic-gate static void rdelete(LINE, LINE); 3177c478bd9Sstevel@tonic-gate static void append(int (*)(void), LINE); 3187c478bd9Sstevel@tonic-gate static int getfile(void); 3197c478bd9Sstevel@tonic-gate static void putfile(void); 3207c478bd9Sstevel@tonic-gate static void onpipe(int); 3217c478bd9Sstevel@tonic-gate static void onhup(int); 3227c478bd9Sstevel@tonic-gate static void onintr(int); 3237c478bd9Sstevel@tonic-gate static void setdot(void); 3247c478bd9Sstevel@tonic-gate static void setall(void); 3257c478bd9Sstevel@tonic-gate static void setnoaddr(void); 3267c478bd9Sstevel@tonic-gate static void nonzero(void); 3277c478bd9Sstevel@tonic-gate static void setzeroasone(void); 3287c478bd9Sstevel@tonic-gate static long putline(void); 3297c478bd9Sstevel@tonic-gate static LINE address(void); 330*23a1cceaSRoger A. Faulkner static char *getaline(long); 3317c478bd9Sstevel@tonic-gate static char *getblock(long, long); 3327c478bd9Sstevel@tonic-gate static char *place(char *, char *, char *); 3337c478bd9Sstevel@tonic-gate static void comple(wchar_t); 3347c478bd9Sstevel@tonic-gate static void putchr(unsigned char); 3357c478bd9Sstevel@tonic-gate static void putwchr(wchar_t); 3367c478bd9Sstevel@tonic-gate static int getchr(void); 3377c478bd9Sstevel@tonic-gate static void unixcom(void); 3387c478bd9Sstevel@tonic-gate static void blkio(int, char *, ssize_t (*)()); 3397c478bd9Sstevel@tonic-gate static void reverse(LINE, LINE); 3407c478bd9Sstevel@tonic-gate static void putd(); 3417c478bd9Sstevel@tonic-gate static wchar_t get_wchr(void); 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate static struct stat Fl, Tf; 3447c478bd9Sstevel@tonic-gate #ifndef RESEARCH 3457c478bd9Sstevel@tonic-gate static struct statvfs U; 3467c478bd9Sstevel@tonic-gate static int Short = 0; 3477c478bd9Sstevel@tonic-gate static mode_t oldmask; /* No umask while writing */ 3487c478bd9Sstevel@tonic-gate #endif 3497c478bd9Sstevel@tonic-gate static jmp_buf savej; 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate #ifdef NULLS 3527c478bd9Sstevel@tonic-gate int nulls; /* Null count */ 3537c478bd9Sstevel@tonic-gate #endif 3547c478bd9Sstevel@tonic-gate static long ccount; 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate static int errcnt = 0; 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate static void 3607c478bd9Sstevel@tonic-gate onpipe(int sig) 3617c478bd9Sstevel@tonic-gate { 3627c478bd9Sstevel@tonic-gate (int)error(0); 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate int 3667c478bd9Sstevel@tonic-gate main(int argc, char **argv) 3677c478bd9Sstevel@tonic-gate { 3687c478bd9Sstevel@tonic-gate char *p1, *p2; 3697c478bd9Sstevel@tonic-gate int c; 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3727c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 3737c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 3747c478bd9Sstevel@tonic-gate #endif 3757c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate oldquit = signal(SIGQUIT, SIG_IGN); 3787c478bd9Sstevel@tonic-gate oldhup = signal(SIGHUP, SIG_IGN); 3797c478bd9Sstevel@tonic-gate oldintr = signal(SIGINT, SIG_IGN); 3807c478bd9Sstevel@tonic-gate oldpipe = signal(SIGPIPE, onpipe); 3817c478bd9Sstevel@tonic-gate if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 3827c478bd9Sstevel@tonic-gate signal(SIGTERM, quit); 3837c478bd9Sstevel@tonic-gate p1 = *argv; 3847c478bd9Sstevel@tonic-gate while (*p1++); 3857c478bd9Sstevel@tonic-gate while (--p1 >= *argv) 3867c478bd9Sstevel@tonic-gate if (*p1 == '/') 3877c478bd9Sstevel@tonic-gate break; 3887c478bd9Sstevel@tonic-gate *argv = p1 + 1; 3897c478bd9Sstevel@tonic-gate /* if SHELL set in environment and is /usr/lib/rsh, set rflg */ 3907c478bd9Sstevel@tonic-gate if ((val = getenv(name)) != NULL) 3917c478bd9Sstevel@tonic-gate if (strcmp(val, rshell) == 0) 3927c478bd9Sstevel@tonic-gate rflg++; 3937c478bd9Sstevel@tonic-gate if (**argv == 'r') 3947c478bd9Sstevel@tonic-gate rflg++; 3957c478bd9Sstevel@tonic-gate home = getenv("HOME"); 3967c478bd9Sstevel@tonic-gate while (1) { 3977c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "sp:qxC")) != EOF) { 3987c478bd9Sstevel@tonic-gate switch (c) { 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate case 's': 4017c478bd9Sstevel@tonic-gate vflag = 0; 4027c478bd9Sstevel@tonic-gate break; 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate case 'p': 4057c478bd9Sstevel@tonic-gate strncpy(prompt, optarg, sizeof (prompt)-1); 4067c478bd9Sstevel@tonic-gate shflg = 1; 4077c478bd9Sstevel@tonic-gate break; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate case 'q': 4107c478bd9Sstevel@tonic-gate signal(SIGQUIT, SIG_DFL); 4117c478bd9Sstevel@tonic-gate vflag = 1; 4127c478bd9Sstevel@tonic-gate break; 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate case 'x': 4157c478bd9Sstevel@tonic-gate crflag = -1; 4167c478bd9Sstevel@tonic-gate xflag = 1; 4177c478bd9Sstevel@tonic-gate break; 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate case 'C': 4207c478bd9Sstevel@tonic-gate crflag = 1; 4217c478bd9Sstevel@tonic-gate xflag = 1; 4227c478bd9Sstevel@tonic-gate break; 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate case '?': 4257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4267c478bd9Sstevel@tonic-gate "Usage: ed [- | -s] [-p string] [-x] [-C] [file]\n" 4277c478bd9Sstevel@tonic-gate " red [- | -s] [-p string] [-x] [-C] [file]\n")); 4287c478bd9Sstevel@tonic-gate exit(2); 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate if (argv[optind] && strcmp(argv[optind], "-") == 0 && 4327c478bd9Sstevel@tonic-gate strcmp(argv[optind-1], "--") != 0) { 4337c478bd9Sstevel@tonic-gate vflag = 0; 4347c478bd9Sstevel@tonic-gate optind++; 4357c478bd9Sstevel@tonic-gate continue; 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate break; 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate argc = argc - optind; 4407c478bd9Sstevel@tonic-gate argv = &argv[optind]; 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate if (xflag) { 4437c478bd9Sstevel@tonic-gate if (permflag) 4447c478bd9Sstevel@tonic-gate crypt_close(perm); 4457c478bd9Sstevel@tonic-gate permflag = 1; 4467c478bd9Sstevel@tonic-gate if ((kflag = run_setkey(&perm[0], getkey(msgtab[66]))) 4477c478bd9Sstevel@tonic-gate == -1) { 4487c478bd9Sstevel@tonic-gate puts(gettext(msgtab[64])); 4497c478bd9Sstevel@tonic-gate xflag = 0; 4507c478bd9Sstevel@tonic-gate kflag = 0; 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate if (kflag == 0) 4537c478bd9Sstevel@tonic-gate crflag = 0; 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate if (argc > 0) { 4577c478bd9Sstevel@tonic-gate p1 = *argv; 4587c478bd9Sstevel@tonic-gate if (strlen(p1) >= (size_t)FNSIZE) { 4597c478bd9Sstevel@tonic-gate puts(gettext("file name too long")); 4607c478bd9Sstevel@tonic-gate if (kflag) 4617c478bd9Sstevel@tonic-gate crypt_close(perm); 4627c478bd9Sstevel@tonic-gate exit(2); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate p2 = savedfile; 4657c478bd9Sstevel@tonic-gate while (*p2++ = *p1++); 4667c478bd9Sstevel@tonic-gate globp = "e"; 4677c478bd9Sstevel@tonic-gate fflg++; 4687c478bd9Sstevel@tonic-gate } else /* editing with no file so set savtime to 0 */ 4697c478bd9Sstevel@tonic-gate savtime = 0; 4707c478bd9Sstevel@tonic-gate eflg++; 4717c478bd9Sstevel@tonic-gate if ((tfname = tempnam("", "ea")) == NULL) { 4727c478bd9Sstevel@tonic-gate puts(gettext(msgtab[69])); 4737c478bd9Sstevel@tonic-gate exit(2); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate fendcore = (LINE)sbrk(0); 4777c478bd9Sstevel@tonic-gate init(); 4787c478bd9Sstevel@tonic-gate if (oldintr != SIG_IGN) 4797c478bd9Sstevel@tonic-gate signal(SIGINT, onintr); 4807c478bd9Sstevel@tonic-gate if (oldhup != SIG_IGN) 4817c478bd9Sstevel@tonic-gate signal(SIGHUP, onhup); 4827c478bd9Sstevel@tonic-gate setjmp(savej); 4837c478bd9Sstevel@tonic-gate commands(); 4847c478bd9Sstevel@tonic-gate quit(sig); 4857c478bd9Sstevel@tonic-gate return (0); 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate static void 4897c478bd9Sstevel@tonic-gate commands(void) 4907c478bd9Sstevel@tonic-gate { 4917c478bd9Sstevel@tonic-gate LINE a1; 4927c478bd9Sstevel@tonic-gate int c; 4937c478bd9Sstevel@tonic-gate char *p1, *p2; 4947c478bd9Sstevel@tonic-gate int fsave, m, n; 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate for (;;) { 4977c478bd9Sstevel@tonic-gate nodelim = 0; 4987c478bd9Sstevel@tonic-gate if (pflag) { 4997c478bd9Sstevel@tonic-gate pflag = 0; 5007c478bd9Sstevel@tonic-gate addr1 = addr2 = dot; 5017c478bd9Sstevel@tonic-gate goto print; 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate if (shflg && globp == 0) 5047c478bd9Sstevel@tonic-gate write(1, gettext(prompt), strlen(gettext(prompt))); 5057c478bd9Sstevel@tonic-gate addr1 = 0; 5067c478bd9Sstevel@tonic-gate addr2 = 0; 5077c478bd9Sstevel@tonic-gate if ((c = getchr()) == ',') { 5087c478bd9Sstevel@tonic-gate addr1 = zero + 1; 5097c478bd9Sstevel@tonic-gate addr2 = dol; 5107c478bd9Sstevel@tonic-gate #ifdef XPG6 5117c478bd9Sstevel@tonic-gate /* XPG4 - it was an error if the second address was */ 5127c478bd9Sstevel@tonic-gate /* input and the first address was ommitted */ 5137c478bd9Sstevel@tonic-gate /* Parse second address */ 5147c478bd9Sstevel@tonic-gate if ((a1 = address()) != 0) { 5157c478bd9Sstevel@tonic-gate addr2 = a1; 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate #endif 5187c478bd9Sstevel@tonic-gate c = getchr(); 5197c478bd9Sstevel@tonic-gate goto swch; 5207c478bd9Sstevel@tonic-gate } else if (c == ';') { 5217c478bd9Sstevel@tonic-gate addr1 = dot; 5227c478bd9Sstevel@tonic-gate addr2 = dol; 5237c478bd9Sstevel@tonic-gate #ifdef XPG6 5247c478bd9Sstevel@tonic-gate /* XPG4 - it was an error if the second address was */ 5257c478bd9Sstevel@tonic-gate /* input and the first address was ommitted */ 5267c478bd9Sstevel@tonic-gate /* Parse second address */ 5277c478bd9Sstevel@tonic-gate if ((a1 = address()) != 0) { 5287c478bd9Sstevel@tonic-gate addr2 = a1; 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate #endif 5317c478bd9Sstevel@tonic-gate c = getchr(); 5327c478bd9Sstevel@tonic-gate goto swch; 5337c478bd9Sstevel@tonic-gate } else 5347c478bd9Sstevel@tonic-gate peekc = c; 5357c478bd9Sstevel@tonic-gate do { 5367c478bd9Sstevel@tonic-gate addr1 = addr2; 5377c478bd9Sstevel@tonic-gate if ((a1 = address()) == 0) { 5387c478bd9Sstevel@tonic-gate c = getchr(); 5397c478bd9Sstevel@tonic-gate break; 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate addr2 = a1; 5427c478bd9Sstevel@tonic-gate if ((c = getchr()) == ';') { 5437c478bd9Sstevel@tonic-gate c = ','; 5447c478bd9Sstevel@tonic-gate dot = a1; 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate } while (c == ','); 5477c478bd9Sstevel@tonic-gate if (addr1 == 0) 5487c478bd9Sstevel@tonic-gate addr1 = addr2; 5497c478bd9Sstevel@tonic-gate swch: 5507c478bd9Sstevel@tonic-gate switch (c) { 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate case 'a': 5537c478bd9Sstevel@tonic-gate setdot(); 5547c478bd9Sstevel@tonic-gate newline(); 5557c478bd9Sstevel@tonic-gate if (!globflg) save(); 5567c478bd9Sstevel@tonic-gate append(gettty, addr2); 5577c478bd9Sstevel@tonic-gate continue; 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate case 'c': 5607c478bd9Sstevel@tonic-gate #ifdef XPG6 5617c478bd9Sstevel@tonic-gate setzeroasone(); 5627c478bd9Sstevel@tonic-gate #endif 5637c478bd9Sstevel@tonic-gate delete(); 5647c478bd9Sstevel@tonic-gate append(gettty, addr1-1); 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate /* XPG4 - If no new lines are inserted, then the current */ 5677c478bd9Sstevel@tonic-gate /* line becomes the line after the lines deleted. */ 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate if (((linebuf[0] != '.') || (dot == (addr1-1))) && 5707c478bd9Sstevel@tonic-gate (addr2 <= dol)) 5717c478bd9Sstevel@tonic-gate dot = addr1; 5727c478bd9Sstevel@tonic-gate continue; 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate case 'd': 5757c478bd9Sstevel@tonic-gate delete(); 5767c478bd9Sstevel@tonic-gate continue; 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate case 'E': 5797c478bd9Sstevel@tonic-gate fchange = 0; 5807c478bd9Sstevel@tonic-gate c = 'e'; 5817c478bd9Sstevel@tonic-gate case 'e': 5827c478bd9Sstevel@tonic-gate fflg++; 5837c478bd9Sstevel@tonic-gate setnoaddr(); 5847c478bd9Sstevel@tonic-gate if (vflag && fchange) { 5857c478bd9Sstevel@tonic-gate fchange = 0; 5867c478bd9Sstevel@tonic-gate (void) error(1); 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate filename(c); 5897c478bd9Sstevel@tonic-gate eflg++; 5907c478bd9Sstevel@tonic-gate init(); 5917c478bd9Sstevel@tonic-gate addr2 = zero; 5927c478bd9Sstevel@tonic-gate goto caseread; 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate case 'f': 5957c478bd9Sstevel@tonic-gate setnoaddr(); 5967c478bd9Sstevel@tonic-gate filename(c); 5977c478bd9Sstevel@tonic-gate if (!ncflg) /* there is a filename */ 5987c478bd9Sstevel@tonic-gate getime(); 5997c478bd9Sstevel@tonic-gate else 6007c478bd9Sstevel@tonic-gate ncflg--; 6017c478bd9Sstevel@tonic-gate puts(savedfile); 6027c478bd9Sstevel@tonic-gate continue; 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate case 'g': 6057c478bd9Sstevel@tonic-gate global(1); 6067c478bd9Sstevel@tonic-gate continue; 6077c478bd9Sstevel@tonic-gate case 'G': 6087c478bd9Sstevel@tonic-gate globaln(1); 6097c478bd9Sstevel@tonic-gate continue; 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate case 'h': 6127c478bd9Sstevel@tonic-gate newline(); 6137c478bd9Sstevel@tonic-gate setnoaddr(); 6147c478bd9Sstevel@tonic-gate PUTM(); 6157c478bd9Sstevel@tonic-gate continue; 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate case 'H': 6187c478bd9Sstevel@tonic-gate newline(); 6197c478bd9Sstevel@tonic-gate setnoaddr(); 6207c478bd9Sstevel@tonic-gate if (!hflag) { 6217c478bd9Sstevel@tonic-gate hflag = 1; 6227c478bd9Sstevel@tonic-gate PUTM(); 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate else 6257c478bd9Sstevel@tonic-gate hflag = 0; 6267c478bd9Sstevel@tonic-gate continue; 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate case 'i': 6297c478bd9Sstevel@tonic-gate #ifdef XPG6 6307c478bd9Sstevel@tonic-gate setzeroasone(); 6317c478bd9Sstevel@tonic-gate #endif 6327c478bd9Sstevel@tonic-gate setdot(); 6337c478bd9Sstevel@tonic-gate nonzero(); 6347c478bd9Sstevel@tonic-gate newline(); 6357c478bd9Sstevel@tonic-gate if (!globflg) save(); 6367c478bd9Sstevel@tonic-gate append(gettty, addr2-1); 6377c478bd9Sstevel@tonic-gate if (dot == addr2-1) 6387c478bd9Sstevel@tonic-gate dot += 1; 6397c478bd9Sstevel@tonic-gate continue; 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate case 'j': 6427c478bd9Sstevel@tonic-gate if (addr2 == 0) { 6437c478bd9Sstevel@tonic-gate addr1 = dot; 6447c478bd9Sstevel@tonic-gate addr2 = dot+1; 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate setdot(); 6477c478bd9Sstevel@tonic-gate newline(); 6487c478bd9Sstevel@tonic-gate nonzero(); 6497c478bd9Sstevel@tonic-gate if (!globflg) save(); 6507c478bd9Sstevel@tonic-gate join(); 6517c478bd9Sstevel@tonic-gate continue; 6527c478bd9Sstevel@tonic-gate 6537c478bd9Sstevel@tonic-gate case 'k': 6547c478bd9Sstevel@tonic-gate if ((c = getchr()) < 'a' || c > 'z') 6557c478bd9Sstevel@tonic-gate (void) error(2); 6567c478bd9Sstevel@tonic-gate newline(); 6577c478bd9Sstevel@tonic-gate setdot(); 6587c478bd9Sstevel@tonic-gate nonzero(); 6597c478bd9Sstevel@tonic-gate names[c-'a'] = addr2->cur & ~01; 6607c478bd9Sstevel@tonic-gate anymarks |= 01; 6617c478bd9Sstevel@tonic-gate continue; 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate case 'm': 6647c478bd9Sstevel@tonic-gate move(0); 6657c478bd9Sstevel@tonic-gate continue; 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate case '\n': 6687c478bd9Sstevel@tonic-gate if (addr2 == 0) 6697c478bd9Sstevel@tonic-gate addr2 = dot+1; 6707c478bd9Sstevel@tonic-gate addr1 = addr2; 6717c478bd9Sstevel@tonic-gate goto print; 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate case 'n': 6747c478bd9Sstevel@tonic-gate listn++; 6757c478bd9Sstevel@tonic-gate newline(); 6767c478bd9Sstevel@tonic-gate goto print; 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate case 'l': 6797c478bd9Sstevel@tonic-gate listf++; 6807c478bd9Sstevel@tonic-gate case 'p': 6817c478bd9Sstevel@tonic-gate newline(); 6827c478bd9Sstevel@tonic-gate print: 6837c478bd9Sstevel@tonic-gate setdot(); 6847c478bd9Sstevel@tonic-gate nonzero(); 6857c478bd9Sstevel@tonic-gate a1 = addr1; 6867c478bd9Sstevel@tonic-gate do { 6877c478bd9Sstevel@tonic-gate if (listn) { 6887c478bd9Sstevel@tonic-gate count = a1 - zero; 6897c478bd9Sstevel@tonic-gate putd(); 6907c478bd9Sstevel@tonic-gate putchr('\t'); 6917c478bd9Sstevel@tonic-gate } 692*23a1cceaSRoger A. Faulkner puts(getaline((a1++)->cur)); 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate while (a1 <= addr2); 6957c478bd9Sstevel@tonic-gate dot = addr2; 6967c478bd9Sstevel@tonic-gate pflag = 0; 6977c478bd9Sstevel@tonic-gate listn = 0; 6987c478bd9Sstevel@tonic-gate listf = 0; 6997c478bd9Sstevel@tonic-gate continue; 7007c478bd9Sstevel@tonic-gate 7017c478bd9Sstevel@tonic-gate case 'Q': 7027c478bd9Sstevel@tonic-gate fchange = 0; 7037c478bd9Sstevel@tonic-gate case 'q': 7047c478bd9Sstevel@tonic-gate setnoaddr(); 7057c478bd9Sstevel@tonic-gate newline(); 7067c478bd9Sstevel@tonic-gate quit(sig); 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate case 'r': 7097c478bd9Sstevel@tonic-gate filename(c); 7107c478bd9Sstevel@tonic-gate caseread: 7117c478bd9Sstevel@tonic-gate readflg = 1; 7127c478bd9Sstevel@tonic-gate save28 = (dol != fendcore); 7137c478bd9Sstevel@tonic-gate if (crflag == 2 || crflag == -2) 7147c478bd9Sstevel@tonic-gate crflag = -1; /* restore crflag for next file */ 7157c478bd9Sstevel@tonic-gate errno = 0; 7167c478bd9Sstevel@tonic-gate if ((io = eopen(file, O_RDONLY)) < 0) { 7177c478bd9Sstevel@tonic-gate lastc = '\n'; 7187c478bd9Sstevel@tonic-gate /* if first entering editor and file does not exist */ 7197c478bd9Sstevel@tonic-gate /* set saved access time to 0 */ 7207c478bd9Sstevel@tonic-gate if (eflg) { 7217c478bd9Sstevel@tonic-gate savtime = 0; 7227c478bd9Sstevel@tonic-gate eflg = 0; 7237c478bd9Sstevel@tonic-gate if (c == 'e' && vflag == 0) 7247c478bd9Sstevel@tonic-gate qflg = 1; 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate if (errno == ENOENT) { 7277c478bd9Sstevel@tonic-gate (void) error(68); 7287c478bd9Sstevel@tonic-gate } else { 7297c478bd9Sstevel@tonic-gate (void) error(3); 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate } 7327c478bd9Sstevel@tonic-gate /* get last mod time of file */ 7337c478bd9Sstevel@tonic-gate /* eflg - entered editor with ed or e */ 7347c478bd9Sstevel@tonic-gate if (eflg) { 7357c478bd9Sstevel@tonic-gate eflg = 0; 7367c478bd9Sstevel@tonic-gate getime(); 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate setall(); 7397c478bd9Sstevel@tonic-gate ninbuf = 0; 7407c478bd9Sstevel@tonic-gate n = zero != dol; 7417c478bd9Sstevel@tonic-gate #ifdef NULLS 7427c478bd9Sstevel@tonic-gate nulls = 0; 7437c478bd9Sstevel@tonic-gate #endif 7447c478bd9Sstevel@tonic-gate if (!globflg && (c == 'r')) save(); 7457c478bd9Sstevel@tonic-gate append(getfile, addr2); 7467c478bd9Sstevel@tonic-gate exfile(); 7477c478bd9Sstevel@tonic-gate readflg = 0; 7487c478bd9Sstevel@tonic-gate fchange = n; 7497c478bd9Sstevel@tonic-gate continue; 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate case 's': 7527c478bd9Sstevel@tonic-gate setdot(); 7537c478bd9Sstevel@tonic-gate nonzero(); 7547c478bd9Sstevel@tonic-gate if (!globflg) save(); 7557c478bd9Sstevel@tonic-gate substitute(globp != 0); 7567c478bd9Sstevel@tonic-gate continue; 7577c478bd9Sstevel@tonic-gate 7587c478bd9Sstevel@tonic-gate case 't': 7597c478bd9Sstevel@tonic-gate move(1); 7607c478bd9Sstevel@tonic-gate continue; 7617c478bd9Sstevel@tonic-gate 7627c478bd9Sstevel@tonic-gate case 'u': 7637c478bd9Sstevel@tonic-gate setdot(); 7647c478bd9Sstevel@tonic-gate newline(); 7657c478bd9Sstevel@tonic-gate if (!initflg) 7667c478bd9Sstevel@tonic-gate undo(); 7677c478bd9Sstevel@tonic-gate else 7687c478bd9Sstevel@tonic-gate (void) error(5); 7697c478bd9Sstevel@tonic-gate fchange = 1; 7707c478bd9Sstevel@tonic-gate continue; 7717c478bd9Sstevel@tonic-gate 7727c478bd9Sstevel@tonic-gate case 'v': 7737c478bd9Sstevel@tonic-gate global(0); 7747c478bd9Sstevel@tonic-gate continue; 7757c478bd9Sstevel@tonic-gate case 'V': 7767c478bd9Sstevel@tonic-gate globaln(0); 7777c478bd9Sstevel@tonic-gate continue; 7787c478bd9Sstevel@tonic-gate 7797c478bd9Sstevel@tonic-gate case 'W': 7807c478bd9Sstevel@tonic-gate case 'w': 7817c478bd9Sstevel@tonic-gate if (flag28) { 7827c478bd9Sstevel@tonic-gate flag28 = 0; 7837c478bd9Sstevel@tonic-gate fchange = 0; 7847c478bd9Sstevel@tonic-gate (void) error(61); 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate setall(); 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate /* on NULL-RE condition do not generate error */ 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate if ((linebuf[0] != '.') && (zero != dol) && 7917c478bd9Sstevel@tonic-gate (addr1 <= zero || addr2 > dol)) 7927c478bd9Sstevel@tonic-gate (void) error(15); 7937c478bd9Sstevel@tonic-gate filename(c); 7947c478bd9Sstevel@tonic-gate if (Xqt) { 7957c478bd9Sstevel@tonic-gate io = eopen(file, O_WRONLY); 7967c478bd9Sstevel@tonic-gate n = 1; /* set n so newtime will not execute */ 7977c478bd9Sstevel@tonic-gate } else { 7987c478bd9Sstevel@tonic-gate struct stat lFl; 7997c478bd9Sstevel@tonic-gate fstat(tfile, &Tf); 8007c478bd9Sstevel@tonic-gate if (stat(file, &Fl) < 0) { 8017c478bd9Sstevel@tonic-gate if ((io = creat(file, S_IRUSR|S_IWUSR|S_IRGRP 8027c478bd9Sstevel@tonic-gate |S_IWGRP|S_IROTH|S_IWOTH)) < 0) 8037c478bd9Sstevel@tonic-gate (void) error(7); 8047c478bd9Sstevel@tonic-gate fstat(io, &Fl); 8057c478bd9Sstevel@tonic-gate Fl.st_mtime = 0; 8067c478bd9Sstevel@tonic-gate lFl = Fl; 8077c478bd9Sstevel@tonic-gate close(io); 8087c478bd9Sstevel@tonic-gate } else { 8097c478bd9Sstevel@tonic-gate #ifndef RESEARCH 8107c478bd9Sstevel@tonic-gate oldmask = umask(0); 8117c478bd9Sstevel@tonic-gate /* 8127c478bd9Sstevel@tonic-gate * Must determine if file is 8137c478bd9Sstevel@tonic-gate * a symbolic link 8147c478bd9Sstevel@tonic-gate */ 8157c478bd9Sstevel@tonic-gate lstat(file, &lFl); 8167c478bd9Sstevel@tonic-gate #endif 8177c478bd9Sstevel@tonic-gate } 8187c478bd9Sstevel@tonic-gate #ifndef RESEARCH 8197c478bd9Sstevel@tonic-gate /* 8207c478bd9Sstevel@tonic-gate * Determine if there are enough free blocks on system 8217c478bd9Sstevel@tonic-gate */ 8227c478bd9Sstevel@tonic-gate if (!Short && statvfs(file, &U) == 0 && 8237c478bd9Sstevel@tonic-gate U.f_bfree < ((Tf.st_size/U.f_frsize) + 100)) { 8247c478bd9Sstevel@tonic-gate Short = 1; 8257c478bd9Sstevel@tonic-gate (void) error(8); 8267c478bd9Sstevel@tonic-gate } 8277c478bd9Sstevel@tonic-gate Short = 0; 8287c478bd9Sstevel@tonic-gate #endif 8297c478bd9Sstevel@tonic-gate p1 = savedfile; /* The current filename */ 8307c478bd9Sstevel@tonic-gate p2 = file; 8317c478bd9Sstevel@tonic-gate m = strcmp(p1, p2); 8327c478bd9Sstevel@tonic-gate if (c == 'w' && Fl.st_nlink == 1 && ISREG(lFl)) { 8337c478bd9Sstevel@tonic-gate if (close(open(file, O_WRONLY)) < 0) 8347c478bd9Sstevel@tonic-gate (void) error(9); 8357c478bd9Sstevel@tonic-gate if (!(n = m)) 8367c478bd9Sstevel@tonic-gate chktime(); 8377c478bd9Sstevel@tonic-gate mkfunny(); 8387c478bd9Sstevel@tonic-gate /* 8397c478bd9Sstevel@tonic-gate * If funlink equals one it means that 8407c478bd9Sstevel@tonic-gate * funny points to a valid file which must 8417c478bd9Sstevel@tonic-gate * be unlinked when interrupted. 8427c478bd9Sstevel@tonic-gate */ 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate funlink = 1; 8457c478bd9Sstevel@tonic-gate if ((io = creat(funny, FMODE(Fl))) >= 0) { 8467c478bd9Sstevel@tonic-gate chown(funny, Fl.st_uid, Fl.st_gid); 8477c478bd9Sstevel@tonic-gate chmod(funny, FMODE(Fl)); 8487c478bd9Sstevel@tonic-gate putfile(); 8497c478bd9Sstevel@tonic-gate exfile(); 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate if (rename(funny, file)) 8527c478bd9Sstevel@tonic-gate (void) error(10); 8537c478bd9Sstevel@tonic-gate funlink = 0; 8547c478bd9Sstevel@tonic-gate /* if filenames are the same */ 8557c478bd9Sstevel@tonic-gate if (!n) 8567c478bd9Sstevel@tonic-gate newtime(); 8577c478bd9Sstevel@tonic-gate /* check if entire buffer was written */ 8587c478bd9Sstevel@tonic-gate fsave = fchange; 8597c478bd9Sstevel@tonic-gate fchange = (((addr1 == zero) || (addr1 == (zero + 1))) && 8607c478bd9Sstevel@tonic-gate (addr2 == dol)) ? 0 : 1; 8617c478bd9Sstevel@tonic-gate if (fchange == 1 && m != 0) fchange = fsave; 8627c478bd9Sstevel@tonic-gate continue; 8637c478bd9Sstevel@tonic-gate } 8647c478bd9Sstevel@tonic-gate } else 8657c478bd9Sstevel@tonic-gate n = 1; /* set n so newtime will not execute */ 8667c478bd9Sstevel@tonic-gate if ((io = open(file, 8677c478bd9Sstevel@tonic-gate (c == 'w') ? O_WRONLY|O_CREAT|O_TRUNC 8687c478bd9Sstevel@tonic-gate : O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR 8697c478bd9Sstevel@tonic-gate |S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) 8707c478bd9Sstevel@tonic-gate (void) error(7); 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate putfile(); 8737c478bd9Sstevel@tonic-gate exfile(); 8747c478bd9Sstevel@tonic-gate if (!n) newtime(); 8757c478bd9Sstevel@tonic-gate fsave = fchange; 8767c478bd9Sstevel@tonic-gate fchange = (((addr1 == zero) || (addr1 == (zero + 1))) && 8777c478bd9Sstevel@tonic-gate (addr2 == dol)) ? 0 : 1; 8787c478bd9Sstevel@tonic-gate /* Leave fchange alone if partial write was to another file */ 8797c478bd9Sstevel@tonic-gate if (fchange == 1 && m != 0) fchange = fsave; 8807c478bd9Sstevel@tonic-gate continue; 8817c478bd9Sstevel@tonic-gate 8827c478bd9Sstevel@tonic-gate case 'C': 8837c478bd9Sstevel@tonic-gate crflag = 1; 8847c478bd9Sstevel@tonic-gate /* 8857c478bd9Sstevel@tonic-gate * C is same as X, but always assume input files are 8867c478bd9Sstevel@tonic-gate * ciphertext 8877c478bd9Sstevel@tonic-gate */ 8887c478bd9Sstevel@tonic-gate goto encrypt; 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate case 'X': 8917c478bd9Sstevel@tonic-gate crflag = -1; 8927c478bd9Sstevel@tonic-gate encrypt: 8937c478bd9Sstevel@tonic-gate setnoaddr(); 8947c478bd9Sstevel@tonic-gate newline(); 8957c478bd9Sstevel@tonic-gate xflag = 1; 8967c478bd9Sstevel@tonic-gate if (permflag) 8977c478bd9Sstevel@tonic-gate (void) crypt_close(perm); 8987c478bd9Sstevel@tonic-gate permflag = 1; 8997c478bd9Sstevel@tonic-gate if ((kflag = run_setkey(&perm[0], getkey(msgtab[66]))) 9007c478bd9Sstevel@tonic-gate == -1) { 9017c478bd9Sstevel@tonic-gate xflag = 0; 9027c478bd9Sstevel@tonic-gate kflag = 0; 9037c478bd9Sstevel@tonic-gate crflag = 0; 9047c478bd9Sstevel@tonic-gate (void) error(64); 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate if (kflag == 0) 9077c478bd9Sstevel@tonic-gate crflag = 0; 9087c478bd9Sstevel@tonic-gate continue; 9097c478bd9Sstevel@tonic-gate 9107c478bd9Sstevel@tonic-gate case '=': 9117c478bd9Sstevel@tonic-gate setall(); 9127c478bd9Sstevel@tonic-gate newline(); 9137c478bd9Sstevel@tonic-gate count = (addr2-zero)&077777; 9147c478bd9Sstevel@tonic-gate putd(); 9157c478bd9Sstevel@tonic-gate putchr('\n'); 9167c478bd9Sstevel@tonic-gate continue; 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate case '!': 9197c478bd9Sstevel@tonic-gate unixcom(); 9207c478bd9Sstevel@tonic-gate continue; 9217c478bd9Sstevel@tonic-gate 9227c478bd9Sstevel@tonic-gate case EOF: 9237c478bd9Sstevel@tonic-gate return; 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate case 'P': 9267c478bd9Sstevel@tonic-gate setnoaddr(); 9277c478bd9Sstevel@tonic-gate newline(); 9287c478bd9Sstevel@tonic-gate if (shflg) 9297c478bd9Sstevel@tonic-gate shflg = 0; 9307c478bd9Sstevel@tonic-gate else 9317c478bd9Sstevel@tonic-gate shflg++; 9327c478bd9Sstevel@tonic-gate continue; 9337c478bd9Sstevel@tonic-gate } 9347c478bd9Sstevel@tonic-gate if (c == 'x') 9357c478bd9Sstevel@tonic-gate (void) error(60); 9367c478bd9Sstevel@tonic-gate else 9377c478bd9Sstevel@tonic-gate (void) error(12); 9387c478bd9Sstevel@tonic-gate } 9397c478bd9Sstevel@tonic-gate } 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate LINE 9427c478bd9Sstevel@tonic-gate address(void) 9437c478bd9Sstevel@tonic-gate { 9447c478bd9Sstevel@tonic-gate int minus, c; 9457c478bd9Sstevel@tonic-gate LINE a1; 9467c478bd9Sstevel@tonic-gate int n, relerr, retval; 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate minus = 0; 9497c478bd9Sstevel@tonic-gate a1 = 0; 9507c478bd9Sstevel@tonic-gate for (;;) { 9517c478bd9Sstevel@tonic-gate c = getchr(); 9527c478bd9Sstevel@tonic-gate if ('0' <= c && c <= '9') { 9537c478bd9Sstevel@tonic-gate n = 0; 9547c478bd9Sstevel@tonic-gate do { 9557c478bd9Sstevel@tonic-gate n *= 10; 9567c478bd9Sstevel@tonic-gate n += c - '0'; 9577c478bd9Sstevel@tonic-gate } while ((c = getchr()) >= '0' && c <= '9'); 9587c478bd9Sstevel@tonic-gate peekc = c; 9597c478bd9Sstevel@tonic-gate if (a1 == 0) 9607c478bd9Sstevel@tonic-gate a1 = zero; 9617c478bd9Sstevel@tonic-gate if (minus < 0) 9627c478bd9Sstevel@tonic-gate n = -n; 9637c478bd9Sstevel@tonic-gate a1 += n; 9647c478bd9Sstevel@tonic-gate minus = 0; 9657c478bd9Sstevel@tonic-gate continue; 9667c478bd9Sstevel@tonic-gate } 9677c478bd9Sstevel@tonic-gate relerr = 0; 9687c478bd9Sstevel@tonic-gate if (a1 || minus) 9697c478bd9Sstevel@tonic-gate relerr++; 9707c478bd9Sstevel@tonic-gate switch (c) { 9717c478bd9Sstevel@tonic-gate case ' ': 9727c478bd9Sstevel@tonic-gate case '\t': 9737c478bd9Sstevel@tonic-gate continue; 9747c478bd9Sstevel@tonic-gate 9757c478bd9Sstevel@tonic-gate case '+': 9767c478bd9Sstevel@tonic-gate minus++; 9777c478bd9Sstevel@tonic-gate if (a1 == 0) 9787c478bd9Sstevel@tonic-gate a1 = dot; 9797c478bd9Sstevel@tonic-gate continue; 9807c478bd9Sstevel@tonic-gate 9817c478bd9Sstevel@tonic-gate case '-': 9827c478bd9Sstevel@tonic-gate case '^': 9837c478bd9Sstevel@tonic-gate minus--; 9847c478bd9Sstevel@tonic-gate if (a1 == 0) 9857c478bd9Sstevel@tonic-gate a1 = dot; 9867c478bd9Sstevel@tonic-gate continue; 9877c478bd9Sstevel@tonic-gate 9887c478bd9Sstevel@tonic-gate case '?': 9897c478bd9Sstevel@tonic-gate case '/': 9907c478bd9Sstevel@tonic-gate comple(c); 9917c478bd9Sstevel@tonic-gate a1 = dot; 9927c478bd9Sstevel@tonic-gate for (;;) { 9937c478bd9Sstevel@tonic-gate if (c == '/') { 9947c478bd9Sstevel@tonic-gate a1++; 9957c478bd9Sstevel@tonic-gate if (a1 > dol) 9967c478bd9Sstevel@tonic-gate a1 = zero; 9977c478bd9Sstevel@tonic-gate } else { 9987c478bd9Sstevel@tonic-gate a1--; 9997c478bd9Sstevel@tonic-gate if (a1 < zero) 10007c478bd9Sstevel@tonic-gate a1 = dol; 10017c478bd9Sstevel@tonic-gate } 10027c478bd9Sstevel@tonic-gate 10037c478bd9Sstevel@tonic-gate if (execute(0, a1)) 10047c478bd9Sstevel@tonic-gate break; 10057c478bd9Sstevel@tonic-gate if (a1 == dot) 10067c478bd9Sstevel@tonic-gate (void) error(13); 10077c478bd9Sstevel@tonic-gate } 10087c478bd9Sstevel@tonic-gate break; 10097c478bd9Sstevel@tonic-gate 10107c478bd9Sstevel@tonic-gate case '$': 10117c478bd9Sstevel@tonic-gate a1 = dol; 10127c478bd9Sstevel@tonic-gate break; 10137c478bd9Sstevel@tonic-gate 10147c478bd9Sstevel@tonic-gate case '.': 10157c478bd9Sstevel@tonic-gate a1 = dot; 10167c478bd9Sstevel@tonic-gate break; 10177c478bd9Sstevel@tonic-gate 10187c478bd9Sstevel@tonic-gate case '\'': 10197c478bd9Sstevel@tonic-gate if ((c = getchr()) < 'a' || c > 'z') 10207c478bd9Sstevel@tonic-gate (void) error(2); 10217c478bd9Sstevel@tonic-gate for (a1 = zero; a1 <= dol; a1++) 10227c478bd9Sstevel@tonic-gate if (names[c-'a'] == (a1->cur & ~01)) 10237c478bd9Sstevel@tonic-gate break; 10247c478bd9Sstevel@tonic-gate break; 10257c478bd9Sstevel@tonic-gate 10267c478bd9Sstevel@tonic-gate default: 10277c478bd9Sstevel@tonic-gate peekc = c; 10287c478bd9Sstevel@tonic-gate if (a1 == 0) 10297c478bd9Sstevel@tonic-gate return (0); 10307c478bd9Sstevel@tonic-gate a1 += minus; 10317c478bd9Sstevel@tonic-gate 10327c478bd9Sstevel@tonic-gate /* on NULL-RE condition do not generate error */ 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate if ((linebuf[0] != '.') && (a1 < zero || a1 > dol)) 10357c478bd9Sstevel@tonic-gate (void) error(15); 10367c478bd9Sstevel@tonic-gate return (a1); 10377c478bd9Sstevel@tonic-gate } 10387c478bd9Sstevel@tonic-gate if (relerr) 10397c478bd9Sstevel@tonic-gate (void) error(16); 10407c478bd9Sstevel@tonic-gate } 10417c478bd9Sstevel@tonic-gate } 10427c478bd9Sstevel@tonic-gate 10437c478bd9Sstevel@tonic-gate static void 10447c478bd9Sstevel@tonic-gate setdot(void) 10457c478bd9Sstevel@tonic-gate { 10467c478bd9Sstevel@tonic-gate if (addr2 == 0) 10477c478bd9Sstevel@tonic-gate addr1 = addr2 = dot; 10487c478bd9Sstevel@tonic-gate if (addr1 > addr2) 10497c478bd9Sstevel@tonic-gate (void) error(17); 10507c478bd9Sstevel@tonic-gate } 10517c478bd9Sstevel@tonic-gate 10527c478bd9Sstevel@tonic-gate static void 10537c478bd9Sstevel@tonic-gate setall(void) 10547c478bd9Sstevel@tonic-gate { 10557c478bd9Sstevel@tonic-gate if (addr2 == 0) { 10567c478bd9Sstevel@tonic-gate addr1 = zero+1; 10577c478bd9Sstevel@tonic-gate addr2 = dol; 10587c478bd9Sstevel@tonic-gate if (dol == zero) 10597c478bd9Sstevel@tonic-gate addr1 = zero; 10607c478bd9Sstevel@tonic-gate } 10617c478bd9Sstevel@tonic-gate setdot(); 10627c478bd9Sstevel@tonic-gate } 10637c478bd9Sstevel@tonic-gate 10647c478bd9Sstevel@tonic-gate static void 10657c478bd9Sstevel@tonic-gate setnoaddr(void) 10667c478bd9Sstevel@tonic-gate { 10677c478bd9Sstevel@tonic-gate if (addr2) 10687c478bd9Sstevel@tonic-gate (void) error(18); 10697c478bd9Sstevel@tonic-gate } 10707c478bd9Sstevel@tonic-gate 10717c478bd9Sstevel@tonic-gate static void 10727c478bd9Sstevel@tonic-gate nonzero(void) 10737c478bd9Sstevel@tonic-gate { 10747c478bd9Sstevel@tonic-gate /* on NULL-RE condition do not generate error */ 10757c478bd9Sstevel@tonic-gate 10767c478bd9Sstevel@tonic-gate if ((linebuf[0] != '.') && (addr1 <= zero || addr2 > dol)) 10777c478bd9Sstevel@tonic-gate (void) error(15); 10787c478bd9Sstevel@tonic-gate } 10797c478bd9Sstevel@tonic-gate 10807c478bd9Sstevel@tonic-gate static void 10817c478bd9Sstevel@tonic-gate setzeroasone(void) 10827c478bd9Sstevel@tonic-gate { 10837c478bd9Sstevel@tonic-gate /* for the c and i commands 0 equal to 1 address */ 10847c478bd9Sstevel@tonic-gate if (addr1 == zero) { 10857c478bd9Sstevel@tonic-gate addr1 = zero+1; 10867c478bd9Sstevel@tonic-gate } 10877c478bd9Sstevel@tonic-gate if (addr2 == zero) { 10887c478bd9Sstevel@tonic-gate addr2 = zero+1; 10897c478bd9Sstevel@tonic-gate } 10907c478bd9Sstevel@tonic-gate } 10917c478bd9Sstevel@tonic-gate 10927c478bd9Sstevel@tonic-gate 10937c478bd9Sstevel@tonic-gate static void 10947c478bd9Sstevel@tonic-gate newline(void) 10957c478bd9Sstevel@tonic-gate { 10967c478bd9Sstevel@tonic-gate int c; 10977c478bd9Sstevel@tonic-gate 10987c478bd9Sstevel@tonic-gate if ((c = getchr()) == '\n') 10997c478bd9Sstevel@tonic-gate return; 11007c478bd9Sstevel@tonic-gate if (c == 'p' || c == 'l' || c == 'n') { 11017c478bd9Sstevel@tonic-gate pflag++; 11027c478bd9Sstevel@tonic-gate if (c == 'l') listf++; 11037c478bd9Sstevel@tonic-gate if (c == 'n') listn++; 11047c478bd9Sstevel@tonic-gate if ((c = getchr()) == '\n') 11057c478bd9Sstevel@tonic-gate return; 11067c478bd9Sstevel@tonic-gate } 11077c478bd9Sstevel@tonic-gate (void) error(20); 11087c478bd9Sstevel@tonic-gate } 11097c478bd9Sstevel@tonic-gate 11107c478bd9Sstevel@tonic-gate static void 11117c478bd9Sstevel@tonic-gate filename(int comm) 11127c478bd9Sstevel@tonic-gate { 11137c478bd9Sstevel@tonic-gate char *p1, *p2; 11147c478bd9Sstevel@tonic-gate int c; 11157c478bd9Sstevel@tonic-gate int i = 0; 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate count = 0; 11187c478bd9Sstevel@tonic-gate c = getchr(); 11197c478bd9Sstevel@tonic-gate if (c == '\n' || c == EOF) { 11207c478bd9Sstevel@tonic-gate p1 = savedfile; 11217c478bd9Sstevel@tonic-gate if (*p1 == 0 && comm != 'f') 11227c478bd9Sstevel@tonic-gate (void) error(21); 11237c478bd9Sstevel@tonic-gate /* ncflg set means do not get mod time of file */ 11247c478bd9Sstevel@tonic-gate /* since no filename followed f */ 11257c478bd9Sstevel@tonic-gate if (comm == 'f') 11267c478bd9Sstevel@tonic-gate ncflg++; 11277c478bd9Sstevel@tonic-gate p2 = file; 11287c478bd9Sstevel@tonic-gate while (*p2++ = *p1++); 11297c478bd9Sstevel@tonic-gate red(savedfile); 11307c478bd9Sstevel@tonic-gate return; 11317c478bd9Sstevel@tonic-gate } 11327c478bd9Sstevel@tonic-gate if (c != ' ') 11337c478bd9Sstevel@tonic-gate (void) error(22); 11347c478bd9Sstevel@tonic-gate while ((c = getchr()) == ' '); 11357c478bd9Sstevel@tonic-gate if (c == '!') 11367c478bd9Sstevel@tonic-gate ++Xqt, c = getchr(); 11377c478bd9Sstevel@tonic-gate if (c == '\n') 11387c478bd9Sstevel@tonic-gate (void) error(21); 11397c478bd9Sstevel@tonic-gate p1 = file; 11407c478bd9Sstevel@tonic-gate do { 11417c478bd9Sstevel@tonic-gate if (++i >= FNSIZE) 11427c478bd9Sstevel@tonic-gate (void) error(24); 11437c478bd9Sstevel@tonic-gate *p1++ = c; 11447c478bd9Sstevel@tonic-gate if (c == EOF || (c == ' ' && !Xqt)) 11457c478bd9Sstevel@tonic-gate (void) error(21); 11467c478bd9Sstevel@tonic-gate } while ((c = getchr()) != '\n'); 11477c478bd9Sstevel@tonic-gate *p1++ = 0; 11487c478bd9Sstevel@tonic-gate if (Xqt) 11497c478bd9Sstevel@tonic-gate if (comm == 'f') { 11507c478bd9Sstevel@tonic-gate --Xqt; 11517c478bd9Sstevel@tonic-gate (void) error(57); 11527c478bd9Sstevel@tonic-gate } 11537c478bd9Sstevel@tonic-gate else 11547c478bd9Sstevel@tonic-gate return; 11557c478bd9Sstevel@tonic-gate if (savedfile[0] == 0 || comm == 'e' || comm == 'f') { 11567c478bd9Sstevel@tonic-gate p1 = savedfile; 11577c478bd9Sstevel@tonic-gate p2 = file; 11587c478bd9Sstevel@tonic-gate while (*p1++ = *p2++); 11597c478bd9Sstevel@tonic-gate } 11607c478bd9Sstevel@tonic-gate red(file); 11617c478bd9Sstevel@tonic-gate } 11627c478bd9Sstevel@tonic-gate 11637c478bd9Sstevel@tonic-gate 11647c478bd9Sstevel@tonic-gate static void 11657c478bd9Sstevel@tonic-gate exfile(void) 11667c478bd9Sstevel@tonic-gate { 11677c478bd9Sstevel@tonic-gate #ifdef NULLS 11687c478bd9Sstevel@tonic-gate int c; 11697c478bd9Sstevel@tonic-gate #endif 11707c478bd9Sstevel@tonic-gate 11717c478bd9Sstevel@tonic-gate #ifndef RESEARCH 11727c478bd9Sstevel@tonic-gate if (oldmask) { 11737c478bd9Sstevel@tonic-gate umask(oldmask); 11747c478bd9Sstevel@tonic-gate oldmask = 0; 11757c478bd9Sstevel@tonic-gate } 11767c478bd9Sstevel@tonic-gate #endif 11777c478bd9Sstevel@tonic-gate eclose(io); 11787c478bd9Sstevel@tonic-gate io = -1; 11797c478bd9Sstevel@tonic-gate if (vflag) { 11807c478bd9Sstevel@tonic-gate putd(); 11817c478bd9Sstevel@tonic-gate putchr('\n'); 11827c478bd9Sstevel@tonic-gate #ifdef NULLS 11837c478bd9Sstevel@tonic-gate if (nulls) { 11847c478bd9Sstevel@tonic-gate c = count; 11857c478bd9Sstevel@tonic-gate count = nulls; 11867c478bd9Sstevel@tonic-gate nulls = 0; 11877c478bd9Sstevel@tonic-gate putd(); 11887c478bd9Sstevel@tonic-gate puts(gettext(" nulls replaced by '\\0'")); 11897c478bd9Sstevel@tonic-gate count = c; 11907c478bd9Sstevel@tonic-gate } 11917c478bd9Sstevel@tonic-gate #endif 11927c478bd9Sstevel@tonic-gate } 11937c478bd9Sstevel@tonic-gate } 11947c478bd9Sstevel@tonic-gate 11957c478bd9Sstevel@tonic-gate static void 11967c478bd9Sstevel@tonic-gate onintr(int sig) 11977c478bd9Sstevel@tonic-gate { 11987c478bd9Sstevel@tonic-gate signal(SIGINT, onintr); 11997c478bd9Sstevel@tonic-gate putchr('\n'); 12007c478bd9Sstevel@tonic-gate lastc = '\n'; 12017c478bd9Sstevel@tonic-gate globflg = 0; 12027c478bd9Sstevel@tonic-gate if (funlink) unlink(funny); /* remove tmp file */ 12037c478bd9Sstevel@tonic-gate /* if interrupted a read, only part of file may be in buffer */ 12047c478bd9Sstevel@tonic-gate if (readflg) { 12057c478bd9Sstevel@tonic-gate sprintf(tstring, "\007read may be incomplete - beware!\007"); 12067c478bd9Sstevel@tonic-gate puts(gettext(tstring)); 12077c478bd9Sstevel@tonic-gate fchange = 0; 12087c478bd9Sstevel@tonic-gate } 12097c478bd9Sstevel@tonic-gate (void) error(26); 12107c478bd9Sstevel@tonic-gate } 12117c478bd9Sstevel@tonic-gate 12127c478bd9Sstevel@tonic-gate static void 12137c478bd9Sstevel@tonic-gate onhup(int sig) 12147c478bd9Sstevel@tonic-gate { 12157c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 12167c478bd9Sstevel@tonic-gate signal(SIGHUP, SIG_IGN); 12177c478bd9Sstevel@tonic-gate /* 12187c478bd9Sstevel@tonic-gate * if there are lines in file and file was not written 12197c478bd9Sstevel@tonic-gate * since last update, save in ed.hup, or $HOME/ed.hup 12207c478bd9Sstevel@tonic-gate */ 12217c478bd9Sstevel@tonic-gate if (dol > zero && fchange == 1) { 12227c478bd9Sstevel@tonic-gate addr1 = zero+1; 12237c478bd9Sstevel@tonic-gate addr2 = dol; 12247c478bd9Sstevel@tonic-gate io = creat("ed.hup", 12257c478bd9Sstevel@tonic-gate S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); 12267c478bd9Sstevel@tonic-gate if (io < 0 && home) { 12277c478bd9Sstevel@tonic-gate char *fn; 12287c478bd9Sstevel@tonic-gate 12297c478bd9Sstevel@tonic-gate fn = (char *)calloc(strlen(home) + 8, sizeof (char)); 12307c478bd9Sstevel@tonic-gate if (fn) { 12317c478bd9Sstevel@tonic-gate strcpy(fn, home); 12327c478bd9Sstevel@tonic-gate strcat(fn, "/ed.hup"); 12337c478bd9Sstevel@tonic-gate io = creat(fn, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP 12347c478bd9Sstevel@tonic-gate |S_IROTH|S_IWOTH); 12357c478bd9Sstevel@tonic-gate free(fn); 12367c478bd9Sstevel@tonic-gate } 12377c478bd9Sstevel@tonic-gate } 12387c478bd9Sstevel@tonic-gate if (io > 0) 12397c478bd9Sstevel@tonic-gate putfile(); 12407c478bd9Sstevel@tonic-gate } 12417c478bd9Sstevel@tonic-gate fchange = 0; 12427c478bd9Sstevel@tonic-gate ++errcnt; 12437c478bd9Sstevel@tonic-gate quit(sig); 12447c478bd9Sstevel@tonic-gate } 12457c478bd9Sstevel@tonic-gate 12467c478bd9Sstevel@tonic-gate static int 12477c478bd9Sstevel@tonic-gate error(int code) 12487c478bd9Sstevel@tonic-gate { 12497c478bd9Sstevel@tonic-gate int c; 12507c478bd9Sstevel@tonic-gate 12517c478bd9Sstevel@tonic-gate if (code == 28 && save28 == 0) { 12527c478bd9Sstevel@tonic-gate fchange = 0; 12537c478bd9Sstevel@tonic-gate flag28++; 12547c478bd9Sstevel@tonic-gate } 12557c478bd9Sstevel@tonic-gate readflg = 0; 12567c478bd9Sstevel@tonic-gate ++errcnt; 12577c478bd9Sstevel@tonic-gate listf = listn = 0; 12587c478bd9Sstevel@tonic-gate pflag = 0; 12597c478bd9Sstevel@tonic-gate #ifndef RESEARCH 12607c478bd9Sstevel@tonic-gate if (oldmask) { 12617c478bd9Sstevel@tonic-gate umask(oldmask); 12627c478bd9Sstevel@tonic-gate oldmask = 0; 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate #endif 12657c478bd9Sstevel@tonic-gate #ifdef NULLS /* Not really nulls, but close enough */ 12667c478bd9Sstevel@tonic-gate /* This is a bug because of buffering */ 12677c478bd9Sstevel@tonic-gate if (code == 28) /* illegal char. */ 12687c478bd9Sstevel@tonic-gate putd(); 12697c478bd9Sstevel@tonic-gate #endif 12707c478bd9Sstevel@tonic-gate /* Cant open file or file does not exist */ 12717c478bd9Sstevel@tonic-gate if ((code == 3) || (code == 68)) { 12727c478bd9Sstevel@tonic-gate if (qflg == 0) { 12737c478bd9Sstevel@tonic-gate putchr('?'); 12747c478bd9Sstevel@tonic-gate puts(file); 12757c478bd9Sstevel@tonic-gate } 12767c478bd9Sstevel@tonic-gate else 12777c478bd9Sstevel@tonic-gate qflg = 0; 12787c478bd9Sstevel@tonic-gate } 12797c478bd9Sstevel@tonic-gate else 12807c478bd9Sstevel@tonic-gate { 12817c478bd9Sstevel@tonic-gate putchr('?'); 12827c478bd9Sstevel@tonic-gate putchr('\n'); 12837c478bd9Sstevel@tonic-gate } 12847c478bd9Sstevel@tonic-gate count = 0; 12857c478bd9Sstevel@tonic-gate lseek(0, (long)0, 2); 12867c478bd9Sstevel@tonic-gate if (globp) 12877c478bd9Sstevel@tonic-gate lastc = '\n'; 12887c478bd9Sstevel@tonic-gate globp = 0; 12897c478bd9Sstevel@tonic-gate peekc = lastc; 12907c478bd9Sstevel@tonic-gate if (lastc) 12917c478bd9Sstevel@tonic-gate while ((c = getchr()) != '\n' && c != EOF); 12927c478bd9Sstevel@tonic-gate if (io) { 12937c478bd9Sstevel@tonic-gate eclose(io); 12947c478bd9Sstevel@tonic-gate io = -1; 12957c478bd9Sstevel@tonic-gate } 12967c478bd9Sstevel@tonic-gate xcode = code; 12977c478bd9Sstevel@tonic-gate if (hflag) 12987c478bd9Sstevel@tonic-gate PUTM(); 12997c478bd9Sstevel@tonic-gate if (code == 4) 13007c478bd9Sstevel@tonic-gate return (0); /* Non-fatal error. */ 13017c478bd9Sstevel@tonic-gate longjmp(savej, 1); 13027c478bd9Sstevel@tonic-gate /* NOTREACHED */ 13037c478bd9Sstevel@tonic-gate } 13047c478bd9Sstevel@tonic-gate 13057c478bd9Sstevel@tonic-gate static int 13067c478bd9Sstevel@tonic-gate getchr(void) 13077c478bd9Sstevel@tonic-gate { 13087c478bd9Sstevel@tonic-gate char c; 13097c478bd9Sstevel@tonic-gate if (lastc = peekc) { 13107c478bd9Sstevel@tonic-gate peekc = 0; 13117c478bd9Sstevel@tonic-gate return (lastc); 13127c478bd9Sstevel@tonic-gate } 13137c478bd9Sstevel@tonic-gate if (globp) { 13147c478bd9Sstevel@tonic-gate if ((lastc = (unsigned char)*globp++) != 0) 13157c478bd9Sstevel@tonic-gate return (lastc); 13167c478bd9Sstevel@tonic-gate globp = 0; 13177c478bd9Sstevel@tonic-gate return (EOF); 13187c478bd9Sstevel@tonic-gate } 13197c478bd9Sstevel@tonic-gate if (read(0, &c, 1) <= 0) 13207c478bd9Sstevel@tonic-gate return (lastc = EOF); 13217c478bd9Sstevel@tonic-gate lastc = (unsigned char)c; 13227c478bd9Sstevel@tonic-gate return (lastc); 13237c478bd9Sstevel@tonic-gate } 13247c478bd9Sstevel@tonic-gate 13257c478bd9Sstevel@tonic-gate static int 13267c478bd9Sstevel@tonic-gate gettty(void) 13277c478bd9Sstevel@tonic-gate { 13287c478bd9Sstevel@tonic-gate int c; 13297c478bd9Sstevel@tonic-gate char *gf; 13307c478bd9Sstevel@tonic-gate char *p; 13317c478bd9Sstevel@tonic-gate 13327c478bd9Sstevel@tonic-gate p = linebuf; 13337c478bd9Sstevel@tonic-gate gf = globp; 13347c478bd9Sstevel@tonic-gate while ((c = getchr()) != '\n') { 13357c478bd9Sstevel@tonic-gate if (c == EOF) { 13367c478bd9Sstevel@tonic-gate if (gf) 13377c478bd9Sstevel@tonic-gate peekc = c; 13387c478bd9Sstevel@tonic-gate return (c); 13397c478bd9Sstevel@tonic-gate } 13407c478bd9Sstevel@tonic-gate if (c == 0) 13417c478bd9Sstevel@tonic-gate continue; 13427c478bd9Sstevel@tonic-gate *p++ = c; 13437c478bd9Sstevel@tonic-gate 13447c478bd9Sstevel@tonic-gate if (p > &linebuf[LBSIZE-1]) 13457c478bd9Sstevel@tonic-gate (void) error(27); 13467c478bd9Sstevel@tonic-gate } 13477c478bd9Sstevel@tonic-gate *p++ = 0; 13487c478bd9Sstevel@tonic-gate if (linebuf[0] == '.' && linebuf[1] == 0) 13497c478bd9Sstevel@tonic-gate return (EOF); 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate /* 13527c478bd9Sstevel@tonic-gate * POSIX.2/XPG4 explicitly says no to this: 13537c478bd9Sstevel@tonic-gate * 13547c478bd9Sstevel@tonic-gate * in Solaris backslash followed by special character "." is 13557c478bd9Sstevel@tonic-gate * special character "." itself; (so terminating input mode can be 13567c478bd9Sstevel@tonic-gate * "\.\n"). 13577c478bd9Sstevel@tonic-gate * 13587c478bd9Sstevel@tonic-gate * however, POSIX2/XPG4 says, input mode is terminated by 13597c478bd9Sstevel@tonic-gate * entering line consisting of only 2 characters: ".\n" 13607c478bd9Sstevel@tonic-gate * 13617c478bd9Sstevel@tonic-gate * if (linebuf[0]=='\\' && linebuf[1]=='.' && linebuf[2]==0) { 13627c478bd9Sstevel@tonic-gate * linebuf[0] = '.'; 13637c478bd9Sstevel@tonic-gate * linebuf[1] = 0; 13647c478bd9Sstevel@tonic-gate * } 13657c478bd9Sstevel@tonic-gate */ 13667c478bd9Sstevel@tonic-gate return (0); 13677c478bd9Sstevel@tonic-gate } 13687c478bd9Sstevel@tonic-gate 13697c478bd9Sstevel@tonic-gate static int 13707c478bd9Sstevel@tonic-gate getfile(void) 13717c478bd9Sstevel@tonic-gate { 13727c478bd9Sstevel@tonic-gate char c; 13737c478bd9Sstevel@tonic-gate char *lp, *fp; 13747c478bd9Sstevel@tonic-gate 13757c478bd9Sstevel@tonic-gate lp = linebuf; 13767c478bd9Sstevel@tonic-gate fp = nextip; 13777c478bd9Sstevel@tonic-gate do { 13787c478bd9Sstevel@tonic-gate if (--ninbuf < 0) { 13797c478bd9Sstevel@tonic-gate if ((ninbuf = read(io, genbuf, LBSIZE)-1) < 0) 13807c478bd9Sstevel@tonic-gate if (lp > linebuf) { 13817c478bd9Sstevel@tonic-gate puts(gettext("'\\n' appended")); 13827c478bd9Sstevel@tonic-gate *genbuf = '\n'; 13837c478bd9Sstevel@tonic-gate } 13847c478bd9Sstevel@tonic-gate else 13857c478bd9Sstevel@tonic-gate return (EOF); 13867c478bd9Sstevel@tonic-gate if (crflag == -1) { 13877c478bd9Sstevel@tonic-gate if (isencrypt(genbuf, ninbuf + 1)) 13887c478bd9Sstevel@tonic-gate crflag = 2; 13897c478bd9Sstevel@tonic-gate else 13907c478bd9Sstevel@tonic-gate crflag = -2; 13917c478bd9Sstevel@tonic-gate } 13927c478bd9Sstevel@tonic-gate fp = genbuf; 13937c478bd9Sstevel@tonic-gate if (crflag > 0) 13947c478bd9Sstevel@tonic-gate if (run_crypt(count, genbuf, ninbuf+1, perm) == -1) 13957c478bd9Sstevel@tonic-gate (void) error(63); 13967c478bd9Sstevel@tonic-gate } 13977c478bd9Sstevel@tonic-gate if (lp >= &linebuf[LBSIZE]) { 13987c478bd9Sstevel@tonic-gate lastc = '\n'; 13997c478bd9Sstevel@tonic-gate (void) error(27); 14007c478bd9Sstevel@tonic-gate } 14017c478bd9Sstevel@tonic-gate if ((*lp++ = c = *fp++) == 0) { 14027c478bd9Sstevel@tonic-gate #ifdef NULLS 14037c478bd9Sstevel@tonic-gate lp[-1] = '\\'; 14047c478bd9Sstevel@tonic-gate *lp++ = '0'; 14057c478bd9Sstevel@tonic-gate nulls++; 14067c478bd9Sstevel@tonic-gate #else 14077c478bd9Sstevel@tonic-gate lp--; 14087c478bd9Sstevel@tonic-gate continue; 14097c478bd9Sstevel@tonic-gate #endif 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate count++; 14127c478bd9Sstevel@tonic-gate } while (c != '\n'); 14137c478bd9Sstevel@tonic-gate *--lp = 0; 14147c478bd9Sstevel@tonic-gate nextip = fp; 14157c478bd9Sstevel@tonic-gate if (fss.Ffill && fss.Flim && lenchk(linebuf, &fss) < 0) { 14167c478bd9Sstevel@tonic-gate write(1, gettext("line too long: lno = "), 14177c478bd9Sstevel@tonic-gate strlen(gettext("line too long: lno = "))); 14187c478bd9Sstevel@tonic-gate ccount = count; 14197c478bd9Sstevel@tonic-gate count = (++dot-zero)&077777; 14207c478bd9Sstevel@tonic-gate dot--; 14217c478bd9Sstevel@tonic-gate putd(); 14227c478bd9Sstevel@tonic-gate count = ccount; 14237c478bd9Sstevel@tonic-gate putchr('\n'); 14247c478bd9Sstevel@tonic-gate } 14257c478bd9Sstevel@tonic-gate return (0); 14267c478bd9Sstevel@tonic-gate } 14277c478bd9Sstevel@tonic-gate 14287c478bd9Sstevel@tonic-gate static void 14297c478bd9Sstevel@tonic-gate putfile(void) 14307c478bd9Sstevel@tonic-gate { 14317c478bd9Sstevel@tonic-gate int n; 14327c478bd9Sstevel@tonic-gate LINE a1; 14337c478bd9Sstevel@tonic-gate char *fp, *lp; 14347c478bd9Sstevel@tonic-gate int nib; 14357c478bd9Sstevel@tonic-gate 14367c478bd9Sstevel@tonic-gate nib = LBSIZE; 14377c478bd9Sstevel@tonic-gate fp = genbuf; 14387c478bd9Sstevel@tonic-gate a1 = addr1; 14397c478bd9Sstevel@tonic-gate do { 1440*23a1cceaSRoger A. Faulkner lp = getaline(a1++->cur); 14417c478bd9Sstevel@tonic-gate if (fss.Ffill && fss.Flim && lenchk(linebuf, &fss) < 0) { 14427c478bd9Sstevel@tonic-gate write(1, gettext("line too long: lno = "), 14437c478bd9Sstevel@tonic-gate strlen(gettext("line too long: lno = "))); 14447c478bd9Sstevel@tonic-gate ccount = count; 14457c478bd9Sstevel@tonic-gate count = (a1-zero-1)&077777; 14467c478bd9Sstevel@tonic-gate putd(); 14477c478bd9Sstevel@tonic-gate count = ccount; 14487c478bd9Sstevel@tonic-gate putchr('\n'); 14497c478bd9Sstevel@tonic-gate } 14507c478bd9Sstevel@tonic-gate for (;;) { 14517c478bd9Sstevel@tonic-gate if (--nib < 0) { 14527c478bd9Sstevel@tonic-gate n = fp-genbuf; 14537c478bd9Sstevel@tonic-gate if (kflag) 14547c478bd9Sstevel@tonic-gate if (run_crypt(count-n, genbuf, n, perm) == -1) 14557c478bd9Sstevel@tonic-gate (void) error(63); 14567c478bd9Sstevel@tonic-gate if (write(io, genbuf, n) != n) 14577c478bd9Sstevel@tonic-gate (void) error(29); 14587c478bd9Sstevel@tonic-gate nib = LBSIZE - 1; 14597c478bd9Sstevel@tonic-gate fp = genbuf; 14607c478bd9Sstevel@tonic-gate } 14617c478bd9Sstevel@tonic-gate if (dol->cur == 0L)break; /* Allow write of null file */ 14627c478bd9Sstevel@tonic-gate count++; 14637c478bd9Sstevel@tonic-gate if ((*fp++ = *lp++) == 0) { 14647c478bd9Sstevel@tonic-gate fp[-1] = '\n'; 14657c478bd9Sstevel@tonic-gate break; 14667c478bd9Sstevel@tonic-gate } 14677c478bd9Sstevel@tonic-gate } 14687c478bd9Sstevel@tonic-gate } while (a1 <= addr2); 14697c478bd9Sstevel@tonic-gate n = fp-genbuf; 14707c478bd9Sstevel@tonic-gate if (kflag) 14717c478bd9Sstevel@tonic-gate if (run_crypt(count-n, genbuf, n, perm) == -1) 14727c478bd9Sstevel@tonic-gate (void) error(63); 14737c478bd9Sstevel@tonic-gate if (write(io, genbuf, n) != n) 14747c478bd9Sstevel@tonic-gate (void) error(29); 14757c478bd9Sstevel@tonic-gate } 14767c478bd9Sstevel@tonic-gate 14777c478bd9Sstevel@tonic-gate static void 14787c478bd9Sstevel@tonic-gate append(int (*f)(void), LINE a) 14797c478bd9Sstevel@tonic-gate { 14807c478bd9Sstevel@tonic-gate LINE a1, a2, rdot; 14817c478bd9Sstevel@tonic-gate long tl; 14827c478bd9Sstevel@tonic-gate 14837c478bd9Sstevel@tonic-gate nline = 0; 14847c478bd9Sstevel@tonic-gate dot = a; 14857c478bd9Sstevel@tonic-gate while ((*f)() == 0) { 14867c478bd9Sstevel@tonic-gate if (dol >= endcore) { 14877c478bd9Sstevel@tonic-gate if ((int)sbrk(512 * sizeof (struct lin)) == -1) { 14887c478bd9Sstevel@tonic-gate lastc = '\n'; 14897c478bd9Sstevel@tonic-gate (void) error(30); 14907c478bd9Sstevel@tonic-gate } 14917c478bd9Sstevel@tonic-gate endcore += 512; 14927c478bd9Sstevel@tonic-gate } 14937c478bd9Sstevel@tonic-gate tl = putline(); 14947c478bd9Sstevel@tonic-gate nline++; 14957c478bd9Sstevel@tonic-gate a1 = ++dol; 14967c478bd9Sstevel@tonic-gate a2 = a1+1; 14977c478bd9Sstevel@tonic-gate rdot = ++dot; 14987c478bd9Sstevel@tonic-gate while (a1 > rdot) 14997c478bd9Sstevel@tonic-gate (--a2)->cur = (--a1)->cur; 15007c478bd9Sstevel@tonic-gate rdot->cur = tl; 15017c478bd9Sstevel@tonic-gate } 15027c478bd9Sstevel@tonic-gate } 15037c478bd9Sstevel@tonic-gate 15047c478bd9Sstevel@tonic-gate static void 15057c478bd9Sstevel@tonic-gate unixcom(void) 15067c478bd9Sstevel@tonic-gate { 15077c478bd9Sstevel@tonic-gate void (*savint)(); 15087c478bd9Sstevel@tonic-gate pid_t pid, rpid; 15097c478bd9Sstevel@tonic-gate int retcode; 15107c478bd9Sstevel@tonic-gate static char savcmd[LBSIZE]; /* last command */ 15117c478bd9Sstevel@tonic-gate char curcmd[LBSIZE]; /* current command */ 15127c478bd9Sstevel@tonic-gate char *psavcmd, *pcurcmd, *psavedfile; 15137c478bd9Sstevel@tonic-gate int endflg = 1, shflg = 0; 15147c478bd9Sstevel@tonic-gate wchar_t c; 15157c478bd9Sstevel@tonic-gate int len; 15167c478bd9Sstevel@tonic-gate 15177c478bd9Sstevel@tonic-gate setnoaddr(); 15187c478bd9Sstevel@tonic-gate if (rflg) 15197c478bd9Sstevel@tonic-gate (void) error(6); 15207c478bd9Sstevel@tonic-gate pcurcmd = curcmd; 15217c478bd9Sstevel@tonic-gate /* read command til end */ 15227c478bd9Sstevel@tonic-gate 15237c478bd9Sstevel@tonic-gate /* 15247c478bd9Sstevel@tonic-gate * a '!' found in beginning of command is replaced with the saved 15257c478bd9Sstevel@tonic-gate * command. a '%' found in command is replaced with the current 15267c478bd9Sstevel@tonic-gate * filename 15277c478bd9Sstevel@tonic-gate */ 15287c478bd9Sstevel@tonic-gate 15297c478bd9Sstevel@tonic-gate c = getchr(); 15307c478bd9Sstevel@tonic-gate if (c == '!') { 15317c478bd9Sstevel@tonic-gate if (savcmd[0] == 0) 15327c478bd9Sstevel@tonic-gate (void) error(56); 15337c478bd9Sstevel@tonic-gate else { 15347c478bd9Sstevel@tonic-gate psavcmd = savcmd; 15357c478bd9Sstevel@tonic-gate while (*pcurcmd++ = *psavcmd++); 15367c478bd9Sstevel@tonic-gate --pcurcmd; 15377c478bd9Sstevel@tonic-gate shflg = 1; 15387c478bd9Sstevel@tonic-gate } 15397c478bd9Sstevel@tonic-gate } else 15407c478bd9Sstevel@tonic-gate UNGETC(c); /* put c back */ 15417c478bd9Sstevel@tonic-gate while (endflg == 1) { 15427c478bd9Sstevel@tonic-gate while ((c = get_wchr()) != '\n' && c != '%' && c != '\\') { 15437c478bd9Sstevel@tonic-gate if ((len = wctomb(pcurcmd, c)) <= 0) { 15447c478bd9Sstevel@tonic-gate *pcurcmd = (unsigned char)c; 15457c478bd9Sstevel@tonic-gate len = 1; 15467c478bd9Sstevel@tonic-gate } 15477c478bd9Sstevel@tonic-gate pcurcmd += len; 15487c478bd9Sstevel@tonic-gate } 15497c478bd9Sstevel@tonic-gate 15507c478bd9Sstevel@tonic-gate if (c == '%') { 15517c478bd9Sstevel@tonic-gate if (savedfile[0] == 0) 15527c478bd9Sstevel@tonic-gate (void) error(21); 15537c478bd9Sstevel@tonic-gate else { 15547c478bd9Sstevel@tonic-gate psavedfile = savedfile; 15557c478bd9Sstevel@tonic-gate while (pcurcmd < curcmd + LBSIZE && 15567c478bd9Sstevel@tonic-gate (*pcurcmd++ = *psavedfile++)); 15577c478bd9Sstevel@tonic-gate --pcurcmd; 15587c478bd9Sstevel@tonic-gate shflg = 1; 15597c478bd9Sstevel@tonic-gate } 15607c478bd9Sstevel@tonic-gate } else if (c == '\\') { 15617c478bd9Sstevel@tonic-gate c = get_wchr(); 15627c478bd9Sstevel@tonic-gate if (c != '%') 15637c478bd9Sstevel@tonic-gate *pcurcmd++ = '\\'; 15647c478bd9Sstevel@tonic-gate if ((len = wctomb(pcurcmd, c)) <= 0) { 15657c478bd9Sstevel@tonic-gate *pcurcmd = (unsigned char)c; 15667c478bd9Sstevel@tonic-gate len = 1; 15677c478bd9Sstevel@tonic-gate } 15687c478bd9Sstevel@tonic-gate pcurcmd += len; 15697c478bd9Sstevel@tonic-gate } 15707c478bd9Sstevel@tonic-gate else 15717c478bd9Sstevel@tonic-gate /* end of command hit */ 15727c478bd9Sstevel@tonic-gate endflg = 0; 15737c478bd9Sstevel@tonic-gate } 15747c478bd9Sstevel@tonic-gate *pcurcmd++ = 0; 15757c478bd9Sstevel@tonic-gate if (shflg == 1) 15767c478bd9Sstevel@tonic-gate puts(curcmd); 15777c478bd9Sstevel@tonic-gate /* save command */ 15787c478bd9Sstevel@tonic-gate strcpy(savcmd, curcmd); 15797c478bd9Sstevel@tonic-gate 15807c478bd9Sstevel@tonic-gate if ((pid = fork()) == 0) { 15817c478bd9Sstevel@tonic-gate signal(SIGHUP, oldhup); 15827c478bd9Sstevel@tonic-gate signal(SIGQUIT, oldquit); 15837c478bd9Sstevel@tonic-gate close(tfile); 15847c478bd9Sstevel@tonic-gate if (__xpg4 == 0) { /* not XPG4 */ 15857c478bd9Sstevel@tonic-gate shpath = "/usr/bin/sh"; 15867c478bd9Sstevel@tonic-gate } else { 15877c478bd9Sstevel@tonic-gate /* XPG4 */ 15887c478bd9Sstevel@tonic-gate shpath = "/usr/xpg4/bin/sh"; 15897c478bd9Sstevel@tonic-gate } 15907c478bd9Sstevel@tonic-gate execlp((const char *)shpath, "sh", "-c", curcmd, (char *)0); 15917c478bd9Sstevel@tonic-gate exit(0100); 15927c478bd9Sstevel@tonic-gate } 15937c478bd9Sstevel@tonic-gate savint = signal(SIGINT, SIG_IGN); 15947c478bd9Sstevel@tonic-gate while ((rpid = wait(&retcode)) != pid && rpid != (pid_t)-1); 15957c478bd9Sstevel@tonic-gate signal(SIGINT, savint); 15967c478bd9Sstevel@tonic-gate if (vflag) puts("!"); 15977c478bd9Sstevel@tonic-gate } 15987c478bd9Sstevel@tonic-gate 15997c478bd9Sstevel@tonic-gate static void 16007c478bd9Sstevel@tonic-gate quit(int sig) 16017c478bd9Sstevel@tonic-gate { 16027c478bd9Sstevel@tonic-gate if (vflag && fchange) { 16037c478bd9Sstevel@tonic-gate fchange = 0; 16047c478bd9Sstevel@tonic-gate if (flag28) { 16057c478bd9Sstevel@tonic-gate flag28 = 0; 16067c478bd9Sstevel@tonic-gate (void) error(62); 16077c478bd9Sstevel@tonic-gate } 16087c478bd9Sstevel@tonic-gate 16097c478bd9Sstevel@tonic-gate /* 16107c478bd9Sstevel@tonic-gate * For case where user reads in BOTH a good 16117c478bd9Sstevel@tonic-gate * file & a bad file 16127c478bd9Sstevel@tonic-gate */ 16137c478bd9Sstevel@tonic-gate (void) error(1); 16147c478bd9Sstevel@tonic-gate } 16157c478bd9Sstevel@tonic-gate unlink(tfname); 16167c478bd9Sstevel@tonic-gate if (kflag) 16177c478bd9Sstevel@tonic-gate crypt_close(perm); 16187c478bd9Sstevel@tonic-gate if (xtflag) 16197c478bd9Sstevel@tonic-gate crypt_close(tperm); 16207c478bd9Sstevel@tonic-gate exit(errcnt? 2: 0); 16217c478bd9Sstevel@tonic-gate } 16227c478bd9Sstevel@tonic-gate 16237c478bd9Sstevel@tonic-gate static void 16247c478bd9Sstevel@tonic-gate delete(void) 16257c478bd9Sstevel@tonic-gate { 16267c478bd9Sstevel@tonic-gate setdot(); 16277c478bd9Sstevel@tonic-gate newline(); 16287c478bd9Sstevel@tonic-gate nonzero(); 16297c478bd9Sstevel@tonic-gate if (!globflg) save(); 16307c478bd9Sstevel@tonic-gate rdelete(addr1, addr2); 16317c478bd9Sstevel@tonic-gate } 16327c478bd9Sstevel@tonic-gate 16337c478bd9Sstevel@tonic-gate static void 16347c478bd9Sstevel@tonic-gate rdelete(LINE ad1, LINE ad2) 16357c478bd9Sstevel@tonic-gate { 16367c478bd9Sstevel@tonic-gate LINE a1, a2, a3; 16377c478bd9Sstevel@tonic-gate 16387c478bd9Sstevel@tonic-gate a1 = ad1; 16397c478bd9Sstevel@tonic-gate a2 = ad2+1; 16407c478bd9Sstevel@tonic-gate a3 = dol; 16417c478bd9Sstevel@tonic-gate dol -= a2 - a1; 16427c478bd9Sstevel@tonic-gate do 16437c478bd9Sstevel@tonic-gate (a1++)->cur = (a2++)->cur; 16447c478bd9Sstevel@tonic-gate while (a2 <= a3); 16457c478bd9Sstevel@tonic-gate a1 = ad1; 16467c478bd9Sstevel@tonic-gate if (a1 > dol) 16477c478bd9Sstevel@tonic-gate a1 = dol; 16487c478bd9Sstevel@tonic-gate dot = a1; 16497c478bd9Sstevel@tonic-gate fchange = 1; 16507c478bd9Sstevel@tonic-gate } 16517c478bd9Sstevel@tonic-gate 16527c478bd9Sstevel@tonic-gate static void 16537c478bd9Sstevel@tonic-gate gdelete(void) 16547c478bd9Sstevel@tonic-gate { 16557c478bd9Sstevel@tonic-gate LINE a1, a2, a3; 16567c478bd9Sstevel@tonic-gate 16577c478bd9Sstevel@tonic-gate a3 = dol; 16587c478bd9Sstevel@tonic-gate for (a1 = zero+1; (a1->cur&01) == 0; a1++) 16597c478bd9Sstevel@tonic-gate if (a1 >= a3) 16607c478bd9Sstevel@tonic-gate return; 16617c478bd9Sstevel@tonic-gate for (a2 = a1 + 1; a2 <= a3; ) { 16627c478bd9Sstevel@tonic-gate if (a2->cur & 01) { 16637c478bd9Sstevel@tonic-gate a2++; 16647c478bd9Sstevel@tonic-gate dot = a1; 16657c478bd9Sstevel@tonic-gate } else 16667c478bd9Sstevel@tonic-gate (a1++)->cur = (a2++)->cur; 16677c478bd9Sstevel@tonic-gate } 16687c478bd9Sstevel@tonic-gate dol = a1-1; 16697c478bd9Sstevel@tonic-gate if (dot > dol) 16707c478bd9Sstevel@tonic-gate dot = dol; 16717c478bd9Sstevel@tonic-gate fchange = 1; 16727c478bd9Sstevel@tonic-gate } 16737c478bd9Sstevel@tonic-gate 16747c478bd9Sstevel@tonic-gate static char * 1675*23a1cceaSRoger A. Faulkner getaline(long tl) 16767c478bd9Sstevel@tonic-gate { 16777c478bd9Sstevel@tonic-gate char *bp, *lp; 16787c478bd9Sstevel@tonic-gate int nl; 16797c478bd9Sstevel@tonic-gate 16807c478bd9Sstevel@tonic-gate lp = linebuf; 16817c478bd9Sstevel@tonic-gate bp = getblock(tl, READ); 16827c478bd9Sstevel@tonic-gate nl = nleft; 16837c478bd9Sstevel@tonic-gate tl &= ~0377; 16847c478bd9Sstevel@tonic-gate while (*lp++ = *bp++) 16857c478bd9Sstevel@tonic-gate if (--nl == 0) { 16867c478bd9Sstevel@tonic-gate bp = getblock(tl += 0400, READ); 16877c478bd9Sstevel@tonic-gate nl = nleft; 16887c478bd9Sstevel@tonic-gate } 16897c478bd9Sstevel@tonic-gate return (linebuf); 16907c478bd9Sstevel@tonic-gate } 16917c478bd9Sstevel@tonic-gate 16927c478bd9Sstevel@tonic-gate static long 16937c478bd9Sstevel@tonic-gate putline(void) 16947c478bd9Sstevel@tonic-gate { 16957c478bd9Sstevel@tonic-gate char *bp, *lp; 16967c478bd9Sstevel@tonic-gate int nl; 16977c478bd9Sstevel@tonic-gate long tl; 16987c478bd9Sstevel@tonic-gate 16997c478bd9Sstevel@tonic-gate fchange = 1; 17007c478bd9Sstevel@tonic-gate lp = linebuf; 17017c478bd9Sstevel@tonic-gate tl = tline; 17027c478bd9Sstevel@tonic-gate bp = getblock(tl, WRITE); 17037c478bd9Sstevel@tonic-gate nl = nleft; 17047c478bd9Sstevel@tonic-gate tl &= ~0377; 17057c478bd9Sstevel@tonic-gate while (*bp = *lp++) { 17067c478bd9Sstevel@tonic-gate if (*bp++ == '\n') { 17077c478bd9Sstevel@tonic-gate *--bp = 0; 17087c478bd9Sstevel@tonic-gate linebp = lp; 17097c478bd9Sstevel@tonic-gate break; 17107c478bd9Sstevel@tonic-gate } 17117c478bd9Sstevel@tonic-gate if (--nl == 0) { 17127c478bd9Sstevel@tonic-gate bp = getblock(tl += 0400, WRITE); 17137c478bd9Sstevel@tonic-gate nl = nleft; 17147c478bd9Sstevel@tonic-gate } 17157c478bd9Sstevel@tonic-gate } 17167c478bd9Sstevel@tonic-gate nl = tline; 17177c478bd9Sstevel@tonic-gate tline += (((lp-linebuf)+03)>>1)&077776; 17187c478bd9Sstevel@tonic-gate return (nl); 17197c478bd9Sstevel@tonic-gate } 17207c478bd9Sstevel@tonic-gate 17217c478bd9Sstevel@tonic-gate static char * 17227c478bd9Sstevel@tonic-gate getblock(long atl, long iof) 17237c478bd9Sstevel@tonic-gate { 17247c478bd9Sstevel@tonic-gate int bno, off; 17257c478bd9Sstevel@tonic-gate char *p1, *p2; 17267c478bd9Sstevel@tonic-gate int n; 17277c478bd9Sstevel@tonic-gate 17287c478bd9Sstevel@tonic-gate bno = atl >> 8; 17297c478bd9Sstevel@tonic-gate off = (atl<<1)&0774; 17307c478bd9Sstevel@tonic-gate 17317c478bd9Sstevel@tonic-gate /* bno is limited to 16 bits */ 17327c478bd9Sstevel@tonic-gate if (bno >= 65535) { 17337c478bd9Sstevel@tonic-gate lastc = '\n'; 17347c478bd9Sstevel@tonic-gate (void) error(31); 17357c478bd9Sstevel@tonic-gate } 17367c478bd9Sstevel@tonic-gate nleft = 512 - off; 17377c478bd9Sstevel@tonic-gate if (bno == iblock) { 17387c478bd9Sstevel@tonic-gate ichanged |= iof; 17397c478bd9Sstevel@tonic-gate return (ibuff+off); 17407c478bd9Sstevel@tonic-gate } 17417c478bd9Sstevel@tonic-gate if (bno == oblock) 17427c478bd9Sstevel@tonic-gate return (obuff+off); 17437c478bd9Sstevel@tonic-gate if (iof == READ) { 17447c478bd9Sstevel@tonic-gate if (ichanged) { 17457c478bd9Sstevel@tonic-gate if (xtflag) 17467c478bd9Sstevel@tonic-gate if (run_crypt(0L, ibuff, 512, tperm) == -1) 17477c478bd9Sstevel@tonic-gate (void) error(63); 17487c478bd9Sstevel@tonic-gate blkio(iblock, ibuff, write); 17497c478bd9Sstevel@tonic-gate } 17507c478bd9Sstevel@tonic-gate ichanged = 0; 17517c478bd9Sstevel@tonic-gate iblock = bno; 17527c478bd9Sstevel@tonic-gate blkio(bno, ibuff, read); 17537c478bd9Sstevel@tonic-gate if (xtflag) 17547c478bd9Sstevel@tonic-gate if (run_crypt(0L, ibuff, 512, tperm) == -1) 17557c478bd9Sstevel@tonic-gate (void) error(63); 17567c478bd9Sstevel@tonic-gate return (ibuff+off); 17577c478bd9Sstevel@tonic-gate } 17587c478bd9Sstevel@tonic-gate if (oblock >= 0) { 17597c478bd9Sstevel@tonic-gate if (xtflag) { 17607c478bd9Sstevel@tonic-gate p1 = obuff; 17617c478bd9Sstevel@tonic-gate p2 = crbuf; 17627c478bd9Sstevel@tonic-gate n = 512; 17637c478bd9Sstevel@tonic-gate while (n--) 17647c478bd9Sstevel@tonic-gate *p2++ = *p1++; 17657c478bd9Sstevel@tonic-gate if (run_crypt(0L, crbuf, 512, tperm) == -1) 17667c478bd9Sstevel@tonic-gate (void) error(63); 17677c478bd9Sstevel@tonic-gate blkio(oblock, crbuf, write); 17687c478bd9Sstevel@tonic-gate } else 17697c478bd9Sstevel@tonic-gate blkio(oblock, obuff, write); 17707c478bd9Sstevel@tonic-gate } 17717c478bd9Sstevel@tonic-gate oblock = bno; 17727c478bd9Sstevel@tonic-gate return (obuff+off); 17737c478bd9Sstevel@tonic-gate } 17747c478bd9Sstevel@tonic-gate 17757c478bd9Sstevel@tonic-gate static void 17767c478bd9Sstevel@tonic-gate blkio(int b, char *buf, ssize_t (*iofcn)()) 17777c478bd9Sstevel@tonic-gate { 17787c478bd9Sstevel@tonic-gate lseek(tfile, (long)b<<9, 0); 17797c478bd9Sstevel@tonic-gate if ((*iofcn)(tfile, buf, 512) != 512) { 17807c478bd9Sstevel@tonic-gate if (dol != zero) 17817c478bd9Sstevel@tonic-gate (void) error(32); /* Bypass this if writing null file */ 17827c478bd9Sstevel@tonic-gate } 17837c478bd9Sstevel@tonic-gate } 17847c478bd9Sstevel@tonic-gate 17857c478bd9Sstevel@tonic-gate static void 17867c478bd9Sstevel@tonic-gate init(void) 17877c478bd9Sstevel@tonic-gate { 17887c478bd9Sstevel@tonic-gate long *markp; 17897c478bd9Sstevel@tonic-gate mode_t omask; 17907c478bd9Sstevel@tonic-gate 17917c478bd9Sstevel@tonic-gate if (tfile != -1) { 17927c478bd9Sstevel@tonic-gate (void) close(tfile); 17937c478bd9Sstevel@tonic-gate (void) unlink(tfname); 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate 17967c478bd9Sstevel@tonic-gate tline = 2; 17977c478bd9Sstevel@tonic-gate for (markp = names; markp < &names[26]; ) 17987c478bd9Sstevel@tonic-gate *markp++ = 0L; 17997c478bd9Sstevel@tonic-gate subnewa = 0L; 18007c478bd9Sstevel@tonic-gate anymarks = 0; 18017c478bd9Sstevel@tonic-gate iblock = -1; 18027c478bd9Sstevel@tonic-gate oblock = -1; 18037c478bd9Sstevel@tonic-gate ichanged = 0; 18047c478bd9Sstevel@tonic-gate initflg = 1; 18057c478bd9Sstevel@tonic-gate omask = umask(0); 18067c478bd9Sstevel@tonic-gate 18077c478bd9Sstevel@tonic-gate if ((tfile = open(tfname, O_CREAT|O_EXCL|O_RDWR, 18087c478bd9Sstevel@tonic-gate S_IRUSR|S_IWUSR)) < 0) { 18097c478bd9Sstevel@tonic-gate puts(gettext(msgtab[70])); 18107c478bd9Sstevel@tonic-gate exit(2); 18117c478bd9Sstevel@tonic-gate } 18127c478bd9Sstevel@tonic-gate 18137c478bd9Sstevel@tonic-gate umask(omask); 18147c478bd9Sstevel@tonic-gate if (xflag) { 18157c478bd9Sstevel@tonic-gate xtflag = 1; 18167c478bd9Sstevel@tonic-gate if (tpermflag) 18177c478bd9Sstevel@tonic-gate (void) crypt_close(tperm); 18187c478bd9Sstevel@tonic-gate tpermflag = 1; 18197c478bd9Sstevel@tonic-gate if (makekey(tperm)) { 18207c478bd9Sstevel@tonic-gate xtflag = 0; 18217c478bd9Sstevel@tonic-gate puts(gettext(msgtab[65])); 18227c478bd9Sstevel@tonic-gate } 18237c478bd9Sstevel@tonic-gate } 18247c478bd9Sstevel@tonic-gate brk((char *)fendcore); 18257c478bd9Sstevel@tonic-gate dot = zero = dol = savdot = savdol = fendcore; 18267c478bd9Sstevel@tonic-gate flag28 = save28 = 0; 18277c478bd9Sstevel@tonic-gate endcore = fendcore - sizeof (struct lin); 18287c478bd9Sstevel@tonic-gate } 18297c478bd9Sstevel@tonic-gate 18307c478bd9Sstevel@tonic-gate static void 18317c478bd9Sstevel@tonic-gate global(int k) 18327c478bd9Sstevel@tonic-gate { 18337c478bd9Sstevel@tonic-gate char *gp; 18347c478bd9Sstevel@tonic-gate wchar_t l; 18357c478bd9Sstevel@tonic-gate char multic[MB_LEN_MAX]; 18367c478bd9Sstevel@tonic-gate wchar_t c; 18377c478bd9Sstevel@tonic-gate LINE a1; 18387c478bd9Sstevel@tonic-gate char globuf[LBSIZE]; 18397c478bd9Sstevel@tonic-gate int n; 18407c478bd9Sstevel@tonic-gate int len; 18417c478bd9Sstevel@tonic-gate 18427c478bd9Sstevel@tonic-gate if (globp) 18437c478bd9Sstevel@tonic-gate (void) error(33); 18447c478bd9Sstevel@tonic-gate setall(); 18457c478bd9Sstevel@tonic-gate nonzero(); 18467c478bd9Sstevel@tonic-gate if ((n = _mbftowc(multic, &l, getchr, &peekc)) <= 0) 18477c478bd9Sstevel@tonic-gate (void) error(67); 18487c478bd9Sstevel@tonic-gate if (l == '\n') 18497c478bd9Sstevel@tonic-gate (void) error(19); 18507c478bd9Sstevel@tonic-gate save(); 18517c478bd9Sstevel@tonic-gate comple(l); 18527c478bd9Sstevel@tonic-gate gp = globuf; 18537c478bd9Sstevel@tonic-gate while ((c = get_wchr()) != '\n') { 18547c478bd9Sstevel@tonic-gate if (c == EOF) 18557c478bd9Sstevel@tonic-gate (void) error(19); 18567c478bd9Sstevel@tonic-gate 18577c478bd9Sstevel@tonic-gate /* '\\' has special meaning only if preceding a '\n' */ 18587c478bd9Sstevel@tonic-gate if (c == '\\') { 18597c478bd9Sstevel@tonic-gate c = get_wchr(); 18607c478bd9Sstevel@tonic-gate if (c != '\n') 18617c478bd9Sstevel@tonic-gate *gp++ = '\\'; 18627c478bd9Sstevel@tonic-gate } 18637c478bd9Sstevel@tonic-gate if ((gp + (unsigned int)MB_CUR_MAX) >= &globuf[LBSIZE-1]) 18647c478bd9Sstevel@tonic-gate (void) error(34); 18657c478bd9Sstevel@tonic-gate if ((len = wctomb(gp, c)) <= 0) { 18667c478bd9Sstevel@tonic-gate *gp = (unsigned char)c; 18677c478bd9Sstevel@tonic-gate len = 1; 18687c478bd9Sstevel@tonic-gate } 18697c478bd9Sstevel@tonic-gate gp += len; 18707c478bd9Sstevel@tonic-gate } 18717c478bd9Sstevel@tonic-gate if (gp == globuf) 18727c478bd9Sstevel@tonic-gate *gp++ = 'p'; 18737c478bd9Sstevel@tonic-gate *gp++ = '\n'; 18747c478bd9Sstevel@tonic-gate *gp++ = 0; 18757c478bd9Sstevel@tonic-gate for (a1 = zero; a1 <= dol; a1++) { 18767c478bd9Sstevel@tonic-gate a1->cur &= ~01; 18777c478bd9Sstevel@tonic-gate if (a1 >= addr1 && a1 <= addr2 && execute(0, a1) == k) 18787c478bd9Sstevel@tonic-gate a1->cur |= 01; 18797c478bd9Sstevel@tonic-gate } 18807c478bd9Sstevel@tonic-gate /* 18817c478bd9Sstevel@tonic-gate * Special case: g/.../d (avoid n^2 algorithm) 18827c478bd9Sstevel@tonic-gate */ 18837c478bd9Sstevel@tonic-gate if (globuf[0] == 'd' && globuf[1] == '\n' && globuf[2] == '\0') { 18847c478bd9Sstevel@tonic-gate gdelete(); 18857c478bd9Sstevel@tonic-gate return; 18867c478bd9Sstevel@tonic-gate } 18877c478bd9Sstevel@tonic-gate for (a1 = zero; a1 <= dol; a1++) { 18887c478bd9Sstevel@tonic-gate if (a1->cur & 01) { 18897c478bd9Sstevel@tonic-gate a1->cur &= ~01; 18907c478bd9Sstevel@tonic-gate dot = a1; 18917c478bd9Sstevel@tonic-gate globp = globuf; 18927c478bd9Sstevel@tonic-gate globflg = 1; 18937c478bd9Sstevel@tonic-gate commands(); 18947c478bd9Sstevel@tonic-gate globflg = 0; 18957c478bd9Sstevel@tonic-gate a1 = zero; 18967c478bd9Sstevel@tonic-gate } 18977c478bd9Sstevel@tonic-gate } 18987c478bd9Sstevel@tonic-gate } 18997c478bd9Sstevel@tonic-gate 19007c478bd9Sstevel@tonic-gate static void 19017c478bd9Sstevel@tonic-gate join(void) 19027c478bd9Sstevel@tonic-gate { 19037c478bd9Sstevel@tonic-gate char *gp, *lp; 19047c478bd9Sstevel@tonic-gate LINE a1; 19057c478bd9Sstevel@tonic-gate 19067c478bd9Sstevel@tonic-gate if (addr1 == addr2) 19077c478bd9Sstevel@tonic-gate return; 19087c478bd9Sstevel@tonic-gate gp = genbuf; 19097c478bd9Sstevel@tonic-gate for (a1 = addr1; a1 <= addr2; a1++) { 1910*23a1cceaSRoger A. Faulkner lp = getaline(a1->cur); 19117c478bd9Sstevel@tonic-gate while (*gp = *lp++) 19127c478bd9Sstevel@tonic-gate if (gp++ > &genbuf[LBSIZE-1]) 19137c478bd9Sstevel@tonic-gate (void) error(27); 19147c478bd9Sstevel@tonic-gate } 19157c478bd9Sstevel@tonic-gate lp = linebuf; 19167c478bd9Sstevel@tonic-gate gp = genbuf; 19177c478bd9Sstevel@tonic-gate while (*lp++ = *gp++); 19187c478bd9Sstevel@tonic-gate addr1->cur = putline(); 19197c478bd9Sstevel@tonic-gate if (addr1 < addr2) 19207c478bd9Sstevel@tonic-gate rdelete(addr1+1, addr2); 19217c478bd9Sstevel@tonic-gate dot = addr1; 19227c478bd9Sstevel@tonic-gate } 19237c478bd9Sstevel@tonic-gate 19247c478bd9Sstevel@tonic-gate static void 19257c478bd9Sstevel@tonic-gate substitute(int inglob) 19267c478bd9Sstevel@tonic-gate { 19277c478bd9Sstevel@tonic-gate int nl; 19287c478bd9Sstevel@tonic-gate LINE a1; 19297c478bd9Sstevel@tonic-gate long *markp; 19307c478bd9Sstevel@tonic-gate int ingsav; /* For saving arg. */ 19317c478bd9Sstevel@tonic-gate 19327c478bd9Sstevel@tonic-gate ingsav = inglob; 19337c478bd9Sstevel@tonic-gate ocerr2 = 0; 19347c478bd9Sstevel@tonic-gate gsubf = compsub(); 19357c478bd9Sstevel@tonic-gate for (a1 = addr1; a1 <= addr2; a1++) { 19367c478bd9Sstevel@tonic-gate if (execute(0, a1) == 0) 19377c478bd9Sstevel@tonic-gate continue; 19387c478bd9Sstevel@tonic-gate numpass = 0; 19397c478bd9Sstevel@tonic-gate ocerr1 = 0; 19407c478bd9Sstevel@tonic-gate inglob |= 01; 19417c478bd9Sstevel@tonic-gate dosub(); 19427c478bd9Sstevel@tonic-gate if (gsubf) { 19437c478bd9Sstevel@tonic-gate while (*loc2) { 19447c478bd9Sstevel@tonic-gate if (execute(1, (LINE)0) == 0) 19457c478bd9Sstevel@tonic-gate break; 19467c478bd9Sstevel@tonic-gate dosub(); 19477c478bd9Sstevel@tonic-gate } 19487c478bd9Sstevel@tonic-gate } 19497c478bd9Sstevel@tonic-gate if (ocerr1 == 0)continue; /* Don't put out-not changed. */ 19507c478bd9Sstevel@tonic-gate subnewa = putline(); 19517c478bd9Sstevel@tonic-gate a1->cur &= ~01; 19527c478bd9Sstevel@tonic-gate if (anymarks) { 19537c478bd9Sstevel@tonic-gate for (markp = names; markp < &names[26]; markp++) 19547c478bd9Sstevel@tonic-gate if (*markp == a1->cur) 19557c478bd9Sstevel@tonic-gate *markp = subnewa; 19567c478bd9Sstevel@tonic-gate } 19577c478bd9Sstevel@tonic-gate a1->cur = subnewa; 19587c478bd9Sstevel@tonic-gate append(getsub, a1); 19597c478bd9Sstevel@tonic-gate nl = nline; 19607c478bd9Sstevel@tonic-gate a1 += nl; 19617c478bd9Sstevel@tonic-gate addr2 += nl; 19627c478bd9Sstevel@tonic-gate } 19637c478bd9Sstevel@tonic-gate if (ingsav) 19647c478bd9Sstevel@tonic-gate return; /* Was in global-no error msg allowed. */ 19657c478bd9Sstevel@tonic-gate if (inglob == 0) 19667c478bd9Sstevel@tonic-gate (void) error(35); /* Not in global, but not found. */ 19677c478bd9Sstevel@tonic-gate if (ocerr2 == 0) 19687c478bd9Sstevel@tonic-gate (void) error(35); /* RE found, but occurrence match failed. */ 19697c478bd9Sstevel@tonic-gate } 19707c478bd9Sstevel@tonic-gate 19717c478bd9Sstevel@tonic-gate static int 19727c478bd9Sstevel@tonic-gate compsub(void) 19737c478bd9Sstevel@tonic-gate { 19747c478bd9Sstevel@tonic-gate int c; 19757c478bd9Sstevel@tonic-gate wchar_t seof; 19767c478bd9Sstevel@tonic-gate char *p; 19777c478bd9Sstevel@tonic-gate char multic[MB_LEN_MAX]; 19787c478bd9Sstevel@tonic-gate int n; 19797c478bd9Sstevel@tonic-gate static char remem[RHSIZE]; 19807c478bd9Sstevel@tonic-gate static int remflg = -1; 19817c478bd9Sstevel@tonic-gate int i; 19827c478bd9Sstevel@tonic-gate 19837c478bd9Sstevel@tonic-gate if ((n = _mbftowc(multic, &seof, getchr, &peekc)) <= 0) 19847c478bd9Sstevel@tonic-gate (void) error(67); 19857c478bd9Sstevel@tonic-gate if (seof == '\n' || seof == ' ') 19867c478bd9Sstevel@tonic-gate (void) error(36); 19877c478bd9Sstevel@tonic-gate comple(seof); 19887c478bd9Sstevel@tonic-gate p = rhsbuf; 19897c478bd9Sstevel@tonic-gate for (;;) { 19907c478bd9Sstevel@tonic-gate wchar_t cl; 19917c478bd9Sstevel@tonic-gate if ((n = _mbftowc(multic, &cl, getchr, &peekc)) <= 0) 19927c478bd9Sstevel@tonic-gate (void) error(67); 19937c478bd9Sstevel@tonic-gate if (cl == '\\') { 19947c478bd9Sstevel@tonic-gate *p++ = '\\'; 19957c478bd9Sstevel@tonic-gate if (p >= &rhsbuf[RHSIZE]) 19967c478bd9Sstevel@tonic-gate (void) error(38); 19977c478bd9Sstevel@tonic-gate if ((n = _mbftowc(multic, &cl, getchr, &peekc)) <= 0) 19987c478bd9Sstevel@tonic-gate (void) error(67); 19997c478bd9Sstevel@tonic-gate } else if (cl == '\n') { 20007c478bd9Sstevel@tonic-gate if (nodelim == 1) { 20017c478bd9Sstevel@tonic-gate nodelim = 0; 20027c478bd9Sstevel@tonic-gate (void) error(36); 20037c478bd9Sstevel@tonic-gate } 20047c478bd9Sstevel@tonic-gate if (!(globp && globp[0])) { 20057c478bd9Sstevel@tonic-gate UNGETC('\n'); 20067c478bd9Sstevel@tonic-gate pflag++; 20077c478bd9Sstevel@tonic-gate break; 20087c478bd9Sstevel@tonic-gate } 20097c478bd9Sstevel@tonic-gate } else if (cl == seof) 20107c478bd9Sstevel@tonic-gate break; 20117c478bd9Sstevel@tonic-gate if (p + n > &rhsbuf[RHSIZE]) 20127c478bd9Sstevel@tonic-gate (void) error(38); 20137c478bd9Sstevel@tonic-gate (void) strncpy(p, multic, n); 20147c478bd9Sstevel@tonic-gate p += n; 20157c478bd9Sstevel@tonic-gate } 20167c478bd9Sstevel@tonic-gate *p++ = 0; 20177c478bd9Sstevel@tonic-gate if (rhsbuf[0] == '%' && rhsbuf[1] == 0) 20187c478bd9Sstevel@tonic-gate /* 20197c478bd9Sstevel@tonic-gate * If there isn't a remembered string, it is an error; 20207c478bd9Sstevel@tonic-gate * otherwise the right hand side is the previous right 20217c478bd9Sstevel@tonic-gate * hand side. 20227c478bd9Sstevel@tonic-gate */ 20237c478bd9Sstevel@tonic-gate 20247c478bd9Sstevel@tonic-gate if (remflg == -1) 20257c478bd9Sstevel@tonic-gate (void) error(55); 20267c478bd9Sstevel@tonic-gate else 20277c478bd9Sstevel@tonic-gate strcpy(rhsbuf, remem); 20287c478bd9Sstevel@tonic-gate else { 20297c478bd9Sstevel@tonic-gate strcpy(remem, rhsbuf); 20307c478bd9Sstevel@tonic-gate remflg = 0; 20317c478bd9Sstevel@tonic-gate } 20327c478bd9Sstevel@tonic-gate c = 0; 20337c478bd9Sstevel@tonic-gate peekc = getchr(); /* Gets char after third delimiter. */ 20347c478bd9Sstevel@tonic-gate if (peekc == 'g') { 20357c478bd9Sstevel@tonic-gate c = LBSIZE; peekc = 0; 20367c478bd9Sstevel@tonic-gate } 20377c478bd9Sstevel@tonic-gate if (peekc >= '1' && peekc <= '9') { 20387c478bd9Sstevel@tonic-gate c = peekc-'0'; 20397c478bd9Sstevel@tonic-gate peekc = 0; /* Allows getchr() to get next char. */ 20407c478bd9Sstevel@tonic-gate while (1) { 20417c478bd9Sstevel@tonic-gate i = getchr(); 20427c478bd9Sstevel@tonic-gate if (i < '0' || i > '9') 20437c478bd9Sstevel@tonic-gate break; 20447c478bd9Sstevel@tonic-gate c = c*10 + i-'0'; 20457c478bd9Sstevel@tonic-gate if (c > LBSIZE-1) 20467c478bd9Sstevel@tonic-gate (void) error(20); /* "Illegal suffix" */ 20477c478bd9Sstevel@tonic-gate } 20487c478bd9Sstevel@tonic-gate peekc = i; /* Effectively an unget. */ 20497c478bd9Sstevel@tonic-gate } 20507c478bd9Sstevel@tonic-gate newline(); 20517c478bd9Sstevel@tonic-gate return (c); 20527c478bd9Sstevel@tonic-gate 20537c478bd9Sstevel@tonic-gate /* 20547c478bd9Sstevel@tonic-gate * Returns occurrence value. 0 & 1 both do first occurrence 20557c478bd9Sstevel@tonic-gate * only: c = 0 if ordinary substitute; c = 1 20567c478bd9Sstevel@tonic-gate * if use 1 in global sub(s/a/b/1). 0 in global form is illegal. 20577c478bd9Sstevel@tonic-gate */ 20587c478bd9Sstevel@tonic-gate } 20597c478bd9Sstevel@tonic-gate 20607c478bd9Sstevel@tonic-gate static int 20617c478bd9Sstevel@tonic-gate getsub(void) 20627c478bd9Sstevel@tonic-gate { 20637c478bd9Sstevel@tonic-gate char *p1, *p2; 20647c478bd9Sstevel@tonic-gate 20657c478bd9Sstevel@tonic-gate p1 = linebuf; 20667c478bd9Sstevel@tonic-gate if ((p2 = linebp) == 0) 20677c478bd9Sstevel@tonic-gate return (EOF); 20687c478bd9Sstevel@tonic-gate while (*p1++ = *p2++); 20697c478bd9Sstevel@tonic-gate linebp = 0; 20707c478bd9Sstevel@tonic-gate return (0); 20717c478bd9Sstevel@tonic-gate } 20727c478bd9Sstevel@tonic-gate 20737c478bd9Sstevel@tonic-gate static void 20747c478bd9Sstevel@tonic-gate dosub(void) 20757c478bd9Sstevel@tonic-gate { 20767c478bd9Sstevel@tonic-gate char *lp, *sp, *rp; 20777c478bd9Sstevel@tonic-gate int c; 20787c478bd9Sstevel@tonic-gate 20797c478bd9Sstevel@tonic-gate if (gsubf > 0 && gsubf < LBSIZE) { 20807c478bd9Sstevel@tonic-gate numpass++; 20817c478bd9Sstevel@tonic-gate if (gsubf != numpass) 20827c478bd9Sstevel@tonic-gate return; 20837c478bd9Sstevel@tonic-gate } 20847c478bd9Sstevel@tonic-gate ocerr1++; 20857c478bd9Sstevel@tonic-gate ocerr2++; 20867c478bd9Sstevel@tonic-gate lp = linebuf; 20877c478bd9Sstevel@tonic-gate sp = genbuf; 20887c478bd9Sstevel@tonic-gate rp = rhsbuf; 20897c478bd9Sstevel@tonic-gate while (lp < loc1) 20907c478bd9Sstevel@tonic-gate *sp++ = *lp++; 20917c478bd9Sstevel@tonic-gate while (c = *rp++) { 20927c478bd9Sstevel@tonic-gate if (c == '&') { 20937c478bd9Sstevel@tonic-gate sp = place(sp, loc1, loc2); 20947c478bd9Sstevel@tonic-gate continue; 20957c478bd9Sstevel@tonic-gate } else if (c == '\\') { 20967c478bd9Sstevel@tonic-gate c = *rp++; 20977c478bd9Sstevel@tonic-gate if (c >= '1' && c < nbra + '1') { 20987c478bd9Sstevel@tonic-gate sp = place(sp, braslist[c-'1'], braelist[c-'1']); 20997c478bd9Sstevel@tonic-gate continue; 21007c478bd9Sstevel@tonic-gate } 21017c478bd9Sstevel@tonic-gate } 21027c478bd9Sstevel@tonic-gate *sp++ = c; 21037c478bd9Sstevel@tonic-gate if (sp >= &genbuf[LBSIZE]) 21047c478bd9Sstevel@tonic-gate (void) error(27); 21057c478bd9Sstevel@tonic-gate } 21067c478bd9Sstevel@tonic-gate lp = loc2; 21077c478bd9Sstevel@tonic-gate loc2 = sp - genbuf + linebuf; 21087c478bd9Sstevel@tonic-gate while (*sp++ = *lp++) 21097c478bd9Sstevel@tonic-gate if (sp >= &genbuf[LBSIZE]) 21107c478bd9Sstevel@tonic-gate (void) error(27); 21117c478bd9Sstevel@tonic-gate lp = linebuf; 21127c478bd9Sstevel@tonic-gate sp = genbuf; 21137c478bd9Sstevel@tonic-gate while (*lp++ = *sp++); 21147c478bd9Sstevel@tonic-gate } 21157c478bd9Sstevel@tonic-gate 21167c478bd9Sstevel@tonic-gate static char * 21177c478bd9Sstevel@tonic-gate place(char *sp, char *l1, char *l2) 21187c478bd9Sstevel@tonic-gate { 21197c478bd9Sstevel@tonic-gate 21207c478bd9Sstevel@tonic-gate while (l1 < l2) { 21217c478bd9Sstevel@tonic-gate *sp++ = *l1++; 21227c478bd9Sstevel@tonic-gate if (sp >= &genbuf[LBSIZE]) 21237c478bd9Sstevel@tonic-gate (void) error(27); 21247c478bd9Sstevel@tonic-gate } 21257c478bd9Sstevel@tonic-gate return (sp); 21267c478bd9Sstevel@tonic-gate } 21277c478bd9Sstevel@tonic-gate 21287c478bd9Sstevel@tonic-gate static void 21297c478bd9Sstevel@tonic-gate comple(wchar_t seof) 21307c478bd9Sstevel@tonic-gate { 21317c478bd9Sstevel@tonic-gate int cclass = 0; 21327c478bd9Sstevel@tonic-gate wchar_t c; 21337c478bd9Sstevel@tonic-gate int n; 21347c478bd9Sstevel@tonic-gate char *cp = genbuf; 21357c478bd9Sstevel@tonic-gate char multic[MB_LEN_MAX]; 21367c478bd9Sstevel@tonic-gate 21377c478bd9Sstevel@tonic-gate while (1) { 21387c478bd9Sstevel@tonic-gate if ((n = _mbftowc(multic, &c, getchr, &peekc)) < 0) 21397c478bd9Sstevel@tonic-gate error1(67); 21407c478bd9Sstevel@tonic-gate if (n == 0 || c == '\n') { 21417c478bd9Sstevel@tonic-gate if (cclass) 21427c478bd9Sstevel@tonic-gate error1(49); 21437c478bd9Sstevel@tonic-gate else 21447c478bd9Sstevel@tonic-gate break; 21457c478bd9Sstevel@tonic-gate } 21467c478bd9Sstevel@tonic-gate if (c == seof && !cclass) 21477c478bd9Sstevel@tonic-gate break; 21487c478bd9Sstevel@tonic-gate if (cclass && c == ']') { 21497c478bd9Sstevel@tonic-gate cclass = 0; 21507c478bd9Sstevel@tonic-gate if (cp > &genbuf[LBSIZE-1]) 21517c478bd9Sstevel@tonic-gate error1(50); 21527c478bd9Sstevel@tonic-gate *cp++ = ']'; 21537c478bd9Sstevel@tonic-gate continue; 21547c478bd9Sstevel@tonic-gate } 21557c478bd9Sstevel@tonic-gate if (c == '[' && !cclass) { 21567c478bd9Sstevel@tonic-gate cclass = 1; 21577c478bd9Sstevel@tonic-gate if (cp > &genbuf[LBSIZE-1]) 21587c478bd9Sstevel@tonic-gate error1(50); 21597c478bd9Sstevel@tonic-gate *cp++ = '['; 21607c478bd9Sstevel@tonic-gate if ((n = _mbftowc(multic, &c, getchr, &peekc)) < 0) 21617c478bd9Sstevel@tonic-gate error1(67); 21627c478bd9Sstevel@tonic-gate if (n == 0 || c == '\n') 21637c478bd9Sstevel@tonic-gate error1(49); 21647c478bd9Sstevel@tonic-gate } 21657c478bd9Sstevel@tonic-gate if (c == '\\' && !cclass) { 21667c478bd9Sstevel@tonic-gate if (cp > &genbuf[LBSIZE-1]) 21677c478bd9Sstevel@tonic-gate error1(50); 21687c478bd9Sstevel@tonic-gate *cp++ = '\\'; 21697c478bd9Sstevel@tonic-gate if ((n = _mbftowc(multic, &c, getchr, &peekc)) < 0) 21707c478bd9Sstevel@tonic-gate error1(67); 21717c478bd9Sstevel@tonic-gate if (n == 0 || c == '\n') 21727c478bd9Sstevel@tonic-gate error1(36); 21737c478bd9Sstevel@tonic-gate } 21747c478bd9Sstevel@tonic-gate if (cp + n > &genbuf[LBSIZE-1]) 21757c478bd9Sstevel@tonic-gate error1(50); 21767c478bd9Sstevel@tonic-gate (void) strncpy(cp, multic, n); 21777c478bd9Sstevel@tonic-gate cp += n; 21787c478bd9Sstevel@tonic-gate } 21797c478bd9Sstevel@tonic-gate *cp = '\0'; 21807c478bd9Sstevel@tonic-gate if (n != 0 && c == '\n') 21817c478bd9Sstevel@tonic-gate UNGETC('\n'); 21827c478bd9Sstevel@tonic-gate if (n == 0 || c == '\n') 21837c478bd9Sstevel@tonic-gate nodelim = 1; 21847c478bd9Sstevel@tonic-gate 21857c478bd9Sstevel@tonic-gate /* 21867c478bd9Sstevel@tonic-gate * NULL RE: do not compile a null regular expression; but process 21877c478bd9Sstevel@tonic-gate * input with last regular expression encountered 21887c478bd9Sstevel@tonic-gate */ 21897c478bd9Sstevel@tonic-gate 21907c478bd9Sstevel@tonic-gate if (genbuf[0] != '\0') { 21917c478bd9Sstevel@tonic-gate if (expbuf) 21927c478bd9Sstevel@tonic-gate free(expbuf); 21937c478bd9Sstevel@tonic-gate expbuf = compile(genbuf, (char *)0, (char *)0); 21947c478bd9Sstevel@tonic-gate } 21957c478bd9Sstevel@tonic-gate if (regerrno) 21967c478bd9Sstevel@tonic-gate error1(regerrno); 21977c478bd9Sstevel@tonic-gate } 21987c478bd9Sstevel@tonic-gate 21997c478bd9Sstevel@tonic-gate static void 22007c478bd9Sstevel@tonic-gate move(int cflag) 22017c478bd9Sstevel@tonic-gate { 22027c478bd9Sstevel@tonic-gate LINE adt, ad1, ad2; 22037c478bd9Sstevel@tonic-gate 22047c478bd9Sstevel@tonic-gate setdot(); 22057c478bd9Sstevel@tonic-gate nonzero(); 22067c478bd9Sstevel@tonic-gate if ((adt = address()) == 0) 22077c478bd9Sstevel@tonic-gate (void) error(39); 22087c478bd9Sstevel@tonic-gate newline(); 22097c478bd9Sstevel@tonic-gate if (!globflg) save(); 22107c478bd9Sstevel@tonic-gate if (cflag) { 22117c478bd9Sstevel@tonic-gate ad1 = dol; 22127c478bd9Sstevel@tonic-gate append(getcopy, ad1++); 22137c478bd9Sstevel@tonic-gate ad2 = dol; 22147c478bd9Sstevel@tonic-gate } else { 22157c478bd9Sstevel@tonic-gate ad2 = addr2; 22167c478bd9Sstevel@tonic-gate for (ad1 = addr1; ad1 <= ad2; ) 22177c478bd9Sstevel@tonic-gate (ad1++)->cur &= ~01; 22187c478bd9Sstevel@tonic-gate ad1 = addr1; 22197c478bd9Sstevel@tonic-gate } 22207c478bd9Sstevel@tonic-gate ad2++; 22217c478bd9Sstevel@tonic-gate if (adt < ad1) { 22227c478bd9Sstevel@tonic-gate dot = adt + (ad2-ad1); 22237c478bd9Sstevel@tonic-gate if ((++adt) == ad1) 22247c478bd9Sstevel@tonic-gate return; 22257c478bd9Sstevel@tonic-gate reverse(adt, ad1); 22267c478bd9Sstevel@tonic-gate reverse(ad1, ad2); 22277c478bd9Sstevel@tonic-gate reverse(adt, ad2); 22287c478bd9Sstevel@tonic-gate } else if (adt >= ad2) { 22297c478bd9Sstevel@tonic-gate dot = adt++; 22307c478bd9Sstevel@tonic-gate reverse(ad1, ad2); 22317c478bd9Sstevel@tonic-gate reverse(ad2, adt); 22327c478bd9Sstevel@tonic-gate reverse(ad1, adt); 22337c478bd9Sstevel@tonic-gate } else 22347c478bd9Sstevel@tonic-gate (void) error(39); 22357c478bd9Sstevel@tonic-gate fchange = 1; 22367c478bd9Sstevel@tonic-gate } 22377c478bd9Sstevel@tonic-gate 22387c478bd9Sstevel@tonic-gate static void 22397c478bd9Sstevel@tonic-gate reverse(LINE a1, LINE a2) 22407c478bd9Sstevel@tonic-gate { 22417c478bd9Sstevel@tonic-gate long t; 22427c478bd9Sstevel@tonic-gate 22437c478bd9Sstevel@tonic-gate for (;;) { 22447c478bd9Sstevel@tonic-gate t = (--a2)->cur; 22457c478bd9Sstevel@tonic-gate if (a2 <= a1) 22467c478bd9Sstevel@tonic-gate return; 22477c478bd9Sstevel@tonic-gate a2->cur = a1->cur; 22487c478bd9Sstevel@tonic-gate (a1++)->cur = t; 22497c478bd9Sstevel@tonic-gate } 22507c478bd9Sstevel@tonic-gate } 22517c478bd9Sstevel@tonic-gate 22527c478bd9Sstevel@tonic-gate static int 22537c478bd9Sstevel@tonic-gate getcopy(void) 22547c478bd9Sstevel@tonic-gate { 22557c478bd9Sstevel@tonic-gate 22567c478bd9Sstevel@tonic-gate if (addr1 > addr2) 22577c478bd9Sstevel@tonic-gate return (EOF); 2258*23a1cceaSRoger A. Faulkner (void) getaline((addr1++)->cur); 22597c478bd9Sstevel@tonic-gate return (0); 22607c478bd9Sstevel@tonic-gate } 22617c478bd9Sstevel@tonic-gate 22627c478bd9Sstevel@tonic-gate 22637c478bd9Sstevel@tonic-gate /* 22647c478bd9Sstevel@tonic-gate * Handles error code returned from comple() routine: regular expression 22657c478bd9Sstevel@tonic-gate * compile and match routines 22667c478bd9Sstevel@tonic-gate */ 22677c478bd9Sstevel@tonic-gate 22687c478bd9Sstevel@tonic-gate static void 22697c478bd9Sstevel@tonic-gate error1(int code) 22707c478bd9Sstevel@tonic-gate { 22717c478bd9Sstevel@tonic-gate nbra = 0; 22727c478bd9Sstevel@tonic-gate (void) error(code); 22737c478bd9Sstevel@tonic-gate } 22747c478bd9Sstevel@tonic-gate 22757c478bd9Sstevel@tonic-gate 22767c478bd9Sstevel@tonic-gate static int 22777c478bd9Sstevel@tonic-gate execute(int gf, LINE addr) 22787c478bd9Sstevel@tonic-gate { 22797c478bd9Sstevel@tonic-gate char *p1; 22807c478bd9Sstevel@tonic-gate int c; 22817c478bd9Sstevel@tonic-gate 22827c478bd9Sstevel@tonic-gate for (c = 0; c < nbra; c++) { 22837c478bd9Sstevel@tonic-gate braslist[c] = 0; 22847c478bd9Sstevel@tonic-gate braelist[c] = 0; 22857c478bd9Sstevel@tonic-gate } 22867c478bd9Sstevel@tonic-gate if (gf) 22877c478bd9Sstevel@tonic-gate locs = p1 = loc2; 22887c478bd9Sstevel@tonic-gate else { 22897c478bd9Sstevel@tonic-gate if (addr == zero) 22907c478bd9Sstevel@tonic-gate return (0); 2291*23a1cceaSRoger A. Faulkner p1 = getaline(addr->cur); 22927c478bd9Sstevel@tonic-gate locs = 0; 22937c478bd9Sstevel@tonic-gate } 22947c478bd9Sstevel@tonic-gate return (step(p1, expbuf)); 22957c478bd9Sstevel@tonic-gate } 22967c478bd9Sstevel@tonic-gate 22977c478bd9Sstevel@tonic-gate 22987c478bd9Sstevel@tonic-gate static void 22997c478bd9Sstevel@tonic-gate putd() 23007c478bd9Sstevel@tonic-gate { 23017c478bd9Sstevel@tonic-gate int r; 23027c478bd9Sstevel@tonic-gate 23037c478bd9Sstevel@tonic-gate r = (int)(count%10); 23047c478bd9Sstevel@tonic-gate count /= 10; 23057c478bd9Sstevel@tonic-gate if (count) 23067c478bd9Sstevel@tonic-gate putd(); 23077c478bd9Sstevel@tonic-gate putchr(r + '0'); 23087c478bd9Sstevel@tonic-gate } 23097c478bd9Sstevel@tonic-gate 23107c478bd9Sstevel@tonic-gate 23117c478bd9Sstevel@tonic-gate int 23127c478bd9Sstevel@tonic-gate puts(const char *sp) 23137c478bd9Sstevel@tonic-gate { 23147c478bd9Sstevel@tonic-gate int n; 23157c478bd9Sstevel@tonic-gate wchar_t c; 23167c478bd9Sstevel@tonic-gate int sz, i; 23177c478bd9Sstevel@tonic-gate if (fss.Ffill && (listf == 0)) { 23187c478bd9Sstevel@tonic-gate 23197c478bd9Sstevel@tonic-gate /* deliberate attempt to remove constness of sp because */ 23207c478bd9Sstevel@tonic-gate /* it needs to be expanded */ 23217c478bd9Sstevel@tonic-gate 23227c478bd9Sstevel@tonic-gate if ((i = expnd((char *)sp, funny, &sz, &fss)) == -1) { 23237c478bd9Sstevel@tonic-gate write(1, funny, fss.Flim & 0377); 23247c478bd9Sstevel@tonic-gate putchr('\n'); 23257c478bd9Sstevel@tonic-gate write(1, gettext("too long"), 23267c478bd9Sstevel@tonic-gate strlen(gettext("too long"))); 23277c478bd9Sstevel@tonic-gate } 23287c478bd9Sstevel@tonic-gate else 23297c478bd9Sstevel@tonic-gate write(1, funny, sz); 23307c478bd9Sstevel@tonic-gate putchr('\n'); 23317c478bd9Sstevel@tonic-gate if (i == -2) 23327c478bd9Sstevel@tonic-gate write(1, gettext("tab count\n"), 23337c478bd9Sstevel@tonic-gate strlen(gettext("tab count\n"))); 23347c478bd9Sstevel@tonic-gate return (0); 23357c478bd9Sstevel@tonic-gate } 23367c478bd9Sstevel@tonic-gate col = 0; 23377c478bd9Sstevel@tonic-gate while (*sp) { 23387c478bd9Sstevel@tonic-gate n = mbtowc(&c, sp, MB_LEN_MAX); 23397c478bd9Sstevel@tonic-gate if (listf) { 23407c478bd9Sstevel@tonic-gate if (n < 1) 23417c478bd9Sstevel@tonic-gate (void) error(28); 23427c478bd9Sstevel@tonic-gate else if (n == 1) 23437c478bd9Sstevel@tonic-gate putchr((unsigned char)*sp++); 23447c478bd9Sstevel@tonic-gate else { 23457c478bd9Sstevel@tonic-gate sp += n; 23467c478bd9Sstevel@tonic-gate putwchr(c); 23477c478bd9Sstevel@tonic-gate } 23487c478bd9Sstevel@tonic-gate } else { 23497c478bd9Sstevel@tonic-gate putchr((unsigned char)*sp++); 23507c478bd9Sstevel@tonic-gate } 23517c478bd9Sstevel@tonic-gate } 23527c478bd9Sstevel@tonic-gate #ifndef XPG6 23537c478bd9Sstevel@tonic-gate if (listf) 23547c478bd9Sstevel@tonic-gate putchr('$'); /* end of line is marked with a $ */ 23557c478bd9Sstevel@tonic-gate #else 23567c478bd9Sstevel@tonic-gate if (listf) { 23577c478bd9Sstevel@tonic-gate /* xpg6 - ensure that the end of line $ is not preceeded with a "\" */ 23587c478bd9Sstevel@tonic-gate /* by doing a putchr() with listf=0, thereby avoiding the $ case */ 23597c478bd9Sstevel@tonic-gate /* statement in putchr() */ 23607c478bd9Sstevel@tonic-gate listf = 0; 23617c478bd9Sstevel@tonic-gate putchr('$'); /* end of line is marked with a $ */ 23627c478bd9Sstevel@tonic-gate listf++; 23637c478bd9Sstevel@tonic-gate } 23647c478bd9Sstevel@tonic-gate #endif 23657c478bd9Sstevel@tonic-gate putchr('\n'); 23667c478bd9Sstevel@tonic-gate return (1); 23677c478bd9Sstevel@tonic-gate } 23687c478bd9Sstevel@tonic-gate 23697c478bd9Sstevel@tonic-gate 23707c478bd9Sstevel@tonic-gate static void 23717c478bd9Sstevel@tonic-gate putwchr(wchar_t ac) 23727c478bd9Sstevel@tonic-gate { 23737c478bd9Sstevel@tonic-gate char buf[MB_LEN_MAX], *p; 23747c478bd9Sstevel@tonic-gate char *lp; 23757c478bd9Sstevel@tonic-gate wchar_t c; 23767c478bd9Sstevel@tonic-gate short len; 23777c478bd9Sstevel@tonic-gate 23787c478bd9Sstevel@tonic-gate lp = linp; 23797c478bd9Sstevel@tonic-gate c = ac; 23807c478bd9Sstevel@tonic-gate if (listf) { 23817c478bd9Sstevel@tonic-gate if (!iswprint(c)) { 23827c478bd9Sstevel@tonic-gate p = &buf[0]; 23837c478bd9Sstevel@tonic-gate if ((len = wctomb(p, c)) <= 0) { 23847c478bd9Sstevel@tonic-gate *p = (unsigned char)c; 23857c478bd9Sstevel@tonic-gate len = 1; 23867c478bd9Sstevel@tonic-gate }; 23877c478bd9Sstevel@tonic-gate while (len--) { 23887c478bd9Sstevel@tonic-gate if (col + 4 >= 72) { 23897c478bd9Sstevel@tonic-gate col = 0; 23907c478bd9Sstevel@tonic-gate *lp++ = '\\'; 23917c478bd9Sstevel@tonic-gate *lp++ = '\n'; 23927c478bd9Sstevel@tonic-gate } 23937c478bd9Sstevel@tonic-gate (void) sprintf(lp, "\\%03o", 23947c478bd9Sstevel@tonic-gate *(unsigned char *)p++); 23957c478bd9Sstevel@tonic-gate col += 4; 23967c478bd9Sstevel@tonic-gate lp += 4; 23977c478bd9Sstevel@tonic-gate } 23987c478bd9Sstevel@tonic-gate } else { 23997c478bd9Sstevel@tonic-gate if ((len = wcwidth(c)) <= 0) 24007c478bd9Sstevel@tonic-gate len = 0; 24017c478bd9Sstevel@tonic-gate if (col + len >= 72) { 24027c478bd9Sstevel@tonic-gate col = 0; 24037c478bd9Sstevel@tonic-gate *lp++ = '\\'; 24047c478bd9Sstevel@tonic-gate *lp++ = '\n'; 24057c478bd9Sstevel@tonic-gate } 24067c478bd9Sstevel@tonic-gate col += len; 24077c478bd9Sstevel@tonic-gate if ((len = wctomb(lp, c)) <= 0) { 24087c478bd9Sstevel@tonic-gate *lp = (unsigned char)c; 24097c478bd9Sstevel@tonic-gate len = 1; 24107c478bd9Sstevel@tonic-gate } 24117c478bd9Sstevel@tonic-gate lp += len; 24127c478bd9Sstevel@tonic-gate } 24137c478bd9Sstevel@tonic-gate } else { 24147c478bd9Sstevel@tonic-gate if ((len = wctomb(lp, c)) <= 0) { 24157c478bd9Sstevel@tonic-gate *lp = (unsigned char)c; 24167c478bd9Sstevel@tonic-gate len = 1; 24177c478bd9Sstevel@tonic-gate } 24187c478bd9Sstevel@tonic-gate lp += len; 24197c478bd9Sstevel@tonic-gate } 24207c478bd9Sstevel@tonic-gate if (c == '\n' || lp >= &line[64]) { 24217c478bd9Sstevel@tonic-gate linp = line; 24227c478bd9Sstevel@tonic-gate len = lp - line; 24237c478bd9Sstevel@tonic-gate write(1, line, len); 24247c478bd9Sstevel@tonic-gate return; 24257c478bd9Sstevel@tonic-gate } 24267c478bd9Sstevel@tonic-gate linp = lp; 24277c478bd9Sstevel@tonic-gate } 24287c478bd9Sstevel@tonic-gate 24297c478bd9Sstevel@tonic-gate 24307c478bd9Sstevel@tonic-gate static void 24317c478bd9Sstevel@tonic-gate putchr(unsigned char c) 24327c478bd9Sstevel@tonic-gate { 24337c478bd9Sstevel@tonic-gate char *lp; 24347c478bd9Sstevel@tonic-gate int len; 24357c478bd9Sstevel@tonic-gate 24367c478bd9Sstevel@tonic-gate lp = linp; 24377c478bd9Sstevel@tonic-gate if (listf && c != '\n') { 24387c478bd9Sstevel@tonic-gate switch (c) { 24397c478bd9Sstevel@tonic-gate case '\\' : 24407c478bd9Sstevel@tonic-gate *lp++ = '\\'; 24417c478bd9Sstevel@tonic-gate *lp++ = '\\'; 24427c478bd9Sstevel@tonic-gate col += 2; 24437c478bd9Sstevel@tonic-gate break; 24447c478bd9Sstevel@tonic-gate case '\007' : 24457c478bd9Sstevel@tonic-gate *lp++ = '\\'; 24467c478bd9Sstevel@tonic-gate *lp++ = 'a'; 24477c478bd9Sstevel@tonic-gate col += 2; 24487c478bd9Sstevel@tonic-gate break; 24497c478bd9Sstevel@tonic-gate case '\b' : 24507c478bd9Sstevel@tonic-gate *lp++ = '\\'; 24517c478bd9Sstevel@tonic-gate *lp++ = 'b'; 24527c478bd9Sstevel@tonic-gate col += 2; 24537c478bd9Sstevel@tonic-gate break; 24547c478bd9Sstevel@tonic-gate case '\f' : 24557c478bd9Sstevel@tonic-gate *lp++ = '\\'; 24567c478bd9Sstevel@tonic-gate *lp++ = 'f'; 24577c478bd9Sstevel@tonic-gate col += 2; 24587c478bd9Sstevel@tonic-gate break; 24597c478bd9Sstevel@tonic-gate case '\r' : 24607c478bd9Sstevel@tonic-gate *lp++ = '\\'; 24617c478bd9Sstevel@tonic-gate *lp++ = 'r'; 24627c478bd9Sstevel@tonic-gate col += 2; 24637c478bd9Sstevel@tonic-gate break; 24647c478bd9Sstevel@tonic-gate case '\t' : 24657c478bd9Sstevel@tonic-gate *lp++ = '\\'; 24667c478bd9Sstevel@tonic-gate *lp++ = 't'; 24677c478bd9Sstevel@tonic-gate col += 2; 24687c478bd9Sstevel@tonic-gate break; 24697c478bd9Sstevel@tonic-gate case '\v' : 24707c478bd9Sstevel@tonic-gate *lp++ = '\\'; 24717c478bd9Sstevel@tonic-gate *lp++ = 'v'; 24727c478bd9Sstevel@tonic-gate col += 2; 24737c478bd9Sstevel@tonic-gate break; 24747c478bd9Sstevel@tonic-gate #ifdef XPG6 24757c478bd9Sstevel@tonic-gate /* if $ characters are within the line preceed with \ */ 24767c478bd9Sstevel@tonic-gate case '$' : 24777c478bd9Sstevel@tonic-gate *lp++ = '\\'; 24787c478bd9Sstevel@tonic-gate *lp++ = '$'; 24797c478bd9Sstevel@tonic-gate col += 2; 24807c478bd9Sstevel@tonic-gate break; 24817c478bd9Sstevel@tonic-gate #endif 24827c478bd9Sstevel@tonic-gate default: 24837c478bd9Sstevel@tonic-gate if (isprint(c)) { 24847c478bd9Sstevel@tonic-gate *lp++ = c; 24857c478bd9Sstevel@tonic-gate col += 1; 24867c478bd9Sstevel@tonic-gate } else { 24877c478bd9Sstevel@tonic-gate (void) sprintf(lp, "\\%03o", c); 24887c478bd9Sstevel@tonic-gate col += 4; 24897c478bd9Sstevel@tonic-gate lp += 4; 24907c478bd9Sstevel@tonic-gate } 24917c478bd9Sstevel@tonic-gate break; 24927c478bd9Sstevel@tonic-gate } 24937c478bd9Sstevel@tonic-gate 24947c478bd9Sstevel@tonic-gate /* 24957c478bd9Sstevel@tonic-gate * long lines are folded w/ pt of folding indicated by writing 24967c478bd9Sstevel@tonic-gate * backslash/newline character 24977c478bd9Sstevel@tonic-gate */ 24987c478bd9Sstevel@tonic-gate 24997c478bd9Sstevel@tonic-gate if (col + 1 >= 72) { 25007c478bd9Sstevel@tonic-gate col = 0; 25017c478bd9Sstevel@tonic-gate *lp++ = '\\'; 25027c478bd9Sstevel@tonic-gate *lp++ = '\n'; 25037c478bd9Sstevel@tonic-gate } 25047c478bd9Sstevel@tonic-gate } else 25057c478bd9Sstevel@tonic-gate *lp++ = c; 25067c478bd9Sstevel@tonic-gate if (c == '\n' || lp >= &line[64]) { 25077c478bd9Sstevel@tonic-gate linp = line; 25087c478bd9Sstevel@tonic-gate len = lp - line; 25097c478bd9Sstevel@tonic-gate (void) write(1, line, len); 25107c478bd9Sstevel@tonic-gate return; 25117c478bd9Sstevel@tonic-gate } 25127c478bd9Sstevel@tonic-gate linp = lp; 25137c478bd9Sstevel@tonic-gate } 25147c478bd9Sstevel@tonic-gate 25157c478bd9Sstevel@tonic-gate 25167c478bd9Sstevel@tonic-gate static char * 25177c478bd9Sstevel@tonic-gate getkey(const char *prompt) 25187c478bd9Sstevel@tonic-gate { 25197c478bd9Sstevel@tonic-gate struct termio b; 25207c478bd9Sstevel@tonic-gate int save; 25217c478bd9Sstevel@tonic-gate void (*sig)(); 25227c478bd9Sstevel@tonic-gate static char key[KSIZE+1]; 25237c478bd9Sstevel@tonic-gate char *p; 25247c478bd9Sstevel@tonic-gate int c; 25257c478bd9Sstevel@tonic-gate 25267c478bd9Sstevel@tonic-gate sig = signal(SIGINT, SIG_IGN); 25277c478bd9Sstevel@tonic-gate ioctl(0, TCGETA, &b); 25287c478bd9Sstevel@tonic-gate save = b.c_lflag; 25297c478bd9Sstevel@tonic-gate b.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); 25307c478bd9Sstevel@tonic-gate ioctl(0, TCSETAW, &b); 25317c478bd9Sstevel@tonic-gate write(1, gettext(prompt), strlen(gettext(prompt))); 25327c478bd9Sstevel@tonic-gate p = key; 25337c478bd9Sstevel@tonic-gate while (((c = getchr()) != EOF) && (c != '\n')) { 25347c478bd9Sstevel@tonic-gate if (p < &key[KSIZE]) 25357c478bd9Sstevel@tonic-gate *p++ = c; 25367c478bd9Sstevel@tonic-gate } 25377c478bd9Sstevel@tonic-gate *p = 0; 25387c478bd9Sstevel@tonic-gate write(1, "\n", 1); 25397c478bd9Sstevel@tonic-gate b.c_lflag = save; 25407c478bd9Sstevel@tonic-gate ioctl(0, TCSETAW, &b); 25417c478bd9Sstevel@tonic-gate signal(SIGINT, sig); 25427c478bd9Sstevel@tonic-gate return (key); 25437c478bd9Sstevel@tonic-gate } 25447c478bd9Sstevel@tonic-gate 25457c478bd9Sstevel@tonic-gate 25467c478bd9Sstevel@tonic-gate static void 25477c478bd9Sstevel@tonic-gate globaln(int k) 25487c478bd9Sstevel@tonic-gate { 25497c478bd9Sstevel@tonic-gate char *gp; 25507c478bd9Sstevel@tonic-gate int c; 25517c478bd9Sstevel@tonic-gate int n; 25527c478bd9Sstevel@tonic-gate wchar_t cl; 25537c478bd9Sstevel@tonic-gate LINE a1; 25547c478bd9Sstevel@tonic-gate int nfirst; 25557c478bd9Sstevel@tonic-gate char globuf[LBSIZE]; 25567c478bd9Sstevel@tonic-gate char multic[MB_LEN_MAX]; 25577c478bd9Sstevel@tonic-gate int len; 25587c478bd9Sstevel@tonic-gate int pflag_save = 0; 25597c478bd9Sstevel@tonic-gate int listf_save = 0; 25607c478bd9Sstevel@tonic-gate int listn_save = 0; 25617c478bd9Sstevel@tonic-gate 25627c478bd9Sstevel@tonic-gate if (globp) 25637c478bd9Sstevel@tonic-gate (void) error(33); 25647c478bd9Sstevel@tonic-gate setall(); 25657c478bd9Sstevel@tonic-gate nonzero(); 25667c478bd9Sstevel@tonic-gate if ((n = _mbftowc(multic, &cl, getchr, &peekc)) <= 0) 25677c478bd9Sstevel@tonic-gate (void) error(67); 25687c478bd9Sstevel@tonic-gate if (cl == '\n') 25697c478bd9Sstevel@tonic-gate (void) error(19); 25707c478bd9Sstevel@tonic-gate save(); 25717c478bd9Sstevel@tonic-gate comple(cl); 25727c478bd9Sstevel@tonic-gate for (a1 = zero; a1 <= dol; a1++) { 25737c478bd9Sstevel@tonic-gate a1->cur &= ~01; 25747c478bd9Sstevel@tonic-gate if (a1 >= addr1 && a1 <= addr2 && execute(0, a1) == k) 25757c478bd9Sstevel@tonic-gate a1->cur |= 01; 25767c478bd9Sstevel@tonic-gate } 25777c478bd9Sstevel@tonic-gate nfirst = 0; 25787c478bd9Sstevel@tonic-gate newline(); 25797c478bd9Sstevel@tonic-gate /* 25807c478bd9Sstevel@tonic-gate * preserve the p, l, and n suffix commands of the G and V 25817c478bd9Sstevel@tonic-gate * commands during the interactive section and restore 25827c478bd9Sstevel@tonic-gate * on completion of the G and V command. 25837c478bd9Sstevel@tonic-gate */ 25847c478bd9Sstevel@tonic-gate pflag_save = pflag; 25857c478bd9Sstevel@tonic-gate listf_save = listf; 25867c478bd9Sstevel@tonic-gate listn_save = listn; 25877c478bd9Sstevel@tonic-gate pflag = 0; 25887c478bd9Sstevel@tonic-gate listf = 0; 25897c478bd9Sstevel@tonic-gate listn = 0; 25907c478bd9Sstevel@tonic-gate for (a1 = zero; a1 <= dol; a1++) { 25917c478bd9Sstevel@tonic-gate if (a1->cur & 01) { 25927c478bd9Sstevel@tonic-gate a1->cur &= ~01; 25937c478bd9Sstevel@tonic-gate dot = a1; 2594*23a1cceaSRoger A. Faulkner puts(getaline(a1->cur)); 25957c478bd9Sstevel@tonic-gate if ((c = get_wchr()) == EOF) 25967c478bd9Sstevel@tonic-gate (void) error(52); 25977c478bd9Sstevel@tonic-gate if (c == 'a' || c == 'i' || c == 'c') 25987c478bd9Sstevel@tonic-gate (void) error(53); 25997c478bd9Sstevel@tonic-gate if (c == '\n') { 26007c478bd9Sstevel@tonic-gate a1 = zero; 26017c478bd9Sstevel@tonic-gate continue; 26027c478bd9Sstevel@tonic-gate } 26037c478bd9Sstevel@tonic-gate if (c != '&') { 26047c478bd9Sstevel@tonic-gate gp = globuf; 26057c478bd9Sstevel@tonic-gate if ((len = wctomb(gp, c)) <= 0) { 26067c478bd9Sstevel@tonic-gate *gp = (unsigned char)c; 26077c478bd9Sstevel@tonic-gate len = 1; 26087c478bd9Sstevel@tonic-gate } 26097c478bd9Sstevel@tonic-gate gp += len; 26107c478bd9Sstevel@tonic-gate while ((c = get_wchr()) != '\n') { 26117c478bd9Sstevel@tonic-gate 26127c478bd9Sstevel@tonic-gate /* '\\' has special meaning only if preceding a '\n' */ 26137c478bd9Sstevel@tonic-gate if (c == '\\') { 26147c478bd9Sstevel@tonic-gate c = get_wchr(); 26157c478bd9Sstevel@tonic-gate if (c != '\n') 26167c478bd9Sstevel@tonic-gate *gp++ = '\\'; 26177c478bd9Sstevel@tonic-gate } 26187c478bd9Sstevel@tonic-gate if ((gp + (unsigned int)MB_CUR_MAX) >= 26197c478bd9Sstevel@tonic-gate &globuf[LBSIZE-1]) 26207c478bd9Sstevel@tonic-gate (void) error(34); 26217c478bd9Sstevel@tonic-gate 26227c478bd9Sstevel@tonic-gate if ((len = wctomb(gp, c)) <= 0) { 26237c478bd9Sstevel@tonic-gate *gp = (unsigned char)c; 26247c478bd9Sstevel@tonic-gate len = 1; 26257c478bd9Sstevel@tonic-gate } 26267c478bd9Sstevel@tonic-gate gp += len; 26277c478bd9Sstevel@tonic-gate } 26287c478bd9Sstevel@tonic-gate *gp++ = '\n'; 26297c478bd9Sstevel@tonic-gate *gp++ = 0; 26307c478bd9Sstevel@tonic-gate nfirst = 1; 26317c478bd9Sstevel@tonic-gate } else if ((c = get_wchr()) != '\n') 26327c478bd9Sstevel@tonic-gate (void) error(54); 26337c478bd9Sstevel@tonic-gate globp = globuf; 26347c478bd9Sstevel@tonic-gate if (nfirst) { 26357c478bd9Sstevel@tonic-gate globflg = 1; 26367c478bd9Sstevel@tonic-gate commands(); 26377c478bd9Sstevel@tonic-gate globflg = 0; 26387c478bd9Sstevel@tonic-gate } else 26397c478bd9Sstevel@tonic-gate (void) error(56); 26407c478bd9Sstevel@tonic-gate globp = 0; 26417c478bd9Sstevel@tonic-gate a1 = zero; 26427c478bd9Sstevel@tonic-gate } 26437c478bd9Sstevel@tonic-gate } 26447c478bd9Sstevel@tonic-gate pflag = pflag_save; 26457c478bd9Sstevel@tonic-gate listf = listf_save; 26467c478bd9Sstevel@tonic-gate listn = listn_save; 26477c478bd9Sstevel@tonic-gate } 26487c478bd9Sstevel@tonic-gate 26497c478bd9Sstevel@tonic-gate 26507c478bd9Sstevel@tonic-gate static int 26517c478bd9Sstevel@tonic-gate eopen(char *string, int rw) 26527c478bd9Sstevel@tonic-gate { 26537c478bd9Sstevel@tonic-gate #define w_or_r(a, b) (rw ? a : b) 26547c478bd9Sstevel@tonic-gate int pf[2]; 26557c478bd9Sstevel@tonic-gate pid_t i; 26567c478bd9Sstevel@tonic-gate int io; 26577c478bd9Sstevel@tonic-gate int chcount; /* # of char read. */ 26587c478bd9Sstevel@tonic-gate 26597c478bd9Sstevel@tonic-gate if (rflg) { /* restricted shell */ 26607c478bd9Sstevel@tonic-gate if (Xqt) { 26617c478bd9Sstevel@tonic-gate Xqt = 0; 26627c478bd9Sstevel@tonic-gate (void) error(6); 26637c478bd9Sstevel@tonic-gate } 26647c478bd9Sstevel@tonic-gate } 26657c478bd9Sstevel@tonic-gate if (!Xqt) { 26667c478bd9Sstevel@tonic-gate if ((io = open(string, rw)) >= 0) { 26677c478bd9Sstevel@tonic-gate if (fflg) { 26687c478bd9Sstevel@tonic-gate chcount = read(io, crbuf, LBSIZE); 26697c478bd9Sstevel@tonic-gate if (crflag == -1) { 26707c478bd9Sstevel@tonic-gate if (isencrypt(crbuf, chcount)) 26717c478bd9Sstevel@tonic-gate crflag = 2; 26727c478bd9Sstevel@tonic-gate else 26737c478bd9Sstevel@tonic-gate crflag = -2; 26747c478bd9Sstevel@tonic-gate } 26757c478bd9Sstevel@tonic-gate if (crflag > 0) 26767c478bd9Sstevel@tonic-gate if (run_crypt(0L, crbuf, chcount, perm) == -1) 26777c478bd9Sstevel@tonic-gate (void) error(63); 26787c478bd9Sstevel@tonic-gate if (fspec(crbuf, &fss, 0) < 0) { 26797c478bd9Sstevel@tonic-gate fss.Ffill = 0; 26807c478bd9Sstevel@tonic-gate fflg = 0; 26817c478bd9Sstevel@tonic-gate (void) error(4); 26827c478bd9Sstevel@tonic-gate } 26837c478bd9Sstevel@tonic-gate lseek(io, 0L, 0); 26847c478bd9Sstevel@tonic-gate } 26857c478bd9Sstevel@tonic-gate } 26867c478bd9Sstevel@tonic-gate fflg = 0; 26877c478bd9Sstevel@tonic-gate return (io); 26887c478bd9Sstevel@tonic-gate } 26897c478bd9Sstevel@tonic-gate if (pipe(pf) < 0) 26907c478bd9Sstevel@tonic-gate xerr: (void) error(0); 26917c478bd9Sstevel@tonic-gate if ((i = fork()) == 0) { 26927c478bd9Sstevel@tonic-gate signal(SIGHUP, oldhup); 26937c478bd9Sstevel@tonic-gate signal(SIGQUIT, oldquit); 26947c478bd9Sstevel@tonic-gate signal(SIGPIPE, oldpipe); 26957c478bd9Sstevel@tonic-gate signal(SIGINT, (void (*)()) 0); 26967c478bd9Sstevel@tonic-gate close(w_or_r(pf[1], pf[0])); 26977c478bd9Sstevel@tonic-gate close(w_or_r(0, 1)); 26987c478bd9Sstevel@tonic-gate dup(w_or_r(pf[0], pf[1])); 26997c478bd9Sstevel@tonic-gate close(w_or_r(pf[0], pf[1])); 27007c478bd9Sstevel@tonic-gate if (__xpg4 == 0) { /* not XPG4 */ 27017c478bd9Sstevel@tonic-gate shpath = "/usr/bin/sh"; 27027c478bd9Sstevel@tonic-gate } else { 27037c478bd9Sstevel@tonic-gate /* XPG4 */ 27047c478bd9Sstevel@tonic-gate shpath = "/usr/xpg4/bin/sh"; 27057c478bd9Sstevel@tonic-gate } 27067c478bd9Sstevel@tonic-gate execlp((const char *)shpath, "sh", "-c", string, (char *)0); 27077c478bd9Sstevel@tonic-gate exit(1); 27087c478bd9Sstevel@tonic-gate } 27097c478bd9Sstevel@tonic-gate if (i == (pid_t)-1) 27107c478bd9Sstevel@tonic-gate goto xerr; 27117c478bd9Sstevel@tonic-gate close(w_or_r(pf[0], pf[1])); 27127c478bd9Sstevel@tonic-gate return (w_or_r(pf[1], pf[0])); 27137c478bd9Sstevel@tonic-gate } 27147c478bd9Sstevel@tonic-gate 27157c478bd9Sstevel@tonic-gate 27167c478bd9Sstevel@tonic-gate static void 27177c478bd9Sstevel@tonic-gate eclose(int f) 27187c478bd9Sstevel@tonic-gate { 27197c478bd9Sstevel@tonic-gate close(f); 27207c478bd9Sstevel@tonic-gate if (Xqt) 27217c478bd9Sstevel@tonic-gate Xqt = 0, wait((int *)0); 27227c478bd9Sstevel@tonic-gate } 27237c478bd9Sstevel@tonic-gate 27247c478bd9Sstevel@tonic-gate 27257c478bd9Sstevel@tonic-gate static void 27267c478bd9Sstevel@tonic-gate mkfunny(void) 27277c478bd9Sstevel@tonic-gate { 27287c478bd9Sstevel@tonic-gate char *p, *p1, *p2; 27297c478bd9Sstevel@tonic-gate 27307c478bd9Sstevel@tonic-gate p2 = p1 = funny; 27317c478bd9Sstevel@tonic-gate p = file; 27327c478bd9Sstevel@tonic-gate /* 27337c478bd9Sstevel@tonic-gate * Go to end of file name 27347c478bd9Sstevel@tonic-gate */ 27357c478bd9Sstevel@tonic-gate while (*p) 27367c478bd9Sstevel@tonic-gate p++; 27377c478bd9Sstevel@tonic-gate while (*--p == '/') /* delete trailing slashes */ 27387c478bd9Sstevel@tonic-gate *p = '\0'; 27397c478bd9Sstevel@tonic-gate /* 27407c478bd9Sstevel@tonic-gate * go back to beginning of file 27417c478bd9Sstevel@tonic-gate */ 27427c478bd9Sstevel@tonic-gate p = file; 27437c478bd9Sstevel@tonic-gate /* 27447c478bd9Sstevel@tonic-gate * Copy file name to funny setting p2 at 27457c478bd9Sstevel@tonic-gate * basename of file. 27467c478bd9Sstevel@tonic-gate */ 27477c478bd9Sstevel@tonic-gate while (*p1++ = *p) 27487c478bd9Sstevel@tonic-gate if (*p++ == '/') p2 = p1; 27497c478bd9Sstevel@tonic-gate /* 27507c478bd9Sstevel@tonic-gate * Set p1 to point to basename of tfname. 27517c478bd9Sstevel@tonic-gate */ 27527c478bd9Sstevel@tonic-gate p1 = strrchr(tfname, '/'); 27537c478bd9Sstevel@tonic-gate if (strlen(tfname) > (size_t)6) 27547c478bd9Sstevel@tonic-gate p1 = &tfname[strlen(tfname)-6]; 27557c478bd9Sstevel@tonic-gate p1++; 27567c478bd9Sstevel@tonic-gate *p2 = '\007'; /* add unprintable char for funny a unique name */ 27577c478bd9Sstevel@tonic-gate /* 27587c478bd9Sstevel@tonic-gate * Copy tfname to file. 27597c478bd9Sstevel@tonic-gate */ 27607c478bd9Sstevel@tonic-gate while (*++p2 = *p1++); 27617c478bd9Sstevel@tonic-gate } 27627c478bd9Sstevel@tonic-gate 27637c478bd9Sstevel@tonic-gate 27647c478bd9Sstevel@tonic-gate static void 27657c478bd9Sstevel@tonic-gate getime(void) /* get modified time of file and save */ 27667c478bd9Sstevel@tonic-gate { 27677c478bd9Sstevel@tonic-gate if (stat(file, &Fl) < 0) 27687c478bd9Sstevel@tonic-gate savtime = 0; 27697c478bd9Sstevel@tonic-gate else 27707c478bd9Sstevel@tonic-gate savtime = Fl.st_mtime; 27717c478bd9Sstevel@tonic-gate } 27727c478bd9Sstevel@tonic-gate 27737c478bd9Sstevel@tonic-gate 27747c478bd9Sstevel@tonic-gate static void 27757c478bd9Sstevel@tonic-gate chktime(void) /* check saved mod time against current mod time */ 27767c478bd9Sstevel@tonic-gate { 27777c478bd9Sstevel@tonic-gate if (savtime != 0 && Fl.st_mtime != 0) { 27787c478bd9Sstevel@tonic-gate if (savtime != Fl.st_mtime) 27797c478bd9Sstevel@tonic-gate (void) error(58); 27807c478bd9Sstevel@tonic-gate } 27817c478bd9Sstevel@tonic-gate } 27827c478bd9Sstevel@tonic-gate 27837c478bd9Sstevel@tonic-gate 27847c478bd9Sstevel@tonic-gate static void 27857c478bd9Sstevel@tonic-gate newtime(void) /* get new mod time and save */ 27867c478bd9Sstevel@tonic-gate { 27877c478bd9Sstevel@tonic-gate stat(file, &Fl); 27887c478bd9Sstevel@tonic-gate savtime = Fl.st_mtime; 27897c478bd9Sstevel@tonic-gate } 27907c478bd9Sstevel@tonic-gate 27917c478bd9Sstevel@tonic-gate 27927c478bd9Sstevel@tonic-gate static void 27937c478bd9Sstevel@tonic-gate red(char *op) /* restricted - check for '/' in name */ 27947c478bd9Sstevel@tonic-gate /* and delete trailing '/' */ 27957c478bd9Sstevel@tonic-gate { 27967c478bd9Sstevel@tonic-gate char *p; 27977c478bd9Sstevel@tonic-gate 27987c478bd9Sstevel@tonic-gate p = op; 27997c478bd9Sstevel@tonic-gate while (*p) 28007c478bd9Sstevel@tonic-gate if (*p++ == '/'&& rflg) { 28017c478bd9Sstevel@tonic-gate *op = 0; 28027c478bd9Sstevel@tonic-gate (void) error(6); 28037c478bd9Sstevel@tonic-gate } 28047c478bd9Sstevel@tonic-gate /* delete trailing '/' */ 28057c478bd9Sstevel@tonic-gate while (p > op) { 28067c478bd9Sstevel@tonic-gate if (*--p == '/') 28077c478bd9Sstevel@tonic-gate *p = '\0'; 28087c478bd9Sstevel@tonic-gate else break; 28097c478bd9Sstevel@tonic-gate } 28107c478bd9Sstevel@tonic-gate } 28117c478bd9Sstevel@tonic-gate 28127c478bd9Sstevel@tonic-gate 28137c478bd9Sstevel@tonic-gate /* 28147c478bd9Sstevel@tonic-gate * Searches thru beginning of file looking for a string of the form 28157c478bd9Sstevel@tonic-gate * <: values... :> 28167c478bd9Sstevel@tonic-gate * 28177c478bd9Sstevel@tonic-gate * where "values" are 28187c478bd9Sstevel@tonic-gate * 28197c478bd9Sstevel@tonic-gate * \b ignored 28207c478bd9Sstevel@tonic-gate * s<num> sets the Flim to <num> 28217c478bd9Sstevel@tonic-gate * t??? sets tab stop stuff 28227c478bd9Sstevel@tonic-gate * d ignored 28237c478bd9Sstevel@tonic-gate * m<num> ignored 28247c478bd9Sstevel@tonic-gate * e ignored 28257c478bd9Sstevel@tonic-gate */ 28267c478bd9Sstevel@tonic-gate 28277c478bd9Sstevel@tonic-gate static int 28287c478bd9Sstevel@tonic-gate fspec(char line[], struct Fspec *f, int up) 28297c478bd9Sstevel@tonic-gate { 28307c478bd9Sstevel@tonic-gate struct termio arg; 28317c478bd9Sstevel@tonic-gate int havespec, n; 28327c478bd9Sstevel@tonic-gate int len; 28337c478bd9Sstevel@tonic-gate 28347c478bd9Sstevel@tonic-gate if (!up) clear(f); 28357c478bd9Sstevel@tonic-gate 28367c478bd9Sstevel@tonic-gate havespec = fsprtn = 0; 28377c478bd9Sstevel@tonic-gate for (fsp = line; *fsp && *fsp != '\n'; fsp += len) { 28387c478bd9Sstevel@tonic-gate if ((len = mblen(fsp, MB_CUR_MAX)) <= 0) 28397c478bd9Sstevel@tonic-gate len = 1; 28407c478bd9Sstevel@tonic-gate switch (*fsp) { 28417c478bd9Sstevel@tonic-gate 28427c478bd9Sstevel@tonic-gate case '<': if (havespec) 28437c478bd9Sstevel@tonic-gate return (-1); 28447c478bd9Sstevel@tonic-gate if (*(fsp+1) == ':') { 28457c478bd9Sstevel@tonic-gate havespec = 1; 28467c478bd9Sstevel@tonic-gate clear(f); 28477c478bd9Sstevel@tonic-gate if (!ioctl(1, TCGETA, &arg) && 28487c478bd9Sstevel@tonic-gate ((arg.c_oflag&TAB3) == TAB3)) 28497c478bd9Sstevel@tonic-gate f->Ffill = 1; 28507c478bd9Sstevel@tonic-gate fsp++; 28517c478bd9Sstevel@tonic-gate continue; 28527c478bd9Sstevel@tonic-gate } 28537c478bd9Sstevel@tonic-gate 28547c478bd9Sstevel@tonic-gate case ' ': continue; 28557c478bd9Sstevel@tonic-gate 28567c478bd9Sstevel@tonic-gate case 's': if (havespec && (n = numb()) >= 0) 28577c478bd9Sstevel@tonic-gate f->Flim = n; 28587c478bd9Sstevel@tonic-gate continue; 28597c478bd9Sstevel@tonic-gate 28607c478bd9Sstevel@tonic-gate case 't': if (havespec) targ(f); 28617c478bd9Sstevel@tonic-gate continue; 28627c478bd9Sstevel@tonic-gate 28637c478bd9Sstevel@tonic-gate case 'd': continue; 28647c478bd9Sstevel@tonic-gate 28657c478bd9Sstevel@tonic-gate case 'm': if (havespec) n = numb(); 28667c478bd9Sstevel@tonic-gate continue; 28677c478bd9Sstevel@tonic-gate 28687c478bd9Sstevel@tonic-gate case 'e': continue; 28697c478bd9Sstevel@tonic-gate case ':': if (!havespec) continue; 28707c478bd9Sstevel@tonic-gate if (*(fsp+1) != '>') fsprtn = -1; 28717c478bd9Sstevel@tonic-gate return (fsprtn); 28727c478bd9Sstevel@tonic-gate 28737c478bd9Sstevel@tonic-gate default: if (!havespec) continue; 28747c478bd9Sstevel@tonic-gate return (-1); 28757c478bd9Sstevel@tonic-gate } 28767c478bd9Sstevel@tonic-gate } 28777c478bd9Sstevel@tonic-gate return (1); 28787c478bd9Sstevel@tonic-gate } 28797c478bd9Sstevel@tonic-gate 28807c478bd9Sstevel@tonic-gate 28817c478bd9Sstevel@tonic-gate static int 28827c478bd9Sstevel@tonic-gate numb(void) 28837c478bd9Sstevel@tonic-gate { 28847c478bd9Sstevel@tonic-gate int n; 28857c478bd9Sstevel@tonic-gate 28867c478bd9Sstevel@tonic-gate n = 0; 28877c478bd9Sstevel@tonic-gate while (*++fsp >= '0' && *fsp <= '9') 28887c478bd9Sstevel@tonic-gate n = 10*n + *fsp-'0'; 28897c478bd9Sstevel@tonic-gate fsp--; 28907c478bd9Sstevel@tonic-gate return (n); 28917c478bd9Sstevel@tonic-gate } 28927c478bd9Sstevel@tonic-gate 28937c478bd9Sstevel@tonic-gate 28947c478bd9Sstevel@tonic-gate static void 28957c478bd9Sstevel@tonic-gate targ(struct Fspec *f) 28967c478bd9Sstevel@tonic-gate { 28977c478bd9Sstevel@tonic-gate 28987c478bd9Sstevel@tonic-gate if (*++fsp == '-') { 28997c478bd9Sstevel@tonic-gate if (*(fsp + 1) >= '0' && *(fsp+1) <= '9') tincr(numb(), f); 29007c478bd9Sstevel@tonic-gate else tstd(f); 29017c478bd9Sstevel@tonic-gate return; 29027c478bd9Sstevel@tonic-gate } 29037c478bd9Sstevel@tonic-gate if (*fsp >= '0' && *fsp <= '9') { 29047c478bd9Sstevel@tonic-gate tlist(f); 29057c478bd9Sstevel@tonic-gate return; 29067c478bd9Sstevel@tonic-gate } 29077c478bd9Sstevel@tonic-gate fsprtn = -1; 29087c478bd9Sstevel@tonic-gate fsp--; 29097c478bd9Sstevel@tonic-gate } 29107c478bd9Sstevel@tonic-gate 29117c478bd9Sstevel@tonic-gate 29127c478bd9Sstevel@tonic-gate static void 29137c478bd9Sstevel@tonic-gate tincr(int n, struct Fspec *f) 29147c478bd9Sstevel@tonic-gate { 29157c478bd9Sstevel@tonic-gate int l, i; 29167c478bd9Sstevel@tonic-gate 29177c478bd9Sstevel@tonic-gate l = 1; 29187c478bd9Sstevel@tonic-gate for (i = 0; i < 20; i++) 29197c478bd9Sstevel@tonic-gate f->Ftabs[i] = l += n; 29207c478bd9Sstevel@tonic-gate f->Ftabs[i] = 0; 29217c478bd9Sstevel@tonic-gate } 29227c478bd9Sstevel@tonic-gate 29237c478bd9Sstevel@tonic-gate 29247c478bd9Sstevel@tonic-gate static void 29257c478bd9Sstevel@tonic-gate tstd(struct Fspec *f) 29267c478bd9Sstevel@tonic-gate { 29277c478bd9Sstevel@tonic-gate char std[3]; 29287c478bd9Sstevel@tonic-gate 29297c478bd9Sstevel@tonic-gate std[0] = *++fsp; 29307c478bd9Sstevel@tonic-gate if (*(fsp+1) >= '0' && *(fsp+1) <= '9') { 29317c478bd9Sstevel@tonic-gate std[1] = *++fsp; 29327c478bd9Sstevel@tonic-gate std[2] = '\0'; 29337c478bd9Sstevel@tonic-gate } else std[1] = '\0'; 29347c478bd9Sstevel@tonic-gate fsprtn = stdtab(std, f->Ftabs); 29357c478bd9Sstevel@tonic-gate } 29367c478bd9Sstevel@tonic-gate 29377c478bd9Sstevel@tonic-gate 29387c478bd9Sstevel@tonic-gate static void 29397c478bd9Sstevel@tonic-gate tlist(struct Fspec *f) 29407c478bd9Sstevel@tonic-gate { 29417c478bd9Sstevel@tonic-gate int n, last, i; 29427c478bd9Sstevel@tonic-gate 29437c478bd9Sstevel@tonic-gate fsp--; 29447c478bd9Sstevel@tonic-gate last = i = 0; 29457c478bd9Sstevel@tonic-gate 29467c478bd9Sstevel@tonic-gate do { 29477c478bd9Sstevel@tonic-gate if ((n = numb()) <= last || i >= 20) { 29487c478bd9Sstevel@tonic-gate fsprtn = -1; 29497c478bd9Sstevel@tonic-gate return; 29507c478bd9Sstevel@tonic-gate } 29517c478bd9Sstevel@tonic-gate f->Ftabs[i++] = last = n; 29527c478bd9Sstevel@tonic-gate } while (*++fsp == ','); 29537c478bd9Sstevel@tonic-gate 29547c478bd9Sstevel@tonic-gate f->Ftabs[i] = 0; 29557c478bd9Sstevel@tonic-gate fsp--; 29567c478bd9Sstevel@tonic-gate } 29577c478bd9Sstevel@tonic-gate 29587c478bd9Sstevel@tonic-gate 29597c478bd9Sstevel@tonic-gate static int 29607c478bd9Sstevel@tonic-gate expnd(char line[], char buf[], int *sz, struct Fspec *f) 29617c478bd9Sstevel@tonic-gate { 29627c478bd9Sstevel@tonic-gate char *l, *t; 29637c478bd9Sstevel@tonic-gate int b; 29647c478bd9Sstevel@tonic-gate 29657c478bd9Sstevel@tonic-gate l = line - 1; 29667c478bd9Sstevel@tonic-gate b = 1; 29677c478bd9Sstevel@tonic-gate t = f->Ftabs; 29687c478bd9Sstevel@tonic-gate fsprtn = 0; 29697c478bd9Sstevel@tonic-gate 29707c478bd9Sstevel@tonic-gate while (*++l && *l != '\n' && b < 511) { 29717c478bd9Sstevel@tonic-gate if (*l == '\t') { 29727c478bd9Sstevel@tonic-gate while (*t && b >= *t) t++; 29737c478bd9Sstevel@tonic-gate if (*t == 0) fsprtn = -2; 29747c478bd9Sstevel@tonic-gate do buf[b-1] = ' '; while (++b < *t); 29757c478bd9Sstevel@tonic-gate } else buf[b++ - 1] = *l; 29767c478bd9Sstevel@tonic-gate } 29777c478bd9Sstevel@tonic-gate 29787c478bd9Sstevel@tonic-gate buf[b] = '\0'; 29797c478bd9Sstevel@tonic-gate *sz = b; 29807c478bd9Sstevel@tonic-gate if (*l != '\0' && *l != '\n') { 29817c478bd9Sstevel@tonic-gate buf[b-1] = '\n'; 29827c478bd9Sstevel@tonic-gate return (-1); 29837c478bd9Sstevel@tonic-gate } 29847c478bd9Sstevel@tonic-gate buf[b-1] = *l; 29857c478bd9Sstevel@tonic-gate if (f->Flim && (b-1 > (int)f->Flim)) 29867c478bd9Sstevel@tonic-gate return (-1); 29877c478bd9Sstevel@tonic-gate return (fsprtn); 29887c478bd9Sstevel@tonic-gate } 29897c478bd9Sstevel@tonic-gate 29907c478bd9Sstevel@tonic-gate 29917c478bd9Sstevel@tonic-gate static void 29927c478bd9Sstevel@tonic-gate clear(struct Fspec *f) 29937c478bd9Sstevel@tonic-gate { 29947c478bd9Sstevel@tonic-gate f->Ftabs[0] = f->Fdel = f->Fmov = f->Ffill = 0; 29957c478bd9Sstevel@tonic-gate f->Flim = 0; 29967c478bd9Sstevel@tonic-gate } 29977c478bd9Sstevel@tonic-gate 29987c478bd9Sstevel@tonic-gate 29997c478bd9Sstevel@tonic-gate static int 30007c478bd9Sstevel@tonic-gate lenchk(char line[], struct Fspec *f) 30017c478bd9Sstevel@tonic-gate { 30027c478bd9Sstevel@tonic-gate char *l, *t; 30037c478bd9Sstevel@tonic-gate int b; 30047c478bd9Sstevel@tonic-gate 30057c478bd9Sstevel@tonic-gate l = line - 1; 30067c478bd9Sstevel@tonic-gate b = 1; 30077c478bd9Sstevel@tonic-gate t = f->Ftabs; 30087c478bd9Sstevel@tonic-gate 30097c478bd9Sstevel@tonic-gate while (*++l && *l != '\n' && b < 511) { 30107c478bd9Sstevel@tonic-gate if (*l == '\t') { 30117c478bd9Sstevel@tonic-gate while (*t && b >= *t) t++; 30127c478bd9Sstevel@tonic-gate while (++b < *t); 30137c478bd9Sstevel@tonic-gate } else b++; 30147c478bd9Sstevel@tonic-gate } 30157c478bd9Sstevel@tonic-gate 30167c478bd9Sstevel@tonic-gate if ((*l != '\0' && *l != '\n') || (f->Flim && (b-1 > (int)f->Flim))) 30177c478bd9Sstevel@tonic-gate return (-1); 30187c478bd9Sstevel@tonic-gate return (0); 30197c478bd9Sstevel@tonic-gate } 30207c478bd9Sstevel@tonic-gate #define NTABS 21 30217c478bd9Sstevel@tonic-gate 30227c478bd9Sstevel@tonic-gate 30237c478bd9Sstevel@tonic-gate /* 30247c478bd9Sstevel@tonic-gate * stdtabs: standard tabs table 30257c478bd9Sstevel@tonic-gate * format: option code letter(s), null, tabs, null 30267c478bd9Sstevel@tonic-gate */ 30277c478bd9Sstevel@tonic-gate 30287c478bd9Sstevel@tonic-gate static char stdtabs[] = { 30297c478bd9Sstevel@tonic-gate 'a', 0, 1, 10, 16, 36, 72, 0, /* IBM 370 Assembler */ 30307c478bd9Sstevel@tonic-gate 'a', '2', 0, 1, 10, 16, 40, 72, 0, /* IBM Assembler alternative */ 30317c478bd9Sstevel@tonic-gate 'c', 0, 1, 8, 12, 16, 20, 55, 0, /* COBOL, normal */ 30327c478bd9Sstevel@tonic-gate 'c', '2', 0, 1, 6, 10, 14, 49, 0, /* COBOL, crunched */ 30337c478bd9Sstevel@tonic-gate 'c', '3', 0, 1, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 30347c478bd9Sstevel@tonic-gate 54, 58, 62, 67, 0, 30357c478bd9Sstevel@tonic-gate 'f', 0, 1, 7, 11, 15, 19, 23, 0, /* FORTRAN */ 30367c478bd9Sstevel@tonic-gate 'p', 0, 1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 0, 30377c478bd9Sstevel@tonic-gate /* PL/I */ 30387c478bd9Sstevel@tonic-gate 's', 0, 1, 10, 55, 0, /* SNOBOL */ 30397c478bd9Sstevel@tonic-gate 'u', 0, 1, 12, 20, 44, 0, /* UNIVAC ASM */ 30407c478bd9Sstevel@tonic-gate 0 }; 30417c478bd9Sstevel@tonic-gate 30427c478bd9Sstevel@tonic-gate 30437c478bd9Sstevel@tonic-gate /* 30447c478bd9Sstevel@tonic-gate * stdtab: return tab list for any "canned" tab option. 30457c478bd9Sstevel@tonic-gate * entry: option points to null-terminated option string 30467c478bd9Sstevel@tonic-gate * tabvect points to vector to be filled in 30477c478bd9Sstevel@tonic-gate * exit: return (0) if legal, tabvect filled, ending with zero 30487c478bd9Sstevel@tonic-gate * return (-1) if unknown option 30497c478bd9Sstevel@tonic-gate */ 30507c478bd9Sstevel@tonic-gate 30517c478bd9Sstevel@tonic-gate 30527c478bd9Sstevel@tonic-gate static int 30537c478bd9Sstevel@tonic-gate stdtab(char option[], char tabvect[NTABS]) 30547c478bd9Sstevel@tonic-gate { 30557c478bd9Sstevel@tonic-gate char *scan; 30567c478bd9Sstevel@tonic-gate tabvect[0] = 0; 30577c478bd9Sstevel@tonic-gate scan = stdtabs; 30587c478bd9Sstevel@tonic-gate while (*scan) { 30597c478bd9Sstevel@tonic-gate if (strequal(&scan, option)) { 30607c478bd9Sstevel@tonic-gate strcopy(scan, tabvect); 30617c478bd9Sstevel@tonic-gate break; 30627c478bd9Sstevel@tonic-gate } else 30637c478bd9Sstevel@tonic-gate while (*scan++); /* skip over tab specs */ 30647c478bd9Sstevel@tonic-gate } 30657c478bd9Sstevel@tonic-gate 30667c478bd9Sstevel@tonic-gate /* later: look up code in /etc/something */ 30677c478bd9Sstevel@tonic-gate return (tabvect[0] ? 0 : -1); 30687c478bd9Sstevel@tonic-gate } 30697c478bd9Sstevel@tonic-gate 30707c478bd9Sstevel@tonic-gate 30717c478bd9Sstevel@tonic-gate /* 30727c478bd9Sstevel@tonic-gate * strequal: checks strings for equality 30737c478bd9Sstevel@tonic-gate * entry: scan1 points to scan pointer, str points to string 30747c478bd9Sstevel@tonic-gate * exit: return (1) if equal, return (0) if not 30757c478bd9Sstevel@tonic-gate * *scan1 is advanced to next nonzero byte after null 30767c478bd9Sstevel@tonic-gate */ 30777c478bd9Sstevel@tonic-gate 30787c478bd9Sstevel@tonic-gate 30797c478bd9Sstevel@tonic-gate static int 30807c478bd9Sstevel@tonic-gate strequal(char **scan1, char *str) 30817c478bd9Sstevel@tonic-gate { 30827c478bd9Sstevel@tonic-gate char c, *scan; 30837c478bd9Sstevel@tonic-gate scan = *scan1; 30847c478bd9Sstevel@tonic-gate while ((c = *scan++) == *str && c) str++; 30857c478bd9Sstevel@tonic-gate *scan1 = scan; 30867c478bd9Sstevel@tonic-gate if (c == 0 && *str == 0) 30877c478bd9Sstevel@tonic-gate return (1); 30887c478bd9Sstevel@tonic-gate if (c) 30897c478bd9Sstevel@tonic-gate while (*scan++); 30907c478bd9Sstevel@tonic-gate *scan1 = scan; 30917c478bd9Sstevel@tonic-gate return (0); 30927c478bd9Sstevel@tonic-gate } 30937c478bd9Sstevel@tonic-gate 30947c478bd9Sstevel@tonic-gate 30957c478bd9Sstevel@tonic-gate /* strcopy: copy source to destination */ 30967c478bd9Sstevel@tonic-gate 30977c478bd9Sstevel@tonic-gate 30987c478bd9Sstevel@tonic-gate static void 30997c478bd9Sstevel@tonic-gate strcopy(char *source, char *dest) 31007c478bd9Sstevel@tonic-gate { 31017c478bd9Sstevel@tonic-gate while (*dest++ = *source++); 31027c478bd9Sstevel@tonic-gate } 31037c478bd9Sstevel@tonic-gate 31047c478bd9Sstevel@tonic-gate 31057c478bd9Sstevel@tonic-gate /* This is called before a buffer modifying command so that the */ 31067c478bd9Sstevel@tonic-gate /* current array of line ptrs is saved in sav and dot and dol are saved */ 31077c478bd9Sstevel@tonic-gate 31087c478bd9Sstevel@tonic-gate 31097c478bd9Sstevel@tonic-gate static void 31107c478bd9Sstevel@tonic-gate save(void) { 31117c478bd9Sstevel@tonic-gate LINE i; 31127c478bd9Sstevel@tonic-gate int j; 31137c478bd9Sstevel@tonic-gate 31147c478bd9Sstevel@tonic-gate savdot = dot; 31157c478bd9Sstevel@tonic-gate savdol = dol; 31167c478bd9Sstevel@tonic-gate for (j = 0; j <= 25; j++) 31177c478bd9Sstevel@tonic-gate savnames[j] = names[j]; 31187c478bd9Sstevel@tonic-gate 31197c478bd9Sstevel@tonic-gate for (i = zero + 1; i <= dol; i++) 31207c478bd9Sstevel@tonic-gate i->sav = i->cur; 31217c478bd9Sstevel@tonic-gate initflg = 0; 31227c478bd9Sstevel@tonic-gate } 31237c478bd9Sstevel@tonic-gate 31247c478bd9Sstevel@tonic-gate 31257c478bd9Sstevel@tonic-gate /* The undo command calls this to restore the previous ptr array sav */ 31267c478bd9Sstevel@tonic-gate /* and swap with cur - dot and dol are swapped also. This allows user to */ 31277c478bd9Sstevel@tonic-gate /* undo an undo */ 31287c478bd9Sstevel@tonic-gate 31297c478bd9Sstevel@tonic-gate 31307c478bd9Sstevel@tonic-gate static void 31317c478bd9Sstevel@tonic-gate undo(void) { 31327c478bd9Sstevel@tonic-gate int j; 31337c478bd9Sstevel@tonic-gate long tmp; 31347c478bd9Sstevel@tonic-gate LINE i, tmpdot, tmpdol; 31357c478bd9Sstevel@tonic-gate 31367c478bd9Sstevel@tonic-gate tmpdot = dot; dot = savdot; savdot = tmpdot; 31377c478bd9Sstevel@tonic-gate tmpdol = dol; dol = savdol; savdol = tmpdol; 31387c478bd9Sstevel@tonic-gate /* swap arrays using the greater of dol or savdol as upper limit */ 31397c478bd9Sstevel@tonic-gate for (i = zero + 1; i <= ((dol > savdol) ? dol : savdol); i++) { 31407c478bd9Sstevel@tonic-gate tmp = i->cur; 31417c478bd9Sstevel@tonic-gate i->cur = i->sav; 31427c478bd9Sstevel@tonic-gate i->sav = tmp; 31437c478bd9Sstevel@tonic-gate } 31447c478bd9Sstevel@tonic-gate /* 31457c478bd9Sstevel@tonic-gate * If the current text lines are swapped with the 31467c478bd9Sstevel@tonic-gate * text lines in the save buffer, then swap the current 31477c478bd9Sstevel@tonic-gate * marks with those in the save area. 31487c478bd9Sstevel@tonic-gate */ 31497c478bd9Sstevel@tonic-gate 31507c478bd9Sstevel@tonic-gate for (j = 0; j <= 25; j++) { 31517c478bd9Sstevel@tonic-gate tmp = names[j]; 31527c478bd9Sstevel@tonic-gate names[j] = savnames[j]; 31537c478bd9Sstevel@tonic-gate savnames[j] = tmp; 31547c478bd9Sstevel@tonic-gate } 31557c478bd9Sstevel@tonic-gate } 31567c478bd9Sstevel@tonic-gate 31577c478bd9Sstevel@tonic-gate static wchar_t 31587c478bd9Sstevel@tonic-gate get_wchr() 31597c478bd9Sstevel@tonic-gate { 31607c478bd9Sstevel@tonic-gate wchar_t wc; 31617c478bd9Sstevel@tonic-gate char multi[MB_LEN_MAX]; 31627c478bd9Sstevel@tonic-gate 31637c478bd9Sstevel@tonic-gate if (_mbftowc(multi, &wc, getchr, &peekc) <= 0) 31647c478bd9Sstevel@tonic-gate wc = getchr(); 31657c478bd9Sstevel@tonic-gate return (wc); 31667c478bd9Sstevel@tonic-gate } 3167