auto.def: tclprefix should not be enabled by default
[jimtcl.git] / jim-history.c
blobfe6e628e34275c6503e8837cd41667a87d5ce7a4
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(interp, 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_setcompletion(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
40 Jim_HistorySetCompletion(interp, Jim_Length(argv[0]) ? argv[0] : NULL);
41 return JIM_OK;
44 static int history_cmd_load(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
46 Jim_HistoryLoad(Jim_String(argv[0]));
47 return JIM_OK;
50 static int history_cmd_save(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
52 Jim_HistorySave(Jim_String(argv[0]));
53 return JIM_OK;
56 static int history_cmd_add(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
58 Jim_HistoryAdd(Jim_String(argv[0]));
59 return JIM_OK;
62 static int history_cmd_show(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
64 Jim_HistoryShow();
65 return JIM_OK;
68 static const jim_subcmd_type history_command_table[] = {
69 { "getline",
70 "prompt ?varname?",
71 history_cmd_getline,
74 /* Description: Reads one line from the user. Similar to gets. */
76 { "completion",
77 "command",
78 history_cmd_setcompletion,
81 /* Description: Sets an autocompletion callback command, or none if "" */
83 { "load",
84 "filename",
85 history_cmd_load,
88 /* Description: Loads history from the given file, if possible */
90 { "save",
91 "filename",
92 history_cmd_save,
95 /* Description: Saves history to the given file */
97 { "add",
98 "line",
99 history_cmd_add,
102 /* Description: Adds the line to the history ands saves */
104 { "show",
105 NULL,
106 history_cmd_show,
109 /* Description: Displays the history */
111 { NULL }
114 static int JimHistorySubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
116 return Jim_CallSubCmd(interp, Jim_ParseSubCmd(interp, history_command_table, argc, argv), argc, argv);
119 static void JimHistoryDelProc(Jim_Interp *interp, void *privData)
121 Jim_Free(privData);
124 int Jim_historyInit(Jim_Interp *interp)
126 void **history;
127 if (Jim_PackageProvide(interp, "history", "1.0", JIM_ERRMSG))
128 return JIM_ERR;
130 history = Jim_Alloc(sizeof(*history));
131 *history = NULL;
133 Jim_CreateCommand(interp, "history", JimHistorySubCmdProc, history, JimHistoryDelProc);
134 return JIM_OK;