Skip to content

Commit

Permalink
Merge pull request #3 from 3epi/revert-2-q4
Browse files Browse the repository at this point in the history
Revert "Update console.c"
  • Loading branch information
3epi authored Oct 20, 2024
2 parents 6a971a6 + f1cb28f commit 0b3ea15
Showing 1 changed file with 2 additions and 203 deletions.
205 changes: 2 additions & 203 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 0b3ea15

Please sign in to comment.