diff options
Diffstat (limited to 'userspace/src/args.c')
-rw-r--r-- | userspace/src/args.c | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/userspace/src/args.c b/userspace/src/args.c new file mode 100644 index 00000000..d91c6f6e --- /dev/null +++ b/userspace/src/args.c | |||
@@ -0,0 +1,134 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <stdio.h> | ||
24 | #include <getopt.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <string.h> | ||
27 | |||
28 | #include <unit/core.h> | ||
29 | #include <unit/args.h> | ||
30 | #include <unit/io.h> | ||
31 | |||
32 | static struct option core_opts[] = { | ||
33 | { "help", 0, NULL, 'h' }, | ||
34 | { "verbose", 0, NULL, 'v' }, | ||
35 | { "quiet", 0, NULL, 'q' }, | ||
36 | { "no-color", 0, NULL, 'C' }, | ||
37 | |||
38 | { "unit-load-path", 1, NULL, 'L' }, | ||
39 | |||
40 | { NULL, 0, NULL, 0 } | ||
41 | }; | ||
42 | |||
43 | static const char *core_opts_str = "hvqCL:"; | ||
44 | |||
45 | void core_print_help(struct unit_fw *fw) | ||
46 | { | ||
47 | const char **line, *help_msg[] = { | ||
48 | "NvGpu Unit Testing FW. Basic usage\n", | ||
49 | "\n", | ||
50 | " $ nvgpu_unit [options] <unit>\n", | ||
51 | "\n", | ||
52 | "Basic usage consists of one or more options and a particular unit test to\n", | ||
53 | "execute.\n", | ||
54 | "\n", | ||
55 | "Available options are as follows:\n", | ||
56 | "\n", | ||
57 | " -h, --help Print this help message and exit.\n", | ||
58 | " -v, --verbose Increment the verbosity level. Can be specified\n", | ||
59 | " multiple times.\n", | ||
60 | " -q, --quiet Set the verbose level back to 0.\n", | ||
61 | " -C, --no-color Disable color printing; for example, if writing\n", | ||
62 | " output to a file the color escape sequences will\n", | ||
63 | " corrupt that file.\n", | ||
64 | " -L, --unit-load-path <PATH>\n", | ||
65 | " Path to where the unit test libraries reside.\n", | ||
66 | "\n", | ||
67 | "Note: mandatory arguments to long arguments are mandatory for short\n", | ||
68 | "arguments as well.\n", | ||
69 | NULL | ||
70 | }; | ||
71 | |||
72 | line = help_msg; | ||
73 | while (*line != NULL) { | ||
74 | core_msg(fw, "%s", *line); | ||
75 | line++; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | static void set_arg_defaults(struct unit_fw_args *args) | ||
80 | { | ||
81 | args->unit_load_path = DEFAULT_ARG_UNIT_LOAD_PATH; | ||
82 | } | ||
83 | |||
84 | /* | ||
85 | * Parse command line arguments. | ||
86 | */ | ||
87 | int core_parse_args(struct unit_fw *fw, int argc, char **argv) | ||
88 | { | ||
89 | int c, opt_index; | ||
90 | struct unit_fw_args *args; | ||
91 | |||
92 | args = malloc(sizeof(*args)); | ||
93 | if (!args) | ||
94 | return -1; | ||
95 | |||
96 | memset(args, 0, sizeof(*args)); | ||
97 | set_arg_defaults(args); | ||
98 | |||
99 | fw->args = args; | ||
100 | |||
101 | while (1) { | ||
102 | c = getopt_long(argc, argv, | ||
103 | core_opts_str, core_opts, &opt_index); | ||
104 | |||
105 | if (c == -1) | ||
106 | break; | ||
107 | |||
108 | switch (c) { | ||
109 | case 'h': | ||
110 | args->help = true; | ||
111 | break; | ||
112 | case 'v': | ||
113 | args->verbose_lvl += 1; | ||
114 | break; | ||
115 | case 'q': | ||
116 | args->verbose_lvl = 0; | ||
117 | break; | ||
118 | case 'C': | ||
119 | args->no_color = true; | ||
120 | break; | ||
121 | case 'L': | ||
122 | args->unit_load_path = optarg; | ||
123 | break; | ||
124 | case '?': | ||
125 | args->help = true; | ||
126 | return -1; | ||
127 | default: | ||
128 | core_err(fw, "bug?!\n"); | ||
129 | return -1; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | return 0; | ||
134 | } | ||