Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / usbmodeswitch / jim / jim-interactive.c
blob658e8d8ffc7bf14618818a599be4b9ae49054ce6
1 #include <errno.h>
2 #include <string.h>
3 #include "jim.h"
4 #include "jimautoconf.h"
6 #ifdef USE_LINENOISE
7 #include <unistd.h>
8 #include "linenoise.h"
9 #else
11 #define MAX_LINE_LEN 512
13 static char *linenoise(const char *prompt)
15 char *line = malloc(MAX_LINE_LEN);
17 fputs(prompt, stdout);
18 fflush(stdout);
20 if (fgets(line, MAX_LINE_LEN, stdin) == NULL) {
21 free(line);
22 return NULL;
24 return line;
26 #endif
28 int Jim_InteractivePrompt(Jim_Interp *interp)
30 int retcode = JIM_OK;
31 char *history_file = NULL;
32 #ifdef USE_LINENOISE
33 const char *home;
35 home = getenv("HOME");
36 if (home && isatty(STDIN_FILENO)) {
37 int history_len = strlen(home) + sizeof("/.jim_history");
38 history_file = Jim_Alloc(history_len);
39 snprintf(history_file, history_len, "%s/.jim_history", home);
40 linenoiseHistoryLoad(history_file);
42 #endif
44 printf("Welcome to Jim version %d.%d" JIM_NL,
45 JIM_VERSION / 100, JIM_VERSION % 100);
46 Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, "1");
48 while (1) {
49 Jim_Obj *scriptObjPtr;
50 const char *result;
51 int reslen;
52 char prompt[20];
53 const char *str;
55 if (retcode != 0) {
56 const char *retcodestr = Jim_ReturnCode(retcode);
58 if (*retcodestr == '?') {
59 snprintf(prompt, sizeof(prompt) - 3, "[%d] ", retcode);
61 else {
62 snprintf(prompt, sizeof(prompt) - 3, "[%s] ", retcodestr);
65 else {
66 prompt[0] = '\0';
68 strcat(prompt, ". ");
70 scriptObjPtr = Jim_NewStringObj(interp, "", 0);
71 Jim_IncrRefCount(scriptObjPtr);
72 while (1) {
73 char state;
74 int len;
75 char *line;
77 line = linenoise(prompt);
78 if (line == NULL) {
79 if (errno == EINTR) {
80 continue;
82 Jim_DecrRefCount(interp, scriptObjPtr);
83 goto out;
85 if (Jim_Length(scriptObjPtr) != 0) {
86 Jim_AppendString(interp, scriptObjPtr, "\n", 1);
88 Jim_AppendString(interp, scriptObjPtr, line, -1);
89 free(line);
90 str = Jim_GetString(scriptObjPtr, &len);
91 if (len == 0) {
92 continue;
94 if (Jim_ScriptIsComplete(str, len, &state))
95 break;
97 snprintf(prompt, sizeof(prompt), "%c> ", state);
99 #ifdef USE_LINENOISE
100 if (strcmp(str, "h") == 0) {
101 /* built-in history command */
102 int i;
103 int len;
104 char **history = linenoiseHistory(&len);
105 for (i = 0; i < len; i++) {
106 printf("%4d %s\n", i + 1, history[i]);
108 Jim_DecrRefCount(interp, scriptObjPtr);
109 continue;
112 linenoiseHistoryAdd(Jim_String(scriptObjPtr));
113 if (history_file) {
114 linenoiseHistorySave(history_file);
116 #endif
117 retcode = Jim_EvalObj(interp, scriptObjPtr);
118 Jim_DecrRefCount(interp, scriptObjPtr);
122 if (retcode == JIM_EXIT) {
123 Jim_Free(history_file);
124 return JIM_EXIT;
126 if (retcode == JIM_ERR) {
127 Jim_MakeErrorMessage(interp);
129 result = Jim_GetString(Jim_GetResult(interp), &reslen);
130 if (reslen) {
131 printf("%s\n", result);
134 out:
135 Jim_Free(history_file);
136 return JIM_OK;