1 /*********************************************************************
3 * Copyright 2011 Intel Corporation
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use, copy,
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 *********************************************************************/
30 * Top-level application for accessing most all apitrace
34 #include "apitrace_cli.hpp"
36 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
38 typedef void (*command_usage_t) (const char *argv0);
39 typedef int (*command_function_t) (int argc, char *argv[], int first_command_arg);
44 command_usage_t usage;
45 command_function_t function;
48 #define APITRACE_HELP_SYNOPSIS "Print detailed help for the given command."
51 apitrace_help_usage(const char *argv0)
53 std::cout << argv0 << " [<command>]"
55 APITRACE_HELP_SYNOPSIS
57 "Except in this case, where this is all the help you will get."
62 apitrace_help_command(int argc, char *argv[], int first_command_arg);
64 static Command commands[] = {
66 APITRACE_DUMP_SYNOPSIS,
68 apitrace_dump_command },
70 APITRACE_HELP_SYNOPSIS,
72 apitrace_help_command }
82 "Usage: apitrace <command> [<args> ...]\n\n"
83 "The available commands are as follows:\n\n";
85 std::cout << std::setiosflags(std::ios::left);
87 for (i = 0; i < ARRAY_SIZE(commands); i++) {
88 command = &commands[i];
89 if (strlen(command->name) > max_width)
90 max_width = strlen(command->name);
93 for (i = 0; i < ARRAY_SIZE(commands); i++) {
94 command = &commands[i];
96 std::cout << " " << std::setw(max_width+2) << command->name
97 << " " << command->synopsis << "\n";
101 "Use \"apitrace help <command>\" for more details on each command.\n";
105 apitrace_help_command(int argc, char *argv[], int first_arg_command)
110 if(first_arg_command == argc) {
115 char *command_name = argv[first_arg_command];
117 for (i = 0; i < ARRAY_SIZE(commands); i++) {
118 command = &commands[i];
120 if (strcmp(command_name, command->name) == 0) {
121 std::cout << "Help for \"apitrace " << command_name << "\":\n\n";
122 (command->usage) ("apitrace");
128 std::cerr << "Error: Unknown command: " << command_name
129 << " (see \"apitrace help\").\n";
135 main(int argc, char **argv)
137 const char *command_name = "trace";
139 int i, first_command_arg;
146 for (i = 1; i < argc; ++i) {
147 const char *arg = argv[i];
153 if (strcmp(arg, "--help") == 0) {
154 return apitrace_help_command (0, NULL, 0);
156 std::cerr << "Error: unknown option " << arg << "\n";
161 first_command_arg = i;
163 if (first_command_arg < argc) {
164 command_name = argv[first_command_arg];
168 for (i = 0; i < ARRAY_SIZE(commands); i++) {
169 command = &commands[i];
171 if (strcmp(command_name, command->name) == 0)
172 return (command->function) (argc, argv, first_command_arg);
175 std::cerr << "Error: unknown command " << command_name
176 << " (see \"apitrace help\").\n";