-
Notifications
You must be signed in to change notification settings - Fork 22
/
console.c
executable file
·582 lines (489 loc) · 11.9 KB
/
console.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Console input and output.
// Input is from the keyboard or serial port.
// Output is written to the screen and serial port.
#include "types.h"
#include "defs.h"
#include "param.h"
#include "traps.h"
#include "spinlock.h"
#include "fs.h"
#include "file.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "x86.h"
#define KEY_UP 0xE2
#define KEY_DN 0xE3
#define KEY_LF 0xE4
#define KEY_RT 0xE5
#define MAX_HISTORY (16) /*the max number of the comand histories*/
#define MAX_COMMAND_LENGTH (128) /*the max length of the comand*/
//its used to hold the command history
char commandHistory[MAX_HISTORY][MAX_COMMAND_LENGTH];
int commandHistoryCounter = 0;
int currentCommandId = 0;
static void consputc(int);
static int panicked = 0;
static struct {
struct spinlock lock;
int locking;
} cons;
/*
its a system call to access history command for user programm
parameters:
buffer
a pointer to a buffer that will hold the history command, assume max buffer size if 128
historyId
the history line requested, values 0 - 15 (MAX_HISTORY)
return
0 history copied to the buffer properly
-1 no history for the given id
-2 historyId illegal
*/
int
sys_history(void){
char * buffer;
int historyId;
int ret = 0;
//get parameters
if(argstr(0, &buffer) < 0)
return -1;
if(argint(1, &historyId) < 0)
return -1;
if(historyId >= commandHistoryCounter){
return ret = -1;
}else if(historyId < 0 || historyId >= MAX_HISTORY){
return ret = -2;
}else{
memmove(buffer, commandHistory[historyId], MAX_COMMAND_LENGTH * sizeof(char));
}
return ret;
}
/*
add a command to the history array
*/
void addHistory(char *command){
if((command[0]!='\0')
{
int length = strlen(command) <= MAX_COMMAND_LENGTH ? strlen(command) : MAX_COMMAND_LENGTH-1;
int i;
if(commandHistoryCounter < MAX_HISTORY){
commandHistoryCounter++;
}else{
// move back
for(i = 0; i < MAX_HISTORY - 1; i++){
memmove(commandHistory[i], commandHistory[i+1], sizeof(char)* MAX_COMMAND_LENGTH);
}
}
//store
memmove(commandHistory[commandHistoryCounter-1], command, sizeof(char)* length);
commandHistory[commandHistoryCounter-1][length] = '\0';
currentCommandId = commandHistoryCounter - 1;
}
}
static void
printint(int xx, int base, int sign)
{
static char digits[] = "0123456789abcdef";
char buf[16];
int i;
uint x;
if(sign && (sign = xx < 0))
x = -xx;
else
x = xx;
i = 0;
do{
buf[i++] = digits[x % base];
}while((x /= base) != 0);
if(sign)
buf[i++] = '-';
while(--i >= 0)
consputc(buf[i]);
}
//PAGEBREAK: 50
// Print to the console. only understands %d, %x, %p, %s.
void
cprintf(char *fmt, ...)
{
int i, c, locking;
uint *argp;
char *s;
locking = cons.locking;
if(locking)
acquire(&cons.lock);
if (fmt == 0)
panic("null fmt");
argp = (uint*)(void*)(&fmt + 1);
for(i = 0; (c = fmt[i] & 0xff) != 0; i++){
if(c != '%'){
consputc(c);
continue;
}
c = fmt[++i] & 0xff;
if(c == 0)
break;
switch(c){
case 'd':
printint(*argp++, 10, 1);
break;
case 'x':
case 'p':
printint(*argp++, 16, 0);
break;
case 's':
if((s = (char*)*argp++) == 0)
s = "(null)";
for(; *s; s++)
consputc(*s);
break;
case '%':
consputc('%');
break;
default:
// Print unknown % sequence to draw attention.
consputc('%');
consputc(c);
break;
}
}
if(locking)
release(&cons.lock);
}
void
panic(char *s)
{
int i;
uint pcs[10];
cli();
cons.locking = 0;
cprintf("cpu%d: panic: ", cpu->id);
cprintf(s);
cprintf("\n");
getcallerpcs(&s, pcs);
for(i=0; i<10; i++)
cprintf(" %p", pcs[i]);
panicked = 1; // freeze other CPU
for(;;)
;
}
// how many back on this line
int back_counter = 0;
#define INPUT_BUF 128
struct {
struct spinlock lock;
char buf[INPUT_BUF];
uint r; // Read index
uint w; // Write index
uint e; // Edit index
uint pos; // current pos, real cursor pos = row * 80 + pos
} input;
//PAGEBREAK: 50
#define BACKSPACE 0x100
#define CRTPORT 0x3d4
static ushort *crt = (ushort*)P2V(0xb8000); // CGA memory
static void
cgaputc(int c)
{
int pos;
// Cursor position: col + 80*row.
outb(CRTPORT, 14); //read line number? 1280 ... 256 per line??
pos = inb(CRTPORT+1) << 8;
outb(CRTPORT, 15); //read column? 2
pos |= inb(CRTPORT+1);
if(c == '\n')
pos += 80 - pos%80;
else if(c == BACKSPACE){
if(pos > 0) --pos;
} else
crt[pos++] = (c&0xff) | 0x0700; // black on white
if((pos/80) >= 24){ // Scroll up.
memmove(crt, crt+80, sizeof(crt[0])*23*80);
pos -= 80;
memset(crt+pos, 0, sizeof(crt[0])*(24*80 - pos));
}
outb(CRTPORT, 14);
outb(CRTPORT+1, pos>>8);
outb(CRTPORT, 15);
outb(CRTPORT+1, pos);
crt[pos] = ' ' | 0x0700;
}
//move back one cursor
void vga_move_back_cursor(){
int pos;
// get cursor position
outb(CRTPORT, 14);
pos = inb(CRTPORT+1) << 8;
outb(CRTPORT, 15);
pos |= inb(CRTPORT+1);
// move back
pos--;
// reset cursor
outb(CRTPORT, 15);
outb(CRTPORT+1, (unsigned char)(pos&0xFF));
outb(CRTPORT, 14);
outb(CRTPORT+1, (unsigned char )((pos>>8)&0xFF));
//crt[pos] = ' ' | 0x0700;
}
void vga_move_forward_cursor(){
int pos;
// get cursor position
outb(CRTPORT, 14);
pos = inb(CRTPORT+1) << 8;
outb(CRTPORT, 15);
pos |= inb(CRTPORT+1);
// move back
pos++;
// reset cursor
outb(CRTPORT, 15);
outb(CRTPORT+1, (unsigned char)(pos&0xFF));
outb(CRTPORT, 14);
outb(CRTPORT+1, (unsigned char )((pos>>8)&0xFF));
//crt[pos] = ' ' | 0x0700;
}
/*
insert a char to CGA buffer
c the character
*/
void vga_insert_char(int c, int back_counter){
int pos;
// get cursor position
outb(CRTPORT, 14);
pos = inb(CRTPORT+1) << 8;
outb(CRTPORT, 15);
pos |= inb(CRTPORT+1);
//move back crt buffer
for(int i = pos + back_counter; i >= pos; i--){
crt[i+1] = crt[i];
}
crt[pos] = (c&0xff) | 0x0700;
// move cursor to next position
pos += 1;
outb(CRTPORT, 14);
outb(CRTPORT+1, pos>>8);
outb(CRTPORT, 15);
outb(CRTPORT+1, pos);
crt[pos+back_counter] = ' ' | 0x0700;
}
/*delete one character from vga*/
void vga_remove_char(){
int pos;
// get cursor position
outb(CRTPORT, 14);
pos = inb(CRTPORT+1) << 8;
outb(CRTPORT, 15);
pos |= inb(CRTPORT+1);
// move back
pos--;
// reset cursor
outb(CRTPORT, 15);
outb(CRTPORT+1, (unsigned char)(pos&0xFF));
outb(CRTPORT, 14);
outb(CRTPORT+1, (unsigned char )((pos>>8)&0xFF));
crt[pos] = ' ' | 0x0700;
}
void
consputc(int c)
{
if(panicked){
cli();
for(;;)
;
}
// write to serial port
if(c == BACKSPACE){
uartputc('\b');
uartputc(' ');
uartputc('\b');
} else
uartputc(c);
// write to screen
cgaputc(c);
}
#define C(x) ((x)-'@') // Control-x
void
consoleintr(int (*getc)(void))
{
int c;
char buffer[MAX_COMMAND_LENGTH];
int x;
acquire(&input.lock);
while((c = getc()) >= 0){
switch(c){
case C('P'): // Process listing.
procdump();
break;
case C('U'): // Kill line.
while(input.e != input.w &&
input.buf[(input.e-1) % INPUT_BUF] != '\n'){
input.e--;
consputc(BACKSPACE);
}
break;
case C('H'): case '\x7f': // Backspace
if(input.e != input.w){
input.e--;
consputc(BACKSPACE);
}
break;
case KEY_LF:
if(input.pos > input.r){ // cannot beyond most left character
input.pos --; // move back one
back_counter += 1;
vga_move_back_cursor();
}
break;
case KEY_RT:
if(input.pos < input.e){ // cannot beyond most left character
input.pos ++; // move back one
back_counter -= 1;
vga_move_forward_cursor();
}
break;
case KEY_UP: // last command in history
if(currentCommandId > 0){
//move cursor to most right position
for(int i=input.pos; i < input.e; i++){
vga_move_forward_cursor();
}
//clear current input
while(input.e > input.w){
input.e--;
vga_remove_char();
}
//show last command
for(int i=0; i < strlen(commandHistory[currentCommandId]); i++){
x = commandHistory[currentCommandId][i];
consputc(x);
input.buf[input.e++] = x;
}
currentCommandId --;
input.pos = input.e;
}
break;
case KEY_DN: // last command in history
if(currentCommandId < commandHistoryCounter-1){
//move cursor to most right position
for(int i=input.pos; i < input.e; i++){
vga_move_forward_cursor();
}
//clear current input
while(input.e > input.w){
input.e--;
vga_remove_char();
}
//show last command
currentCommandId ++;
for(int i=0; i < strlen(commandHistory[currentCommandId]); i++){
x = commandHistory[currentCommandId][i];
consputc(x);
input.buf[input.e++] = x;
}
input.pos = input.e;
}
break;
default:
if(c != 0 && input.e-input.r < INPUT_BUF){
uartputc('-');
uartputc(c);
c = (c == '\r') ? '\n' : c;
if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){ // if input is \n, put it on the back, and process it
//write to buffer
input.buf[input.e++ % INPUT_BUF] = c;
consputc(c);
//back counter to 0
back_counter = 0;
//copy the command
for(int i=input.w, k=0; i < input.e-1; i++, k++){
buffer[k] = input.buf[i % INPUT_BUF];
}
buffer[(input.e-1-input.w) % INPUT_BUF] = '\0';
//add histories
addHistory(buffer);
//process
input.w = input.e;
input.pos = input.e;
wakeup(&input.r);
}else{
if(back_counter == 0){
input.buf[input.e++ % INPUT_BUF] = c;
input.pos ++;
// output direct
consputc(c);
}else{
//move back
for(int k=input.e; k >= input.pos; k--){
input.buf[(k + 1) % INPUT_BUF] = input.buf[k % INPUT_BUF];
}
//insert
input.buf[input.pos % INPUT_BUF] = c;
input.e++;
input.pos++;
//insert the char into CRT propoly position
vga_insert_char(c, back_counter);
}
}
}
break;
}
}
release(&input.lock);
}
int
consoleread(struct inode *ip, char *dst, int n)
{
uint target;
int c;
iunlock(ip);
target = n;
acquire(&input.lock);
while(n > 0){
while(input.r == input.w){
if(proc->killed){
release(&input.lock);
ilock(ip);
return -1;
}
sleep(&input.r, &input.lock);
}
c = input.buf[input.r++ % INPUT_BUF];
if(c == C('D')){ // EOF
if(n < target){
// Save ^D for next time, to make sure
// caller gets a 0-byte result.
input.r--;
}
break;
}
*dst++ = c;
--n;
if(c == '\n')
break;
}
release(&input.lock);
ilock(ip);
return target - n;
}
int
consolewrite(struct inode *ip, char *buf, int n)
{
int i;
iunlock(ip);
acquire(&cons.lock);
for(i = 0; i < n; i++)
consputc(buf[i] & 0xff);
release(&cons.lock);
ilock(ip);
return n;
}
void
consoleinit(void)
{
initlock(&cons.lock, "console");
initlock(&input.lock, "input");
devsw[CONSOLE].write = consolewrite;
devsw[CONSOLE].read = consoleread;
cons.locking = 1;
picenable(IRQ_KBD);
ioapicenable(IRQ_KBD, 0);
}