1 /* $OpenBSD: ex_bang.c,v 1.4 2001/01/29 01:58:42 niklas Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1992, 1993, 1994, 1995, 1996 7 * Keith Bostic. All rights reserved. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12 #include "config.h" 13 14 #ifndef lint 15 static const char sccsid[] = "@(#)ex_bang.c 10.33 (Berkeley) 9/23/96"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <sys/queue.h> 20 #include <sys/time.h> 21 22 #include <bitstring.h> 23 #include <errno.h> 24 #include <limits.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #include "../common/common.h" 31 #include "../vi/vi.h" 32 33 /* 34 * ex_bang -- :[line [,line]] ! command 35 * 36 * Pass the rest of the line after the ! character to the program named by 37 * the O_SHELL option. 38 * 39 * Historical vi did NOT do shell expansion on the arguments before passing 40 * them, only file name expansion. This means that the O_SHELL program got 41 * "$t" as an argument if that is what the user entered. Also, there's a 42 * special expansion done for the bang command. Any exclamation points in 43 * the user's argument are replaced by the last, expanded ! command. 44 * 45 * There's some fairly amazing slop in this routine to make the different 46 * ways of getting here display the right things. It took a long time to 47 * get it right (wrong?), so be careful. 48 * 49 * PUBLIC: int ex_bang __P((SCR *, EXCMD *)); 50 */ 51 int 52 ex_bang(sp, cmdp) 53 SCR *sp; 54 EXCMD *cmdp; 55 { 56 enum filtertype ftype; 57 ARGS *ap; 58 EX_PRIVATE *exp; 59 MARK rm; 60 recno_t lno; 61 int rval; 62 const char *msg; 63 64 ap = cmdp->argv[0]; 65 if (ap->len == 0) { 66 ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE); 67 return (1); 68 } 69 70 /* Set the "last bang command" remembered value. */ 71 exp = EXP(sp); 72 if (exp->lastbcomm != NULL) 73 free(exp->lastbcomm); 74 if ((exp->lastbcomm = strdup(ap->bp)) == NULL) { 75 msgq(sp, M_SYSERR, NULL); 76 return (1); 77 } 78 79 /* 80 * If the command was modified by the expansion, it was historically 81 * redisplayed. 82 */ 83 if (F_ISSET(cmdp, E_MODIFY) && !F_ISSET(sp, SC_EX_SILENT)) { 84 /* 85 * Display the command if modified. Historic ex/vi displayed 86 * the command if it was modified due to file name and/or bang 87 * expansion. If piping lines in vi, it would be immediately 88 * overwritten by any error or line change reporting. 89 */ 90 if (F_ISSET(sp, SC_VI)) 91 vs_update(sp, "!", ap->bp); 92 else { 93 (void)ex_printf(sp, "!%s\n", ap->bp); 94 (void)ex_fflush(sp); 95 } 96 } 97 98 /* 99 * If no addresses were specified, run the command. If there's an 100 * underlying file, it's been modified and autowrite is set, write 101 * the file back. If the file has been modified, autowrite is not 102 * set and the warn option is set, tell the user about the file. 103 */ 104 if (cmdp->addrcnt == 0) { 105 msg = NULL; 106 if (sp->ep != NULL && F_ISSET(sp->ep, F_MODIFIED)) 107 if (O_ISSET(sp, O_AUTOWRITE)) { 108 if (file_aw(sp, FS_ALL)) 109 return (0); 110 } else if (O_ISSET(sp, O_WARN) && 111 !F_ISSET(sp, SC_EX_SILENT)) 112 msg = msg_cat(sp, 113 "303|File modified since last write.", 114 NULL); 115 116 /* If we're still in a vi screen, move out explicitly. */ 117 (void)ex_exec_proc(sp, 118 cmdp, ap->bp, msg, !F_ISSET(sp, SC_EX | SC_SCR_EXWROTE)); 119 } 120 121 /* 122 * If addresses were specified, pipe lines from the file through the 123 * command. 124 * 125 * Historically, vi lines were replaced by both the stdout and stderr 126 * lines of the command, but ex lines by only the stdout lines. This 127 * makes no sense to me, so nvi makes it consistent for both, and 128 * matches vi's historic behavior. 129 */ 130 else { 131 NEEDFILE(sp, cmdp); 132 133 /* Autoprint is set historically, even if the command fails. */ 134 F_SET(cmdp, E_AUTOPRINT); 135 136 /* 137 * !!! 138 * Historical vi permitted "!!" in an empty file. When this 139 * happens, we arrive here with two addresses of 1,1 and a 140 * bad attitude. The simple solution is to turn it into a 141 * FILTER_READ operation, with the exception that stdin isn't 142 * opened for the utility, and the cursor position isn't the 143 * same. The only historic glitch (I think) is that we don't 144 * put an empty line into the default cut buffer, as historic 145 * vi did. Imagine, if you can, my disappointment. 146 */ 147 ftype = FILTER_BANG; 148 if (cmdp->addr1.lno == 1 && cmdp->addr2.lno == 1) { 149 if (db_last(sp, &lno)) 150 return (1); 151 if (lno == 0) { 152 cmdp->addr1.lno = cmdp->addr2.lno = 0; 153 ftype = FILTER_RBANG; 154 } 155 } 156 rval = ex_filter(sp, cmdp, 157 &cmdp->addr1, &cmdp->addr2, &rm, ap->bp, ftype); 158 159 /* 160 * If in vi mode, move to the first nonblank. 161 * 162 * !!! 163 * Historic vi wasn't consistent in this area -- if you used 164 * a forward motion it moved to the first nonblank, but if you 165 * did a backward motion it didn't. And, if you followed a 166 * backward motion with a forward motion, it wouldn't move to 167 * the nonblank for either. Going to the nonblank generally 168 * seems more useful and consistent, so we do it. 169 */ 170 sp->lno = rm.lno; 171 if (F_ISSET(sp, SC_VI)) { 172 sp->cno = 0; 173 (void)nonblank(sp, sp->lno, &sp->cno); 174 } else 175 sp->cno = rm.cno; 176 } 177 178 /* Ex terminates with a bang, even if the command fails. */ 179 if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT)) 180 (void)ex_puts(sp, "!\n"); 181 182 /* 183 * XXX 184 * The ! commands never return an error, so that autoprint always 185 * happens in the ex parser. 186 */ 187 return (0); 188 } 189