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