1 /* $NetBSD: ex_argv.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
4 * Copyright (c) 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
31 #include "../common/common.h"
33 static int argv_alloc
__P((SCR
*, size_t));
34 static int argv_comp
__P((const void *, const void *));
35 static int argv_fexp
__P((SCR
*, EXCMD
*,
36 const CHAR_T
*, size_t, CHAR_T
*, size_t *, CHAR_T
**, size_t *, int));
37 static int argv_lexp
__P((SCR
*, EXCMD
*, const char *));
38 static int argv_sexp
__P((SCR
*, CHAR_T
**, size_t *, size_t *));
42 * Build a prototype arguments list.
44 * PUBLIC: int argv_init __P((SCR *, EXCMD *));
47 argv_init(SCR
*sp
, EXCMD
*excp
)
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_T *, size_t));
67 argv_exp0(SCR
*sp
, EXCMD
*excp
, const CHAR_T
*cmd
, size_t cmdlen
)
72 argv_alloc(sp
, cmdlen
);
73 MEMCPY(exp
->args
[exp
->argsoff
]->bp
, cmd
, cmdlen
);
74 exp
->args
[exp
->argsoff
]->bp
[cmdlen
] = '\0';
75 exp
->args
[exp
->argsoff
]->len
= cmdlen
;
77 excp
->argv
= exp
->args
;
78 excp
->argc
= exp
->argsoff
;
84 * Do file name expansion on a string, and append it to the
87 * PUBLIC: int argv_exp1 __P((SCR *, EXCMD *, const CHAR_T *, size_t, int));
90 argv_exp1(SCR
*sp
, EXCMD
*excp
, const 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
, const 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((unsigned char)*np
) ||
180 isalnum((unsigned char)*np
))
182 p
= bp
+ SHELLOFFSET
;
183 n
= len
- SHELLOFFSET
;
185 for (; n
> 0; --n
, ++p
)
186 if (strchr(mp
, *p
) != NULL
)
189 for (; n
> 0; --n
, ++p
)
190 if (!isblank((unsigned char)*p
) &&
191 !isalnum((unsigned char)*p
) && strchr(mp
, *p
) != NULL
)
196 * If we found a meta character in the string, fork a shell to expand
197 * it. Unfortunately, this is comparatively slow. Historically, it
198 * didn't matter much, since users don't enter meta characters as part
199 * of pathnames that frequently. The addition of filename completion
200 * broke that assumption because it's easy to use. As a result, lots
201 * folks have complained that the expansion code is too slow. So, we
202 * detect filename completion as a special case, and do it internally.
203 * Note that this code assumes that the <asterisk> character is the
204 * match-anything meta character. That feels safe -- if anyone writes
205 * a shell that doesn't follow that convention, I'd suggest giving them
206 * a festive hot-lead enema.
210 p
= bp
+ SHELLOFFSET
;
212 rval
= argv_exp3(sp
, excp
, p
, len
);
221 INT2CHAR(sp
, bp
+ SHELLOFFSET
,
222 STRLEN(bp
+ SHELLOFFSET
) + 1, np1
, nlen
);
224 rval
= argv_lexp(sp
, excp
, d
);
230 if (argv_sexp(sp
, &bp
, &blen
, &len
)) {
235 rval
= argv_exp3(sp
, excp
, p
, len
);
239 err
: FREE_SPACEW(sp
, bp
, blen
);
245 * Take a string and break it up into an argv, which is appended
246 * to the argument list.
248 * PUBLIC: int argv_exp3 __P((SCR *, EXCMD *, CHAR_T *, size_t));
251 argv_exp3(SCR
*sp
, EXCMD
*excp
, const CHAR_T
*cmd
, size_t cmdlen
)
259 for (exp
= EXP(sp
); cmdlen
> 0; ++exp
->argsoff
) {
260 /* Skip any leading whitespace. */
261 for (; cmdlen
> 0; --cmdlen
, ++cmd
) {
270 * Determine the length of this whitespace delimited
275 * Skip any character preceded by the user's quoting
278 for (ap
= cmd
, len
= 0; cmdlen
> 0; ++cmd
, --cmdlen
, ++len
) {
280 if (IS_ESCAPE(sp
, excp
, ch
) && cmdlen
> 1) {
283 } else if (isblank(ch
))
288 * Copy the argument into place.
296 exp
->args
[off
]->len
= len
;
297 for (p
= exp
->args
[off
]->bp
; len
> 0; --len
, *p
++ = *ap
++)
298 if (IS_ESCAPE(sp
, excp
, *ap
))
302 excp
->argv
= exp
->args
;
303 excp
->argc
= exp
->argsoff
;
305 #if defined(DEBUG) && 0
306 for (cnt
= 0; cnt
< exp
->argsoff
; ++cnt
)
307 vtrace(sp
, "arg %d: {%s}\n", cnt
, exp
->argv
[cnt
]);
314 * Do file name and bang command expansion.
317 argv_fexp(SCR
*sp
, EXCMD
*excp
, const CHAR_T
*cmd
, size_t cmdlen
, CHAR_T
*p
, size_t *lenp
, CHAR_T
**bpp
, size_t *blenp
, int is_bang
)
321 size_t blen
, len
, off
, tlen
;
326 /* Replace file name characters. */
327 for (bp
= *bpp
, blen
= *blenp
, len
= *lenp
; cmdlen
> 0; --cmdlen
, ++cmd
)
333 if (exp
->lastbcomm
== NULL
) {
335 "115|No previous command to replace \"!\"");
338 len
+= tlen
= STRLEN(exp
->lastbcomm
);
340 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
342 MEMCPY(p
, exp
->lastbcomm
, tlen
);
344 F_SET(excp
, E_MODIFY
);
347 if ((t
= sp
->frp
->name
) == NULL
) {
349 "116|No filename to substitute for %%");
355 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
357 CHAR2INT(sp
, t
, tlen
, wp
, wlen
);
360 F_SET(excp
, E_MODIFY
);
363 if ((t
= sp
->alt_name
) == NULL
) {
365 "117|No filename to substitute for #");
368 len
+= tlen
= strlen(t
);
370 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
372 CHAR2INT(sp
, t
, tlen
, wp
, wlen
);
375 F_SET(excp
, E_MODIFY
);
381 * Strip any backslashes that protected the file
382 * expansion characters.
385 (cmd
[1] == '%' || cmd
[1] == '#' || cmd
[1] == '!')) {
393 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
398 /* Nul termination. */
401 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
405 /* Return the new string length, buffer, buffer length. */
414 * Make more space for arguments.
417 argv_alloc(SCR
*sp
, size_t len
)
424 * Allocate room for another argument, always leaving
425 * enough room for an ARGS structure with a length of 0.
430 if (exp
->argscnt
== 0 || off
+ 2 >= exp
->argscnt
- 1) {
431 cnt
= exp
->argscnt
+ INCREMENT
;
432 REALLOC(sp
, exp
->args
, ARGS
**, cnt
* sizeof(ARGS
*));
433 if (exp
->args
== NULL
) {
437 memset(&exp
->args
[exp
->argscnt
], 0, INCREMENT
* sizeof(ARGS
*));
441 /* First argument. */
442 if (exp
->args
[off
] == NULL
) {
443 CALLOC(sp
, exp
->args
[off
], ARGS
*, 1, sizeof(ARGS
));
444 if (exp
->args
[off
] == NULL
)
448 /* First argument buffer. */
451 if (ap
->blen
< len
+ 1) {
453 REALLOC(sp
, ap
->bp
, CHAR_T
*, ap
->blen
* sizeof(CHAR_T
));
454 if (ap
->bp
== NULL
) {
457 F_CLR(ap
, A_ALLOCATED
);
458 mem
: msgq(sp
, M_SYSERR
, NULL
);
461 F_SET(ap
, A_ALLOCATED
);
464 /* Second argument. */
465 if (exp
->args
[++off
] == NULL
) {
466 CALLOC(sp
, exp
->args
[off
], ARGS
*, 1, sizeof(ARGS
));
467 if (exp
->args
[off
] == NULL
)
470 /* 0 length serves as end-of-argument marker. */
471 exp
->args
[off
]->len
= 0;
477 * Free up argument structures.
479 * PUBLIC: int argv_free __P((SCR *));
488 if (exp
->args
!= NULL
) {
489 for (off
= 0; off
< exp
->argscnt
; ++off
) {
490 if (exp
->args
[off
] == NULL
)
492 if (F_ISSET(exp
->args
[off
], A_ALLOCATED
))
493 free(exp
->args
[off
]->bp
);
494 free(exp
->args
[off
]);
506 * Find all file names matching the prefix and append them to the
510 argv_lexp(SCR
*sp
, EXCMD
*excp
, const char *path
)
516 size_t dlen
, len
, nlen
;
517 const char *dname
, *name
;
525 /* Set up the name and length for comparison. */
526 if ((p
= strrchr(path
, '/')) == NULL
) {
545 * We don't use the d_namlen field, it's not portable enough; we
546 * assume that d_name is nul terminated, instead.
548 if ((dirp
= opendir(dname
)) == NULL
) {
549 msgq_str(sp
, M_SYSERR
, dname
, "%s");
552 for (off
= exp
->argsoff
; (dp
= readdir(dirp
)) != NULL
;) {
554 if (dp
->d_name
[0] == '.')
556 len
= strlen(dp
->d_name
);
558 len
= strlen(dp
->d_name
);
559 if (len
< nlen
|| memcmp(dp
->d_name
, name
, nlen
))
563 /* Directory + name + slash + null. */
564 argv_alloc(sp
, dlen
+ len
+ 2);
565 n
= exp
->args
[exp
->argsoff
]->bp
;
567 CHAR2INT(sp
, dname
, dlen
, wp
, wlen
);
570 if (dlen
> 1 || dname
[0] != '/')
573 CHAR2INT(sp
, dp
->d_name
, len
+ 1, wp
, wlen
);
575 exp
->args
[exp
->argsoff
]->len
= dlen
+ len
+ 1;
577 excp
->argv
= exp
->args
;
578 excp
->argc
= exp
->argsoff
;
582 if (off
== exp
->argsoff
) {
584 * If we didn't find a match, complain that the expansion
585 * failed. We can't know for certain that's the error, but
586 * it's a good guess, and it matches historic practice.
588 msgq(sp
, M_ERR
, "304|Shell expansion failed");
591 qsort(exp
->args
+ off
, exp
->argsoff
- off
, sizeof(ARGS
*), argv_comp
);
597 * Alphabetic comparison.
600 argv_comp(const void *a
, const void *b
)
602 return (STRCMP((*(const ARGS
* const*)a
)->bp
, (*(const ARGS
* const*)b
)->bp
));
606 runcmd(SCR
*sp
, const char *sh_path
, const char *sh
, const char *np
,
611 * Do the minimal amount of work possible, the shell is going to run
612 * briefly and then exit. We sincerely hope.
614 switch (pid
= vfork()) {
615 case -1: /* Error. */
616 msgq(sp
, M_SYSERR
, "vfork");
618 case 0: /* Utility. */
619 /* Redirect stdout to the write end of the pipe. */
620 (void)dup2(std_output
[1], STDOUT_FILENO
);
622 /* Close the utility's file descriptors. */
623 (void)close(std_output
[0]);
624 (void)close(std_output
[1]);
625 (void)close(STDERR_FILENO
);
629 * Assume that all shells have -c.
631 execl(sh_path
, sh
, "-c", np
, (char *)NULL
);
632 msgq_str(sp
, M_SYSERR
, sh_path
, "118|Error: execl: %s");
634 default: /* Parent. */
635 /* Close the pipe ends the parent won't use. */
636 (void)close(std_output
[1]);
643 * Fork a shell, pipe a command through it, and read the output into
647 argv_sexp(SCR
*sp
, CHAR_T
**bpp
, size_t *blenp
, size_t *lenp
)
649 enum { SEXP_ERR
, SEXP_EXPANSION_ERR
, SEXP_OK
} rval
;
653 int ch
, std_output
[2];
655 const char *sh
, *sh_path
;
659 /* Secure means no shell access. */
660 if (O_ISSET(sp
, O_SECURE
)) {
662 "289|Shell expansions not supported when the secure edit option is set");
666 sh_path
= O_STR(sp
, O_SHELL
);
667 if ((sh
= strrchr(sh_path
, '/')) == NULL
)
672 /* Local copies of the buffer variables. */
677 * There are two different processes running through this code, named
678 * the utility (the shell) and the parent. The utility reads standard
679 * input and writes standard output and standard error output. The
680 * parent writes to the utility, reads its standard output and ignores
681 * its standard error output. Historically, the standard error output
682 * was discarded by vi, as it produces a lot of noise when file patterns
685 * The parent reads std_output[0], and the utility writes std_output[1].
688 std_output
[0] = std_output
[1] = -1;
689 if (pipe(std_output
) < 0) {
690 msgq(sp
, M_SYSERR
, "pipe");
693 if ((ifp
= fdopen(std_output
[0], "r")) == NULL
) {
694 msgq(sp
, M_SYSERR
, "fdopen");
697 INT2CHAR(sp
, bp
, STRLEN(bp
)+1, np
, nlen
);
698 pid
= runcmd(sp
, sh_path
, sh
, np
, std_output
);
703 * Copy process standard output into a buffer.
706 * Historic vi apparently discarded leading \n and \r's from
707 * the shell output stream. We don't on the grounds that any
708 * shell that does that is broken.
710 for (p
= bp
, len
= 0, ch
= EOF
;
711 (ch
= getc(ifp
)) != EOF
; *p
++ = ch
, blen
-=sizeof(CHAR_T
), ++len
)
713 ADD_SPACE_GOTOW(sp
, bp
, *blenp
, *blenp
* 2);
718 /* Delete the final newline, nul terminate the string. */
719 if (p
> bp
&& (p
[-1] == '\n' || p
[-1] == '\r')) {
725 *bpp
= bp
; /* *blenp is already updated. */
730 ioerr
: msgq_str(sp
, M_ERR
, sh
, "119|I/O error: %s");
731 alloc_err
: rval
= SEXP_ERR
;
736 * Wait for the process. If the shell process fails (e.g., "echo $q"
737 * where q wasn't a defined variable) or if the returned string has
738 * no characters or only blank characters, (e.g., "echo $5"), complain
739 * that the shell expansion failed. We can't know for certain that's
740 * the error, but it's a good guess, and it matches historic practice.
741 * This won't catch "echo foo_$5", but that's not a common error and
742 * historic vi didn't catch it either.
744 if (proc_wait(sp
, (long)pid
, sh
, 1, 0))
745 rval
= SEXP_EXPANSION_ERR
;
747 for (p
= bp
; len
; ++p
, --len
)
751 rval
= SEXP_EXPANSION_ERR
;
753 if (rval
== SEXP_EXPANSION_ERR
)
754 msgq(sp
, M_ERR
, "304|Shell expansion failed");
756 return (rval
== SEXP_OK
? 0 : 1);
757 err
: if (ifp
!= NULL
)
759 else if (std_output
[0] != -1)
760 close(std_output
[0]);
761 if (std_output
[1] != -1)
762 close(std_output
[0]);