1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1982-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * David Korn <dgk@research.att.com> *
19 ***********************************************************************/
22 * command [-pvVx] name [arg...]
23 * whence [-afvp] name...
45 static int whence(Shell_t
*,char**, int);
48 * command is called with argc==0 when checking for -V or -v option
49 * In this case return 0 when -v or -V or unknown option, otherwise
50 * the shift count to the command is returned
52 int b_command(register int argc
,char *argv
[],void *extra
)
54 register int n
, flags
=0;
55 register Shell_t
*shp
= ((Shbltin_t
*)extra
)->shp
;
56 opt_info
.index
= opt_info
.offset
= 0;
57 while((n
= optget(argv
,sh_optcommand
))) switch(n
)
60 if(sh_isoption(SH_RESTRICTED
))
61 errormsg(SH_DICT
,ERROR_exit(1),e_restricted
,"-p");
62 sh_onstate(SH_DEFPATH
);
76 errormsg(SH_DICT
,2, "%s", opt_info
.arg
);
81 errormsg(SH_DICT
,ERROR_usage(2), "%s", opt_info
.arg
);
85 return(flags
?0:opt_info
.index
);
86 argv
+= opt_info
.index
;
87 if(error_info
.errors
|| !*argv
)
88 errormsg(SH_DICT
,ERROR_usage(2),"%s", optusage((char*)0));
89 return(whence(shp
,argv
, flags
));
93 * for the whence command
95 int b_whence(int argc
,char *argv
[],void *extra
)
97 register int flags
=0, n
;
98 register Shell_t
*shp
= ((Shbltin_t
*)extra
)->shp
;
102 while((n
= optget(argv
,sh_optwhence
))) switch(n
)
121 errormsg(SH_DICT
,2, "%s", opt_info
.arg
);
124 errormsg(SH_DICT
,ERROR_usage(2), "%s", opt_info
.arg
);
127 argv
+= opt_info
.index
;
128 if(error_info
.errors
|| !*argv
)
129 errormsg(SH_DICT
,ERROR_usage(2),optusage((char*)0));
130 return(whence(shp
, argv
, flags
));
133 static int whence(Shell_t
*shp
,char **argv
, register int flags
)
135 register const char *name
;
136 register Namval_t
*np
;
137 register const char *cp
;
138 register int aflag
,r
=0;
139 register const char *msg
;
151 aflag
= ((flags
&A_FLAG
)!=0);
158 /* reserved words first */
159 if(sh_lookup(name
,shtab_reserved
))
161 sfprintf(sfstdout
,"%s%s\n",name
,(flags
&V_FLAG
)?sh_translate(is_reserved
):"");
166 /* non-tracked aliases */
167 if((np
=nv_search(name
,shp
->alias_tree
,0))
168 && !nv_isnull(np
) && !(notrack
=nv_isattr(np
,NV_TAGGED
))
169 && (cp
=nv_getval(np
)))
173 if(nv_isattr(np
,NV_EXPORT
))
174 msg
= sh_translate(is_xalias
);
176 msg
= sh_translate(is_alias
);
177 sfprintf(sfstdout
,msg
,name
);
179 sfputr(sfstdout
,sh_fmtq(cp
),'\n');
185 /* built-ins and functions next */
187 root
= (flags
&F_FLAG
)?shp
->bltin_tree
:shp
->fun_tree
;
188 if(np
= nv_bfsearch(name
, root
, &nq
, ¬used
))
190 if(is_abuiltin(np
) && nv_isnull(np
))
196 cp
= sh_translate(is_ufunction
);
197 else if(is_abuiltin(np
))
199 if(nv_isattr(np
,BLT_SPC
))
200 cp
= sh_translate(is_spcbuiltin
);
202 cp
= sh_translate(is_builtin
);
205 cp
= sh_translate(is_function
);
209 sfprintf(sfstdout
,"%s%s\n",name
,cp
);
216 if(sh_isstate(SH_DEFPATH
))
223 if(path_search(name
,&pp
,2+(aflag
>1)))
227 cp
= stakptr(PATH_OFFSET
);
232 cp
= path_fullname(cp
);
244 if(!np
&& (np
=nv_search(name
,shp
->track_tree
,0)))
245 sfprintf(sfstdout
,"%s %s %s/%s\n",name
,sh_translate(is_talias
),path_pwd(0),cp
);
246 else if(!np
|| nv_isnull(np
))
247 sfprintf(sfstdout
,"%s%s\n",name
,sh_translate(is_ufunction
));
250 sfputr(sfstdout
,sh_fmtq(name
),' ');
251 /* built-in version of program */
252 if(*cp
=='/' && (np
=nv_search(cp
,shp
->bltin_tree
,0)))
253 msg
= sh_translate(is_builtver
);
254 /* tracked aliases next */
255 else if(aflag
>1 || !notrack
|| strchr(name
,'/'))
256 msg
= sh_translate("is");
258 msg
= sh_translate(is_talias
);
259 sfputr(sfstdout
,msg
,' ');
261 sfputr(sfstdout
,sh_fmtq(cp
),'\n');
278 errormsg(SH_DICT
,ERROR_exit(0),e_found
,sh_fmtq(name
));