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