6 #include "chiark-tcl.h"
8 int cht_staticerr(Tcl_Interp *ip, const char *m, const char *ec) {
9 Tcl_SetResult(ip, (char*)m, TCL_STATIC);
10 if (ec) Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(ec,-1));
14 int cht_posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
19 em= Tcl_PosixError(ip);
20 Tcl_AppendResult(ip, m, ": ", em, (char*)0);
24 int cht_newfdposixerr(Tcl_Interp *ip, int fd, const char *m) {
28 return cht_posixerr(ip,e,m);
31 void cht_objfreeir(Tcl_Obj *o) {
32 if (o->typePtr && o->typePtr->freeIntRepProc)
33 o->typePtr->freeIntRepProc(o);
37 void cht_obj_updatestr_vstringls(Tcl_Obj *o, ...) {
44 for (l=0; (part= va_arg(al, const char*)); )
49 o->bytes= TALLOC(l+1);
52 for (p= o->bytes; (part= va_arg(al, const char*)); p += pl) {
61 void cht_obj_updatestr_string(Tcl_Obj *o, const char *str) {
62 cht_obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
65 #define URANDOM "/dev/urandom"
67 int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
73 urandom= fopen(URANDOM,"rb");
74 if (!urandom) return cht_posixerr(ip,errno,"open " URANDOM);
76 r= fread(buffer,1,l,urandom);
80 fclose(urandom); urandom=0;
82 if (ferror(urandom)) {
83 return cht_posixerr(ip,errno,"read " URANDOM);
85 assert(feof(urandom));
86 return cht_staticerr(ip, URANDOM " gave eof!", 0);
90 int cht_initextension(Tcl_Interp *ip, const TopLevel_Command *cmds,
91 int *donep /* or 0, meaning no types follow */,
92 ... /* types, terminated by 0 */) {
95 const TopLevel_Command *cmd;
102 Tcl_RegisterObjType(&cht_tabledataid_nearlytype);
103 Tcl_RegisterObjType(&cht_enum_nearlytype);
104 Tcl_RegisterObjType(&cht_enum1_nearlytype);
107 if (donep && !*donep) {
110 while ((ot= va_arg(al, Tcl_ObjType*)))
111 Tcl_RegisterObjType(ot);
117 Tcl_CreateObjCommand(ip, (char*)cmd->name, cmd->func, 0,0);