1 | #include "config.h" |
---|
2 | |
---|
3 | |
---|
4 | typedef struct command |
---|
5 | { |
---|
6 | char *text; |
---|
7 | struct command *child; |
---|
8 | int (*func)(char *line); |
---|
9 | }; |
---|
10 | |
---|
11 | command main_list[] = { |
---|
12 | "Key", NULL, Key_func, |
---|
13 | "Driver", Driver_commands,NULL, |
---|
14 | NULL, NULL, NULL, |
---|
15 | }; |
---|
16 | |
---|
17 | command Driver_commands[] = { |
---|
18 | "MtxOrb", NULL, MtxOrb_drv_init, |
---|
19 | "curses", NULL, curses_drv_init, |
---|
20 | "hd44780", NULL, hd44780_drv_init, |
---|
21 | // "X11", NULL, X11_drv_init, |
---|
22 | // "debug", NULL, debug_drv_init, |
---|
23 | "text", NULL, text_drv_init, |
---|
24 | }; |
---|
25 | |
---|
26 | |
---|
27 | |
---|
28 | // Interpret a line... |
---|
29 | int parse_line(char *line); |
---|
30 | // Interpret the rest of the line... |
---|
31 | int parse_rest(char *line, command *commands); |
---|
32 | |
---|
33 | |
---|
34 | ////////// Helper functions for line parsing ///////////////// |
---|
35 | // Pops off the first word of the string. |
---|
36 | char *shift(char *string); |
---|
37 | |
---|
38 | |
---|
39 | ////////////// Config Functions ///////////////////////////////// |
---|
40 | int Key_func(char *line) |
---|
41 | { |
---|
42 | // uh, interpret the command... |
---|
43 | return 0; |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | int parse_line(char *line, command *commands) |
---|
48 | { |
---|
49 | int i, j; |
---|
50 | int newtoken, inquote; |
---|
51 | char *str; |
---|
52 | // char *tok; |
---|
53 | int argc; |
---|
54 | char *argv[256]; |
---|
55 | char delimiters[] = " \0"; |
---|
56 | char leftquote[] = "\0\"'`([{\0"; |
---|
57 | char rightquote[] = "\0\"'`)]}\0"; |
---|
58 | char errmsg[256]; |
---|
59 | int invalid=0; |
---|
60 | int err=0; |
---|
61 | |
---|
62 | // And parse all its messages... |
---|
63 | //debug("parse: Getting messages...\n"); |
---|
64 | str = strdup(line); |
---|
65 | debug("parse: ...%s\n", str); |
---|
66 | // Now, split up the string... |
---|
67 | //len = strlen(str); |
---|
68 | argc=0; |
---|
69 | newtoken=1; |
---|
70 | inquote=0; |
---|
71 | for(i=0; str[i]; i++) |
---|
72 | { |
---|
73 | if(inquote) // Scan for the end of the quote |
---|
74 | { |
---|
75 | if(str[i] == rightquote[inquote]) |
---|
76 | { // Found the end of the quote |
---|
77 | inquote=0; |
---|
78 | str[i] = 0; |
---|
79 | newtoken=1; |
---|
80 | } |
---|
81 | } |
---|
82 | else // Normal operation; split at delimiters |
---|
83 | { |
---|
84 | for(j=1; leftquote[j]; j++) |
---|
85 | { |
---|
86 | // Found the beginning of a new quote... |
---|
87 | if(str[i] == leftquote[j]) |
---|
88 | { |
---|
89 | inquote = j; |
---|
90 | str[i] = 0; |
---|
91 | continue; |
---|
92 | } |
---|
93 | } |
---|
94 | for(j=0; delimiters[j]; j++) |
---|
95 | { |
---|
96 | // Break into a new string... |
---|
97 | if(str[i] == delimiters[j]) |
---|
98 | { |
---|
99 | str[i] = 0; |
---|
100 | newtoken = 1; |
---|
101 | continue; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | if(newtoken && str[i]) |
---|
106 | { |
---|
107 | newtoken=0; |
---|
108 | argv[argc] = str + i; |
---|
109 | argc++; |
---|
110 | } |
---|
111 | else |
---|
112 | { |
---|
113 | } |
---|
114 | } |
---|
115 | if(inquote) |
---|
116 | { |
---|
117 | sprintf(errmsg, "huh? Unterminated string: missing %c\n", |
---|
118 | rightquote[inquote]); |
---|
119 | sock_send_string(c->sock, errmsg); |
---|
120 | continue; |
---|
121 | } |
---|
122 | /* |
---|
123 | for(tok = strtok(str, delimiters); |
---|
124 | tok; |
---|
125 | tok=strtok(NULL, delimiters)) |
---|
126 | { |
---|
127 | argv[argc] = tok; |
---|
128 | argc++; |
---|
129 | } |
---|
130 | */ |
---|
131 | argv[argc] = NULL; |
---|
132 | if(argc < 1) continue; |
---|
133 | |
---|
134 | // Now find and call the appropriate function... |
---|
135 | // debug("parse: Finding function...\n"); |
---|
136 | invalid = 1; |
---|
137 | for(i=0; commands[i].keyword; i++) |
---|
138 | { |
---|
139 | // debug("(checking %s)\n", commands[i].keyword); |
---|
140 | if(0 == strcmp(argv[0], commands[i].keyword)) |
---|
141 | { |
---|
142 | // debug("(FOUND %s)\n", commands[i].keyword); |
---|
143 | invalid = commands[i].function(c, argc, argv); |
---|
144 | // debug("parse: Returned %i...\n", err); |
---|
145 | } |
---|
146 | } |
---|
147 | if(invalid) |
---|
148 | { |
---|
149 | // FIXME: Check for buffer overflows here... |
---|
150 | err = 1; |
---|
151 | //sprintf(errmsg, "huh? Invalid command \"%s\"\n", argv[0]); |
---|
152 | //sock_send_string(c->sock, errmsg); |
---|
153 | } |
---|
154 | |
---|
155 | free(str); // Don't want to forget this... :) |
---|
156 | |
---|
157 | return err; |
---|
158 | } |
---|