1: /* Copyright (c) 1979 Regents of the University of California */ 2: #ifdef V6 3: #include <retrofit.h> 4: #endif 5: 6: /* 7: * Ex version 2 (see exact version in ex_cmds.c, search for /Version/ 8: * 9: * Mark Horton, UC Berkeley 10: * Bill Joy, UC Berkeley 11: * 12: * This file contains most of the declarations common to a large number 13: * of routines. The file ex_vis.h contains declarations 14: * which are used only inside the screen editor. 15: * The file ex_tune.h contains parameters which can be diddled per installation. 16: * 17: * The declarations relating to the argument list, regular expressions, 18: * the temporary file data structure used by the editor 19: * and the data describing terminals are each fairly substantial and 20: * are kept in the files ex_{argv,re,temp,tty}.h which 21: * we #include separately. 22: * 23: * If you are going to dig into ex, you should look at the outline of the 24: * distribution of the code into files at the beginning of ex.c and ex_v.c. 25: * Code which is similar to that of ed is lightly or undocumented in spots 26: * (e.g. the regular expression code). Newer code (e.g. open and visual) 27: * is much more carefully documented, and still rough in spots. 28: * 29: * Please forward bug reports to 30: * 31: * Mark Horton 32: * Computer Science Division, EECS 33: * EVANS HALL 34: * U.C. Berkeley 94704 35: * (415) 642-4948 36: * (415) 642-1024 (dept. office) 37: * 38: * or to csvax.mark@berkeley on the ARPA-net. I would particularly like to hear 39: * of additional terminal descriptions you add to the termcap data base. 40: */ 41: 42: #include <sys/types.h> 43: #include <ctype.h> 44: #include <errno.h> 45: #include <signal.h> 46: #include <setjmp.h> 47: #include <sys/stat.h> 48: 49: /* 50: * The following little dance copes with the new USG tty handling. 51: * This stuff has the advantage of considerable flexibility, and 52: * the disadvantage of being incompatible with anything else. 53: * The presence of the symbol USG3TTy wil indicate the new code: 54: * in this case, we define CBREAK (because we can simulate it exactly), 55: * but we won't actually use it, so we set it to a value that will 56: * probably blow the compilation if we goof up. 57: */ 58: #ifdef USG3TTY 59: #include <termio.h> 60: #define CBREAK xxxxx 61: #else 62: #include <sgtty.h> 63: #endif 64: 65: extern int errno; 66: 67: typedef short line; 68: typedef short bool; 69: 70: #include "ex_tune.h" 71: #include "ex_vars.h" 72: /* 73: * Options in the editor are referred to usually by "value(name)" where 74: * name is all uppercase, i.e. "value(PROMPT)". This is actually a macro 75: * which expands to a fixed field in a static structure and so generates 76: * very little code. The offsets for the option names in the structure 77: * are generated automagically from the structure initializing them in 78: * ex_data.c... see the shell script "makeoptions". 79: */ 80: struct option { 81: char *oname; 82: char *oabbrev; 83: short otype; /* Types -- see below */ 84: short odefault; /* Default value */ 85: short ovalue; /* Current value */ 86: char *osvalue; 87: }; 88: 89: #define ONOFF 0 90: #define NUMERIC 1 91: #define STRING 2 /* SHELL or DIRECTORY */ 92: #define OTERM 3 93: 94: #define value(a) options[a].ovalue 95: #define svalue(a) options[a].osvalue 96: 97: struct option options[NOPTS + 1]; 98: 99: 100: /* 101: * The editor does not normally use the standard i/o library. Because 102: * we expect the editor to be a heavily used program and because it 103: * does a substantial amount of input/output processing it is appropriate 104: * for it to call low level read/write primitives directly. In fact, 105: * when debugging the editor we use the standard i/o library. In any 106: * case the editor needs a printf which prints through "putchar" ala the 107: * old version 6 printf. Thus we normally steal a copy of the "printf.c" 108: * and "strout" code from the standard i/o library and mung it for our 109: * purposes to avoid dragging in the stdio library headers, etc if we 110: * are not debugging. Such a modified printf exists in "printf.c" here. 111: */ 112: #ifdef TRACE 113: # include <stdio.h> 114: FILE *trace; 115: bool trubble; 116: bool techoin; 117: char tracbuf[BUFSIZ]; 118: # undef putchar 119: # undef getchar 120: #else 121: # ifndef BUFSIZ 122: # define BUFSIZ 512 123: # endif 124: # define NULL 0 125: # define EOF -1 126: #endif 127: 128: /* 129: * Character constants and bits 130: * 131: * The editor uses the QUOTE bit as a flag to pass on with characters 132: * e.g. to the putchar routine. The editor never uses a simple char variable. 133: * Only arrays of and pointers to characters are used and parameters and 134: * registers are never declared character. 135: */ 136: #define QUOTE 0200 137: #define TRIM 0177 138: #define CTRL(c) ('c' & 037) 139: #define NL CTRL(j) 140: #define CR CTRL(m) 141: #define DELETE 0177 /* See also ATTN, QUIT in ex_tune.h */ 142: #define ESCAPE 033 143: 144: /* 145: * Miscellaneous random variables used in more than one place 146: */ 147: bool aiflag; /* Append/change/insert with autoindent */ 148: bool anymarks; /* We have used '[a-z] */ 149: short chng; /* Warn "No write" */ 150: char *Command; 151: bool die; /* We are child, go away on error */ 152: short dirtcnt; /* When >= MAXDIRT, should sync temporary */ 153: bool edited; /* Current file is [Edited] */ 154: line *endcore; /* Last available core location */ 155: bool endline; /* Last cmd mode command ended with \n */ 156: short erfile; /* Error message file unit */ 157: line *fendcore; /* First address in line pointer space */ 158: char file[FNSIZE]; /* Working file name */ 159: char genbuf[LBSIZE]; /* Working buffer when manipulating linebuf */ 160: bool hush; /* Command line option - was given, hush up! */ 161: char *globp; /* (Untyped) input string to command mode */ 162: bool holdcm; /* Don't cursor address */ 163: bool inglobal; /* Inside g//... or v//... */ 164: char *initev; /* Initial : escape for visual */ 165: bool inopen; /* Inside open or visual */ 166: char *input; /* Current position in cmd line input buffer */ 167: bool intty; /* Input is a tty */ 168: short io; /* General i/o unit (auto-closed on error!) */ 169: short lastc; /* Last character ret'd from cmd input */ 170: bool laste; /* Last command was an "e" (or "rec") */ 171: char lasttag[TAGSIZE]; /* Last argument to a tag command */ 172: char *linebp; /* Used in substituting in \n */ 173: char linebuf[LBSIZE]; /* The primary line buffer */ 174: bool listf; /* Command should run in list mode */ 175: char *loc1; /* Where re began to match (in linebuf) */ 176: char *loc2; /* First char after re match (") */ 177: line names['z'-'a'+2]; /* Mark registers a-z,' */ 178: short notecnt; /* Count for notify (to visual from cmd) */ 179: bool numberf; /* Command should run in number mode */ 180: char obuf[BUFSIZ]; /* Buffer for tty output */ 181: short oprompt; /* Saved during source */ 182: short ospeed; /* Output speed (from gtty) */ 183: short peekc; /* Peek ahead character (cmd mode input) */ 184: char *pkill[2]; /* Trim for put with ragged (LISP) delete */ 185: bool pfast; /* Have stty -nl'ed to go faster */ 186: int pid; /* Process id of child */ 187: int ppid; /* Process id of parent (e.g. main ex proc) */ 188: jmp_buf resetlab; /* For error throws to top level (cmd mode) */ 189: int rpid; /* Pid returned from wait() */ 190: bool ruptible; /* Interruptible is normal state */ 191: bool shudclob; /* Have a prompt to clobber (e.g. on ^D) */ 192: int status; /* Status returned from wait() */ 193: short tchng; /* If nonzero, then [Modified] */ 194: short tfile; /* Temporary file unit */ 195: bool vcatch; /* Want to catch an error (open/visual) */ 196: jmp_buf vreslab; /* For error throws to a visual catch */ 197: short xchng; /* Suppresses multiple "No writes" in !cmd */ 198: 199: /* 200: * Macros 201: */ 202: #define CP(a, b) (ignore(strcpy(a, b))) 203: #define ckaw() {if (chng && value(AUTOWRITE)) wop(0);} 204: #define copy(a,b,c) Copy((char *) a, (char *) b, c) 205: #define eq(a, b) ((a) && (b) && strcmp(a, b) == 0) 206: #define getexit(a) copy(a, resetlab, sizeof (jmp_buf)) 207: #define lastchar() lastc 208: #define outchar(c) (*Outchar)(c) 209: #define pastwh() (ignore(skipwh())) 210: #define pline(no) (*Pline)(no) 211: #define reset() longjmp(resetlab,1) 212: #define resexit(a) copy(resetlab, a, sizeof (jmp_buf)) 213: #define setexit() setjmp(resetlab) 214: #define setlastchar(c) lastc = c 215: #define ungetchar(c) peekc = c 216: 217: #define CATCH vcatch = 1; if (setjmp(vreslab) == 0) { 218: #define FIXUNDO ((inopen >= 0) && (inopen || !inglobal)) 219: #define ONERR } else { vcatch = 0; 220: #define ENDCATCH } vcatch = 0; 221: 222: /* 223: * Environment like memory 224: */ 225: char altfile[FNSIZE]; /* Alternate file name */ 226: char direct[32]; /* Temp file goes here */ 227: char shell[32]; /* Copied to be settable */ 228: char ttytype[16]; /* A long and pretty name */ 229: char uxb[UXBSIZE + 2]; /* Last !command for !! */ 230: 231: /* 232: * The editor data structure for accessing the current file consists 233: * of an incore array of pointers into the temporary file tfile. 234: * Each pointer is 15 bits (the low bit is used by global) and is 235: * padded with zeroes to make an index into the temp file where the 236: * actual text of the line is stored. 237: * 238: * To effect undo, copies of affected lines are saved after the last 239: * line considered to be in the buffer, between dol and unddol. 240: * During an open or visual, which uses the command mode undo between 241: * dol and unddol, a copy of the entire, pre-command buffer state 242: * is saved between unddol and truedol. 243: */ 244: line *addr1; /* First addressed line in a command */ 245: line *addr2; /* Second addressed line */ 246: line *dol; /* Last line in buffer */ 247: line *dot; /* Current line */ 248: line *one; /* First line */ 249: line *truedol; /* End of all lines, including saves */ 250: line *unddol; /* End of undo saved lines */ 251: line *zero; /* Points to empty slot before one */ 252: 253: /* 254: * Undo information 255: * 256: * For most commands we save lines changed by salting them away between 257: * dol and unddol before they are changed (i.e. we save the descriptors 258: * into the temp file tfile which is never garbage collected). The 259: * lines put here go back after unddel, and to complete the undo 260: * we delete the lines [undap1,undap2). 261: * 262: * Undoing a move is much easier and we treat this as a special case. 263: * Similarly undoing a "put" is a special case for although there 264: * are lines saved between dol and unddol we don't stick these back 265: * into the buffer. 266: */ 267: short undkind; 268: 269: line *unddel; /* Saved deleted lines go after here */ 270: line *undap1; /* Beginning of new lines */ 271: line *undap2; /* New lines end before undap2 */ 272: line *undadot; /* If we saved all lines, dot reverts here */ 273: 274: #define UNDCHANGE 0 275: #define UNDMOVE 1 276: #define UNDALL 2 277: #define UNDNONE 3 278: #define UNDPUT 4 279: 280: /* 281: * Function type definitions 282: */ 283: #define NOSTR (char *) 0 284: #define NOLINE (line *) 0 285: 286: int (*Outchar)(); 287: int (*Pline)(); 288: int (*Putchar)(); 289: int (*oldhup)(); 290: int (*setlist())(); 291: int (*setnorm())(); 292: int (*setnorm())(); 293: int (*setnumb())(); 294: line *address(); 295: char *cgoto(); 296: char *genindent(); 297: char *getblock(); 298: char *getenv(); 299: line *getmark(); 300: char *longname(); 301: char *mesg(); 302: char *place(); 303: char *plural(); 304: line *scanfor(); 305: line *setin(); 306: char *strcat(); 307: char *strcpy(); 308: char *strend(); 309: char *tailpath(); 310: char *tgetstr(); 311: char *tgoto(); 312: char *ttyname(); 313: line *vback(); 314: char *vfindcol(); 315: char *vgetline(); 316: char *vinit(); 317: char *vpastwh(); 318: char *vskipwh(); 319: int put(); 320: int putreg(); 321: int YANKreg(); 322: int delete(); 323: int execl(); 324: int filter(); 325: int getfile(); 326: int getsub(); 327: int gettty(); 328: int join(); 329: int listchar(); 330: off_t lseek(); 331: int normchar(); 332: int normline(); 333: int numbline(); 334: int (*oldquit)(); 335: int onhup(); 336: int onintr(); 337: int putch(); 338: int shift(); 339: int termchar(); 340: int vfilter(); 341: #ifdef CBREAK 342: int vintr(); 343: #endif 344: int vputch(); 345: int vshftop(); 346: int yank(); 347: 348: /* 349: * C doesn't have a (void) cast, so we have to fake it for lint's sake. 350: */ 351: #ifdef lint 352: # define ignore(a) Ignore((char *) (a)) 353: # define ignorf(a) Ignorf((int (*) ()) (a)) 354: #else 355: # define ignore(a) a 356: # define ignorf(a) a 357: #endif