NetSurf
dispatch.c
Go to the documentation of this file.
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
30typedef struct cmdhandler {
32 char *cmd;
35
37
40{
41 monkey_cmdhandler_t *ret = calloc(1, sizeof(*ret));
42 if (ret == NULL) {
43 NSLOG(netsurf, INFO, "Unable to allocate handler");
44 return NSERROR_NOMEM;
45 }
46 ret->cmd = strdup(cmd);
47 ret->fn = fn;
49 return NSERROR_OK;
50}
51
52void
54{
55 while (handler_ring != NULL) {
57 RING_REMOVE(handler_ring, handler);
58 free(handler->cmd);
59 free(handler);
60 }
61}
62
63void
65{
66 char buffer[PATH_MAX];
67 int argc = 0;
68 char **argv = NULL;
69 char *p, *r = NULL;
70 handle_command_fn fn = NULL;
71 char **nargv;
72
73 if (fgets(buffer, PATH_MAX, stdin) == NULL) {
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) {
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) {
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
105 if (strcmp(argv[0], handler->cmd) == 0) {
106 fn = handler->fn;
108 }
109 } RING_ITERATE_END(handler_ring, handler);
110
111 if (fn != NULL) {
112 fn(argc, argv);
113 }
114
115 free(argv);
116}
#define PATH_MAX
Definition: gui.h:31
static osspriteop_area * buffer
The buffer characteristics.
Definition: buffer.c:55
static monkey_cmdhandler_t * handler_ring
Definition: dispatch.c:36
void monkey_process_command(void)
Definition: dispatch.c:64
nserror monkey_register_handler(const char *cmd, handle_command_fn fn)
Definition: dispatch.c:39
void monkey_free_handlers(void)
Definition: dispatch.c:53
struct cmdhandler monkey_cmdhandler_t
void(* handle_command_fn)(int argc, char **argv)
Definition: dispatch.h:22
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
Ring list structure.
#define RING_ITERATE_STOP(ring, iteratorptr)
Definition: ring.h:133
#define RING_REMOVE(ring, element)
Remove the given element from the specified ring.
Definition: ring.h:53
#define RING_INSERT(ring, element)
Insert the given item into the specified ring.
Definition: ring.h:40
#define RING_ITERATE_END(ring, iteratorptr)
Definition: ring.h:136
#define RING_ITERATE_START(ringtype, ring, iteratorptr)
Definition: ring.h:127
Interface to utility string handling.
struct cmdhandler * r_next
Definition: dispatch.c:31
handle_command_fn fn
Definition: dispatch.c:33
char * cmd
Definition: dispatch.c:32
struct cmdhandler * r_prev
Definition: dispatch.c:31
Interface to a number of general purpose functionality.