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