1 /*- 2 * Copyright (c) 1992, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1992, 1993, 1994, 1995, 1996 5 * Keith Bostic. All rights reserved. 6 * 7 * See the LICENSE file for redistribution information. 8 */ 9 10 #include "config.h" 11 12 #include <sys/types.h> 13 #include <sys/queue.h> 14 #include <sys/stat.h> 15 16 #include <bitstring.h> 17 #include <ctype.h> 18 #include <errno.h> 19 #include <fcntl.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 28 enum which {WN, WQ, WRITE, XIT}; 29 static int exwr(SCR *, EXCMD *, enum which); 30 31 /* 32 * ex_wn -- :wn[!] [>>] [file] 33 * Write to a file and switch to the next one. 34 * 35 * PUBLIC: int ex_wn(SCR *, EXCMD *); 36 */ 37 int 38 ex_wn(SCR *sp, EXCMD *cmdp) 39 { 40 if (exwr(sp, cmdp, WN)) 41 return (1); 42 if (file_m3(sp, 0)) 43 return (1); 44 45 /* The file name isn't a new file to edit. */ 46 cmdp->argc = 0; 47 48 return (ex_next(sp, cmdp)); 49 } 50 51 /* 52 * ex_wq -- :wq[!] [>>] [file] 53 * Write to a file and quit. 54 * 55 * PUBLIC: int ex_wq(SCR *, EXCMD *); 56 */ 57 int 58 ex_wq(SCR *sp, EXCMD *cmdp) 59 { 60 int force; 61 62 if (exwr(sp, cmdp, WQ)) 63 return (1); 64 if (file_m3(sp, 0)) 65 return (1); 66 67 force = FL_ISSET(cmdp->iflags, E_C_FORCE); 68 69 if (ex_ncheck(sp, force)) 70 return (1); 71 72 F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT); 73 return (0); 74 } 75 76 /* 77 * ex_write -- :write[!] [>>] [file] 78 * :write [!] [cmd] 79 * Write to a file. 80 * 81 * PUBLIC: int ex_write(SCR *, EXCMD *); 82 */ 83 int 84 ex_write(SCR *sp, EXCMD *cmdp) 85 { 86 return (exwr(sp, cmdp, WRITE)); 87 } 88 89 90 /* 91 * ex_xit -- :x[it]! [file] 92 * Write out any modifications and quit. 93 * 94 * PUBLIC: int ex_xit(SCR *, EXCMD *); 95 */ 96 int 97 ex_xit(SCR *sp, EXCMD *cmdp) 98 { 99 int force; 100 101 NEEDFILE(sp, cmdp); 102 103 if (F_ISSET(sp->ep, F_MODIFIED) && exwr(sp, cmdp, XIT)) 104 return (1); 105 if (file_m3(sp, 0)) 106 return (1); 107 108 force = FL_ISSET(cmdp->iflags, E_C_FORCE); 109 110 if (ex_ncheck(sp, force)) 111 return (1); 112 113 F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT); 114 return (0); 115 } 116 117 /* 118 * exwr -- 119 * The guts of the ex write commands. 120 */ 121 static int 122 exwr(SCR *sp, EXCMD *cmdp, enum which cmd) 123 { 124 MARK rm; 125 int flags; 126 char *name; 127 CHAR_T *p = NULL; 128 size_t nlen; 129 char *n; 130 int rc; 131 EX_PRIVATE *exp; 132 133 NEEDFILE(sp, cmdp); 134 135 /* All write commands can have an associated '!'. */ 136 LF_INIT(FS_POSSIBLE); 137 if (FL_ISSET(cmdp->iflags, E_C_FORCE)) 138 LF_SET(FS_FORCE); 139 140 /* Skip any leading whitespace. */ 141 if (cmdp->argc != 0) 142 for (p = cmdp->argv[0]->bp; *p != '\0' && cmdskip(*p); ++p); 143 144 /* If "write !" it's a pipe to a utility. */ 145 if (cmdp->argc != 0 && cmd == WRITE && *p == '!') { 146 /* Secure means no shell access. */ 147 if (O_ISSET(sp, O_SECURE)) { 148 ex_wemsg(sp, cmdp->cmd->name, EXM_SECURE_F); 149 return (1); 150 } 151 152 /* Expand the argument. */ 153 for (++p; *p && cmdskip(*p); ++p); 154 if (*p == '\0') { 155 ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE); 156 return (1); 157 } 158 if (argv_exp1(sp, cmdp, p, STRLEN(p), 1)) 159 return (1); 160 161 /* Set the last bang command */ 162 exp = EXP(sp); 163 free(exp->lastbcomm); 164 exp->lastbcomm = v_wstrdup(sp, cmdp->argv[1]->bp, 165 cmdp->argv[1]->len); 166 167 /* 168 * Historically, vi waited after a write filter even if there 169 * wasn't any output from the command. People complained when 170 * nvi waited only if there was output, wanting the visual cue 171 * that the program hadn't written anything. 172 */ 173 F_SET(sp, SC_EX_WAIT_YES); 174 175 /* 176 * !!! 177 * Ignore the return cursor position, the cursor doesn't 178 * move. 179 */ 180 if (ex_filter(sp, cmdp, &cmdp->addr1, 181 &cmdp->addr2, &rm, cmdp->argv[1]->bp, FILTER_WRITE)) 182 return (1); 183 184 /* Ex terminates with a bang, even if the command fails. */ 185 if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT)) 186 (void)ex_puts(sp, "!\n"); 187 188 return (0); 189 } 190 191 /* Set the FS_ALL flag if we're writing the entire file. */ 192 if (cmdp->addr1.lno <= 1 && !db_exist(sp, cmdp->addr2.lno + 1)) 193 LF_SET(FS_ALL); 194 195 /* If "write >>" it's an append to a file. */ 196 if (cmdp->argc != 0 && cmd != XIT && p[0] == '>' && p[1] == '>') { 197 LF_SET(FS_APPEND); 198 199 /* Skip ">>" and whitespace. */ 200 for (p += 2; *p && cmdskip(*p); ++p); 201 } 202 203 /* If no other arguments, just write the file back. */ 204 if (cmdp->argc == 0 || *p == '\0') 205 return (file_write(sp, 206 &cmdp->addr1, &cmdp->addr2, NULL, flags)); 207 208 /* Build an argv so we get an argument count and file expansion. */ 209 if (argv_exp2(sp, cmdp, p, STRLEN(p))) 210 return (1); 211 212 /* 213 * 0 args: impossible. 214 * 1 args: impossible (I hope). 215 * 2 args: read it. 216 * >2 args: object, too many args. 217 * 218 * The 1 args case depends on the argv_sexp() function refusing 219 * to return success without at least one non-blank character. 220 */ 221 switch (cmdp->argc) { 222 case 0: 223 case 1: 224 abort(); 225 /* NOTREACHED */ 226 case 2: 227 INT2CHAR(sp, cmdp->argv[1]->bp, cmdp->argv[1]->len+1, 228 n, nlen); 229 name = v_strdup(sp, n, nlen - 1); 230 231 /* 232 * !!! 233 * Historically, the read and write commands renamed 234 * "unnamed" files, or, if the file had a name, set 235 * the alternate file name. 236 */ 237 if (F_ISSET(sp->frp, FR_TMPFILE) && 238 !F_ISSET(sp->frp, FR_EXNAMED)) { 239 if ((n = v_strdup(sp, name, nlen - 1)) != NULL) { 240 free(sp->frp->name); 241 sp->frp->name = n; 242 } 243 /* 244 * The file has a real name, it's no longer a 245 * temporary, clear the temporary file flags. 246 * 247 * !!! 248 * If we're writing the whole file, FR_NAMECHANGE 249 * will be cleared by the write routine -- this is 250 * historic practice. 251 */ 252 F_CLR(sp->frp, FR_TMPEXIT | FR_TMPFILE); 253 F_SET(sp->frp, FR_NAMECHANGE | FR_EXNAMED); 254 255 /* Notify the screen. */ 256 (void)sp->gp->scr_rename(sp, sp->frp->name, 1); 257 } else 258 set_alt_name(sp, name); 259 break; 260 default: 261 INT2CHAR(sp, p, STRLEN(p) + 1, n, nlen); 262 ex_emsg(sp, n, EXM_FILECOUNT); 263 return (1); 264 } 265 266 rc = file_write(sp, &cmdp->addr1, &cmdp->addr2, name, flags); 267 268 free(name); 269 270 return rc; 271 } 272 273 /* 274 * ex_writefp -- 275 * Write a range of lines to a FILE *. 276 * 277 * PUBLIC: int ex_writefp(SCR *, 278 * PUBLIC: char *, FILE *, MARK *, MARK *, u_long *, u_long *, int); 279 */ 280 int 281 ex_writefp(SCR *sp, char *name, FILE *fp, MARK *fm, MARK *tm, u_long *nlno, u_long *nch, int silent) 282 { 283 struct stat sb; 284 GS *gp; 285 u_long ccnt; /* XXX: can't print off_t portably. */ 286 recno_t fline, tline, lcnt; 287 size_t len; 288 int rval; 289 char *msg, *p; 290 291 gp = sp->gp; 292 fline = fm->lno; 293 tline = tm->lno; 294 295 if (nlno != NULL) { 296 *nch = 0; 297 *nlno = 0; 298 } 299 300 /* 301 * The vi filter code has multiple processes running simultaneously, 302 * and one of them calls ex_writefp(). The "unsafe" function calls 303 * in this code are to db_get() and msgq(). Db_get() is safe, see 304 * the comment in ex_filter.c:ex_filter() for details. We don't call 305 * msgq if the multiple process bit in the EXF is set. 306 * 307 * !!! 308 * Historic vi permitted files of 0 length to be written. However, 309 * since the way vi got around dealing with "empty" files was to 310 * always have a line in the file no matter what, it wrote them as 311 * files of a single, empty line. We write empty files. 312 * 313 * "Alex, I'll take vi trivia for $1000." 314 */ 315 ccnt = 0; 316 lcnt = 0; 317 msg = "253|Writing..."; 318 if (tline != 0) 319 for (; fline <= tline; ++fline, ++lcnt) { 320 /* Caller has to provide any interrupt message. */ 321 if ((lcnt + 1) % INTERRUPT_CHECK == 0) { 322 if (INTERRUPTED(sp)) 323 break; 324 if (!silent) { 325 gp->scr_busy(sp, msg, msg == NULL ? 326 BUSY_UPDATE : BUSY_ON); 327 msg = NULL; 328 } 329 } 330 if (db_rget(sp, fline, &p, &len)) 331 goto err; 332 if (fwrite(p, 1, len, fp) != len) 333 goto err; 334 ccnt += len; 335 if (putc('\n', fp) != '\n') 336 break; 337 ++ccnt; 338 } 339 340 if (fflush(fp)) 341 goto err; 342 /* 343 * XXX 344 * I don't trust NFS -- check to make sure that we're talking to 345 * a regular file and sync so that NFS is forced to flush. 346 */ 347 if (!fstat(fileno(fp), &sb) && 348 S_ISREG(sb.st_mode) && fsync(fileno(fp))) 349 goto err; 350 351 if (fclose(fp)) 352 goto err; 353 354 rval = 0; 355 if (0) { 356 err: if (!F_ISSET(sp->ep, F_MULTILOCK)) 357 msgq_str(sp, M_SYSERR, name, "%s"); 358 (void)fclose(fp); 359 rval = 1; 360 } 361 362 if (!silent) 363 gp->scr_busy(sp, NULL, BUSY_OFF); 364 365 /* Report the possibly partial transfer. */ 366 if (nlno != NULL) { 367 *nch = ccnt; 368 *nlno = lcnt; 369 } 370 return (rval); 371 } 372