version 1.21, 2016/10/14 22:14:22 |
version 1.22, 2016/10/16 19:04:05 |
|
|
* Show environment. |
* Show environment. |
*/ |
*/ |
|
|
static enum cmd_retval cmd_show_environment_exec(struct cmd *, struct cmd_q *); |
static enum cmd_retval cmd_show_environment_exec(struct cmd *, |
|
struct cmdq_item *); |
|
|
static char *cmd_show_environment_escape(struct environ_entry *); |
static char *cmd_show_environment_escape(struct environ_entry *); |
static void cmd_show_environment_print(struct cmd *, struct cmd_q *, |
static void cmd_show_environment_print(struct cmd *, struct cmdq_item *, |
struct environ_entry *); |
struct environ_entry *); |
|
|
const struct cmd_entry cmd_show_environment_entry = { |
const struct cmd_entry cmd_show_environment_entry = { |
|
|
} |
} |
|
|
static void |
static void |
cmd_show_environment_print(struct cmd *self, struct cmd_q *cmdq, |
cmd_show_environment_print(struct cmd *self, struct cmdq_item *item, |
struct environ_entry *envent) |
struct environ_entry *envent) |
{ |
{ |
char *escaped; |
char *escaped; |
|
|
if (!args_has(self->args, 's')) { |
if (!args_has(self->args, 's')) { |
if (envent->value != NULL) |
if (envent->value != NULL) |
cmdq_print(cmdq, "%s=%s", envent->name, envent->value); |
cmdq_print(item, "%s=%s", envent->name, envent->value); |
else |
else |
cmdq_print(cmdq, "-%s", envent->name); |
cmdq_print(item, "-%s", envent->name); |
return; |
return; |
} |
} |
|
|
if (envent->value != NULL) { |
if (envent->value != NULL) { |
escaped = cmd_show_environment_escape(envent); |
escaped = cmd_show_environment_escape(envent); |
cmdq_print(cmdq, "%s=\"%s\"; export %s;", envent->name, escaped, |
cmdq_print(item, "%s=\"%s\"; export %s;", envent->name, escaped, |
envent->name); |
envent->name); |
free(escaped); |
free(escaped); |
} else |
} else |
cmdq_print(cmdq, "unset %s;", envent->name); |
cmdq_print(item, "unset %s;", envent->name); |
} |
} |
|
|
static enum cmd_retval |
static enum cmd_retval |
cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq) |
cmd_show_environment_exec(struct cmd *self, struct cmdq_item *item) |
{ |
{ |
struct args *args = self->args; |
struct args *args = self->args; |
struct environ *env; |
struct environ *env; |
|
|
const char *target; |
const char *target; |
|
|
if ((target = args_get(args, 't')) != NULL) { |
if ((target = args_get(args, 't')) != NULL) { |
if (cmdq->state.tflag.s == NULL) { |
if (item->state.tflag.s == NULL) { |
cmdq_error(cmdq, "no such session: %s", target); |
cmdq_error(item, "no such session: %s", target); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
} |
} |
|
|
if (args_has(self->args, 'g')) |
if (args_has(self->args, 'g')) |
env = global_environ; |
env = global_environ; |
else { |
else { |
if (cmdq->state.tflag.s == NULL) { |
if (item->state.tflag.s == NULL) { |
target = args_get(args, 't'); |
target = args_get(args, 't'); |
if (target != NULL) |
if (target != NULL) |
cmdq_error(cmdq, "no such session: %s", target); |
cmdq_error(item, "no such session: %s", target); |
else |
else |
cmdq_error(cmdq, "no current session"); |
cmdq_error(item, "no current session"); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
env = cmdq->state.tflag.s->environ; |
env = item->state.tflag.s->environ; |
} |
} |
|
|
if (args->argc != 0) { |
if (args->argc != 0) { |
envent = environ_find(env, args->argv[0]); |
envent = environ_find(env, args->argv[0]); |
if (envent == NULL) { |
if (envent == NULL) { |
cmdq_error(cmdq, "unknown variable: %s", args->argv[0]); |
cmdq_error(item, "unknown variable: %s", args->argv[0]); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
cmd_show_environment_print(self, cmdq, envent); |
cmd_show_environment_print(self, item, envent); |
return (CMD_RETURN_NORMAL); |
return (CMD_RETURN_NORMAL); |
} |
} |
|
|
envent = environ_first(env); |
envent = environ_first(env); |
while (envent != NULL) { |
while (envent != NULL) { |
cmd_show_environment_print(self, cmdq, envent); |
cmd_show_environment_print(self, item, envent); |
envent = environ_next(envent); |
envent = environ_next(envent); |
} |
} |
return (CMD_RETURN_NORMAL); |
return (CMD_RETURN_NORMAL); |