forked from checkra1n/PongoOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.c
More file actions
180 lines (171 loc) · 5.62 KB
/
Copy pathcommand.c
File metadata and controls
180 lines (171 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* pongoOS - https://checkra.in
*
* Copyright (C) 2019-2021 checkra1n team
*
* This file is part of pongoOS.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include <stdlib.h>
#include <pongo.h>
struct task* command_task;
char command_buffer[0x200];
int command_buffer_idx = 0;
struct command {
const char* name;
const char* desc;
void (*cb)(const char* cmd, char* args);
} commands[64];
char is_masking_autoboot;
static lock command_lock;
static int cmp_cmd(const void *a, const void *b)
{
const struct command *x = a, *y = b;
if(!x->name && !y->name) return 0;
if(!x->name) return 1;
if(!y->name) return -1;
return strcmp(x->name, y->name);
}
void command_unregister(const char* name) {
lock_take(&command_lock);
for (int i=0; i<64; i++) {
if (commands[i].name && strcmp(commands[i].name, name) == 0) {
commands[i].name = 0;
commands[i].desc = 0;
commands[i].cb = 0;
}
}
qsort(commands, 64, sizeof(struct command), &cmp_cmd);
lock_release(&command_lock);
}
void command_register(const char* name, const char* desc, void (*cb)(const char* cmd, char* args)) {
if (is_masking_autoboot && strcmp(name,"autoboot") == 0) return;
lock_take(&command_lock);
for (int i=0; i<64; i++) {
if (!commands[i].name || strcmp(commands[i].name, name) == 0) {
commands[i].name = name;
commands[i].desc = desc;
commands[i].cb = cb;
qsort(commands, 64, sizeof(struct command), &cmp_cmd);
lock_release(&command_lock);
return;
}
}
lock_release(&command_lock);
panic("too many commands");
}
char* command_tokenize(char* str, uint32_t strbufsz) {
char* bound = &str[strbufsz];
while (*str) {
if (str > bound) return NULL;
if (*str == ' ') {
*str++ = 0;
while (*str) {
if (str > bound) return NULL;
if (*str == ' ') {
str++;
} else
break;
}
if (str > bound) return NULL;
if (!*str) return "";
return str;
}
str++;
}
return "";
}
char is_executing_command;
uint32_t command_flags;
#define COMMAND_NOTFOUND 1
void command_execute(char* cmd) {
char* arguments = command_tokenize(cmd, 0x1ff);
if (arguments) {
lock_take(&command_lock);
for (int i=0; i<64; i++) {
if (commands[i].name && !strcmp(cmd, commands[i].name)) {
void (*cb)(const char* cmd, char* args) = commands[i].cb;
lock_release(&command_lock);
cb(command_buffer, arguments);
return;
}
}
lock_release(&command_lock);
}
if(cmd[0] != '\0')
{
iprintf("Bad command: %s\n", cmd);
}
if (*cmd)
command_flags |= COMMAND_NOTFOUND;
}
extern uint32_t uart_should_drop_rx;
char command_handler_ready = 0;
volatile uint8_t command_in_progress = 0;
struct event command_handler_iter;
static inline void put_serial_modifier(const char* str) {
while (*str) serial_putc(*str++);
}
void command_main() {
while (1) {
if (!uart_should_drop_rx) {
fflush(stdout);
putchar('\r');
if (command_flags & COMMAND_NOTFOUND) {
put_serial_modifier("\x1b[31m");
}
iprintf("pongoOS> ");
fflush(stdout);
if (command_flags & COMMAND_NOTFOUND) {
put_serial_modifier("\x1b[0m");
command_flags &= ~COMMAND_NOTFOUND;
}
}
fflush(stdout);
event_fire(&command_handler_iter);
command_handler_ready = 1;
command_in_progress = 0;
fgets(command_buffer,512,stdin);
command_in_progress = 1;
char* cmd_end = command_buffer + strlen(command_buffer);
while (cmd_end != command_buffer) {
cmd_end --;
if (cmd_end[0] == '\n' || cmd_end[0] == '\r')
cmd_end[0] = 0;
}
command_execute(command_buffer);
}
}
void help(const char * cmd, char* arg) {
lock_take(&command_lock);
for (int i=0; i<64; i++) {
if (commands[i].name) {
iprintf("%16s | %s\n", commands[i].name, commands[i].desc ? commands[i].desc : "no description");
}
}
lock_release(&command_lock);
}
void command_init() {
command_task = task_create("command", command_main);
command_task->flags |= TASK_RESTART_ON_EXIT;
command_task->flags &= ~TASK_CAN_EXIT;
command_register("help", "shows this help message", help);
}