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