chiark / gitweb /
Debian package wip
[chiark-tcl.git] / base / chiark-tcl.h
1 /*
2  * base code for various Tcl extensions
3  * Copyright 2006 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  */
20
21 #ifndef CHIARK_TCL_H
22 #define CHIARK_TCL_H
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/socket.h>
30 #include <sys/uio.h>
31 #include <sys/un.h>
32 #include <arpa/inet.h>
33
34 #include <tcl8.3/tcl.h>
35
36 #include <adns.h>
37
38 typedef unsigned char Byte;
39
40 /* for assisting tcmdifgen and tcmdiflib.c */
41
42 typedef struct TopLevel_Command TopLevel_Command;
43
44 struct TopLevel_Command {
45   const char *name;
46   Tcl_ObjCmdProc *func;
47 };
48
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);
52
53 /* from scriptinv.c */
54
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   */
61 } ScriptToInvoke;
62
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 */
71
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 */
76
77 void cht_scriptinv_invoke(ScriptToInvoke *si, int argc, Tcl_Obj *const *argv);
78   /* if script fails, reports it with Tcl_BackgroundError */
79
80 /* from idtable.c */
81
82 typedef struct {
83   const char *valprefix, *assockey;
84   void (*destroyitem)(Tcl_Interp *ip, void *val);
85 } IdDataSpec;
86
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. */
91
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 */
97
98 /* from hook.c */
99
100 int cht_initextension(Tcl_Interp *ip, const TopLevel_Command *cmds,
101                       int *donep /* or 0, meaning no types follow */,
102                       ... /* types, terminated by 0 */);
103
104 int cht_staticerr(Tcl_Interp *ip, const char *m, const char *ec);
105 int cht_posixerr(Tcl_Interp *ip, int errnoval, const char *m);
106 int cht_newfdposixerr(Tcl_Interp *ip, int fd, const char *m);
107 void cht_objfreeir(Tcl_Obj *o);
108 int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
109
110 void cht_obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
111 void cht_obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
112                                 int l, const char *prefix);
113
114 void cht_obj_updatestr_vstringls(Tcl_Obj *o, ...);
115   /* const char*, int, const char*, int, ..., (const char*)0 */
116 void cht_obj_updatestr_string_len(Tcl_Obj *o, const char *str, int l);
117 void cht_obj_updatestr_string(Tcl_Obj *o, const char *str);
118
119 /* from parse.c */
120
121 typedef struct {
122   Tcl_Obj *obj, *var;
123   int copied;
124 } Something_Var;
125
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);
130
131 /* from enum.c */
132
133 extern Tcl_ObjType cht_enum_nearlytype;
134 extern Tcl_ObjType cht_enum1_nearlytype;
135
136 const void *cht_enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
137                                     const void *firstentry, size_t entrysize,
138                                     const char *what);
139 #define enum_lookup_cached(ip,o,table,what)                     \
140     (cht_enum_lookup_cached_func((ip),(o),                              \
141                              &(table)[0],sizeof((table)[0]),    \
142                              (what)))
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.
148    */
149
150 int cht_enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
151                              const char *opts, const char *what);
152   /* -1 => error */
153
154 /* useful macros */
155
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)))
159
160 #endif /*CHIARK_TCL_H*/