#include <errno.h>
-#include "hbytes.h"
-#include "tables.h"
+#include "chiark-tcl.h"
-int staticerr(Tcl_Interp *ip, const char *m) {
+int cht_staticerr(Tcl_Interp *ip, const char *m, const char *ec) {
Tcl_SetResult(ip, (char*)m, TCL_STATIC);
+ if (ec) Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(ec,-1));
return TCL_ERROR;
}
-int posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
+int cht_posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
const char *em;
Tcl_ResetResult(ip);
return TCL_ERROR;
}
-void objfreeir(Tcl_Obj *o) {
+int cht_newfdposixerr(Tcl_Interp *ip, int fd, const char *m) {
+ int e;
+ e= errno;
+ close(fd);
+ return cht_posixerr(ip,e,m);
+}
+
+void cht_objfreeir(Tcl_Obj *o) {
if (o->typePtr && o->typePtr->freeIntRepProc)
o->typePtr->freeIntRepProc(o);
o->typePtr= 0;
}
-int do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip,
- Tcl_Obj *obj, Tcl_Obj **result) {
- const char *tn;
- int nums[3], i, lnl;
- Tcl_Obj *objl[4];
-
- if (obj->typePtr == &hbytes_type) {
- HBytes_Value *v= OBJ_HBYTES(obj);
- memset(nums,0,sizeof(nums));
- nums[1]= hbytes_len(v);
-
- if (HBYTES_ISEMPTY(v)) tn= "empty";
- else if (HBYTES_ISSENTINEL(v)) tn= "sentinel!";
- else if (HBYTES_ISSIMPLE(v)) tn= "simple";
- else {
- HBytes_ComplexValue *cx= v->begin_complex;
- tn= "complex";
- nums[0]= cx->prespace;
- nums[2]= cx->avail - cx->len;
- }
- lnl= 3;
- } else {
- tn= "other";
- lnl= 0;
- }
-
- objl[0]= Tcl_NewStringObj((char*)tn,-1);
- for (i=0; i<lnl; i++) objl[i+1]= Tcl_NewIntObj(nums[i]);
- *result= Tcl_NewListObj(lnl+1,objl);
-
- return TCL_OK;
-}
-
-static void hbytes_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
- hbytes_array(OBJ_HBYTES(dup),
- hbytes_data(OBJ_HBYTES(src)),
- hbytes_len(OBJ_HBYTES(src)));
- dup->typePtr= &hbytes_type;
-}
-
-static void hbytes_t_free(Tcl_Obj *o) {
- hbytes_free(OBJ_HBYTES(o));
-}
-
-void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
- int l, const char *prefix) {
- char *str;
- int pl;
-
- pl= strlen(prefix);
- o->length= l*2+pl;
- str= o->bytes= TALLOC(o->length+1);
-
- memcpy(str,prefix,pl);
- str += pl;
-
- while (l>0) {
- sprintf(str,"%02x",*byte);
- str+=2; byte++; l--;
- }
- *str= 0;
-}
-
-void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
- obj_updatestr_array_prefix(o,byte,l,"");
-}
-
-void obj_updatestr_vstringls(Tcl_Obj *o, ...) {
+void cht_obj_updatestr_vstringls(Tcl_Obj *o, ...) {
va_list al;
char *p;
const char *part;
*p= 0;
}
-void obj_updatestr_string(Tcl_Obj *o, const char *str) {
- obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
-}
-
-static void hbytes_t_ustr(Tcl_Obj *o) {
- obj_updatestr_array(o,
- hbytes_data(OBJ_HBYTES(o)),
- hbytes_len(OBJ_HBYTES(o)));
-}
-
-static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
- char *str, *ep, *os;
- Byte *startbytes, *bytes;
- int l;
- char cbuf[3];
-
- os= str= Tcl_GetStringFromObj(o,&l); assert(str);
- objfreeir(o);
-
- if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
- " odd length in hex");
-
- startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
-
- cbuf[2]= 0;
- while (l>0) {
- cbuf[0]= *str++;
- cbuf[1]= *str++;
- *bytes++= strtoul(cbuf,&ep,16);
- if (ep != cbuf+2) {
- hbytes_free(OBJ_HBYTES(o));
- return staticerr(ip, "hbytes: conversion from hex:"
- " bad hex digit");
- }
- l -= 2;
- }
-
- o->typePtr = &hbytes_type;
- return TCL_OK;
-}
-
-Tcl_ObjType hbytes_type = {
- "hbytes",
- hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
-};
-
-int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
- Tcl_Obj *binary, HBytes_Value *result) {
- const unsigned char *str;
- int l;
-
- str= Tcl_GetByteArrayFromObj(binary,&l);
- hbytes_array(result, str, l);
- return TCL_OK;
-}
-
-int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
- HBytes_Value hex, Tcl_Obj **result) {
- *result= Tcl_NewByteArrayObj(hbytes_data(&hex), hbytes_len(&hex));
- return TCL_OK;
-}
-
-int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
- HBytes_Value v, int *result) {
- *result= hbytes_len(&v);
- return TCL_OK;
-}
-
-int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
- int length, HBytes_Value *result) {
- Byte *space;
- int rc;
-
- space= hbytes_arrayspace(result, length);
- rc= get_urandom(ip, space, length);
- if (rc) { hbytes_free(result); return rc; }
- return TCL_OK;
-}
-
-int do_hbytes_overwrite(ClientData cd, Tcl_Interp *ip,
- HBytes_Var v, int start, HBytes_Value sub) {
- int sub_l;
-
- sub_l= hbytes_len(&sub);
- if (start < 0)
- return staticerr(ip, "hbytes overwrite start -ve");
- if (start + sub_l > hbytes_len(v.hb))
- return staticerr(ip, "hbytes overwrite out of range");
- memcpy(hbytes_data(v.hb) + start, hbytes_data(&sub), sub_l);
- return TCL_OK;
-}
-
-int do_hbytes_trimleft(ClientData cd, Tcl_Interp *ip, HBytes_Var v) {
- const Byte *o, *p, *e;
- o= p= hbytes_data(v.hb);
- e= p + hbytes_len(v.hb);
-
- while (p<e && !*p) p++;
- if (p != o)
- hbytes_unprepend(v.hb, p-o);
-
- return TCL_OK;
-}
-
-int do_hbytes_repeat(ClientData cd, Tcl_Interp *ip,
- HBytes_Value sub, int count, HBytes_Value *result) {
- int sub_l;
- Byte *data;
- const Byte *sub_d;
-
- sub_l= hbytes_len(&sub);
- if (count < 0) return staticerr(ip, "hbytes repeat count -ve");
- if (count > INT_MAX/sub_l) return staticerr(ip, "hbytes repeat too long");
-
- data= hbytes_arrayspace(result, sub_l*count);
- sub_d= hbytes_data(&sub);
- while (count) {
- memcpy(data, sub_d, sub_l);
- count--; data += sub_l;
- }
- return TCL_OK;
-}
-
-int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
- int length, HBytes_Value *result) {
- Byte *space;
- space= hbytes_arrayspace(result, length);
- memset(space,0,length);
- return TCL_OK;
-}
-
-int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
- HBytes_Value a, HBytes_Value b, int *result) {
- int al, bl, minl, r;
-
- al= hbytes_len(&a);
- bl= hbytes_len(&b);
- minl= al<bl ? al : bl;
-
- r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
-
- if (r<0) *result= -2;
- else if (r>0) *result= +2;
- else {
- if (al<bl) *result= -1;
- else if (al>bl) *result= +1;
- else *result= 0;
- }
- return TCL_OK;
-}
-
-int do_hbytes_range(ClientData cd, Tcl_Interp *ip,
- HBytes_Value v, int start, int size,
- HBytes_Value *result) {
- const Byte *data;
- int l;
-
- l= hbytes_len(&v);
- if (start<0 || size<0 || l<start+size)
- return staticerr(ip, "hbytes range subscripts out of range");
-
- data= hbytes_data(&v);
- hbytes_array(result, data+start, size);
- return TCL_OK;
-}
-
-int do_toplevel_hbytes(ClientData cd, Tcl_Interp *ip,
- const HBytes_SubCommand *subcmd,
- int objc, Tcl_Obj *const *objv) {
- return subcmd->func(0,ip,objc,objv);
-}
-
-int do_toplevel_dgram_socket(ClientData cd, Tcl_Interp *ip,
- const DgramSocket_SubCommand *subcmd,
- int objc, Tcl_Obj *const *objv) {
- return subcmd->func(0,ip,objc,objv);
-}
-
-int do_toplevel_ulong(ClientData cd, Tcl_Interp *ip,
- const ULong_SubCommand *subcmd,
- int objc, Tcl_Obj *const *objv) {
- return subcmd->func(0,ip,objc,objv);
+void cht_obj_updatestr_string(Tcl_Obj *o, const char *str) {
+ cht_obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
}
#define URANDOM "/dev/urandom"
-int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
+int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
static FILE *urandom;
int r, esave;
if (!urandom) {
urandom= fopen(URANDOM,"rb");
- if (!urandom) return posixerr(ip,errno,"open " URANDOM);
+ if (!urandom) return cht_posixerr(ip,errno,"open " URANDOM);
}
r= fread(buffer,1,l,urandom);
if (r==l) return 0;
fclose(urandom); urandom=0;
if (ferror(urandom)) {
- return posixerr(ip,errno,"read " URANDOM);
+ return cht_posixerr(ip,errno,"read " URANDOM);
} else {
assert(feof(urandom));
- return staticerr(ip, URANDOM " gave eof!");
+ return cht_staticerr(ip, URANDOM " gave eof!", 0);
}
}
-int Hbytes_Init(Tcl_Interp *ip) {
+int cht_initextension(Tcl_Interp *ip, const TopLevel_Command *cmds,
+ int *donep /* or 0, meaning no types follow */,
+ ... /* types, terminated by 0 */) {
+ static int cht_initd;
+
const TopLevel_Command *cmd;
+ Tcl_ObjType *ot;
- Tcl_RegisterObjType(&hbytes_type);
- Tcl_RegisterObjType(&blockcipherkey_type);
- Tcl_RegisterObjType(&enum_nearlytype);
- Tcl_RegisterObjType(&enum1_nearlytype);
- Tcl_RegisterObjType(&sockaddr_type);
- Tcl_RegisterObjType(&dgramsockid_type);
- Tcl_RegisterObjType(&ulong_type);
+ va_list al;
+
+ if (!cht_initd) {
+ cht_initd= 1;
+ Tcl_RegisterObjType(&cht_tabledataid_nearlytype);
+ Tcl_RegisterObjType(&cht_enum_nearlytype);
+ Tcl_RegisterObjType(&cht_enum1_nearlytype);
+ }
+
+ if (donep && !*donep) {
+ *donep= 1;
+ va_start(al, donep);
+ while ((ot= va_arg(al, Tcl_ObjType*)))
+ Tcl_RegisterObjType(ot);
+ }
- for (cmd=toplevel_commands;
+ for (cmd= cmds;
cmd->name;
cmd++)
- Tcl_CreateObjCommand(ip, cmd->name, cmd->func, 0,0);
+ Tcl_CreateObjCommand(ip, (char*)cmd->name, cmd->func, 0,0);
return TCL_OK;
}