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