blob: 944d7dedc3a5908179b0865f5920dec067a5acaa [file] [log] [blame]
Carl Worth6d543af2011-10-19 18:02:33 -07001/*********************************************************************
2 *
3 * Copyright 2011 Intel Corporation
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
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
24 * SOFTWARE.
25 *
26 *********************************************************************/
27
28
29/*
30 * Top-level application for accessing most all apitrace
31 * functionality.
32 */
33
José Fonseca71913ee2011-10-30 13:35:33 +000034#include <string.h>
35
36#include <iomanip>
37#include <iostream>
38
José Fonseca5cc28b02011-10-30 13:38:25 +000039#include "cli.hpp"
Carl Worth6d543af2011-10-19 18:02:33 -070040
41#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
42
Carl Worthbd5e1642011-10-21 20:40:56 -070043typedef void (*command_usage_t) (const char *argv0);
Carl Worth6d543af2011-10-19 18:02:33 -070044typedef int (*command_function_t) (int argc, char *argv[], int first_command_arg);
45
46typedef struct {
47 const char *name;
Carl Worthbd5e1642011-10-21 20:40:56 -070048 const char *synopsis;
49 command_usage_t usage;
Carl Worth6d543af2011-10-19 18:02:33 -070050 command_function_t function;
Carl Worth6d543af2011-10-19 18:02:33 -070051} Command;
52
Carl Worthbd5e1642011-10-21 20:40:56 -070053#define APITRACE_HELP_SYNOPSIS "Print detailed help for the given command."
54
55static void
56apitrace_help_usage(const char *argv0)
57{
58 std::cout << argv0 << " [<command>]"
59 "\n\n\t"
60 APITRACE_HELP_SYNOPSIS
61 "\n\n\t"
62 "Except in this case, where this is all the help you will get."
63 "\n\n";
64}
65
Carl Worth6d543af2011-10-19 18:02:33 -070066static int
67apitrace_help_command(int argc, char *argv[], int first_command_arg);
68
69static Command commands[] = {
Carl Worthbd5e1642011-10-21 20:40:56 -070070 { "dump",
71 APITRACE_DUMP_SYNOPSIS,
72 apitrace_dump_usage,
73 apitrace_dump_command },
74 { "help",
75 APITRACE_HELP_SYNOPSIS,
76 apitrace_help_usage,
77 apitrace_help_command }
Carl Worth6d543af2011-10-19 18:02:33 -070078};
79
Carl Worthbd5e1642011-10-21 20:40:56 -070080void
Carl Worth6d543af2011-10-19 18:02:33 -070081usage(void)
82{
83 Command *command;
84 int i, max_width = 0;
85
86 std::cout <<
87 "Usage: apitrace <command> [<args> ...]\n\n"
88 "The available commands are as follows:\n\n";
89
90 std::cout << std::setiosflags(std::ios::left);
91
92 for (i = 0; i < ARRAY_SIZE(commands); i++) {
93 command = &commands[i];
94 if (strlen(command->name) > max_width)
95 max_width = strlen(command->name);
96 }
97
98 for (i = 0; i < ARRAY_SIZE(commands); i++) {
99 command = &commands[i];
100
101 std::cout << " " << std::setw(max_width+2) << command->name
Carl Worthbd5e1642011-10-21 20:40:56 -0700102 << " " << command->synopsis << "\n";
Carl Worth6d543af2011-10-19 18:02:33 -0700103 }
104
105 std::cout << "\n"
106 "Use \"apitrace help <command>\" for more details on each command.\n";
107}
108
109static int
110apitrace_help_command(int argc, char *argv[], int first_arg_command)
111{
112 Command *command;
113 int i;
114
115 if(first_arg_command == argc) {
116 usage();
117 return 0;
118 }
119
120 char *command_name = argv[first_arg_command];
121
122 for (i = 0; i < ARRAY_SIZE(commands); i++) {
123 command = &commands[i];
124
125 if (strcmp(command_name, command->name) == 0) {
126 std::cout << "Help for \"apitrace " << command_name << "\":\n\n";
Carl Worthbd5e1642011-10-21 20:40:56 -0700127 (command->usage) ("apitrace");
Carl Worth6d543af2011-10-19 18:02:33 -0700128
129 return 0;
130 }
131 }
132
133 std::cerr << "Error: Unknown command: " << command_name
134 << " (see \"apitrace help\").\n";
135
136 return 1;
137}
138
139int
140main(int argc, char **argv)
141{
142 const char *command_name = "trace";
143 Command *command;
144 int i, first_command_arg;
145
146 if (argc == 1) {
147 usage();
148 return 1;
149 }
150
151 for (i = 1; i < argc; ++i) {
152 const char *arg = argv[i];
153
154 if (arg[0] != '-') {
155 break;
156 }
157
158 if (strcmp(arg, "--help") == 0) {
159 return apitrace_help_command (0, NULL, 0);
160 } else {
161 std::cerr << "Error: unknown option " << arg << "\n";
162 usage();
163 return 1;
164 }
165 }
166 first_command_arg = i;
167
168 if (first_command_arg < argc) {
169 command_name = argv[first_command_arg];
170 first_command_arg++;
171 }
172
173 for (i = 0; i < ARRAY_SIZE(commands); i++) {
174 command = &commands[i];
175
176 if (strcmp(command_name, command->name) == 0)
177 return (command->function) (argc, argv, first_command_arg);
178 }
179
180 std::cerr << "Error: unknown command " << command_name
181 << " (see \"apitrace help\").\n";
182
183 return 1;
184}