-
Notifications
You must be signed in to change notification settings - Fork 120
/
jim-history.c
154 lines (140 loc) · 3.71 KB
/
jim-history.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "jim.h"
#include "jimautoconf.h"
#include "jim-subcmd.h"
static int history_cmd_getline(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_Obj *objPtr;
char *line = Jim_HistoryGetline(interp, Jim_String(argv[0]));
/* On EOF returns -1 if varName was specified; otherwise the empty string. */
if (line == NULL) {
if (argc == 2) {
Jim_SetResultInt(interp, -1);
}
return JIM_OK;
}
objPtr = Jim_NewStringObjNoAlloc(interp, line, -1);
/* Returns the length of the string if varName was specified */
if (argc == 2) {
if (Jim_SetVariable(interp, argv[1], objPtr) != JIM_OK) {
Jim_FreeNewObj(interp, objPtr);
return JIM_ERR;
}
Jim_SetResultInt(interp, Jim_Length(objPtr));
}
else {
Jim_SetResult(interp, objPtr);
}
return JIM_OK;
}
static int history_cmd_setcompletion(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_HistorySetCompletion(interp, Jim_Length(argv[0]) ? argv[0] : NULL);
return JIM_OK;
}
static int history_cmd_sethints(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_HistorySetHints(interp, Jim_Length(argv[0]) ? argv[0] : NULL);
return JIM_OK;
}
static int history_cmd_load(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_HistoryLoad(Jim_String(argv[0]));
return JIM_OK;
}
static int history_cmd_save(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_HistorySave(Jim_String(argv[0]));
return JIM_OK;
}
static int history_cmd_add(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_HistoryAdd(Jim_String(argv[0]));
return JIM_OK;
}
static int history_cmd_show(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_HistoryShow();
return JIM_OK;
}
static int history_cmd_keep(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
long len;
if (argc == 1) {
if (Jim_GetLong(interp, argv[0], &len) != JIM_OK) {
return JIM_ERR;
}
Jim_HistorySetMaxLen(len);
}
else {
Jim_SetResultInt(interp, Jim_HistoryGetMaxLen());
}
return JIM_OK;
}
static const jim_subcmd_type history_command_table[] = {
{ "add",
"line",
history_cmd_add,
1,
1,
/* Description: Adds the line to the history ands saves */
},
{ "completion",
"command",
history_cmd_setcompletion,
1,
1,
/* Description: Sets an autocompletion callback command, or none if "" */
},
{ "hints",
"command",
history_cmd_sethints,
1,
1,
/* Description: Sets a hints callback command, or none if "" */
},
{ "getline",
"prompt ?varname?",
history_cmd_getline,
1,
2,
/* Description: Reads one line from the user. Similar to gets. */
},
{ "keep",
"?count?",
history_cmd_keep,
0,
1,
/* Description: Set or display the max history length */
},
{ "load",
"filename",
history_cmd_load,
1,
1,
/* Description: Loads history from the given file, if possible */
},
{ "save",
"filename",
history_cmd_save,
1,
1,
/* Description: Saves history to the given file */
},
{ "show",
NULL,
history_cmd_show,
0,
0,
/* Description: Displays the history */
},
{ NULL }
};
int Jim_historyInit(Jim_Interp *interp)
{
Jim_PackageProvideCheck(interp, "history");
Jim_CreateCommand(interp, "history", Jim_SubCmdProc, (void *)history_command_table, NULL);
return JIM_OK;
}