This source file includes following definitions.
- db_putstring
- db_putnchars
- db_delete
- db_delete_line
- db_inputchar
- db_readline
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 #include <sys/param.h>
34 #include <sys/proc.h>
35
36 #include <uvm/uvm_extern.h>
37
38 #include <machine/db_machdep.h>
39
40 #include <ddb/db_var.h>
41 #include <ddb/db_output.h>
42 #include <ddb/db_command.h>
43 #include <ddb/db_sym.h>
44 #include <ddb/db_extern.h>
45
46 #include <dev/cons.h>
47
48
49
50
51
52
53
54
55
56
57 char * db_lbuf_start;
58 char * db_lbuf_end;
59 char * db_lc;
60 char * db_le;
61 #if DB_HISTORY_SIZE != 0
62 char db_history[DB_HISTORY_SIZE];
63 int db_history_size = DB_HISTORY_SIZE;
64 char * db_history_curr = db_history;
65 char * db_history_last = db_history;
66 char * db_history_prev = (char *) 0;
67 #endif
68
69
70 #define CTRL(c) ((c) & 0x1f)
71 #define isspace(c) ((c) == ' ' || (c) == '\t')
72 #define BLANK ' '
73 #define BACKUP '\b'
74
75 void
76 db_putstring(char *s, int count)
77 {
78 while (--count >= 0)
79 cnputc(*s++);
80 }
81
82 void
83 db_putnchars(int c, int count)
84 {
85 while (--count >= 0)
86 cnputc(c);
87 }
88
89
90
91
92 #define DEL_FWD 0
93 #define DEL_BWD 1
94 void
95 db_delete(int n, int bwd)
96 {
97 char *p;
98
99 if (bwd) {
100 db_lc -= n;
101 db_putnchars(BACKUP, n);
102 }
103 for (p = db_lc; p < db_le-n; p++) {
104 *p = *(p+n);
105 cnputc(*p);
106 }
107 db_putnchars(BLANK, n);
108 db_putnchars(BACKUP, db_le - db_lc);
109 db_le -= n;
110 }
111
112 void
113 db_delete_line(void)
114 {
115 db_delete(db_le - db_lc, DEL_FWD);
116 db_delete(db_lc - db_lbuf_start, DEL_BWD);
117 db_le = db_lc = db_lbuf_start;
118 }
119
120 #if DB_HISTORY_SIZE != 0
121 #define INC_DB_CURR() \
122 do { \
123 db_history_curr++; \
124 if (db_history_curr > \
125 db_history + db_history_size - 1) \
126 db_history_curr = db_history; \
127 } while (0)
128 #define DEC_DB_CURR() \
129 do { \
130 db_history_curr--; \
131 if (db_history_curr < db_history) \
132 db_history_curr = db_history + \
133 db_history_size - 1; \
134 } while (0)
135 #endif
136
137
138 int
139 db_inputchar(int c)
140 {
141 switch (c) {
142 case CTRL('b'):
143
144 if (db_lc > db_lbuf_start) {
145 cnputc(BACKUP);
146 db_lc--;
147 }
148 break;
149 case CTRL('f'):
150
151 if (db_lc < db_le) {
152 cnputc(*db_lc);
153 db_lc++;
154 }
155 break;
156 case CTRL('a'):
157
158 while (db_lc > db_lbuf_start) {
159 cnputc(BACKUP);
160 db_lc--;
161 }
162 break;
163 case CTRL('e'):
164
165 while (db_lc < db_le) {
166 cnputc(*db_lc);
167 db_lc++;
168 }
169 break;
170 case CTRL('w'):
171
172 while (db_lc > db_lbuf_start && db_lc[-1] != BLANK)
173 db_delete(1, DEL_BWD);
174 break;
175 case CTRL('h'):
176 case 0177:
177
178 if (db_lc > db_lbuf_start)
179 db_delete(1, DEL_BWD);
180 break;
181 case CTRL('d'):
182
183 if (db_lc < db_le)
184 db_delete(1, DEL_FWD);
185 break;
186 case CTRL('k'):
187
188 if (db_lc < db_le)
189 db_delete(db_le - db_lc, DEL_FWD);
190 break;
191 case CTRL('u'):
192
193 db_delete_line();
194 break;
195 case CTRL('t'):
196
197 if (db_lc >= db_lbuf_start + 2) {
198 c = db_lc[-2];
199 db_lc[-2] = db_lc[-1];
200 db_lc[-1] = c;
201 cnputc(BACKUP);
202 cnputc(BACKUP);
203 cnputc(db_lc[-2]);
204 cnputc(db_lc[-1]);
205 }
206 break;
207 #if DB_HISTORY_SIZE != 0
208 case CTRL('p'):
209 DEC_DB_CURR();
210 while (db_history_curr != db_history_last) {
211 DEC_DB_CURR();
212 if (*db_history_curr == '\0')
213 break;
214 }
215 db_delete_line();
216 if (db_history_curr == db_history_last) {
217 INC_DB_CURR();
218 db_le = db_lc = db_lbuf_start;
219 } else {
220 char *p;
221 INC_DB_CURR();
222 for (p = db_history_curr, db_le = db_lbuf_start;*p; ) {
223 *db_le++ = *p++;
224 if (p == db_history + db_history_size)
225 p = db_history;
226 }
227 db_lc = db_le;
228 }
229 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
230 break;
231 case CTRL('n'):
232 while (db_history_curr != db_history_last) {
233 if (*db_history_curr == '\0')
234 break;
235 INC_DB_CURR();
236 }
237 if (db_history_curr != db_history_last) {
238 INC_DB_CURR();
239 db_delete_line();
240 if (db_history_curr != db_history_last) {
241 char *p;
242 for (p = db_history_curr,
243 db_le = db_lbuf_start; *p;) {
244 *db_le++ = *p++;
245 if (p == db_history + db_history_size)
246 p = db_history;
247 }
248 db_lc = db_le;
249 }
250 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
251 }
252 break;
253 #endif
254 case CTRL('r'):
255 db_putstring("^R\n", 3);
256 if (db_le > db_lbuf_start) {
257 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
258 db_putnchars(BACKUP, db_le - db_lc);
259 }
260 break;
261 case '\n':
262 case '\r':
263 #if DB_HISTORY_SIZE != 0
264
265
266
267
268
269 if (db_history_curr == db_history_prev) {
270 char *pp, *pc;
271
272
273
274
275 for (pp = db_history_prev, pc = db_lbuf_start;
276 pc != db_le && *pp; ) {
277 if (*pp != *pc)
278 break;
279 if (++pp == db_history + db_history_size)
280 pp = db_history;
281 pc++;
282 }
283 if (!*pp && pc == db_le) {
284
285
286
287 db_history_curr = db_history_last;
288 *db_le++ = c;
289 return TRUE;
290 }
291 }
292 if (db_le != db_lbuf_start) {
293 char *p;
294 db_history_prev = db_history_last;
295 for (p = db_lbuf_start; p != db_le; p++) {
296 *db_history_last++ = *p;
297 if (db_history_last ==
298 db_history + db_history_size)
299 db_history_last = db_history;
300 }
301 *db_history_last++ = '\0';
302 }
303 db_history_curr = db_history_last;
304 #endif
305 *db_le++ = c;
306 return TRUE;
307 default:
308 if (db_le == db_lbuf_end) {
309 cnputc('\007');
310 }
311 else if (c >= ' ' && c <= '~') {
312 char *p;
313
314 for (p = db_le; p > db_lc; p--)
315 *p = *(p-1);
316 *db_lc++ = c;
317 db_le++;
318 cnputc(c);
319 db_putstring(db_lc, db_le - db_lc);
320 db_putnchars(BACKUP, db_le - db_lc);
321 }
322 break;
323 }
324 return FALSE;
325 }
326
327 int
328 db_readline(char *lstart, int lsize)
329 {
330 db_force_whitespace();
331
332 db_lbuf_start = lstart;
333 db_lbuf_end = lstart + lsize - 1;
334 db_lc = lstart;
335 db_le = lstart;
336
337 while (!db_inputchar(cngetc()))
338 continue;
339
340 db_putchar('\n');
341
342 *db_le = 0;
343 return (db_le - db_lbuf_start);
344 }