comparison src/g23m-fad/app/app_util.c @ 174:90eb61ecd093

src/g23m-fad: initial import from TCS3.2/LoCosto
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 12 Oct 2016 05:40:46 +0000
parents
children
comparison
equal deleted inserted replaced
173:bf64d785238a 174:90eb61ecd093
1 /*
2 +------------------------------------------------------------------------------
3 | File: app_cmds.c
4 +------------------------------------------------------------------------------
5 | Copyright 2004 Texas Instruments Berlin, AG
6 | All rights reserved.
7 |
8 | This file is confidential and a trade secret of Texas
9 | Instruments Berlin, AG
10 | The receipt of or possession of this file does not convey
11 | any rights to reproduce or disclose its contents or to
12 | manufacture, use, or sell anything it may describe, in
13 | whole, or in part, without the specific written consent of
14 | Texas Instruments Berlin, AG.
15 +-----------------------------------------------------------------------------
16 | Purpose : Utilities and stuff common for the different test parts
17 +-----------------------------------------------------------------------------
18 */
19
20
21 #define APP_UTIL_C
22
23 /*==== INCLUDES =============================================================*/
24
25 #include "app_util.h"
26
27 #include <string.h> /* String functions, e. g. strncpy(). */
28 #include <ctype.h>
29 #include <stdlib.h>
30 #ifndef _SIMULATION_
31 #endif /* _SIMULATION_ */
32 #include "vsi.h" /* A lot of macros. */
33 #ifndef _SIMULATION_
34 #include "custom.h"
35 #include "gsm.h" /* A lot of macros. */
36 #include "tools.h" /* Common tools. */
37 #endif /* _SIMULATION_ */
38
39 /*==== Local defines =========================================================*/
40
41 #define MAX_TOKENS 4 /* Maximum number of tokens to search for;
42 * first token is the command name, last token
43 * the rest of the line. */
44 /*==== Local functions =======================================================*/
45
46 static char **tokenize_cmd(char *command)
47 {
48 /* Three tokens will be enough: command name, parameter 1, parameter 2. Empty
49 * tokens will be NULL. */
50 static char *cmd_token[MAX_TOKENS] ;
51 char *cur ; /* Pointer to current character. */
52
53 int cur_tok ; /* Current token number. */
54
55
56 cur = command ;
57 cur_tok = 0 ;
58 do
59 {
60 while (isspace(*cur)) /* FALSE also for NUl character. */
61 {
62 cur++ ; /* Skip whitespace. */
63 }
64 if (!*cur) /* String terminated. */
65 {
66 cmd_token[cur_tok] = 0 ; /* No token here and stop. */
67 break ;
68 }
69 cmd_token[cur_tok++] = cur ;
70 cmd_token[cur_tok] = 0 ;
71 while (*cur && !isspace(*cur))
72 {
73 cur++ ; /* Skip non-whitespace. */
74 }
75 if (*cur) *cur++ = 0 ; /* Zero-terminate token if not end of string. */
76 }
77 while (cur_tok < MAX_TOKENS) ;
78
79 return cmd_token ;
80 }
81
82 char *string_to_lower(char *s)
83 {
84 char *tmp ;
85
86 for (tmp = s; *tmp; tmp++)
87 {
88 *tmp = tolower(*tmp) ;
89 }
90 return s ;
91 }
92
93 /*==== Exported functions ====================================================*/
94
95 /** Parse a command and execute it if it is valid. Return appropriate error
96 * message if the command is invalid or fails.
97 *
98 * @param command command line to execute
99 * @return an error message or NULL on success
100 */
101 char *app_handle_command(char *command, app_cmd_entry_t * cmd_table)
102 {
103 char **tokened_cmd ; /* Tokenized command line. */
104 int cmd_index ;
105
106 TRACE_FUNCTION("app_handle_command()") ;
107
108 tokened_cmd = tokenize_cmd(command) ;
109 if (!tokened_cmd[0])
110 {
111 return "ERROR: empty command line" ;
112 }
113
114 string_to_lower(tokened_cmd[0]) ; /* convert to lower char */
115 cmd_index = 0 ;
116 while (cmd_table[cmd_index].cmd_name)
117 {
118 if (!strcmp(tokened_cmd[0], cmd_table[cmd_index].cmd_name))
119 {
120 TRACE_EVENT_P4("Call %s(%s, %s, %s)", cmd_table[cmd_index].cmd_name,
121 tokened_cmd[1] ? tokened_cmd[1] : "(null)",
122 tokened_cmd[2] ? tokened_cmd[2] : "(null)",
123 tokened_cmd[3] ? tokened_cmd[3] : "(null)") ;
124 return cmd_table[cmd_index].cmd_func(&cmd_table[cmd_index],
125 tokened_cmd[1],
126 tokened_cmd[2],
127 tokened_cmd[3],
128 cmd_table[cmd_index].core_func) ;
129 }
130 cmd_index++ ;
131 }
132 return "ERROR: command not recognized" ;
133 }
134
135
136
137 /**
138 * Convert a string into an integer.
139 * If not possibly, assume the default value.
140 */
141 int get_item(char *param, int default_item, BOOL can_null)
142 {
143 U32 item = default_item;
144 if (param)
145 {
146 item = atoi(param);
147 if (!can_null AND item EQ 0)
148 {
149 TRACE_EVENT("WARNING: item is 0, setting to default");
150 item = default_item;
151 }
152 }
153 return item;
154 }
155
156
157 /* This is onoy for the case that the macro UI_TRACE() is undefined, in which
158 * case the linker bemoans it as missing. */
159
160 /* EOF */