1234353Sdim/* $OpenBSD$ */
2193323Sed
3193323Sed/*
4193323Sed * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5193323Sed *
6193323Sed * Permission to use, copy, modify, and distribute this software for any
7193323Sed * purpose with or without fee is hereby granted, provided that the above
8193323Sed * copyright notice and this permission notice appear in all copies.
9193323Sed *
10224145Sdim * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11193323Sed * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12193323Sed * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13193323Sed * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14251662Sdim * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15251662Sdim * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16251662Sdim * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17193323Sed */
18251662Sdim
19193323Sed#include <sys/types.h>
20234353Sdim
21251662Sdim#include <stdlib.h>
22251662Sdim#include <string.h>
23251662Sdim#include <time.h>
24251662Sdim
25226633Sdim#include "tmux.h"
26251662Sdim
27224145Sdim/*
28224145Sdim * List all sessions.
29224145Sdim */
30224145Sdim
31224145Sdim#define LIST_SESSIONS_TEMPLATE				\
32251662Sdim	"#{session_name}: #{session_windows} windows "	\
33193323Sed	"(created #{t:session_created})"		\
34193323Sed	"#{?session_grouped, (group ,}"			\
35251662Sdim	"#{session_group}#{?session_grouped,),}"	\
36251662Sdim	"#{?session_attached, (attached),}"
37251662Sdim
38251662Sdimstatic enum cmd_retval	cmd_list_sessions_exec(struct cmd *,
39251662Sdim			    struct cmdq_item *);
40251662Sdim
41251662Sdimconst struct cmd_entry cmd_list_sessions_entry = {
42251662Sdim	.name = "list-sessions",
43251662Sdim	.alias = "ls",
44251662Sdim
45251662Sdim	.args = { "F:f:", 0, 0, NULL },
46251662Sdim	.usage = "[-F format] [-f filter]",
47251662Sdim
48251662Sdim	.flags = CMD_AFTERHOOK,
49251662Sdim	.exec = cmd_list_sessions_exec
50251662Sdim};
51263508Sdim
52263508Sdimstatic enum cmd_retval
53263508Sdimcmd_list_sessions_exec(struct cmd *self, struct cmdq_item *item)
54263508Sdim{
55263508Sdim	struct args		*args = cmd_get_args(self);
56263508Sdim	struct session		*s;
57263508Sdim	u_int		 	 n;
58263508Sdim	struct format_tree	*ft;
59263508Sdim	const char		*template, *filter;
60263508Sdim	char			*line, *expanded;
61263508Sdim	int			 flag;
62234353Sdim
63234353Sdim	if ((template = args_get(args, 'F')) == NULL)
64224145Sdim		template = LIST_SESSIONS_TEMPLATE;
65243830Sdim	filter = args_get(args, 'f');
66251662Sdim
67224145Sdim	n = 0;
68234353Sdim	RB_FOREACH(s, sessions, &sessions) {
69226633Sdim		ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
70249423Sdim		format_add(ft, "line", "%u", n);
71249423Sdim		format_defaults(ft, NULL, s, NULL, NULL);
72263508Sdim
73263508Sdim		if (filter != NULL) {
74263508Sdim			expanded = format_expand(ft, filter);
75251662Sdim			flag = format_true(expanded);
76193323Sed			free(expanded);
77224145Sdim		} else
78224145Sdim			flag = 1;
79234353Sdim		if (flag) {
80193323Sed			line = format_expand(ft, template);
81193323Sed			cmdq_print(item, "%s", line);
82224145Sdim			free(line);
83193323Sed		}
84251662Sdim
85251662Sdim		format_free(ft);
86224145Sdim		n++;
87224145Sdim	}
88224145Sdim
89226633Sdim	return (CMD_RETURN_NORMAL);
90226633Sdim}
91234353Sdim