Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 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 | /* |
Carl Worth | efa42fa | 2011-11-01 13:48:55 -0700 | [diff] [blame] | 30 | * Top-level application for accessing almost all of apitrace |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 31 | * functionality. |
| 32 | */ |
| 33 | |
José Fonseca | 71913ee | 2011-10-30 13:35:33 +0000 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
| 36 | #include <iomanip> |
| 37 | #include <iostream> |
| 38 | |
José Fonseca | 5cc28b0 | 2011-10-30 13:38:25 +0000 | [diff] [blame] | 39 | #include "cli.hpp" |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 40 | |
| 41 | #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0])) |
| 42 | |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 43 | static const char *help_synopsis = "Print detailed help for the given command."; |
Carl Worth | bd5e164 | 2011-10-21 20:40:56 -0700 | [diff] [blame] | 44 | |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 45 | static void list_commands(void); |
| 46 | |
Carl Worth | bd5e164 | 2011-10-21 20:40:56 -0700 | [diff] [blame] | 47 | static void |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 48 | help_usage() |
Carl Worth | bd5e164 | 2011-10-21 20:40:56 -0700 | [diff] [blame] | 49 | { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 50 | std::cout |
| 51 | << "usage: apitrace help [<command>]\n" |
| 52 | << help_synopsis << "\n" |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 53 | "\n"; |
| 54 | |
| 55 | list_commands(); |
Carl Worth | bd5e164 | 2011-10-21 20:40:56 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 58 | static int |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 59 | do_help_command(int argc, char *argv[]); |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 60 | |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 61 | const Command help_command = { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 62 | "help", |
| 63 | help_synopsis, |
| 64 | help_usage, |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 65 | do_help_command |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static const Command * commands[] = { |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 69 | &dump_command, |
| 70 | &help_command |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 73 | static void |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 74 | usage(void) |
| 75 | { |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 76 | std::cout << |
| 77 | "Usage: apitrace <command> [<args> ...]\n" |
| 78 | "Top-level command line frontend to apitrace.\n" |
| 79 | "\n"; |
| 80 | |
| 81 | list_commands(); |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | list_commands(void) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 86 | const Command *command; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 87 | int i, max_width = 0; |
| 88 | |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 89 | std::cout << "The available commands are as follows:\n\n"; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 90 | |
| 91 | std::cout << std::setiosflags(std::ios::left); |
| 92 | |
| 93 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 94 | command = commands[i]; |
| 95 | if (strlen(command->name) > max_width) { |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 96 | max_width = strlen(command->name); |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 97 | } |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 101 | command = commands[i]; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 102 | |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 103 | std::cout << " " << std::setw(max_width + 2) << command->name |
Carl Worth | bd5e164 | 2011-10-21 20:40:56 -0700 | [diff] [blame] | 104 | << " " << command->synopsis << "\n"; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | std::cout << "\n" |
| 108 | "Use \"apitrace help <command>\" for more details on each command.\n"; |
| 109 | } |
| 110 | |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 111 | |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 112 | static int |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 113 | do_help_command(int argc, char *argv[]) |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 114 | { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 115 | const Command *command; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 116 | int i; |
| 117 | |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 118 | if (argc != 1) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 119 | help_usage(); |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 123 | char *command_name = argv[0]; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 124 | |
| 125 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 126 | command = commands[i]; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 127 | |
| 128 | if (strcmp(command_name, command->name) == 0) { |
José Fonseca | 646a00d | 2011-10-30 14:07:20 +0000 | [diff] [blame] | 129 | (command->usage) (); |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | std::cerr << "Error: Unknown command: " << command_name |
| 135 | << " (see \"apitrace help\").\n"; |
| 136 | |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | int |
| 141 | main(int argc, char **argv) |
| 142 | { |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 143 | const char *command_name; |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 144 | const Command *command; |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 145 | int i; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 146 | |
| 147 | for (i = 1; i < argc; ++i) { |
| 148 | const char *arg = argv[i]; |
| 149 | |
| 150 | if (arg[0] != '-') { |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | if (strcmp(arg, "--help") == 0) { |
Carl Worth | 68f7c98 | 2011-11-01 13:47:26 -0700 | [diff] [blame] | 155 | return do_help_command(0, NULL); |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 156 | } else { |
| 157 | std::cerr << "Error: unknown option " << arg << "\n"; |
| 158 | usage(); |
| 159 | return 1; |
| 160 | } |
| 161 | } |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 162 | |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 163 | if (i == argc) { |
| 164 | usage(); |
| 165 | return 1; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 166 | } |
| 167 | |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 168 | command_name = argv[i++]; |
| 169 | |
| 170 | argc -= i; |
| 171 | argv = &argv[i]; |
| 172 | |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 173 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
José Fonseca | 3c167fe | 2011-10-30 14:21:03 +0000 | [diff] [blame] | 174 | command = commands[i]; |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 175 | |
| 176 | if (strcmp(command_name, command->name) == 0) |
José Fonseca | ffba597 | 2011-10-30 14:29:28 +0000 | [diff] [blame] | 177 | return (command->function) (argc, argv); |
Carl Worth | 6d543af | 2011-10-19 18:02:33 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | std::cerr << "Error: unknown command " << command_name |
| 181 | << " (see \"apitrace help\").\n"; |
| 182 | |
| 183 | return 1; |
| 184 | } |