chiark / gitweb /
0cb2f9d47961615e8dfde1af3966e2198401edcf
[chiark-tcl.git] / base / hook.c
1 /*
2  * base code for various Tcl extensions
3  * Copyright 2006-2012 Ian Jackson
4  *
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.
9  *
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.
14  *
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
18  * 02110-1301, USA.
19  */
20
21 #include "chiark-tcl-base.h"
22
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));
26   return TCL_ERROR;
27 }
28
29 int cht_posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
30   const char *em;
31   
32   Tcl_ResetResult(ip);
33   errno= errnoval;
34   em= Tcl_PosixError(ip);
35   Tcl_AppendResult(ip, m, ": ", em, (char*)0);
36   return TCL_ERROR;
37 }
38
39 int cht_newfdposixerr(Tcl_Interp *ip, int fd, const char *m) {
40   int e;
41   e= errno;
42   close(fd);
43   return cht_posixerr(ip,e,m);
44 }
45
46 void cht_objfreeir(Tcl_Obj *o) {
47   if (o->typePtr && o->typePtr->freeIntRepProc)
48     o->typePtr->freeIntRepProc(o);
49   o->typePtr= 0;
50 }  
51
52 void cht_obj_updatestr_vstringls(Tcl_Obj *o, ...) {
53   va_list al;
54   char *p;
55   const char *part;
56   int l;
57   size_t pl;
58
59   va_start(al,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);
63     l += pl;
64   }
65   va_end(al);
66   
67   o->length= l;
68   o->bytes= TALLOC(l+1);
69
70   va_start(al,o);
71   for (p= o->bytes; (part= va_arg(al, const char*)); p += pl) {
72     pl= va_arg(al, size_t);
73     memcpy(p, part, pl);
74   }
75   va_end(al);
76
77   *p= 0;
78 }
79
80 void cht_obj_updatestr_string(Tcl_Obj *o, const char *str) {
81   cht_obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
82 }
83
84 #define URANDOM "/dev/urandom"
85
86 int cht_get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
87   static FILE *urandom;
88
89   int r, esave;
90
91   if (!urandom) {
92     urandom= fopen(URANDOM,"rb");
93     if (!urandom) return cht_posixerr(ip,errno,"open " URANDOM);
94   }
95   r= fread(buffer,1,l,urandom);
96   if (r==l) return 0;
97
98   esave= errno;
99   fclose(urandom); urandom=0;
100
101   if (ferror(urandom)) {
102     return cht_posixerr(ip,errno,"read " URANDOM);
103   } else {
104     assert(feof(urandom));
105     return cht_staticerr(ip, URANDOM " gave eof!", 0);
106   }
107 }
108
109 void cht_prepare__basic(Tcl_Interp *ip) {
110   static int prepared;
111
112   if (prepared) return;
113   Tcl_RegisterObjType(&cht_tabledataid_nearlytype);
114   Tcl_RegisterObjType(&cht_enum_nearlytype);
115   Tcl_RegisterObjType(&cht_enum1_nearlytype);
116   prepared= 1;
117 }
118
119 void cht_setup__commands(Tcl_Interp *ip, const TopLevel_Command *cmds) {
120   const TopLevel_Command *cmd;
121   
122   for (cmd= cmds;
123        cmd->name;
124        cmd++)
125     Tcl_CreateObjCommand(ip, (char*)cmd->name, cmd->func, 0,0);
126 }