2 * base code for various Tcl extensions
3 * Copyright 2006-2012 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, see <http://www.gnu.org/licenses/>.
19 #include "chiark-tcl-base.h"
21 int cht_staticerr(Tcl_Interp *ip, const char *m, const char *ec) {
22 Tcl_SetResult(ip, (char*)m, TCL_STATIC);
23 if (ec) Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(ec,-1));
27 int cht_posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
32 em= Tcl_PosixError(ip);
33 Tcl_AppendResult(ip, m, ": ", em, (char*)0);
37 int cht_newfdposixerr(Tcl_Interp *ip, int fd, const char *m) {
41 return cht_posixerr(ip,e,m);
44 void cht_objfreeir(Tcl_Obj *o) {
45 if (o->typePtr && o->typePtr->freeIntRepProc)
46 o->typePtr->freeIntRepProc(o);
50 void cht_obj_updatestr_vstringls(Tcl_Obj *o, ...) {
58 for (l=0; (part= va_arg(al, const char*)); ) {
59 pl= va_arg(al, size_t);
60 assert(pl <= INT_MAX/2 - l);
66 o->bytes= TALLOC(l+1);
69 for (p= o->bytes; (part= va_arg(al, const char*)); p += pl) {
70 pl= va_arg(al, size_t);
78 void cht_obj_updatestr_string(Tcl_Obj *o, const char *str) {
79 cht_obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
82 #define URANDOM "/dev/urandom"
84 int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
90 urandom= fopen(URANDOM,"rb");
91 if (!urandom) return cht_posixerr(ip,errno,"open " URANDOM);
93 r= fread(buffer,1,l,urandom);
96 if (ferror(urandom)) {
97 r = cht_posixerr(ip,errno,"read " URANDOM);
99 assert(feof(urandom));
100 r = cht_staticerr(ip, URANDOM " gave eof!", 0);
102 fclose(urandom); urandom=0;
106 void cht_prepare__basic(Tcl_Interp *ip) {
109 if (prepared) return;
110 Tcl_RegisterObjType(&cht_tabledataid_nearlytype);
111 Tcl_RegisterObjType(&cht_enum_nearlytype);
112 Tcl_RegisterObjType(&cht_enum1_nearlytype);
116 void cht_setup__commands(Tcl_Interp *ip, const TopLevel_Command *cmds) {
117 const TopLevel_Command *cmd;
122 Tcl_CreateObjCommand(ip, (char*)cmd->name, cmd->func, 0,0);