cmd-list-clients.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <time.h>
  21. #include "tmux.h"
  22. /*
  23. * List all clients.
  24. */
  25. #define LIST_CLIENTS_TEMPLATE \
  26. "#{client_tty}: #{session_name} " \
  27. "[#{client_width}x#{client_height} #{client_termname}]" \
  28. "#{?client_utf8, (utf8),} #{?client_readonly, (ro),}"
  29. enum cmd_retval cmd_list_clients_exec(struct cmd *, struct cmd_q *);
  30. const struct cmd_entry cmd_list_clients_entry = {
  31. .name = "list-clients",
  32. .alias = "lsc",
  33. .args = { "F:t:", 0, 0 },
  34. .usage = "[-F format] " CMD_TARGET_SESSION_USAGE,
  35. .tflag = CMD_SESSION,
  36. .flags = CMD_READONLY,
  37. .exec = cmd_list_clients_exec
  38. };
  39. enum cmd_retval
  40. cmd_list_clients_exec(struct cmd *self, struct cmd_q *cmdq)
  41. {
  42. struct args *args = self->args;
  43. struct client *c;
  44. struct session *s;
  45. struct format_tree *ft;
  46. const char *template;
  47. u_int idx;
  48. char *line;
  49. if (args_has(args, 't'))
  50. s = cmdq->state.tflag.s;
  51. else
  52. s = NULL;
  53. if ((template = args_get(args, 'F')) == NULL)
  54. template = LIST_CLIENTS_TEMPLATE;
  55. idx = 0;
  56. TAILQ_FOREACH(c, &clients, entry) {
  57. if (c->session == NULL || (s != NULL && s != c->session))
  58. continue;
  59. ft = format_create(cmdq, 0);
  60. format_add(ft, "line", "%u", idx);
  61. format_defaults(ft, c, NULL, NULL, NULL);
  62. line = format_expand(ft, template);
  63. cmdq_print(cmdq, "%s", line);
  64. free(line);
  65. format_free(ft);
  66. idx++;
  67. }
  68. return (CMD_RETURN_NORMAL);
  69. }