1 /* $NetBSD: ex_argv.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */
3 * Copyright (c) 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <bitstring.h>
30 #include "../common/common.h"
32 static int argv_alloc
__P((SCR
*, size_t));
33 static int argv_comp
__P((const void *, const void *));
34 static int argv_fexp
__P((SCR
*, EXCMD
*,
35 const CHAR_T
*, size_t, CHAR_T
*, size_t *, CHAR_T
**, size_t *, int));
36 static int argv_lexp
__P((SCR
*, EXCMD
*, const char *));
37 static int argv_sexp
__P((SCR
*, CHAR_T
**, size_t *, size_t *));
41 * Build a prototype arguments list.
43 * PUBLIC: int argv_init __P((SCR *, EXCMD *));
46 argv_init(SCR
*sp
, EXCMD
*excp
)
54 excp
->argv
= exp
->args
;
55 excp
->argc
= exp
->argsoff
;
61 * Append a string to the argument list.
63 * PUBLIC: int argv_exp0 __P((SCR *, EXCMD *, const CHAR_T *, size_t));
66 argv_exp0(SCR
*sp
, EXCMD
*excp
, const CHAR_T
*cmd
, size_t cmdlen
)
71 argv_alloc(sp
, cmdlen
);
72 MEMCPY(exp
->args
[exp
->argsoff
]->bp
, cmd
, cmdlen
);
73 exp
->args
[exp
->argsoff
]->bp
[cmdlen
] = '\0';
74 exp
->args
[exp
->argsoff
]->len
= cmdlen
;
76 excp
->argv
= exp
->args
;
77 excp
->argc
= exp
->argsoff
;
83 * Do file name expansion on a string, and append it to the
86 * PUBLIC: int argv_exp1 __P((SCR *, EXCMD *, const CHAR_T *, size_t, int));
89 argv_exp1(SCR
*sp
, EXCMD
*excp
, const CHAR_T
*cmd
, size_t cmdlen
, int is_bang
)
94 GET_SPACE_RETW(sp
, bp
, blen
, 512);
97 if (argv_fexp(sp
, excp
, cmd
, cmdlen
, bp
, &len
, &bp
, &blen
, is_bang
)) {
98 FREE_SPACEW(sp
, bp
, blen
);
102 /* If it's empty, we're done. */
104 for (p
= bp
, t
= bp
+ len
; p
< t
; ++p
)
112 (void)argv_exp0(sp
, excp
, bp
, len
);
114 ret
: FREE_SPACEW(sp
, bp
, blen
);
120 * Do file name and shell expansion on a string, and append it to
123 * PUBLIC: int argv_exp2 __P((SCR *, EXCMD *, const CHAR_T *, size_t));
126 argv_exp2(SCR
*sp
, EXCMD
*excp
, const CHAR_T
*cmd
, size_t cmdlen
)
133 GET_SPACE_RETW(sp
, bp
, blen
, 512);
135 #define SHELLECHO "echo "
136 #define SHELLOFFSET (sizeof(SHELLECHO) - 1)
145 #if defined(DEBUG) && 0
146 vtrace(sp
, "file_argv: {%.*s}\n", (int)cmdlen
, cmd
);
149 if (argv_fexp(sp
, excp
, cmd
, cmdlen
, p
, &len
, &bp
, &blen
, 0)) {
154 #if defined(DEBUG) && 0
155 vtrace(sp
, "before shell: %d: {%s}\n", len
, bp
);
159 * Do shell word expansion -- it's very, very hard to figure out what
160 * magic characters the user's shell expects. Historically, it was a
161 * union of v7 shell and csh meta characters. We match that practice
162 * by default, so ":read \%" tries to read a file named '%'. It would
163 * make more sense to pass any special characters through the shell,
164 * but then, if your shell was csh, the above example will behave
165 * differently in nvi than in vi. If you want to get other characters
166 * passed through to your shell, change the "meta" option.
168 * To avoid a function call per character, we do a first pass through
169 * the meta characters looking for characters that aren't expected
170 * to be there, and then we can ignore them in the user's argument.
172 if (opts_empty(sp
, O_SHELL
, 1) || opts_empty(sp
, O_SHELLMETA
, 1))
175 for (np
= mp
= O_STR(sp
, O_SHELLMETA
); *np
!= '\0'; ++np
)
176 if (ISBLANK((UCHAR_T
)*np
) ||
177 ISALNUM((UCHAR_T
)*np
))
179 p
= bp
+ SHELLOFFSET
;
180 n
= len
- SHELLOFFSET
;
182 for (; n
> 0; --n
, ++p
)
183 if (strchr(mp
, *p
) != NULL
)
186 for (; n
> 0; --n
, ++p
)
187 if (!ISBLANK((UCHAR_T
)*p
) &&
188 !ISALNUM((UCHAR_T
)*p
) &&
189 strchr(mp
, *p
) != NULL
)
194 * If we found a meta character in the string, fork a shell to expand
195 * it. Unfortunately, this is comparatively slow. Historically, it
196 * didn't matter much, since users don't enter meta characters as part
197 * of pathnames that frequently. The addition of filename completion
198 * broke that assumption because it's easy to use. As a result, lots
199 * folks have complained that the expansion code is too slow. So, we
200 * detect filename completion as a special case, and do it internally.
201 * Note that this code assumes that the <asterisk> character is the
202 * match-anything meta character. That feels safe -- if anyone writes
203 * a shell that doesn't follow that convention, I'd suggest giving them
204 * a festive hot-lead enema.
208 p
= bp
+ SHELLOFFSET
;
210 rval
= argv_exp3(sp
, excp
, p
, len
);
219 INT2CHAR(sp
, bp
+ SHELLOFFSET
,
220 STRLEN(bp
+ SHELLOFFSET
) + 1, np1
, 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 *, const CHAR_T *, size_t));
249 argv_exp3(SCR
*sp
, EXCMD
*excp
, const CHAR_T
*cmd
, size_t cmdlen
)
258 for (exp
= EXP(sp
); cmdlen
> 0; ++exp
->argsoff
) {
259 /* Skip any leading whitespace. */
260 for (; cmdlen
> 0; --cmdlen
, ++cmd
) {
269 * Determine the length of this whitespace delimited
274 * Skip any character preceded by the user's quoting
277 for (ap
= cmd
, len
= 0; cmdlen
> 0; ++cmd
, --cmdlen
, ++len
) {
279 if (IS_ESCAPE(sp
, excp
, ch
) && cmdlen
> 1) {
282 } else if (ISBLANK(ch
))
287 * Copy the argument into place.
295 exp
->args
[off
]->len
= len
;
296 for (p
= exp
->args
[off
]->bp
; len
> 0; --len
, *p
++ = *ap
++)
297 if (IS_ESCAPE(sp
, excp
, *ap
))
301 excp
->argv
= exp
->args
;
302 excp
->argc
= exp
->argsoff
;
304 #if defined(DEBUG) && 0
305 for (cnt
= 0; cnt
< exp
->argsoff
; ++cnt
)
306 vtrace(sp
, "arg %d: {%s}\n", cnt
, exp
->argv
[cnt
]);
313 * Do file name and bang command expansion.
316 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
)
320 size_t blen
, len
, off
, tlen
;
325 /* Replace file name characters. */
326 for (bp
= *bpp
, blen
= *blenp
, len
= *lenp
; cmdlen
> 0; --cmdlen
, ++cmd
)
332 if (exp
->lastbcomm
== NULL
) {
334 "115|No previous command to replace \"!\"");
337 len
+= tlen
= STRLEN(exp
->lastbcomm
);
339 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
341 MEMCPY(p
, exp
->lastbcomm
, tlen
);
343 F_SET(excp
, E_MODIFY
);
346 if ((t
= sp
->frp
->name
) == NULL
) {
348 "116|No filename to substitute for %%");
354 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
356 CHAR2INT(sp
, t
, tlen
, wp
, wlen
);
359 F_SET(excp
, E_MODIFY
);
362 if ((t
= sp
->alt_name
) == NULL
) {
364 "117|No filename to substitute for #");
367 len
+= tlen
= strlen(t
);
369 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
371 CHAR2INT(sp
, t
, tlen
, wp
, wlen
);
374 F_SET(excp
, E_MODIFY
);
380 * Strip any backslashes that protected the file
381 * expansion characters.
384 (cmd
[1] == '%' || cmd
[1] == '#' || cmd
[1] == '!')) {
392 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
397 /* Nul termination. */
400 ADD_SPACE_RETW(sp
, bp
, blen
, len
);
404 /* Return the new string length, buffer, buffer length. */
413 * Make more space for arguments.
416 argv_alloc(SCR
*sp
, size_t len
)
423 * Allocate room for another argument, always leaving
424 * enough room for an ARGS structure with a length of 0.
429 if (exp
->argscnt
== 0 || off
+ 2 >= exp
->argscnt
- 1) {
430 cnt
= exp
->argscnt
+ INCREMENT
;
431 REALLOC(sp
, exp
->args
, ARGS
**, cnt
* sizeof(ARGS
*));
432 if (exp
->args
== NULL
) {
436 memset(&exp
->args
[exp
->argscnt
], 0, INCREMENT
* sizeof(ARGS
*));
440 /* First argument. */
441 if (exp
->args
[off
] == NULL
) {
442 CALLOC(sp
, exp
->args
[off
], ARGS
*, 1, sizeof(ARGS
));
443 if (exp
->args
[off
] == NULL
)
447 /* First argument buffer. */
450 if (ap
->blen
< len
+ 1) {
452 REALLOC(sp
, ap
->bp
, CHAR_T
*, ap
->blen
* sizeof(CHAR_T
));
453 if (ap
->bp
== NULL
) {
456 F_CLR(ap
, A_ALLOCATED
);
457 mem
: msgq(sp
, M_SYSERR
, NULL
);
460 F_SET(ap
, A_ALLOCATED
);
463 /* Second argument. */
464 if (exp
->args
[++off
] == NULL
) {
465 CALLOC(sp
, exp
->args
[off
], ARGS
*, 1, sizeof(ARGS
));
466 if (exp
->args
[off
] == NULL
)
469 /* 0 length serves as end-of-argument marker. */
470 exp
->args
[off
]->len
= 0;
476 * Free up argument structures.
478 * PUBLIC: int argv_free __P((SCR *));
487 if (exp
->args
!= NULL
) {
488 for (off
= 0; off
< exp
->argscnt
; ++off
) {
489 if (exp
->args
[off
] == NULL
)
491 if (F_ISSET(exp
->args
[off
], A_ALLOCATED
))
492 free(exp
->args
[off
]->bp
);
493 free(exp
->args
[off
]);
505 * Find all file names matching the prefix and append them to the
509 argv_lexp(SCR
*sp
, EXCMD
*excp
, const char *path
)
515 size_t dlen
, len
, nlen
;
516 const char *dname
, *name
;
524 /* Set up the name and length for comparison. */
525 if ((p
= strrchr(path
, '/')) == NULL
) {
544 * We don't use the d_namlen field, it's not portable enough; we
545 * assume that d_name is nul terminated, instead.
547 if ((dirp
= opendir(dname
)) == NULL
) {
548 msgq_str(sp
, M_SYSERR
, dname
, "%s");
551 for (off
= exp
->argsoff
; (dp
= readdir(dirp
)) != NULL
;) {
553 if (dp
->d_name
[0] == '.')
555 len
= strlen(dp
->d_name
);
557 len
= strlen(dp
->d_name
);
558 if (len
< nlen
|| memcmp(dp
->d_name
, name
, nlen
))
562 /* Directory + name + slash + null. */
563 argv_alloc(sp
, dlen
+ len
+ 2);
564 n
= exp
->args
[exp
->argsoff
]->bp
;
566 CHAR2INT(sp
, dname
, dlen
, wp
, wlen
);
569 if (dlen
> 1 || dname
[0] != '/')
572 CHAR2INT(sp
, dp
->d_name
, len
+ 1, wp
, wlen
);
574 exp
->args
[exp
->argsoff
]->len
= dlen
+ len
+ 1;
576 excp
->argv
= exp
->args
;
577 excp
->argc
= exp
->argsoff
;
581 if (off
== exp
->argsoff
) {
583 * If we didn't find a match, complain that the expansion
584 * failed. We can't know for certain that's the error, but
585 * it's a good guess, and it matches historic practice.
587 msgq(sp
, M_ERR
, "304|Shell expansion failed");
590 qsort(exp
->args
+ off
, exp
->argsoff
- off
, sizeof(ARGS
*), argv_comp
);
596 * Alphabetic comparison.
599 argv_comp(const void *a
, const void *b
)
601 return (STRCMP((*(const ARGS
* const*)a
)->bp
, (*(const ARGS
* const*)b
)->bp
));
605 runcmd(SCR
*sp
, const char *sh_path
, const char *sh
, const char *np
,
610 * Do the minimal amount of work possible, the shell is going to run
611 * briefly and then exit. We sincerely hope.
613 switch (pid
= vfork()) {
614 case -1: /* Error. */
615 msgq(sp
, M_SYSERR
, "vfork");
617 case 0: /* Utility. */
618 /* Redirect stdout to the write end of the pipe. */
619 (void)dup2(std_output
[1], STDOUT_FILENO
);
621 /* Close the utility's file descriptors. */
622 (void)close(std_output
[0]);
623 (void)close(std_output
[1]);
624 (void)close(STDERR_FILENO
);
628 * Assume that all shells have -c.
630 execl(sh_path
, sh
, "-c", np
, (char *)NULL
);
631 msgq_str(sp
, M_SYSERR
, sh_path
, "118|Error: execl: %s");
633 default: /* Parent. */
634 /* Close the pipe ends the parent won't use. */
635 (void)close(std_output
[1]);
642 * Fork a shell, pipe a command through it, and read the output into
646 argv_sexp(SCR
*sp
, CHAR_T
**bpp
, size_t *blenp
, size_t *lenp
)
648 enum { SEXP_ERR
, SEXP_EXPANSION_ERR
, SEXP_OK
} rval
;
652 int ch
, std_output
[2];
654 const char *sh
, *sh_path
;
658 /* Secure means no shell access. */
659 if (O_ISSET(sp
, O_SECURE
)) {
661 "289|Shell expansions not supported when the secure edit option is set");
665 sh_path
= O_STR(sp
, O_SHELL
);
666 if ((sh
= strrchr(sh_path
, '/')) == NULL
)
671 /* Local copies of the buffer variables. */
676 * There are two different processes running through this code, named
677 * the utility (the shell) and the parent. The utility reads standard
678 * input and writes standard output and standard error output. The
679 * parent writes to the utility, reads its standard output and ignores
680 * its standard error output. Historically, the standard error output
681 * was discarded by vi, as it produces a lot of noise when file patterns
684 * The parent reads std_output[0], and the utility writes std_output[1].
687 std_output
[0] = std_output
[1] = -1;
688 if (pipe(std_output
) < 0) {
689 msgq(sp
, M_SYSERR
, "pipe");
692 if ((ifp
= fdopen(std_output
[0], "r")) == NULL
) {
693 msgq(sp
, M_SYSERR
, "fdopen");
696 INT2CHAR(sp
, bp
, STRLEN(bp
)+1, np
, nlen
);
697 pid
= runcmd(sp
, sh_path
, sh
, np
, std_output
);
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
-=sizeof(CHAR_T
), ++len
)
712 ADD_SPACE_GOTOW(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);
756 err
: if (ifp
!= NULL
)
758 else if (std_output
[0] != -1)
759 close(std_output
[0]);
760 if (std_output
[1] != -1)
761 close(std_output
[0]);