Skip to content

Commit aed28e5

Browse files
committed
improve the shell's logic flow
1 parent d9bf5aa commit aed28e5

4 files changed

Lines changed: 148 additions & 148 deletions

File tree

include/keyboard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "types.h"
55

6-
typedef void (*keyboard_callback_t)(void);
6+
typedef void (*keyboard_callback_t)(int);
77

88
#define KEY_ARROW_UP 0x80
99
#define KEY_ARROW_DOWN 0x81

src/drivers/keyboard.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,27 @@ static registers_t* keyboard_irq(registers_t* regs) {
8484
}
8585
if (extended_down) {
8686
extended_down = 0;
87+
int key_to_pass = 0;
8788
if (scancode == 0x48) {
88-
buffer_push((char)KEY_ARROW_UP);
89+
key_to_pass = KEY_ARROW_UP;
8990
} else if (scancode == 0x50) {
90-
buffer_push((char)KEY_ARROW_DOWN);
91+
key_to_pass = KEY_ARROW_DOWN;
9192
} else if (scancode == 0x4B) {
92-
buffer_push((char)KEY_ARROW_LEFT);
93+
key_to_pass = KEY_ARROW_LEFT;
9394
} else if (scancode == 0x4D) {
94-
buffer_push((char)KEY_ARROW_RIGHT);
95+
key_to_pass = KEY_ARROW_RIGHT;
9596
} else if (scancode == 0x53) {
96-
buffer_push((char)KEY_DELETE);
97+
key_to_pass = KEY_DELETE;
9798
} else if (scancode == 0x49) {
98-
buffer_push((char)KEY_PAGE_UP);
99+
key_to_pass = KEY_PAGE_UP;
99100
} else if (scancode == 0x51) {
100-
buffer_push((char)KEY_PAGE_DOWN);
101+
key_to_pass = KEY_PAGE_DOWN;
101102
}
102-
if (key_callback) {
103-
key_callback();
103+
if (key_to_pass) {
104+
buffer_push((char)key_to_pass);
105+
if (key_callback) {
106+
key_callback(key_to_pass);
107+
}
104108
}
105109
return regs;
106110
}
@@ -131,9 +135,9 @@ static registers_t* keyboard_irq(registers_t* regs) {
131135
}
132136
if (c) {
133137
buffer_push(c);
134-
}
135-
if (key_callback) {
136-
key_callback();
138+
if (key_callback) {
139+
key_callback((int)c);
140+
}
137141
}
138142
return regs;
139143
}

src/mem/vm_space.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ int vm_space_clone_cow(process_t* parent, process_t* child) {
3131
child->region_count = 0;
3232
for (uint32_t i = 0; i < parent->region_count; ++i) {
3333
vm_region_t region = parent->regions[i];
34-
uint32_t size = region.end - region.start;
35-
(void)size;
3634
if (region.flags & VM_SHARED) {
3735
if (!vm_map_shared(child, region.shared_id, region.start)) {
3836
return 0;

src/shell/shell.c

Lines changed: 131 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,155 +1438,153 @@ void shell_init(void) {
14381438

14391439
void shell_on_input(int key) {
14401440
int ch = key;
1441-
{
1442-
if (ch == 12) {
1443-
fb_clear(0);
1444-
fb_console_init(0xFFFFFF, 0);
1445-
line_length = 0;
1446-
cursor_pos = 0;
1447-
shell_prompt();
1448-
continue;
1449-
}
1450-
if (ch == 21) {
1451-
line_length = 0;
1452-
cursor_pos = 0;
1453-
shell_redraw_line();
1454-
continue;
1441+
if (ch == 12) {
1442+
fb_clear(0);
1443+
fb_console_init(0xFFFFFF, 0);
1444+
line_length = 0;
1445+
cursor_pos = 0;
1446+
shell_prompt();
1447+
return;
1448+
}
1449+
if (ch == 21) {
1450+
line_length = 0;
1451+
cursor_pos = 0;
1452+
shell_redraw_line();
1453+
return;
1454+
}
1455+
if (ch == 1) {
1456+
while (cursor_pos > 0) {
1457+
cursor_pos--;
1458+
shell_putc('\b');
14551459
}
1456-
if (ch == 1) {
1457-
while (cursor_pos > 0) {
1458-
cursor_pos--;
1459-
shell_putc('\b');
1460-
}
1461-
continue;
1460+
return;
1461+
}
1462+
if (ch == 5) {
1463+
while (cursor_pos < line_length) {
1464+
shell_putc(line_buffer[cursor_pos]);
1465+
cursor_pos++;
14621466
}
1463-
if (ch == 5) {
1464-
while (cursor_pos < line_length) {
1465-
shell_putc(line_buffer[cursor_pos]);
1466-
cursor_pos++;
1467-
}
1468-
continue;
1467+
return;
1468+
}
1469+
if (ch == 2) {
1470+
if (cursor_pos > 0) {
1471+
cursor_pos--;
1472+
shell_putc('\b');
14691473
}
1470-
if (ch == 2) {
1471-
if (cursor_pos > 0) {
1472-
cursor_pos--;
1473-
shell_putc('\b');
1474-
}
1475-
continue;
1474+
return;
1475+
}
1476+
if (ch == 6) {
1477+
if (cursor_pos < line_length) {
1478+
shell_putc(line_buffer[cursor_pos]);
1479+
cursor_pos++;
14761480
}
1477-
if (ch == 6) {
1478-
if (cursor_pos < line_length) {
1479-
shell_putc(line_buffer[cursor_pos]);
1480-
cursor_pos++;
1481+
return;
1482+
}
1483+
if (ch == 4) {
1484+
if (cursor_pos < line_length) {
1485+
for (uint32_t i = cursor_pos; i + 1 < line_length; ++i) {
1486+
line_buffer[i] = line_buffer[i + 1];
14811487
}
1482-
continue;
1488+
line_length--;
1489+
shell_redraw_line();
14831490
}
1484-
if (ch == 4) {
1485-
if (cursor_pos < line_length) {
1486-
for (uint32_t i = cursor_pos; i + 1 < line_length; ++i) {
1487-
line_buffer[i] = line_buffer[i + 1];
1488-
}
1489-
line_length--;
1490-
shell_redraw_line();
1491-
}
1492-
continue;
1491+
return;
1492+
}
1493+
if (ch == KEY_ARROW_UP || ch == 16) {
1494+
if (history_count > 0 && history_index > 0) {
1495+
history_index--;
1496+
shell_set_line(history_at(history_index));
14931497
}
1494-
if (ch == KEY_ARROW_UP || ch == 16) {
1495-
if (history_count > 0 && history_index > 0) {
1496-
history_index--;
1497-
shell_set_line(history_at(history_index));
1498-
}
1499-
continue;
1498+
return;
1499+
}
1500+
if (ch == KEY_ARROW_DOWN || ch == 14) {
1501+
if (history_index + 1 < history_count) {
1502+
history_index++;
1503+
shell_set_line(history_at(history_index));
1504+
} else if (history_index < history_count) {
1505+
history_index = history_count;
1506+
line_length = 0;
1507+
cursor_pos = 0;
1508+
shell_redraw_line();
15001509
}
1501-
if (ch == KEY_ARROW_DOWN || ch == 14) {
1502-
if (history_index + 1 < history_count) {
1503-
history_index++;
1504-
shell_set_line(history_at(history_index));
1505-
} else if (history_index < history_count) {
1506-
history_index = history_count;
1507-
line_length = 0;
1508-
cursor_pos = 0;
1509-
shell_redraw_line();
1510-
}
1511-
continue;
1510+
return;
1511+
}
1512+
if (ch == KEY_ARROW_LEFT) {
1513+
if (cursor_pos > 0) {
1514+
cursor_pos--;
1515+
shell_putc('\b');
15121516
}
1513-
if (ch == KEY_ARROW_LEFT) {
1514-
if (cursor_pos > 0) {
1515-
cursor_pos--;
1516-
shell_putc('\b');
1517-
}
1518-
continue;
1517+
return;
1518+
}
1519+
if (ch == KEY_ARROW_RIGHT) {
1520+
if (cursor_pos < line_length) {
1521+
shell_putc(line_buffer[cursor_pos]);
1522+
cursor_pos++;
15191523
}
1520-
if (ch == KEY_ARROW_RIGHT) {
1521-
if (cursor_pos < line_length) {
1522-
shell_putc(line_buffer[cursor_pos]);
1523-
cursor_pos++;
1524+
return;
1525+
}
1526+
if (ch == KEY_DELETE) {
1527+
if (cursor_pos < line_length) {
1528+
for (uint32_t i = cursor_pos; i + 1 < line_length; ++i) {
1529+
line_buffer[i] = line_buffer[i + 1];
15241530
}
1525-
continue;
1531+
line_length--;
1532+
shell_redraw_line();
15261533
}
1527-
if (ch == KEY_DELETE) {
1528-
if (cursor_pos < line_length) {
1529-
for (uint32_t i = cursor_pos; i + 1 < line_length; ++i) {
1530-
line_buffer[i] = line_buffer[i + 1];
1531-
}
1532-
line_length--;
1533-
shell_redraw_line();
1534+
return;
1535+
}
1536+
if (ch == KEY_PAGE_UP) {
1537+
fb_console_scroll(5);
1538+
return;
1539+
}
1540+
if (ch == KEY_PAGE_DOWN) {
1541+
fb_console_scroll(-5);
1542+
return;
1543+
}
1544+
if (ch == '\n') {
1545+
shell_putc('\n');
1546+
if (line_length > 0) {
1547+
uint32_t slot = history_head;
1548+
for (uint32_t i = 0; i < line_length && i + 1 < SHELL_LINE_SIZE; ++i) {
1549+
history[slot][i] = line_buffer[i];
15341550
}
1535-
continue;
1536-
}
1537-
if (ch == KEY_PAGE_UP) {
1538-
fb_console_scroll(5);
1539-
continue;
1540-
}
1541-
if (ch == KEY_PAGE_DOWN) {
1542-
fb_console_scroll(-5);
1543-
continue;
1544-
}
1545-
if (ch == '\n') {
1546-
shell_putc('\n');
1547-
if (line_length > 0) {
1548-
uint32_t slot = history_head;
1549-
for (uint32_t i = 0; i < line_length && i + 1 < SHELL_LINE_SIZE; ++i) {
1550-
history[slot][i] = line_buffer[i];
1551-
}
1552-
history[slot][line_length] = 0;
1553-
history_head = (history_head + 1) % SHELL_HISTORY;
1554-
if (history_count < SHELL_HISTORY) {
1555-
history_count++;
1556-
}
1557-
history_index = history_count;
1551+
history[slot][line_length] = 0;
1552+
history_head = (history_head + 1) % SHELL_HISTORY;
1553+
if (history_count < SHELL_HISTORY) {
1554+
history_count++;
15581555
}
1559-
line_buffer[line_length] = 0;
1560-
shell_execute_line();
1561-
line_length = 0;
1562-
cursor_pos = 0;
1563-
shell_prompt();
1564-
continue;
1565-
}
1566-
if (ch == '\t') {
1567-
shell_autocomplete();
1568-
continue;
1556+
history_index = history_count;
15691557
}
1570-
if (ch == '\b') {
1571-
if (cursor_pos > 0) {
1572-
for (uint32_t i = cursor_pos - 1; i + 1 < line_length; ++i) {
1573-
line_buffer[i] = line_buffer[i + 1];
1574-
}
1575-
line_length--;
1576-
cursor_pos--;
1577-
shell_redraw_line();
1558+
line_buffer[line_length] = 0;
1559+
shell_execute_line();
1560+
line_length = 0;
1561+
cursor_pos = 0;
1562+
shell_prompt();
1563+
return;
1564+
}
1565+
if (ch == '\t') {
1566+
shell_autocomplete();
1567+
return;
1568+
}
1569+
if (ch == '\b') {
1570+
if (cursor_pos > 0) {
1571+
for (uint32_t i = cursor_pos - 1; i + 1 < line_length; ++i) {
1572+
line_buffer[i] = line_buffer[i + 1];
15781573
}
1579-
continue;
1580-
}
1581-
if (line_length + 1 >= SHELL_LINE_SIZE) {
1582-
continue;
1583-
}
1584-
for (uint32_t i = line_length; i > cursor_pos; --i) {
1585-
line_buffer[i] = line_buffer[i - 1];
1574+
line_length--;
1575+
cursor_pos--;
1576+
shell_redraw_line();
15861577
}
1587-
line_buffer[cursor_pos] = (char)ch;
1588-
line_length++;
1589-
cursor_pos++;
1590-
shell_redraw_line();
1578+
return;
1579+
}
1580+
if (line_length + 1 >= SHELL_LINE_SIZE) {
1581+
return;
15911582
}
1583+
for (uint32_t i = line_length; i > cursor_pos; --i) {
1584+
line_buffer[i] = line_buffer[i - 1];
1585+
}
1586+
line_buffer[cursor_pos] = (char)ch;
1587+
line_length++;
1588+
cursor_pos++;
1589+
shell_redraw_line();
15921590
}

0 commit comments

Comments
 (0)