cmd-source-file.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2008 Tiago Cunha <me@tiagocunha.org>
  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 "tmux.h"
  20. /*
  21. * Sources a configuration file.
  22. */
  23. enum cmd_retval cmd_source_file_exec(struct cmd *, struct cmd_q *);
  24. void cmd_source_file_done(struct cmd_q *);
  25. const struct cmd_entry cmd_source_file_entry = {
  26. .name = "source-file",
  27. .alias = "source",
  28. .args = { "", 1, 1 },
  29. .usage = "path",
  30. .flags = 0,
  31. .exec = cmd_source_file_exec
  32. };
  33. enum cmd_retval
  34. cmd_source_file_exec(struct cmd *self, struct cmd_q *cmdq)
  35. {
  36. struct args *args = self->args;
  37. struct cmd_q *cmdq1;
  38. char *cause;
  39. cmdq1 = cmdq_new(cmdq->client);
  40. cmdq1->emptyfn = cmd_source_file_done;
  41. cmdq1->data = cmdq;
  42. switch (load_cfg(args->argv[0], cmdq1, &cause)) {
  43. case -1:
  44. if (cfg_references == 0) {
  45. cmdq_free(cmdq1);
  46. cmdq_error(cmdq, "%s", cause);
  47. free(cause);
  48. return (CMD_RETURN_ERROR);
  49. }
  50. cfg_add_cause("%s", cause);
  51. free(cause);
  52. /* FALLTHROUGH */
  53. case 0:
  54. if (cfg_references == 0)
  55. cfg_print_causes(cmdq);
  56. cmdq_free(cmdq1);
  57. return (CMD_RETURN_NORMAL);
  58. }
  59. cmdq->references++;
  60. cfg_references++;
  61. cmdq_continue(cmdq1);
  62. return (CMD_RETURN_WAIT);
  63. }
  64. void
  65. cmd_source_file_done(struct cmd_q *cmdq1)
  66. {
  67. struct cmd_q *cmdq = cmdq1->data;
  68. if (cmdq1->client_exit >= 0)
  69. cmdq->client_exit = cmdq1->client_exit;
  70. cmdq_free(cmdq1);
  71. cfg_references--;
  72. if (cmdq_free(cmdq))
  73. return;
  74. if (cfg_references == 0)
  75. cfg_print_causes(cmdq);
  76. cmdq_continue(cmdq);
  77. }