]> git.cworth.org Git - apitrace/blob - cli/cli_main.cpp
040adcf82928c56f8a35b76617ddeb72b11e1689
[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 static void
83 usage(void)
84 {
85     std::cout <<
86         "Usage: apitrace <command> [<args> ...]\n"
87         "Top-level command line frontend to apitrace.\n"
88         "\n";
89
90     list_commands();
91 }
92
93 static void
94 list_commands(void) {
95     const Command *command;
96     int i, max_width = 0;
97
98     std::cout << "The available commands are as follows:\n\n";
99
100     std::cout << std::setiosflags(std::ios::left);
101
102     for (i = 0; i < ARRAY_SIZE(commands); i++) {
103         command = commands[i];
104         if (strlen(command->name) > max_width) {
105             max_width = strlen(command->name);
106         }
107     }
108
109     for (i = 0; i < ARRAY_SIZE(commands); i++) {
110         command = commands[i];
111
112         std::cout << "    " << std::setw(max_width + 2) << command->name
113                   << " " << command->synopsis << "\n";
114     }
115
116     std::cout << "\n"
117         "Use \"apitrace help <command>\" for more details on each command.\n";
118 }
119
120
121 static int
122 do_help_command(int argc, char *argv[])
123 {
124     const Command *command;
125     int i;
126
127     if (argc != 2) {
128         help_usage();
129         return 0;
130     }
131
132     char *command_name = argv[1];
133
134     for (i = 0; i < ARRAY_SIZE(commands); i++) {
135         command = commands[i];
136
137         if (strcmp(command_name, command->name) == 0) {
138             (command->usage) ();
139             return 0;
140         }
141     }
142
143     std::cerr << "Error: Unknown command: " << command_name
144               << " (see \"apitrace help\").\n";
145
146     return 1;
147 }
148
149 int
150 main(int argc, char **argv)
151 {
152     const char *command_name;
153     const Command *command;
154     int i;
155
156     for (i = 1; i < argc; ++i) {
157         const char *arg = argv[i];
158
159         if (arg[0] != '-') {
160             break;
161         }
162
163         if (strcmp(arg, "--help") == 0) {
164             return do_help_command(0, NULL);
165         } else {
166             std::cerr << "Error: unknown option " << arg << "\n";
167             usage();
168             return 1;
169         }
170     }
171
172     if (i == argc) {
173         usage();
174         return 1;
175     }
176
177     command_name = argv[i];
178
179     argc -= i;
180     argv = &argv[i];
181
182     for (i = 0; i < ARRAY_SIZE(commands); i++) {
183         command = commands[i];
184
185         if (strcmp(command_name, command->name) == 0)
186             return (command->function) (argc, argv);
187     }
188
189     std::cerr << "Error: unknown command " << command_name
190               << " (see \"apitrace help\").\n";
191
192    return 1;
193 }