2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid[] = "@(#)ex_argv.c 10.26 (Berkeley) 9/20/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
29 #include "../common/common.h"
31 static int argv_alloc __P((SCR *, size_t));
32 static int argv_comp __P((const void *, const void *));
33 static int argv_fexp __P((SCR *, EXCMD *,
34 char *, size_t, char *, size_t *, char **, size_t *, int));
35 static int argv_lexp __P((SCR *, EXCMD *, char *));
36 static int argv_sexp __P((SCR *, char **, size_t *, size_t *));
40 * Build a prototype arguments list.
42 * PUBLIC: int argv_init __P((SCR *, EXCMD *));
55 excp->argv = exp->args;
56 excp->argc = exp->argsoff;
62 * Append a string to the argument list.
64 * PUBLIC: int argv_exp0 __P((SCR *, EXCMD *, char *, size_t));
67 argv_exp0(sp, excp, cmd, cmdlen)
76 argv_alloc(sp, cmdlen);
77 memcpy(exp->args[exp->argsoff]->bp, cmd, cmdlen);
78 exp->args[exp->argsoff]->bp[cmdlen] = '\0';
79 exp->args[exp->argsoff]->len = cmdlen;
81 excp->argv = exp->args;
82 excp->argc = exp->argsoff;
88 * Do file name expansion on a string, and append it to the
91 * PUBLIC: int argv_exp1 __P((SCR *, EXCMD *, char *, size_t, int));
94 argv_exp1(sp, excp, cmd, cmdlen, is_bang)
105 GET_SPACE_RET(sp, bp, blen, 512);
109 if (argv_fexp(sp, excp, cmd, cmdlen, bp, &len, &bp, &blen, is_bang)) {
110 FREE_SPACE(sp, bp, blen);
114 /* If it's empty, we're done. */
116 for (p = bp, t = bp + len; p < t; ++p)
124 (void)argv_exp0(sp, excp, bp, len);
126 ret: FREE_SPACE(sp, bp, blen);
132 * Do file name and shell expansion on a string, and append it to
135 * PUBLIC: int argv_exp2 __P((SCR *, EXCMD *, char *, size_t));
138 argv_exp2(sp, excp, cmd, cmdlen)
148 GET_SPACE_RET(sp, bp, blen, 512);
150 #define SHELLECHO "echo "
151 #define SHELLOFFSET (sizeof(SHELLECHO) - 1)
152 memcpy(bp, SHELLECHO, SHELLOFFSET);
153 p = bp + SHELLOFFSET;
156 #if defined(DEBUG) && 0
157 TRACE(sp, "file_argv: {%.*s}\n", (int)cmdlen, cmd);
160 if (argv_fexp(sp, excp, cmd, cmdlen, p, &len, &bp, &blen, 0)) {
165 #if defined(DEBUG) && 0
166 TRACE(sp, "before shell: %d: {%s}\n", len, bp);
170 * Do shell word expansion -- it's very, very hard to figure out what
171 * magic characters the user's shell expects. Historically, it was a
172 * union of v7 shell and csh meta characters. We match that practice
173 * by default, so ":read \%" tries to read a file named '%'. It would
174 * make more sense to pass any special characters through the shell,
175 * but then, if your shell was csh, the above example will behave
176 * differently in nvi than in vi. If you want to get other characters
177 * passed through to your shell, change the "meta" option.
179 * To avoid a function call per character, we do a first pass through
180 * the meta characters looking for characters that aren't expected
181 * to be there, and then we can ignore them in the user's argument.
183 if (opts_empty(sp, O_SHELL, 1) || opts_empty(sp, O_SHELLMETA, 1))
186 for (p = mp = O_STR(sp, O_SHELLMETA); *p != '\0'; ++p)
187 if (isblank(*p) || isalnum(*p))
189 p = bp + SHELLOFFSET;
190 n = len - SHELLOFFSET;
192 for (; n > 0; --n, ++p)
193 if (strchr(mp, *p) != NULL)
196 for (; n > 0; --n, ++p)
198 !isalnum(*p) && strchr(mp, *p) != NULL)
203 * If we found a meta character in the string, fork a shell to expand
204 * it. Unfortunately, this is comparatively slow. Historically, it
205 * didn't matter much, since users don't enter meta characters as part
206 * of pathnames that frequently. The addition of filename completion
207 * broke that assumption because it's easy to use. As a result, lots
208 * folks have complained that the expansion code is too slow. So, we
209 * detect filename completion as a special case, and do it internally.
210 * Note that this code assumes that the <asterisk> character is the
211 * match-anything meta character. That feels safe -- if anyone writes
212 * a shell that doesn't follow that convention, I'd suggest giving them
213 * a festive hot-lead enema.
217 p = bp + SHELLOFFSET;
219 rval = argv_exp3(sp, excp, p, len);
224 rval = argv_lexp(sp, excp, bp + SHELLOFFSET);
229 if (argv_sexp(sp, &bp, &blen, &len)) {
234 rval = argv_exp3(sp, excp, p, len);
238 err: FREE_SPACE(sp, bp, blen);
244 * Take a string and break it up into an argv, which is appended
245 * to the argument list.
247 * PUBLIC: int argv_exp3 __P((SCR *, EXCMD *, char *, size_t));
250 argv_exp3(sp, excp, cmd, cmdlen)
261 for (exp = EXP(sp); cmdlen > 0; ++exp->argsoff) {
262 /* Skip any leading whitespace. */
263 for (; cmdlen > 0; --cmdlen, ++cmd) {
272 * Determine the length of this whitespace delimited
277 * Skip any character preceded by the user's quoting
280 for (ap = cmd, len = 0; cmdlen > 0; ++cmd, --cmdlen, ++len) {
282 if (IS_ESCAPE(sp, excp, ch) && cmdlen > 1) {
285 } else if (isblank(ch))
290 * Copy the argument into place.
298 exp->args[off]->len = len;
299 for (p = exp->args[off]->bp; len > 0; --len, *p++ = *ap++)
300 if (IS_ESCAPE(sp, excp, *ap))
304 excp->argv = exp->args;
305 excp->argc = exp->argsoff;
307 #if defined(DEBUG) && 0
308 for (cnt = 0; cnt < exp->argsoff; ++cnt)
309 TRACE(sp, "arg %d: {%s}\n", cnt, exp->argv[cnt]);
316 * Do file name and bang command expansion.
319 argv_fexp(sp, excp, cmd, cmdlen, p, lenp, bpp, blenp, is_bang)
322 char *cmd, *p, **bpp;
323 size_t cmdlen, *lenp, *blenp;
328 size_t blen, len, off, tlen;
330 /* Replace file name characters. */
331 for (bp = *bpp, blen = *blenp, len = *lenp; cmdlen > 0; --cmdlen, ++cmd)
337 if (exp->lastbcomm == NULL) {
339 "115|No previous command to replace \"!\"");
342 len += tlen = strlen(exp->lastbcomm);
344 ADD_SPACE_RET(sp, bp, blen, len);
346 memcpy(p, exp->lastbcomm, tlen);
348 F_SET(excp, E_MODIFY);
351 if ((t = sp->frp->name) == NULL) {
353 "116|No filename to substitute for %%");
359 ADD_SPACE_RET(sp, bp, blen, len);
363 F_SET(excp, E_MODIFY);
366 if ((t = sp->alt_name) == NULL) {
368 "117|No filename to substitute for #");
371 len += tlen = strlen(t);
373 ADD_SPACE_RET(sp, bp, blen, len);
377 F_SET(excp, E_MODIFY);
383 * Strip any backslashes that protected the file
384 * expansion characters.
387 (cmd[1] == '%' || cmd[1] == '#' || cmd[1] == '!')) {
395 ADD_SPACE_RET(sp, bp, blen, len);
400 /* Nul termination. */
403 ADD_SPACE_RET(sp, bp, blen, len);
407 /* Return the new string length, buffer, buffer length. */
416 * Make more space for arguments.
428 * Allocate room for another argument, always leaving
429 * enough room for an ARGS structure with a length of 0.
434 if (exp->argscnt == 0 || off + 2 >= exp->argscnt - 1) {
435 cnt = exp->argscnt + INCREMENT;
436 REALLOC(sp, exp->args, ARGS **, cnt * sizeof(ARGS *));
437 if (exp->args == NULL) {
441 memset(&exp->args[exp->argscnt], 0, INCREMENT * sizeof(ARGS *));
445 /* First argument. */
446 if (exp->args[off] == NULL) {
447 CALLOC(sp, exp->args[off], ARGS *, 1, sizeof(ARGS));
448 if (exp->args[off] == NULL)
452 /* First argument buffer. */
455 if (ap->blen < len + 1) {
457 REALLOC(sp, ap->bp, CHAR_T *, ap->blen * sizeof(CHAR_T));
458 if (ap->bp == NULL) {
461 F_CLR(ap, A_ALLOCATED);
462 mem: msgq(sp, M_SYSERR, NULL);
465 F_SET(ap, A_ALLOCATED);
468 /* Second argument. */
469 if (exp->args[++off] == NULL) {
470 CALLOC(sp, exp->args[off], ARGS *, 1, sizeof(ARGS));
471 if (exp->args[off] == NULL)
474 /* 0 length serves as end-of-argument marker. */
475 exp->args[off]->len = 0;
481 * Free up argument structures.
483 * PUBLIC: int argv_free __P((SCR *));
493 if (exp->args != NULL) {
494 for (off = 0; off < exp->argscnt; ++off) {
495 if (exp->args[off] == NULL)
497 if (F_ISSET(exp->args[off], A_ALLOCATED))
498 free(exp->args[off]->bp);
499 free(exp->args[off]);
511 * Find all file names matching the prefix and append them to the
515 argv_lexp(sp, excp, path)
524 size_t dlen, len, nlen;
525 char *dname, *name, *p;
529 /* Set up the name and length for comparison. */
530 if ((p = strrchr(path, '/')) == NULL) {
549 * We don't use the d_namlen field, it's not portable enough; we
550 * assume that d_name is nul terminated, instead.
552 if ((dirp = opendir(dname)) == NULL) {
553 msgq_str(sp, M_SYSERR, dname, "%s");
556 for (off = exp->argsoff; (dp = readdir(dirp)) != NULL;) {
558 if (dp->d_name[0] == '.')
560 len = strlen(dp->d_name);
562 len = strlen(dp->d_name);
563 if (len < nlen || memcmp(dp->d_name, name, nlen))
567 /* Directory + name + slash + null. */
568 argv_alloc(sp, dlen + len + 2);
569 p = exp->args[exp->argsoff]->bp;
571 memcpy(p, dname, dlen);
573 if (dlen > 1 || dname[0] != '/')
576 memcpy(p, dp->d_name, len + 1);
577 exp->args[exp->argsoff]->len = dlen + len + 1;
579 excp->argv = exp->args;
580 excp->argc = exp->argsoff;
584 if (off == exp->argsoff) {
586 * If we didn't find a match, complain that the expansion
587 * failed. We can't know for certain that's the error, but
588 * it's a good guess, and it matches historic practice.
590 msgq(sp, M_ERR, "304|Shell expansion failed");
593 qsort(exp->args + off, exp->argsoff - off, sizeof(ARGS *), argv_comp);
599 * Alphabetic comparison.
605 return (strcmp((char *)(*(ARGS **)a)->bp, (char *)(*(ARGS **)b)->bp));
610 * Fork a shell, pipe a command through it, and read the output into
614 argv_sexp(sp, bpp, blenp, lenp)
617 size_t *blenp, *lenp;
619 enum { SEXP_ERR, SEXP_EXPANSION_ERR, SEXP_OK } rval;
623 int ch, std_output[2];
624 char *bp, *p, *sh, *sh_path;
626 /* Secure means no shell access. */
627 if (O_ISSET(sp, O_SECURE)) {
629 "289|Shell expansions not supported when the secure edit option is set");
633 sh_path = O_STR(sp, O_SHELL);
634 if ((sh = strrchr(sh_path, '/')) == NULL)
639 /* Local copies of the buffer variables. */
644 * There are two different processes running through this code, named
645 * the utility (the shell) and the parent. The utility reads standard
646 * input and writes standard output and standard error output. The
647 * parent writes to the utility, reads its standard output and ignores
648 * its standard error output. Historically, the standard error output
649 * was discarded by vi, as it produces a lot of noise when file patterns
652 * The parent reads std_output[0], and the utility writes std_output[1].
655 std_output[0] = std_output[1] = -1;
656 if (pipe(std_output) < 0) {
657 msgq(sp, M_SYSERR, "pipe");
660 if ((ifp = fdopen(std_output[0], "r")) == NULL) {
661 msgq(sp, M_SYSERR, "fdopen");
666 * Do the minimal amount of work possible, the shell is going to run
667 * briefly and then exit. We sincerely hope.
669 switch (pid = vfork()) {
670 case -1: /* Error. */
671 msgq(sp, M_SYSERR, "vfork");
672 err: if (ifp != NULL)
674 else if (std_output[0] != -1)
675 close(std_output[0]);
676 if (std_output[1] != -1)
677 close(std_output[0]);
679 case 0: /* Utility. */
680 /* Redirect stdout to the write end of the pipe. */
681 (void)dup2(std_output[1], STDOUT_FILENO);
683 /* Close the utility's file descriptors. */
684 (void)close(std_output[0]);
685 (void)close(std_output[1]);
686 (void)close(STDERR_FILENO);
690 * Assume that all shells have -c.
692 execl(sh_path, sh, "-c", bp, NULL);
693 msgq_str(sp, M_SYSERR, sh_path, "118|Error: execl: %s");
695 default: /* Parent. */
696 /* Close the pipe ends the parent won't use. */
697 (void)close(std_output[1]);
702 * Copy process standard output into a buffer.
705 * Historic vi apparently discarded leading \n and \r's from
706 * the shell output stream. We don't on the grounds that any
707 * shell that does that is broken.
709 for (p = bp, len = 0, ch = EOF;
710 (ch = getc(ifp)) != EOF; *p++ = ch, --blen, ++len)
712 ADD_SPACE_GOTO(sp, bp, *blenp, *blenp * 2);
717 /* Delete the final newline, nul terminate the string. */
718 if (p > bp && (p[-1] == '\n' || p[-1] == '\r')) {
724 *bpp = bp; /* *blenp is already updated. */
729 ioerr: msgq_str(sp, M_ERR, sh, "119|I/O error: %s");
730 alloc_err: rval = SEXP_ERR;
735 * Wait for the process. If the shell process fails (e.g., "echo $q"
736 * where q wasn't a defined variable) or if the returned string has
737 * no characters or only blank characters, (e.g., "echo $5"), complain
738 * that the shell expansion failed. We can't know for certain that's
739 * the error, but it's a good guess, and it matches historic practice.
740 * This won't catch "echo foo_$5", but that's not a common error and
741 * historic vi didn't catch it either.
743 if (proc_wait(sp, (long)pid, sh, 1, 0))
744 rval = SEXP_EXPANSION_ERR;
746 for (p = bp; len; ++p, --len)
750 rval = SEXP_EXPANSION_ERR;
752 if (rval == SEXP_EXPANSION_ERR)
753 msgq(sp, M_ERR, "304|Shell expansion failed");
755 return (rval == SEXP_OK ? 0 : 1);