1 | /* |
2 | Copyright (c) 2013, Broadcom Europe Ltd |
3 | Copyright (c) 2013, James Hughes |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: |
8 | * Redistributions of source code must retain the above copyright |
9 | notice, this list of conditions and the following disclaimer. |
10 | * Redistributions in binary form must reproduce the above copyright |
11 | notice, this list of conditions and the following disclaimer in the |
12 | documentation and/or other materials provided with the distribution. |
13 | * Neither the name of the copyright holder nor the |
14 | names of its contributors may be used to endorse or promote products |
15 | derived from this software without specific prior written permission. |
16 | |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | /** |
30 | * \file RaspiCLI.c |
31 | * Code for handling command line parameters |
32 | * |
33 | * \date 4th March 2013 |
34 | * \Author: James Hughes |
35 | * |
36 | * Description |
37 | * |
38 | * Some functions/structures for command line parameter parsing |
39 | * |
40 | */ |
41 | #include <stdio.h> |
42 | #include <stdlib.h> |
43 | #include <string.h> |
44 | #include <memory.h> |
45 | |
46 | #include "interface/vcos/vcos.h" |
47 | |
48 | #include "RaspiCLI.h" |
49 | |
50 | |
51 | /** |
52 | * Convert a string from command line to a comand_id from the list |
53 | * |
54 | * @param commands Array of command to check |
55 | * @param num_command Number of commands in the array |
56 | * @param arg String to search for in the list |
57 | * @param num_parameters Returns the number of parameters used by the command |
58 | * |
59 | * @return command ID if found, -1 if not found |
60 | * |
61 | */ |
62 | int raspicli_get_command_id(const COMMAND_LIST *commands, const int num_commands, const char *arg, int *num_parameters) |
63 | { |
64 | int command_id = -1; |
65 | int j; |
66 | |
67 | vcos_assert(commands); |
68 | vcos_assert(num_parameters); |
69 | vcos_assert(arg); |
70 | |
71 | if (!commands || !num_parameters || !arg) |
72 | return -1; |
73 | |
74 | for (j = 0; j < num_commands; j++) |
75 | { |
76 | if (!strcmp(arg, commands[j].command) || |
77 | !strcmp(arg, commands[j].abbrev)) |
78 | { |
79 | // match |
80 | command_id = commands[j].id; |
81 | *num_parameters = commands[j].num_parameters; |
82 | break; |
83 | } |
84 | } |
85 | |
86 | return command_id; |
87 | } |
88 | |
89 | |
90 | /** |
91 | * Display the list of commands in help format |
92 | * |
93 | * @param commands Array of command to check |
94 | * @param num_command Number of commands in the arry |
95 | * |
96 | * |
97 | */ |
98 | void raspicli_display_help(const COMMAND_LIST *commands, const int num_commands) |
99 | { |
100 | int i; |
101 | |
102 | vcos_assert(commands); |
103 | |
104 | if (!commands) |
105 | return; |
106 | |
107 | for (i = 0; i < num_commands; i++) |
108 | { |
109 | fprintf(stdout, "-%s, -%s\t: %s\n" , commands[i].abbrev, |
110 | commands[i].command, commands[i].help); |
111 | } |
112 | } |
113 | |
114 | |
115 | /** |
116 | * Function to take a string, a mapping, and return the int equivalent |
117 | * @param str Incoming string to match |
118 | * @param map Mapping data |
119 | * @param num_refs The number of items in the mapping data |
120 | * @return The integer match for the string, or -1 if no match |
121 | */ |
122 | int raspicli_map_xref(const char *str, const XREF_T *map, int num_refs) |
123 | { |
124 | int i; |
125 | |
126 | for (i=0; i<num_refs; i++) |
127 | { |
128 | if (!strcasecmp(str, map[i].mode)) |
129 | { |
130 | return map[i].mmal_mode; |
131 | } |
132 | } |
133 | return -1; |
134 | } |
135 | |
136 | /** |
137 | * Function to take a mmal enum (as int) and return the string equivalent |
138 | * @param en Incoming int to match |
139 | * @param map Mapping data |
140 | * @param num_refs The number of items in the mapping data |
141 | * @return const pointer to string, or NULL if no match |
142 | */ |
143 | const char *raspicli_unmap_xref(const int en, XREF_T *map, int num_refs) |
144 | { |
145 | int i; |
146 | |
147 | for (i=0; i<num_refs; i++) |
148 | { |
149 | if (en == map[i].mmal_mode) |
150 | { |
151 | return map[i].mode; |
152 | } |
153 | } |
154 | return NULL; |
155 | } |
156 | |