diff options
Diffstat (limited to 'testing/toxic/prompt.c')
-rw-r--r-- | testing/toxic/prompt.c | 94 |
1 files changed, 57 insertions, 37 deletions
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index f973afd7..20f6b480 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * Toxic -- Tox Curses Client | 2 | * Toxic -- Tox Curses Client |
3 | */ | 3 | */ |
4 | 4 | ||
5 | #include <stdlib.h> | 5 | #include <stdlib.h> |
6 | #include <string.h> | 6 | #include <string.h> |
@@ -41,7 +41,17 @@ unsigned char * hex_string_to_bin(char hex_string[]) | |||
41 | static char prompt_buf[256] = {0}; | 41 | static char prompt_buf[256] = {0}; |
42 | static int prompt_buf_pos=0; | 42 | static int prompt_buf_pos=0; |
43 | 43 | ||
44 | static void execute(ToxWindow* self, char* cmd) { | 44 | static void execute(ToxWindow* self, char* u_cmd) { |
45 | int i; | ||
46 | int newlines = 0; | ||
47 | char cmd[256] = {0}; | ||
48 | for(i = 0; i < strlen(prompt_buf); i++) | ||
49 | { | ||
50 | if (u_cmd[i] == '\n') | ||
51 | ++newlines; | ||
52 | else | ||
53 | cmd[i - newlines] = u_cmd[i]; | ||
54 | } | ||
45 | 55 | ||
46 | if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) { | 56 | if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) { |
47 | endwin(); | 57 | endwin(); |
@@ -91,7 +101,7 @@ static void execute(ToxWindow* self, char* cmd) { | |||
91 | dht.ip.i = resolved_address; | 101 | dht.ip.i = resolved_address; |
92 | unsigned char *binary_string = hex_string_to_bin(key); | 102 | unsigned char *binary_string = hex_string_to_bin(key); |
93 | DHT_bootstrap(dht, binary_string); | 103 | DHT_bootstrap(dht, binary_string); |
94 | free(binary_string); | 104 | free(binary_string); |
95 | } | 105 | } |
96 | else if(!strncmp(cmd, "add ", strlen("add "))) { | 106 | else if(!strncmp(cmd, "add ", strlen("add "))) { |
97 | uint8_t id_bin[32]; | 107 | uint8_t id_bin[32]; |
@@ -137,7 +147,7 @@ static void execute(ToxWindow* self, char* cmd) { | |||
137 | 147 | ||
138 | num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1); | 148 | num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1); |
139 | switch (num) { | 149 | switch (num) { |
140 | case -1: | 150 | case -1: |
141 | wprintw(self->window, "Message is too long.\n"); | 151 | wprintw(self->window, "Message is too long.\n"); |
142 | break; | 152 | break; |
143 | case -2: | 153 | case -2: |
@@ -151,18 +161,19 @@ static void execute(ToxWindow* self, char* cmd) { | |||
151 | break; | 161 | break; |
152 | case -5: | 162 | case -5: |
153 | wprintw(self->window, "Undefined error when adding friend.\n"); | 163 | wprintw(self->window, "Undefined error when adding friend.\n"); |
154 | break; | 164 | break; |
155 | default: | 165 | default: |
156 | wprintw(self->window, "Friend added as %d.\n", num); | 166 | wprintw(self->window, "Friend added as %d.\n", num); |
157 | on_friendadded(num); | 167 | on_friendadded(num); |
158 | break; | 168 | break; |
159 | } | 169 | } |
160 | } | 170 | } |
161 | else if (!strcmp(cmd, "clear")) { | 171 | else if(!strcmp(cmd, "clear")) { |
162 | wclear(self->window); | 172 | wclear(self->window); |
163 | } | 173 | } |
164 | else if(!strcmp(cmd, "help")) { | 174 | else if(!strcmp(cmd, "help")) { |
165 | print_usage(self); | 175 | wclear(self->window); |
176 | print_usage(self); | ||
166 | } | 177 | } |
167 | else if(!strncmp(cmd, "status ", strlen("status "))) { | 178 | else if(!strncmp(cmd, "status ", strlen("status "))) { |
168 | char* msg; | 179 | char* msg; |
@@ -174,7 +185,7 @@ static void execute(ToxWindow* self, char* cmd) { | |||
174 | } | 185 | } |
175 | msg++; | 186 | msg++; |
176 | 187 | ||
177 | m_set_userstatus((uint8_t*) msg, strlen(msg)+1); | 188 | m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1); |
178 | wprintw(self->window, "Status set to: %s\n", msg); | 189 | wprintw(self->window, "Status set to: %s\n", msg); |
179 | } | 190 | } |
180 | else if(!strncmp(cmd, "nick ", strlen("nick "))) { | 191 | else if(!strncmp(cmd, "nick ", strlen("nick "))) { |
@@ -196,7 +207,7 @@ static void execute(ToxWindow* self, char* cmd) { | |||
196 | 207 | ||
197 | for(i=0; i<32; i++) { | 208 | for(i=0; i<32; i++) { |
198 | char xx[3]; | 209 | char xx[3]; |
199 | snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); | 210 | snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); |
200 | strcat(id, xx); | 211 | strcat(id, xx); |
201 | } | 212 | } |
202 | 213 | ||
@@ -264,8 +275,16 @@ static void execute(ToxWindow* self, char* cmd) { | |||
264 | static void prompt_onKey(ToxWindow* self, int key) { | 275 | static void prompt_onKey(ToxWindow* self, int key) { |
265 | // PRINTABLE characters: Add to line. | 276 | // PRINTABLE characters: Add to line. |
266 | if(isprint(key)) { | 277 | if(isprint(key)) { |
267 | if(prompt_buf_pos == (sizeof(prompt_buf) - 1)) { | 278 | if (prompt_buf_pos == (sizeof(prompt_buf) - 1)){ |
268 | return; | 279 | wprintw(self->window, "\nToo Long.\n"); |
280 | prompt_buf_pos = 0; | ||
281 | prompt_buf[0] = 0; | ||
282 | } | ||
283 | else if(!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS) && (prompt_buf_pos % (COLS - 3) == 0)) { | ||
284 | prompt_buf[prompt_buf_pos++] = '\n'; | ||
285 | } | ||
286 | else if(!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS) && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) { | ||
287 | prompt_buf[prompt_buf_pos++] = '\n'; | ||
269 | } | 288 | } |
270 | prompt_buf[prompt_buf_pos++] = key; | 289 | prompt_buf[prompt_buf_pos++] = key; |
271 | prompt_buf[prompt_buf_pos] = 0; | 290 | prompt_buf[prompt_buf_pos] = 0; |
@@ -288,20 +307,22 @@ static void prompt_onKey(ToxWindow* self, int key) { | |||
288 | } | 307 | } |
289 | 308 | ||
290 | static void prompt_onDraw(ToxWindow* self) { | 309 | static void prompt_onDraw(ToxWindow* self) { |
291 | curs_set(1); | 310 | curs_set(1); |
292 | int x, y; | 311 | int x, y; |
293 | 312 | getyx(self->window, y, x); | |
294 | getyx(self->window, y, x); | 313 | (void) x; |
295 | (void) x; | 314 | int i; |
296 | 315 | for (i = 0; i < (strlen(prompt_buf)); i++) | |
297 | wattron(self->window, COLOR_PAIR(1)); | 316 | { |
298 | mvwprintw(self->window, y, 0, "# "); | 317 | if ((prompt_buf[i] == '\n') && (y != 0)) |
299 | wattroff(self->window, COLOR_PAIR(1)); | 318 | --y; |
300 | 319 | } | |
301 | mvwprintw(self->window, y, 2, "%s", prompt_buf); | 320 | wattron(self->window, COLOR_PAIR(1)); |
302 | wclrtoeol(self->window); | 321 | mvwprintw(self->window, y, 0, "# "); |
303 | 322 | wattroff(self->window, COLOR_PAIR(1)); | |
304 | wrefresh(self->window); | 323 | mvwprintw(self->window, y, 2, "%s", prompt_buf); |
324 | wclrtoeol(self->window); | ||
325 | wrefresh(self->window); | ||
305 | } | 326 | } |
306 | 327 | ||
307 | static void print_usage(ToxWindow* self) { | 328 | static void print_usage(ToxWindow* self) { |
@@ -309,16 +330,15 @@ static void print_usage(ToxWindow* self) { | |||
309 | wprintw(self->window, "Commands:\n"); | 330 | wprintw(self->window, "Commands:\n"); |
310 | wattroff(self->window, A_BOLD); | 331 | wattroff(self->window, A_BOLD); |
311 | 332 | ||
312 | wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); | 333 | wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); |
313 | wprintw(self->window, " add <id> <message> : Add friend\n"); | 334 | wprintw(self->window, " add <id> <message> : Add friend\n"); |
314 | wprintw(self->window, " status <message> : Set your status\n"); | 335 | wprintw(self->window, " status <message> : Set your status\n"); |
315 | wprintw(self->window, " nick <nickname> : Set your nickname\n"); | 336 | wprintw(self->window, " nick <nickname> : Set your nickname\n"); |
316 | wprintw(self->window, " accept <number> : Accept friend request\n"); | 337 | wprintw(self->window, " accept <number> : Accept friend request\n"); |
317 | wprintw(self->window, " myid : Print your ID\n"); | 338 | wprintw(self->window, " myid : Print your ID\n"); |
318 | wprintw(self->window, " clear : Clear this window\n"); | 339 | wprintw(self->window, " quit/exit : Exit program\n"); |
319 | wprintw(self->window, " quit/exit : Exit program\n"); | 340 | wprintw(self->window, " help : Print this message again\n"); |
320 | wprintw(self->window, " help : Print this message again\n"); | 341 | wprintw(self->window, " clear : Clear this window\n"); |
321 | |||
322 | 342 | ||
323 | wattron(self->window, A_BOLD); | 343 | wattron(self->window, A_BOLD); |
324 | wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); | 344 | wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); |