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