2011-07-09 11:42:33 +02:00
|
|
|
/* $Id$ */
|
2009-01-18 15:40:48 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 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>
|
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
#include <stdlib.h>
|
2009-01-18 15:40:48 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
struct cmd_list *
|
2013-02-23 23:25:58 +01:00
|
|
|
cmd_list_parse(int argc, char **argv, const char* file, u_int line,
|
|
|
|
char **cause)
|
2009-01-18 15:40:48 +01:00
|
|
|
{
|
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
struct cmd *cmd;
|
|
|
|
int i, lastsplit;
|
2009-04-30 23:53:32 +02:00
|
|
|
size_t arglen, new_argc;
|
2010-12-06 22:48:56 +01:00
|
|
|
char **copy_argv, **new_argv;
|
|
|
|
|
|
|
|
copy_argv = cmd_copy_argv(argc, argv);
|
2009-01-18 15:40:48 +01:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
cmdlist = xcalloc(1, sizeof *cmdlist);
|
2010-07-02 04:43:01 +02:00
|
|
|
cmdlist->references = 1;
|
|
|
|
TAILQ_INIT(&cmdlist->list);
|
2009-01-18 15:40:48 +01:00
|
|
|
|
|
|
|
lastsplit = 0;
|
|
|
|
for (i = 0; i < argc; i++) {
|
2010-12-06 22:48:56 +01:00
|
|
|
arglen = strlen(copy_argv[i]);
|
|
|
|
if (arglen == 0 || copy_argv[i][arglen - 1] != ';')
|
2009-04-30 23:53:32 +02:00
|
|
|
continue;
|
2010-12-06 22:48:56 +01:00
|
|
|
copy_argv[i][arglen - 1] = '\0';
|
2009-04-30 23:53:32 +02:00
|
|
|
|
2010-12-06 22:48:56 +01:00
|
|
|
if (arglen > 1 && copy_argv[i][arglen - 2] == '\\') {
|
|
|
|
copy_argv[i][arglen - 2] = ';';
|
2009-04-30 23:53:32 +02:00
|
|
|
continue;
|
2009-05-04 19:58:27 +02:00
|
|
|
}
|
2009-04-30 23:53:32 +02:00
|
|
|
|
|
|
|
new_argc = i - lastsplit;
|
2010-12-06 22:48:56 +01:00
|
|
|
new_argv = copy_argv + lastsplit;
|
2009-04-30 23:53:32 +02:00
|
|
|
if (arglen != 1)
|
|
|
|
new_argc++;
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
cmd = cmd_parse(new_argc, new_argv, file, line, cause);
|
2009-04-30 23:53:32 +02:00
|
|
|
if (cmd == NULL)
|
|
|
|
goto bad;
|
2010-07-02 04:43:01 +02:00
|
|
|
TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry);
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2009-04-30 23:53:32 +02:00
|
|
|
lastsplit = i + 1;
|
2009-01-18 15:40:48 +01:00
|
|
|
}
|
|
|
|
|
2009-04-30 23:53:32 +02:00
|
|
|
if (lastsplit != argc) {
|
2013-02-23 23:25:58 +01:00
|
|
|
cmd = cmd_parse(argc - lastsplit, copy_argv + lastsplit,
|
|
|
|
file, line, cause);
|
2009-04-30 23:53:32 +02:00
|
|
|
if (cmd == NULL)
|
|
|
|
goto bad;
|
2010-07-02 04:43:01 +02:00
|
|
|
TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry);
|
2009-04-30 23:53:32 +02:00
|
|
|
}
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2010-12-06 22:48:56 +01:00
|
|
|
cmd_free_argv(argc, copy_argv);
|
2009-01-18 15:40:48 +01:00
|
|
|
return (cmdlist);
|
|
|
|
|
|
|
|
bad:
|
|
|
|
cmd_list_free(cmdlist);
|
2010-12-06 22:48:56 +01:00
|
|
|
cmd_free_argv(argc, copy_argv);
|
2009-01-18 15:40:48 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_list_free(struct cmd_list *cmdlist)
|
|
|
|
{
|
2013-02-23 23:25:58 +01:00
|
|
|
struct cmd *cmd, *cmd1;
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2010-07-02 04:43:01 +02:00
|
|
|
if (--cmdlist->references != 0)
|
|
|
|
return;
|
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
TAILQ_FOREACH_SAFE(cmd, &cmdlist->list, qentry, cmd1) {
|
2010-07-02 04:43:01 +02:00
|
|
|
TAILQ_REMOVE(&cmdlist->list, cmd, qentry);
|
2013-02-19 00:20:21 +01:00
|
|
|
args_free(cmd->args);
|
2013-02-23 23:25:58 +01:00
|
|
|
free(cmd->file);
|
2013-02-19 00:20:21 +01:00
|
|
|
free(cmd);
|
2009-01-18 15:40:48 +01:00
|
|
|
}
|
2013-02-23 23:25:58 +01:00
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
free(cmdlist);
|
2009-01-18 15:40:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
cmd_list_print(struct cmd_list *cmdlist, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct cmd *cmd;
|
|
|
|
size_t off;
|
|
|
|
|
|
|
|
off = 0;
|
2010-07-02 04:43:01 +02:00
|
|
|
TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
|
2009-01-18 15:40:48 +01:00
|
|
|
if (off >= len)
|
|
|
|
break;
|
|
|
|
off += cmd_print(cmd, buf + off, len - off);
|
|
|
|
if (off >= len)
|
|
|
|
break;
|
|
|
|
if (TAILQ_NEXT(cmd, qentry) != NULL)
|
|
|
|
off += xsnprintf(buf + off, len - off, " ; ");
|
|
|
|
}
|
|
|
|
return (off);
|
|
|
|
}
|