chiark / gitweb /
Debian package wip
[chiark-tcl.git] / base / chiark-tcl.h
index a545edeae9f4cd949516d78dc09ef55034417724..2f80d58172904c0eea95e3feef49fe1e04e294da 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)
+ * base code for various Tcl extensions
+ * Copyright 2006 Ian Jackson
  *
- *  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
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
  *
- *  hbytes hash ALG MESSAGE                      => hash
- *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
  *
- * Refs: HMAC: RFC2104
+ * You should have received a copy of the GNU General Public License
+ * along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
  */
 
-#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 <tcl.h>
+#include <tcl8.3/tcl.h>
+
+#include <adns.h>
 
 typedef unsigned char Byte;
 
-/* from hbytes.c */
+/* for assisting tcmdifgen and tcmdiflib.c */
 
-typedef struct {
-  Byte *start, *end; /* always allocated dynamically */
-} HBytes_Value; /* overlays internalRep */
+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 { /* opaque; comments are for scriptinv.c impl'n only */
+  /* states:                Cancelled       Set                          */
+  Tcl_Interp *ipq;     /*    0               valid, non-0, useable       */
+  Tcl_Obj *script;     /*    0               valid, non-0                */
+  Tcl_Obj *xargs;      /*    0               valid, may be 0             */
+  int llen;            /*    undefined       llength of script + xargs   */
+} ScriptToInvoke;
+
+void cht_scriptinv_init(ScriptToInvoke *si); /* undefined -> Cancelled */
+int cht_scriptinv_set(ScriptToInvoke *si, Tcl_Interp *ip,
+                     Tcl_Obj *newscript, Tcl_Obj *xargs);
+  /* Cancelled/Set -> Set (newscript!=0, ok) / Cancelled (otherwise) */
+void cht_scriptinv_cancel(ScriptToInvoke *si);
+  /* Cancelled/Set -> Cancelled.  No separate free function - just cancel. */
+#define cht_scriptinv_interp(si) ((si)->ipq)
+  /* int cht_scriptinv_interp(ScriptToInvoke *si);  returns 0 if Cancelled */
 
-extern Tcl_ObjType hbytes_type;
+int cht_scriptinv_invoke_fg(ScriptToInvoke *si, int argc,
+                           Tcl_Obj *const *argv);
+  /* is a no-op if Cancelled rather than Set */
+  /* if script fails, returns that error */
 
-int staticerr(Tcl_Interp *ip, const char *m);
-void objfreeir(Tcl_Obj *o);
+void cht_scriptinv_invoke(ScriptToInvoke *si, int argc, Tcl_Obj *const *argv);
+  /* if script fails, reports it with Tcl_BackgroundError */
 
-void hbytes_set(HBytes_Value *upd, const Byte *array, int l);
-Tcl_Obj *hbytes_set_obj(Tcl_Obj *overwrite, const Byte *array, int l);
+/* from idtable.c */
+
+typedef struct {
+  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);
+
+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);
+
+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);
 
 /* from parse.c */
 
 typedef struct {
-  HBytes_Value *hb;
   Tcl_Obj *obj, *var;
-} HBytes_Var;
+  int copied;
+} Something_Var;
 
-void fini_hbv(Tcl_Interp *ip, int rc, HBytes_Var *agg);
+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 enum1_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,
+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*.
@@ -74,30 +147,14 @@ const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
    * set to the error message.
    */
 
-int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
+int cht_enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
                             const char *opts, const char *what);
   /* -1 => error */
 
-/* from crypto.c */
-
-typedef struct {
-  int blocksize, hashsize;
-} HashAlgInfo;
-
-typedef struct {
-  int blocksize;
-} BlockCipherAlgInfo;
-
-typedef struct {
-  int dummy;
-} BlockCipherModeInfo;
-
 /* useful macros */
 
-#define HBYTES(o) (*(HBytes_Value*)&(o)->internalRep.twoPtrValue)
-#define HBYTES_LEN(hb) ((hb).end - (hb).start)
-
 #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*/