control.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2012 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. * Copyright (c) 2012 George Nachman <tmux@georgester.com>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  15. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  16. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/types.h>
  19. #include <event.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include "tmux.h"
  24. /* Write a line. */
  25. void
  26. control_write(struct client *c, const char *fmt, ...)
  27. {
  28. va_list ap;
  29. va_start(ap, fmt);
  30. evbuffer_add_vprintf(c->stdout_data, fmt, ap);
  31. va_end(ap);
  32. evbuffer_add(c->stdout_data, "\n", 1);
  33. server_client_push_stdout(c);
  34. }
  35. /* Write a buffer, adding a terminal newline. Empties buffer. */
  36. void
  37. control_write_buffer(struct client *c, struct evbuffer *buffer)
  38. {
  39. evbuffer_add_buffer(c->stdout_data, buffer);
  40. evbuffer_add(c->stdout_data, "\n", 1);
  41. server_client_push_stdout(c);
  42. }
  43. /* Control input callback. Read lines and fire commands. */
  44. void
  45. control_callback(struct client *c, int closed, __unused void *data)
  46. {
  47. char *line, *cause;
  48. struct cmd_list *cmdlist;
  49. struct cmd *cmd;
  50. if (closed)
  51. c->flags |= CLIENT_EXIT;
  52. for (;;) {
  53. line = evbuffer_readln(c->stdin_data, NULL, EVBUFFER_EOL_LF);
  54. if (line == NULL)
  55. break;
  56. if (*line == '\0') { /* empty line exit */
  57. c->flags |= CLIENT_EXIT;
  58. break;
  59. }
  60. if (cmd_string_parse(line, &cmdlist, NULL, 0, &cause) != 0) {
  61. c->cmdq->time = time(NULL);
  62. c->cmdq->number++;
  63. cmdq_guard(c->cmdq, "begin", 1);
  64. control_write(c, "parse error: %s", cause);
  65. cmdq_guard(c->cmdq, "error", 1);
  66. free(cause);
  67. } else {
  68. TAILQ_FOREACH(cmd, &cmdlist->list, qentry)
  69. cmd->flags |= CMD_CONTROL;
  70. cmdq_run(c->cmdq, cmdlist, NULL);
  71. cmd_list_free(cmdlist);
  72. }
  73. free(line);
  74. }
  75. }