+/* XXX: The memory management for the request strings is pretty cheesy. */
+static void
+_append_to_request (ttt_client_t *client,
+ const char *string)
+{
+ client->num_request_strings++;
+ client->request_strings =
+ xrealloc (client->request_strings,
+ client->num_request_strings * sizeof (char *));
+
+ client->request_strings[client->num_request_strings - 1] = xstrdup (string);
+}
+
+static void
+_free_request (ttt_client_t *client)
+{
+ int i;
+
+ for (i = 0; i < client->num_request_strings; i++)
+ free (client->request_strings[i]);
+
+ free (client->request_strings);
+
+ client->request_strings = NULL;
+ client->num_request_strings = 0;
+}
+
+static ttt_status_t
+_read_request (ttt_client_t *client)
+{
+ ttt_token_t token;
+
+ _free_request (client);
+
+ while (1) {
+ token = yylex (client->scanner);
+ /* Yes, EOF in two different enums is pretty ugly. */
+ if (token == TTT_TOKEN_EOF)
+ return TTT_STATUS_EOF;
+
+ if (token == TTT_TOKEN_NEWLINE) {
+ if (client->num_request_strings)
+ return TTT_STATUS_SUCCESS;
+ else
+ continue;
+ }
+
+ assert (token == TTT_TOKEN_STRING);
+
+ _append_to_request (client, yyget_text (client->scanner));
+ }
+}
+
+static ttt_error_t
+_execute_request (ttt_client_t *client)
+{
+ int i;
+
+ char *command = client->request_strings[0];
+ int num_args = client->num_request_strings-1;
+ ttt_command_description_t *desc;
+
+ for (i=0; i < strlen (command); i++)
+ command[i] = toupper (command[i]);
+
+ for (i=0; i < ARRAY_SIZE(command_descriptions); i++) {
+ desc = &command_descriptions[i];
+ if (strcmp(command, desc->command) == 0) {
+ if (num_args != desc->args_required)
+ return TTT_ERROR_SYNTAX;
+ return (desc->execute) (client, &client->request_strings[1], num_args);
+ }
+ }
+
+ return TTT_ERROR_COMMAND;
+}
+
+static void *
+_handle_requests_thread (void *closure)
+{
+ ttt_status_t status;
+ ttt_error_t error;
+ ttt_client_t *client = closure;
+
+ while (1) {
+
+ status = _read_request (client);
+ if (status == TTT_STATUS_EOF)
+ break;
+ if (status)
+ ASSERT_NOT_REACHED;
+
+ error = _execute_request (client);
+ if (error == TTT_ERROR_QUIT_REQUESTED)
+ break;
+ if (error)
+ ttt_client_send (client, ttt_error_string (error));
+ }
+
+ _ttt_client_fini (client);
+ free (client);
+
+ return (void *) 0;
+}
+
+/* Exported: See ttt-client.h for documentation. */
+void
+ttt_client_new (void *closure, int client_socket)
+{
+ ttt_server_t *server = closure;
+ ttt_client_t *client;
+ int err;
+
+ client = xmalloc (sizeof (ttt_client_t));
+
+ _ttt_client_init (client, server, client_socket);
+
+ err = pthread_create (&client->thread, NULL,
+ _handle_requests_thread, client);
+ if (err != 0) {
+ fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\n",
+ strerror (err));
+ exit (1);
+ }
+}
+
+/* Exported: See ttt-client.h for documentation. */
+void
+ttt_client_send (ttt_client_t *client, const char *message)
+{
+ pthread_mutex_lock (&client->mutex);
+
+ ttt_socket_write (client->socket, message, strlen (message));
+
+ pthread_mutex_unlock (&client->mutex);
+}
+
+/* Exported: See ttt-client.h for documentation. */
+const char*
+ttt_client_get_name (ttt_client_t *client)
+{
+ return client->name;
+}
+
+/* Exported: See ttt-client.h for documentation. */
+void
+ttt_client_set_name (ttt_client_t *client, const char *name)
+{
+ free (client->name);
+ client->name = xstrdup (name);