4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5 * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
28 * Runs a command without a window.
31 int cmd_run_shell_exec(struct cmd
*, struct cmd_ctx
*);
33 void cmd_run_shell_callback(struct job
*);
34 void cmd_run_shell_free(void *);
36 const struct cmd_entry cmd_run_shell_entry
= {
46 struct cmd_run_shell_data
{
52 cmd_run_shell_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
54 struct args
*args
= self
->args
;
55 struct cmd_run_shell_data
*cdata
;
56 const char *shellcmd
= args
->argv
[0];
58 cdata
= xmalloc(sizeof *cdata
);
59 cdata
->cmd
= xstrdup(args
->argv
[0]);
60 memcpy(&cdata
->ctx
, ctx
, sizeof cdata
->ctx
);
62 if (ctx
->cmdclient
!= NULL
)
63 ctx
->cmdclient
->references
++;
64 if (ctx
->curclient
!= NULL
)
65 ctx
->curclient
->references
++;
67 job_run(shellcmd
, cmd_run_shell_callback
, cmd_run_shell_free
, cdata
);
69 return (1); /* don't let client exit */
73 cmd_run_shell_callback(struct job
*job
)
75 struct cmd_run_shell_data
*cdata
= job
->data
;
76 struct cmd_ctx
*ctx
= &cdata
->ctx
;
77 char *cmd
, *msg
, *line
;
82 if (ctx
->cmdclient
!= NULL
&& ctx
->cmdclient
->flags
& CLIENT_DEAD
)
84 if (ctx
->curclient
!= NULL
&& ctx
->curclient
->flags
& CLIENT_DEAD
)
89 if ((line
= evbuffer_readline(job
->event
->input
)) != NULL
) {
90 ctx
->print(ctx
, "%s", line
);
93 } while (line
!= NULL
);
95 size
= EVBUFFER_LENGTH(job
->event
->input
);
97 line
= xmalloc(size
+ 1);
98 memcpy(line
, EVBUFFER_DATA(job
->event
->input
), size
);
101 ctx
->print(ctx
, "%s", line
);
110 if (WIFEXITED(job
->status
)) {
111 if ((retcode
= WEXITSTATUS(job
->status
)) != 0)
112 xasprintf(&msg
, "'%s' returned %d", cmd
, retcode
);
113 } else if (WIFSIGNALED(job
->status
)) {
114 retcode
= WTERMSIG(job
->status
);
115 xasprintf(&msg
, "'%s' terminated by signal %d", cmd
, retcode
);
119 ctx
->print(ctx
, "%s", msg
);
121 ctx
->info(ctx
, "%s", msg
);
127 cmd_run_shell_free(void *data
)
129 struct cmd_run_shell_data
*cdata
= data
;
130 struct cmd_ctx
*ctx
= &cdata
->ctx
;
132 if (ctx
->cmdclient
!= NULL
) {
133 ctx
->cmdclient
->references
--;
134 ctx
->cmdclient
->flags
|= CLIENT_EXIT
;
136 if (ctx
->curclient
!= NULL
)
137 ctx
->curclient
->references
--;