remove OSF1 support
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / cmd / ksh93 / bltins / whence.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $XConsortium: whence.c /main/3 1995/11/01 16:31:17 rswiston $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
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             *
32 *                                                              *
33 *                Copyright (c) 1995 AT&T Corp.                 *
34 *              Unpublished & Not for Publication               *
35 *                     All Rights Reserved                      *
36 *                                                              *
37 *       The copyright notice above does not evidence any       *
38 *      actual or intended publication of such source code      *
39 *                                                              *
40 *               This software was created by the               *
41 *           Advanced Software Technology Department            *
42 *                    AT&T Bell Laboratories                    *
43 *                                                              *
44 *               For further information contact                *
45 *                    {research,attmail}!dgk                    *
46 *                                                              *
47 ***************************************************************/
48
49 /* : : generated by proto : : */
50
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__      "C"
55 #else
56 #define __MANGLE__
57 #endif
58 #define __STDARG__
59 #define __PROTO__(x)    x
60 #define __OTORP__(x)
61 #define __PARAM__(n,o)  n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
64 #define const
65 #endif
66 #define signed
67 #define void            int
68 #define volatile
69 #define __V_            char
70 #else
71 #define __V_            void
72 #endif
73 #else
74 #define __PROTO__(x)    ()
75 #define __OTORP__(x)    x
76 #define __PARAM__(n,o)  o
77 #define __MANGLE__
78 #define __V_            char
79 #define const
80 #define signed
81 #define void            int
82 #define volatile
83 #endif
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__      ...
86 #else
87 #define __VARARG__
88 #endif
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a)       va_start(p,a)
91 #else
92 #define __VA_START__(p,a)       va_start(p)
93 #endif
94 #endif
95 #include        "defs.h"
96 #include        <error.h>
97 #include        "shtable.h"
98 #include        "name.h"
99 #include        "path.h"
100 #include        "shlex.h"
101 #include        "builtins.h"
102
103 #define P_FLAG  1
104 #define V_FLAG  2
105 #define A_FLAG  4
106 #define F_FLAG  010
107 #define X_FLAG  020
108
109 static int whence __PROTO__((char**, int));
110
111 /*
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
115  */
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;
118         NOT_USED(extra);
119         opt_index = opt_char = 0;
120         while((n = optget(argv,sh_optcommand))) switch(n)
121         {
122             case 'p':
123                 sh_onstate(SH_DEFPATH);
124                 break;
125             case 'v':
126                 flags |= X_FLAG;
127                 break;
128             case 'V':
129                 flags |= V_FLAG;
130                 break;
131             case ':':
132                 if(argc==0)
133                         return(0);
134                 error(2, opt_arg);
135                 break;
136             case '?':
137                 if(argc==0)
138                         return(0);
139                 error(ERROR_usage(2), opt_arg);
140                 break;
141         }
142         if(argc==0)
143                 return(flags?0:opt_index);
144         argv += opt_index;
145         if(error_info.errors || !*argv)
146                 error(ERROR_usage(2),optusage((char*)0));
147         return(whence(argv, flags));
148 }
149
150 /*
151  *  for the whence command
152  */
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;
155         NOT_USED(argc);
156         NOT_USED(extra);
157         if(*argv[0]=='t')
158                 flags = V_FLAG;
159         while((n = optget(argv,sh_optwhence))) switch(n)
160         {
161             case 'a':
162                 flags |= A_FLAG;
163                 /* FALL THRU */
164             case 'v':
165                 flags |= V_FLAG;
166                 break;
167             case 'f':
168                 flags |= F_FLAG;
169                 break;
170             case 'p':
171                 flags |= P_FLAG;
172                 break;
173             case ':':
174                 error(2, opt_arg);
175                 break;
176             case '?':
177                 error(ERROR_usage(2), opt_arg);
178                 break;
179         }
180         argv += opt_index;
181         if(error_info.errors || !*argv)
182                 error(ERROR_usage(2),optusage((char*)0));
183         return(whence(argv, flags));
184 }
185
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;
190         register aflag,r=0;
191         register const char *msg;
192         int notrack = 1;
193         while(name= *argv++)
194         {
195                 aflag = ((flags&A_FLAG)!=0);
196                 cp = 0;
197                 np = 0;
198                 if(flags&P_FLAG)
199                         goto search;
200                 /* reserved words first */
201                 if(sh_lookup(name,shtab_reserved))
202                 {
203                         sfprintf(sfstdout,"%s%s\n",name,(flags&V_FLAG)?ERROR_translate(is_reserved,1):"");
204                         if(!aflag)
205                                 continue;
206                         aflag++;
207                 }
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))) 
212                 {
213                         if(flags&V_FLAG)
214                         {
215                                 if(nv_isattr(np,NV_EXPORT))
216                                         msg = is_xalias;
217                                 else
218                                         msg = is_alias;
219                                 sfprintf(sfstdout,ERROR_translate(msg,1),name);
220                         }
221                         sfputr(sfstdout,sh_fmtq(cp),'\n');
222                         if(!aflag)
223                                 continue;
224                         cp = 0;
225                         aflag++;
226                 }
227                 /* built-ins and functions next */
228                 if((np=nv_search(name,sh.fun_tree,0)) && (!nv_isnull(np) || nv_isattr(np,NV_FUNCTION)))
229                 {
230                         if(is_abuiltin(np) && (cp=np->nvenv))
231                                 goto search;
232                         if((flags&F_FLAG) && nv_isattr(np,NV_FUNCTION))
233                                 if(!(np=nv_search(name,sh.bltin_tree,0)) || nv_isnull(np))
234                                         goto search;
235                         cp = "";
236                         if(flags&V_FLAG)
237                         {
238                                 if(nv_isnull(np))
239                                 {
240                                         if(!nv_isattr(np,NV_FUNCTION))
241                                                 goto search;
242                                         cp = is_ufunction;
243                                 }
244                                 else if(is_abuiltin(np))
245                                         cp = is_builtin;
246                                 else if(nv_isattr(np,NV_EXPORT))
247                                         cp = is_xfunction;
248                                 else
249                                         cp = is_function;
250                         }
251                         sfprintf(sfstdout,"%s%s\n",name,ERROR_translate(cp,1));
252                         if(!aflag)
253                                 continue;
254                         cp = 0;
255                         aflag++;
256                 }
257         search:
258                 if(sh_isstate(SH_DEFPATH))
259                 {
260                         cp=0;
261                         notrack=1;
262                 }
263                 if(path_search(name,cp,2))
264                         cp = name;
265                 else
266                         cp = sh.lastpath;
267                 sh.lastpath = 0;
268                 if(cp)
269                 {
270                         if(flags&V_FLAG)
271                         {
272                                 if(*cp!= '/')
273                                 {
274                                         if(!np || nv_isnull(np))
275                                                 sfprintf(sfstdout,"%s%s\n",name,ERROR_translate(is_ufunction,1));
276                                         continue;
277                                 }
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)
281                                         msg = is_builtver;
282                                 /* tracked aliases next */
283                                 else if(!notrack && *name == '/')
284                                         msg = "is";
285                                 else
286                                         msg = is_talias;
287                                 sfputr(sfstdout,ERROR_translate(msg,1),' ');
288                         }
289                         sfputr(sfstdout,sh_fmtq(cp),'\n');
290                 }
291                 else if(aflag<=1) 
292                 {
293                         r |= 1;
294                         if(flags&V_FLAG)
295                         {
296                                 sfprintf(sfstdout,ERROR_translate(e_found,1),sh_fmtq(name));
297                                 sfputc(sfstdout,'\n');
298                         }
299                 }
300         }
301         return(r);
302 }
303