blob: e96673ac70e251442e6bd1f20e430dccc164269f [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/*
José Fonseca646a00d2011-10-30 14:07:20 +000030 * Top-level application for accessing almost of apitrace
Carl Worth6d543af2011-10-19 18:02:33 -070031 * 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
José Fonseca646a00d2011-10-30 14:07:20 +000043typedef void (*command_usage_t) (void);
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
José Fonseca646a00d2011-10-30 14:07:20 +000055static void list_commands(void);
56
Carl Worthbd5e1642011-10-21 20:40:56 -070057static void
José Fonseca646a00d2011-10-30 14:07:20 +000058apitrace_help_usage()
Carl Worthbd5e1642011-10-21 20:40:56 -070059{
José Fonseca646a00d2011-10-30 14:07:20 +000060 std::cout << "usage: apitrace help [<command>]\n"
61 APITRACE_HELP_SYNOPSIS "\n"
62 "\n";
63
64 list_commands();
Carl Worthbd5e1642011-10-21 20:40:56 -070065}
66
Carl Worth6d543af2011-10-19 18:02:33 -070067static int
68apitrace_help_command(int argc, char *argv[], int first_command_arg);
69
70static Command commands[] = {
Carl Worthbd5e1642011-10-21 20:40:56 -070071 { "dump",
72 APITRACE_DUMP_SYNOPSIS,
73 apitrace_dump_usage,
74 apitrace_dump_command },
75 { "help",
76 APITRACE_HELP_SYNOPSIS,
77 apitrace_help_usage,
78 apitrace_help_command }
Carl Worth6d543af2011-10-19 18:02:33 -070079};
80
José Fonseca646a00d2011-10-30 14:07:20 +000081static void
Carl Worth6d543af2011-10-19 18:02:33 -070082usage(void)
83{
José Fonseca646a00d2011-10-30 14:07:20 +000084 std::cout <<
85 "Usage: apitrace <command> [<args> ...]\n"
86 "Top-level command line frontend to apitrace.\n"
87 "\n";
88
89 list_commands();
90}
91
92static void
93list_commands(void) {
Carl Worth6d543af2011-10-19 18:02:33 -070094 Command *command;
95 int i, max_width = 0;
96
José Fonseca646a00d2011-10-30 14:07:20 +000097 std::cout << "The available commands are as follows:\n\n";
Carl Worth6d543af2011-10-19 18:02:33 -070098
99 std::cout << std::setiosflags(std::ios::left);
100
101 for (i = 0; i < ARRAY_SIZE(commands); i++) {
102 command = &commands[i];
103 if (strlen(command->name) > max_width)
104 max_width = strlen(command->name);
105 }
106
107 for (i = 0; i < ARRAY_SIZE(commands); i++) {
108 command = &commands[i];
109
José Fonseca646a00d2011-10-30 14:07:20 +0000110 std::cout << " " << std::setw(max_width+2) << command->name
Carl Worthbd5e1642011-10-21 20:40:56 -0700111 << " " << command->synopsis << "\n";
Carl Worth6d543af2011-10-19 18:02:33 -0700112 }
113
114 std::cout << "\n"
115 "Use \"apitrace help <command>\" for more details on each command.\n";
116}
117
José Fonseca646a00d2011-10-30 14:07:20 +0000118
Carl Worth6d543af2011-10-19 18:02:33 -0700119static int
120apitrace_help_command(int argc, char *argv[], int first_arg_command)
121{
122 Command *command;
123 int i;
124
José Fonseca646a00d2011-10-30 14:07:20 +0000125 if (first_arg_command == argc) {
126 apitrace_help_usage();
Carl Worth6d543af2011-10-19 18:02:33 -0700127 return 0;
128 }
129
130 char *command_name = argv[first_arg_command];
131
132 for (i = 0; i < ARRAY_SIZE(commands); i++) {
133 command = &commands[i];
134
135 if (strcmp(command_name, command->name) == 0) {
José Fonseca646a00d2011-10-30 14:07:20 +0000136 (command->usage) ();
Carl Worth6d543af2011-10-19 18:02:33 -0700137 return 0;
138 }
139 }
140
141 std::cerr << "Error: Unknown command: " << command_name
142 << " (see \"apitrace help\").\n";
143
144 return 1;
145}
146
147int
148main(int argc, char **argv)
149{
150 const char *command_name = "trace";
151 Command *command;
152 int i, first_command_arg;
153
154 if (argc == 1) {
155 usage();
156 return 1;
157 }
158
159 for (i = 1; i < argc; ++i) {
160 const char *arg = argv[i];
161
162 if (arg[0] != '-') {
163 break;
164 }
165
166 if (strcmp(arg, "--help") == 0) {
167 return apitrace_help_command (0, NULL, 0);
168 } else {
169 std::cerr << "Error: unknown option " << arg << "\n";
170 usage();
171 return 1;
172 }
173 }
174 first_command_arg = i;
175
176 if (first_command_arg < argc) {
177 command_name = argv[first_command_arg];
178 first_command_arg++;
179 }
180
181 for (i = 0; i < ARRAY_SIZE(commands); i++) {
182 command = &commands[i];
183
184 if (strcmp(command_name, command->name) == 0)
185 return (command->function) (argc, argv, first_command_arg);
186 }
187
188 std::cerr << "Error: unknown command " << command_name
189 << " (see \"apitrace help\").\n";
190
191 return 1;
192}