| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 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. |
| 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 | * |
| 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. |
| 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 | |
| 27 | #include "../command.h" |
| 28 | |
| 29 | static char *opt_session_name; |
| 30 | |
| 31 | enum { |
| 32 | OPT_HELP = 1, |
| 33 | OPT_LIST_OPTIONS, |
| 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}, |
| 39 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 40 | {0, 0, 0, 0, 0, 0, 0} |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * usage |
| 45 | */ |
| 46 | static void usage(FILE *ofp) |
| 47 | { |
| 48 | fprintf(ofp, "usage: lttng set-session NAME\n"); |
| 49 | fprintf(ofp, "\n"); |
| 50 | fprintf(ofp, "Options:\n"); |
| 51 | fprintf(ofp, " -h, --help Show this help\n"); |
| 52 | fprintf(ofp, " --list-options Simple listing of options\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 | |
| 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 | |
| 70 | ret = config_init(opt_session_name); |
| 71 | if (ret < 0) { |
| 72 | ERR("Unable to set session name"); |
| 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: |
| 98 | usage(stdout); |
| 99 | goto end; |
| 100 | case OPT_LIST_OPTIONS: |
| 101 | list_cmd_options(stdout, long_options); |
| 102 | goto end; |
| 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); |
| 114 | ret = CMD_ERROR; |
| 115 | goto end; |
| 116 | } |
| 117 | |
| 118 | ret = set_session(); |
| 119 | |
| 120 | end: |
| 121 | poptFreeContext(pc); |
| 122 | return ret; |
| 123 | } |