Commit | Line | Data |
---|---|---|
3087ef18 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
3087ef18 DG |
7 | * |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
3087ef18 DG |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #include <popt.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <sys/stat.h> | |
24 | #include <sys/types.h> | |
25 | #include <unistd.h> | |
26 | ||
c399183f | 27 | #include "../command.h" |
3087ef18 DG |
28 | |
29 | static char *opt_session_name; | |
30 | ||
31 | enum { | |
32 | OPT_HELP = 1, | |
679b4943 | 33 | OPT_LIST_OPTIONS, |
3087ef18 DG |
34 | }; |
35 | ||
36 | static struct poptOption long_options[] = { | |
37 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
38 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
679b4943 | 39 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
3087ef18 DG |
40 | {0, 0, 0, 0, 0, 0, 0} |
41 | }; | |
42 | ||
43 | /* | |
44 | * usage | |
45 | */ | |
46 | static void usage(FILE *ofp) | |
47 | { | |
7c96a096 | 48 | fprintf(ofp, "usage: lttng set-session NAME [OPTIONS]\n"); |
3087ef18 DG |
49 | fprintf(ofp, "\n"); |
50 | fprintf(ofp, "Options:\n"); | |
51 | fprintf(ofp, " -h, --help Show this help\n"); | |
679b4943 | 52 | fprintf(ofp, " --list-options Simple listing of options\n"); |
3087ef18 DG |
53 | fprintf(ofp, "\n"); |
54 | } | |
55 | ||
56 | /* | |
57 | * set_session | |
58 | */ | |
59 | static int set_session(void) | |
60 | { | |
61 | int ret = CMD_SUCCESS; | |
3087ef18 | 62 | |
487b253b DG |
63 | if (opt_session_name && strlen(opt_session_name) > NAME_MAX) { |
64 | ERR("Session name too long. Length must be lower or equal to %d", | |
65 | NAME_MAX); | |
66 | ret = CMD_ERROR; | |
67 | goto error; | |
68 | } | |
69 | ||
fffd0547 | 70 | ret = config_init(opt_session_name); |
3087ef18 | 71 | if (ret < 0) { |
fffd0547 | 72 | ERR("Unable to set session name"); |
3087ef18 DG |
73 | ret = CMD_ERROR; |
74 | goto error; | |
75 | } | |
76 | ||
77 | MSG("Session set to %s", opt_session_name); | |
78 | ret = CMD_SUCCESS; | |
79 | ||
80 | error: | |
81 | return ret; | |
82 | } | |
83 | ||
84 | /* | |
85 | * cmd_set_session | |
86 | */ | |
87 | int cmd_set_session(int argc, const char **argv) | |
88 | { | |
89 | int opt, ret = CMD_SUCCESS; | |
90 | static poptContext pc; | |
91 | ||
92 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
93 | poptReadDefaultConfig(pc, 0); | |
94 | ||
95 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
96 | switch (opt) { | |
97 | case OPT_HELP: | |
ca1c3607 | 98 | usage(stdout); |
3087ef18 | 99 | goto end; |
679b4943 SM |
100 | case OPT_LIST_OPTIONS: |
101 | list_cmd_options(stdout, long_options); | |
679b4943 | 102 | goto end; |
3087ef18 DG |
103 | default: |
104 | usage(stderr); | |
105 | ret = CMD_UNDEFINED; | |
106 | goto end; | |
107 | } | |
108 | } | |
109 | ||
110 | opt_session_name = (char *) poptGetArg(pc); | |
111 | if (opt_session_name == NULL) { | |
112 | ERR("Missing session name"); | |
113 | usage(stderr); | |
ca1c3607 | 114 | ret = CMD_ERROR; |
3087ef18 DG |
115 | goto end; |
116 | } | |
117 | ||
118 | ret = set_session(); | |
119 | ||
120 | end: | |
ca1c3607 | 121 | poptFreeContext(pc); |
3087ef18 DG |
122 | return ret; |
123 | } |