chiark / gitweb /
Update build-dependencies to permit tcl8.6. Closes:#818475.
[chiark-tcl.git] / base / chiark-tcl.h
1 /*
2  * base code for various Tcl extensions
3  * Copyright 2006-2012 Ian Jackson
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this library; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef CHIARK_TCL_H
20 #define CHIARK_TCL_H
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <sys/socket.h>
29 #include <sys/uio.h>
30 #include <sys/un.h>
31 #include <arpa/inet.h>
32
33 #ifndef _TCL /* if someone already included some tcl.h, use that */
34 #include <tcl.h>
35 #endif /*_TCL*/
36
37 #include <adns.h>
38
39 typedef unsigned char Byte;
40
41 /* for assisting tcmdifgen and tcmdiflib.c */
42
43 typedef struct TopLevel_Command TopLevel_Command;
44
45 struct TopLevel_Command {
46   const char *name;
47   Tcl_ObjCmdProc *func;
48 };
49
50 void cht_setstringresult(Tcl_Interp*, const char*);
51 int cht_pat_enum(Tcl_Interp*, Tcl_Obj*, const void**,
52                  const void*, size_t, const char *what);
53
54 /* from scriptinv.c */
55
56 typedef struct { /* opaque; comments are for scriptinv.c impl'n only */
57   /* states:                Cancelled       Set                          */
58   Tcl_Interp *ipq;     /*    0               valid, non-0, useable       */
59   Tcl_Obj *script;     /*    0               valid, non-0                */
60   Tcl_Obj *xargs;      /*    0               valid, may be 0             */
61   int llen;            /*    undefined       llength of script + xargs   */
62 } ScriptToInvoke;
63
64 void cht_scriptinv_init(ScriptToInvoke *si); /* undefined -> Cancelled */
65 int cht_scriptinv_set(ScriptToInvoke *si, Tcl_Interp *ip,
66                       Tcl_Obj *newscript, Tcl_Obj *xargs);
67   /* Cancelled/Set -> Set (newscript!=0, ok) / Cancelled (otherwise) */
68 void cht_scriptinv_cancel(ScriptToInvoke *si);
69   /* Cancelled/Set -> Cancelled.  No separate free function - just cancel. */
70 #define cht_scriptinv_interp(si) ((si)->ipq)
71   /* int cht_scriptinv_interp(ScriptToInvoke *si);  returns 0 if Cancelled */
72
73 int cht_scriptinv_invoke_fg(ScriptToInvoke *si, int argc,
74                             Tcl_Obj *const *argv);
75   /* is a no-op if Cancelled rather than Set */
76   /* if script fails, returns that error */
77
78 void cht_scriptinv_invoke(ScriptToInvoke *si, int argc, Tcl_Obj *const *argv);
79   /* if script fails, reports it with Tcl_BackgroundError */
80
81 /* from idtable.c */
82
83 typedef struct {
84   const char *valprefix, *assockey;
85   void (*destroyitem)(Tcl_Interp *ip, void *val);
86 } IdDataSpec;
87
88 /* The stored struct must start with a single int, conventionally
89  * named `ix'.  When the struct is returned for the first time ix must
90  * be -1; on subsequent occasions it must be >=0.  ix will be -1 iff
91  * the struct is registered by the iddatatable machinery. */
92
93 extern Tcl_ObjType cht_tabledataid_nearlytype;
94 int cht_tabledataid_parse(Tcl_Interp *ip, Tcl_Obj *o, const IdDataSpec *idds);
95 void cht_tabledataid_disposing(Tcl_Interp *ip, void *val, const IdDataSpec *idds);
96   /* call this when you destroy the struct, to remove its name;
97    * _disposing is idempotent */
98
99 /* from hook.c */
100
101 int cht_staticerr(Tcl_Interp *ip, const char *m, const char *ec);
102 int cht_posixerr(Tcl_Interp *ip, int errnoval, const char *m);
103 int cht_newfdposixerr(Tcl_Interp *ip, int fd, const char *m);
104 void cht_objfreeir(Tcl_Obj *o);
105 int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
106
107 void cht_obj_updatestr_vstringls(Tcl_Obj *o, ...);
108   /* const char*, size_t, const char*, size_t, ..., (const char*)0 */
109 void cht_obj_updatestr_string_len(Tcl_Obj *o, const char *str, int l);
110 void cht_obj_updatestr_string(Tcl_Obj *o, const char *str);
111
112 void cht_prepare__basic(Tcl_Interp *ip);
113 void cht_setup__commands(Tcl_Interp *ip, const TopLevel_Command *cmds);
114   /* ... for use by CHT_INIT and CHTI_... macros only */
115
116 /* from parse.c */
117
118 typedef struct {
119   Tcl_Obj *obj, *var;
120   int copied;
121 } Something_Var;
122
123 void cht_init_somethingv(Something_Var *sth);
124 void cht_fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth);
125 int cht_pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
126                    Something_Var *sth, Tcl_ObjType *type);
127
128 /* from enum.c */
129
130 extern Tcl_ObjType cht_enum_nearlytype;
131 extern Tcl_ObjType cht_enum1_nearlytype;
132
133 const void *cht_enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
134                                     const void *firstentry, size_t entrysize,
135                                     const char *what);
136 #define enum_lookup_cached(ip,o,table,what)                     \
137     (cht_enum_lookup_cached_func((ip),(o),                              \
138                              &(table)[0],sizeof((table)[0]),    \
139                              (what)))
140   /* table should be a pointer to an array of structs of size
141    * entrysize, the first member of which should be a const char*.
142    * The table should finish with a null const char *.
143    * On error, 0 is returned and the ip->result will have been
144    * set to the error message.
145    */
146
147 int cht_enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
148                              const char *opts, const char *what);
149   /* -1 => error */
150
151 /* useful macros */
152
153 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
154 #define TFREE(f) (Tcl_Free((void*)(f)))
155 #define TREALLOC(p,l) ((void*)Tcl_Realloc((void*)(p),(l)))
156
157 /* macros for Chiark_tcl_FOOBAR_Init et al */
158
159   /*
160    * use these macros like this:
161    *    CHT_INIT(<extbase>,
162    *             <preparations>,
163    *             <results>)
164    * where
165    *
166    *   <extbase> is the short name eg `hbytes'
167    *     and should correspond to EXTBASE from the Makefile.
168    *
169    *   <results> are the initialisations which cause new commands
170    *     etc. to appear in the Tcl namespace.  Eg, CHTI_COMMANDS,
171    *     These initialisations are called only when a Tcl `load'
172    *     command loads this extension.
173    *
174    *   <preparations> are the initialisations that we need but which
175    *     do not interfere with the Tcl namespaces.  For example,
176    *     OBJECT types we used (CHTI_TYPE), and other chiark_tcl
177    *     extensions (CHTI_OTHER).  These initialisations are called
178    *     both as a result of Tcl `load' (before the <results>
179    *     initialisations) and also when another extension declares a
180    *     dependency on this one with CHTI_OTHER.
181    *
182    *   Both <results> and <preparations> are whitespace-separated
183    *   lists of calls to CHTI_... macros.  If the list is to be empty,
184    *   write `{ }' instead to prevent an empty macro argument.  The
185    *   preparations and results currently supported are:
186    *
187    *      CHTI_COMMANDS(cht_<somethingtoplevel>_entries)
188    *          where the .tct file contains
189    *            Table *<somethingtoplevel> TopLevel_Command
190    *
191    *      CHTI_OTHER(<extbase-of-underlying-extension>)
192    *          which does the <preparations> of that extension
193    *          (if they have not already been done).
194    *
195    *      CHTI_TYPE(cht_<something>_type)
196    *          where   extern Tcl_ObjType cht_<something>_type;
197    *          Note that CHTI_TYPE should only be called by the
198    *          extension which actually implements the type.  Other
199    *          extensions which need it should use CHTI_OTHER to bring
200    *          in the implementing extension.
201    */
202
203 #define CHT_INIT(e, preparations, results)                                   \
204   extern void cht_prepare_##e(Tcl_Interp *ip);                               \
205   void cht_prepare_##e(Tcl_Interp *ip) {                                     \
206     static int prepared;                                                     \
207     if (prepared) return;                                                    \
208     cht_prepare__basic(ip);                                                  \
209     { preparations }                                                         \
210     prepared= 1;                                                             \
211   }                                                                          \
212   extern int Chiark_tcl_##e##_Init(Tcl_Interp *ip); /*called by load(3tcl)*/ \
213   int Chiark_tcl_##e##_Init(Tcl_Interp *ip) {                                \
214     static int initd;                                                        \
215     if (initd) return TCL_OK;                                                \
216     cht_prepare_##e(ip);                                                     \
217     { results }                                                              \
218     initd= 1;                                                                \
219     return TCL_OK;                                                           \
220   }
221
222 #define CHTI_OTHER(e)                                                    \
223   { extern void cht_prepare_##e(Tcl_Interp *ip); cht_prepare_##e(ip); }
224
225 #define CHTI_TYPE(ot)      { Tcl_RegisterObjType(&(ot)); }
226
227 #define CHTI_COMMANDS(cl)  { cht_setup__commands(ip,cl); }
228
229 #endif /*CHIARK_TCL_H*/