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
[] = "$Id: ex_argv.c,v 10.39 2003/11/05 17:11:54 skimo Exp $ (Berkeley) $Date: 2003/11/05 17:11:54 $";
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_T
*, size_t, CHAR_T
*, size_t *, CHAR_T
**, size_t *, int));
35 static int argv_lexp
__P((SCR
*, EXCMD
*, char *));
36 static int argv_sexp
__P((SCR
*, CHAR_T
**, size_t *, size_t *));
40 * Build a prototype arguments list.
42 * PUBLIC: int argv_init __P((SCR *, EXCMD *));
45 argv_init(SCR
*sp
, EXCMD
*excp
)
53 excp
->argv
= exp
->args
;
54 excp
->argc
= exp
->argsoff
;
60 * Append a string to the argument list.
62 * PUBLIC: int argv_exp0 __P((SCR *, EXCMD *, CHAR_T *, size_t));
65 argv_exp0(SCR
*sp
, EXCMD
*excp
, CHAR_T
*cmd
, size_t cmdlen
)
70 argv_alloc(sp
, cmdlen
);
71 MEMCPY(exp
->args
[exp
->argsoff
]->bp
, cmd
, cmdlen
);
72 exp
->args
[exp
->argsoff
]->bp
[cmdlen
] = '\0';
73 exp
->args
[exp
->argsoff
]->len
= cmdlen
;
75 excp
->argv
= exp
->args
;
76 excp
->argc
= exp
->argsoff
;
82 * Do file name expansion on a string, and append it to the
85 * PUBLIC: int argv_exp1 __P((SCR *, EXCMD *, CHAR_T *, size_t, int));
88 argv_exp1(SCR
*sp
, EXCMD
*excp
, CHAR_T
*cmd
, size_t cmdlen
, int is_bang
)
96 GET_SPACE_RETW(sp
, bp
, blen
, 512);
100 if (argv_fexp(sp
, excp
, cmd
, cmdlen
, bp
, &len
, &bp
, &blen
, is_bang
)) {
101 FREE_SPACEW(sp
, bp
, blen
);
105 /* If it's empty, we're done. */
107 for (p
= bp
, t
= bp
+ len
; p
< t
; ++p
)
115 (void)argv_exp0(sp
, excp
, bp
, len
);
117 ret
: FREE_SPACEW(sp
, bp
, blen
);
123 * Do file name and shell expansion on a string, and append it to
126 * PUBLIC: int argv_exp2 __P((SCR *, EXCMD *, CHAR_T *, size_t));
129 argv_exp2(SCR
*sp
, EXCMD
*excp
, CHAR_T
*cmd
, size_t cmdlen
)
136 GET_SPACE_RETW(sp
, bp
, blen
, 512);
138 #define SHELLECHO "echo "
139 #define SHELLOFFSET (sizeof(SHELLECHO) - 1)
148 #if defined(DEBUG) && 0
149 vtrace(sp
, "file_argv: {%.*s}\n", (int)cmdlen
, cmd
);
152 if (argv_fexp(sp
, excp
, cmd
, cmdlen
, p
, &len
, &bp
, &blen
, 0)) {
157 #if defined(DEBUG) && 0
158 vtrace(sp
, "before shell: %d: {%s}\n", len
, bp
);
162 * Do shell word expansion -- it's very, very hard to figure out what
163 * magic characters the user's shell expects. Historically, it was a
164 * union of v7 shell and csh meta characters. We match that practice
165 * by default, so ":read \%" tries to read a file named '%'. It would
166 * make more sense to pass any special characters through the shell,
167 * but then, if your shell was csh, the above example will behave
168 * differently in nvi than in vi. If you want to get other characters
169 * passed through to your shell, change the "meta" option.
171 * To avoid a function call per character, we do a first pass through
172 * the meta characters looking for characters that aren't expected
173 * to be there, and then we can ignore them in the user's argument.
175 if (opts_empty(sp
, O_SHELL
, 1) || opts_empty(sp
, O_SHELLMETA
, 1))
178 for (np
= mp
= O_STR(sp
, O_SHELLMETA
); *np
!= '\0'; ++np
)
179 if (isblank(*np
) || isalnum(*np
))
181 p
= bp
+ SHELLOFFSET
;
182 n
= len
- SHELLOFFSET
;
184 for (; n
> 0; --n
, ++p
)
185 if (strchr(mp
, *p
) != NULL
)
188 for (; n
> 0; --n
, ++p
)
190 !isalnum(*p
) && strchr(mp
, *p
) != NULL
)
195 * If we found a meta character in the string, fork a shell to expand
196 * it. Unfortunately, this is comparatively slow. Historically, it
197 * didn't matter much, since users don't enter meta characters as part
198 * of pathnames that frequently. The addition of filename completion
199 * broke that assumption because it's easy to use. As a result, lots
200 * folks have complained that the expansion code is too slow. So, we
201 * detect filename completion as a special case, and do it internally.
202 * Note that this code assumes that the <asterisk> character is the
203 * match-anything meta character. That feels safe -- if anyone writes
204 * a shell that doesn't follow that convention, I'd suggest giving them
205 * a festive hot-lead enema.
209 p
= bp
+ SHELLOFFSET
;
211 rval
= argv_exp3(sp
, excp
, p
, len
);
219 INT2CHAR(sp
, bp
+ SHELLOFFSET
,
220 STRLEN(bp
+ SHELLOFFSET
) + 1, np
, nlen
);
222 rval
= argv_lexp(sp
, excp
, d
);
228 if (argv_sexp(sp
, &bp
, &blen
, &len
)) {
233 rval
= argv_exp3(sp
, excp
, p
, len
);
237 err
: FREE_SPACEW(sp
, bp
, blen
);
243 * Take a string and break it up into an argv, which is appended
244 * to the argument list.
246 * PUBLIC: int argv_exp3 __P((SCR *, EXCMD *, CHAR_T *, size_t));
249 argv_exp3(SCR
*sp
, EXCMD
*excp
, CHAR_T
*cmd
, size_t cmdlen
)
256 for (exp
= EXP(sp
); cmdlen
> 0; ++exp
->argsoff
) {
257 /* Skip any leading whitespace. */
258 for (; cmdlen
> 0; --cmdlen
, ++cmd
) {
267 * Determine the length of this whitespace delimited
272 * Skip any character preceded by the user's quoting
275 for (ap
= cmd
, len
= 0; cmdlen
> 0; ++cmd
, --cmdlen
, ++len
) {
277 if (IS_ESCAPE(sp
, excp
, ch
) && cmdlen
> 1) {
280 } else if (isblank(ch
))
285 * Copy the argument into place.
293 exp
->args
[off
]->len
= len
;
294 for (p
= exp
->args
[off
]->bp
; len
> 0; --len
, *p
++ = *ap
++)
295 if (IS_ESCAPE(sp
, excp
, *ap
))
299 excp
->argv
= exp
->args
;
300 excp
->argc
= exp
->argsoff
;
302 #if defined(DEBUG) && 0
303 for (cnt
= 0; cnt
< exp
->argsoff
; ++cnt
)
304 vtrace(sp
, "arg %d: {%s}\n", cnt
, exp
->argv
[cnt
]);
311 * Do file name and bang command expansion.
314 argv_fexp(SCR
*sp
, EXCMD
*excp
, CHAR_T
*cmd
, size_t cmdlen
, CHAR_T
*p
, size_t *lenp
, CHAR_T
**bpp
, size_t *blenp
, int is_bang
)
318 size_t blen
, len
, off
, tlen
;
323 /* Replace file name characters. */
324 for (bp
= *bpp
, blen
= *blenp
, len
= *lenp
; cmdlen
> 0; --cmdlen
, ++cmd
)
330 if (exp
->lastbcomm
== NULL
) {
332 "115|No previous command to replace \"!\"");
335 len
+= tlen
= STRLEN(exp
->lastbcomm
);
337 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
339 MEMCPY(p
, exp
->lastbcomm
, tlen
);
341 F_SET(excp
, E_MODIFY
);
344 if ((t
= sp
->frp
->name
) == NULL
) {
346 "116|No filename to substitute for %%");
352 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
354 CHAR2INT(sp
, t
, tlen
, wp
, wlen
);
357 F_SET(excp
, E_MODIFY
);
360 if ((t
= sp
->alt_name
) == NULL
) {
362 "117|No filename to substitute for #");
365 len
+= tlen
= strlen(t
);
367 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
369 CHAR2INT(sp
, t
, tlen
, wp
, wlen
);
372 F_SET(excp
, E_MODIFY
);
378 * Strip any backslashes that protected the file
379 * expansion characters.
382 (cmd
[1] == '%' || cmd
[1] == '#' || cmd
[1] == '!')) {
390 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
395 /* Nul termination. */
398 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
402 /* Return the new string length, buffer, buffer length. */
411 * Make more space for arguments.
414 argv_alloc(SCR
*sp
, size_t len
)
421 * Allocate room for another argument, always leaving
422 * enough room for an ARGS structure with a length of 0.
427 if (exp
->argscnt
== 0 || off
+ 2 >= exp
->argscnt
- 1) {
428 cnt
= exp
->argscnt
+ INCREMENT
;
429 REALLOC(sp
, exp
->args
, ARGS
**, cnt
* sizeof(ARGS
*));
430 if (exp
->args
== NULL
) {
434 memset(&exp
->args
[exp
->argscnt
], 0, INCREMENT
* sizeof(ARGS
*));
438 /* First argument. */
439 if (exp
->args
[off
] == NULL
) {
440 CALLOC(sp
, exp
->args
[off
], ARGS
*, 1, sizeof(ARGS
));
441 if (exp
->args
[off
] == NULL
)
445 /* First argument buffer. */
448 if (ap
->blen
< len
+ 1) {
450 REALLOC(sp
, ap
->bp
, CHAR_T
*, ap
->blen
* sizeof(CHAR_T
));
451 if (ap
->bp
== NULL
) {
454 F_CLR(ap
, A_ALLOCATED
);
455 mem
: msgq(sp
, M_SYSERR
, NULL
);
458 F_SET(ap
, A_ALLOCATED
);
461 /* Second argument. */
462 if (exp
->args
[++off
] == NULL
) {
463 CALLOC(sp
, exp
->args
[off
], ARGS
*, 1, sizeof(ARGS
));
464 if (exp
->args
[off
] == NULL
)
467 /* 0 length serves as end-of-argument marker. */
468 exp
->args
[off
]->len
= 0;
474 * Free up argument structures.
476 * PUBLIC: int argv_free __P((SCR *));
485 if (exp
->args
!= NULL
) {
486 for (off
= 0; off
< exp
->argscnt
; ++off
) {
487 if (exp
->args
[off
] == NULL
)
489 if (F_ISSET(exp
->args
[off
], A_ALLOCATED
))
490 free(exp
->args
[off
]->bp
);
491 free(exp
->args
[off
]);
503 * Find all file names matching the prefix and append them to the
507 argv_lexp(SCR
*sp
, EXCMD
*excp
, char *path
)
513 size_t dlen
, len
, nlen
;
522 /* Set up the name and length for comparison. */
523 if ((p
= strrchr(path
, '/')) == NULL
) {
542 * We don't use the d_namlen field, it's not portable enough; we
543 * assume that d_name is nul terminated, instead.
545 if ((dirp
= opendir(dname
)) == NULL
) {
546 msgq_str(sp
, M_SYSERR
, dname
, "%s");
549 for (off
= exp
->argsoff
; (dp
= readdir(dirp
)) != NULL
;) {
551 if (dp
->d_name
[0] == '.')
553 len
= strlen(dp
->d_name
);
555 len
= strlen(dp
->d_name
);
556 if (len
< nlen
|| memcmp(dp
->d_name
, name
, nlen
))
560 /* Directory + name + slash + null. */
561 argv_alloc(sp
, dlen
+ len
+ 2);
562 n
= exp
->args
[exp
->argsoff
]->bp
;
564 CHAR2INT(sp
, dname
, dlen
, wp
, wlen
);
567 if (dlen
> 1 || dname
[0] != '/')
570 CHAR2INT(sp
, dp
->d_name
, len
+ 1, wp
, wlen
);
572 exp
->args
[exp
->argsoff
]->len
= dlen
+ len
+ 1;
574 excp
->argv
= exp
->args
;
575 excp
->argc
= exp
->argsoff
;
579 if (off
== exp
->argsoff
) {
581 * If we didn't find a match, complain that the expansion
582 * failed. We can't know for certain that's the error, but
583 * it's a good guess, and it matches historic practice.
585 msgq(sp
, M_ERR
, "304|Shell expansion failed");
588 qsort(exp
->args
+ off
, exp
->argsoff
- off
, sizeof(ARGS
*), argv_comp
);
594 * Alphabetic comparison.
597 argv_comp(const void *a
, const void *b
)
599 return (STRCMP((*(ARGS
**)a
)->bp
, (*(ARGS
**)b
)->bp
));
604 * Fork a shell, pipe a command through it, and read the output into
608 argv_sexp(SCR
*sp
, CHAR_T
**bpp
, size_t *blenp
, size_t *lenp
)
610 enum { SEXP_ERR
, SEXP_EXPANSION_ERR
, SEXP_OK
} rval
;
614 int ch
, std_output
[2];
620 /* Secure means no shell access. */
621 if (O_ISSET(sp
, O_SECURE
)) {
623 "289|Shell expansions not supported when the secure edit option is set");
627 sh_path
= O_STR(sp
, O_SHELL
);
628 if ((sh
= strrchr(sh_path
, '/')) == NULL
)
633 /* Local copies of the buffer variables. */
638 * There are two different processes running through this code, named
639 * the utility (the shell) and the parent. The utility reads standard
640 * input and writes standard output and standard error output. The
641 * parent writes to the utility, reads its standard output and ignores
642 * its standard error output. Historically, the standard error output
643 * was discarded by vi, as it produces a lot of noise when file patterns
646 * The parent reads std_output[0], and the utility writes std_output[1].
649 std_output
[0] = std_output
[1] = -1;
650 if (pipe(std_output
) < 0) {
651 msgq(sp
, M_SYSERR
, "pipe");
654 if ((ifp
= fdopen(std_output
[0], "r")) == NULL
) {
655 msgq(sp
, M_SYSERR
, "fdopen");
660 * Do the minimal amount of work possible, the shell is going to run
661 * briefly and then exit. We sincerely hope.
663 switch (pid
= vfork()) {
664 case -1: /* Error. */
665 msgq(sp
, M_SYSERR
, "vfork");
666 err
: if (ifp
!= NULL
)
668 else if (std_output
[0] != -1)
669 close(std_output
[0]);
670 if (std_output
[1] != -1)
671 close(std_output
[0]);
673 case 0: /* Utility. */
674 /* Redirect stdout to the write end of the pipe. */
675 (void)dup2(std_output
[1], STDOUT_FILENO
);
677 /* Close the utility's file descriptors. */
678 (void)close(std_output
[0]);
679 (void)close(std_output
[1]);
680 (void)close(STDERR_FILENO
);
684 * Assume that all shells have -c.
686 INT2CHAR(sp
, bp
, STRLEN(bp
)+1, np
, nlen
);
687 execl(sh_path
, sh
, "-c", np
, (char *)NULL
);
688 msgq_str(sp
, M_SYSERR
, sh_path
, "118|Error: execl: %s");
690 default: /* Parent. */
691 /* Close the pipe ends the parent won't use. */
692 (void)close(std_output
[1]);
697 * Copy process standard output into a buffer.
700 * Historic vi apparently discarded leading \n and \r's from
701 * the shell output stream. We don't on the grounds that any
702 * shell that does that is broken.
704 for (p
= bp
, len
= 0, ch
= EOF
;
705 (ch
= getc(ifp
)) != EOF
; *p
++ = ch
, blen
-=sizeof(CHAR_T
), ++len
)
707 ADD_SPACE_GOTOW(sp
, bp
, *blenp
, *blenp
* 2);
712 /* Delete the final newline, nul terminate the string. */
713 if (p
> bp
&& (p
[-1] == '\n' || p
[-1] == '\r')) {
719 *bpp
= bp
; /* *blenp is already updated. */
724 ioerr
: msgq_str(sp
, M_ERR
, sh
, "119|I/O error: %s");
725 alloc_err
: rval
= SEXP_ERR
;
730 * Wait for the process. If the shell process fails (e.g., "echo $q"
731 * where q wasn't a defined variable) or if the returned string has
732 * no characters or only blank characters, (e.g., "echo $5"), complain
733 * that the shell expansion failed. We can't know for certain that's
734 * the error, but it's a good guess, and it matches historic practice.
735 * This won't catch "echo foo_$5", but that's not a common error and
736 * historic vi didn't catch it either.
738 if (proc_wait(sp
, (long)pid
, sh
, 1, 0))
739 rval
= SEXP_EXPANSION_ERR
;
741 for (p
= bp
; len
; ++p
, --len
)
745 rval
= SEXP_EXPANSION_ERR
;
747 if (rval
== SEXP_EXPANSION_ERR
)
748 msgq(sp
, M_ERR
, "304|Shell expansion failed");
750 return (rval
== SEXP_OK
? 0 : 1);