2 * base code for various Tcl extensions
3 * Copyright 2006 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 #include "chiark-tcl-base.h"
23 int cht_staticerr(Tcl_Interp *ip, const char *m, const char *ec) {
24 Tcl_SetResult(ip, (char*)m, TCL_STATIC);
25 if (ec) Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(ec,-1));
29 int cht_posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
34 em= Tcl_PosixError(ip);
35 Tcl_AppendResult(ip, m, ": ", em, (char*)0);
39 int cht_newfdposixerr(Tcl_Interp *ip, int fd, const char *m) {
43 return cht_posixerr(ip,e,m);
46 void cht_objfreeir(Tcl_Obj *o) {
47 if (o->typePtr && o->typePtr->freeIntRepProc)
48 o->typePtr->freeIntRepProc(o);
52 void cht_obj_updatestr_vstringls(Tcl_Obj *o, ...) {
60 for (l=0; (part= va_arg(al, const char*)); ) {
61 pl= va_arg(al, size_t);
62 assert(pl <= INT_MAX/2 - l);
68 o->bytes= TALLOC(l+1);
71 for (p= o->bytes; (part= va_arg(al, const char*)); p += pl) {
72 pl= va_arg(al, size_t);
80 void cht_obj_updatestr_string(Tcl_Obj *o, const char *str) {
81 cht_obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
84 #define URANDOM "/dev/urandom"
86 int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
92 urandom= fopen(URANDOM,"rb");
93 if (!urandom) return cht_posixerr(ip,errno,"open " URANDOM);
95 r= fread(buffer,1,l,urandom);
99 fclose(urandom); urandom=0;
101 if (ferror(urandom)) {
102 return cht_posixerr(ip,errno,"read " URANDOM);
104 assert(feof(urandom));
105 return cht_staticerr(ip, URANDOM " gave eof!", 0);
109 void cht_prepare__basic(Tcl_Interp *ip) {
112 if (prepared) return;
113 Tcl_RegisterObjType(&cht_tabledataid_nearlytype);
114 Tcl_RegisterObjType(&cht_enum_nearlytype);
115 Tcl_RegisterObjType(&cht_enum1_nearlytype);
119 void cht_setup__commands(Tcl_Interp *ip, const TopLevel_Command *cmds) {
120 const TopLevel_Command *cmd;
125 Tcl_CreateObjCommand(ip, (char*)cmd->name, cmd->func, 0,0);