From: José Fonseca Date: Sun, 30 Oct 2011 13:38:25 +0000 (+0000) Subject: Use a slightly different naming convention for cli source files. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;ds=sidebyside;h=5cc28b0393274f17682c000c114c3f6ffa280e68;p=apitrace Use a slightly different naming convention for cli source files. --- diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index c25b4ce..c41cdb4 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -1,5 +1,6 @@ add_executable (apitrace - apitrace.cpp - apitrace_dump.cpp) + cli_main.cpp + cli_dump.cpp +) install (TARGETS apitrace RUNTIME DESTINATION bin) diff --git a/cli/apitrace.cpp b/cli/apitrace.cpp deleted file mode 100644 index dde2c59..0000000 --- a/cli/apitrace.cpp +++ /dev/null @@ -1,184 +0,0 @@ -/********************************************************************* - * - * Copyright 2011 Intel Corporation - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, copy, - * modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - *********************************************************************/ - - -/* - * Top-level application for accessing most all apitrace - * functionality. - */ - -#include - -#include -#include - -#include "apitrace_cli.hpp" - -#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0])) - -typedef void (*command_usage_t) (const char *argv0); -typedef int (*command_function_t) (int argc, char *argv[], int first_command_arg); - -typedef struct { - const char *name; - const char *synopsis; - command_usage_t usage; - command_function_t function; -} Command; - -#define APITRACE_HELP_SYNOPSIS "Print detailed help for the given command." - -static void -apitrace_help_usage(const char *argv0) -{ - std::cout << argv0 << " []" - "\n\n\t" - APITRACE_HELP_SYNOPSIS - "\n\n\t" - "Except in this case, where this is all the help you will get." - "\n\n"; -} - -static int -apitrace_help_command(int argc, char *argv[], int first_command_arg); - -static Command commands[] = { - { "dump", - APITRACE_DUMP_SYNOPSIS, - apitrace_dump_usage, - apitrace_dump_command }, - { "help", - APITRACE_HELP_SYNOPSIS, - apitrace_help_usage, - apitrace_help_command } -}; - -void -usage(void) -{ - Command *command; - int i, max_width = 0; - - std::cout << - "Usage: apitrace [ ...]\n\n" - "The available commands are as follows:\n\n"; - - std::cout << std::setiosflags(std::ios::left); - - for (i = 0; i < ARRAY_SIZE(commands); i++) { - command = &commands[i]; - if (strlen(command->name) > max_width) - max_width = strlen(command->name); - } - - for (i = 0; i < ARRAY_SIZE(commands); i++) { - command = &commands[i]; - - std::cout << " " << std::setw(max_width+2) << command->name - << " " << command->synopsis << "\n"; - } - - std::cout << "\n" - "Use \"apitrace help \" for more details on each command.\n"; -} - -static int -apitrace_help_command(int argc, char *argv[], int first_arg_command) -{ - Command *command; - int i; - - if(first_arg_command == argc) { - usage(); - return 0; - } - - char *command_name = argv[first_arg_command]; - - for (i = 0; i < ARRAY_SIZE(commands); i++) { - command = &commands[i]; - - if (strcmp(command_name, command->name) == 0) { - std::cout << "Help for \"apitrace " << command_name << "\":\n\n"; - (command->usage) ("apitrace"); - - return 0; - } - } - - std::cerr << "Error: Unknown command: " << command_name - << " (see \"apitrace help\").\n"; - - return 1; -} - -int -main(int argc, char **argv) -{ - const char *command_name = "trace"; - Command *command; - int i, first_command_arg; - - if (argc == 1) { - usage(); - return 1; - } - - for (i = 1; i < argc; ++i) { - const char *arg = argv[i]; - - if (arg[0] != '-') { - break; - } - - if (strcmp(arg, "--help") == 0) { - return apitrace_help_command (0, NULL, 0); - } else { - std::cerr << "Error: unknown option " << arg << "\n"; - usage(); - return 1; - } - } - first_command_arg = i; - - if (first_command_arg < argc) { - command_name = argv[first_command_arg]; - first_command_arg++; - } - - for (i = 0; i < ARRAY_SIZE(commands); i++) { - command = &commands[i]; - - if (strcmp(command_name, command->name) == 0) - return (command->function) (argc, argv, first_command_arg); - } - - std::cerr << "Error: unknown command " << command_name - << " (see \"apitrace help\").\n"; - - return 1; -} diff --git a/cli/apitrace_cli.hpp b/cli/apitrace_cli.hpp deleted file mode 100644 index 8e43277..0000000 --- a/cli/apitrace_cli.hpp +++ /dev/null @@ -1,42 +0,0 @@ -/********************************************************************* - * - * Copyright 2011 Intel Corporation - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, copy, - * modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - *********************************************************************/ - -#ifndef _APITRACE_CLI_HPP_ -#define _APITRACE_CLI_HPP_ - -#define APITRACE_DUMP_SYNOPSIS "Dump given trace(s) to standard output." - -void -apitrace_dump_usage(const char *argv0); - -int -apitrace_dump_command(int argc, char *argv[], int first_command_arg); - -void -usage(void); - -#endif /* _APITRACE_CLI_HPP_ */ diff --git a/cli/apitrace_dump.cpp b/cli/apitrace_dump.cpp deleted file mode 100644 index 540a21c..0000000 --- a/cli/apitrace_dump.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/************************************************************************** - * - * Copyright 2010 VMware, Inc. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - **************************************************************************/ - -#include - -#include "apitrace_cli.hpp" - -#include "trace_parser.hpp" - -enum ColorOption { - COLOR_OPTION_NEVER = 0, - COLOR_OPTION_ALWAYS = 1, - COLOR_OPTION_AUTO = -1 -}; - -static ColorOption color = COLOR_OPTION_AUTO; - -void -apitrace_dump_usage(const char *argv0) -{ - std::cout << argv0 << " [OPTIONS] ..." - "\n\n\t" - APITRACE_DUMP_SYNOPSIS - "\n\n\t" - "Supports the following options:\n\t" - "\t--color=\n\t" - "\t--colour= Colored syntax highlighting\n\t" - "\t WHEN is 'auto', 'always', or 'never'\n"; -} - -int -apitrace_dump_command(int argc, char *argv[], int first_arg_command) -{ - int i; - - for (i = first_arg_command; i < argc; ++i) { - const char *arg = argv[i]; - - if (arg[0] != '-') { - break; - } - - if (!strcmp(arg, "--")) { - break; - } else if (!strcmp(arg, "--color=auto") || - !strcmp(arg, "--colour=auto")) { - color = COLOR_OPTION_AUTO; - } else if (!strcmp(arg, "--color") || - !strcmp(arg, "--colour") || - !strcmp(arg, "--color=always") || - !strcmp(arg, "--colour=always")) { - color = COLOR_OPTION_ALWAYS; - } else if (!strcmp(arg, "--color=never") || - !strcmp(arg, "--colour=never") || - !strcmp(arg, "--no-color") || - !strcmp(arg, "--no-colour")) { - color = COLOR_OPTION_NEVER; - } else { - std::cerr << "error: unknown option " << arg << "\n"; - usage(); - return 1; - } - } - - if (color == COLOR_OPTION_AUTO) { -#ifdef _WIN32 - color = COLOR_OPTION_ALWAYS; -#else - color = isatty(1) ? COLOR_OPTION_ALWAYS : COLOR_OPTION_NEVER; -#endif - } - - for (; i < argc; ++i) { - trace::Parser p; - - if (!p.open(argv[i])) { - std::cerr << "error: failed to open " << argv[i] << "\n"; - return 1; - } - - trace::Call *call; - while ((call = p.parse_call())) { - call->dump(std::cout, color); - delete call; - } - } - - return 0; -} diff --git a/cli/cli.hpp b/cli/cli.hpp new file mode 100644 index 0000000..8e43277 --- /dev/null +++ b/cli/cli.hpp @@ -0,0 +1,42 @@ +/********************************************************************* + * + * Copyright 2011 Intel Corporation + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *********************************************************************/ + +#ifndef _APITRACE_CLI_HPP_ +#define _APITRACE_CLI_HPP_ + +#define APITRACE_DUMP_SYNOPSIS "Dump given trace(s) to standard output." + +void +apitrace_dump_usage(const char *argv0); + +int +apitrace_dump_command(int argc, char *argv[], int first_command_arg); + +void +usage(void); + +#endif /* _APITRACE_CLI_HPP_ */ diff --git a/cli/cli_dump.cpp b/cli/cli_dump.cpp new file mode 100644 index 0000000..fcc08f7 --- /dev/null +++ b/cli/cli_dump.cpp @@ -0,0 +1,111 @@ +/************************************************************************** + * + * Copyright 2010 VMware, Inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + **************************************************************************/ + +#include + +#include "cli.hpp" + +#include "trace_parser.hpp" + +enum ColorOption { + COLOR_OPTION_NEVER = 0, + COLOR_OPTION_ALWAYS = 1, + COLOR_OPTION_AUTO = -1 +}; + +static ColorOption color = COLOR_OPTION_AUTO; + +void +apitrace_dump_usage(const char *argv0) +{ + std::cout << argv0 << " [OPTIONS] ..." + "\n\n\t" + APITRACE_DUMP_SYNOPSIS + "\n\n\t" + "Supports the following options:\n\t" + "\t--color=\n\t" + "\t--colour= Colored syntax highlighting\n\t" + "\t WHEN is 'auto', 'always', or 'never'\n"; +} + +int +apitrace_dump_command(int argc, char *argv[], int first_arg_command) +{ + int i; + + for (i = first_arg_command; i < argc; ++i) { + const char *arg = argv[i]; + + if (arg[0] != '-') { + break; + } + + if (!strcmp(arg, "--")) { + break; + } else if (!strcmp(arg, "--color=auto") || + !strcmp(arg, "--colour=auto")) { + color = COLOR_OPTION_AUTO; + } else if (!strcmp(arg, "--color") || + !strcmp(arg, "--colour") || + !strcmp(arg, "--color=always") || + !strcmp(arg, "--colour=always")) { + color = COLOR_OPTION_ALWAYS; + } else if (!strcmp(arg, "--color=never") || + !strcmp(arg, "--colour=never") || + !strcmp(arg, "--no-color") || + !strcmp(arg, "--no-colour")) { + color = COLOR_OPTION_NEVER; + } else { + std::cerr << "error: unknown option " << arg << "\n"; + usage(); + return 1; + } + } + + if (color == COLOR_OPTION_AUTO) { +#ifdef _WIN32 + color = COLOR_OPTION_ALWAYS; +#else + color = isatty(1) ? COLOR_OPTION_ALWAYS : COLOR_OPTION_NEVER; +#endif + } + + for (; i < argc; ++i) { + trace::Parser p; + + if (!p.open(argv[i])) { + std::cerr << "error: failed to open " << argv[i] << "\n"; + return 1; + } + + trace::Call *call; + while ((call = p.parse_call())) { + call->dump(std::cout, color); + delete call; + } + } + + return 0; +} diff --git a/cli/cli_main.cpp b/cli/cli_main.cpp new file mode 100644 index 0000000..944d7de --- /dev/null +++ b/cli/cli_main.cpp @@ -0,0 +1,184 @@ +/********************************************************************* + * + * Copyright 2011 Intel Corporation + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *********************************************************************/ + + +/* + * Top-level application for accessing most all apitrace + * functionality. + */ + +#include + +#include +#include + +#include "cli.hpp" + +#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0])) + +typedef void (*command_usage_t) (const char *argv0); +typedef int (*command_function_t) (int argc, char *argv[], int first_command_arg); + +typedef struct { + const char *name; + const char *synopsis; + command_usage_t usage; + command_function_t function; +} Command; + +#define APITRACE_HELP_SYNOPSIS "Print detailed help for the given command." + +static void +apitrace_help_usage(const char *argv0) +{ + std::cout << argv0 << " []" + "\n\n\t" + APITRACE_HELP_SYNOPSIS + "\n\n\t" + "Except in this case, where this is all the help you will get." + "\n\n"; +} + +static int +apitrace_help_command(int argc, char *argv[], int first_command_arg); + +static Command commands[] = { + { "dump", + APITRACE_DUMP_SYNOPSIS, + apitrace_dump_usage, + apitrace_dump_command }, + { "help", + APITRACE_HELP_SYNOPSIS, + apitrace_help_usage, + apitrace_help_command } +}; + +void +usage(void) +{ + Command *command; + int i, max_width = 0; + + std::cout << + "Usage: apitrace [ ...]\n\n" + "The available commands are as follows:\n\n"; + + std::cout << std::setiosflags(std::ios::left); + + for (i = 0; i < ARRAY_SIZE(commands); i++) { + command = &commands[i]; + if (strlen(command->name) > max_width) + max_width = strlen(command->name); + } + + for (i = 0; i < ARRAY_SIZE(commands); i++) { + command = &commands[i]; + + std::cout << " " << std::setw(max_width+2) << command->name + << " " << command->synopsis << "\n"; + } + + std::cout << "\n" + "Use \"apitrace help \" for more details on each command.\n"; +} + +static int +apitrace_help_command(int argc, char *argv[], int first_arg_command) +{ + Command *command; + int i; + + if(first_arg_command == argc) { + usage(); + return 0; + } + + char *command_name = argv[first_arg_command]; + + for (i = 0; i < ARRAY_SIZE(commands); i++) { + command = &commands[i]; + + if (strcmp(command_name, command->name) == 0) { + std::cout << "Help for \"apitrace " << command_name << "\":\n\n"; + (command->usage) ("apitrace"); + + return 0; + } + } + + std::cerr << "Error: Unknown command: " << command_name + << " (see \"apitrace help\").\n"; + + return 1; +} + +int +main(int argc, char **argv) +{ + const char *command_name = "trace"; + Command *command; + int i, first_command_arg; + + if (argc == 1) { + usage(); + return 1; + } + + for (i = 1; i < argc; ++i) { + const char *arg = argv[i]; + + if (arg[0] != '-') { + break; + } + + if (strcmp(arg, "--help") == 0) { + return apitrace_help_command (0, NULL, 0); + } else { + std::cerr << "Error: unknown option " << arg << "\n"; + usage(); + return 1; + } + } + first_command_arg = i; + + if (first_command_arg < argc) { + command_name = argv[first_command_arg]; + first_command_arg++; + } + + for (i = 0; i < ARRAY_SIZE(commands); i++) { + command = &commands[i]; + + if (strcmp(command_name, command->name) == 0) + return (command->function) (argc, argv, first_command_arg); + } + + std::cerr << "Error: unknown command " << command_name + << " (see \"apitrace help\").\n"; + + return 1; +}