chiark / gitweb /
fix up opening and closing
[chiark-tcl.git] / base / chiark-tcl.h
1 /*
2  */
3
4 #ifndef CHIARK_TCL_H
5 #define CHIARK_TCL_H
6
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <errno.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <sys/uio.h>
14 #include <sys/un.h>
15 #include <arpa/inet.h>
16
17 #include <tcl8.3/tcl.h>
18
19 #include <adns.h>
20
21 typedef unsigned char Byte;
22
23 /* for assisting tcmdifgen and tcmdiflib.c */
24
25 typedef struct TopLevel_Command TopLevel_Command;
26
27 struct TopLevel_Command {
28   const char *name;
29   Tcl_ObjCmdProc *func;
30 };
31
32 void cht_setstringresult(Tcl_Interp*, const char*);
33 int cht_pat_enum(Tcl_Interp*, Tcl_Obj*, const void**,
34                  const void*, size_t, const char *what);
35
36 /* from scriptinv.c */
37
38 typedef struct { /* opaque; comments are for scriptinv.c impl'n only */
39   /* states:                Cancelled       Set                          */
40   Tcl_Interp *ipq;     /*    0               valid, non-0, useable       */
41   Tcl_Obj *script;     /*    0               valid, non-0                */
42   Tcl_Obj *xargs;      /*    0               valid, may be 0             */
43   int llen;            /*    undefined       llength of script + xargs   */
44 } ScriptToInvoke;
45
46 void cht_scriptinv_init(ScriptToInvoke *si); /* undefined -> Cancelled */
47 int cht_scriptinv_set(ScriptToInvoke *si, Tcl_Interp *ip,
48                       Tcl_Obj *newscript, Tcl_Obj *xargs);
49   /* Cancelled/Set -> Set (newscript!=0, ok) / Cancelled (otherwise) */
50 void cht_scriptinv_cancel(ScriptToInvoke *si);
51   /* Cancelled/Set -> Cancelled.  No separate free function - just cancel. */
52 #define cht_scriptinv_interp(si) ((si)->ipq)
53   /* int cht_scriptinv_interp(ScriptToInvoke *si);  returns 0 if Cancelled */
54
55 int cht_scriptinv_invoke_fg(ScriptToInvoke *si, int argc,
56                             Tcl_Obj *const *argv);
57   /* is a no-op if Cancelled rather than Set */
58   /* if script fails, returns that error */
59
60 void cht_scriptinv_invoke(ScriptToInvoke *si, int argc, Tcl_Obj *const *argv);
61   /* if script fails, reports it with Tcl_BackgroundError */
62
63 /* from idtable.c */
64
65 typedef struct {
66   const char *valprefix, *assockey;
67   void (*destroyitem)(Tcl_Interp *ip, void *val);
68 } IdDataSpec;
69
70 /* The stored struct must start with a single int, conventionally
71  * named `ix'.  When the struct is returned for the first time ix must
72  * be -1; on subsequent occasions it must be >=0.  ix will be -1 iff
73  * the struct is registered by the iddatatable machinery. */
74
75 extern Tcl_ObjType cht_tabledataid_nearlytype;
76 int cht_tabledataid_parse(Tcl_Interp *ip, Tcl_Obj *o, const IdDataSpec *idds);
77 void cht_tabledataid_disposing(Tcl_Interp *ip, void *val, const IdDataSpec *idds);
78   /* call this when you destroy the struct, to remove its name;
79    * _disposing is idempotent */
80
81 /* from hook.c */
82
83 int cht_initextension(Tcl_Interp *ip, const TopLevel_Command *cmds,
84                       int *donep /* or 0, meaning no types follow */,
85                       ... /* types, terminated by 0 */);
86
87 int cht_staticerr(Tcl_Interp *ip, const char *m, const char *ec);
88 int cht_posixerr(Tcl_Interp *ip, int errnoval, const char *m);
89 int cht_newfdposixerr(Tcl_Interp *ip, int fd, const char *m);
90 void cht_objfreeir(Tcl_Obj *o);
91 int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
92
93 void cht_obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
94 void cht_obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
95                                 int l, const char *prefix);
96
97 void cht_obj_updatestr_vstringls(Tcl_Obj *o, ...);
98   /* const char*, int, const char*, int, ..., (const char*)0 */
99 void cht_obj_updatestr_string_len(Tcl_Obj *o, const char *str, int l);
100 void cht_obj_updatestr_string(Tcl_Obj *o, const char *str);
101
102 /* from parse.c */
103
104 typedef struct {
105   Tcl_Obj *obj, *var;
106   int copied;
107 } Something_Var;
108
109 void cht_init_somethingv(Something_Var *sth);
110 void cht_fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth);
111 int cht_pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
112                    Something_Var *sth, Tcl_ObjType *type);
113
114 /* from enum.c */
115
116 extern Tcl_ObjType cht_enum_nearlytype;
117 extern Tcl_ObjType cht_enum1_nearlytype;
118
119 const void *cht_enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
120                                     const void *firstentry, size_t entrysize,
121                                     const char *what);
122 #define enum_lookup_cached(ip,o,table,what)                     \
123     (cht_enum_lookup_cached_func((ip),(o),                              \
124                              &(table)[0],sizeof((table)[0]),    \
125                              (what)))
126   /* table should be a pointer to an array of structs of size
127    * entrysize, the first member of which should be a const char*.
128    * The table should finish with a null const char *.
129    * On error, 0 is returned and the ip->result will have been
130    * set to the error message.
131    */
132
133 int cht_enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
134                              const char *opts, const char *what);
135   /* -1 => error */
136
137 /* useful macros */
138
139 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
140 #define TFREE(f) (Tcl_Free((void*)(f)))
141 #define TREALLOC(p,l) ((void*)Tcl_Realloc((void*)(p),(l)))
142
143 #endif /*CHIARK_TCL_H*/