| File: | frontends/monkey/dispatch.c |
| Warning: | line 88, column 11 Although the value stored to 'r' is used in the enclosing expression, the value is never actually read from 'r' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* | |||
| 2 | * Copyright 2011 Daniel Silverstone <dsilvers@digital-scurf.org> | |||
| 3 | * | |||
| 4 | * This file is part of NetSurf, http://www.netsurf-browser.org/ | |||
| 5 | * | |||
| 6 | * NetSurf is free software; you can redistribute it and/or modify | |||
| 7 | * it under the terms of the GNU General Public License as published by | |||
| 8 | * the Free Software Foundation; version 2 of the License. | |||
| 9 | * | |||
| 10 | * NetSurf is distributed in the hope that it will be useful, | |||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
| 13 | * GNU General Public License for more details. | |||
| 14 | * | |||
| 15 | * You should have received a copy of the GNU General Public License | |||
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 17 | */ | |||
| 18 | ||||
| 19 | #include <limits.h> | |||
| 20 | #include <stdio.h> | |||
| 21 | #include <string.h> | |||
| 22 | #include <stdlib.h> | |||
| 23 | ||||
| 24 | #include "utils/log.h" | |||
| 25 | #include "utils/utils.h" | |||
| 26 | #include "utils/ring.h" | |||
| 27 | ||||
| 28 | #include "monkey/dispatch.h" | |||
| 29 | ||||
| 30 | typedef struct cmdhandler { | |||
| 31 | struct cmdhandler *r_next, *r_prev; | |||
| 32 | char *cmd; | |||
| 33 | handle_command_fn fn; | |||
| 34 | } monkey_cmdhandler_t; | |||
| 35 | ||||
| 36 | static monkey_cmdhandler_t *handler_ring = NULL((void*)0); | |||
| 37 | ||||
| 38 | nserror | |||
| 39 | monkey_register_handler(const char *cmd, handle_command_fn fn) | |||
| 40 | { | |||
| 41 | monkey_cmdhandler_t *ret = calloc(1, sizeof(*ret)); | |||
| 42 | if (ret == NULL((void*)0)) { | |||
| 43 | NSLOG(netsurf, INFO, "Unable to allocate handler")do { if (NSLOG_LEVEL_INFO >= NSLOG_LEVEL_VERBOSE) { static nslog_entry_context_t _nslog_ctx = { &__nslog_category_netsurf , NSLOG_LEVEL_INFO, "frontends/monkey/dispatch.c", sizeof("frontends/monkey/dispatch.c" ) - 1, __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, 43 , }; nslog__log(&_nslog_ctx, "Unable to allocate handler" ); } } while(0); | |||
| 44 | return NSERROR_NOMEM; | |||
| 45 | } | |||
| 46 | ret->cmd = strdup(cmd); | |||
| 47 | ret->fn = fn; | |||
| 48 | RING_INSERT(handler_ring, ret)if (handler_ring) { ret->r_next = handler_ring; ret->r_prev = handler_ring->r_prev; handler_ring->r_prev = ret; ret ->r_prev->r_next = ret; } else handler_ring = ret->r_prev = ret->r_next = ret; | |||
| 49 | return NSERROR_OK; | |||
| 50 | } | |||
| 51 | ||||
| 52 | void | |||
| 53 | monkey_free_handlers(void) | |||
| 54 | { | |||
| 55 | while (handler_ring != NULL((void*)0)) { | |||
| 56 | monkey_cmdhandler_t *handler = handler_ring; | |||
| 57 | RING_REMOVE(handler_ring, handler)if (handler->r_next != handler ) { handler->r_next-> r_prev = handler->r_prev; handler->r_prev->r_next = handler ->r_next; if (handler_ring == handler) handler_ring = handler ->r_next; } else { handler_ring = 0; } handler->r_next = handler->r_prev = 0; | |||
| 58 | free(handler->cmd); | |||
| 59 | free(handler); | |||
| 60 | } | |||
| 61 | } | |||
| 62 | ||||
| 63 | void | |||
| 64 | monkey_process_command(void) | |||
| ||||
| 65 | { | |||
| 66 | char buffer[PATH_MAX4096]; | |||
| 67 | int argc = 0; | |||
| 68 | char **argv = NULL((void*)0); | |||
| 69 | char *p, *r = NULL((void*)0); | |||
| 70 | handle_command_fn fn = NULL((void*)0); | |||
| 71 | char **nargv; | |||
| 72 | ||||
| 73 | if (fgets(buffer, PATH_MAX4096, stdinstdin) == NULL((void*)0)) { | |||
| 74 | /* end of input or read error so issue QUIT */ | |||
| 75 | sprintf(buffer, "QUIT\n"); | |||
| 76 | } | |||
| 77 | ||||
| 78 | /* remove newline */ | |||
| 79 | buffer[strlen(buffer) - 1] = '\0'; | |||
| 80 | ||||
| 81 | argv = malloc(sizeof *argv); | |||
| 82 | if (argv == NULL((void*)0)) { | |||
| 83 | return; | |||
| 84 | } | |||
| 85 | argc = 1; | |||
| 86 | *argv = buffer; | |||
| 87 | ||||
| 88 | for (p = r = buffer; *p != '\0'; p++) { | |||
| ||||
| 89 | if (*p == ' ') { | |||
| 90 | nargv = realloc(argv, sizeof(*argv) * (argc + 1)); | |||
| 91 | if (nargv == NULL((void*)0)) { | |||
| 92 | /* reallocation of argument vector failed, try using what is | |||
| 93 | * already processed. | |||
| 94 | */ | |||
| 95 | break; | |||
| 96 | } else { | |||
| 97 | argv = nargv; | |||
| 98 | } | |||
| 99 | argv[argc++] = r = p + 1; | |||
| 100 | *p = '\0'; | |||
| 101 | } | |||
| 102 | } | |||
| 103 | ||||
| 104 | RING_ITERATE_START(monkey_cmdhandler_t, handler_ring, handler)if (handler_ring != ((void*)0)) { monkey_cmdhandler_t *handler = handler_ring; do { do { { | |||
| 105 | if (strcmp(argv[0], handler->cmd) == 0) { | |||
| 106 | fn = handler->fn; | |||
| 107 | RING_ITERATE_STOP(handler_ring, handler)goto iteration_end_ring_handler; | |||
| 108 | } | |||
| 109 | } RING_ITERATE_END(handler_ring, handler)} while (0); handler = handler->r_next; } while (handler != handler_ring); } iteration_end_ring_handler:; | |||
| 110 | ||||
| 111 | if (fn != NULL((void*)0)) { | |||
| 112 | fn(argc, argv); | |||
| 113 | } | |||
| 114 | ||||
| 115 | free(argv); | |||
| 116 | } |