xref: /titanic_51/usr/src/lib/libcmd/common/cmd.h (revision 3d5869cc80c26887ee2c78b0f35e58f574e97192)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1992-2009 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 typedef int (*Shbltin_f)(int, char**, void*);
61 
62 #else
63 
64 extern int CMD_STANDALONE(int, char**, void*);
65 
66 #endif
67 
68 #ifndef CMD_BUILTIN
69 
70 /*
71  * command initialization
72  */
73 
74 static int
75 cmdinit(int argc, register char** argv, void* context, const char* catalog, int flags)
76 {
77 	register char*	cp;
78 	register char*	pp;
79 
80 	if (cp = strrchr(argv[0], '/'))
81 		cp++;
82 	else
83 		cp = argv[0];
84 	if (pp = strrchr(cp, '_'))
85 		cp = pp + 1;
86 	error_info.id = cp;
87 	if (!error_info.catalog)
88 		error_info.catalog = (char*)catalog;
89 	opt_info.index = 0;
90 	if (context)
91 		error_info.flags |= flags & ~(ERROR_CALLBACK|ERROR_NOTIFY);
92 	return 0;
93 }
94 
95 #endif
96 
97 int
98 main(int argc, char** argv)
99 {
100 #if CMD_DYNAMIC
101 	register char*	s;
102 	register char*	t;
103 	void*		dll;
104 	Shbltin_f	fun;
105 	char		buf[64];
106 
107 	if (s = strrchr(argv[0], '/'))
108 		s++;
109 	else if (!(s = argv[0]))
110 		return 127;
111 	if ((t = strrchr(s, '_')) && *++t)
112 		s = t;
113 	buf[0] = '_';
114 	buf[1] = 'b';
115 	buf[2] = '_';
116 	strncpy(buf + 3, s, sizeof(buf) - 4);
117 	buf[sizeof(buf) - 1] = 0;
118 	if (t = strchr(buf, '.'))
119 		*t = 0;
120 	for (;;)
121 	{
122 		if (dll = dlopen(NiL, RTLD_LAZY))
123 		{
124 			if (fun = (Shbltin_f)dlsym(dll, buf + 1))
125 				break;
126 			if (fun = (Shbltin_f)dlsym(dll, buf))
127 				break;
128 		}
129 		if (dll = dllfind("cmd", NiL, RTLD_LAZY))
130 		{
131 			if (fun = (Shbltin_f)dlsym(dll, buf + 1))
132 				break;
133 			if (fun = (Shbltin_f)dlsym(dll, buf))
134 				break;
135 		}
136 		return 127;
137 	}
138 	return (*fun)(argc, argv, NiL);
139 #else
140 	return CMD_STANDALONE(argc, argv, NiL);
141 #endif
142 }
143 
144 #else
145 
146 #undef	cmdinit
147 #ifdef _MSC_VER
148 #define CMD_CONTEXT(p)		((Shbltin_t*)(p))
149 #define cmdinit(a,b,c,d,e)	do{if(_cmd_init(a,b,c,d,e))return -1;}while(0)
150 #else
151 #define CMD_CONTEXT(p)		(((p)&&((Shbltin_t*)(p))->version>=20071012&&((Shbltin_t*)(p))->version<20350101)?((Shbltin_t*)(p)):0)
152 #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)
153 #endif
154 
155 #if _BLD_cmd && defined(__EXPORT__)
156 #define extern			extern __EXPORT__
157 #endif
158 
159 extern int	_cmd_init(int, char**, void*, const char*, int);
160 
161 #undef	extern
162 
163 #endif
164 
165 #endif
166