xref: /titanic_41/usr/src/lib/libcmd/common/cmd.h (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1992-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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 
35 #define cmdinit			_cmd_init
36 #define cmdquit()		0
37 
38 #if _BLD_cmd && defined(__EXPORT__)
39 #define extern		__EXPORT__
40 #endif
41 
42 #include <cmdext.h>
43 
44 #undef	extern
45 
46 #if defined(CMD_BUILTIN) && !defined(CMD_STANDALONE)
47 #define CMD_STANDALONE	CMD_BUILTIN
48 #endif
49 
50 #ifdef CMD_STANDALONE
51 
52 #if CMD_DYNAMIC
53 
54 #include <dlldefs.h>
55 
56 typedef int (*Builtin_f)(int, char**, void*);
57 
58 #else
59 
60 extern int CMD_STANDALONE(int, char**, void*);
61 
62 #endif
63 
64 #ifndef CMD_BUILTIN
65 
66 /*
67  * command initialization
68  */
69 
70 static int
71 cmdinit(int argc, register char** argv, void* context, const char* catalog, int flags)
72 {
73 	register char*	cp;
74 	register char*	pp;
75 
76 	if (cp = strrchr(argv[0], '/'))
77 		cp++;
78 	else
79 		cp = argv[0];
80 	if (pp = strrchr(cp, '_'))
81 		cp = pp + 1;
82 	error_info.id = cp;
83 	if (!error_info.catalog)
84 		error_info.catalog = (char*)catalog;
85 	opt_info.index = 0;
86 	if (context)
87 		error_info.flags |= flags;
88 	return 0;
89 }
90 
91 #endif
92 
93 int
94 main(int argc, char** argv)
95 {
96 #if CMD_DYNAMIC
97 	register char*	s;
98 	register char*	t;
99 	void*		dll;
100 	Builtin_f	fun;
101 	char		buf[64];
102 
103 	if (s = strrchr(argv[0], '/'))
104 		s++;
105 	else if (!(s = argv[0]))
106 		return 127;
107 	if ((t = strrchr(s, '_')) && *++t)
108 		s = t;
109 	buf[0] = '_';
110 	buf[1] = 'b';
111 	buf[2] = '_';
112 	strncpy(buf + 3, s, sizeof(buf) - 4);
113 	buf[sizeof(buf) - 1] = 0;
114 	if (t = strchr(buf, '.'))
115 		*t = 0;
116 	for (;;)
117 	{
118 		if (dll = dlopen(NiL, RTLD_LAZY))
119 		{
120 			if (fun = (Builtin_f)dlsym(dll, buf + 1))
121 				break;
122 			if (fun = (Builtin_f)dlsym(dll, buf))
123 				break;
124 		}
125 		if (dll = dllfind("cmd", NiL, RTLD_LAZY))
126 		{
127 			if (fun = (Builtin_f)dlsym(dll, buf + 1))
128 				break;
129 			if (fun = (Builtin_f)dlsym(dll, buf))
130 				break;
131 		}
132 		return 127;
133 	}
134 	return (*fun)(argc, argv, NiL);
135 #else
136 	return CMD_STANDALONE(argc, argv, NiL);
137 #endif
138 }
139 
140 #else
141 
142 #undef	cmdinit
143 #define cmdinit(a,b,c,d,e)	do{if(_cmd_init(a,b,c,d,e))return -1;}while(0)
144 
145 #ifndef CMD_BUILTIN
146 
147 #undef	cmdquit
148 #define cmdquit()		(_cmd_quit)
149 
150 #if _BLD_cmd && defined(__EXPORT__)
151 #define extern			extern __EXPORT__
152 #endif
153 #if !_BLD_cmd && defined(__IMPORT__)
154 #define extern			extern __IMPORT__
155 #endif
156 
157 extern int	_cmd_quit;
158 
159 #undef	extern
160 
161 #endif
162 
163 #if _BLD_cmd && defined(__EXPORT__)
164 #define extern			extern __EXPORT__
165 #endif
166 
167 extern int	_cmd_init(int, char**, void*, const char*, int);
168 
169 #undef	extern
170 
171 #endif
172 
173 #endif
174