MIDAS
Loading...
Searching...
No Matches
cmdedit.cxx
Go to the documentation of this file.
1/********************************************************************\
2
3 Name: cmdedit.c
4 Created by: Stefan Ritt
5
6 Contents: Command-line editor for ODBEdit
7
8 $Id:$
9
10\********************************************************************/
11
12#include "midas.h"
13#include "msystem.h"
14
15#ifdef OS_MSDOS
16#define MAX_HISTORY 10
17#else
18#define MAX_HISTORY 50
19#endif
20
21#define LINE_LENGTH 256
22
25
26#ifdef OS_VMS
27
28INT cmd_edit(const char *prompt, char *cmd, INT(*dir) (char *, INT *), INT(*idle) ())
29{
31 ss_gets(cmd, 256);
32
33 return strlen(cmd);
34}
35
36#else
37
38INT cmd_edit(const char *prompt, char *cmd, INT(*dir) (char *, INT *), INT(*idle) ())
39{
40 char line[LINE_LENGTH];
41 INT i, j, k, c, hi;
42 INT status;
43 DWORD last_time = 0;
44 BOOL escape_flag = 0;
45
46 if (ss_getchar(0) == -1) {
47 /* normal input if ss_getchar not supported */
49 ss_gets(cmd, 256);
50 return strlen(cmd);
51 }
52
55
56 hi = his_index;
57 memset(line, 0, LINE_LENGTH);
59 strcpy(line, cmd);
60 fputs(line, stdout);
61 i = strlen(cmd);
63
64 do {
65 c = ss_getchar(0);
66
67 if (c == 27)
69
70 if (c >= ' ' && c < CH_EXT && escape_flag) {
72 if (c == 'p')
73 c = 6;
74 }
75
76 /* normal input */
77 if (c >= ' ' && c < CH_EXT) {
78 if (strlen(line) < LINE_LENGTH - 1) {
79 for (j = strlen(line); j >= i; j--)
80 line[j + 1] = line[j];
81 if (i < LINE_LENGTH - 1) {
82 line[i++] = c;
83 fputc(c, stdout);
84 }
85 for (j = i; j < (INT) strlen(line); j++)
86 fputc(line[j], stdout);
87 for (j = i; j < (INT) strlen(line); j++)
88 fputc('\b', stdout);
89 }
90 }
91
92 /* BS */
93 if (c == CH_BS && i > 0) {
94 i--;
95 fputc('\b', stdout);
96 for (j = i; j <= (INT) strlen(line); j++) {
97 line[j] = line[j + 1];
98 if (line[j])
99 fputc(line[j], stdout);
100 else
101 fputc(' ', stdout);
102 }
103 for (k = 0; k < j - i; k++)
104 fputc('\b', stdout);
105 }
106
107 /* DELETE/Ctrl-D */
108 if (c == CH_DELETE || c == 4) {
109 for (j = i; j <= (INT) strlen(line); j++) {
110 line[j] = line[j + 1];
111 if (line[j])
112 fputc(line[j], stdout);
113 else
114 fputc(' ', stdout);
115 }
116 for (k = 0; k < j - i; k++)
117 fputc('\b', stdout);
118 }
119
120 /* Erase line: CTRL-W, CTRL-U */
121 if (c == 23 || c == 21) {
122 i = strlen(line);
123 memset(line, 0, sizeof(line));
124 printf("\r%s", prompt);
125 for (j = 0; j < i; j++)
126 fputc(' ', stdout);
127 for (j = 0; j < i; j++)
128 fputc('\b', stdout);
129 i = 0;
130 }
131
132 /* Erase line from cursor: CTRL-K */
133 if (c == 11) {
134 for (j = i; j < (INT) strlen(line); j++)
135 fputc(' ', stdout);
136 for (j = i; j < (INT) strlen(line); j++)
137 fputc('\b', stdout);
138 for (j = strlen(line); j >= i; j--)
139 line[j] = 0;
140 }
141
142 /* left arrow, CTRL-B */
143 if ((c == CH_LEFT || c == 2) && i > 0) {
144 i--;
145 fputc('\b', stdout);
146 }
147
148 /* right arrow, CTRL-F */
149 if ((c == CH_RIGHT || c == 6) && i < (INT) strlen(line))
150 fputc(line[i++], stdout);
151
152 /* HOME, CTRL-A */
153 if ((c == CH_HOME || c == 1) && i > 0) {
154 for (j = 0; j < i; j++)
155 fputc('\b', stdout);
156 i = 0;
157 }
158
159 /* END, CTRL-E */
160 if ((c == CH_END || c == 5) && i < (INT) strlen(line)) {
161 for (j = i; j < (INT) strlen(line); j++)
162 fputc(line[i++], stdout);
163 i = strlen(line);
164 }
165
166 /* up arrow / CTRL-P */
167 if (c == CH_UP || c == 16) {
168 if (history[(hi + MAX_HISTORY - 1) % MAX_HISTORY][0]) {
169 hi = (hi + MAX_HISTORY - 1) % MAX_HISTORY;
170 i = strlen(line);
171 fputc('\r', stdout);
173 for (j = 0; j < i; j++)
174 fputc(' ', stdout);
175 for (j = 0; j < i; j++)
176 fputc('\b', stdout);
177 memcpy(line, history[hi], 256);
178 i = strlen(line);
179 for (j = 0; j < i; j++)
180 fputc(line[j], stdout);
181 }
182 }
183
184 /* down arrow / CTRL-N */
185 if (c == CH_DOWN || c == 14) {
186 if (history[hi][0]) {
187 hi = (hi + 1) % MAX_HISTORY;
188 i = strlen(line);
189 fputc('\r', stdout);
191 for (j = 0; j < i; j++)
192 fputc(' ', stdout);
193 for (j = 0; j < i; j++)
194 fputc('\b', stdout);
195 memcpy(line, history[hi], 256);
196 i = strlen(line);
197 for (j = 0; j < i; j++)
198 fputc(line[j], stdout);
199 }
200 }
201
202 /* CTRL-F */
203 if (c == 6) {
204 for (j = (hi + MAX_HISTORY - 1) % MAX_HISTORY; j != hi;
205 j = (j + MAX_HISTORY - 1) % MAX_HISTORY)
206 if (history[j][0] && strncmp(line, history[j], i) == 0) {
207 memcpy(line, history[j], 256);
208 fputs(line + i, stdout);
209 i = strlen(line);
210 break;
211 }
212 if (j == hi)
213 fputc(7, stdout);
214 }
215
216 /* tab */
217 if (c == 9 && dir != NULL) {
218 status = dir(line, &i);
219
220 /* redraw line */
221 fputc('\r', stdout);
223 fputs(line, stdout);
224
225 for (j = 0; j < (INT) strlen(line) - i; j++)
226 fputc('\b', stdout);
227 }
228
229 if (c != 0) {
231 fflush(stdout);
232 }
233
234 if ((ss_millitime() - last_time > 300) && idle != NULL) {
235 status = idle();
236
237 if (status) {
238 fputc('\r', stdout);
240 fputs(line, stdout);
241
242 for (j = 0; j < (INT) strlen(line) - i; j++)
243 fputc('\b', stdout);
244
245 fflush(stdout);
246 }
247 }
248
249 } while (c != CH_CR && c != CH_LF);
250
251 strcpy(cmd, line);
252
253 if (dir != NULL)
254 if (strcmp(cmd, history[(his_index + MAX_HISTORY - 1) % MAX_HISTORY]) != 0 &&
255 cmd[0]) {
256 strcpy(history[his_index], cmd);
258 }
259
260 /* reset terminal */
261 ss_getchar(1);
262
263 fputc('\n', stdout);
264
265 return strlen(line);
266}
267
268#endif
#define FALSE
Definition cfortran.h:309
INT cmd_edit(const char *prompt, char *cmd, INT(*dir)(char *, INT *), INT(*idle)())
Definition cmdedit.cxx:38
char history[MAX_HISTORY][LINE_LENGTH]
Definition cmdedit.cxx:23
#define MAX_HISTORY
Definition cmdedit.cxx:18
#define LINE_LENGTH
Definition cmdedit.cxx:21
INT his_index
Definition cmdedit.cxx:24
unsigned int DWORD
Definition mcstd.h:51
DWORD ss_millitime()
Definition system.cxx:3393
INT ss_getchar(BOOL reset)
Definition system.cxx:7503
char * ss_gets(char *string, int size)
Definition system.cxx:7770
DWORD last_time
Definition mana.cxx:3070
INT i
Definition mdump.cxx:32
#define CH_LF
Definition midas.h:447
#define CH_END
Definition midas.h:455
DWORD BOOL
Definition midas.h:105
#define CH_DOWN
Definition midas.h:459
int INT
Definition midas.h:129
#define CH_DELETE
Definition midas.h:454
#define CH_EXT
Definition midas.h:450
#define CH_RIGHT
Definition midas.h:460
#define CH_CR
Definition midas.h:448
#define CH_LEFT
Definition midas.h:461
#define TRUE
Definition midas.h:182
#define CH_HOME
Definition midas.h:452
#define CH_UP
Definition midas.h:458
#define CH_BS
Definition midas.h:445
INT j
Definition odbhist.cxx:40
INT k
Definition odbhist.cxx:40
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
char c
Definition system.cxx:1310