chiark / gitweb /
slight improvements to scriptinv, including scriptinv_invoke_fg
[chiark-tcl.git] / base / chiark-tcl.h
index f136758fdfe8be32f4d29235cbf45297588887c1..f3f0922bdc1320708687bc25c23f3015f6b57979 100644 (file)
 /*
  */
-/*
- *  hbytes raw2h BINARY                          => hex
- *  hbytes h2raw HEX                             => binary
- *
- *  hbytes prefix VAR [VALUE ...]         = set VAR [concat VALUE ... $VAR]
- *  hbytes append VAR [VALUE ...]         = set VAR [concat $VAR VALUE ...]
- *  hbytes concat VAR [VALUE ...]         = set VAR [concat VALUE ...]
- *  hbytes unprepend VAR PREFIXLENGTH            => prefix (removed from VAR)
- *  hbytes unappend VAR SUFFIXLENGTH             => suffix (removed from VAR)
- *  hbytes chopto VAR NEWVARLENGTH               => suffix (removed from VAR)
- *                                                  (too short? error)
- *
- *  hbytes pkcs5 pa|ua VAR ALG                   => worked?  (always 1 for p)
- *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
- *  hbytes blockcipher d|e VAR ALG MODE [IV]     => IV
- *
- *  hbytes hash ALG MESSAGE                      => hash
- *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
- *
- * Refs: HMAC: RFC2104
- */
 
-#ifndef HBYTES_H
-#define HBYTES_H
+#ifndef CHIARK_TCL_H
+#define CHIARK_TCL_H
 
 #include <assert.h>
 #include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/socket.h>
+#include <sys/uio.h>
+#include <sys/un.h>
+#include <arpa/inet.h>
+
+#include <tcl8.3/tcl.h>
 
-#include <tcl.h>
+#include <adns.h>
 
 typedef unsigned char Byte;
 
-/* from hbytes.c */
+/* for assisting tcmdifgen and tcmdiflib.c */
+
+typedef struct TopLevel_Command TopLevel_Command;
+
+struct TopLevel_Command {
+  const char *name;
+  Tcl_ObjCmdProc *func;
+};
+
+void cht_setstringresult(Tcl_Interp*, const char*);
+int cht_pat_enum(Tcl_Interp*, Tcl_Obj*, const void**,
+                const void*, size_t, const char *what);
+
+/* from scriptinv.c */
+
+typedef struct { /* semi-opaque - read only, and then only where commented */
+  Tcl_Interp *ip; /* valid, non-0 and useable if set */
+  Tcl_Obj *obj; /* non-0 iff set (but only test for 0/non-0) */
+  Tcl_Obj *xargs;
+  int llength; /* after set, is llength of script + xargs */
+} ScriptToInvoke;
+
+void cht_scriptinv_init(ScriptToInvoke *si);
+int cht_scriptinv_set(ScriptToInvoke *si, Tcl_Interp *ip,
+                 Tcl_Obj *newscript, Tcl_Obj *xargs);
+void cht_scriptinv_cancel(ScriptToInvoke *si); /* then don't invoke */
+  /* no separate free function - just cancel */
+
+int cht_scriptinv_invoke_fg(ScriptToInvoke *si, int argc,
+                           Tcl_Obj *const *argv);
+  /* if script fails, returns that error */
+
+void cht_scriptinv_invoke(ScriptToInvoke *si, int argc, Tcl_Obj *const *argv);
+  /* if script fails, reports it with Tcl_BackgroundError */
+
+/* from idtable.c */
 
 typedef struct {
-  Byte *start, *end; /* always allocated dynamically */
-} HBytes_Value; /* overlays internalRep */
+  const char *valprefix, *assockey;
+  void (*destroyitem)(Tcl_Interp *ip, void *val);
+} IdDataSpec;
+
+/* The stored struct must start with a single int, conventionally
+ * named `ix'.  When the struct is returned for the first time ix must
+ * be -1; on subsequent occasions it must be >=0.  ix will be -1 iff
+ * the struct is registered by the iddatatable machinery. */
+
+extern Tcl_ObjType cht_tabledataid_nearlytype;
+int cht_tabledataid_parse(Tcl_Interp *ip, Tcl_Obj *o, const IdDataSpec *idds);
+void cht_tabledataid_disposing(Tcl_Interp *ip, void *val, const IdDataSpec *idds);
+  /* call this when you destroy the struct, to remove its name;
+   * _disposing is idempotent */
+
+/* from hook.c */
+
+int cht_initextension(Tcl_Interp *ip, const TopLevel_Command *cmds,
+                     int *donep /* or 0, meaning no types follow */,
+                     ... /* types, terminated by 0 */);
+
+int cht_staticerr(Tcl_Interp *ip, const char *m, const char *ec);
+int cht_posixerr(Tcl_Interp *ip, int errnoval, const char *m);
+int cht_newfdposixerr(Tcl_Interp *ip, int fd, const char *m);
+void cht_objfreeir(Tcl_Obj *o);
+int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
 
-extern Tcl_ObjType hbytes_type;
+void cht_obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
+void cht_obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
+                               int l, const char *prefix);
 
-int staticerr(Tcl_Interp *ip, const char *m);
-void objfreeir(Tcl_Obj *o);
+void cht_obj_updatestr_vstringls(Tcl_Obj *o, ...);
+  /* const char*, int, const char*, int, ..., (const char*)0 */
+void cht_obj_updatestr_string_len(Tcl_Obj *o, const char *str, int l);
+void cht_obj_updatestr_string(Tcl_Obj *o, const char *str);
 
-Tcl_Obj *hbytes_set(Tcl_Obj *overwrite, const Byte *array, int l);
+/* from parse.c */
+
+typedef struct {
+  Tcl_Obj *obj, *var;
+  int copied;
+} Something_Var;
+
+void cht_init_somethingv(Something_Var *sth);
+void cht_fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth);
+int cht_pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
+                  Something_Var *sth, Tcl_ObjType *type);
 
 /* from enum.c */
 
-extern Tcl_ObjType enum_nearlytype;
+extern Tcl_ObjType cht_enum_nearlytype;
+extern Tcl_ObjType cht_enum1_nearlytype;
 
-const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
-                                   size_t entrysize, const void *firstentry,
+const void *cht_enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
+                                   const void *firstentry, size_t entrysize,
                                    const char *what);
 #define enum_lookup_cached(ip,o,table,what)                    \
-    (enum_lookup_cached_func((ip),(o),                         \
-                            sizeof((table)[0]),&(table)[0],    \
+    (cht_enum_lookup_cached_func((ip),(o),                             \
+                            &(table)[0],sizeof((table)[0]),    \
                             (what)))
   /* table should be a pointer to an array of structs of size
    * entrysize, the first member of which should be a const char*.
@@ -63,12 +125,14 @@ const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
    * set to the error message.
    */
 
-/* useful macros */
+int cht_enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
+                            const char *opts, const char *what);
+  /* -1 => error */
 
-#define HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
-#define HBYTES_LEN(o) (HBYTES((o))->end - HBYTES((o))->start)
+/* useful macros */
 
 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
 #define TFREE(f) (Tcl_Free((void*)(f)))
+#define TREALLOC(p,l) ((void*)Tcl_Realloc((void*)(p),(l)))
 
-#endif /*HBYTES_H*/
+#endif /*CHIARK_TCL_H*/