00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "common.h"
00039
00040
00041 #ifdef HAVE_STRING_H
00042 #include <string.h>
00043 #endif
00044
00045 #include "tcl.h"
00046 #include "machine.h"
00047 #include "cmd.h"
00048
00049 static int bu_observer_attach_tcl(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
00050 static int bu_observer_detach_tcl(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
00051 static int bu_observer_show_tcl(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
00052
00053 struct bu_cmdtab bu_observer_cmds[] = {
00054 {"attach", bu_observer_attach_tcl},
00055 {"detach", bu_observer_detach_tcl},
00056 {"show", bu_observer_show_tcl},
00057 {(char *)0, (int (*)())0}
00058 };
00059
00060
00061
00062
00063
00064
00065
00066
00067 static int
00068 bu_observer_attach_tcl(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
00069 {
00070 struct bu_observer *headp = (struct bu_observer *)clientData;
00071 struct bu_observer *op;
00072
00073 if (argc < 2 || 3 < argc) {
00074 struct bu_vls vls;
00075
00076 bu_vls_init(&vls);
00077 bu_vls_printf(&vls, "helplib bu_observer_attach");
00078 Tcl_Eval(interp, bu_vls_addr(&vls));
00079 bu_vls_free(&vls);
00080 return TCL_ERROR;
00081 }
00082
00083
00084 for (BU_LIST_FOR(op, bu_observer, &headp->l))
00085 if (strcmp(bu_vls_addr(&op->observer), argv[1]) == 0) {
00086 if (argc == 2)
00087
00088 bu_vls_init(&op->cmd);
00089 else
00090
00091 bu_vls_strcpy(&op->cmd, argv[2]);
00092
00093 return TCL_OK;
00094 }
00095
00096
00097 BU_GETSTRUCT(op, bu_observer);
00098
00099
00100 bu_vls_init(&op->observer);
00101 bu_vls_strcpy(&op->observer, argv[1]);
00102 bu_vls_init(&op->cmd);
00103
00104 if (argc == 3)
00105 bu_vls_strcpy(&op->cmd, argv[2]);
00106
00107
00108 BU_LIST_APPEND(&headp->l, &op->l);
00109
00110 return TCL_OK;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120 static int
00121 bu_observer_detach_tcl(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
00122 {
00123 struct bu_observer *headp = (struct bu_observer *)clientData;
00124 struct bu_observer *op;
00125
00126 if (argc != 2) {
00127 struct bu_vls vls;
00128
00129 bu_vls_init(&vls);
00130 bu_vls_printf(&vls, "helplib bu_observer_attach");
00131 Tcl_Eval(interp, bu_vls_addr(&vls));
00132 bu_vls_free(&vls);
00133 return TCL_ERROR;
00134 }
00135
00136
00137 for (BU_LIST_FOR(op, bu_observer, &headp->l))
00138 if (strcmp(bu_vls_addr(&op->observer), argv[1]) == 0) {
00139 BU_LIST_DEQUEUE(&op->l);
00140 bu_vls_free(&op->observer);
00141 bu_vls_free(&op->cmd);
00142 bu_free((genptr_t)op, "bu_observer_detach_tcl: op");
00143
00144 return TCL_OK;
00145 }
00146
00147 Tcl_AppendResult(interp, "detach: ", argv[1], " not found", (char *)NULL);
00148 return TCL_ERROR;
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158 static int
00159 bu_observer_show_tcl(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
00160 {
00161 struct bu_observer *headp = (struct bu_observer *)clientData;
00162 struct bu_observer *op;
00163
00164 for (BU_LIST_FOR(op, bu_observer, &headp->l)) {
00165 Tcl_AppendResult(interp, bu_vls_addr(&op->observer), " - ",
00166 bu_vls_addr(&op->cmd), "\n", (char *)NULL);
00167 }
00168
00169 return TCL_OK;
00170 }
00171
00172
00173
00174
00175 void
00176 bu_observer_notify(Tcl_Interp *interp, struct bu_observer *headp, char *this)
00177 {
00178 struct bu_observer *op;
00179 struct bu_vls vls;
00180
00181 bu_vls_init(&vls);
00182 for (BU_LIST_FOR(op, bu_observer, &headp->l)) {
00183 if (bu_vls_strlen(&op->cmd) > 0) {
00184
00185 bu_vls_strcpy(&vls, bu_vls_addr(&op->cmd));
00186 Tcl_Eval(interp, bu_vls_addr(&vls));
00187 } else {
00188
00189 bu_vls_trunc(&vls, 0);
00190 bu_vls_printf(&vls, "%s update %s", bu_vls_addr(&op->observer), this);
00191 Tcl_Eval(interp, bu_vls_addr(&vls));
00192 }
00193 }
00194 bu_vls_free(&vls);
00195 }
00196
00197
00198
00199
00200 void
00201 bu_observer_free(struct bu_observer *headp)
00202 {
00203 struct bu_observer *op;
00204 struct bu_observer *nop;
00205
00206 op = BU_LIST_FIRST(bu_observer, &headp->l);
00207 while (BU_LIST_NOT_HEAD(op, &headp->l)) {
00208 nop = BU_LIST_PNEXT(bu_observer, op);
00209 BU_LIST_DEQUEUE(&op->l);
00210 bu_vls_free(&op->observer);
00211 bu_vls_free(&op->cmd);
00212 bu_free((genptr_t)op, "bu_observer_free: op");
00213 op = nop;
00214 }
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226