xref: /onnv-gate/usr/src/lib/libcmd/common/cmd.h (revision 4887)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1992-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                                                                      *
20*4887Schin ***********************************************************************/
21*4887Schin #pragma prototyped
22*4887Schin /*
23*4887Schin  * AT&T Research
24*4887Schin  *
25*4887Schin  * builtin cmd definitions
26*4887Schin  */
27*4887Schin 
28*4887Schin #ifndef _CMD_H
29*4887Schin #define _CMD_H
30*4887Schin 
31*4887Schin #include <ast.h>
32*4887Schin #include <error.h>
33*4887Schin #include <stak.h>
34*4887Schin 
35*4887Schin #define cmdinit			_cmd_init
36*4887Schin #define cmdquit()		0
37*4887Schin 
38*4887Schin #if _BLD_cmd && defined(__EXPORT__)
39*4887Schin #define extern		__EXPORT__
40*4887Schin #endif
41*4887Schin 
42*4887Schin #include <cmdext.h>
43*4887Schin 
44*4887Schin #undef	extern
45*4887Schin 
46*4887Schin #if defined(CMD_BUILTIN) && !defined(CMD_STANDALONE)
47*4887Schin #define CMD_STANDALONE	CMD_BUILTIN
48*4887Schin #endif
49*4887Schin 
50*4887Schin #ifdef CMD_STANDALONE
51*4887Schin 
52*4887Schin #if CMD_DYNAMIC
53*4887Schin 
54*4887Schin #include <dlldefs.h>
55*4887Schin 
56*4887Schin typedef int (*Builtin_f)(int, char**, void*);
57*4887Schin 
58*4887Schin #else
59*4887Schin 
60*4887Schin extern int CMD_STANDALONE(int, char**, void*);
61*4887Schin 
62*4887Schin #endif
63*4887Schin 
64*4887Schin #ifndef CMD_BUILTIN
65*4887Schin 
66*4887Schin /*
67*4887Schin  * command initialization
68*4887Schin  */
69*4887Schin 
70*4887Schin static int
71*4887Schin cmdinit(int argc, register char** argv, void* context, const char* catalog, int flags)
72*4887Schin {
73*4887Schin 	register char*	cp;
74*4887Schin 	register char*	pp;
75*4887Schin 
76*4887Schin 	if (cp = strrchr(argv[0], '/'))
77*4887Schin 		cp++;
78*4887Schin 	else
79*4887Schin 		cp = argv[0];
80*4887Schin 	if (pp = strrchr(cp, '_'))
81*4887Schin 		cp = pp + 1;
82*4887Schin 	error_info.id = cp;
83*4887Schin 	if (!error_info.catalog)
84*4887Schin 		error_info.catalog = (char*)catalog;
85*4887Schin 	opt_info.index = 0;
86*4887Schin 	if (context)
87*4887Schin 		error_info.flags |= flags;
88*4887Schin 	return 0;
89*4887Schin }
90*4887Schin 
91*4887Schin #endif
92*4887Schin 
93*4887Schin int
94*4887Schin main(int argc, char** argv)
95*4887Schin {
96*4887Schin #if CMD_DYNAMIC
97*4887Schin 	register char*	s;
98*4887Schin 	register char*	t;
99*4887Schin 	void*		dll;
100*4887Schin 	Builtin_f	fun;
101*4887Schin 	char		buf[64];
102*4887Schin 
103*4887Schin 	if (s = strrchr(argv[0], '/'))
104*4887Schin 		s++;
105*4887Schin 	else if (!(s = argv[0]))
106*4887Schin 		return 127;
107*4887Schin 	if ((t = strrchr(s, '_')) && *++t)
108*4887Schin 		s = t;
109*4887Schin 	buf[0] = '_';
110*4887Schin 	buf[1] = 'b';
111*4887Schin 	buf[2] = '_';
112*4887Schin 	strncpy(buf + 3, s, sizeof(buf) - 4);
113*4887Schin 	buf[sizeof(buf) - 1] = 0;
114*4887Schin 	if (t = strchr(buf, '.'))
115*4887Schin 		*t = 0;
116*4887Schin 	for (;;)
117*4887Schin 	{
118*4887Schin 		if (dll = dlopen(NiL, RTLD_LAZY))
119*4887Schin 		{
120*4887Schin 			if (fun = (Builtin_f)dlsym(dll, buf + 1))
121*4887Schin 				break;
122*4887Schin 			if (fun = (Builtin_f)dlsym(dll, buf))
123*4887Schin 				break;
124*4887Schin 		}
125*4887Schin 		if (dll = dllfind("cmd", NiL, RTLD_LAZY))
126*4887Schin 		{
127*4887Schin 			if (fun = (Builtin_f)dlsym(dll, buf + 1))
128*4887Schin 				break;
129*4887Schin 			if (fun = (Builtin_f)dlsym(dll, buf))
130*4887Schin 				break;
131*4887Schin 		}
132*4887Schin 		return 127;
133*4887Schin 	}
134*4887Schin 	return (*fun)(argc, argv, NiL);
135*4887Schin #else
136*4887Schin 	return CMD_STANDALONE(argc, argv, NiL);
137*4887Schin #endif
138*4887Schin }
139*4887Schin 
140*4887Schin #else
141*4887Schin 
142*4887Schin #undef	cmdinit
143*4887Schin #define cmdinit(a,b,c,d,e)	do{if(_cmd_init(a,b,c,d,e))return -1;}while(0)
144*4887Schin 
145*4887Schin #ifndef CMD_BUILTIN
146*4887Schin 
147*4887Schin #undef	cmdquit
148*4887Schin #define cmdquit()		(_cmd_quit)
149*4887Schin 
150*4887Schin #if _BLD_cmd && defined(__EXPORT__)
151*4887Schin #define extern			extern __EXPORT__
152*4887Schin #endif
153*4887Schin #if !_BLD_cmd && defined(__IMPORT__)
154*4887Schin #define extern			extern __IMPORT__
155*4887Schin #endif
156*4887Schin 
157*4887Schin extern int	_cmd_quit;
158*4887Schin 
159*4887Schin #undef	extern
160*4887Schin 
161*4887Schin #endif
162*4887Schin 
163*4887Schin #if _BLD_cmd && defined(__EXPORT__)
164*4887Schin #define extern			extern __EXPORT__
165*4887Schin #endif
166*4887Schin 
167*4887Schin extern int	_cmd_init(int, char**, void*, const char*, int);
168*4887Schin 
169*4887Schin #undef	extern
170*4887Schin 
171*4887Schin #endif
172*4887Schin 
173*4887Schin #endif
174