cmd-show-options.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 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 options.
  23. */
  24. enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmd_q *);
  25. enum cmd_retval cmd_show_options_one(struct cmd *, struct cmd_q *,
  26. struct options *, int);
  27. enum cmd_retval cmd_show_options_all(struct cmd *, struct cmd_q *,
  28. struct options *, enum options_table_scope);
  29. const struct cmd_entry cmd_show_options_entry = {
  30. .name = "show-options",
  31. .alias = "show",
  32. .args = { "gqst:vw", 0, 1 },
  33. .usage = "[-gqsvw] [-t target-session|target-window] [option]",
  34. .tflag = CMD_WINDOW_CANFAIL,
  35. .flags = 0,
  36. .exec = cmd_show_options_exec
  37. };
  38. const struct cmd_entry cmd_show_window_options_entry = {
  39. .name = "show-window-options",
  40. .alias = "showw",
  41. .args = { "gvt:", 0, 1 },
  42. .usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
  43. .tflag = CMD_WINDOW_CANFAIL,
  44. .flags = 0,
  45. .exec = cmd_show_options_exec
  46. };
  47. enum cmd_retval
  48. cmd_show_options_exec(struct cmd *self, struct cmd_q *cmdq)
  49. {
  50. struct args *args = self->args;
  51. struct session *s = cmdq->state.tflag.s;
  52. struct winlink *wl = cmdq->state.tflag.wl;
  53. struct options *oo;
  54. enum options_table_scope scope;
  55. int quiet;
  56. const char *target;
  57. if (args_has(self->args, 's')) {
  58. oo = global_options;
  59. scope = OPTIONS_TABLE_SERVER;
  60. } else if (args_has(self->args, 'w') ||
  61. self->entry == &cmd_show_window_options_entry) {
  62. scope = OPTIONS_TABLE_WINDOW;
  63. if (args_has(self->args, 'g'))
  64. oo = global_w_options;
  65. else if (wl == NULL) {
  66. target = args_get(args, 't');
  67. if (target != NULL) {
  68. cmdq_error(cmdq, "no such window: %s", target);
  69. } else
  70. cmdq_error(cmdq, "no current window");
  71. return (CMD_RETURN_ERROR);
  72. } else
  73. oo = wl->window->options;
  74. } else {
  75. scope = OPTIONS_TABLE_SESSION;
  76. if (args_has(self->args, 'g'))
  77. oo = global_s_options;
  78. else if (s == NULL) {
  79. target = args_get(args, 't');
  80. if (target != NULL) {
  81. cmdq_error(cmdq, "no such session: %s", target);
  82. } else
  83. cmdq_error(cmdq, "no current session");
  84. return (CMD_RETURN_ERROR);
  85. } else
  86. oo = s->options;
  87. }
  88. quiet = args_has(self->args, 'q');
  89. if (args->argc == 0)
  90. return (cmd_show_options_all(self, cmdq, oo, scope));
  91. else
  92. return (cmd_show_options_one(self, cmdq, oo, quiet));
  93. }
  94. enum cmd_retval
  95. cmd_show_options_one(struct cmd *self, struct cmd_q *cmdq,
  96. struct options *oo, int quiet)
  97. {
  98. struct args *args = self->args;
  99. const char *name = args->argv[0];
  100. const struct options_table_entry *oe;
  101. struct options_entry *o;
  102. const char *optval;
  103. retry:
  104. if (*name == '@') {
  105. if ((o = options_find1(oo, name)) == NULL) {
  106. if (quiet)
  107. return (CMD_RETURN_NORMAL);
  108. cmdq_error(cmdq, "unknown option: %s", name);
  109. return (CMD_RETURN_ERROR);
  110. }
  111. if (args_has(self->args, 'v'))
  112. cmdq_print(cmdq, "%s", o->str);
  113. else
  114. cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
  115. return (CMD_RETURN_NORMAL);
  116. }
  117. oe = NULL;
  118. if (options_table_find(name, &oe) != 0) {
  119. cmdq_error(cmdq, "ambiguous option: %s", name);
  120. return (CMD_RETURN_ERROR);
  121. }
  122. if (oe == NULL) {
  123. if (quiet)
  124. return (CMD_RETURN_NORMAL);
  125. cmdq_error(cmdq, "unknown option: %s", name);
  126. return (CMD_RETURN_ERROR);
  127. }
  128. if (oe->style != NULL) {
  129. name = oe->style;
  130. goto retry;
  131. }
  132. if ((o = options_find1(oo, oe->name)) == NULL)
  133. return (CMD_RETURN_NORMAL);
  134. optval = options_table_print_entry(oe, o, args_has(self->args, 'v'));
  135. if (args_has(self->args, 'v'))
  136. cmdq_print(cmdq, "%s", optval);
  137. else
  138. cmdq_print(cmdq, "%s %s", oe->name, optval);
  139. return (CMD_RETURN_NORMAL);
  140. }
  141. enum cmd_retval
  142. cmd_show_options_all(struct cmd *self, struct cmd_q *cmdq, struct options *oo,
  143. enum options_table_scope scope)
  144. {
  145. const struct options_table_entry *oe;
  146. struct options_entry *o;
  147. const char *optval;
  148. int vflag;
  149. o = options_first(oo);
  150. while (o != NULL) {
  151. if (*o->name == '@') {
  152. if (args_has(self->args, 'v'))
  153. cmdq_print(cmdq, "%s", o->str);
  154. else
  155. cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
  156. }
  157. o = options_next(o);
  158. }
  159. vflag = args_has(self->args, 'v');
  160. for (oe = options_table; oe->name != NULL; oe++) {
  161. if (oe->style != NULL || oe->scope != scope)
  162. continue;
  163. if ((o = options_find1(oo, oe->name)) == NULL)
  164. continue;
  165. optval = options_table_print_entry(oe, o, vflag);
  166. if (vflag)
  167. cmdq_print(cmdq, "%s", optval);
  168. else
  169. cmdq_print(cmdq, "%s %s", oe->name, optval);
  170. }
  171. return (CMD_RETURN_NORMAL);
  172. }