Go to the source code of this file.
Defines | |
#define | MAX_HISTORY 50 |
#define | LINE_LENGTH 256 |
Functions | |
INT | cmd_edit (char *prompt, char *cmd, INT(*dir)(char *, INT *), INT(*idle)()) |
Variables | |
char | history [MAX_HISTORY][LINE_LENGTH] |
INT | his_index = 0 |
#define LINE_LENGTH 256 |
#define MAX_HISTORY 50 |
Definition at line 18 of file cmdedit.c.
Referenced by close_history(), cmd_edit(), log_history(), and open_history().
Definition at line 38 of file cmdedit.c.
Referenced by command_loop().
00039 { 00040 char line[LINE_LENGTH]; 00041 INT i, j, k, c, hi; 00042 INT status; 00043 DWORD last_time = 0; 00044 BOOL escape_flag = 0; 00045 00046 if (ss_getchar(0) == -1) { 00047 /* normal input if ss_getchar not supported */ 00048 fputs(prompt, stdout); 00049 ss_gets(cmd, 256); 00050 return strlen(cmd); 00051 } 00052 00053 fputs(prompt, stdout); 00054 fflush(stdout); 00055 00056 hi = his_index; 00057 memset(line, 0, LINE_LENGTH); 00058 memset(history[hi], 0, LINE_LENGTH); 00059 strcpy(line, cmd); 00060 fputs(line, stdout); 00061 i = strlen(cmd); 00062 fflush(stdout); 00063 00064 do { 00065 c = ss_getchar(0); 00066 00067 if (c == 27) 00068 escape_flag = TRUE; 00069 00070 if (c >= ' ' && c < CH_EXT && escape_flag) { 00071 escape_flag = FALSE; 00072 if (c == 'p') 00073 c = 6; 00074 } 00075 00076 /* normal input */ 00077 if (c >= ' ' && c < CH_EXT) { 00078 if (strlen(line) < LINE_LENGTH - 1) { 00079 for (j = strlen(line); j >= i; j--) 00080 line[j + 1] = line[j]; 00081 if (i < LINE_LENGTH - 1) { 00082 line[i++] = c; 00083 fputc(c, stdout); 00084 } 00085 for (j = i; j < (INT) strlen(line); j++) 00086 fputc(line[j], stdout); 00087 for (j = i; j < (INT) strlen(line); j++) 00088 fputc('\b', stdout); 00089 } 00090 } 00091 00092 /* BS */ 00093 if (c == CH_BS && i > 0) { 00094 i--; 00095 fputc('\b', stdout); 00096 for (j = i; j <= (INT) strlen(line); j++) { 00097 line[j] = line[j + 1]; 00098 if (line[j]) 00099 fputc(line[j], stdout); 00100 else 00101 fputc(' ', stdout); 00102 } 00103 for (k = 0; k < j - i; k++) 00104 fputc('\b', stdout); 00105 } 00106 00107 /* DELETE/Ctrl-D */ 00108 if (c == CH_DELETE || c == 4) { 00109 for (j = i; j <= (INT) strlen(line); j++) { 00110 line[j] = line[j + 1]; 00111 if (line[j]) 00112 fputc(line[j], stdout); 00113 else 00114 fputc(' ', stdout); 00115 } 00116 for (k = 0; k < j - i; k++) 00117 fputc('\b', stdout); 00118 } 00119 00120 /* Erase line: CTRL-W, CTRL-U */ 00121 if (c == 23 || c == 21) { 00122 i = strlen(line); 00123 memset(line, 0, sizeof(line)); 00124 printf("\r%s", prompt); 00125 for (j = 0; j < i; j++) 00126 fputc(' ', stdout); 00127 for (j = 0; j < i; j++) 00128 fputc('\b', stdout); 00129 i = 0; 00130 } 00131 00132 /* Erase line from cursor: CTRL-K */ 00133 if (c == 11) { 00134 for (j = i; j < (INT) strlen(line); j++) 00135 fputc(' ', stdout); 00136 for (j = i; j < (INT) strlen(line); j++) 00137 fputc('\b', stdout); 00138 for (j = strlen(line); j >= i; j--) 00139 line[j] = 0; 00140 } 00141 00142 /* left arrow, CTRL-B */ 00143 if ((c == CH_LEFT || c == 2) && i > 0) { 00144 i--; 00145 fputc('\b', stdout); 00146 } 00147 00148 /* right arrow, CTRL-F */ 00149 if ((c == CH_RIGHT || c == 6) && i < (INT) strlen(line)) 00150 fputc(line[i++], stdout); 00151 00152 /* HOME, CTRL-A */ 00153 if ((c == CH_HOME || c == 1) && i > 0) { 00154 for (j = 0; j < i; j++) 00155 fputc('\b', stdout); 00156 i = 0; 00157 } 00158 00159 /* END, CTRL-E */ 00160 if ((c == CH_END || c == 5) && i < (INT) strlen(line)) { 00161 for (j = i; j < (INT) strlen(line); j++) 00162 fputc(line[i++], stdout); 00163 i = strlen(line); 00164 } 00165 00166 /* up arrow / CTRL-P */ 00167 if (c == CH_UP || c == 16) { 00168 if (history[(hi + MAX_HISTORY - 1) % MAX_HISTORY][0]) { 00169 hi = (hi + MAX_HISTORY - 1) % MAX_HISTORY; 00170 i = strlen(line); 00171 fputc('\r', stdout); 00172 fputs(prompt, stdout); 00173 for (j = 0; j < i; j++) 00174 fputc(' ', stdout); 00175 for (j = 0; j < i; j++) 00176 fputc('\b', stdout); 00177 memcpy(line, history[hi], 256); 00178 i = strlen(line); 00179 for (j = 0; j < i; j++) 00180 fputc(line[j], stdout); 00181 } 00182 } 00183 00184 /* down arrow / CTRL-N */ 00185 if (c == CH_DOWN || c == 14) { 00186 if (history[hi][0]) { 00187 hi = (hi + 1) % MAX_HISTORY; 00188 i = strlen(line); 00189 fputc('\r', stdout); 00190 fputs(prompt, stdout); 00191 for (j = 0; j < i; j++) 00192 fputc(' ', stdout); 00193 for (j = 0; j < i; j++) 00194 fputc('\b', stdout); 00195 memcpy(line, history[hi], 256); 00196 i = strlen(line); 00197 for (j = 0; j < i; j++) 00198 fputc(line[j], stdout); 00199 } 00200 } 00201 00202 /* CTRL-F */ 00203 if (c == 6) { 00204 for (j = (hi + MAX_HISTORY - 1) % MAX_HISTORY; j != hi; 00205 j = (j + MAX_HISTORY - 1) % MAX_HISTORY) 00206 if (history[j][0] && strncmp(line, history[j], i) == 0) { 00207 memcpy(line, history[j], 256); 00208 fputs(line + i, stdout); 00209 i = strlen(line); 00210 break; 00211 } 00212 if (j == hi) 00213 fputc(7, stdout); 00214 } 00215 00216 /* tab */ 00217 if (c == 9 && dir != NULL) { 00218 status = dir(line, &i); 00219 00220 /* redraw line */ 00221 fputc('\r', stdout); 00222 fputs(prompt, stdout); 00223 fputs(line, stdout); 00224 00225 for (j = 0; j < (INT) strlen(line) - i; j++) 00226 fputc('\b', stdout); 00227 } 00228 00229 if (c != 0) { 00230 last_time = ss_millitime(); 00231 fflush(stdout); 00232 } 00233 00234 if ((ss_millitime() - last_time > 300) && idle != NULL) { 00235 status = idle(); 00236 00237 if (status) { 00238 fputc('\r', stdout); 00239 fputs(prompt, stdout); 00240 fputs(line, stdout); 00241 00242 for (j = 0; j < (INT) strlen(line) - i; j++) 00243 fputc('\b', stdout); 00244 00245 fflush(stdout); 00246 } 00247 } 00248 00249 } while (c != CH_CR); 00250 00251 strcpy(cmd, line); 00252 00253 if (dir != NULL) 00254 if (strcmp(cmd, history[(his_index + MAX_HISTORY - 1) % MAX_HISTORY]) != 0 && 00255 cmd[0]) { 00256 strcpy(history[his_index], cmd); 00257 his_index = (his_index + 1) % MAX_HISTORY; 00258 } 00259 00260 /* reset terminal */ 00261 ss_getchar(1); 00262 00263 fputc('\n', stdout); 00264 00265 return strlen(line); 00266 }
char history[MAX_HISTORY][LINE_LENGTH] |
Definition at line 23 of file cmdedit.c.
Referenced by cmd_edit(), MidasHistory::GetEventsFromEquipment(), and open_history().