e20482084913f890d43507f51f1fda7fe3806abd
[rpikern] / src / main.c
1 #include "config.h"
2
3 #include <string.h>
4 #include <stdint.h>
5 #include <ctype.h>
6 #include "asm.h"
7 #include "serial.h"
8
9 void dbgled(int x);
10 void exit(int x);
11
12 static void cmdrun(char *cmd);
13
14 int main(void)
15 {
16         int lastnl = 0;
17         static char cmdbuf[256];
18         static int cmdend;
19
20         init_serial(115200);
21         ser_printstr("starting rpikern\n");
22
23         for(;;) {
24                 int c = ser_getchar();
25
26                 switch(c) {
27                 case '\r':
28                 case '\n':
29                         if(!lastnl) {
30                                 ser_printstr("\r\n");
31                                 cmdbuf[cmdend] = 0;
32                                 cmdend = 0;
33                                 cmdrun(cmdbuf);
34                         }
35                         lastnl = 1;
36                         break;
37
38                 case -1:
39                         lastnl = 0;
40                         ser_printstr("error!\n");
41                         break;
42
43                 default:
44                         lastnl = 0;
45                         ser_putchar(c);
46                         if(cmdend < sizeof cmdbuf) {
47                                 cmdbuf[cmdend++] = c;
48                         }
49                 }
50         }
51
52         return 0;
53 }
54
55 void panic(void)
56 {
57         ser_printstr("PANIC!\n");
58         exit(0);
59 }
60
61 static void cmdrun(char *cmd)
62 {
63         char *ptr, *args;
64
65         while(*cmd && isspace(*cmd)) cmd++;
66         ptr = cmd;
67         while(*ptr && !isspace(*ptr)) ptr++;
68         *ptr = 0;
69         args = ptr + 1;
70
71         if(strcmp(cmd, "help") == 0) {
72                 ser_printstr("help not implemented yet\n");
73         } else if(strcmp(cmd, "ver") == 0) {
74                 ser_printstr("rpikern version 0.0\n");
75         } else {
76                 ser_printstr("Unknown command: ");
77                 ser_printstr(cmd);
78                 ser_printstr("\n");
79         }
80 }