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