strtok() is recursive by default on win32.
[mpd-mk.git] / src / client_process.c
blobaeb75bb570651688ba90b878349c17efd0fd4dee
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "client_internal.h"
23 #include <string.h>
25 #define CLIENT_LIST_MODE_BEGIN "command_list_begin"
26 #define CLIENT_LIST_OK_MODE_BEGIN "command_list_ok_begin"
27 #define CLIENT_LIST_MODE_END "command_list_end"
29 static enum command_return
30 client_process_command_list(struct client *client, bool list_ok, GSList *list)
32 enum command_return ret = COMMAND_RETURN_OK;
33 unsigned num = 0;
35 for (GSList *cur = list; cur != NULL; cur = g_slist_next(cur)) {
36 char *cmd = cur->data;
38 g_debug("command_process_list: process command \"%s\"",
39 cmd);
40 ret = command_process(client, num++, cmd);
41 g_debug("command_process_list: command returned %i", ret);
42 if (ret != COMMAND_RETURN_OK || client_is_expired(client))
43 break;
44 else if (list_ok)
45 client_puts(client, "list_OK\n");
48 return ret;
51 enum command_return
52 client_process_line(struct client *client, char *line)
54 enum command_return ret;
56 if (strcmp(line, "noidle") == 0) {
57 if (client->idle_waiting) {
58 /* send empty idle response and leave idle mode */
59 client->idle_waiting = false;
60 command_success(client);
61 client_write_output(client);
64 /* do nothing if the client wasn't idling: the client
65 has already received the full idle response from
66 client_idle_notify(), which he can now evaluate */
68 return COMMAND_RETURN_OK;
69 } else if (client->idle_waiting) {
70 /* during idle mode, clients must not send anything
71 except "noidle" */
72 g_warning("[%u] command \"%s\" during idle",
73 client->num, line);
74 return COMMAND_RETURN_CLOSE;
77 if (client->cmd_list_OK >= 0) {
78 if (strcmp(line, CLIENT_LIST_MODE_END) == 0) {
79 g_debug("[%u] process command list",
80 client->num);
82 /* for scalability reasons, we have prepended
83 each new command; now we have to reverse it
84 to restore the correct order */
85 client->cmd_list = g_slist_reverse(client->cmd_list);
87 ret = client_process_command_list(client,
88 client->cmd_list_OK,
89 client->cmd_list);
90 g_debug("[%u] process command "
91 "list returned %i", client->num, ret);
93 if (ret == COMMAND_RETURN_CLOSE ||
94 client_is_expired(client))
95 return COMMAND_RETURN_CLOSE;
97 if (ret == COMMAND_RETURN_OK)
98 command_success(client);
100 client_write_output(client);
101 free_cmd_list(client->cmd_list);
102 client->cmd_list = NULL;
103 client->cmd_list_OK = -1;
104 } else {
105 size_t len = strlen(line) + 1;
106 client->cmd_list_size += len;
107 if (client->cmd_list_size >
108 client_max_command_list_size) {
109 g_warning("[%u] command list size (%lu) "
110 "is larger than the max (%lu)",
111 client->num,
112 (unsigned long)client->cmd_list_size,
113 (unsigned long)client_max_command_list_size);
114 return COMMAND_RETURN_CLOSE;
117 new_cmd_list_ptr(client, line);
118 ret = COMMAND_RETURN_OK;
120 } else {
121 if (strcmp(line, CLIENT_LIST_MODE_BEGIN) == 0) {
122 client->cmd_list_OK = 0;
123 ret = COMMAND_RETURN_OK;
124 } else if (strcmp(line, CLIENT_LIST_OK_MODE_BEGIN) == 0) {
125 client->cmd_list_OK = 1;
126 ret = COMMAND_RETURN_OK;
127 } else {
128 g_debug("[%u] process command \"%s\"",
129 client->num, line);
130 ret = command_process(client, 0, line);
131 g_debug("[%u] command returned %i",
132 client->num, ret);
134 if (ret == COMMAND_RETURN_CLOSE ||
135 client_is_expired(client))
136 return COMMAND_RETURN_CLOSE;
138 if (ret == COMMAND_RETURN_OK)
139 command_success(client);
141 client_write_output(client);
145 return ret;