1 /* @(#)parser2.c 8.1 (Berkeley) 6/6/93 */
2 /* $NetBSD: parser2.c,v 1.11 2009/04/14 08:50:06 lukem Exp $ */
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Edward Wang at The University of California, Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * name == 0 means we don't have a function name but
47 * want to parse the arguments anyway. flag == 0 in this case.
50 p_function(const char *name
, struct value
*v
, int flag
)
53 struct lcmd_tab
*c
= NULL
;
54 struct alias
*a
= NULL
;
55 struct lcmd_arg
*ap
; /* this arg */
56 struct lcmd_arg
*lp
= NULL
; /* list arg */
58 struct value av
[LCMD_NARG
+ 1];
62 if ((c
= lcmd_lookup(name
)))
64 else if ((a
= alias_lookup(name
)))
67 p_error("%s: No such command or alias.", name
);
71 for (vp
= av
; vp
< &av
[LCMD_NARG
+ 1]; vp
++)
80 if (token
== T_COMMA
) /* null argument */
83 if (p_expr0(&t
, flag
) < 0)
85 if (t
.v_type
== V_ERR
)
88 if (token
!= T_ASSIGN
) {
90 (c
!= NULL
&& (ap
= lp
) == NULL
&&
91 (ap
= c
->lc_arg
+ i
)->arg_name
== 0)) {
92 p_error("%s: Too many arguments.", name
);
98 if (p_convstr(&t
) < 0)
100 tmp
= t
.v_type
== V_STR
? t
.v_str
: 0;
102 if (p_expr(&t
, flag
) < 0) {
108 if (t
.v_type
== V_ERR
)
113 p_error("%s: Bad alias syntax.", name
);
116 for (ap
= c
->lc_arg
, vp
= av
;
117 ap
!= NULL
&& ap
->arg_name
!= 0 &&
118 (*ap
->arg_name
== '\0' ||
119 !str_match(tmp
, ap
->arg_name
,
123 if (ap
== NULL
|| ap
->arg_name
== 0) {
124 p_error("%s: Unknown argument \"%s\".",
135 if (ap
->arg_flags
& ARG_LIST
) {
139 if (vp
&& vp
->v_type
!= V_ERR
) {
141 p_error("%s: Argument %d (%s) duplicated.",
142 name
, (int)(vp
- av
+ 1),
145 p_error("%s: Argument %d duplicated.",
146 name
, (int)(vp
- av
+ 1));
149 } else if (t
.v_type
== V_ERR
) {
151 } else if (((ap
->arg_flags
&ARG_TYPE
) == ARG_NUM
&&
152 t
.v_type
!= V_NUM
) ||
153 ((ap
->arg_flags
&ARG_TYPE
) == ARG_STR
&&
154 t
.v_type
!= V_STR
)) {
156 p_error("%s: Argument %d (%s) type mismatch.",
157 name
, (int)(vp
- av
+ 1),
160 p_error("%s: Argument %d type mismatch.",
161 name
, (int)(vp
- av
+ 1));
170 if (token
== T_COMMA
)
178 else if (token
!= T_EOL
&& token
!= T_EOF
)
179 flag
= 0; /* look for legal follow set */
183 (*c
->lc_func
)(v
, av
);
185 if (a
->a_flags
& A_INUSE
)
186 p_error("%s: Recursive alias.", a
->a_name
);
188 a
->a_flags
|= A_INUSE
;
189 if (dolongcmd(a
->a_buf
, av
, i
) < 0)
191 a
->a_flags
&= ~A_INUSE
;
200 for (vp
= av
; vp
< &av
[LCMD_NARG
]; vp
++)
204 for (vp
= av
; vp
< &av
[LCMD_NARG
]; vp
++)
210 p_assign(const char *name
, struct value
*v
, int flag
)
214 if (p_expr(v
, flag
) < 0) {
221 if (name
&& flag
&& var_set(name
, v
) == 0) {