2007-10-03 23:31:07 +02:00
|
|
|
/* $Id: cmd.c,v 1.5 2007-10-03 21:31:07 nicm Exp $ */
|
2007-10-03 12:18:32 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
#include <getopt.h>
|
2007-10-03 13:26:34 +02:00
|
|
|
#include <string.h>
|
2007-10-03 12:18:32 +02:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
const struct cmd_entry *cmd_table[] = {
|
|
|
|
&cmd_detach_session_entry,
|
|
|
|
&cmd_list_sessions_entry,
|
|
|
|
&cmd_new_session_entry,
|
|
|
|
NULL
|
2007-10-03 13:26:34 +02:00
|
|
|
};
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
struct cmd *
|
|
|
|
cmd_parse(int argc, char **argv, char **cause)
|
2007-10-03 13:26:34 +02:00
|
|
|
{
|
2007-10-03 23:31:07 +02:00
|
|
|
const struct cmd_entry **this, *entry;
|
|
|
|
struct cmd *cmd;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
*cause = NULL;
|
|
|
|
if (argc == 0)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
entry = NULL;
|
|
|
|
for (this = cmd_table; *this != NULL; this++) {
|
|
|
|
if (strcmp((*this)->alias, argv[0]) == 0) {
|
|
|
|
entry = *this;
|
2007-10-03 13:26:34 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
if (strncmp((*this)->name, argv[0], strlen(argv[0])) != 0)
|
|
|
|
continue;
|
|
|
|
if (entry != NULL) {
|
|
|
|
xasprintf(cause, "ambiguous command: %s", argv[0]);
|
|
|
|
return (NULL);
|
2007-10-03 13:26:34 +02:00
|
|
|
}
|
2007-10-03 23:31:07 +02:00
|
|
|
entry = *this;
|
2007-10-03 13:26:34 +02:00
|
|
|
}
|
2007-10-03 23:31:07 +02:00
|
|
|
if (entry == NULL) {
|
|
|
|
xasprintf(cause, "unknown command: %s", argv[0]);
|
|
|
|
return (NULL);
|
2007-10-03 14:34:16 +02:00
|
|
|
}
|
2007-10-03 13:26:34 +02:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
optind = 1;
|
|
|
|
if (entry->parse == NULL) {
|
|
|
|
while ((opt = getopt(argc, argv, "")) != EOF) {
|
|
|
|
switch (opt) {
|
|
|
|
default:
|
|
|
|
goto usage;
|
|
|
|
}
|
2007-10-03 13:26:34 +02:00
|
|
|
}
|
2007-10-03 23:31:07 +02:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc != 0)
|
|
|
|
goto usage;
|
2007-10-03 13:26:34 +02:00
|
|
|
}
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
cmd = xmalloc(sizeof *cmd);
|
|
|
|
cmd->entry = entry;
|
|
|
|
if (entry->parse != NULL) {
|
|
|
|
if (entry->parse(&cmd->data, argc, argv, cause) != 0) {
|
|
|
|
xfree(cmd);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (cmd);
|
2007-10-03 13:26:34 +02:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
usage:
|
|
|
|
if (entry->usage == NULL)
|
|
|
|
usage(cause, "%s", entry->name);
|
|
|
|
else
|
|
|
|
usage(cause, "%s", entry->usage());
|
|
|
|
return (NULL);
|
2007-10-03 13:26:34 +02:00
|
|
|
}
|
2007-10-03 12:18:32 +02:00
|
|
|
|
|
|
|
void
|
2007-10-03 23:31:07 +02:00
|
|
|
cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
|
2007-10-03 12:18:32 +02:00
|
|
|
{
|
2007-10-03 23:31:07 +02:00
|
|
|
return (cmd->entry->exec(cmd->data, ctx));
|
2007-10-03 12:18:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-03 23:31:07 +02:00
|
|
|
cmd_send(struct cmd *cmd, struct buffer *b)
|
2007-10-03 12:18:32 +02:00
|
|
|
{
|
2007-10-03 23:31:07 +02:00
|
|
|
buffer_write(b, &cmd->entry->type, sizeof cmd->entry->type);
|
2007-10-03 12:18:32 +02:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
if (cmd->entry->send == NULL)
|
|
|
|
return;
|
|
|
|
return (cmd->entry->send(cmd->data, b));
|
2007-10-03 12:18:32 +02:00
|
|
|
}
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
struct cmd *
|
|
|
|
cmd_recv(struct buffer *b)
|
2007-10-03 12:18:32 +02:00
|
|
|
{
|
2007-10-03 23:31:07 +02:00
|
|
|
const struct cmd_entry **this, *entry;
|
|
|
|
struct cmd *cmd;
|
|
|
|
enum cmd_type type;
|
|
|
|
|
|
|
|
buffer_read(b, &type, sizeof type);
|
|
|
|
|
|
|
|
entry = NULL;
|
|
|
|
for (this = cmd_table; *this != NULL; this++) {
|
|
|
|
if ((*this)->type == type) {
|
|
|
|
entry = *this;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*this == NULL)
|
|
|
|
return (NULL);
|
2007-10-03 12:18:32 +02:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
cmd = xmalloc(sizeof *cmd);
|
|
|
|
cmd->entry = entry;
|
2007-10-03 12:18:32 +02:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
if (cmd->entry->recv != NULL)
|
|
|
|
cmd->entry->recv(&cmd->data, b);
|
|
|
|
return (cmd);
|
2007-10-03 12:18:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-03 23:31:07 +02:00
|
|
|
cmd_free(struct cmd *cmd)
|
2007-10-03 12:18:32 +02:00
|
|
|
{
|
2007-10-03 23:31:07 +02:00
|
|
|
if (cmd->entry->free != NULL)
|
|
|
|
cmd->entry->free(cmd->data);
|
|
|
|
xfree(cmd);
|
2007-10-03 12:18:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-10-03 23:31:07 +02:00
|
|
|
cmd_send_string(struct buffer *b, const char *s)
|
2007-10-03 12:18:32 +02:00
|
|
|
{
|
2007-10-03 23:31:07 +02:00
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (s == NULL) {
|
|
|
|
n = 0;
|
|
|
|
buffer_write(b, &n, sizeof n);
|
|
|
|
return;
|
|
|
|
}
|
2007-10-03 12:18:32 +02:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
n = strlen(s) + 1;
|
|
|
|
buffer_write(b, &n, sizeof n);
|
2007-10-03 12:18:32 +02:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
buffer_write(b, s, n);
|
2007-10-03 12:18:32 +02:00
|
|
|
}
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
char *
|
|
|
|
cmd_recv_string(struct buffer *b)
|
2007-10-03 12:18:32 +02:00
|
|
|
{
|
2007-10-03 23:31:07 +02:00
|
|
|
char *s;
|
|
|
|
size_t n;
|
2007-10-03 12:18:32 +02:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
buffer_read(b, &n, sizeof n);
|
2007-10-03 12:18:32 +02:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
if (n == 0)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
s = xmalloc(n);
|
|
|
|
buffer_read(b, s, n);
|
|
|
|
s[n - 1] = '\0';
|
2007-10-03 12:18:32 +02:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
return (s);
|
2007-10-03 12:18:32 +02:00
|
|
|
}
|