cmdhist_obj.c

Go to the documentation of this file.
00001 /*                   C M D H I S T _ O B J . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 1998-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup butcl */
00023 /*@{*/
00024 /** @file cmdhist_obj.c
00025  *
00026  * A cmdhist object contains the attributes and
00027  * methods for maintaining command history.
00028  *
00029  *
00030  *  @author
00031  *        Robert G. Parker
00032  *
00033  *  @par Source -
00034  *      The U. S. Army Research Laboratory                      @n
00035  *      Aberdeen Proving Ground, Maryland  21005-5068  USA
00036  *
00037  */
00038 
00039 
00040 #include "common.h"
00041 
00042 
00043 #include "tcl.h"
00044 #ifdef HAVE_STRING_H
00045 #include <string.h>
00046 #endif
00047 #include "machine.h"
00048 #include "cmd.h"
00049 
00050 /* bu_cmdhist routines are defined in libbu/cmdhist.c */
00051 extern int bu_cmdhist_history(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
00052 extern int bu_cmdhist_add(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
00053 extern int bu_cmdhist_curr(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
00054 extern int bu_cmdhist_next(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
00055 extern int bu_cmdhist_prev(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
00056 
00057 int cho_open_tcl(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
00058 
00059 static struct bu_cmdhist_obj HeadCmdHistObj;            /* head of command history object list */
00060 
00061 static struct bu_cmdtab ch_cmds[] =
00062 {
00063         {"add",         bu_cmdhist_add},
00064         {"curr",        bu_cmdhist_curr},
00065         {"next",        bu_cmdhist_next},
00066         {"prev",        bu_cmdhist_prev},
00067         {(char *)NULL,  CMD_NULL}
00068 };
00069 
00070 /**
00071  *
00072  */
00073 int
00074 cho_hist(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
00075 {
00076         return bu_cmd(clientData, interp, argc, argv, ch_cmds, 1);
00077 }
00078 
00079 
00080 /**
00081  *
00082  */
00083 static struct bu_cmdtab cho_cmds[] =
00084 {
00085         {"add",         bu_cmdhist_add},
00086         {"curr",        bu_cmdhist_curr},
00087         {"history",     bu_cmdhist_history},
00088         {"next",        bu_cmdhist_next},
00089         {"prev",        bu_cmdhist_prev},
00090         {(char *)NULL,  CMD_NULL}
00091 };
00092 
00093 
00094 /**
00095  *
00096  */
00097 static int
00098 cho_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
00099 {
00100         return bu_cmd(clientData, interp, argc, argv, cho_cmds, 1);
00101 }
00102 
00103 
00104 /**
00105  *
00106  */
00107 int
00108 Cho_Init(Tcl_Interp *interp)
00109 {
00110         BU_LIST_INIT(&HeadCmdHistObj.l);
00111         (void)Tcl_CreateCommand(interp, "ch_open", (Tcl_CmdProc *)cho_open_tcl,
00112                                 (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);
00113 
00114         return TCL_OK;
00115 }
00116 
00117 
00118 /**
00119  *
00120  */
00121 static void
00122 cho_deleteProc(ClientData clientData)
00123 {
00124         struct bu_cmdhist_obj *chop = (struct  bu_cmdhist_obj *)clientData;
00125         struct bu_cmdhist *curr, *next;
00126 
00127         /* free list of commands */
00128         curr = BU_LIST_NEXT(bu_cmdhist, &chop->cho_head.l);
00129         while(BU_LIST_NOT_HEAD(curr,&chop->cho_head.l)) {
00130                 curr = BU_LIST_NEXT(bu_cmdhist, &chop->cho_head.l);
00131                 next = BU_LIST_PNEXT(bu_cmdhist, curr);
00132 
00133                 bu_vls_free(&curr->h_command);
00134 
00135                 BU_LIST_DEQUEUE(&curr->l);
00136                 bu_free((genptr_t)curr, "cho_deleteProc: curr");
00137                 curr = next;
00138         }
00139 
00140         bu_vls_free(&chop->cho_name);
00141         bu_vls_free(&chop->cho_head.h_command);
00142 
00143         BU_LIST_DEQUEUE(&chop->l);
00144         bu_free((genptr_t)chop, "cho_deleteProc: chop");
00145 }
00146 
00147 #if 0                   /* As far as I can tell, this is not used.  CTJ */
00148 /*
00149  * Close a command history object.
00150  *
00151  * USAGE:
00152  *        procname close
00153  */
00154 static int
00155 cho_close_tcl(clientData, interp, argc, argv)
00156      ClientData      clientData;
00157      Tcl_Interp      *interp;
00158      int             argc;
00159      char            **argv;
00160 {
00161         struct bu_cmdhist_obj *chop = (struct  bu_cmdhist_obj *)clientData;
00162         struct bu_vls vls;
00163 
00164         if (argc != 2) {
00165                 bu_vls_init(&vls);
00166                 bu_vls_printf(&vls, "helplib cho_close");
00167                 Tcl_Eval(interp, bu_vls_addr(&vls));
00168                 bu_vls_free(&vls);
00169                 return TCL_ERROR;
00170         }
00171 
00172         /* Among other things, this will call cho_deleteProc. */
00173         Tcl_DeleteCommand(interp, bu_vls_addr(&chop->cho_name));
00174 
00175         return TCL_OK;
00176 }
00177 #endif
00178 
00179 
00180 /**
00181  *
00182  */
00183 static struct bu_cmdhist_obj *
00184 cho_open(ClientData clientData, Tcl_Interp *interp, char *name)
00185 {
00186         struct bu_cmdhist_obj *chop;
00187 
00188         /* check to see if command history object exists */
00189         for (BU_LIST_FOR(chop, bu_cmdhist_obj, &HeadCmdHistObj.l)) {
00190                 if (strcmp(name,bu_vls_addr(&chop->cho_name)) == 0) {
00191                         Tcl_AppendResult(interp, "ch_open: ", name,
00192                                          " exists.\n", (char *)NULL);
00193                         return CMDHIST_OBJ_NULL;
00194                 }
00195         }
00196 
00197         BU_GETSTRUCT(chop, bu_cmdhist_obj);
00198         bu_vls_init(&chop->cho_name);
00199         bu_vls_strcpy(&chop->cho_name, name);
00200         BU_LIST_INIT(&chop->cho_head.l);
00201         bu_vls_init(&chop->cho_head.h_command);
00202         chop->cho_head.h_start.tv_sec = chop->cho_head.h_start.tv_usec =
00203                 chop->cho_head.h_finish.tv_sec = chop->cho_head.h_finish.tv_usec = 0L;
00204         chop->cho_head.h_status = TCL_OK;
00205         chop->cho_curr = &chop->cho_head;
00206 
00207         BU_LIST_APPEND(&HeadCmdHistObj.l, &chop->l);
00208         return chop;
00209 }
00210 
00211 /**
00212  * @brief
00213  * Open a command history object.
00214  *
00215  * USAGE:
00216  *        ch_open name
00217  */
00218 int
00219 cho_open_tcl(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
00220 {
00221         struct bu_cmdhist_obj *chop;
00222         struct bu_vls vls;
00223 
00224         if (argc == 1) {
00225                 /* get list of command history objects */
00226                 for (BU_LIST_FOR(chop, bu_cmdhist_obj, &HeadCmdHistObj.l))
00227                         Tcl_AppendResult(interp, bu_vls_addr(&chop->cho_name), " ", (char *)NULL);
00228 
00229                 return TCL_OK;
00230         }
00231 
00232         if (argc == 2) {
00233                 if ((chop = cho_open(clientData, interp, argv[1])) == CMDHIST_OBJ_NULL)
00234                         return TCL_ERROR;
00235 
00236                 (void)Tcl_CreateCommand(interp,
00237                                         bu_vls_addr(&chop->cho_name),
00238                                         (Tcl_CmdProc *)cho_cmd,
00239                                         (ClientData)chop,
00240                                         cho_deleteProc);
00241 
00242                 /* Return new function name as result */
00243                 Tcl_ResetResult(interp);
00244                 Tcl_AppendResult(interp, bu_vls_addr(&chop->cho_name), (char *)NULL);
00245                 return TCL_OK;
00246         }
00247 
00248         bu_vls_init(&vls);
00249         bu_vls_printf(&vls, "helplib ch_open");
00250         Tcl_Eval(interp, bu_vls_addr(&vls));
00251         bu_vls_free(&vls);
00252         return TCL_ERROR;
00253 }
00254 /*@}*/
00255 /*
00256  * Local Variables:
00257  * mode: C
00258  * tab-width: 8
00259  * c-basic-offset: 4
00260  * indent-tabs-mode: t
00261  * End:
00262  * ex: shiftwidth=4 tabstop=8
00263  */

Generated on Mon Sep 18 01:24:47 2006 for BRL-CAD by  doxygen 1.4.6