]> git.cworth.org Git - apitrace/blob - cli/cli_main.cpp
Merge branch 'attrib_list'
[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     &sed_command,
76     &repack_command,
77     &retrace_command,
78     &trace_command,
79     &trim_command,
80     &help_command
81 };
82
83 /* Aliases provide a mechanism to allow compatibility with old command
84  * names (such as "retrace") for current commands (such as the replay
85  * command). */
86 typedef struct {
87     const char *name;
88     const Command *command;
89 } Alias;
90
91 static const Alias aliases[] = {
92     { "retrace", &retrace_command }
93 };
94
95 static void
96 usage(void)
97 {
98     std::cout <<
99         "Usage: apitrace <command> [<args> ...]\n"
100         "Top-level command line frontend to apitrace.\n"
101         "\n";
102
103     list_commands();
104 }
105
106 static void
107 list_commands(void) {
108     const Command *command;
109     int i, max_width = 0;
110
111     std::cout << "The available commands are as follows:\n\n";
112
113     std::cout << std::setiosflags(std::ios::left);
114
115     for (i = 0; i < ARRAY_SIZE(commands); i++) {
116         command = commands[i];
117         if (strlen(command->name) > max_width) {
118             max_width = strlen(command->name);
119         }
120     }
121
122     for (i = 0; i < ARRAY_SIZE(commands); i++) {
123         command = commands[i];
124
125         std::cout << "    " << std::setw(max_width + 2) << command->name
126                   << " " << command->synopsis << "\n";
127     }
128
129     std::cout << "\n"
130         "Use \"apitrace help <command>\" for more details on each command.\n";
131 }
132
133
134 static int
135 do_help_command(int argc, char *argv[])
136 {
137     const Command *command;
138     int i;
139
140     if (argc != 2) {
141         help_usage();
142         return 0;
143     }
144
145     char *command_name = argv[1];
146
147     for (i = 0; i < ARRAY_SIZE(commands); i++) {
148         command = commands[i];
149
150         if (strcmp(command_name, command->name) == 0) {
151             (command->usage) ();
152             return 0;
153         }
154     }
155
156     std::cerr << "Error: Unknown command: " << command_name
157               << " (see \"apitrace help\").\n";
158
159     return 1;
160 }
161
162 int
163 main(int argc, char **argv)
164 {
165     const char *command_name;
166     const Command *command;
167     const Alias *alias;
168     int i;
169
170     for (i = 1; i < argc; ++i) {
171         const char *arg = argv[i];
172
173         if (arg[0] != '-') {
174             break;
175         }
176
177         if (strcmp(arg, "--help") == 0) {
178             return do_help_command(0, NULL);
179         } else {
180             std::cerr << "Error: unknown option " << arg << "\n";
181             usage();
182             return 1;
183         }
184     }
185
186     if (i == argc) {
187         usage();
188         return 1;
189     }
190
191     command_name = argv[i];
192
193     argc -= i;
194     argv = &argv[i];
195
196     for (i = 0; i < ARRAY_SIZE(commands); i++) {
197         command = commands[i];
198
199         if (strcmp(command_name, command->name) == 0)
200             return (command->function) (argc, argv);
201     }
202
203     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
204         alias = &aliases[i];
205
206         if (strcmp(command_name, alias->name) == 0)
207             return (alias->command->function) (argc, argv);
208     }
209
210     std::cerr << "Error: unknown command " << command_name
211               << " (see \"apitrace help\").\n";
212
213    return 1;
214 }