fix tcl issues ... build with pre tcl8.4 ... do not try to install into the tcl tree...
[rrdtool.git] / bindings / tcl / tclrrd.c
1 /*
2  * tclrrd.c -- A TCL interpreter extension to access the RRD library.
3  *
4  * Copyright (c) 1999,2000 Frank Strauss, Technical University of Braunschweig.
5  *
6  * Thread-safe code copyright (c) 2005 Oleg Derevenetz, CenterTelecom Voronezh ISP.
7  *
8  * See the file "COPYING" for information on usage and redistribution
9  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10  *
11  * $Id$
12  */
13
14
15
16 #include <errno.h>
17 #include <string.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include <tcl.h>
21 #include <rrd_tool.h>
22 #include <rrd_format.h>
23
24 /* support pre-8.4 tcl */
25
26 #ifndef CONST84
27 #   define CONST84
28 #endif
29
30 extern int Tclrrd_Init(Tcl_Interp *interp);
31 extern int Tclrrd_SafeInit(Tcl_Interp *interp);
32
33
34 /*
35  * some rrd_XXX() and new thread-safe versions of Rrd_XXX()
36  * functions might modify the argv strings passed to it.
37  * Hence, we need to do some preparation before
38  * calling the rrd library functions.
39  */
40 static char ** getopt_init(int argc, CONST84 char *argv[])
41 {
42     char **argv2;
43     int i;
44     
45     argv2 = calloc(argc, sizeof(char *));
46     for (i = 0; i < argc; i++) {
47         argv2[i] = strdup(argv[i]);
48     }
49     return argv2;
50 }
51
52 static void getopt_cleanup(int argc, char **argv2)
53 {
54     int i;
55     
56     for (i = 0; i < argc; i++) {
57         if (argv2[i] != NULL) {
58             free(argv2[i]);
59         }
60     }
61     free(argv2);
62 }
63
64 static void getopt_free_element(argv2, argn)
65     char *argv2[];
66     int  argn;
67 {
68     if (argv2[argn] != NULL) {
69         free(argv2[argn]);
70         argv2[argn] = NULL;
71     }
72 }
73
74 static void getopt_squieeze(argc, argv2)
75     int  *argc;
76     char *argv2[];
77 {
78     int i, null_i = 0, argc_tmp = *argc;
79
80     for (i = 0; i < argc_tmp; i++) {
81         if (argv2[i] == NULL) {
82             (*argc)--;
83         } else {
84             argv2[null_i++] = argv2[i];
85         }
86     }
87 }
88
89
90
91 /* Thread-safe version */
92 static int
93 Rrd_Create(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
94 {
95     int                         argv_i;
96     char                        **argv2;
97     char                        *parsetime_error = NULL;
98     time_t                      last_up = time(NULL) - 10;
99     long int                    long_tmp;
100     unsigned long int           pdp_step = 300;
101     struct rrd_time_value       last_up_tv;
102
103     argv2 = getopt_init(argc, argv);
104
105     for (argv_i = 1; argv_i < argc; argv_i++) {
106         if (!strcmp(argv2[argv_i], "--start") || !strcmp(argv2[argv_i], "-b")) {
107             if (argv_i++>=argc) {
108                 Tcl_AppendResult(interp, "RRD Error: option '",
109                                  argv2[argv_i - 1], "' needs an argument", (char *) NULL);
110                 getopt_cleanup(argc, argv2);
111                 return TCL_ERROR;
112             }
113             if ((parsetime_error = parsetime(argv2[argv_i], &last_up_tv))) {
114                 Tcl_AppendResult(interp, "RRD Error: invalid time format: '",
115                                  argv2[argv_i], "'", (char *) NULL);
116                 getopt_cleanup(argc, argv2);
117                 return TCL_ERROR;
118             }
119             if (last_up_tv.type == RELATIVE_TO_END_TIME ||
120                 last_up_tv.type == RELATIVE_TO_START_TIME) {
121                 Tcl_AppendResult(interp, "RRD Error: specifying time relative to the 'start' ",
122                                  "or 'end' makes no sense here", (char *) NULL);
123                 getopt_cleanup(argc, argv2);
124                 return TCL_ERROR;
125             }
126             last_up = mktime(&last_up_tv.tm) + last_up_tv.offset;
127             if (last_up < 3600*24*365*10) {
128                 Tcl_AppendResult(interp, "RRD Error: the first entry to the RRD should be after 1980",
129                                  (char *) NULL);
130                 getopt_cleanup(argc, argv2);
131                 return TCL_ERROR;
132             }
133             getopt_free_element(argv2, argv_i - 1);
134             getopt_free_element(argv2, argv_i);
135         } else if (!strcmp(argv2[argv_i], "--step") || !strcmp(argv2[argv_i], "-s")) {
136             if (argv_i++>=argc) {
137                 Tcl_AppendResult(interp, "RRD Error: option '",
138                                  argv2[argv_i - 1], "' needs an argument", (char *) NULL);
139                 getopt_cleanup(argc, argv2);
140                 return TCL_ERROR;
141             }
142             long_tmp = atol(argv2[argv_i]);
143             if (long_tmp < 1) {
144                 Tcl_AppendResult(interp, "RRD Error: step size should be no less than one second",
145                                  (char *) NULL);
146                 getopt_cleanup(argc, argv2);
147                 return TCL_ERROR;
148             }
149             pdp_step = long_tmp;
150             getopt_free_element(argv2, argv_i - 1);
151             getopt_free_element(argv2, argv_i);
152         } else if (!strcmp(argv2[argv_i], "--")) {
153             getopt_free_element(argv2, argv_i);
154             break;
155         } else if (argv2[argv_i][0]=='-') {
156             Tcl_AppendResult(interp, "RRD Error: unknown option '",
157                              argv2[argv_i], "'", (char *) NULL);
158             getopt_cleanup(argc, argv2);
159             return TCL_ERROR;
160         }
161     }
162
163     getopt_squieeze(&argc, argv2);
164
165     if (argc < 2) {
166         Tcl_AppendResult(interp, "RRD Error: needs rrd filename",
167                          (char *) NULL);
168         getopt_cleanup(argc, argv2);
169         return TCL_ERROR;
170     }
171
172     rrd_create_r(argv2[1], pdp_step, last_up, argc - 2, argv2 + 2);
173
174     getopt_cleanup(argc, argv2);
175     
176     if (rrd_test_error()) {
177         Tcl_AppendResult(interp, "RRD Error: ",
178                          rrd_get_error(), (char *) NULL);
179         rrd_clear_error();
180         return TCL_ERROR;
181     }
182
183     return TCL_OK;
184 }
185
186
187
188 /* Thread-safe version */
189 static int
190 Rrd_Dump(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
191 {
192     if (argc < 2) {
193         Tcl_AppendResult(interp, "RRD Error: needs rrd filename",
194                          (char *) NULL);
195         return TCL_ERROR;
196     }
197
198     rrd_dump_r(argv[1]);
199
200     /* NOTE: rrd_dump() writes to stdout. No interaction with TCL. */
201
202     if (rrd_test_error()) {
203         Tcl_AppendResult(interp, "RRD Error: ",
204                          rrd_get_error(), (char *) NULL);
205         rrd_clear_error();
206         return TCL_ERROR;
207     }
208
209     return TCL_OK;
210 }
211
212
213
214 /* Thread-safe version */
215 static int
216 Rrd_Last(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
217 {
218     time_t t;
219     
220     if (argc < 2) {
221         Tcl_AppendResult(interp, "RRD Error: needs rrd filename",
222                          (char *) NULL);
223         return TCL_ERROR;
224     }
225
226     t = rrd_last_r(argv[1]);
227
228     if (rrd_test_error()) {
229         Tcl_AppendResult(interp, "RRD Error: ",
230                          rrd_get_error(), (char *) NULL);
231         rrd_clear_error();
232         return TCL_ERROR;
233     }
234
235     Tcl_SetIntObj(Tcl_GetObjResult(interp), t);
236
237     return TCL_OK;
238 }
239
240
241
242 /* Thread-safe version */
243 static int
244 Rrd_Update(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
245 {
246     int         argv_i;
247     char        **argv2, *template = NULL;
248     
249     argv2 = getopt_init(argc, argv);
250
251     for (argv_i = 1; argv_i < argc; argv_i++) {
252         if (!strcmp(argv2[argv_i], "--template") || !strcmp(argv2[argv_i], "-t")) {
253             if (argv_i++>=argc) {
254                 Tcl_AppendResult(interp, "RRD Error: option '",
255                                  argv2[argv_i - 1], "' needs an argument", (char *) NULL);
256                 if (template != NULL) {
257                     free(template);
258                 }
259                 getopt_cleanup(argc, argv2);
260                 return TCL_ERROR;
261             }
262             if (template != NULL) {
263                 free(template);
264             }
265             template = strdup(argv2[argv_i]);
266             getopt_free_element(argv2, argv_i - 1);
267             getopt_free_element(argv2, argv_i);
268         } else if (!strcmp(argv2[argv_i], "--")) {
269             getopt_free_element(argv2, argv_i);
270             break;
271         } else if (argv2[argv_i][0]=='-') {
272             Tcl_AppendResult(interp, "RRD Error: unknown option '",
273                              argv2[argv_i], "'", (char *) NULL);
274             if (template != NULL) {
275                 free(template);
276             }
277             getopt_cleanup(argc, argv2);
278             return TCL_ERROR;
279         }
280     }
281
282     getopt_squieeze(&argc, argv2);
283
284     if (argc < 2) {
285         Tcl_AppendResult(interp, "RRD Error: needs rrd filename",
286                          (char *) NULL);
287         if (template != NULL) {
288             free(template);
289         }
290         getopt_cleanup(argc, argv2);
291         return TCL_ERROR;
292     }
293
294     rrd_update_r(argv2[1], template, argc - 2, argv2 + 2);
295
296     if (template != NULL) {
297         free(template);
298     }
299     getopt_cleanup(argc, argv2);
300
301     if (rrd_test_error()) {
302         Tcl_AppendResult(interp, "RRD Error: ",
303                          rrd_get_error(), (char *) NULL);
304         rrd_clear_error();
305         return TCL_ERROR;
306     }
307
308     return TCL_OK;
309 }
310
311
312
313 static int
314 Rrd_Fetch(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
315 {
316     time_t start, end, j;
317     unsigned long step, ds_cnt, i, ii;
318     rrd_value_t *data, *datai;
319     char **ds_namv;
320     Tcl_Obj *listPtr;
321     char s[30];
322     char **argv2;
323     
324     argv2 = getopt_init(argc, argv);
325     if (rrd_fetch(argc, argv2, &start, &end, &step,
326                   &ds_cnt, &ds_namv, &data) != -1) {
327         datai = data;
328         listPtr = Tcl_GetObjResult(interp);
329         for (j = start; j <= end; j += step) {
330             for (ii = 0; ii < ds_cnt; ii++) {
331                 sprintf(s, "%.2f", *(datai++));
332                 Tcl_ListObjAppendElement(interp, listPtr,
333                                          Tcl_NewStringObj(s, -1));
334             }
335         }
336         for (i=0; i<ds_cnt; i++) free(ds_namv[i]);
337         free(ds_namv);
338         free(data);
339     }
340     getopt_cleanup(argc, argv2);
341
342     if (rrd_test_error()) {
343         Tcl_AppendResult(interp, "RRD Error: ",
344                          rrd_get_error(), (char *) NULL);
345         rrd_clear_error();
346         return TCL_ERROR;
347     }
348
349     return TCL_OK;
350 }
351
352
353
354 static int
355 Rrd_Graph(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
356 {
357     Tcl_Channel channel;
358     int mode, fd2;
359     ClientData fd1;
360     FILE *stream = NULL;
361     char **calcpr = NULL;
362     int rc, xsize, ysize;
363     double ymin, ymax;
364     char dimensions[50];
365     char **argv2;
366     CONST84 char *save;
367     
368     /*
369      * If the "filename" is a Tcl fileID, then arrange for rrd_graph() to write to
370      * that file descriptor.  Will this work with windoze?  I have no idea.
371      */
372     if ((channel = Tcl_GetChannel(interp, argv[1], &mode)) != NULL) {
373         /*
374          * It >is< a Tcl fileID
375          */
376         if (!(mode & TCL_WRITABLE)) {
377             Tcl_AppendResult(interp, "channel \"", argv[1],
378                 "\" wasn't opened for writing", (char *) NULL);
379             return TCL_ERROR;
380         }
381         /*
382          * Must flush channel to make sure any buffered data is written before
383          * rrd_graph() writes to the stream
384          */
385         if (Tcl_Flush(channel) != TCL_OK) {
386             Tcl_AppendResult(interp, "flush failed for \"", argv[1], "\": ",
387                 strerror(Tcl_GetErrno()), (char *) NULL);
388             return TCL_ERROR;
389         }
390         if (Tcl_GetChannelHandle(channel, TCL_WRITABLE, &fd1) != TCL_OK) {
391             Tcl_AppendResult(interp, "cannot get file descriptor associated with \"",
392                 argv[1], "\"", (char *) NULL);
393             return TCL_ERROR;
394         }
395         /*
396          * Must dup() file descriptor so we can fclose(stream), otherwise the fclose()
397          * would close Tcl's file descriptor
398          */
399         if ((fd2 = dup((int)fd1)) == -1) {
400             Tcl_AppendResult(interp, "dup() failed for file descriptor associated with \"",
401                 argv[1], "\": ", strerror(errno), (char *) NULL);
402             return TCL_ERROR;
403         }
404         /*
405          * rrd_graph() wants a FILE*
406          */
407         if ((stream = fdopen(fd2, "wb")) == NULL) {
408             Tcl_AppendResult(interp, "fdopen() failed for file descriptor associated with \"",
409                 argv[1], "\": ", strerror(errno), (char *) NULL);
410             close(fd2);         /* plug potential file descriptor leak */
411             return TCL_ERROR;
412         }
413
414         save = argv[1];
415         argv[1] = "-";
416         argv2 = getopt_init(argc, argv);
417         argv[1] = save;
418     } else {
419         Tcl_ResetResult(interp);        /* clear error from Tcl_GetChannel() */
420         argv2 = getopt_init(argc, argv);
421     }
422
423     rc = rrd_graph(argc, argv2, &calcpr, &xsize, &ysize, stream, &ymin, &ymax);
424     getopt_cleanup(argc, argv2);
425
426     if (stream != NULL)
427         fclose(stream);         /* plug potential malloc & file descriptor leak */
428
429     if (rc != -1) {
430         sprintf(dimensions, "%d %d", xsize, ysize);
431         Tcl_AppendResult(interp, dimensions, (char *) NULL);
432         if (calcpr) {
433 #if 0
434             int i;
435             
436             for(i = 0; calcpr[i]; i++){
437                 printf("%s\n", calcpr[i]);
438                 free(calcpr[i]);
439             } 
440 #endif
441             free(calcpr);
442         }
443     }
444
445     if (rrd_test_error()) {
446         Tcl_AppendResult(interp, "RRD Error: ",
447                          rrd_get_error(), (char *) NULL);
448         rrd_clear_error();
449         return TCL_ERROR;
450     }
451
452     return TCL_OK;
453 }
454
455
456
457 static int
458 Rrd_Tune(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
459 {
460     char **argv2;
461     
462     argv2 = getopt_init(argc, argv);
463     rrd_tune(argc, argv2);
464     getopt_cleanup(argc, argv2);
465
466     if (rrd_test_error()) {
467         Tcl_AppendResult(interp, "RRD Error: ",
468                          rrd_get_error(), (char *) NULL);
469         rrd_clear_error();
470         return TCL_ERROR;
471     }
472
473     return TCL_OK;
474 }
475
476
477
478 static int
479 Rrd_Resize(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
480 {
481     char **argv2;
482     
483     argv2 = getopt_init(argc, argv);
484     rrd_resize(argc, argv2);
485     getopt_cleanup(argc, argv2);
486
487     if (rrd_test_error()) {
488         Tcl_AppendResult(interp, "RRD Error: ",
489                          rrd_get_error(), (char *) NULL);
490         rrd_clear_error();
491         return TCL_ERROR;
492     }
493
494     return TCL_OK;
495 }
496
497
498
499 static int
500 Rrd_Restore(ClientData clientData, Tcl_Interp *interp, int argc, CONST84 char *argv[])
501 {
502     char **argv2;
503     
504     argv2 = getopt_init(argc, argv);
505     rrd_restore(argc, argv2);
506     getopt_cleanup(argc, argv2);
507
508     if (rrd_test_error()) {
509         Tcl_AppendResult(interp, "RRD Error: ",
510                          rrd_get_error(), (char *) NULL);
511         rrd_clear_error();
512         return TCL_ERROR;
513     }
514
515     return TCL_OK;
516 }
517
518
519
520 /*
521  * The following structure defines the commands in the Rrd extension.
522  */
523
524 typedef struct {
525     char *name;                 /* Name of the command. */
526     Tcl_CmdProc *proc;          /* Procedure for command. */
527     int hide;                   /* Hide if safe interpreter */
528 } CmdInfo;
529
530 static CmdInfo rrdCmds[] = {
531     { "Rrd::create",    Rrd_Create,     1 }, /* Thread-safe version */
532     { "Rrd::dump",      Rrd_Dump,       0 }, /* Thread-safe version */
533     { "Rrd::last",      Rrd_Last,       0 }, /* Thread-safe version */
534     { "Rrd::update",    Rrd_Update,     1 }, /* Thread-safe version */
535     { "Rrd::fetch",     Rrd_Fetch,      0 },
536     { "Rrd::graph",     Rrd_Graph,      1 }, /* Due to RRD's API, a safe
537                                                 interpreter cannot create
538                                                 a graph since it writes to
539                                                 a filename supplied by the
540                                                 caller */
541     { "Rrd::tune",      Rrd_Tune,       1 },
542     { "Rrd::resize",    Rrd_Resize,     1 },
543     { "Rrd::restore",   Rrd_Restore,    1 },
544     { (char *) NULL,    (Tcl_CmdProc *) NULL, 0 }
545 };
546
547
548
549 static int
550 init(Tcl_Interp *interp, int safe)
551
552     CmdInfo *cmdInfoPtr;
553     Tcl_CmdInfo info;
554
555     if ( Tcl_InitStubs(interp,TCL_VERSION,0) == NULL )
556         return TCL_ERROR;
557
558     if (Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, 1) == NULL) {
559         return TCL_ERROR;
560     }
561
562     /*
563      * Why a global array?  In keeping with the Rrd:: namespace, why
564      * not simply create a normal variable Rrd::version and set it?
565      */
566     Tcl_SetVar2(interp, "rrd", "version", VERSION, TCL_GLOBAL_ONLY);
567
568     for (cmdInfoPtr = rrdCmds; cmdInfoPtr->name != NULL; cmdInfoPtr++) {
569         /*
570          * Check if the command already exists and return an error
571          * to ensure we detect name clashes while loading the Rrd
572          * extension.
573          */
574         if (Tcl_GetCommandInfo(interp, cmdInfoPtr->name, &info)) {
575             Tcl_AppendResult(interp, "command \"", cmdInfoPtr->name,
576                              "\" already exists", (char *) NULL);
577             return TCL_ERROR;
578         }
579         if (safe && cmdInfoPtr->hide) {
580 #if 0
581             /*
582              * Turns out the one cannot hide a command in a namespace
583              * due to a limitation of Tcl, one can only hide global
584              * commands.  Thus, if we created the commands without
585              * the Rrd:: namespace in a safe interpreter, then the
586              * "unsafe" commands could be hidden -- which would allow
587              * an owning interpreter either un-hiding them or doing
588              * an "interp invokehidden".  If the Rrd:: namespace is
589              * used, then it's still possible for the owning interpreter
590              * to fake out the missing commands:
591              *
592              *   # Make all Rrd::* commands available in master interperter
593              *   package require Rrd
594              *   set safe [interp create -safe]
595              *   # Make safe Rrd::* commands available in safe interperter
596              *   interp invokehidden $safe -global load ./tclrrd1.2.11.so
597              *   # Provide the safe interpreter with the missing commands
598              *   $safe alias Rrd::update do_update $safe
599              *   proc do_update {which_interp $args} {
600              *     # Do some checking maybe...
601              *       :
602              *     return [eval Rrd::update $args]
603              *   }
604              *
605              * Our solution for now is to just not create the "unsafe"
606              * commands in a safe interpreter.
607              */
608             if (Tcl_HideCommand(interp, cmdInfoPtr->name, cmdInfoPtr->name) != TCL_OK)
609                 return TCL_ERROR;
610 #endif
611         }
612         else
613             Tcl_CreateCommand(interp, cmdInfoPtr->name, cmdInfoPtr->proc,
614                           (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
615     }
616
617     if (Tcl_PkgProvide(interp, "Rrd", VERSION) != TCL_OK) {
618         return TCL_ERROR;
619     }
620
621     return TCL_OK;
622 }
623
624 int
625 Tclrrd_Init(Tcl_Interp *interp)
626
627   return init(interp, 0);
628 }
629
630 /*
631  * See the comments above and note how few commands are considered "safe"...
632  * Using rrdtool in a safe interpreter has very limited functionality.  It's
633  * tempting to just return TCL_ERROR and forget about it.
634  */
635 int
636 Tclrrd_SafeInit(Tcl_Interp *interp)
637
638   return init(interp, 1);
639 }