blob: fd4dbbb53db2f07d3b59db1787a458dbce8b2b9f [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é Fonseca3c167fe2011-10-30 14:21:03 +000043static const char *help_synopsis = "Print detailed help for the given command.";
Carl Worthbd5e1642011-10-21 20:40:56 -070044
José Fonseca646a00d2011-10-30 14:07:20 +000045static void list_commands(void);
46
Carl Worthbd5e1642011-10-21 20:40:56 -070047static void
José Fonseca3c167fe2011-10-30 14:21:03 +000048help_usage()
Carl Worthbd5e1642011-10-21 20:40:56 -070049{
José Fonseca3c167fe2011-10-30 14:21:03 +000050 std::cout
51 << "usage: apitrace help [<command>]\n"
52 << help_synopsis << "\n"
José Fonseca646a00d2011-10-30 14:07:20 +000053 "\n";
54
55 list_commands();
Carl Worthbd5e1642011-10-21 20:40:56 -070056}
57
Carl Worth6d543af2011-10-19 18:02:33 -070058static int
José Fonseca3c167fe2011-10-30 14:21:03 +000059help_command(int argc, char *argv[], int first_command_arg);
Carl Worth6d543af2011-10-19 18:02:33 -070060
José Fonseca3c167fe2011-10-30 14:21:03 +000061const Command help = {
62 "help",
63 help_synopsis,
64 help_usage,
65 help_command
66};
67
68static const Command * commands[] = {
69 &dump,
70 &help,
Carl Worth6d543af2011-10-19 18:02:33 -070071};
72
José Fonseca646a00d2011-10-30 14:07:20 +000073static void
Carl Worth6d543af2011-10-19 18:02:33 -070074usage(void)
75{
José Fonseca646a00d2011-10-30 14:07:20 +000076 std::cout <<
77 "Usage: apitrace <command> [<args> ...]\n"
78 "Top-level command line frontend to apitrace.\n"
79 "\n";
80
81 list_commands();
82}
83
84static void
85list_commands(void) {
José Fonseca3c167fe2011-10-30 14:21:03 +000086 const Command *command;
Carl Worth6d543af2011-10-19 18:02:33 -070087 int i, max_width = 0;
88
José Fonseca646a00d2011-10-30 14:07:20 +000089 std::cout << "The available commands are as follows:\n\n";
Carl Worth6d543af2011-10-19 18:02:33 -070090
91 std::cout << std::setiosflags(std::ios::left);
92
93 for (i = 0; i < ARRAY_SIZE(commands); i++) {
José Fonseca3c167fe2011-10-30 14:21:03 +000094 command = commands[i];
95 if (strlen(command->name) > max_width) {
Carl Worth6d543af2011-10-19 18:02:33 -070096 max_width = strlen(command->name);
José Fonseca3c167fe2011-10-30 14:21:03 +000097 }
Carl Worth6d543af2011-10-19 18:02:33 -070098 }
99
100 for (i = 0; i < ARRAY_SIZE(commands); i++) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000101 command = commands[i];
Carl Worth6d543af2011-10-19 18:02:33 -0700102
José Fonseca3c167fe2011-10-30 14:21:03 +0000103 std::cout << " " << std::setw(max_width + 2) << command->name
Carl Worthbd5e1642011-10-21 20:40:56 -0700104 << " " << command->synopsis << "\n";
Carl Worth6d543af2011-10-19 18:02:33 -0700105 }
106
107 std::cout << "\n"
108 "Use \"apitrace help <command>\" for more details on each command.\n";
109}
110
José Fonseca646a00d2011-10-30 14:07:20 +0000111
Carl Worth6d543af2011-10-19 18:02:33 -0700112static int
José Fonseca3c167fe2011-10-30 14:21:03 +0000113help_command(int argc, char *argv[], int first_arg_command)
Carl Worth6d543af2011-10-19 18:02:33 -0700114{
José Fonseca3c167fe2011-10-30 14:21:03 +0000115 const Command *command;
Carl Worth6d543af2011-10-19 18:02:33 -0700116 int i;
117
José Fonseca646a00d2011-10-30 14:07:20 +0000118 if (first_arg_command == argc) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000119 help_usage();
Carl Worth6d543af2011-10-19 18:02:33 -0700120 return 0;
121 }
122
123 char *command_name = argv[first_arg_command];
124
125 for (i = 0; i < ARRAY_SIZE(commands); i++) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000126 command = commands[i];
Carl Worth6d543af2011-10-19 18:02:33 -0700127
128 if (strcmp(command_name, command->name) == 0) {
José Fonseca646a00d2011-10-30 14:07:20 +0000129 (command->usage) ();
Carl Worth6d543af2011-10-19 18:02:33 -0700130 return 0;
131 }
132 }
133
134 std::cerr << "Error: Unknown command: " << command_name
135 << " (see \"apitrace help\").\n";
136
137 return 1;
138}
139
140int
141main(int argc, char **argv)
142{
143 const char *command_name = "trace";
José Fonseca3c167fe2011-10-30 14:21:03 +0000144 const Command *command;
Carl Worth6d543af2011-10-19 18:02:33 -0700145 int i, first_command_arg;
146
147 if (argc == 1) {
148 usage();
149 return 1;
150 }
151
152 for (i = 1; i < argc; ++i) {
153 const char *arg = argv[i];
154
155 if (arg[0] != '-') {
156 break;
157 }
158
159 if (strcmp(arg, "--help") == 0) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000160 return help_command (0, NULL, 0);
Carl Worth6d543af2011-10-19 18:02:33 -0700161 } else {
162 std::cerr << "Error: unknown option " << arg << "\n";
163 usage();
164 return 1;
165 }
166 }
167 first_command_arg = i;
168
169 if (first_command_arg < argc) {
170 command_name = argv[first_command_arg];
171 first_command_arg++;
172 }
173
174 for (i = 0; i < ARRAY_SIZE(commands); i++) {
José Fonseca3c167fe2011-10-30 14:21:03 +0000175 command = commands[i];
Carl Worth6d543af2011-10-19 18:02:33 -0700176
177 if (strcmp(command_name, command->name) == 0)
178 return (command->function) (argc, argv, first_command_arg);
179 }
180
181 std::cerr << "Error: unknown command " << command_name
182 << " (see \"apitrace help\").\n";
183
184 return 1;
185}