cmd-show-environment.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "tmux.h"
  21. /*
  22. * Show environment.
  23. */
  24. enum cmd_retval cmd_show_environment_exec(struct cmd *, struct cmd_q *);
  25. char *cmd_show_environment_escape(struct environ_entry *);
  26. void cmd_show_environment_print(struct cmd *, struct cmd_q *,
  27. struct environ_entry *);
  28. const struct cmd_entry cmd_show_environment_entry = {
  29. .name = "show-environment",
  30. .alias = "showenv",
  31. .args = { "gst:", 0, 1 },
  32. .usage = "[-gs] " CMD_TARGET_SESSION_USAGE " [name]",
  33. .tflag = CMD_SESSION_CANFAIL,
  34. .flags = 0,
  35. .exec = cmd_show_environment_exec
  36. };
  37. char *
  38. cmd_show_environment_escape(struct environ_entry *envent)
  39. {
  40. const char *value = envent->value;
  41. char c, *out, *ret;
  42. out = ret = xmalloc(strlen(value) * 2 + 1); /* at most twice the size */
  43. while ((c = *value++) != '\0') {
  44. /* POSIX interprets $ ` " and \ in double quotes. */
  45. if (c == '$' || c == '`' || c == '"' || c == '\\')
  46. *out++ = '\\';
  47. *out++ = c;
  48. }
  49. *out = '\0';
  50. return (ret);
  51. }
  52. void
  53. cmd_show_environment_print(struct cmd *self, struct cmd_q *cmdq,
  54. struct environ_entry *envent)
  55. {
  56. char *escaped;
  57. if (!args_has(self->args, 's')) {
  58. if (envent->value != NULL)
  59. cmdq_print(cmdq, "%s=%s", envent->name, envent->value);
  60. else
  61. cmdq_print(cmdq, "-%s", envent->name);
  62. return;
  63. }
  64. if (envent->value != NULL) {
  65. escaped = cmd_show_environment_escape(envent);
  66. cmdq_print(cmdq, "%s=\"%s\"; export %s;", envent->name, escaped,
  67. envent->name);
  68. free(escaped);
  69. } else
  70. cmdq_print(cmdq, "unset %s;", envent->name);
  71. }
  72. enum cmd_retval
  73. cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq)
  74. {
  75. struct args *args = self->args;
  76. struct environ *env;
  77. struct environ_entry *envent;
  78. const char *target;
  79. if ((target = args_get(args, 't')) != NULL) {
  80. if (cmdq->state.tflag.s == NULL) {
  81. cmdq_error(cmdq, "no such session: %s", target);
  82. return (CMD_RETURN_ERROR);
  83. }
  84. }
  85. if (args_has(self->args, 'g'))
  86. env = global_environ;
  87. else {
  88. if (cmdq->state.tflag.s == NULL) {
  89. target = args_get(args, 't');
  90. if (target != NULL)
  91. cmdq_error(cmdq, "no such session: %s", target);
  92. else
  93. cmdq_error(cmdq, "no current session");
  94. return (CMD_RETURN_ERROR);
  95. }
  96. env = cmdq->state.tflag.s->environ;
  97. }
  98. if (args->argc != 0) {
  99. envent = environ_find(env, args->argv[0]);
  100. if (envent == NULL) {
  101. cmdq_error(cmdq, "unknown variable: %s", args->argv[0]);
  102. return (CMD_RETURN_ERROR);
  103. }
  104. cmd_show_environment_print(self, cmdq, envent);
  105. return (CMD_RETURN_NORMAL);
  106. }
  107. envent = environ_first(env);
  108. while (envent != NULL) {
  109. cmd_show_environment_print(self, cmdq, envent);
  110. envent = environ_next(envent);
  111. }
  112. return (CMD_RETURN_NORMAL);
  113. }