cmd-set-option.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. * Set an option.
  23. */
  24. enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmd_q *);
  25. enum cmd_retval cmd_set_option_user(struct cmd *, struct cmd_q *,
  26. const char *, const char *);
  27. int cmd_set_option_unset(struct cmd *, struct cmd_q *,
  28. const struct options_table_entry *, struct options *,
  29. const char *);
  30. int cmd_set_option_set(struct cmd *, struct cmd_q *,
  31. const struct options_table_entry *, struct options *,
  32. const char *);
  33. struct options_entry *cmd_set_option_string(struct cmd *, struct cmd_q *,
  34. const struct options_table_entry *, struct options *,
  35. const char *);
  36. struct options_entry *cmd_set_option_number(struct cmd *, struct cmd_q *,
  37. const struct options_table_entry *, struct options *,
  38. const char *);
  39. struct options_entry *cmd_set_option_key(struct cmd *, struct cmd_q *,
  40. const struct options_table_entry *, struct options *,
  41. const char *);
  42. struct options_entry *cmd_set_option_colour(struct cmd *, struct cmd_q *,
  43. const struct options_table_entry *, struct options *,
  44. const char *);
  45. struct options_entry *cmd_set_option_attributes(struct cmd *, struct cmd_q *,
  46. const struct options_table_entry *, struct options *,
  47. const char *);
  48. struct options_entry *cmd_set_option_flag(struct cmd *, struct cmd_q *,
  49. const struct options_table_entry *, struct options *,
  50. const char *);
  51. struct options_entry *cmd_set_option_choice(struct cmd *, struct cmd_q *,
  52. const struct options_table_entry *, struct options *,
  53. const char *);
  54. struct options_entry *cmd_set_option_style(struct cmd *, struct cmd_q *,
  55. const struct options_table_entry *, struct options *,
  56. const char *);
  57. const struct cmd_entry cmd_set_option_entry = {
  58. .name = "set-option",
  59. .alias = "set",
  60. .args = { "agoqst:uw", 1, 2 },
  61. .usage = "[-agosquw] [-t target-window] option [value]",
  62. .tflag = CMD_WINDOW_CANFAIL,
  63. .flags = 0,
  64. .exec = cmd_set_option_exec
  65. };
  66. const struct cmd_entry cmd_set_window_option_entry = {
  67. .name = "set-window-option",
  68. .alias = "setw",
  69. .args = { "agoqt:u", 1, 2 },
  70. .usage = "[-agoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
  71. .tflag = CMD_WINDOW_CANFAIL,
  72. .flags = 0,
  73. .exec = cmd_set_option_exec
  74. };
  75. enum cmd_retval
  76. cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq)
  77. {
  78. struct args *args = self->args;
  79. struct session *s = cmdq->state.tflag.s;
  80. struct winlink *wl = cmdq->state.tflag.wl;
  81. struct window *w;
  82. struct client *c;
  83. const struct options_table_entry *oe;
  84. struct options *oo;
  85. const char *optstr, *valstr, *target;
  86. /* Get the option name and value. */
  87. optstr = args->argv[0];
  88. if (*optstr == '\0') {
  89. cmdq_error(cmdq, "invalid option");
  90. return (CMD_RETURN_ERROR);
  91. }
  92. if (args->argc < 2)
  93. valstr = NULL;
  94. else
  95. valstr = args->argv[1];
  96. /* Is this a user option? */
  97. if (*optstr == '@')
  98. return (cmd_set_option_user(self, cmdq, optstr, valstr));
  99. /* Find the option entry, try each table. */
  100. oe = NULL;
  101. if (options_table_find(optstr, &oe) != 0) {
  102. if (!args_has(args, 'q')) {
  103. cmdq_error(cmdq, "ambiguous option: %s", optstr);
  104. return (CMD_RETURN_ERROR);
  105. }
  106. return (CMD_RETURN_NORMAL);
  107. }
  108. if (oe == NULL) {
  109. if (!args_has(args, 'q')) {
  110. cmdq_error(cmdq, "unknown option: %s", optstr);
  111. return (CMD_RETURN_ERROR);
  112. }
  113. return (CMD_RETURN_NORMAL);
  114. }
  115. /* Work out the tree from the scope of the option. */
  116. if (oe->scope == OPTIONS_TABLE_SERVER)
  117. oo = global_options;
  118. else if (oe->scope == OPTIONS_TABLE_WINDOW) {
  119. if (args_has(self->args, 'g'))
  120. oo = global_w_options;
  121. else if (wl == NULL) {
  122. target = args_get(args, 't');
  123. if (target != NULL) {
  124. cmdq_error(cmdq, "no such window: %s",
  125. target);
  126. } else
  127. cmdq_error(cmdq, "no current window");
  128. return (CMD_RETURN_ERROR);
  129. } else
  130. oo = wl->window->options;
  131. } else if (oe->scope == OPTIONS_TABLE_SESSION) {
  132. if (args_has(self->args, 'g'))
  133. oo = global_s_options;
  134. else if (s == NULL) {
  135. target = args_get(args, 't');
  136. if (target != NULL) {
  137. cmdq_error(cmdq, "no such session: %s",
  138. target);
  139. } else
  140. cmdq_error(cmdq, "no current session");
  141. return (CMD_RETURN_ERROR);
  142. } else
  143. oo = s->options;
  144. } else {
  145. cmdq_error(cmdq, "unknown table");
  146. return (CMD_RETURN_ERROR);
  147. }
  148. /* Unset or set the option. */
  149. if (args_has(args, 'u')) {
  150. if (cmd_set_option_unset(self, cmdq, oe, oo, valstr) != 0)
  151. return (CMD_RETURN_ERROR);
  152. } else {
  153. if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) {
  154. if (!args_has(args, 'q')) {
  155. cmdq_error(cmdq, "already set: %s", optstr);
  156. return (CMD_RETURN_ERROR);
  157. }
  158. return (CMD_RETURN_NORMAL);
  159. }
  160. if (cmd_set_option_set(self, cmdq, oe, oo, valstr) != 0)
  161. return (CMD_RETURN_ERROR);
  162. }
  163. /* Start or stop timers if necessary. */
  164. if (strcmp(oe->name, "automatic-rename") == 0) {
  165. RB_FOREACH(w, windows, &windows) {
  166. if (options_get_number(w->options, "automatic-rename"))
  167. w->active->flags |= PANE_CHANGED;
  168. }
  169. }
  170. if (strcmp(oe->name, "key-table") == 0) {
  171. TAILQ_FOREACH(c, &clients, entry)
  172. server_client_set_key_table(c, NULL);
  173. }
  174. if (strcmp(oe->name, "status") == 0 ||
  175. strcmp(oe->name, "status-interval") == 0)
  176. status_timer_start_all();
  177. if (strcmp(oe->name, "monitor-silence") == 0)
  178. alerts_reset_all();
  179. /* Update sizes and redraw. May not need it but meh. */
  180. recalculate_sizes();
  181. TAILQ_FOREACH(c, &clients, entry) {
  182. if (c->session != NULL)
  183. server_redraw_client(c);
  184. }
  185. return (CMD_RETURN_NORMAL);
  186. }
  187. /* Set user option. */
  188. enum cmd_retval
  189. cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char *optstr,
  190. const char *valstr)
  191. {
  192. struct args *args = self->args;
  193. struct session *s = cmdq->state.tflag.s;
  194. struct winlink *wl = cmdq->state.tflag.wl;
  195. struct options *oo;
  196. if (args_has(args, 's'))
  197. oo = global_options;
  198. else if (args_has(self->args, 'w') ||
  199. self->entry == &cmd_set_window_option_entry) {
  200. if (args_has(self->args, 'g'))
  201. oo = global_w_options;
  202. else
  203. oo = wl->window->options;
  204. } else {
  205. if (args_has(self->args, 'g'))
  206. oo = global_s_options;
  207. else
  208. oo = s->options;
  209. }
  210. if (args_has(args, 'u')) {
  211. if (options_find1(oo, optstr) == NULL) {
  212. if (!args_has(args, 'q')) {
  213. cmdq_error(cmdq, "unknown option: %s", optstr);
  214. return (CMD_RETURN_ERROR);
  215. }
  216. return (CMD_RETURN_NORMAL);
  217. }
  218. if (valstr != NULL) {
  219. cmdq_error(cmdq, "value passed to unset option: %s",
  220. optstr);
  221. return (CMD_RETURN_ERROR);
  222. }
  223. options_remove(oo, optstr);
  224. } else {
  225. if (valstr == NULL) {
  226. cmdq_error(cmdq, "empty value");
  227. return (CMD_RETURN_ERROR);
  228. }
  229. if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) {
  230. if (!args_has(args, 'q')) {
  231. cmdq_error(cmdq, "already set: %s", optstr);
  232. return (CMD_RETURN_ERROR);
  233. }
  234. return (CMD_RETURN_NORMAL);
  235. }
  236. options_set_string(oo, optstr, "%s", valstr);
  237. }
  238. return (CMD_RETURN_NORMAL);
  239. }
  240. /* Unset an option. */
  241. int
  242. cmd_set_option_unset(struct cmd *self, struct cmd_q *cmdq,
  243. const struct options_table_entry *oe, struct options *oo,
  244. const char *value)
  245. {
  246. struct args *args = self->args;
  247. if (value != NULL) {
  248. cmdq_error(cmdq, "value passed to unset option: %s", oe->name);
  249. return (-1);
  250. }
  251. if (args_has(args, 'g') || oo == global_options) {
  252. switch (oe->type) {
  253. case OPTIONS_TABLE_STRING:
  254. options_set_string(oo, oe->name, "%s", oe->default_str);
  255. break;
  256. case OPTIONS_TABLE_STYLE:
  257. options_set_style(oo, oe->name, oe->default_str, 0);
  258. break;
  259. default:
  260. options_set_number(oo, oe->name, oe->default_num);
  261. break;
  262. }
  263. } else
  264. options_remove(oo, oe->name);
  265. return (0);
  266. }
  267. /* Set an option. */
  268. int
  269. cmd_set_option_set(struct cmd *self, struct cmd_q *cmdq,
  270. const struct options_table_entry *oe, struct options *oo,
  271. const char *value)
  272. {
  273. struct options_entry *o;
  274. switch (oe->type) {
  275. case OPTIONS_TABLE_FLAG:
  276. case OPTIONS_TABLE_CHOICE:
  277. break;
  278. default:
  279. if (value == NULL) {
  280. cmdq_error(cmdq, "empty value");
  281. return (-1);
  282. }
  283. }
  284. o = NULL;
  285. switch (oe->type) {
  286. case OPTIONS_TABLE_STRING:
  287. o = cmd_set_option_string(self, cmdq, oe, oo, value);
  288. break;
  289. case OPTIONS_TABLE_NUMBER:
  290. o = cmd_set_option_number(self, cmdq, oe, oo, value);
  291. break;
  292. case OPTIONS_TABLE_KEY:
  293. o = cmd_set_option_key(self, cmdq, oe, oo, value);
  294. break;
  295. case OPTIONS_TABLE_COLOUR:
  296. o = cmd_set_option_colour(self, cmdq, oe, oo, value);
  297. if (o != NULL)
  298. style_update_new(oo, o->name, oe->style);
  299. break;
  300. case OPTIONS_TABLE_ATTRIBUTES:
  301. o = cmd_set_option_attributes(self, cmdq, oe, oo, value);
  302. if (o != NULL)
  303. style_update_new(oo, o->name, oe->style);
  304. break;
  305. case OPTIONS_TABLE_FLAG:
  306. o = cmd_set_option_flag(self, cmdq, oe, oo, value);
  307. break;
  308. case OPTIONS_TABLE_CHOICE:
  309. o = cmd_set_option_choice(self, cmdq, oe, oo, value);
  310. break;
  311. case OPTIONS_TABLE_STYLE:
  312. o = cmd_set_option_style(self, cmdq, oe, oo, value);
  313. break;
  314. }
  315. if (o == NULL)
  316. return (-1);
  317. return (0);
  318. }
  319. /* Set a string option. */
  320. struct options_entry *
  321. cmd_set_option_string(struct cmd *self, __unused struct cmd_q *cmdq,
  322. const struct options_table_entry *oe, struct options *oo,
  323. const char *value)
  324. {
  325. struct args *args = self->args;
  326. struct options_entry *o;
  327. char *oldval, *newval;
  328. if (args_has(args, 'a')) {
  329. oldval = options_get_string(oo, oe->name);
  330. xasprintf(&newval, "%s%s", oldval, value);
  331. } else
  332. newval = xstrdup(value);
  333. o = options_set_string(oo, oe->name, "%s", newval);
  334. free(newval);
  335. return (o);
  336. }
  337. /* Set a number option. */
  338. struct options_entry *
  339. cmd_set_option_number(__unused struct cmd *self, struct cmd_q *cmdq,
  340. const struct options_table_entry *oe, struct options *oo,
  341. const char *value)
  342. {
  343. long long ll;
  344. const char *errstr;
  345. ll = strtonum(value, oe->minimum, oe->maximum, &errstr);
  346. if (errstr != NULL) {
  347. cmdq_error(cmdq, "value is %s: %s", errstr, value);
  348. return (NULL);
  349. }
  350. return (options_set_number(oo, oe->name, ll));
  351. }
  352. /* Set a key option. */
  353. struct options_entry *
  354. cmd_set_option_key(__unused struct cmd *self, struct cmd_q *cmdq,
  355. const struct options_table_entry *oe, struct options *oo,
  356. const char *value)
  357. {
  358. key_code key;
  359. key = key_string_lookup_string(value);
  360. if (key == KEYC_UNKNOWN) {
  361. cmdq_error(cmdq, "bad key: %s", value);
  362. return (NULL);
  363. }
  364. return (options_set_number(oo, oe->name, key));
  365. }
  366. /* Set a colour option. */
  367. struct options_entry *
  368. cmd_set_option_colour(__unused struct cmd *self, struct cmd_q *cmdq,
  369. const struct options_table_entry *oe, struct options *oo,
  370. const char *value)
  371. {
  372. int colour;
  373. if ((colour = colour_fromstring(value)) == -1) {
  374. cmdq_error(cmdq, "bad colour: %s", value);
  375. return (NULL);
  376. }
  377. return (options_set_number(oo, oe->name, colour));
  378. }
  379. /* Set an attributes option. */
  380. struct options_entry *
  381. cmd_set_option_attributes(__unused struct cmd *self, struct cmd_q *cmdq,
  382. const struct options_table_entry *oe, struct options *oo,
  383. const char *value)
  384. {
  385. int attr;
  386. if ((attr = attributes_fromstring(value)) == -1) {
  387. cmdq_error(cmdq, "bad attributes: %s", value);
  388. return (NULL);
  389. }
  390. return (options_set_number(oo, oe->name, attr));
  391. }
  392. /* Set a flag option. */
  393. struct options_entry *
  394. cmd_set_option_flag(__unused struct cmd *self, struct cmd_q *cmdq,
  395. const struct options_table_entry *oe, struct options *oo,
  396. const char *value)
  397. {
  398. int flag;
  399. if (value == NULL || *value == '\0')
  400. flag = !options_get_number(oo, oe->name);
  401. else {
  402. if ((value[0] == '1' && value[1] == '\0') ||
  403. strcasecmp(value, "on") == 0 ||
  404. strcasecmp(value, "yes") == 0)
  405. flag = 1;
  406. else if ((value[0] == '0' && value[1] == '\0') ||
  407. strcasecmp(value, "off") == 0 ||
  408. strcasecmp(value, "no") == 0)
  409. flag = 0;
  410. else {
  411. cmdq_error(cmdq, "bad value: %s", value);
  412. return (NULL);
  413. }
  414. }
  415. return (options_set_number(oo, oe->name, flag));
  416. }
  417. /* Set a choice option. */
  418. struct options_entry *
  419. cmd_set_option_choice(__unused struct cmd *self, struct cmd_q *cmdq,
  420. const struct options_table_entry *oe, struct options *oo,
  421. const char *value)
  422. {
  423. const char **choicep;
  424. int n, choice = -1;
  425. if (value == NULL) {
  426. choice = options_get_number(oo, oe->name);
  427. if (choice < 2)
  428. choice = !choice;
  429. } else {
  430. n = 0;
  431. for (choicep = oe->choices; *choicep != NULL; choicep++) {
  432. n++;
  433. if (strncmp(*choicep, value, strlen(value)) != 0)
  434. continue;
  435. if (choice != -1) {
  436. cmdq_error(cmdq, "ambiguous value: %s", value);
  437. return (NULL);
  438. }
  439. choice = n - 1;
  440. }
  441. if (choice == -1) {
  442. cmdq_error(cmdq, "unknown value: %s", value);
  443. return (NULL);
  444. }
  445. }
  446. return (options_set_number(oo, oe->name, choice));
  447. }
  448. /* Set a style option. */
  449. struct options_entry *
  450. cmd_set_option_style(struct cmd *self, struct cmd_q *cmdq,
  451. const struct options_table_entry *oe, struct options *oo,
  452. const char *value)
  453. {
  454. struct args *args = self->args;
  455. struct options_entry *o;
  456. int append;
  457. append = args_has(args, 'a');
  458. if ((o = options_set_style(oo, oe->name, value, append)) == NULL) {
  459. cmdq_error(cmdq, "bad style: %s", value);
  460. return (NULL);
  461. }
  462. style_update_old(oo, oe->name, &o->style);
  463. return (o);
  464. }