swanctl: Stop logging with Ctrl+C on Windows as well
[strongswan.git] / src / swanctl / commands / list_pools.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include <stdio.h>
18 #include <errno.h>
19
20 #include "command.h"
21
22 CALLBACK(list_pool, int,
23 linked_list_t *list, vici_res_t *res, char *name)
24 {
25 char pool[64], leases[32];
26
27 snprintf(pool, sizeof(pool), "%s:", name);
28 snprintf(leases, sizeof(leases), "%s / %s / %s",
29 vici_find_str(res, "", "%s.online", name),
30 vici_find_str(res, "", "%s.offline", name),
31 vici_find_str(res, "", "%s.size", name));
32
33 printf("%-20s %-30s %16s\n",
34 name, vici_find_str(res, "", "%s.base", name), leases);
35
36 return 0;
37 }
38
39 static int list_pools(vici_conn_t *conn)
40 {
41 vici_req_t *req;
42 vici_res_t *res;
43 bool raw = FALSE;
44 char *arg;
45 int ret = 0;
46
47 while (TRUE)
48 {
49 switch (command_getopt(&arg))
50 {
51 case 'h':
52 return command_usage(NULL);
53 case 'r':
54 raw = TRUE;
55 continue;
56 case EOF:
57 break;
58 default:
59 return command_usage("invalid --list-pools option");
60 }
61 break;
62 }
63
64 req = vici_begin("get-pools");
65 res = vici_submit(req, conn);
66 if (!res)
67 {
68 fprintf(stderr, "get-pools request failed: %s\n", strerror(errno));
69 return errno;
70 }
71 if (raw)
72 {
73 vici_dump(res, "get-pools reply", stdout);
74 }
75 else
76 {
77 ret = vici_parse_cb(res, list_pool, NULL, NULL, NULL);
78 }
79 vici_free_res(res);
80 return ret;
81 }
82
83 /**
84 * Register the command.
85 */
86 static void __attribute__ ((constructor))reg()
87 {
88 command_register((command_t) {
89 list_pools, 'A', "list-pools", "list loaded pool configurations",
90 {"[--raw]"},
91 {
92 {"help", 'h', 0, "show usage information"},
93 {"raw", 'r', 0, "dump raw response message"},
94 }
95 });
96 }