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 | |
6 | * as published by the Free Software Foundation; either version 2 | |
7 | * of the License, or (at your option) any later version. | |
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 | ||
28 | #include "cmd.h" | |
29 | #include "conf.h" | |
30 | #include "utils.h" | |
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; | |
62 | char *path, *alloc_path; | |
63 | ||
64 | alloc_path = config_get_default_path(); | |
65 | if (alloc_path == NULL) { | |
66 | ERR("Unable to find config directory"); | |
67 | ret = CMD_ERROR; | |
68 | goto error; | |
69 | } | |
70 | ||
71 | path = config_generate_dir_path(alloc_path); | |
72 | if (path == NULL) { | |
73 | ret = CMD_FATAL; | |
74 | goto error; | |
75 | } | |
76 | ||
77 | ret = config_init(path); | |
78 | if (ret < 0) { | |
79 | ERR("Init config directory and file failed"); | |
80 | ret = CMD_ERROR; | |
81 | goto error; | |
82 | } | |
83 | ||
84 | ret = config_add_session_name(path, opt_session_name); | |
85 | if (ret < 0) { | |
86 | ERR("Unable to add session name to config"); | |
87 | ret = CMD_ERROR; | |
88 | goto error; | |
89 | } | |
90 | ||
91 | MSG("Session set to %s", opt_session_name); | |
92 | ret = CMD_SUCCESS; | |
93 | ||
94 | error: | |
95 | return ret; | |
96 | } | |
97 | ||
98 | /* | |
99 | * cmd_set_session | |
100 | */ | |
101 | int cmd_set_session(int argc, const char **argv) | |
102 | { | |
103 | int opt, ret = CMD_SUCCESS; | |
104 | static poptContext pc; | |
105 | ||
106 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
107 | poptReadDefaultConfig(pc, 0); | |
108 | ||
109 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
110 | switch (opt) { | |
111 | case OPT_HELP: | |
112 | usage(stderr); | |
113 | ret = CMD_SUCCESS; | |
114 | goto end; | |
115 | default: | |
116 | usage(stderr); | |
117 | ret = CMD_UNDEFINED; | |
118 | goto end; | |
119 | } | |
120 | } | |
121 | ||
122 | opt_session_name = (char *) poptGetArg(pc); | |
123 | if (opt_session_name == NULL) { | |
124 | ERR("Missing session name"); | |
125 | usage(stderr); | |
126 | goto end; | |
127 | } | |
128 | ||
129 | ret = set_session(); | |
130 | ||
131 | end: | |
132 | return ret; | |
133 | } |