]> git.cworth.org Git - apitrace/blob - cli/cli_main.cpp
cli: Add an alias mechanism and alias "retrace" to "replay"
[apitrace] / cli / cli_main.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 almost all of apitrace
31  * functionality.
32  */
33
34 #include <string.h>
35
36 #include <iomanip>
37 #include <iostream>
38
39 #include "cli.hpp"
40
41 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
42
43 static const char *help_synopsis = "Print detailed help for the given command.";
44
45 static void list_commands(void);
46
47 static void
48 help_usage()
49 {
50     std::cout
51         << "usage: apitrace help [<command>]\n"
52         << help_synopsis << "\n"
53         "\n";
54
55     list_commands();
56 }
57
58 static int
59 do_help_command(int argc, char *argv[]);
60
61 const Command help_command = {
62     "help",
63     help_synopsis,
64     help_usage,
65     do_help_command
66 };
67
68 static const Command * commands[] = {
69     &diff_command,
70     &diff_state_command,
71     &diff_images_command,
72     &dump_command,
73     &dump_images_command,
74     &pickle_command,
75     &repack_command,
76     &retrace_command,
77     &trace_command,
78     &trim_command,
79     &help_command
80 };
81
82 /* Aliases provide a mechanism to allow compatibility with old command
83  * names (such as "retrace") for current commands (such as the replay
84  * command). */
85 typedef struct {
86     const char *name;
87     const Command *command;
88 } Alias;
89
90 static const Alias aliases[] = {
91     { "retrace", &retrace_command }
92 };
93
94 static void
95 usage(void)
96 {
97     std::cout <<
98         "Usage: apitrace <command> [<args> ...]\n"
99         "Top-level command line frontend to apitrace.\n"
100         "\n";
101
102     list_commands();
103 }
104
105 static void
106 list_commands(void) {
107     const Command *command;
108     int i, max_width = 0;
109
110     std::cout << "The available commands are as follows:\n\n";
111
112     std::cout << std::setiosflags(std::ios::left);
113
114     for (i = 0; i < ARRAY_SIZE(commands); i++) {
115         command = commands[i];
116         if (strlen(command->name) > max_width) {
117             max_width = strlen(command->name);
118         }
119     }
120
121     for (i = 0; i < ARRAY_SIZE(commands); i++) {
122         command = commands[i];
123
124         std::cout << "    " << std::setw(max_width + 2) << command->name
125                   << " " << command->synopsis << "\n";
126     }
127
128     std::cout << "\n"
129         "Use \"apitrace help <command>\" for more details on each command.\n";
130 }
131
132
133 static int
134 do_help_command(int argc, char *argv[])
135 {
136     const Command *command;
137     int i;
138
139     if (argc != 2) {
140         help_usage();
141         return 0;
142     }
143
144     char *command_name = argv[1];
145
146     for (i = 0; i < ARRAY_SIZE(commands); i++) {
147         command = commands[i];
148
149         if (strcmp(command_name, command->name) == 0) {
150             (command->usage) ();
151             return 0;
152         }
153     }
154
155     std::cerr << "Error: Unknown command: " << command_name
156               << " (see \"apitrace help\").\n";
157
158     return 1;
159 }
160
161 int
162 main(int argc, char **argv)
163 {
164     const char *command_name;
165     const Command *command;
166     const Alias *alias;
167     int i;
168
169     for (i = 1; i < argc; ++i) {
170         const char *arg = argv[i];
171
172         if (arg[0] != '-') {
173             break;
174         }
175
176         if (strcmp(arg, "--help") == 0) {
177             return do_help_command(0, NULL);
178         } else {
179             std::cerr << "Error: unknown option " << arg << "\n";
180             usage();
181             return 1;
182         }
183     }
184
185     if (i == argc) {
186         usage();
187         return 1;
188     }
189
190     command_name = argv[i];
191
192     argc -= i;
193     argv = &argv[i];
194
195     for (i = 0; i < ARRAY_SIZE(commands); i++) {
196         command = commands[i];
197
198         if (strcmp(command_name, command->name) == 0)
199             return (command->function) (argc, argv);
200     }
201
202     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
203         alias = &aliases[i];
204
205         if (strcmp(command_name, alias->name) == 0)
206             return (alias->command->function) (argc, argv);
207     }
208
209     std::cerr << "Error: unknown command " << command_name
210               << " (see \"apitrace help\").\n";
211
212    return 1;
213 }