BRL-CAD
tcl.c
Go to the documentation of this file.
1 /* T C L . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-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 /** @file libdm/tcl.c
21  *
22  * LIBDM's tcl interface.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <math.h>
29 #include "tcl.h"
30 #include "vmath.h"
31 #include "dm.h"
32 #include "bu/cmd.h"
33 
34 /* private headers */
35 #include "brlcad_version.h"
36 
37 
38 /* from libdm/query.c */
39 extern int dm_validXType();
40 extern char *dm_bestXType();
41 
42 /* from libdm/dm_obj.c */
43 extern int Dmo_Init(Tcl_Interp *interp);
44 
45 /* TODO: this doesn't belong in here, move to a globals.c or eliminate */
46 int vectorThreshold = 100000;
47 
48 
49 HIDDEN int
50 dm_validXType_tcl(void *clientData, int argc, const char **argv)
51 {
52  Tcl_Interp *interp = (Tcl_Interp *)clientData;
53  struct bu_vls vls = BU_VLS_INIT_ZERO;
54  Tcl_Obj *obj;
55 
56  if (argc != 3) {
57  bu_vls_printf(&vls, "helplib dm_validXType");
58  Tcl_Eval(interp, bu_vls_addr(&vls));
59  bu_vls_free(&vls);
60  return TCL_ERROR;
61  }
62 
63  bu_vls_printf(&vls, "%d", dm_validXType(argv[1], argv[2]));
64  obj = Tcl_GetObjResult(interp);
65  if (Tcl_IsShared(obj))
66  obj = Tcl_DuplicateObj(obj);
67  Tcl_AppendStringsToObj(obj, bu_vls_addr(&vls), (char *)NULL);
68  bu_vls_free(&vls);
69 
70  Tcl_SetObjResult(interp, obj);
71  return TCL_OK;
72 }
73 
74 
75 HIDDEN int
76 dm_bestXType_tcl(void *clientData, int argc, const char **argv)
77 {
78  Tcl_Interp *interp = (Tcl_Interp *)clientData;
79  Tcl_Obj *obj;
80  const char *best_dm;
81  char buffer[256] = {0};
82 
83  if (argc != 2) {
84  struct bu_vls vls = BU_VLS_INIT_ZERO;
85 
86  bu_vls_printf(&vls, "helplib dm_bestXType");
87  Tcl_Eval(interp, bu_vls_addr(&vls));
88  bu_vls_free(&vls);
89  return TCL_ERROR;
90  }
91 
92  obj = Tcl_GetObjResult(interp);
93  if (Tcl_IsShared(obj))
94  obj = Tcl_DuplicateObj(obj);
95  snprintf(buffer, 256, "%s", argv[1]);
96  best_dm = dm_bestXType(buffer);
97  if (best_dm) {
98  Tcl_AppendStringsToObj(obj, best_dm, (char *)NULL);
99  Tcl_SetObjResult(interp, obj);
100  return TCL_OK;
101  }
102  return TCL_ERROR;
103 }
104 
105 
106 static int
107 wrapper_func(ClientData data, Tcl_Interp *interp, int argc, const char *argv[])
108 {
109  struct bu_cmdtab *ctp = (struct bu_cmdtab *)data;
110 
111  return ctp->ct_func(interp, argc, argv);
112 }
113 
114 
115 static void
116 register_cmds(Tcl_Interp *interp, struct bu_cmdtab *cmds)
117 {
118  struct bu_cmdtab *ctp = NULL;
119 
120  for (ctp = cmds; ctp->ct_name != (char *)NULL; ctp++) {
121  (void)Tcl_CreateCommand(interp, ctp->ct_name, wrapper_func, (ClientData)ctp, (Tcl_CmdDeleteProc *)NULL);
122  }
123 }
124 
125 
126 int
127 Dm_Init(void *interpreter)
128 {
129  Tcl_Interp *interp = (Tcl_Interp *)interpreter;
130 
131  static struct bu_cmdtab cmdtab[] = {
132  {"dm_validXType", dm_validXType_tcl},
133  {"dm_bestXType", dm_bestXType_tcl},
134  {(const char *)NULL, BU_CMD_NULL}
135  };
136 
137  struct bu_vls vls = BU_VLS_INIT_ZERO;
138 
139  /* register commands */
140  register_cmds(interp, cmdtab);
141 
142  bu_vls_strcpy(&vls, "vectorThreshold");
143  Tcl_LinkVar(interp, bu_vls_addr(&vls), (char *)&vectorThreshold,
144  TCL_LINK_INT);
145  bu_vls_free(&vls);
146 
147  /* initialize display manager object code */
148  Dmo_Init(interp);
149 
150  Tcl_PkgProvide(interp, "Dm", brlcad_version());
151 
152  return TCL_OK;
153 }
154 
155 
156 /*
157  * Local Variables:
158  * mode: C
159  * tab-width: 8
160  * indent-tabs-mode: t
161  * c-file-style: "stroustrup"
162  * End:
163  * ex: shiftwidth=4 tabstop=8
164  */
Definition: cmd.h:48
ustring interp
Header file for the BRL-CAD common definitions.
#define BU_CMD_NULL
Definition: cmd.h:37
#define HIDDEN
Definition: common.h:86
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
COMPLEX data[64]
Definition: fftest.c:34
int vectorThreshold
Definition: tcl.c:46
const char * ct_name
Definition: cmd.h:49
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
int Dmo_Init(Tcl_Interp *interp)
Definition: dm_obj.c:3057
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
HIDDEN int dm_bestXType_tcl(void *clientData, int argc, const char **argv)
Definition: tcl.c:76
HIDDEN int dm_validXType_tcl(void *clientData, int argc, const char **argv)
Definition: tcl.c:50
void bu_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
int(* ct_func)(void *data, int argc, const char *argv[])
Definition: cmd.h:50
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
int Dm_Init(void *interpreter)
Definition: tcl.c:127
int dm_validXType()
Definition: vls.h:56
char * dm_bestXType()