Home | History | Annotate | Line # | Download | only in dist
cmd-if-shell.c revision 1.11
      1   1.4  christos /* $OpenBSD$ */
      2   1.1      jmmv 
      3   1.1      jmmv /*
      4   1.1      jmmv  * Copyright (c) 2009 Tiago Cunha <me (at) tiagocunha.org>
      5   1.1      jmmv  * Copyright (c) 2009 Nicholas Marriott <nicm (at) openbsd.org>
      6   1.1      jmmv  *
      7   1.1      jmmv  * Permission to use, copy, modify, and distribute this software for any
      8   1.1      jmmv  * purpose with or without fee is hereby granted, provided that the above
      9   1.1      jmmv  * copyright notice and this permission notice appear in all copies.
     10   1.1      jmmv  *
     11   1.1      jmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1      jmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1      jmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1      jmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1      jmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     16   1.1      jmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     17   1.1      jmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1      jmmv  */
     19   1.1      jmmv 
     20   1.1      jmmv #include <sys/types.h>
     21   1.1      jmmv #include <sys/wait.h>
     22   1.1      jmmv 
     23   1.3  christos #include <stdlib.h>
     24   1.1      jmmv #include <string.h>
     25   1.1      jmmv 
     26   1.1      jmmv #include "tmux.h"
     27   1.1      jmmv 
     28   1.1      jmmv /*
     29   1.3  christos  * Executes a tmux command if a shell command returns true or false.
     30   1.1      jmmv  */
     31   1.1      jmmv 
     32   1.6  christos static enum cmd_retval	cmd_if_shell_exec(struct cmd *, struct cmdq_item *);
     33   1.1      jmmv 
     34   1.6  christos static void		cmd_if_shell_callback(struct job *);
     35   1.6  christos static void		cmd_if_shell_free(void *);
     36   1.1      jmmv 
     37   1.1      jmmv const struct cmd_entry cmd_if_shell_entry = {
     38   1.5  christos 	.name = "if-shell",
     39   1.5  christos 	.alias = "if",
     40   1.5  christos 
     41   1.5  christos 	.args = { "bFt:", 2, 3 },
     42   1.5  christos 	.usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
     43   1.5  christos 		 "[command]",
     44   1.5  christos 
     45   1.7  christos 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
     46   1.5  christos 
     47   1.5  christos 	.flags = 0,
     48   1.5  christos 	.exec = cmd_if_shell_exec
     49   1.1      jmmv };
     50   1.1      jmmv 
     51   1.1      jmmv struct cmd_if_shell_data {
     52  1.10  christos 	struct cmd_parse_input	 input;
     53   1.6  christos 
     54   1.4  christos 	char			*cmd_if;
     55   1.4  christos 	char			*cmd_else;
     56   1.4  christos 
     57   1.6  christos 	struct client		*client;
     58   1.6  christos 	struct cmdq_item	*item;
     59   1.1      jmmv };
     60   1.1      jmmv 
     61   1.6  christos static enum cmd_retval
     62   1.6  christos cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
     63   1.1      jmmv {
     64  1.11  christos 	struct args			*args = cmd_get_args(self);
     65  1.11  christos 	struct cmd_find_state		*target = cmdq_get_target(item);
     66  1.11  christos 	struct cmdq_state		*state = cmdq_get_state(item);
     67   1.1      jmmv 	struct cmd_if_shell_data	*cdata;
     68  1.11  christos 	char				*shellcmd, *cmd, *error;
     69  1.11  christos 	const char			*file;
     70  1.11  christos 	struct client			*tc = cmdq_get_target_client(item);
     71  1.11  christos 	struct session			*s = target->s;
     72  1.10  christos 	struct cmd_parse_input		 pi;
     73  1.11  christos 	enum cmd_parse_status		 status;
     74   1.3  christos 
     75  1.11  christos 	shellcmd = format_single_from_target(item, args->argv[0]);
     76   1.4  christos 	if (args_has(args, 'F')) {
     77   1.4  christos 		if (*shellcmd != '0' && *shellcmd != '\0')
     78   1.4  christos 			cmd = args->argv[1];
     79   1.4  christos 		else if (args->argc == 3)
     80   1.4  christos 			cmd = args->argv[2];
     81  1.10  christos 		else
     82  1.10  christos 			cmd = NULL;
     83   1.5  christos 		free(shellcmd);
     84   1.4  christos 		if (cmd == NULL)
     85   1.4  christos 			return (CMD_RETURN_NORMAL);
     86  1.10  christos 
     87  1.10  christos 		memset(&pi, 0, sizeof pi);
     88  1.11  christos 		cmd_get_source(self, &pi.file, &pi.line);
     89  1.10  christos 		pi.item = item;
     90  1.11  christos 		pi.c = tc;
     91  1.11  christos 		cmd_find_copy_state(&pi.fs, target);
     92  1.10  christos 
     93  1.11  christos 		status = cmd_parse_and_insert(cmd, &pi, item, state, &error);
     94  1.11  christos 		if (status == CMD_PARSE_ERROR) {
     95  1.11  christos 			cmdq_error(item, "%s", error);
     96  1.11  christos 			free(error);
     97   1.4  christos 			return (CMD_RETURN_ERROR);
     98   1.4  christos 		}
     99   1.4  christos 		return (CMD_RETURN_NORMAL);
    100   1.4  christos 	}
    101   1.4  christos 
    102   1.6  christos 	cdata = xcalloc(1, sizeof *cdata);
    103   1.4  christos 
    104   1.3  christos 	cdata->cmd_if = xstrdup(args->argv[1]);
    105   1.3  christos 	if (args->argc == 3)
    106   1.3  christos 		cdata->cmd_else = xstrdup(args->argv[2]);
    107   1.3  christos 	else
    108   1.3  christos 		cdata->cmd_else = NULL;
    109   1.4  christos 
    110  1.10  christos 	if (!args_has(args, 'b'))
    111  1.11  christos 		cdata->client = cmdq_get_client(item);
    112  1.10  christos 	else
    113  1.11  christos 		cdata->client = tc;
    114   1.7  christos 	if (cdata->client != NULL)
    115   1.7  christos 		cdata->client->references++;
    116   1.3  christos 
    117   1.6  christos 	if (!args_has(args, 'b'))
    118   1.6  christos 		cdata->item = item;
    119   1.6  christos 	else
    120   1.6  christos 		cdata->item = NULL;
    121  1.10  christos 
    122  1.10  christos 	memset(&cdata->input, 0, sizeof cdata->input);
    123  1.11  christos 	cmd_get_source(self, &file, &cdata->input.line);
    124  1.11  christos 	if (file != NULL)
    125  1.11  christos 		cdata->input.file = xstrdup(file);
    126  1.11  christos 	cdata->input.c = tc;
    127  1.10  christos 	if (cdata->input.c != NULL)
    128  1.10  christos 		cdata->input.c->references++;
    129  1.11  christos 	cmd_find_copy_state(&cdata->input.fs, target);
    130   1.3  christos 
    131  1.11  christos 	if (job_run(shellcmd, 0, NULL, s,
    132  1.11  christos 	    server_client_get_cwd(cmdq_get_client(item), s), NULL,
    133  1.11  christos 	    cmd_if_shell_callback, cmd_if_shell_free, cdata, 0, -1,
    134  1.11  christos 	    -1) == NULL) {
    135   1.9  christos 		cmdq_error(item, "failed to run command: %s", shellcmd);
    136   1.9  christos 		free(shellcmd);
    137   1.9  christos 		free(cdata);
    138   1.9  christos 		return (CMD_RETURN_ERROR);
    139   1.9  christos 	}
    140   1.3  christos 	free(shellcmd);
    141   1.3  christos 
    142   1.6  christos 	if (args_has(args, 'b'))
    143   1.3  christos 		return (CMD_RETURN_NORMAL);
    144   1.3  christos 	return (CMD_RETURN_WAIT);
    145   1.1      jmmv }
    146   1.1      jmmv 
    147   1.6  christos static void
    148   1.1      jmmv cmd_if_shell_callback(struct job *job)
    149   1.1      jmmv {
    150   1.9  christos 	struct cmd_if_shell_data	*cdata = job_get_data(job);
    151   1.6  christos 	struct client			*c = cdata->client;
    152  1.10  christos 	struct cmdq_item		*new_item = NULL;
    153  1.11  christos 	struct cmdq_state		*new_state = NULL;
    154  1.10  christos 	char				*cmd;
    155   1.9  christos 	int				 status;
    156  1.10  christos 	struct cmd_parse_result		*pr;
    157   1.1      jmmv 
    158   1.9  christos 	status = job_get_status(job);
    159   1.9  christos 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
    160   1.3  christos 		cmd = cdata->cmd_else;
    161   1.3  christos 	else
    162   1.3  christos 		cmd = cdata->cmd_if;
    163   1.3  christos 	if (cmd == NULL)
    164   1.6  christos 		goto out;
    165   1.1      jmmv 
    166  1.10  christos 	pr = cmd_parse_from_string(cmd, &cdata->input);
    167  1.10  christos 	switch (pr->status) {
    168  1.10  christos 	case CMD_PARSE_EMPTY:
    169  1.10  christos 		break;
    170  1.10  christos 	case CMD_PARSE_ERROR:
    171  1.10  christos 		if (cdata->item != NULL)
    172  1.10  christos 		       cmdq_error(cdata->item, "%s", pr->error);
    173  1.10  christos 		free(pr->error);
    174  1.10  christos 		break;
    175  1.10  christos 	case CMD_PARSE_SUCCESS:
    176  1.11  christos 		if (cdata->item == NULL)
    177  1.11  christos 			new_state = cmdq_new_state(NULL, NULL, 0);
    178  1.11  christos 		else
    179  1.11  christos 			new_state = cmdq_get_state(cdata->item);
    180  1.11  christos 		new_item = cmdq_get_command(pr->cmdlist, new_state);
    181  1.11  christos 		if (cdata->item == NULL)
    182  1.11  christos 			cmdq_free_state(new_state);
    183  1.10  christos 		cmd_list_free(pr->cmdlist);
    184  1.10  christos 		break;
    185   1.1      jmmv 	}
    186   1.6  christos 	if (new_item != NULL) {
    187   1.6  christos 		if (cdata->item == NULL)
    188   1.6  christos 			cmdq_append(c, new_item);
    189   1.6  christos 		else
    190   1.6  christos 			cmdq_insert_after(cdata->item, new_item);
    191   1.6  christos 	}
    192   1.3  christos 
    193   1.6  christos out:
    194   1.6  christos 	if (cdata->item != NULL)
    195  1.10  christos 		cmdq_continue(cdata->item);
    196   1.3  christos }
    197   1.3  christos 
    198   1.6  christos static void
    199   1.1      jmmv cmd_if_shell_free(void *data)
    200   1.1      jmmv {
    201   1.1      jmmv 	struct cmd_if_shell_data	*cdata = data;
    202   1.3  christos 
    203   1.7  christos 	if (cdata->client != NULL)
    204   1.7  christos 		server_client_unref(cdata->client);
    205   1.1      jmmv 
    206   1.3  christos 	free(cdata->cmd_else);
    207   1.3  christos 	free(cdata->cmd_if);
    208   1.6  christos 
    209  1.10  christos 	if (cdata->input.c != NULL)
    210  1.10  christos 		server_client_unref(cdata->input.c);
    211  1.10  christos 	free(__UNCONST(cdata->input.file));
    212  1.10  christos 
    213   1.3  christos 	free(cdata);
    214   1.1      jmmv }
    215