From dc4aa089db541ef49fc62fbd539469185b846a53 Mon Sep 17 00:00:00 2001 From: sepehr Date: Sat, 19 Oct 2024 16:08:05 +0330 Subject: [PATCH 01/16] left arrow key works --- console.c | 82 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/console.c b/console.c index a280d2ba60..52d7b48807 100644 --- a/console.c +++ b/console.c @@ -15,6 +15,8 @@ #include "proc.h" #include "x86.h" +#define LEFT_ARROW 0xE4 +#define RIGHT_ARROW 0xE5 static void consputc(int); static int panicked = 0; @@ -128,8 +130,10 @@ panic(char *s) #define CRTPORT 0x3d4 static ushort *crt = (ushort*)P2V(0xb8000); // CGA memory + +int back_counter = 0; static void -cgaputc(int c) +cgaputc(int c, int flag) { int pos; @@ -139,12 +143,21 @@ cgaputc(int c) outb(CRTPORT, 15); 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 + switch(c) { + case '\n': + pos += 80 - pos%80; + break; + case BACKSPACE: + if(pos > 0) --pos; + break; + case LEFT_ARROW: + if(pos > 0) --pos; + break; + case RIGHT_ARROW: + break; + default: + crt[pos++] = (c&0xff) | 0x0700; // black on white + } if(pos < 0 || pos > 25*80) panic("pos under/overflow"); @@ -159,9 +172,21 @@ cgaputc(int c) outb(CRTPORT+1, pos>>8); outb(CRTPORT, 15); outb(CRTPORT+1, pos); - crt[pos] = ' ' | 0x0700; + + if (c != LEFT_ARROW && c != RIGHT_ARROW && flag != 1) crt[pos] = ' ' | 0x0700; } + +#define INPUT_BUF 128 +struct { + char buf[INPUT_BUF]; + uint r; // Read index + uint w; // Write index + uint e; // Edit index + uint rightmost; +} input; + + void consputc(int c) { @@ -171,20 +196,27 @@ consputc(int c) ; } - if(c == BACKSPACE){ - uartputc('\b'); uartputc(' '); uartputc('\b'); - } else - uartputc(c); - cgaputc(c); + switch (c) { + case BACKSPACE: + // uartputc prints to Linux's terminal + uartputc('\b'); uartputc(' '); uartputc('\b'); // uart is writing to the linux shell + break; + case LEFT_ARROW: + uartputc('\b'); + break; + case RIGHT_ARROW: + if (input.e < input.rightmost) { + uartputc(input.buf[input.e % INPUT_BUF]); + cgaputc(input.buf[input.e % INPUT_BUF], 1); + input.e++; + } + break; + default: + uartputc(c); + } + if (c != RIGHT_ARROW) cgaputc(c, 0); } -#define INPUT_BUF 128 -struct { - char buf[INPUT_BUF]; - uint r; // Read index - uint w; // Write index - uint e; // Edit index -} input; #define C(x) ((x)-'@') // Control-x @@ -213,6 +245,16 @@ consoleintr(int (*getc)(void)) consputc(BACKSPACE); } break; + case LEFT_ARROW: + if (input.e != input.w) { + input.e--; + consputc(c); + } + break; + case RIGHT_ARROW: + consputc(RIGHT_ARROW); + break; + default: if(c != 0 && input.e-input.r < INPUT_BUF){ c = (c == '\r') ? '\n' : c; From a495cc385d6ac809508458abbad889a68665bf18 Mon Sep 17 00:00:00 2001 From: sepehr Date: Sat, 19 Oct 2024 17:17:08 +0330 Subject: [PATCH 02/16] right arrow fix --- console.c | 117 +++++++++++++++++++++++++----------------------------- 1 file changed, 55 insertions(+), 62 deletions(-) diff --git a/console.c b/console.c index 52d7b48807..2091b19fd3 100644 --- a/console.c +++ b/console.c @@ -15,8 +15,12 @@ #include "proc.h" #include "x86.h" -#define LEFT_ARROW 0xE4 -#define RIGHT_ARROW 0xE5 +#define KEY_LF 0xE4 +#define KEY_RT 0xE5 +int back_counter = 0; + + + static void consputc(int); static int panicked = 0; @@ -130,10 +134,8 @@ panic(char *s) #define CRTPORT 0x3d4 static ushort *crt = (ushort*)P2V(0xb8000); // CGA memory - -int back_counter = 0; static void -cgaputc(int c, int flag) +cgaputc(int c) { int pos; @@ -143,21 +145,36 @@ cgaputc(int c, int flag) outb(CRTPORT, 15); pos |= inb(CRTPORT+1); - switch(c) { - case '\n': - pos += 80 - pos%80; - break; - case BACKSPACE: - if(pos > 0) --pos; - break; - case LEFT_ARROW: - if(pos > 0) --pos; - break; - case RIGHT_ARROW: - break; - default: - crt[pos++] = (c&0xff) | 0x0700; // black on white + if(c == '\n') + pos += 80 - pos%80; + else if(c == KEY_RT){ + if (back_counter < 0){ + pos++; + back_counter++; + outb(CRTPORT+1, pos); + } + return; } + else if(c == KEY_LF){ + if(pos%80 - 2 > 0){ + --pos; + --back_counter; + outb(CRTPORT+1, pos); + } + return; + } + else if(c == BACKSPACE){ + if(pos > 0) --pos; + } else { + // Shift characters to the right to make space for the new character + int end_pos = 24 * 80 - 1; // The last position on the screen + for (int i = end_pos; i >= pos; i--) { + crt[i] = crt[i - 1]; // Shift characters one position to the right + } + + crt[pos] = (c & 0xff) | 0x0700; // Insert the new character + pos++; + } if(pos < 0 || pos > 25*80) panic("pos under/overflow"); @@ -172,21 +189,9 @@ cgaputc(int c, int flag) outb(CRTPORT+1, pos>>8); outb(CRTPORT, 15); outb(CRTPORT+1, pos); - - if (c != LEFT_ARROW && c != RIGHT_ARROW && flag != 1) crt[pos] = ' ' | 0x0700; + crt[pos] = ' ' | 0x0700; } - -#define INPUT_BUF 128 -struct { - char buf[INPUT_BUF]; - uint r; // Read index - uint w; // Write index - uint e; // Edit index - uint rightmost; -} input; - - void consputc(int c) { @@ -196,27 +201,20 @@ consputc(int c) ; } - switch (c) { - case BACKSPACE: - // uartputc prints to Linux's terminal - uartputc('\b'); uartputc(' '); uartputc('\b'); // uart is writing to the linux shell - break; - case LEFT_ARROW: - uartputc('\b'); - break; - case RIGHT_ARROW: - if (input.e < input.rightmost) { - uartputc(input.buf[input.e % INPUT_BUF]); - cgaputc(input.buf[input.e % INPUT_BUF], 1); - input.e++; - } - break; - default: - uartputc(c); - } - if (c != RIGHT_ARROW) cgaputc(c, 0); + if(c == BACKSPACE){ + uartputc('\b'); uartputc(' '); uartputc('\b'); + } else + uartputc(c); + cgaputc(c); } +#define INPUT_BUF 128 +struct { + char buf[INPUT_BUF]; + uint r; // Read index + uint w; // Write index + uint e; // Edit index +} input; #define C(x) ((x)-'@') // Control-x @@ -245,16 +243,12 @@ consoleintr(int (*getc)(void)) consputc(BACKSPACE); } break; - case LEFT_ARROW: - if (input.e != input.w) { - input.e--; - consputc(c); - } - break; - case RIGHT_ARROW: - consputc(RIGHT_ARROW); - break; - + case KEY_RT: + cgaputc(c); + break; + case KEY_LF: + cgaputc(c); + break; default: if(c != 0 && input.e-input.r < INPUT_BUF){ c = (c == '\r') ? '\n' : c; @@ -338,4 +332,3 @@ consoleinit(void) ioapicenable(IRQ_KBD, 0); } - From 35f186591f5163e1263d0ec8c414a09d29f862b6 Mon Sep 17 00:00:00 2001 From: Kasra Ghorbani Date: Sun, 20 Oct 2024 10:29:06 +0330 Subject: [PATCH 03/16] Code for exercise 3 and advancements on exercise 1 --- console.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 7 deletions(-) diff --git a/console.c b/console.c index a280d2ba60..fa3fe73b3a 100644 --- a/console.c +++ b/console.c @@ -15,6 +15,24 @@ #include "proc.h" #include "x86.h" +#define KEY_LEFT 0xE2 +#define KEY_RIGHT 0xE3 + +#define COPY_BUF_SIZE 128 // Define a reasonable size for the buffer + +char copy_buffer[COPY_BUF_SIZE]; +int copy_mode = 0; // 1 when in copy mode, 0 otherwise +int copylen = 0; // Tracks the number of copied characters + + +#define MAX_HISTORY 10 // Maximum number of commands to keep in history +#define MAX_CMD_LEN 128 // Maximum length of each command +char history[MAX_HISTORY][MAX_CMD_LEN]; // History buffer +int history_count = 0; + +// static int cursor_pos = 0; // Start with cursor at position 0 + + static void consputc(int); static int panicked = 0; @@ -187,23 +205,23 @@ struct { } input; #define C(x) ((x)-'@') // Control-x - void consoleintr(int (*getc)(void)) { int c, doprocdump = 0; + static int cursor_pos = 0; // Tracks the current cursor position in the buffer acquire(&cons.lock); - while((c = getc()) >= 0){ - switch(c){ + while((c = getc()) >= 0) { + switch(c) { case C('P'): // Process listing. - // procdump() locks cons.lock indirectly; invoke later doprocdump = 1; break; case C('U'): // Kill line. while(input.e != input.w && - input.buf[(input.e-1) % INPUT_BUF] != '\n'){ + input.buf[(input.e-1) % INPUT_BUF] != '\n') { input.e--; + cursor_pos = input.e; // Reset cursor position to end of buffer consputc(BACKSPACE); } break; @@ -213,11 +231,58 @@ consoleintr(int (*getc)(void)) consputc(BACKSPACE); } break; + + // Handle Ctrl + S (start copying) + case C('S'): + copy_mode = 1; // Enter copy mode + copylen = 0; // Reset the copy buffer + break; + + // Left arrow key handling + case 0xE2: // Left arrow key (assuming 0xE2 corresponds to left arrow) + if (cursor_pos > 0) { // Prevent moving past the beginning + cursor_pos--; + consputc('\b'); // Visually move the cursor left + } + break; + + // Right arrow key handling + case 0xE3: // Right arrow key (assuming 0xE3 corresponds to right arrow) + if (cursor_pos < input.e) { // Prevent moving past the end + cursor_pos++; + consputc(input.buf[cursor_pos - 1 % INPUT_BUF]); // Move cursor right visually + } + break; + + // Handle Ctrl + F (paste copied text) + case C('F'): + for(int i = 0; i < copylen; i++){ + if(input.e-input.r < INPUT_BUF){ // Ensure there's space in the input buffer + input.buf[input.e++ % INPUT_BUF] = copy_buffer[i]; + consputc(copy_buffer[i]); + } + } + break; + default: if(c != 0 && input.e-input.r < INPUT_BUF){ c = (c == '\r') ? '\n' : c; - input.buf[input.e++ % INPUT_BUF] = c; + + + if(copy_mode && copylen < COPY_BUF_SIZE){ + + copy_buffer[copylen++]=c; + } + for(int i = input.e; i > cursor_pos; i--) { + input.buf[i % INPUT_BUF] = input.buf[(i - 1) % INPUT_BUF]; + } + input.buf[cursor_pos % INPUT_BUF] = c; // Insert the new character at the cursor position + input.e++; // Increment end index + cursor_pos++; // Move cursor position to the right after insertion consputc(c); + + + if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){ input.w = input.e; wakeup(&input.r); @@ -227,11 +292,13 @@ consoleintr(int (*getc)(void)) } } release(&cons.lock); + if(doprocdump) { - procdump(); // now call procdump() wo. cons.lock held + procdump(); // Now call procdump() without cons.lock held } } + int consoleread(struct inode *ip, char *dst, int n) { From 12d251f9be5457a6233699ebb37494a0a9fe8561 Mon Sep 17 00:00:00 2001 From: sepehr Date: Sun, 20 Oct 2024 12:37:58 +0330 Subject: [PATCH 04/16] history header --- history.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 history.h diff --git a/history.h b/history.h new file mode 100644 index 0000000000..41e6367e83 --- /dev/null +++ b/history.h @@ -0,0 +1,35 @@ +// history.h +#ifndef HISTORY_H +#define HISTORY_H + +#define HISTORY_SIZE 10 +#define MAX_CMD_LENGTH 128 + +// Declare the history array and related variables/functions +char history[HISTORY_SIZE][MAX_CMD_LENGTH]; +int history_index = -1; // Initially, no history command has been selected +int history_count = 0; + +void my_strncpy(char *dest, const char *src, int n) { + while (n > 0 && *src != '\0') { + *dest++ = *src++; + n--; + } + // Null-terminate the destination string + while (n > 0) { + *dest++ = '\0'; // Fill the remaining space with null characters + n--; + } +} + +// Function to add a command to history +int add_to_history(char *cmd) { + if (cmd[0] == '\0') { + return -1; // Skip empty commands + } + my_strncpy(history[history_count % HISTORY_SIZE], cmd, MAX_CMD_LENGTH); + history_count++; + history_index = history_count; // Reset index for the next command input + return history_index; +} +#endif \ No newline at end of file From 1b681fecf40f9e4fa74610c505a36bef719deddd Mon Sep 17 00:00:00 2001 From: sepehr Date: Sun, 20 Oct 2024 12:38:50 +0330 Subject: [PATCH 05/16] history implementation --- console.c | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/console.c b/console.c index 703699a42b..cc89667aa6 100644 --- a/console.c +++ b/console.c @@ -159,22 +159,6 @@ cgaputc(int c) if(c == '\n') pos += 80 - pos%80; - else if(c == KEY_RT){ - if (back_counter < 0){ - pos++; - back_counter++; - outb(CRTPORT+1, pos); - } - return; - } - else if(c == KEY_LF){ - if(pos%80 - 2 > 0){ - --pos; - --back_counter; - outb(CRTPORT+1, pos); - } - return; - } else if(c == BACKSPACE){ if(pos > 0) --pos; } else { From f6f23e2be820dedb4c715f2fe2bc38cafc3d8ef7 Mon Sep 17 00:00:00 2001 From: Kasra Ghorbani Date: Sun, 20 Oct 2024 12:43:13 +0330 Subject: [PATCH 06/16] nothing changed --- console.c | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/console.c b/console.c index fa3fe73b3a..ed20375496 100644 --- a/console.c +++ b/console.c @@ -205,9 +205,7 @@ struct { } input; #define C(x) ((x)-'@') // Control-x -void -consoleintr(int (*getc)(void)) -{ +void consoleintr(int (*getc)(void)) { int c, doprocdump = 0; static int cursor_pos = 0; // Tracks the current cursor position in the buffer @@ -218,21 +216,20 @@ consoleintr(int (*getc)(void)) doprocdump = 1; break; case C('U'): // Kill line. - while(input.e != input.w && - input.buf[(input.e-1) % INPUT_BUF] != '\n') { + while(input.e != input.w && input.buf[(input.e-1) % INPUT_BUF] != '\n') { input.e--; cursor_pos = input.e; // Reset cursor position to end of buffer consputc(BACKSPACE); } break; case C('H'): case '\x7f': // Backspace - if(input.e != input.w){ + if(input.e != input.w) { input.e--; consputc(BACKSPACE); } break; - // Handle Ctrl + S (start copying) + // Handle Ctrl + S (start copying) case C('S'): copy_mode = 1; // Enter copy mode copylen = 0; // Reset the copy buffer @@ -256,8 +253,8 @@ consoleintr(int (*getc)(void)) // Handle Ctrl + F (paste copied text) case C('F'): - for(int i = 0; i < copylen; i++){ - if(input.e-input.r < INPUT_BUF){ // Ensure there's space in the input buffer + for(int i = 0; i < copylen; i++) { + if(input.e-input.r < INPUT_BUF) { // Ensure there's space in the input buffer input.buf[input.e++ % INPUT_BUF] = copy_buffer[i]; consputc(copy_buffer[i]); } @@ -265,25 +262,19 @@ consoleintr(int (*getc)(void)) break; default: - if(c != 0 && input.e-input.r < INPUT_BUF){ + if(c != 0 && input.e-input.r < INPUT_BUF) { c = (c == '\r') ? '\n' : c; - - - if(copy_mode && copylen < COPY_BUF_SIZE){ - - copy_buffer[copylen++]=c; - } + if(copy_mode && copylen < COPY_BUF_SIZE) { + copy_buffer[copylen++] = c; + } for(int i = input.e; i > cursor_pos; i--) { - input.buf[i % INPUT_BUF] = input.buf[(i - 1) % INPUT_BUF]; + input.buf[i % INPUT_BUF] = input.buf[(i - 1) % INPUT_BUF]; } input.buf[cursor_pos % INPUT_BUF] = c; // Insert the new character at the cursor position input.e++; // Increment end index cursor_pos++; // Move cursor position to the right after insertion consputc(c); - - - - if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){ + if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF) { input.w = input.e; wakeup(&input.r); } @@ -298,7 +289,6 @@ consoleintr(int (*getc)(void)) } } - int consoleread(struct inode *ip, char *dst, int n) { From 15be4f009f8ed7ffa43bbe2fff09fa0857a4d2e6 Mon Sep 17 00:00:00 2001 From: sepehr Date: Sun, 20 Oct 2024 12:59:12 +0330 Subject: [PATCH 07/16] q1 last --- console.c | 46 ++++++++++++++++++++++++++++++++ sh.c | 80 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 101 insertions(+), 25 deletions(-) diff --git a/console.c b/console.c index cc89667aa6..33a925a84f 100644 --- a/console.c +++ b/console.c @@ -14,8 +14,10 @@ #include "mmu.h" #include "proc.h" #include "x86.h" +#include "history.h" +<<<<<<< Updated upstream #define KEY_LEFT 0xE2 #define KEY_RIGHT 0xE3 @@ -30,6 +32,13 @@ int copylen = 0; // Tracks the number of copied characters #define MAX_CMD_LEN 128 // Maximum length of each command char history[MAX_HISTORY][MAX_CMD_LEN]; // History buffer int history_count = 0; +======= +#define KEY_Down 0xE2 +#define KEY_UP 0xE3 +#define KEY_LF 0xE4 +#define KEY_RT 0xE5 +int back_counter = 0; +>>>>>>> Stashed changes @@ -159,7 +168,38 @@ cgaputc(int c) if(c == '\n') pos += 80 - pos%80; +<<<<<<< Updated upstream else if(c == BACKSPACE){ +======= + else if(c == KEY_RT){ + if (back_counter < 0){ + pos++; + back_counter++; + outb(CRTPORT+1, pos); + } + return; + } + else if(c == KEY_LF){ + if(pos%80 - 2 > 0){ + --pos; + --back_counter; + outb(CRTPORT+1, pos); + } + return; + }else if (c == KEY_UP) { + if (history_index > 0) { + cprintf("\r%s", history[history_index % HISTORY_SIZE]); // Print the command from history + history_index--; + pos = strlen(history[history_index % HISTORY_SIZE]); // Set cursor to end of command + } + } else if (c == KEY_Down) { + if (history_index < history_count - 1) { + cprintf("\r%s", history[history_index % HISTORY_SIZE]); // Print the command from history + history_index++; + pos = strlen(history[history_index % HISTORY_SIZE]); // Set c + } + }else if(c == BACKSPACE){ +>>>>>>> Stashed changes if(pos > 0) --pos; } else { // Shift characters to the right to make space for the new character @@ -270,7 +310,13 @@ consoleintr(int (*getc)(void)) } } break; +<<<<<<< Updated upstream +======= + case KEY_UP: + cgaputc(c); + break; +>>>>>>> Stashed changes default: if(c != 0 && input.e-input.r < INPUT_BUF){ c = (c == '\r') ? '\n' : c; diff --git a/sh.c b/sh.c index 054bab92b3..882e9a4ed1 100644 --- a/sh.c +++ b/sh.c @@ -1,5 +1,5 @@ // Shell. - +#include "history.h" #include "types.h" #include "user.h" #include "fcntl.h" @@ -141,34 +141,64 @@ getcmd(char *buf, int nbuf) return 0; } -int -main(void) -{ - static char buf[100]; - int fd; +int main(void) { + static char buf[100]; + int fd; - // Ensure that three file descriptors are open. - while((fd = open("console", O_RDWR)) >= 0){ - if(fd >= 3){ - close(fd); - break; + // Ensure that three file descriptors are open. + while((fd = open("console", O_RDWR)) >= 0) { + if(fd >= 3) { + close(fd); + break; + } } - } - // Read and run input commands. - while(getcmd(buf, sizeof(buf)) >= 0){ - if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){ - // Chdir must be called by the parent, not the child. - buf[strlen(buf)-1] = 0; // chop \n - if(chdir(buf+3) < 0) - printf(2, "cannot cd %s\n", buf+3); - continue; + // Read and run input commands. + while(getcmd(buf, sizeof(buf)) >= 0) { + // Check for the history command + if (strcmp(buf, "history\n") == 0) { + // Print only the last command from history + if (history_count > 0) { + int last_index = (history_count - 1) % HISTORY_SIZE; // Get last command index + printf(1, "%s\n", history[last_index]); // Print the last command + + } + + // Wait for the user to press Enter again + char wait_buf[10]; // Buffer for waiting for Enter + while (1) { + // Get user input + if (getcmd(wait_buf, sizeof(wait_buf)) < 0) { + // If getcmd fails, break the loop + break; + } + if (wait_buf[0] == '\n') { // Check for Enter key + break; // Exit history command mode + } + } + continue; // Skip the rest of the loop for the history command + } + + // Handle 'cd' command + if (buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ') { + // Chdir must be called by the parent, not the child. + buf[strlen(buf) - 1] = 0; // chop \n + if (chdir(buf + 3) < 0) { + printf(2, "cannot cd %s\n", buf + 3); + } + add_to_history(buf); // Add the command to history + continue; + } + + add_to_history(buf); // Add the command to history + + // Fork a new process to run the command + if (fork1() == 0) { + runcmd(parsecmd(buf)); + } + wait(); // Wait for the child process to finish } - if(fork1() == 0) - runcmd(parsecmd(buf)); - wait(); - } - exit(); + exit(); // Exit the shell } void From a326142e4bfbfaefa5163a0e3327e651ec30057f Mon Sep 17 00:00:00 2001 From: sepehr Date: Sun, 20 Oct 2024 13:03:50 +0330 Subject: [PATCH 08/16] cosnole last --- console.c | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/console.c b/console.c index 33a925a84f..a99df3dc92 100644 --- a/console.c +++ b/console.c @@ -17,9 +17,10 @@ #include "history.h" -<<<<<<< Updated upstream -#define KEY_LEFT 0xE2 -#define KEY_RIGHT 0xE3 +#define KEY_Down 0xE2 +#define KEY_UP 0xE3 +#define KEY_LEFT 0xE4 +#define KEY_RIGHT 0xE5 #define COPY_BUF_SIZE 128 // Define a reasonable size for the buffer @@ -27,20 +28,7 @@ char copy_buffer[COPY_BUF_SIZE]; int copy_mode = 0; // 1 when in copy mode, 0 otherwise int copylen = 0; // Tracks the number of copied characters - -#define MAX_HISTORY 10 // Maximum number of commands to keep in history -#define MAX_CMD_LEN 128 // Maximum length of each command -char history[MAX_HISTORY][MAX_CMD_LEN]; // History buffer -int history_count = 0; -======= -#define KEY_Down 0xE2 -#define KEY_UP 0xE3 -#define KEY_LF 0xE4 -#define KEY_RT 0xE5 -int back_counter = 0; ->>>>>>> Stashed changes - - +int back_counter; static void consputc(int); @@ -168,10 +156,7 @@ cgaputc(int c) if(c == '\n') pos += 80 - pos%80; -<<<<<<< Updated upstream - else if(c == BACKSPACE){ -======= - else if(c == KEY_RT){ + else if(c == KEY_RIGHT){ if (back_counter < 0){ pos++; back_counter++; @@ -179,7 +164,7 @@ cgaputc(int c) } return; } - else if(c == KEY_LF){ + else if(c == KEY_LEFT){ if(pos%80 - 2 > 0){ --pos; --back_counter; @@ -199,7 +184,6 @@ cgaputc(int c) pos = strlen(history[history_index % HISTORY_SIZE]); // Set c } }else if(c == BACKSPACE){ ->>>>>>> Stashed changes if(pos > 0) --pos; } else { // Shift characters to the right to make space for the new character @@ -286,7 +270,7 @@ consoleintr(int (*getc)(void)) break; // Left arrow key handling - case 0xE2: // Left arrow key (assuming 0xE2 corresponds to left arrow) + case KEY_LEFT: // Left arrow key (assuming 0xE2 corresponds to left arrow) if (cursor_pos > 0) { // Prevent moving past the beginning cursor_pos--; consputc('\b'); // Visually move the cursor left @@ -294,7 +278,7 @@ consoleintr(int (*getc)(void)) break; // Right arrow key handling - case 0xE3: // Right arrow key (assuming 0xE3 corresponds to right arrow) + case KEY_RIGHT: // Right arrow key (assuming 0xE3 corresponds to right arrow) if (cursor_pos < input.e) { // Prevent moving past the end cursor_pos++; consputc(input.buf[cursor_pos - 1 % INPUT_BUF]); // Move cursor right visually @@ -310,13 +294,9 @@ consoleintr(int (*getc)(void)) } } break; -<<<<<<< Updated upstream - -======= case KEY_UP: cgaputc(c); break; ->>>>>>> Stashed changes default: if(c != 0 && input.e-input.r < INPUT_BUF){ c = (c == '\r') ? '\n' : c; From 84b0e021ab7277d298e80470676e02948050f73f Mon Sep 17 00:00:00 2001 From: sepehr Date: Sun, 20 Oct 2024 13:20:23 +0330 Subject: [PATCH 09/16] now q1 works with q2 --- console.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/console.c b/console.c index a99df3dc92..639d7ddd95 100644 --- a/console.c +++ b/console.c @@ -271,18 +271,20 @@ consoleintr(int (*getc)(void)) // Left arrow key handling case KEY_LEFT: // Left arrow key (assuming 0xE2 corresponds to left arrow) - if (cursor_pos > 0) { // Prevent moving past the beginning - cursor_pos--; - consputc('\b'); // Visually move the cursor left - } + // if (cursor_pos > 0) { // Prevent moving past the beginning + // cursor_pos--; + // consputc('\b'); // Visually move the cursor left + // } + cgaputc(c); break; // Right arrow key handling case KEY_RIGHT: // Right arrow key (assuming 0xE3 corresponds to right arrow) - if (cursor_pos < input.e) { // Prevent moving past the end - cursor_pos++; - consputc(input.buf[cursor_pos - 1 % INPUT_BUF]); // Move cursor right visually - } + // if (cursor_pos < input.e) { // Prevent moving past the end + // cursor_pos++; + // consputc(input.buf[cursor_pos - 1 % INPUT_BUF]); // Move cursor right visually + // } + cgaputc(c); break; // Handle Ctrl + F (paste copied text) From 11a929ad42651f3126fdd065189c6e38dea74f07 Mon Sep 17 00:00:00 2001 From: sepehr Date: Sun, 20 Oct 2024 13:20:36 +0330 Subject: [PATCH 10/16] minor change to history --- sh.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sh.c b/sh.c index 882e9a4ed1..17d455ede2 100644 --- a/sh.c +++ b/sh.c @@ -158,10 +158,13 @@ int main(void) { // Check for the history command if (strcmp(buf, "history\n") == 0) { // Print only the last command from history - if (history_count > 0) { - int last_index = (history_count - 1) % HISTORY_SIZE; // Get last command index - printf(1, "%s\n", history[last_index]); // Print the last command + // if (history_count > 0) { + // int last_index = (history_count - 1) % HISTORY_SIZE; // Get last command index + // printf(1, "%s\n", history[last_index]); // Print the last command + // } + for (int i=0; i < history_count ; i++){ + printf(1, "%s\n", history[i]); } // Wait for the user to press Enter again From 66287717249f55012f074bc559fcd18434f872b3 Mon Sep 17 00:00:00 2001 From: SepehrJML <123619185+SepehrJML@users.noreply.github.com> Date: Sun, 20 Oct 2024 18:04:48 +0330 Subject: [PATCH 11/16] Update console.c q4 --- console.c | 294 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 252 insertions(+), 42 deletions(-) diff --git a/console.c b/console.c index a280d2ba60..931f001897 100644 --- a/console.c +++ b/console.c @@ -14,6 +14,21 @@ #include "mmu.h" #include "proc.h" #include "x86.h" +#include "history.h" + + +#define KEY_Down 0xE2 +#define KEY_UP 0xE3 +#define KEY_LEFT 0xE4 +#define KEY_RIGHT 0xE5 + +#define COPY_BUF_SIZE 128 // Define a reasonable size for the buffer + +char copy_buffer[COPY_BUF_SIZE]; +int copy_mode = 0; // 1 when in copy mode, 0 otherwise +int copylen = 0; // Tracks the number of copied characters + +int back_counter; static void consputc(int); @@ -141,10 +156,45 @@ cgaputc(int c) if(c == '\n') pos += 80 - pos%80; - else if(c == BACKSPACE){ + else if(c == KEY_RIGHT){ + if (back_counter < 0){ + pos++; + back_counter++; + outb(CRTPORT+1, pos); + } + return; + } + else if(c == KEY_LEFT){ + if(pos%80 - 2 > 0){ + --pos; + --back_counter; + outb(CRTPORT+1, pos); + } + return; + }else if (c == KEY_UP) { + if (history_index > 0) { + cprintf("\r%s", history[history_index % HISTORY_SIZE]); // Print the command from history + history_index--; + pos = strlen(history[history_index % HISTORY_SIZE]); // Set cursor to end of command + } + } else if (c == KEY_Down) { + if (history_index < history_count - 1) { + cprintf("\r%s", history[history_index % HISTORY_SIZE]); // Print the command from history + history_index++; + pos = strlen(history[history_index % HISTORY_SIZE]); // Set c + } + }else if(c == BACKSPACE){ if(pos > 0) --pos; - } else - crt[pos++] = (c&0xff) | 0x0700; // black on white + } else { + // Shift characters to the right to make space for the new character + int end_pos = 24 * 80 - 1; // The last position on the screen + for (int i = end_pos; i >= pos; i--) { + crt[i] = crt[i - 1]; // Shift characters one position to the right + } + + crt[pos] = (c & 0xff) | 0x0700; // Insert the new character + pos++; + } if(pos < 0 || pos > 25*80) panic("pos under/overflow"); @@ -188,50 +238,210 @@ struct { #define C(x) ((x)-'@') // Control-x -void -consoleintr(int (*getc)(void)) -{ - int c, doprocdump = 0; +int is_operator(char c) { + return c == '+' || c == '-' || c == '*' || c == '/'; +} - acquire(&cons.lock); - while((c = getc()) >= 0){ - switch(c){ - case C('P'): // Process listing. - // procdump() locks cons.lock indirectly; invoke later - doprocdump = 1; - 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; - default: - if(c != 0 && input.e-input.r < INPUT_BUF){ - c = (c == '\r') ? '\n' : c; - input.buf[input.e++ % INPUT_BUF] = c; - consputc(c); - if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){ - input.w = input.e; - wakeup(&input.r); +int calculate_expression(char *expr, int len) { + int num1 = 0, num2 = 0; + char op = 0; // Initialize operator to zero + int i = 0; + + // Extract the first number + while (i < len && expr[i] >= '0' && expr[i] <= '9') { + num1 = num1 * 10 + (expr[i] - '0'); + i++; + } + + // Get the operator + if (i < len && is_operator(expr[i])) { + op = expr[i++]; + } else { + return -1; // Invalid expression if no operator is found + } + + // Extract the second number + while (i < len && expr[i] >= '0' && expr[i] <= '9') { + num2 = num2 * 10 + (expr[i] - '0'); + i++; + } + + // Validate the remaining characters + for (; i < len; i++) { + if (expr[i] != ' ' && !is_operator(expr[i]) && (expr[i] < '0' || expr[i] > '9')) { + return -1; // Invalid character in expression } - } - break; } - } - release(&cons.lock); - if(doprocdump) { - procdump(); // now call procdump() wo. cons.lock held - } + + // Perform the calculation based on the operator + switch (op) { + case '+': return num1 + num2; + case '-': return num1 - num2; + case '*': return num1 * num2; + case '/': return (num2 != 0) ? num1 / num2 : -1; // Avoid division by zero + default: return -1; // Invalid operator + } +} + +void itoa(int num, char *buf, int buf_size) { + int i = 0; + int is_negative = (num < 0); // Determine if the number is negative + + if (is_negative) { + num = -num; // Convert to positive for processing + } + + // Convert number to string + do { + if (i < buf_size - 1) { // Ensure we don't overflow the buffer + buf[i++] = (num % 10) + '0'; + } + num /= 10; + } while (num > 0); + + // Add negative sign if the number was negative + if (is_negative && i < buf_size - 1) { + buf[i++] = '-'; + } + + buf[i] = '\0'; // Null-terminate the string + + // Reverse the string to get the correct order + for (int j = 0; j < i / 2; j++) { + char temp = buf[j]; + buf[j] = buf[i - j - 1]; + buf[i - j - 1] = temp; + } } +void consoleintr(int (*getc)(void)) { + int c, doprocdump = 0; + static char expr_buf[32]; + static int expr_len = 0; + char result_str[16]; // Buffer for storing the result of calculations + static int cursor_pos = 0; // Tracks the current cursor position in the buffer + static char last_char = 0; // To keep track of the last character input + static int copy_mode = 0; // Flag for copy mode + static char copy_buffer[COPY_BUF_SIZE]; // Buffer for copied text + static int copylen = 0; // Length of copied text + + acquire(&cons.lock); + while((c = getc()) >= 0) { + switch(c) { + case C('P'): // Process listing. + doprocdump = 1; + break; + case C('U'): // Kill line. + while(input.e != input.w && input.buf[(input.e - 1) % INPUT_BUF] != '\n') { + input.e--; + cursor_pos = input.e; // Reset cursor position to end of buffer + consputc(BACKSPACE); + } + expr_len = 0; // Clear the expression buffer + last_char = 0; // Reset last character + break; + case C('H'): case '\x7f': // Backspace + if(input.e != input.w) { + input.e--; + cursor_pos--; // Move cursor position back + consputc(BACKSPACE); + } + if(expr_len > 0) expr_len--; // Manage backspace in the input buffer + last_char = 0; // Reset last character + break; + case C('S'): // Ctrl + S to start copying + copy_mode = 1; // Enter copy mode + copylen = 0; // Reset the copy buffer + break; + case C('F'): // Ctrl + F to paste copied text + for(int i = 0; i < copylen; i++) { + if(input.e - input.r < INPUT_BUF) { // Ensure there's space in the input buffer + input.buf[input.e++ % INPUT_BUF] = copy_buffer[i]; + consputc(copy_buffer[i]); + } + } + break; + // Left arrow key handling + case KEY_LEFT: // Left arrow key + cgaputc(c); + break; + // Right arrow key handling + case KEY_RIGHT: // Right arrow key + cgaputc(c); + break; + // Up arrow key handling (you can add functionality if needed) + case KEY_UP: + cgaputc(c); + break; + default: + if(c != 0 && input.e - input.r < INPUT_BUF) { + c = (c == '\r') ? '\n' : c; // Normalize carriage return to newline + + // If in copy mode, store the character in the copy buffer + if(copy_mode && copylen < COPY_BUF_SIZE) { + copy_buffer[copylen++] = c; + } + + // Insert the new character at the cursor position + for(int i = input.e; i > cursor_pos; i--) { + input.buf[i % INPUT_BUF] = input.buf[(i - 1) % INPUT_BUF]; + } + input.buf[cursor_pos % INPUT_BUF] = c; // Insert the character + input.e++; // Increment end index + cursor_pos++; // Move cursor position to the right after insertion + consputc(c); + + // Store valid characters in the calculation buffer + if(expr_len < sizeof(expr_buf) - 1) { + // Allow only digits, operators, and spaces + if ((c >= '0' && c <= '9') || is_operator(c) || c == ' ' || c == '=' || c == '?') { + expr_buf[expr_len++] = c; + expr_buf[expr_len] = 0; // Null-terminate the string + + // Check for the calculation trigger sequence "=?" + if(last_char == '=' && c == '?') { + int result = calculate_expression(expr_buf, expr_len - 2); // Remove '=' and '?' for calculation + if (result != -1) { + // Clear the expression + for (int i = 0; i < expr_len; i++) { + consputc(BACKSPACE); + input.e--; + } + + // Convert the result to string + itoa(result, result_str, sizeof(result_str)); + + // Display the result + for (int i = 0; result_str[i] != '\0'; i++) { + input.buf[input.e++ % INPUT_BUF] = result_str[i]; + consputc(result_str[i]); + } + } + expr_len = 0; // Reset the buffer + } + + // Update last character to the current one + last_char = c; + } + } + + if(c == '\n' || c == C('D') || input.e == input.r + INPUT_BUF) { + input.w = input.e; + wakeup(&input.r); + } + } + break; + } + } + release(&cons.lock); + + if(doprocdump) { + procdump(); // Now call procdump() without holding cons.lock + } +} + + + int consoleread(struct inode *ip, char *dst, int n) { From f1cb28f8c8777530a95c38b5dc3bbb8b91bc5394 Mon Sep 17 00:00:00 2001 From: Mohammad Sepehr Bazargan <65105604+3epi@users.noreply.github.com> Date: Sun, 20 Oct 2024 18:59:21 +0330 Subject: [PATCH 12/16] Revert "Update console.c" --- console.c | 205 +----------------------------------------------------- 1 file changed, 2 insertions(+), 203 deletions(-) diff --git a/console.c b/console.c index 37bb0afcca..639d7ddd95 100644 --- a/console.c +++ b/console.c @@ -237,40 +237,6 @@ struct { } input; #define C(x) ((x)-'@') // Control-x - - -int is_operator(char c) { - return c == '+' || c == '-' || c == '*' || c == '/'; -} - -int calculate_expression(char *expr, int len) { - int num1 = 0, num2 = 0; - char op = 0; // Initialize operator to zero - int i = 0; - - // Extract the first number - while (i < len && expr[i] >= '0' && expr[i] <= '9') { - num1 = num1 * 10 + (expr[i] - '0'); - i++; - } - - // Get the operator - if (i < len && is_operator(expr[i])) { - op = expr[i++]; - } else { - return -1; // Invalid expression if no operator is found - } - - // Extract the second number - while (i < len && expr[i] >= '0' && expr[i] <= '9') { - num2 = num2 * 10 + (expr[i] - '0'); - i++; - } - - // Validate the remaining characters - for (; i < len; i++) { - if (expr[i] != ' ' && !is_operator(expr[i]) && (expr[i] < '0' || expr[i] > '9')) { - return -1; // Invalid character in expression void consoleintr(int (*getc)(void)) { @@ -356,176 +322,9 @@ consoleintr(int (*getc)(void)) input.w = input.e; wakeup(&input.r); } + } + break; } - - // Perform the calculation based on the operator - switch (op) { - case '+': return num1 + num2; - case '-': return num1 - num2; - case '*': return num1 * num2; - case '/': return (num2 != 0) ? num1 / num2 : -1; // Avoid division by zero - default: return -1; // Invalid operator - } -} - -void itoa(int num, char *buf, int buf_size) { - int i = 0; - int is_negative = (num < 0); // Determine if the number is negative - - if (is_negative) { - num = -num; // Convert to positive for processing - } - - // Convert number to string - do { - if (i < buf_size - 1) { // Ensure we don't overflow the buffer - buf[i++] = (num % 10) + '0'; - } - num /= 10; - } while (num > 0); - - // Add negative sign if the number was negative - if (is_negative && i < buf_size - 1) { - buf[i++] = '-'; - } - - buf[i] = '\0'; // Null-terminate the string - - // Reverse the string to get the correct order - for (int j = 0; j < i / 2; j++) { - char temp = buf[j]; - buf[j] = buf[i - j - 1]; - buf[i - j - 1] = temp; - } -} - -void consoleintr(int (*getc)(void)) { - int c, doprocdump = 0; - static char expr_buf[32]; - static int expr_len = 0; - char result_str[16]; // Buffer for storing the result of calculations - static int cursor_pos = 0; // Tracks the current cursor position in the buffer - static char last_char = 0; // To keep track of the last character input - static int copy_mode = 0; // Flag for copy mode - static char copy_buffer[COPY_BUF_SIZE]; // Buffer for copied text - static int copylen = 0; // Length of copied text - - acquire(&cons.lock); - while((c = getc()) >= 0) { - switch(c) { - case C('P'): // Process listing. - doprocdump = 1; - break; - case C('U'): // Kill line. - while(input.e != input.w && input.buf[(input.e - 1) % INPUT_BUF] != '\n') { - input.e--; - cursor_pos = input.e; // Reset cursor position to end of buffer - consputc(BACKSPACE); - } - expr_len = 0; // Clear the expression buffer - last_char = 0; // Reset last character - break; - case C('H'): case '\x7f': // Backspace - if(input.e != input.w) { - input.e--; - cursor_pos--; // Move cursor position back - consputc(BACKSPACE); - } - if(expr_len > 0) expr_len--; // Manage backspace in the input buffer - last_char = 0; // Reset last character - break; - case C('S'): // Ctrl + S to start copying - copy_mode = 1; // Enter copy mode - copylen = 0; // Reset the copy buffer - break; - case C('F'): // Ctrl + F to paste copied text - for(int i = 0; i < copylen; i++) { - if(input.e - input.r < INPUT_BUF) { // Ensure there's space in the input buffer - input.buf[input.e++ % INPUT_BUF] = copy_buffer[i]; - consputc(copy_buffer[i]); - } - } - break; - // Left arrow key handling - case KEY_LEFT: // Left arrow key - cgaputc(c); - break; - // Right arrow key handling - case KEY_RIGHT: // Right arrow key - cgaputc(c); - break; - // Up arrow key handling (you can add functionality if needed) - case KEY_UP: - cgaputc(c); - break; - default: - if(c != 0 && input.e - input.r < INPUT_BUF) { - c = (c == '\r') ? '\n' : c; // Normalize carriage return to newline - - // If in copy mode, store the character in the copy buffer - if(copy_mode && copylen < COPY_BUF_SIZE) { - copy_buffer[copylen++] = c; - } - - // Insert the new character at the cursor position - for(int i = input.e; i > cursor_pos; i--) { - input.buf[i % INPUT_BUF] = input.buf[(i - 1) % INPUT_BUF]; - } - input.buf[cursor_pos % INPUT_BUF] = c; // Insert the character - input.e++; // Increment end index - cursor_pos++; // Move cursor position to the right after insertion - consputc(c); - - // Store valid characters in the calculation buffer - if(expr_len < sizeof(expr_buf) - 1) { - // Allow only digits, operators, and spaces - if ((c >= '0' && c <= '9') || is_operator(c) || c == ' ' || c == '=' || c == '?') { - expr_buf[expr_len++] = c; - expr_buf[expr_len] = 0; // Null-terminate the string - - // Check for the calculation trigger sequence "=?" - if(last_char == '=' && c == '?') { - int result = calculate_expression(expr_buf, expr_len - 2); // Remove '=' and '?' for calculation - if (result != -1) { - // Clear the expression - for (int i = 0; i < expr_len; i++) { - consputc(BACKSPACE); - input.e--; - } - - // Convert the result to string - itoa(result, result_str, sizeof(result_str)); - - // Display the result - for (int i = 0; result_str[i] != '\0'; i++) { - input.buf[input.e++ % INPUT_BUF] = result_str[i]; - consputc(result_str[i]); - } - } - expr_len = 0; // Reset the buffer - } - - // Update last character to the current one - last_char = c; - } - } - - if(c == '\n' || c == C('D') || input.e == input.r + INPUT_BUF) { - input.w = input.e; - wakeup(&input.r); - } - } - break; - } - } - release(&cons.lock); - - if(doprocdump) { - procdump(); // Now call procdump() without holding cons.lock - } -} - - } release(&cons.lock); From 3971591bc6395951470031c29e45035235208c3f Mon Sep 17 00:00:00 2001 From: SepehrJML <123619185+SepehrJML@users.noreply.github.com> Date: Sun, 20 Oct 2024 19:04:29 +0330 Subject: [PATCH 13/16] Update console.c fixed!? --- console.c | 94 +------------------------------------------------------ 1 file changed, 1 insertion(+), 93 deletions(-) diff --git a/console.c b/console.c index 37bb0afcca..931f001897 100644 --- a/console.c +++ b/console.c @@ -238,7 +238,6 @@ struct { #define C(x) ((x)-'@') // Control-x - int is_operator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } @@ -271,90 +270,6 @@ int calculate_expression(char *expr, int len) { for (; i < len; i++) { if (expr[i] != ' ' && !is_operator(expr[i]) && (expr[i] < '0' || expr[i] > '9')) { return -1; // Invalid character in expression -void -consoleintr(int (*getc)(void)) -{ - int c, doprocdump = 0; - static int cursor_pos = 0; // Tracks the current cursor position in the buffer - - acquire(&cons.lock); - while((c = getc()) >= 0) { - switch(c) { - case C('P'): // Process listing. - doprocdump = 1; - break; - case C('U'): // Kill line. - while(input.e != input.w && - input.buf[(input.e-1) % INPUT_BUF] != '\n') { - input.e--; - cursor_pos = input.e; // Reset cursor position to end of buffer - consputc(BACKSPACE); - } - break; - case C('H'): case '\x7f': // Backspace - if(input.e != input.w){ - input.e--; - consputc(BACKSPACE); - } - break; - // Handle Ctrl + S (start copying) - case C('S'): - copy_mode = 1; // Enter copy mode - copylen = 0; // Reset the copy buffer - break; - - // Left arrow key handling - case KEY_LEFT: // Left arrow key (assuming 0xE2 corresponds to left arrow) - // if (cursor_pos > 0) { // Prevent moving past the beginning - // cursor_pos--; - // consputc('\b'); // Visually move the cursor left - // } - cgaputc(c); - break; - - // Right arrow key handling - case KEY_RIGHT: // Right arrow key (assuming 0xE3 corresponds to right arrow) - // if (cursor_pos < input.e) { // Prevent moving past the end - // cursor_pos++; - // consputc(input.buf[cursor_pos - 1 % INPUT_BUF]); // Move cursor right visually - // } - cgaputc(c); - break; - - // Handle Ctrl + F (paste copied text) - case C('F'): - for(int i = 0; i < copylen; i++){ - if(input.e-input.r < INPUT_BUF){ // Ensure there's space in the input buffer - input.buf[input.e++ % INPUT_BUF] = copy_buffer[i]; - consputc(copy_buffer[i]); - } - } - break; - case KEY_UP: - cgaputc(c); - break; - default: - if(c != 0 && input.e-input.r < INPUT_BUF){ - c = (c == '\r') ? '\n' : c; - - - if(copy_mode && copylen < COPY_BUF_SIZE){ - - copy_buffer[copylen++]=c; - } - for(int i = input.e; i > cursor_pos; i--) { - input.buf[i % INPUT_BUF] = input.buf[(i - 1) % INPUT_BUF]; - } - input.buf[cursor_pos % INPUT_BUF] = c; // Insert the new character at the cursor position - input.e++; // Increment end index - cursor_pos++; // Move cursor position to the right after insertion - consputc(c); - - - - if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){ - input.w = input.e; - wakeup(&input.r); } } @@ -526,14 +441,6 @@ void consoleintr(int (*getc)(void)) { } - } - release(&cons.lock); - - if(doprocdump) { - procdump(); // Now call procdump() without cons.lock held - } -} - int consoleread(struct inode *ip, char *dst, int n) @@ -599,3 +506,4 @@ consoleinit(void) ioapicenable(IRQ_KBD, 0); } + From 39e90397f65c84101e6ee25027b709a59d6b2af4 Mon Sep 17 00:00:00 2001 From: Kasra Ghorbani Date: Sun, 20 Oct 2024 19:35:36 +0330 Subject: [PATCH 14/16] caesar cipher --- Makefile | 4 +++- decode.c | 40 ++++++++++++++++++++++++++++++++++++++++ encode.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 decode.c create mode 100644 encode.c diff --git a/Makefile b/Makefile index 09d790cf63..ed37215edd 100644 --- a/Makefile +++ b/Makefile @@ -181,6 +181,8 @@ UPROGS=\ _usertests\ _wc\ _zombie\ + _encode\ + _decode\ fs.img: mkfs README $(UPROGS) ./mkfs fs.img README $(UPROGS) @@ -250,7 +252,7 @@ qemu-nox-gdb: fs.img xv6.img .gdbinit EXTRA=\ mkfs.c ulib.c user.h cat.c echo.c forktest.c grep.c kill.c\ ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\ - printf.c umalloc.c\ + printf.c umalloc.c encode.c decode.c\ README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\ .gdbinit.tmpl gdbutil\ diff --git a/decode.c b/decode.c new file mode 100644 index 0000000000..a8dd11a2ac --- /dev/null +++ b/decode.c @@ -0,0 +1,40 @@ +#include "types.h" +#include "user.h" +#include "fcntl.h" + +int main(int argc, char* argv[]) { + if (argc <= 1) { + printf(1, "There is no inputing to be decoded!\n"); + exit(); + } + + unlink("result.txt"); + int fd = open("result.txt", O_CREATE | O_WRONLY); + if (fd < 0) { + printf(2, "decode: cannot create result.txt\n"); + exit(); + } + + int key = 14; + + for (int i = 1; argv[i] != 0; i++) { + char *input = argv[i]; + char c; + while ((c = *input) != 0) { + if (c >= 'a' && c <= 'z') { + c = (c - 'a' + key + 26) % 26 + 'a'; + } else if (c >= 'A' && c <= 'Z') { + c = (c - 'A' + key + 26) % 26 + 'A'; + } + *input = c; + input++; + } + + printf(fd, "%s ", argv[i]); + } + + write(fd, "\n", 1); + close(fd); + + exit(); +} diff --git a/encode.c b/encode.c new file mode 100644 index 0000000000..94e2d29e2f --- /dev/null +++ b/encode.c @@ -0,0 +1,40 @@ +#include "types.h" +#include "user.h" +#include "fcntl.h" + +int main(int argc, char* argv[]) { + if (argc <= 1) { + printf(1, "There is no inputing to be encoded!\n"); + exit(); + } + + unlink("result.txt"); + int fd = open("result.txt", O_CREATE | O_WRONLY); + if (fd < 0) { + printf(2, "encode: cannot create result.txt\n"); + exit(); + } + + int key = 14; + + for (int i = 1; argv[i] != 0; i++) { + char *input = argv[i]; + char c; + while ((c = *input) != 0) { + if (c >= 'a' && c <= 'z') { + c = (c - 'a' + key) % 26 + 'a'; + } else if (c >= 'A' && c <= 'Z') { + c = (c - 'A' + key) % 26 + 'A'; + } + *input = c; + input++; + } + + printf(fd, "%s ", argv[i]); + } + + write(fd, "\n", 1); + close(fd); + + exit(); +} From 0187bb7a3a6b07dbea214f8d8b0bdc612985032c Mon Sep 17 00:00:00 2001 From: SepehrJML <123619185+SepehrJML@users.noreply.github.com> Date: Sun, 20 Oct 2024 19:57:43 +0330 Subject: [PATCH 15/16] Update console.c q1 q3 q4 --- console.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/console.c b/console.c index 116b7daa74..931f001897 100644 --- a/console.c +++ b/console.c @@ -237,9 +237,11 @@ struct { } input; #define C(x) ((x)-'@') // Control-x + int is_operator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } + int calculate_expression(char *expr, int len) { int num1 = 0, num2 = 0; char op = 0; // Initialize operator to zero @@ -250,6 +252,7 @@ int calculate_expression(char *expr, int len) { num1 = num1 * 10 + (expr[i] - '0'); i++; } + // Get the operator if (i < len && is_operator(expr[i])) { op = expr[i++]; @@ -267,8 +270,18 @@ int calculate_expression(char *expr, int len) { for (; i < len; i++) { if (expr[i] != ' ' && !is_operator(expr[i]) && (expr[i] < '0' || expr[i] > '9')) { return -1; // Invalid character in expression - } + } + } + + // Perform the calculation based on the operator + switch (op) { + case '+': return num1 + num2; + case '-': return num1 - num2; + case '*': return num1 * num2; + case '/': return (num2 != 0) ? num1 / num2 : -1; // Avoid division by zero + default: return -1; // Invalid operator } +} void itoa(int num, char *buf, int buf_size) { int i = 0; @@ -428,13 +441,6 @@ void consoleintr(int (*getc)(void)) { } - } - release(&cons.lock); - - if(doprocdump) { - procdump(); // Now call procdump() without cons.lock held - } -} int consoleread(struct inode *ip, char *dst, int n) From 76413d3688d8b3c03c872f9d38554c71aa0280ed Mon Sep 17 00:00:00 2001 From: SepehrJML <123619185+SepehrJML@users.noreply.github.com> Date: Sun, 20 Oct 2024 20:47:52 +0330 Subject: [PATCH 16/16] last changes --- Makefile | 2 ++ decode.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ encode.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 decode.c create mode 100644 encode.c diff --git a/Makefile b/Makefile index 09d790cf63..c639b38313 100644 --- a/Makefile +++ b/Makefile @@ -181,6 +181,8 @@ UPROGS=\ _usertests\ _wc\ _zombie\ + _encode\ + _decode\ fs.img: mkfs README $(UPROGS) ./mkfs fs.img README $(UPROGS) diff --git a/decode.c b/decode.c new file mode 100644 index 0000000000..666604956f --- /dev/null +++ b/decode.c @@ -0,0 +1,62 @@ +#include "types.h" +#include "stat.h" +#include "user.h" +#include "fcntl.h" + + +int str_len(char* str) { + char* start = str; + while (*str != '\0') { + str++; + } + return str - start; +} + +char* decode(char* str) { + int key = 14; + char* ptr = str; + + while (*ptr != '\0') { + if (*ptr >= 'a' && *ptr <= 'z') { + *ptr = 'a' + (*ptr - 'a' - key + 26) % 26; + } else if (*ptr >= 'A' && *ptr <= 'Z') { + *ptr = 'A' + (*ptr - 'A' - key + 26) % 26; + } + ptr++; + } + return str; +} + +int main(int argc, char* argv[]) { + if (argc <= 1) { + printf(1, "No input to decode!\n"); + exit(); + } + + int fd = open("result.txt", O_WRONLY | O_CREATE); + if (fd < 0) { + printf(2, "Cannot open or create result.txt\n"); + exit(); + } + + + for (int i = 1; i < argc; i++) { + char* decoded_str = decode(argv[i]); + int len = str_len(decoded_str); + + if (write(fd, decoded_str, len) != len) { + printf(2, "Cannot write to result.txt\n"); + close(fd); + exit(); + } + + if (i < argc - 1) { + write(fd, " ", 1); + } else { + write(fd, "\n", 1); + } + } + + close(fd); + exit(); +} diff --git a/encode.c b/encode.c new file mode 100644 index 0000000000..63c04db416 --- /dev/null +++ b/encode.c @@ -0,0 +1,61 @@ +#include "types.h" +#include "stat.h" +#include "user.h" +#include "fcntl.h" + +int str_len(char* str) { + char* start = str; + while (*str != '\0') { + str++; + } + return str - start; +} + + +char* encode(char* str) { + int key = 14; + char* ptr = str; + + while (*ptr != '\0') { + if (*ptr >= 'a' && *ptr <= 'z') { + *ptr = 'a' + (*ptr - 'a' + key) % 26; + } else if (*ptr >= 'A' && *ptr <= 'Z') { + *ptr = 'A' + (*ptr - 'A' + key) % 26; + } + ptr++; + } + return str; +} + +int main(int argc, char* argv[]) { + if (argc <= 1) { + printf(1, "No input to encode!\n"); + exit(); + } + + int fd = open("result.txt", O_WRONLY | O_CREATE); + if (fd < 0) { + printf(2, "Cannot open or create result.txt\n"); + exit(); + } + + for (int i = 1; i < argc; i++) { + char* encoded_str = encode(argv[i]); + int len = str_len(encoded_str); + + if (write(fd, encoded_str, len) != len) { + printf(2, "Cannot write to result.txt\n"); + close(fd); + exit(); + } + + if (i < argc - 1) { + write(fd, " ", 1); + } else { + write(fd, "\n", 1); + } + } + + close(fd); + exit(); +}