Skip to content

Commit 3cb8d48

Browse files
committed
Define a search -exec callback function for gsh and register it.
1 parent cc9b551 commit 3cb8d48

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/gtools/gsh/gsh.cpp

+39-1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,42 @@ gsh_post_closedb_clbk(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(
286286
return BRLCAD_OK;
287287
}
288288

289+
extern "C" int
290+
gsh_db_search_callback(int argc, const char *argv[], void *UNUSED(u1), void *u2)
291+
{
292+
GshState *s = (GshState *)u2;
293+
struct ged *gedp = s->gedp;
294+
295+
// Stash any pre-existing non-exec output so it isn't mixed in with the
296+
// results of the exec itself.
297+
struct bu_vls tmp_rstr = BU_VLS_INIT_ZERO;
298+
bu_vls_sprintf(&tmp_rstr, "%s", bu_vls_cstr(gedp->ged_result_str));
299+
300+
// Make sure the result string buffer is clear
301+
bu_vls_trunc(gedp->ged_result_str, 0);
302+
303+
// Do the actual command execution
304+
int gret = ged_exec(gedp, argc, argv);
305+
306+
// Immediately report the output, if any - we're not waiting for the
307+
// overall completion of the entire search command to provide user feedback
308+
// on the exec results. Make sure to add a \n character at the end if the
309+
// command didn't provide it.
310+
if (bu_vls_strlen(gedp->ged_result_str)) {
311+
std::string rstr(bu_vls_cstr(gedp->ged_result_str));
312+
if (rstr.length() && rstr.c_str()[rstr.length() - 1] != '\n')
313+
rstr.append("\n");
314+
bu_log("%s", rstr.c_str());
315+
}
316+
317+
/* Restore any non-exec output */
318+
bu_vls_sprintf(gedp->ged_result_str, "%s", bu_vls_cstr(&tmp_rstr));
319+
bu_vls_free(&tmp_rstr);
320+
321+
return BRLCAD_OK == gret;
322+
}
323+
324+
289325
/* Cross platform I/O subprocess listening is tricky. Tcl and Qt both have
290326
* specific logic necessary for their environments. Even the minimalist
291327
* command line environment has some basic requirements that must be met.
@@ -348,6 +384,8 @@ GshState::GshState()
348384
ged_clbk_set(gedp, "closedb", BU_CLBK_PRE, &gsh_pre_closedb_clbk, (void *)this);
349385
ged_clbk_set(gedp, "closedb", BU_CLBK_POST, &gsh_post_closedb_clbk, (void *)this);
350386

387+
// Register search exec callback
388+
ged_clbk_set(gedp, "search", BU_CLBK_DURING, &gsh_db_search_callback, (void *)this);
351389

352390
// Assign gsh specific I/O handlers to the gedp
353391
gedp->ged_create_io_handler = &gsh_create_io_handler;
@@ -389,7 +427,7 @@ GshState::eval(int argc, const char **argv)
389427
return BRLCAD_ERROR;
390428
}
391429

392-
// We've got a valid GED command Before any BRL-CAD logic is executed;
430+
// We've got a valid GED command. Before any BRL-CAD logic is executed,
393431
// stash the state of the view info so we can recognize changes.
394432
view_checkpoint();
395433

0 commit comments

Comments
 (0)