| 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 | ret = config_init(opt_session_name); |
| 64 | if (ret < 0) { |
| 65 | ERR("Unable to set session name"); |
| 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(stdout); |
| 92 | goto end; |
| 93 | case OPT_LIST_OPTIONS: |
| 94 | list_cmd_options(stdout, long_options); |
| 95 | goto end; |
| 96 | default: |
| 97 | usage(stderr); |
| 98 | ret = CMD_UNDEFINED; |
| 99 | goto end; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | opt_session_name = (char *) poptGetArg(pc); |
| 104 | if (opt_session_name == NULL) { |
| 105 | ERR("Missing session name"); |
| 106 | usage(stderr); |
| 107 | ret = CMD_ERROR; |
| 108 | goto end; |
| 109 | } |
| 110 | |
| 111 | ret = set_session(); |
| 112 | |
| 113 | end: |
| 114 | poptFreeContext(pc); |
| 115 | return ret; |
| 116 | } |