Homemade Operating System Antz day11-Implementing shell (next) command response

Keywords: Linux github Makefile

I've standardized the style of system code, similar to subcontracting linux to differentiate the functions of each part.

Antz System Update Address

Linux Kernel Source Code Analysis Address

Github project address

In the previous task, we have completed simple graphics by directly operating the graphics driver.

Need to know the previous part:

Direct operation graphics card refer to day03

See day09 for a simple graphical implementation

Keyboard key interrupt response refer to day10

Makefile

Project catalogue

1. How to implement command caching

The keyboard response has been completed before, but this response is a big problem, for example, for two interruptions of a key. Later, I added both interrupts to the response judgment so that there would be no bug s mentioned on the previous 10 days.

* Recognition is accomplished in the replacement function. Amend as follows

char* replace_char(char s[40]){
    char *chr = "$" ;
    if((strcmp(s,"1E")==0)||(strcmp(s,"9E")==0)){
            chr = "a" ;
    }else if((strcmp(s,"30")==0)||(strcmp(s,"B0")==0)){
            chr = "b" ;
    }
    ... // ellipsis
    return chr ;

* Here are some key recognition modifications

    // Enter - > Return key to respond to cached command line feeds
    if((strcmp(s,"1C")==0)||(strcmp(s,"9C")==0)){
            action_command(binfo);  //Response commands
            write_x = 58 ;   //Here's the line change
            write_y += 19 ;
            putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "AntzOS>");
    } 
    // F1 - > Terminal refresh is similar to clear command
    else if((strcmp(s,"3B")==0)||(strcmp(s,"BB")==0)){  //Interruption of response on F1
            sprintf(command,"%s","");  // Command cache empty
            flag = 0 ;  // The key mode replies to the default, which looks at the 10th day. The essential purpose is to process two terminals with one key.
            new_pe(binfo);  // Refresh the current terminal directly by operating the display memory
            putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "AntzOS>");
    }
    //  Backspace - > truncation key
    else if(strcmp(s,"0E")==0){
            // Backward
            write_x -= 8 ;
            boxfill8(binfo->vram, binfo->scrnx , COL8_000000,  write_x,     write_y,     write_x+19, write_y+19);
            if(write_x<=58){
                write_x = 146 ;
                write_y -= 19 ;
            }
    } 

These are the current changes in key recognition.

Next, we need to add a function that not only displays the key on the screen after each key, but also caches it in the buffer, identifies it on the next return, and clears it.

Let's start with a simple demo

// The command array is the command buffer
char  command[100]  = "";

void add_command(char *s)  { 
    sprintf(command,"%s%s",command,s); 
}

void action_command(){
    // response
}

II. Realization

A command is an array of command caches that are saved by calling the add_command() command after each key.

sprintf() is a string formatting command. Its main function is to write formatted data into a string. Sprintf is a variable function. There is no limit to the number of characters written to buffers using sprintf, which makes it possible for buffers to overflow.

We use the sprintf function to add the s character directly after the command.

So the next time we press Enter, we just need to call action_command, identify what command is in it, and then respond appropriately.

Take a look at the complete command buffer implementation.

// Instruction caching, but due to interrupt response time, terminal input speed is very slow
char  command[100]  = "";
void add_command(char *s)  {
        
    if(strcmp(s," ")==0){
        sprintf(command,"%s%s",command,"");
    }else if(strcmp(s,"$")){
        //Ignore this error input
    }else {
        sprintf(command,"%s%s",command,s);  
    } 
}

void action_command(struct BOOTINFO *binfo){
        // action command responds to commands
        // ls command
        // data command
        if(strcmp(command,"data")==0){
            // get new data;
            write_y += 19 ;
            putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "AntzOS in 2018");
        }else if(strcmp(command,"cls")==0){
            flag = 0 ;
            new_pe(binfo);
        }else if(strcmp(command,"version")==0){
            write_y += 19 ;
            putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "Antz.version.1.1");
        }else if(strcmp(command,"help")==0){
            // Excessive help content, displayed in the graphical interface area
            
        }else if(sizeof(command)>=1){
                write_y += 19 ;
                putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "Not Found");
        }
        // Command Cache Clearance
        sprintf(command,"%s","");
}

Now use Makefile to generate the img image of this command support.

    make img

Open the mirror using a virtual machine, and the results are as follows

The main code of the terminal is as follows:


int write_x = 55 ;
int write_y = 57 ;

char* replace_char(char s[40]){
    char *chr = "$" ;
    if((strcmp(s,"1E")==0)||(strcmp(s,"9E")==0)){
            chr = "a" ;
    }else if((strcmp(s,"30")==0)||(strcmp(s,"B0")==0)){
            chr = "b" ;
    }else if((strcmp(s,"2E")==0)||(strcmp(s,"AE")==0)){
            chr = "c" ;
    }else if((strcmp(s,"20")==0)||(strcmp(s,"A0")==0)){
            chr = "d" ;
    }else if((strcmp(s,"12")==0)||(strcmp(s,"92")==0)){
            chr = "e" ;
    }else if((strcmp(s,"21")==0)||(strcmp(s,"A1")==0)){
            chr = "f" ;
    }else if((strcmp(s,"22")==0)||(strcmp(s,"A2")==0)){
            chr = "g" ;
    }else if((strcmp(s,"23")==0)||(strcmp(s,"A3")==0)){
            chr = "h" ;
    }else if((strcmp(s,"17")==0)||(strcmp(s,"97")==0)){
        chr = "i" ;
    }else if((strcmp(s,"24")==0)||(strcmp(s,"A4")==0)){
        chr = "j" ;
    }else if((strcmp(s,"25")==0)||(strcmp(s,"A5")==0)){
        chr = "k" ;
    }else if((strcmp(s,"26")==0)||(strcmp(s,"A6")==0)){
        chr = "l" ;
    }else if((strcmp(s,"32")==0)||(strcmp(s,"B2")==0)){
        chr = "m" ;
    }else if((strcmp(s,"31")==0)||(strcmp(s,"B1")==0)){
        chr = "n" ;
    }else if((strcmp(s,"18")==0)||(strcmp(s,"98")==0)){
        chr = "o" ;
    }else if((strcmp(s,"19")==0)||(strcmp(s,"99")==0)){
        chr = "p" ;
    }else if((strcmp(s,"10")==0)||(strcmp(s,"90")==0)){
        chr = "q" ;
    }else if((strcmp(s,"13")==0)||(strcmp(s,"93")==0)){
        chr = "r" ;
    }else if((strcmp(s,"1F")==0)||(strcmp(s,"9F")==0)){
        chr = "s" ;
    }else if((strcmp(s,"14")==0)||(strcmp(s,"94")==0)){
        chr = "t" ;
    }else if((strcmp(s,"16")==0)||(strcmp(s,"96")==0)){
        chr = "u" ;
    }else if((strcmp(s,"2F")==0)||(strcmp(s,"AF")==0)){
        chr = "v" ;
    }else if((strcmp(s,"11")==0)||(strcmp(s,"91")==0)){
        chr = "w" ;
    }else if((strcmp(s,"2D")==0)||(strcmp(s,"AD")==0)){
        chr = "x" ;
    }else if((strcmp(s,"15")==0)||(strcmp(s,"95")==0)){
        chr = "y" ;
    }else if((strcmp(s,"2C")==0)||(strcmp(s,"AC")==0)){
        chr = "z" ;
    }else if((strcmp(s,"39")==0)||(strcmp(s,"B9")==0)){
        chr = " " ;
    }
    return chr ;
}

int flag = 1 ;

// Instruction caching, but due to interrupt response time, terminal input speed is very slow
char  command[100]  = "";
void add_command(char *s)  {
/*
if(strcmp(s," ")==0){
    sprintf(command,"%s%s",command,"");
}else    if(strcmp(s,"$")){
                //Ignore this error input
        }else {
    }
*/
        sprintf(command,"%s%s",command,s);

}

void action_command(struct BOOTINFO *binfo){
        // action command
        // ls
        // data
        if(strcmp(command,"data")==0){
            // get new data;
            write_y += 19 ;
            putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "AntzOS in 2018");
        }else if(strcmp(command,"cls")==0){
            flag = 0 ;
            new_pe(binfo);
        }else if(strcmp(command,"version")==0){
            write_y += 19 ;
            putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "Antz.version.1.1");
        }else if(strcmp(command,"help")==0){
            // Excessive help content, displayed in the graphical interface area
        }else if(sizeof(command)>=1){
                write_y += 19 ;
                putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "Not Found");
        }
        // Command Cache Clearance
        sprintf(command,"%s","");
}

void key(struct BOOTINFO *binfo,char s[40]){
    if((strcmp(s,"1C")==0)||(strcmp(s,"9C")==0)){
            action_command(binfo);
            write_x = 58 ;
            write_y += 19 ;
            putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "AntzOS>");
    }else if((strcmp(s,"3B")==0)||(strcmp(s,"BB")==0)){  //Interruption of response on F1
            sprintf(command,"%s","");
            flag = 0 ;
            new_pe(binfo);
            putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "AntzOS>");
    }else if(strcmp(s,"0E")==0){
            // Backward
            write_x -= 8 ;
            boxfill8(binfo->vram, binfo->scrnx , COL8_000000,  write_x,     write_y,     write_x+19, write_y+19);
            if(write_x<=58){
                write_x = 146 ;
                write_y -= 19 ;
            }
    }else {
            //putfonts8_asc(binfo->vram, binfo->scrnx,  write_x,  write_y, COL8_FFFFFF, s);
            putfonts8_asc(binfo->vram, binfo->scrnx,  write_x,  write_y, COL8_FFFFFF, replace_char(s));
            add_command(replace_char(s));
            write_x += 8 ;
            // Add Response Zone
            //Eliminate
            //boxfill8(binfo->vram, binfo->scrnx, COL8_008400 , 300    ,240    ,310    ,250);
            //Print character Only use debug
            //putfonts8_asc(binfo->vram, binfo->scrnx,  300,  240 ,COL8_000000, s) ;
    }
    if(write_x>148){
        write_x = 58 ;
        write_y += 19 ;
        //putfonts8_asc(binfo->vram, binfo->scrnx, 4, 57, COL8_FFFFFF, "AntzOS>");
    }
    if(write_y>180){
         new_pe(binfo);
        putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "AntzOS>");
    }

}

void main(void)
{
    struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
    char s[40], mcursor[256], keybuf[32], mousebuf[128];
    int mx, my, i;

    init_gdtidt();
    init_pic();
    io_sti(); /* PIC Initialization has been completed*/

    fifo8_init(&keyfifo, 32, keybuf);
    fifo8_init(&mousefifo, 128, mousebuf);
    io_out8(PIC0_IMR, 0xf9); /* Open PIC1 and Keyboard Interruption (11111001) */
    io_out8(PIC1_IMR, 0xef); /* Open mouse interrupt (11101111) */

    init_keyboard();

    init_palette();
    init_screen8(binfo->vram, binfo->scrnx, binfo->scrny);

    putfonts8_asc(binfo->vram, binfo->scrnx,  0,  0, COL8_FFFFFF, "Terminal-Antz");
    putfonts8_asc(binfo->vram, binfo->scrnx,  0,  0, COL8_000000, "Terminal-Antz");
    putfonts8_asc(binfo->vram, binfo->scrnx,  107,  0, COL8_000000, "|-|o|x|");
    putfonts8_asc(binfo->vram, binfo->scrnx, 4, 19, COL8_FFFFFF, "AntzOS> SayHello()");
    putfonts8_asc(binfo->vram, binfo->scrnx, 4, 38, COL8_FFFFFF, "Hello My AntzOs.");
    putfonts8_asc(binfo->vram, binfo->scrnx, 4, 57, COL8_FFFFFF, "AntzOS>_");

 
    for (;;) {
        io_cli();
        if (fifo8_status(&keyfifo) + fifo8_status(&mousefifo) == 0) {
            io_stihlt();
        } else {
            if (fifo8_status(&keyfifo) != 0) {

                i = fifo8_get(&keyfifo);
                io_sti();
                sprintf(s, "%02X", i);
                if (flag){
                    key(binfo,s);
                }
                if(flag==1){
                    flag = 0 ;
                }else {
                    flag = 1 ;
                }
            }  
        }
    }
}

void new_pe(struct BOOTINFO *binfo){
    write_x = 58 ;
    write_y = 19 ;
    // The right side is not saved.
    init_screen8(binfo->vram, binfo->scrnx, binfo->scrny);
    putfonts8_asc(binfo->vram, binfo->scrnx,  0,  0, COL8_FFFFFF, "Terminal-Antz");
    putfonts8_asc(binfo->vram, binfo->scrnx,  0,  0, COL8_000000, "Terminal-Antz");
    putfonts8_asc(binfo->vram, binfo->scrnx,  107,  0, COL8_000000, "|-|o|x|");
    // This output is retained here and handed over to the caller himself
    //    putfonts8_asc(binfo->vram, binfo->scrnx, 4, write_y, COL8_FFFFFF, "AntzOS>");
}

Posted by jmantra on Sun, 03 Feb 2019 11:51:16 -0800