2 * base code for various Tcl extensions
3 * Copyright 2006 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29 #include <sys/socket.h>
32 #include <arpa/inet.h>
34 #include <tcl8.3/tcl.h>
38 typedef unsigned char Byte;
40 /* for assisting tcmdifgen and tcmdiflib.c */
42 typedef struct TopLevel_Command TopLevel_Command;
44 struct TopLevel_Command {
49 void cht_setstringresult(Tcl_Interp*, const char*);
50 int cht_pat_enum(Tcl_Interp*, Tcl_Obj*, const void**,
51 const void*, size_t, const char *what);
53 /* from scriptinv.c */
55 typedef struct { /* opaque; comments are for scriptinv.c impl'n only */
56 /* states: Cancelled Set */
57 Tcl_Interp *ipq; /* 0 valid, non-0, useable */
58 Tcl_Obj *script; /* 0 valid, non-0 */
59 Tcl_Obj *xargs; /* 0 valid, may be 0 */
60 int llen; /* undefined llength of script + xargs */
63 void cht_scriptinv_init(ScriptToInvoke *si); /* undefined -> Cancelled */
64 int cht_scriptinv_set(ScriptToInvoke *si, Tcl_Interp *ip,
65 Tcl_Obj *newscript, Tcl_Obj *xargs);
66 /* Cancelled/Set -> Set (newscript!=0, ok) / Cancelled (otherwise) */
67 void cht_scriptinv_cancel(ScriptToInvoke *si);
68 /* Cancelled/Set -> Cancelled. No separate free function - just cancel. */
69 #define cht_scriptinv_interp(si) ((si)->ipq)
70 /* int cht_scriptinv_interp(ScriptToInvoke *si); returns 0 if Cancelled */
72 int cht_scriptinv_invoke_fg(ScriptToInvoke *si, int argc,
73 Tcl_Obj *const *argv);
74 /* is a no-op if Cancelled rather than Set */
75 /* if script fails, returns that error */
77 void cht_scriptinv_invoke(ScriptToInvoke *si, int argc, Tcl_Obj *const *argv);
78 /* if script fails, reports it with Tcl_BackgroundError */
83 const char *valprefix, *assockey;
84 void (*destroyitem)(Tcl_Interp *ip, void *val);
87 /* The stored struct must start with a single int, conventionally
88 * named `ix'. When the struct is returned for the first time ix must
89 * be -1; on subsequent occasions it must be >=0. ix will be -1 iff
90 * the struct is registered by the iddatatable machinery. */
92 extern Tcl_ObjType cht_tabledataid_nearlytype;
93 int cht_tabledataid_parse(Tcl_Interp *ip, Tcl_Obj *o, const IdDataSpec *idds);
94 void cht_tabledataid_disposing(Tcl_Interp *ip, void *val, const IdDataSpec *idds);
95 /* call this when you destroy the struct, to remove its name;
96 * _disposing is idempotent */
100 int cht_staticerr(Tcl_Interp *ip, const char *m, const char *ec);
101 int cht_posixerr(Tcl_Interp *ip, int errnoval, const char *m);
102 int cht_newfdposixerr(Tcl_Interp *ip, int fd, const char *m);
103 void cht_objfreeir(Tcl_Obj *o);
104 int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
106 void cht_obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
107 void cht_obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
108 int l, const char *prefix);
110 void cht_obj_updatestr_vstringls(Tcl_Obj *o, ...);
111 /* const char*, int, const char*, int, ..., (const char*)0 */
112 void cht_obj_updatestr_string_len(Tcl_Obj *o, const char *str, int l);
113 void cht_obj_updatestr_string(Tcl_Obj *o, const char *str);
115 void cht_prepare__basic(Tcl_Interp *ip);
116 void cht_setup__commands(Tcl_Interp *ip, const TopLevel_Command *cmds);
117 /* ... for use by CHT_INIT and CHTI_... macros only */
126 void cht_init_somethingv(Something_Var *sth);
127 void cht_fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth);
128 int cht_pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
129 Something_Var *sth, Tcl_ObjType *type);
133 extern Tcl_ObjType cht_enum_nearlytype;
134 extern Tcl_ObjType cht_enum1_nearlytype;
136 const void *cht_enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
137 const void *firstentry, size_t entrysize,
139 #define enum_lookup_cached(ip,o,table,what) \
140 (cht_enum_lookup_cached_func((ip),(o), \
141 &(table)[0],sizeof((table)[0]), \
143 /* table should be a pointer to an array of structs of size
144 * entrysize, the first member of which should be a const char*.
145 * The table should finish with a null const char *.
146 * On error, 0 is returned and the ip->result will have been
147 * set to the error message.
150 int cht_enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
151 const char *opts, const char *what);
156 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
157 #define TFREE(f) (Tcl_Free((void*)(f)))
158 #define TREALLOC(p,l) ((void*)Tcl_Realloc((void*)(p),(l)))
160 /* macros for Chiark_tcl_FOOBAR_Init et al */
163 * use these macros like this:
164 * CHT_INIT(<extbase>,
169 * <extbase> is the short name eg `hbytes'
170 * and should correspond to EXTBASE from the Makefile.
172 * <results> are the initialisations which cause new commands
173 * etc. to appear in the Tcl namespace. Eg, CHTI_COMMANDS,
174 * These initialisations are called only when a Tcl `load'
175 * command loads this extension.
177 * <preparations> are the initialisations that we need but which
178 * do not interfere with the Tcl namespaces. For example,
179 * OBJECT types we used (CHTI_TYPE), and other chiark_tcl
180 * extensions (CHTI_OTHER). These initialisations are called
181 * both as a result of Tcl `load' (before the <results>
182 * initialisations) and also when another extension declares a
183 * dependency on this one with CHTI_OTHER.
185 * Both <results> and <preparations> are whitespace-separated
186 * lists of calls to CHTI_... macros. If the list is to be empty,
187 * write `{ }' instead to prevent an empty macro argument. The
188 * preparations and results currently supported are:
190 * CHTI_COMMANDS(cht_<somethingtoplevel>_entries)
191 * where the .tct file contains
192 * Table *<somethingtoplevel> TopLevel_Command
194 * CHTI_OTHER(<extbase-of-underlying-extension>)
195 * which does the <preparations> of that extension
196 * (if they have not already been done).
198 * CHTI_TYPE(cht_<something>_type)
199 * where extern Tcl_ObjType cht_<something>_type;
200 * Note that CHTI_TYPE should only be called by the
201 * extension which actually implements the type. Other
202 * extensions which need it should use CHTI_OTHER to bring
203 * in the implementing extension.
206 #define CHT_INIT(e, preparations, results) \
207 extern void cht_prepare_##e(Tcl_Interp *ip); \
208 void cht_prepare_##e(Tcl_Interp *ip) { \
209 static int prepared; \
210 if (prepared) return; \
211 cht_prepare__basic(ip); \
215 extern int Chiark_tcl_##e##_Init(Tcl_Interp *ip); /*called by load(3tcl)*/ \
216 int Chiark_tcl_##e##_Init(Tcl_Interp *ip) { \
218 if (initd) return TCL_OK; \
219 cht_prepare_##e(ip); \
225 #define CHTI_OTHER(e) \
226 { extern void cht_prepare_##e(Tcl_Interp *ip); cht_prepare_##e(ip); }
228 #define CHTI_TYPE(ot) { Tcl_RegisterObjType(&(ot)); }
229 #define CHTI_COMMANDS(cl) { cht_setup__commands(ip,cl); }
231 #endif /*CHIARK_TCL_H*/