tcltest: do a better job of cleanup up after tests
[jimtcl.git] / jim-history.c
blob9a56e04d6519d541f270cdd2c858c02ce4587613
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
5 #include "jim.h"
6 #include "jimautoconf.h"
7 #include "jim-subcmd.h"
9 static int history_cmd_getline(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
11 Jim_Obj *objPtr;
12 char *line = Jim_HistoryGetline(Jim_String(argv[0]));
14 /* On EOF returns -1 if varName was specified; otherwise the empty string. */
15 if (line == NULL) {
16 if (argc == 2) {
17 Jim_SetResultInt(interp, -1);
19 return JIM_OK;
22 objPtr = Jim_NewStringObjNoAlloc(interp, line, -1);
24 /* Returns the length of the string if varName was specified */
25 if (argc == 2) {
26 if (Jim_SetVariable(interp, argv[1], objPtr) != JIM_OK) {
27 Jim_FreeNewObj(interp, objPtr);
28 return JIM_ERR;
30 Jim_SetResultInt(interp, Jim_Length(objPtr));
32 else {
33 Jim_SetResult(interp, objPtr);
35 return JIM_OK;
38 static int history_cmd_load(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
40 Jim_HistoryLoad(Jim_String(argv[0]));
41 return JIM_OK;
44 static int history_cmd_save(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
46 Jim_HistorySave(Jim_String(argv[0]));
47 return JIM_OK;
50 static int history_cmd_add(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
52 Jim_HistoryAdd(Jim_String(argv[0]));
53 return JIM_OK;
56 static int history_cmd_show(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
58 Jim_HistoryShow();
59 return JIM_OK;
62 static const jim_subcmd_type history_command_table[] = {
63 { "getline",
64 "prompt ?varname?",
65 history_cmd_getline,
68 /* Description: Reads one line from the user. Similar to gets. */
70 { "load",
71 "filename",
72 history_cmd_load,
75 /* Description: Loads history from the given file, if possible */
77 { "save",
78 "filename",
79 history_cmd_save,
82 /* Description: Saves history to the given file */
84 { "add",
85 "line",
86 history_cmd_add,
89 /* Description: Adds the line to the history ands saves */
91 { "show",
92 NULL,
93 history_cmd_show,
96 /* Description: Displays the history */
98 { NULL }
101 static int JimHistorySubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
103 return Jim_CallSubCmd(interp, Jim_ParseSubCmd(interp, history_command_table, argc, argv), argc, argv);
106 static void JimHistoryDelProc(Jim_Interp *interp, void *privData)
108 Jim_Free(privData);
111 int Jim_historyInit(Jim_Interp *interp)
113 void **history;
114 if (Jim_PackageProvide(interp, "history", "1.0", JIM_ERRMSG))
115 return JIM_ERR;
117 history = Jim_Alloc(sizeof(*history));
118 *history = NULL;
120 Jim_CreateCommand(interp, "history", JimHistorySubCmdProc, history, JimHistoryDelProc);
121 return JIM_OK;