BRL-CAD
tcl.c
Go to the documentation of this file.
1 /* T C L . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1997-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @addtogroup libfb */
21 /** @{ */
22 /** @file ./libfb/tcl.c
23  *
24  * LIBFB's Tcl interface.
25  *
26  */
27 /** @} */
28 
29 #include "common.h"
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 
35 #ifdef HAVE_STRINGS_H
36 # include <strings.h>
37 #endif
38 
39 #include "tcl.h"
40 #include "bu/cmd.h"
41 #include "bu/log.h"
42 #include "bu/vls.h"
43 #include "fb_private.h"
44 #include "fb.h"
45 
46 /* private headers */
47 #include "brlcad_version.h"
48 
49 /* from libfb/fb_obj.c */
50 extern int Fbo_Init(Tcl_Interp *interp);
51 
52 /**
53  * Hook function wrapper to the fb_common_file_size Tcl command
54  */
55 int
56 fb_cmd_common_file_size(ClientData clientData, int argc, const char **argv)
57 {
58  Tcl_Interp *interp = (Tcl_Interp *)clientData;
59  size_t width, height;
60  int pixel_size = 3;
61 
62  if (argc != 2 && argc != 3) {
63  bu_log("wrong #args: should be \" fileName [#bytes/pixel]\"");
64  return TCL_ERROR;
65  }
66 
67  if (argc >= 3) {
68  pixel_size = atoi(argv[2]);
69  }
70 
71  if (fb_common_file_size(&width, &height, argv[1], pixel_size) > 0) {
72  struct bu_vls vls = BU_VLS_INIT_ZERO;
73  bu_vls_printf(&vls, "%lu %lu", (unsigned long)width, (unsigned long)height);
74  Tcl_SetObjResult(interp,
75  Tcl_NewStringObj(bu_vls_addr(&vls), bu_vls_strlen(&vls)));
76  bu_vls_free(&vls);
77  return TCL_OK;
78  }
79 
80  /* Signal error */
81  Tcl_SetResult(interp, "0 0", TCL_STATIC);
82  return TCL_OK;
83 }
84 
85 
86 static int
87 wrapper_func(ClientData data, Tcl_Interp *interp, int argc, const char *argv[])
88 {
89  struct bu_cmdtab *ctp = (struct bu_cmdtab *)data;
90 
91  return ctp->ct_func(interp, argc, argv);
92 }
93 
94 
95 static void
96 register_cmds(Tcl_Interp *interp, struct bu_cmdtab *cmds)
97 {
98  struct bu_cmdtab *ctp = NULL;
99 
100  for (ctp = cmds; ctp->ct_name != (char *)NULL; ctp++) {
101  (void)Tcl_CreateCommand(interp, ctp->ct_name, wrapper_func, (ClientData)ctp, (Tcl_CmdDeleteProc *)NULL);
102  }
103 }
104 
105 
106 /*
107  * Allows LIBFB to be dynamically loaded to a vanilla tclsh/wish with
108  * "load /usr/brlcad/lib/libfb.so"
109  *
110  * The name of this function is specified by TCL.
111  */
112 int
113 Fb_Init(Tcl_Interp *interp)
114 {
115  static struct bu_cmdtab cmdtab[] = {
116  {"fb_common_file_size", fb_cmd_common_file_size},
117  {(const char *)NULL, BU_CMD_NULL}
118  };
119 
120  /* register commands */
121  register_cmds(interp, cmdtab);
122 
123  /* initialize framebuffer object code */
124  Fbo_Init(interp);
125 
126  Tcl_PkgProvide(interp, "Fb", brlcad_version());
127 
128  return TCL_OK;
129 }
130 
131 
132 /*
133  * Local Variables:
134  * mode: C
135  * tab-width: 8
136  * indent-tabs-mode: t
137  * c-file-style: "stroustrup"
138  * End:
139  * ex: shiftwidth=4 tabstop=8
140  */
Definition: cmd.h:48
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
ustring interp
Header file for the BRL-CAD common definitions.
#define BU_CMD_NULL
Definition: cmd.h:37
ustring width
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
COMPLEX data[64]
Definition: fftest.c:34
int fb_cmd_common_file_size(ClientData clientData, int argc, const char **argv)
Definition: tcl.c:56
size_t bu_vls_strlen(const struct bu_vls *vp)
Definition: vls.c:189
const char * ct_name
Definition: cmd.h:49
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
int Fbo_Init(Tcl_Interp *interp)
Definition: fb_obj.c:864
int fb_common_file_size(size_t *widthp, size_t *heightp, const char *filename, int pixel_size)
Definition: asize.c:86
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
int(* ct_func)(void *data, int argc, const char *argv[])
Definition: cmd.h:50
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
Definition: vls.h:56
int Fb_Init(Tcl_Interp *interp)
Definition: tcl.c:113