2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: whence.c /main/3 1995/11/01 16:31:17 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF *
29 * AT&T BELL LABORATORIES *
30 * AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN *
31 * ACCORDANCE WITH APPLICABLE AGREEMENTS *
33 * Copyright (c) 1995 AT&T Corp. *
34 * Unpublished & Not for Publication *
35 * All Rights Reserved *
37 * The copyright notice above does not evidence any *
38 * actual or intended publication of such source code *
40 * This software was created by the *
41 * Advanced Software Technology Department *
42 * AT&T Bell Laboratories *
44 * For further information contact *
45 * {research,attmail}!dgk *
47 ***************************************************************/
49 /* : : generated by proto : : */
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__ "C"
59 #define __PROTO__(x) x
61 #define __PARAM__(n,o) n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
74 #define __PROTO__(x) ()
75 #define __OTORP__(x) x
76 #define __PARAM__(n,o) o
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__ ...
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a) va_start(p,a)
92 #define __VA_START__(p,a) va_start(p)
101 #include "builtins.h"
109 static int whence __PROTO__((char**, int));
112 * command is called with argc==0 when checking for -V or -v option
113 * In this case return 0 when -v or -V or unknown option, otherwise
114 * the shift count to the command is returned
116 int b_command __PARAM__((register int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(register int argc;char *argv[];__V_ *extra;){
117 register int n, flags=0;
119 opt_index = opt_char = 0;
120 while((n = optget(argv,sh_optcommand))) switch(n)
123 sh_onstate(SH_DEFPATH);
139 error(ERROR_usage(2), opt_arg);
143 return(flags?0:opt_index);
145 if(error_info.errors || !*argv)
146 error(ERROR_usage(2),optusage((char*)0));
147 return(whence(argv, flags));
151 * for the whence command
153 int b_whence __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
154 register int flags=0, n;
159 while((n = optget(argv,sh_optwhence))) switch(n)
177 error(ERROR_usage(2), opt_arg);
181 if(error_info.errors || !*argv)
182 error(ERROR_usage(2),optusage((char*)0));
183 return(whence(argv, flags));
186 static int whence __PARAM__((char **argv, register int flags), (argv, flags)) __OTORP__(char **argv; register int flags;){
187 register const char *name;
188 register Namval_t *np;
189 register const char *cp;
191 register const char *msg;
195 aflag = ((flags&A_FLAG)!=0);
200 /* reserved words first */
201 if(sh_lookup(name,shtab_reserved))
203 sfprintf(sfstdout,"%s%s\n",name,(flags&V_FLAG)?ERROR_translate(is_reserved,1):"");
208 /* non-tracked aliases */
209 if((np=nv_search(name,sh.alias_tree,0))
210 && !nv_isnull(np) && !(notrack=nv_isattr(np,NV_TAGGED))
211 && (cp=nv_getval(np)))
215 if(nv_isattr(np,NV_EXPORT))
219 sfprintf(sfstdout,ERROR_translate(msg,1),name);
221 sfputr(sfstdout,sh_fmtq(cp),'\n');
227 /* built-ins and functions next */
228 if((np=nv_search(name,sh.fun_tree,0)) && (!nv_isnull(np) || nv_isattr(np,NV_FUNCTION)))
230 if(is_abuiltin(np) && (cp=np->nvenv))
232 if((flags&F_FLAG) && nv_isattr(np,NV_FUNCTION))
233 if(!(np=nv_search(name,sh.bltin_tree,0)) || nv_isnull(np))
240 if(!nv_isattr(np,NV_FUNCTION))
244 else if(is_abuiltin(np))
246 else if(nv_isattr(np,NV_EXPORT))
251 sfprintf(sfstdout,"%s%s\n",name,ERROR_translate(cp,1));
258 if(sh_isstate(SH_DEFPATH))
263 if(path_search(name,cp,2))
274 if(!np || nv_isnull(np))
275 sfprintf(sfstdout,"%s%s\n",name,ERROR_translate(is_ufunction,1));
278 sfputr(sfstdout,sh_fmtq(name),' ');
279 /* built-in version of program */
280 if(np && is_abuiltin(np) && np->nvenv && strcmp(np->nvenv,cp)==0)
282 /* tracked aliases next */
283 else if(!notrack && *name == '/')
287 sfputr(sfstdout,ERROR_translate(msg,1),' ');
289 sfputr(sfstdout,sh_fmtq(cp),'\n');
296 sfprintf(sfstdout,ERROR_translate(e_found,1),sh_fmtq(name));
297 sfputc(sfstdout,'\n');