BRL-CAD
cmdhist_obj.c
Go to the documentation of this file.
1 /* C M D H I S T _ O B J . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1998-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 
21 #include "common.h"
22 
23 #include <string.h>
24 
25 #include "tcl.h"
26 #include "bu/cmd.h"
27 #include "tclcad.h"
28 
29 
30 /* FIXME: this is apparently used by src/tclscripts/lib/Command.tcl so
31  * it needs to migrate elsewhere if mged/archer is to continue using
32  * it as it doesn't belong in libbu. if it can be fully decoupled
33  * from tcl (ideal), it would belong in libged. otherwise, it belongs
34  * in libtclcad.
35  */
36 
37 
38 static struct bu_cmdhist_obj HeadCmdHistObj; /* head of command history object list */
39 
40 
41 HIDDEN int
42 cho_cmd(ClientData clientData, Tcl_Interp *interp, int argc, const char **argv)
43 {
44  int ret;
45 
46  static struct bu_cmdtab cho_cmds[] = {
47  {"add", bu_cmdhist_add},
48  {"curr", bu_cmdhist_curr},
49  {"history", bu_cmdhist_history},
50  {"next", bu_cmdhist_next},
51  {"prev", bu_cmdhist_prev},
52  {(const char *)NULL, BU_CMD_NULL}
53  };
54 
55  if (bu_cmd(cho_cmds, argc, argv, 1, clientData, &ret) == BRLCAD_OK) {
56  if (ret == BRLCAD_OK)
57  Tcl_AppendResult(interp, bu_vls_addr(&((struct bu_cmdhist_obj *)clientData)->cho_curr->h_command), NULL);
58  return ret;
59  }
60 
61  bu_log("ERROR: '%s' command not found\n", argv[1]);
62  return BRLCAD_ERROR;
63 }
64 
65 
66 HIDDEN void
67 cho_deleteProc(ClientData clientData)
68 {
69  struct bu_cmdhist_obj *chop = (struct bu_cmdhist_obj *)clientData;
70  struct bu_cmdhist *curr, *next;
71 
72  /* free list of commands */
73  curr = BU_LIST_NEXT(bu_cmdhist, &chop->cho_head.l);
74  while (BU_LIST_NOT_HEAD(curr, &chop->cho_head.l)) {
75  curr = BU_LIST_NEXT(bu_cmdhist, &chop->cho_head.l);
76  next = BU_LIST_PNEXT(bu_cmdhist, curr);
77 
78  bu_vls_free(&curr->h_command);
79 
80  BU_LIST_DEQUEUE(&curr->l);
81  bu_free((void *)curr, "cho_deleteProc: curr");
82  curr = next;
83  }
84 
85  bu_vls_free(&chop->cho_name);
87 
88  BU_LIST_DEQUEUE(&chop->l);
89  BU_PUT(chop, struct bu_cmdhist_obj);
90 }
91 
92 
93 HIDDEN struct bu_cmdhist_obj *
94 cho_open(ClientData UNUSED(clientData), Tcl_Interp *interp, const char *name)
95 {
96  struct bu_cmdhist_obj *chop;
97 
98  /* check to see if command history object exists */
99  for (BU_LIST_FOR(chop, bu_cmdhist_obj, &HeadCmdHistObj.l)) {
100  if (BU_STR_EQUAL(name, bu_vls_addr(&chop->cho_name))) {
101  Tcl_AppendResult(interp, "ch_open: ", name,
102  " exists.\n", (char *)NULL);
103  return BU_CMDHIST_OBJ_NULL;
104  }
105  }
106 
107  BU_GET(chop, struct bu_cmdhist_obj);
108  bu_vls_init(&chop->cho_name);
109  bu_vls_strcpy(&chop->cho_name, name);
110  BU_LIST_INIT(&chop->cho_head.l);
112  chop->cho_head.h_start.tv_sec = chop->cho_head.h_start.tv_usec =
113  chop->cho_head.h_finish.tv_sec = chop->cho_head.h_finish.tv_usec = 0L;
114  chop->cho_head.h_status = TCL_OK;
115  chop->cho_curr = &chop->cho_head;
116 
117  BU_LIST_APPEND(&HeadCmdHistObj.l, &chop->l);
118  return chop;
119 }
120 
121 
122 int
123 cho_open_tcl(ClientData clientData, Tcl_Interp *interp, int argc, const char **argv)
124 {
125  struct bu_cmdhist_obj *chop;
126  struct bu_vls vls = BU_VLS_INIT_ZERO;
127 
128  if (argc == 1) {
129  /* get list of command history objects */
130  for (BU_LIST_FOR(chop, bu_cmdhist_obj, &HeadCmdHistObj.l))
131  Tcl_AppendResult(interp, bu_vls_addr(&chop->cho_name), " ", (char *)NULL);
132 
133  return TCL_OK;
134  }
135 
136  if (argc == 2) {
137  if ((chop = cho_open(clientData, interp, argv[1])) == BU_CMDHIST_OBJ_NULL)
138  return TCL_ERROR;
139 
140  (void)Tcl_CreateCommand(interp,
141  bu_vls_addr(&chop->cho_name),
142  (Tcl_CmdProc *)cho_cmd,
143  (ClientData)chop,
145 
146  /* Return new function name as result */
147  Tcl_ResetResult(interp);
148  Tcl_AppendResult(interp, bu_vls_addr(&chop->cho_name), (char *)NULL);
149  return TCL_OK;
150  }
151 
152  bu_vls_printf(&vls, "helplib ch_open");
153  Tcl_Eval(interp, bu_vls_addr(&vls));
154  bu_vls_free(&vls);
155  return TCL_ERROR;
156 }
157 
158 
159 int
160 Cho_Init(Tcl_Interp *interp)
161 {
162  memset(&HeadCmdHistObj, 0, sizeof(struct bu_cmdhist_obj));
163  BU_LIST_INIT(&HeadCmdHistObj.l);
164  BU_VLS_INIT(&HeadCmdHistObj.cho_name);
165  /* cho_head already zero'd */
166  HeadCmdHistObj.cho_curr = NULL;
167 
168  (void)Tcl_CreateCommand(interp, "ch_open", (Tcl_CmdProc *)cho_open_tcl,
169  (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);
170 
171  return TCL_OK;
172 }
173 
174 
175 /*
176  * Local Variables:
177  * mode: C
178  * tab-width: 8
179  * indent-tabs-mode: t
180  * c-file-style: "stroustrup"
181  * End:
182  * ex: shiftwidth=4 tabstop=8
183  */
Definition: cmd.h:48
void bu_vls_init(struct bu_vls *vp)
Definition: vls.c:56
#define BU_LIST_FOR(p, structure, hp)
Definition: list.h:365
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
ustring interp
struct bu_list l
Definition: cmd.h:54
struct bu_cmdhist * cho_curr
Definition: cmd.h:66
Definition: clone.c:90
HIDDEN int cho_cmd(ClientData clientData, Tcl_Interp *interp, int argc, const char **argv)
Definition: cmdhist_obj.c:42
struct timeval h_start
Definition: cmd.h:56
Header file for the BRL-CAD common definitions.
struct timeval h_finish
Definition: cmd.h:57
int bu_cmdhist_curr(void *data, int argc, const char **argv)
Definition: cmdhist.c:196
#define BU_CMD_NULL
Definition: cmd.h:37
#define BU_LIST_APPEND(old, new)
Definition: list.h:197
#define HIDDEN
Definition: common.h:86
int bu_cmdhist_prev(void *data, int argc, const char **argv)
Definition: cmdhist.c:176
struct bu_vls cho_name
Definition: cmd.h:64
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
int Cho_Init(Tcl_Interp *interp)
Definition: cmdhist_obj.c:160
void * memset(void *s, int c, size_t n)
struct bu_cmdhist cho_head
Definition: cmd.h:65
int bu_cmdhist_add(void *data, int argc, const char **argv)
Add a command to the history list.
Definition: cmdhist.c:147
#define BRLCAD_OK
Definition: defines.h:71
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
#define BU_CMDHIST_OBJ_NULL
Definition: cmd.h:68
#define BU_LIST_PNEXT(structure, p)
Definition: list.h:422
#define UNUSED(parameter)
Definition: common.h:239
#define BU_PUT(_ptr, _type)
Definition: malloc.h:215
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
int cho_open_tcl(ClientData clientData, Tcl_Interp *interp, int argc, const char **argv)
Definition: cmdhist_obj.c:123
#define BU_LIST_INIT(_hp)
Definition: list.h:148
struct bu_list l
Definition: cmd.h:63
HIDDEN void cho_deleteProc(ClientData clientData)
Definition: cmdhist_obj.c:67
int h_status
Definition: cmd.h:58
struct bu_vls h_command
Definition: cmd.h:55
Definition: cmd.h:53
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
HIDDEN struct bu_cmdhist_obj * cho_open(ClientData clientData, Tcl_Interp *interp, const char *name)
Definition: cmdhist_obj.c:94
Header file for the BRL-CAD TclCAD Library, LIBTCLCAD.
void bu_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
#define BU_LIST_DEQUEUE(cur)
Definition: list.h:209
Definition: vls.h:56
#define BRLCAD_ERROR
Definition: defines.h:72
#define BU_LIST_NEXT(structure, hp)
Definition: list.h:316
#define BU_LIST_NOT_HEAD(p, hp)
Definition: list.h:324
int bu_cmdhist_next(void *data, int argc, const char **argv)
Definition: cmdhist.c:216
int bu_cmdhist_history(void *data, int argc, const char **argv)
Prints out the command history.
int bu_cmd(const struct bu_cmdtab *cmds, int argc, const char *argv[], int cmd_index, void *data, int *result)
#define BU_VLS_INIT(_vp)
Definition: vls.h:74
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126