1 #pragma prototyped
2 #ifndef __TKSH_H_
3 #define __TKSH_H_
4 
5 #include <ast/shell.h>	/* X11 has Shell.h that clashes on case ignorant systems */
6 #include <tcl.h>
7 #include <errno.h>
8 
9 /*
10  * The structure below defines a deletion callback, which is
11  * a procedure to invoke just before an interpreter is deleted.
12  */
13 
14 typedef struct DeleteCallback {
15     Tcl_InterpDeleteProc *proc;	/* Procedure to call. */
16     ClientData clientData;	/* Value to pass to procedure. */
17     struct DeleteCallback *nextPtr;
18 				/* Next in list of callbacks for this
19 				 * interpreter (or NULL for end of list). */
20 } DeleteCallback;
21 
22 typedef struct Interp {
23 
24     /*
25      * Note:  the first three fields must match exactly the fields in
26      * a Tcl_Interp struct (see tcl.h).  If you change one, be sure to
27      * change the other.
28      */
29 
30     char *result;               /* Points to result returned by last
31                                  * command. */
32     Tcl_FreeProc *freeProc;     /* Zero means result is statically allocated.
33                                  * If non-zero, gives address of procedure
34                                  * to invoke to free the result.  Must be
35                                  * freed by Tcl_Eval before executing next
36                                  * command. */
37     int errorLine;              /* When TCL_ERROR is returned, this gives
38                                  * the line number within the command where
39                                  * the error occurred (1 means first line). */
40     Tcl_HashTable xxxcommandTable; /* Contains all of the commands currently
41                                  * registered in this interpreter.  Indexed
42                                  * by strings; values have type (Command *). */
43     Tcl_HashTable mathFuncTable;/* Contains all of the math functions currently
44                                  * defined for the interpreter.  Indexed by
45                                  * strings (function names);  values have
46                                  * type (MathFunc *). */
47 
48     /*
49      * Information related to procedures and variables.  See tclProc.c
50      * and tclvar.c for usage.
51      */
52 
53     Tcl_HashTable xxxglobalTable;  /* Contains all global variables for
54                                  * interpreter. */
55 #ifndef NO_TCL_INTERP
56     int numLevels;              /* Keeps track of how many nested calls to
57                                  * Tcl_Eval are in progress for this
58                                  * interpreter.  It's used to delay deletion
59                                  * of the table until all Tcl_Eval invocations
60                                  * are completed. */
61     int maxNestingDepth;        /* If numLevels exceeds this value then Tcl
62                                  * assumes that infinite recursion has
63                                  * occurred and it generates an error. */
64 #endif
65     void * /* CallFrame */ xxxframePtr;        /* Points to top-most in stack of all nested
66                                  * procedure invocations.  NULL means there
67                                  * are no active procedures. */
68     void * /* CallFrame */ xxxvarFramePtr;     /* Points to the call frame whose variables
69                                  * are currently in use (same as framePtr
70                                  * unless an "uplevel" command is being
71                                  * executed).  NULL means no procedure is
72                                  * active or "uplevel 0" is being exec'ed. */
73     void * /* ActiveVarTrace */ xxxactiveTracePtr;
74                                 /* First in list of active traces for interp,
75                                  * or NULL if no active traces. */
76 
77     int returnCode;             /* Completion code to return if current
78                                  * procedure exits with a TCL_RETURN code. */
79     char *errorInfo;            /* Value to store in errorInfo if returnCode
80                                  * is TCL_ERROR.  Malloc'ed, may be NULL */
81     char *errorCode;            /* Value to store in errorCode if returnCode
82                                  * is TCL_ERROR.  Malloc'ed, may be NULL */
83 
84     /*
85      * Information related to history:
86      */
87 
88 #ifdef TCL_CODE
89     int xxxnumEvents;              /* Number of previously-executed commands
90                                  * to retain. */
91 #else
92     int interpType;
93 #endif
94     void * /*HistoryEvent */ xxxevents;       /* Array containing numEvents entries
95                                  * (dynamically allocated). */
96     int xxxcurEvent;               /* Index into events of place where current
97                                  * (or most recent) command is recorded. */
98     int xxxcurEventNum;            /* Event number associated with the slot
99                                  * given by curEvent. */
100     void * /*HistoryRev */ xxxrevPtr;         /* First in list of pending revisions. */
101     char *xxxhistoryFirst;         /* First char. of current command executed
102                                  * from history module or NULL if none. */
103     int xxxrevDisables;            /* 0 means history revision OK;  > 0 gives
104                                  * a count of number of times revision has
105                                  * been disabled. */
106     char *xxxevalFirst;            /* If TCL_RECORD_BOUNDS flag set, Tcl_Eval
107                                  * sets this field to point to the first
108                                  * char. of text from which the current
109                                  * command came.  Otherwise Tcl_Eval sets
110                                  * this to NULL. */
111     char *xxxevalLast;             /* Similar to evalFirst, except points to
112                                  * last character of current command. */
113 
114 
115     /*
116      * Information used by Tcl_AppendResult to keep track of partial
117      * results.  See Tcl_AppendResult code for details.
118      */
119 
120     char *appendResult;         /* Storage space for results generated
121                                  * by Tcl_AppendResult.  Malloc-ed.  NULL
122                                  * means not yet allocated. */
123     int appendAvl;              /* Total amount of space available at
124                                  * partialResult. */
125     int appendUsed;             /* Number of non-null bytes currently
126                                  * stored at partialResult. */
127 
128     /*
129      * A cache of compiled regular expressions.  See TclCompileRegexp
130      * in tclUtil.c for details.
131      */
132 
133 #define NUM_REGEXPS 5
134     char *xxxpatterns[NUM_REGEXPS];/* Strings corresponding to compiled
135                                  * regular expression patterns.  NULL
136                                  * means that this slot isn't used.
137                                  * Malloc-ed. */
138     int xxxpatLengths[NUM_REGEXPS];/* Number of non-null characters in
139                                  * corresponding entry in patterns.
140                                  * -1 means entry isn't used. */
141     void * /* regexp */ xxxregexps[NUM_REGEXPS];
142                                 /* Compiled forms of above strings.  Also
143                                  * malloc-ed, or NULL if not in use yet. */
144 
145 
146     /*
147      * Information used by Tcl_PrintDouble:
148      */
149 
150     char pdFormat[10];          /* Format string used by Tcl_PrintDouble. */
151     int pdPrec;                 /* Current precision (used to restore the
152                                  * the tcl_precision variable after a bogus
153                                  * value has been put into it). */
154 
155 #ifndef NO_TCL_INTERP
156 
157     /*
158      * Miscellaneous information:
159      */
160 
161     int cmdCount;               /* Total number of times a command procedure
162                                  * has been called for this interpreter. */
163     int noEval;                 /* Non-zero means no commands should actually
164                                  * be executed:  just parse only.  Used in
165                                  * expressions when the result is already
166                                  * determined. */
167     int evalFlags;              /* Flags to control next call to Tcl_Eval.
168                                  * Normally zero, but may be set before
169                                  * calling Tcl_Eval to an OR'ed combination
170                                  * of TCL_BRACKET_TERM and TCL_RECORD_BOUNDS. */
171     char *termPtr;              /* Character just after the last one in
172                                  * a command.  Set by Tcl_Eval before
173                                  * returning. */
174     char *scriptFile;           /* NULL means there is no nested source
175                                  * command active;  otherwise this points to
176                                  * the name of the file being sourced (it's
177                                  * not malloc-ed:  it points to an argument
178                                  * to Tcl_EvalFile. */
179     int flags;                  /* Various flag bits.  See below. */
180     void * /* Trace */ tracePtr;            /* List of traces for this interpreter. */
181     DeleteCallback* deleteCallbackPtr;
182                                 /* First in list of callbacks to invoke when
183                                  * interpreter is deleted. */
184 
185 #endif
186     /*
187      * Information about packages.  Used only in tclPkg.c.
188      */
189 
190     Tcl_HashTable packageTable; /* Describes all of the packages loaded
191                                  * in or available to this interpreter.
192                                  * Keys are package names, values are
193                                  * (Package *) pointers. */
194     char *packageUnknown;       /* Command to invoke during "package
195                                  * require" commands for packages that
196                                  * aren't described in packageTable.
197                                  * Malloc'ed, may be NULL. */
198 
199     Tcl_HashTable *assocData;   /* Hash table for associating data with
200                                  * this interpreter. Cleaned up when
201                                  * this interpreter is deleted. */
202 
203     void* shbltin;		/* shell (Shbltin_t*) */
204 
205     char resultSpace[TCL_RESULT_SIZE+1];
206                                 /* Static space for storing small results. */
207 } Interp;
208 
209 /*
210  * Flag bits for Interp structures:
211  *
212  * DELETED:             Non-zero means the interpreter has been deleted:
213  *                      don't process any more commands for it, and destroy
214  *                      the structure as soon as all nested invocations of
215  *                      Tcl_Eval are done.
216  * ERR_IN_PROGRESS:     Non-zero means an error unwind is already in progress.
217  *                      Zero means a command proc has been invoked since last
218  *                      error occured.
219  * ERR_ALREADY_LOGGED:  Non-zero means information has already been logged
220  *                      in $errorInfo for the current Tcl_Eval instance,
221  *                      so Tcl_Eval needn't log it (used to implement the
222  *                      "error message log" command).
223  * ERROR_CODE_SET:      Non-zero means that Tcl_SetErrorCode has been
224  *                      called to record information for the current
225  *                      error.  Zero means Tcl_Eval must clear the
226  *                      errorCode variable if an error is returned.
227  */
228 
229 #define DELETED                 1
230 #define ERR_IN_PROGRESS         2
231 #define ERR_ALREADY_LOGGED	4
232 #define ERROR_CODE_SET          8
233 
234 #define INTERP_KSH		0
235 #define INTERP_TCL		1
236 #define INTERP_MASK		3
237 #define INTERP_CURRENT		2
238 
239 #define TKSH_TRACE_RUNNING	0x400	/* Set when trace is running */
240 #define COMMAND_ACTIVE		4	/* Used in commandType field */
241 #define NV_FUNC			(NV_NOADD | NV_NOASSIGN)
242 
243 #ifndef LIB_DIR_ENV
244 #define LIB_DIR_ENV	"TKSH_LIBRARY"
245 #endif
246 
247 #ifndef LIB_DIR
248 #define LIB_DIR		"lib/tksh7.6"
249 #endif
250 
251 #define SPLIT_CMD_NAME "_tksh_split_list"
252 
253 typedef struct TkshArrayInfo
254 {
255 	ClientData clientData;
256 } TkshArrayInfo;
257 
258 typedef struct TkshCommandData
259 {
260 	Tcl_Interp *interp;
261 	Tcl_CmdInfo info;
262 	char commandType;
263 } TkshCommandData;
264 
265 #define Tksh_MapReturn(i)	((((i) >= 0) && ((i) <= TCL_CONTINUE)) ? (i) \
266 				: TCL_ERROR)
267 #define Tksh_ReturnVal()	(Tksh_MapReturn(sh.exitval))
268 #define Tksh_OkOrErr()		((sh.exitval == 0) ? TCL_OK : TCL_ERROR)
269 #define Tksh_InterpString(i)	(((i)==INTERP_TCL)? "tcl": (((i)==INTERP_KSH) \
270 					? "ksh" : "either"))
271 
272 #define Tksh_BeginBlock(interp, kind)	do { int oldInterp = ((Interp *)interp)->interpType; ((Interp *)interp)->interpType = kind
273 #define Tksh_EndBlock(interp)		((Interp *)interp)->interpType = oldInterp; } while(0)
274 
275 #if _BLD_tcl && defined(__EXPORT__)
276 #define extern	__EXPORT__
277 #endif
278 
279 extern void		TkshDeleteSearches(TkshArrayInfo *);
280 extern TkshArrayInfo*	TkshArrayData(Namval_t *namval);
281 extern void		TkshTracesOff(void);
282 
283 extern int		tksh_waitevent(int, long, int);
284 extern int		nv_getlevel(void);
285 
286 extern int		Tksh_Eval(Tcl_Interp *interp, char *cmd, int flag);
287 extern int		Tksh_SetCommandType(Tcl_Interp *, char *, int tp);
288 extern int		Tksh_Init(Tcl_Interp *interp);
289 extern char*		Tksh_ConvertList(Tcl_Interp *interp, char *lst, int to);
290 extern void		TkshCreateInterp(Tcl_Interp *interp, void *data);
291 extern void		TkshSubShell(void);
292 extern int		Tksh_CreatePipeline(Tcl_Interp*, int, char**, Tcl_DString*);
293 
294 extern int		b_print(int argc, char *argv[], Shbltin_t *);
295 extern int		b_tclinit(int argc, char *argv[], Shbltin_t *);
296 extern int		b_tkinit(int argc, char *argv[], Shbltin_t *);
297 
298 #undef	extern
299 
300 #endif /* __TKSH_H_ */
301