talin2010 发表于 2013-1-26 15:45:50

[MIT6.828]LAB1输入输出TAB字符的若干问题及总结

LAB1给代码中的'\t'的处理很简陋,直接把'\t'替换为5个空格,这和我们平时习惯有很大不同,所以要改造一下。

1、在kern/console.c cga_putc()函数中,可以看到'\t'case分支,把这个分支替换为如下代码:
      case '\t':                do{                        cons_putc(' ');                }while(crt_pos%TAB_SIZE != 0);                break;<img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt="">
其中的TAB_SIZE我将其定义在kern/console.h,尺寸为8:
#define TAB_SIZE 8
这样重新编译,用输出一些TAB字符,其行为已正常。

2、发现在系统终端下输入TAB是被禁止的,但是我想把TAB键作为制表符输入,所以要修改输入的部分,经过定位,发现是在lib/readline.c readline()函数中处理的输入,于是把22行的:
} else if ( c >= ' ' && i < BUFLEN-1) {<img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt="">
修改为:
} else if ( ((c >= ' ')||(c == '\t')) && i < BUFLEN-1) {<img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt="">
保存后重新编译,可以正常输入TAB字符。

3、又发现输入TAB后的退格处理不正常,一个TAB应该一次性退格,但是目前的退格仅仅是退一个空格,这不是我们想要的功能,发现对退格的处理仍然是在kern/console.c cga_putc()函数中。
思考:cga_putc()要怎么才能知道退格的是一个空格还是一个制表符?
参考步骤1,我们都是输出的' ',在显示缓冲区里面是不能区分,这必然要求我们用一个缓冲来保存以前处理的字符串,而且TAB在不同位置下长度不固定,也这也要求我们必须要记录其输出长度。对于缓冲长度要取多少,肯定要大于等于lib/readline.c readline()函数中缓冲区的长度,至于上限,就是个见仁见智的问题。
cga_putc()代码修改如下:
//mod by davelv @ 2010-11-02//add the facility to display and clear '\t'static void cga_putc(int c){static int line_buf;//line buffer = 80*25,high_word:char's length,low_word:attribute &valuestatic int line_pos;//line buffer's current postionint i;//i as a temp var;// if no attribute given, then use black on whiteif (!(c & ~0xFF))c |= 0x0700;switch (c & 0xff) {case '\b':if ((crt_pos > 0)&&(line_pos > 0)) {//not only crt_pos but also line_posi = line_buf[--line_pos]>>16;//get char's lengthdo{crt_buf[--crt_pos] = (c & ~0xff) | ' ';//clear last char}while(((line_buf&0xff) == '\t')&&( --i));//loop when char '\t' hasn't been clear all;}break;case '\n':crt_pos += CRT_COLS;/* fallthru */case '\r':crt_pos -= (crt_pos % CRT_COLS);line_pos = 0;//when '\r' or '\n',clear bufbreak;case '\t':i = 0;//TAB length counterdo{crt_buf=(c & ~0xff) | ' ';//display one blanki++;//length++}while(crt_pos%TAB_SIZE != 0);line_buf = c|(i<<16) ;//save length&valuebreak;default:line_buf = c;//save new charcrt_buf = c;/* write the character */break;}// What is the purpose of this?if (crt_pos >= CRT_SIZE) {memmove(crt_buf, crt_buf + CRT_COLS, (CRT_SIZE - CRT_COLS) * sizeof(uint16_t));for (i = CRT_SIZE - CRT_COLS; i < CRT_SIZE; i++)crt_buf = 0x0700 | ' ';crt_pos -= CRT_COLS;}/* move that little blinky thing */outb(addr_6845, 14);outb(addr_6845 + 1, crt_pos >> 8);outb(addr_6845, 15);outb(addr_6845 + 1, crt_pos);}<img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt=""><img title="It's All Text!" style="cursor: pointer ! important; display: none ! important; width: 28px ! important; height: 14px ! important;" alt="">

保存,编译,运行,无论是输入TAB还是删除TAB均已变得正常。

4、新的问题,我暂不说个人看法,先供大家讨论和思考:
(1)据我们所知,TAB的尺寸是可以由软件定制的,但是我们这个LAB只有内核,所以直接写死到驱动里面,但哪些是应该由驱动做的,哪些是由用户程序自定义的,这是OS设计者必须划分清楚的,在驱动里去做TAB,还有我新加的行缓冲,这样是否合理,应该怎样改进或者拆分。
(2)我这里把驱动里面的行缓冲定义为一屏字符,但是如果只输出TAB的话,其实是八屏,但是一旦屏幕字符刷到上面去以后,就不能再回来。如何实现整屏缓冲,缓冲要做到哪里合适。

不知道新的LAB中会不会添加新的代码,如果添加新的代码正好把这部分做了的话,倒是可以参考下。
页: [1]
查看完整版本: [MIT6.828]LAB1输入输出TAB字符的若干问题及总结