| 1 | /* |
| 2 | * babeltrace.c |
| 3 | * |
| 4 | * Babeltrace Trace Converter |
| 5 | * |
| 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | */ |
| 18 | |
| 19 | #include <babeltrace/babeltrace.h> |
| 20 | #include <babeltrace/format.h> |
| 21 | #include <popt.h> |
| 22 | #include <errno.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | static const char *opt_input_format; |
| 26 | static const char *opt_output_format; |
| 27 | |
| 28 | static const char *opt_input_path; |
| 29 | static const char *opt_output_path; |
| 30 | |
| 31 | int babeltrace_verbose, babeltrace_debug; |
| 32 | |
| 33 | enum { |
| 34 | OPT_NONE = 0, |
| 35 | OPT_HELP, |
| 36 | OPT_LIST, |
| 37 | OPT_VERBOSE, |
| 38 | OPT_DEBUG, |
| 39 | }; |
| 40 | |
| 41 | static struct poptOption long_options[] = { |
| 42 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 43 | { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL }, |
| 44 | { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL }, |
| 45 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, |
| 46 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, |
| 47 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, |
| 48 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, |
| 49 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
| 50 | }; |
| 51 | |
| 52 | static void list_formats(FILE *fp) |
| 53 | { |
| 54 | fprintf(fp, "\n"); |
| 55 | bt_fprintf_format_list(fp); |
| 56 | } |
| 57 | |
| 58 | static void usage(FILE *fp) |
| 59 | { |
| 60 | fprintf(fp, "Babeltrace %u.%u\n\n", BABELTRACE_VERSION_MAJOR, |
| 61 | BABELTRACE_VERSION_MINOR); |
| 62 | fprintf(fp, "usage : babeltrace [OPTIONS] INPUT OUTPUT\n"); |
| 63 | fprintf(fp, "\n"); |
| 64 | fprintf(fp, " INPUT Input trace path\n"); |
| 65 | fprintf(fp, " OUTPUT Output trace path\n"); |
| 66 | fprintf(fp, "\n"); |
| 67 | fprintf(fp, " -i, --input-format Input trace path\n"); |
| 68 | fprintf(fp, " -o, --output-format Input trace path\n"); |
| 69 | fprintf(fp, "\n"); |
| 70 | fprintf(fp, " -h, --help This help message\n"); |
| 71 | fprintf(fp, " -l, --list List available formats\n"); |
| 72 | fprintf(fp, " -v, --verbose Verbose mode\n"); |
| 73 | fprintf(fp, " -d, --debug Debug mode\n"); |
| 74 | list_formats(fp); |
| 75 | fprintf(fp, "\n"); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Return 0 if caller should continue, < 0 if caller should return |
| 80 | * error, > 0 if caller should exit without reporting error. |
| 81 | */ |
| 82 | static int parse_options(int argc, const char **argv) |
| 83 | { |
| 84 | poptContext pc; |
| 85 | int opt, ret = 0; |
| 86 | |
| 87 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 88 | poptReadDefaultConfig(pc, 0); |
| 89 | |
| 90 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 91 | switch (opt) { |
| 92 | case OPT_HELP: |
| 93 | usage(stdout); |
| 94 | ret = 1; /* exit cleanly */ |
| 95 | goto end; |
| 96 | case OPT_LIST: |
| 97 | list_formats(stdout); |
| 98 | ret = 1; |
| 99 | goto end; |
| 100 | case OPT_VERBOSE: |
| 101 | babeltrace_verbose = 1; |
| 102 | break; |
| 103 | case OPT_DEBUG: |
| 104 | babeltrace_debug = 1; |
| 105 | break; |
| 106 | default: |
| 107 | ret = -EINVAL; |
| 108 | goto end; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | opt_input_path = poptGetArg(pc); |
| 113 | if (!opt_input_path) { |
| 114 | ret = -EINVAL; |
| 115 | goto end; |
| 116 | } |
| 117 | opt_output_path = poptGetArg(pc); |
| 118 | if (!opt_output_path) { |
| 119 | ret = -EINVAL; |
| 120 | goto end; |
| 121 | } |
| 122 | end: |
| 123 | if (pc) { |
| 124 | poptFreeContext(pc); |
| 125 | } |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | int main(int argc, const char **argv) |
| 130 | { |
| 131 | int ret; |
| 132 | |
| 133 | ret = parse_options(argc, argv); |
| 134 | if (ret < 0) { |
| 135 | fprintf(stdout, "Error parsing options.\n"); |
| 136 | usage(stdout); |
| 137 | exit(EXIT_FAILURE); |
| 138 | } else if (ret > 0) { |
| 139 | exit(EXIT_SUCCESS); |
| 140 | } |
| 141 | printf_verbose("Verbose mode active.\n"); |
| 142 | printf_debug("Debug mode active.\n"); |
| 143 | |
| 144 | printf_verbose("Converting from file: %s\n", opt_input_path); |
| 145 | printf_verbose("Converting from format: %s\n", |
| 146 | opt_input_format ? : "<autodetect>"); |
| 147 | printf_verbose("Converting to file: %s\n", opt_output_path); |
| 148 | printf_verbose("Converting to format: %s\n", |
| 149 | opt_output_format ? : "CTF"); |
| 150 | |
| 151 | return 0; |
| 152 | } |