cmd-set-environment.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. * Set an environment variable.
  23. */
  24. enum cmd_retval cmd_set_environment_exec(struct cmd *, struct cmd_q *);
  25. const struct cmd_entry cmd_set_environment_entry = {
  26. .name = "set-environment",
  27. .alias = "setenv",
  28. .args = { "grt:u", 1, 2 },
  29. .usage = "[-gru] " CMD_TARGET_SESSION_USAGE " name [value]",
  30. .tflag = CMD_SESSION_CANFAIL,
  31. .flags = 0,
  32. .exec = cmd_set_environment_exec
  33. };
  34. enum cmd_retval
  35. cmd_set_environment_exec(struct cmd *self, struct cmd_q *cmdq)
  36. {
  37. struct args *args = self->args;
  38. struct environ *env;
  39. const char *name, *value, *target;
  40. name = args->argv[0];
  41. if (*name == '\0') {
  42. cmdq_error(cmdq, "empty variable name");
  43. return (CMD_RETURN_ERROR);
  44. }
  45. if (strchr(name, '=') != NULL) {
  46. cmdq_error(cmdq, "variable name contains =");
  47. return (CMD_RETURN_ERROR);
  48. }
  49. if (args->argc < 2)
  50. value = NULL;
  51. else
  52. value = args->argv[1];
  53. if (args_has(self->args, 'g'))
  54. env = global_environ;
  55. else {
  56. if (cmdq->state.tflag.s == NULL) {
  57. target = args_get(args, 't');
  58. if (target != NULL)
  59. cmdq_error(cmdq, "no such session: %s", target);
  60. else
  61. cmdq_error(cmdq, "no current session");
  62. return (CMD_RETURN_ERROR);
  63. }
  64. env = cmdq->state.tflag.s->environ;
  65. }
  66. if (args_has(self->args, 'u')) {
  67. if (value != NULL) {
  68. cmdq_error(cmdq, "can't specify a value with -u");
  69. return (CMD_RETURN_ERROR);
  70. }
  71. environ_unset(env, name);
  72. } else if (args_has(self->args, 'r')) {
  73. if (value != NULL) {
  74. cmdq_error(cmdq, "can't specify a value with -r");
  75. return (CMD_RETURN_ERROR);
  76. }
  77. environ_clear(env, name);
  78. } else {
  79. if (value == NULL) {
  80. cmdq_error(cmdq, "no value specified");
  81. return (CMD_RETURN_ERROR);
  82. }
  83. environ_set(env, name, "%s", value);
  84. }
  85. return (CMD_RETURN_NORMAL);
  86. }