]> git.cworth.org Git - apitrace/blob - cli/apitrace.cpp
107952cf5529165ce8211df11d2fffaa55ff50c8
[apitrace] / cli / apitrace.cpp
1 /*********************************************************************
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
34 #include "apitrace_cli.hpp"
35
36 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
37
38 typedef void (*command_usage_t) (const char *argv0);
39 typedef int (*command_function_t) (int argc, char *argv[], int first_command_arg);
40
41 typedef struct {
42     const char *name;
43     const char *synopsis;
44     command_usage_t usage;
45     command_function_t function;
46 } Command;
47
48 #define APITRACE_HELP_SYNOPSIS "Print detailed help for the given command."
49
50 static void
51 apitrace_help_usage(const char *argv0)
52 {
53     std::cout << argv0 << " [<command>]"
54         "\n\n\t"
55         APITRACE_HELP_SYNOPSIS
56         "\n\n\t"
57         "Except in this case, where this is all the help you will get."
58         "\n\n";
59 }
60
61 static int
62 apitrace_help_command(int argc, char *argv[], int first_command_arg);
63
64 static Command commands[] = {
65     { "dump",
66       APITRACE_DUMP_SYNOPSIS,
67       apitrace_dump_usage,
68       apitrace_dump_command },
69     { "help",
70       APITRACE_HELP_SYNOPSIS,
71       apitrace_help_usage,
72       apitrace_help_command }
73 };
74
75 void
76 usage(void)
77 {
78     Command *command;
79     int i, max_width = 0;
80
81     std::cout <<
82         "Usage: apitrace <command> [<args> ...]\n\n"
83         "The available commands are as follows:\n\n";
84
85     std::cout << std::setiosflags(std::ios::left);
86
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);
91     }
92
93     for (i = 0; i < ARRAY_SIZE(commands); i++) {
94         command = &commands[i];
95
96         std::cout << " " << std::setw(max_width+2) << command->name
97                   << " " << command->synopsis << "\n";
98     }
99
100     std::cout << "\n"
101         "Use \"apitrace help <command>\" for more details on each command.\n";
102 }
103
104 static int
105 apitrace_help_command(int argc, char *argv[], int first_arg_command)
106 {
107     Command *command;
108     int i;
109
110     if(first_arg_command == argc) {
111         usage();
112         return 0;
113     }
114
115     char *command_name = argv[first_arg_command];
116
117     for (i = 0; i < ARRAY_SIZE(commands); i++) {
118         command = &commands[i];
119
120         if (strcmp(command_name, command->name) == 0) {
121             std::cout << "Help for \"apitrace " << command_name << "\":\n\n";
122             (command->usage) ("apitrace");
123
124             return 0;
125         }
126     }
127
128     std::cerr << "Error: Unknown command: " << command_name
129               << " (see \"apitrace help\").\n";
130
131     return 1;
132 }
133
134 int
135 main(int argc, char **argv)
136 {
137     const char *command_name = "trace";
138     Command *command;
139     int i, first_command_arg;
140
141     if (argc == 1) {
142         usage();
143         return 1;
144     }
145
146     for (i = 1; i < argc; ++i) {
147         const char *arg = argv[i];
148
149         if (arg[0] != '-') {
150             break;
151         }
152
153         if (strcmp(arg, "--help") == 0) {
154             return apitrace_help_command (0, NULL, 0);
155         } else {
156             std::cerr << "Error: unknown option " << arg << "\n";
157             usage();
158             return 1;
159         }
160     }
161     first_command_arg = i;
162
163     if (first_command_arg < argc) {
164         command_name = argv[first_command_arg];
165         first_command_arg++;
166     }
167
168     for (i = 0; i < ARRAY_SIZE(commands); i++) {
169         command = &commands[i];
170
171         if (strcmp(command_name, command->name) == 0)
172             return (command->function) (argc, argv, first_command_arg);
173     }
174
175     std::cerr << "Error: unknown command " << command_name
176               << " (see \"apitrace help\").\n";
177
178     return 1;
179 }