2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 #define KEY_CODE_ESCAPE 27
27 #define KEY_CODE_BACKSPACE 127
28 #define KEY_CODE_TAB 9
29 #define KEY_CODE_ARROW_UP 65
30 #define KEY_CODE_ARROW_DOWN 66
31 #define KEY_CODE_ARROW_RIGHT 67
32 #define KEY_CODE_ARROW_LEFT 68
34 /* The list data structure to keep history of the test suite commands */
35 typedef struct __cmd_history_s {
36 struct __cmd_history_s *prev;
37 struct __cmd_history_s *next;
38 char cmd[MAX_COMMAND_LINE_LEN];
41 static _cmd_history_t *_cmd_history_tail = NULL;
42 static _cmd_history_t *_cmd_history_curp = NULL;
43 static char OUTPUT_LOG_FILE[MAX_PATH_LEN] = "out.log";
47 printf("Usage: sound_pool_test [options] \n");
48 printf("Test Suite for testing Tizen SoundPool module \n");
49 printf("-h, --help - shows this help; \n");
50 printf("-s, --script=SCRIPT - executes commands from the text file \n"
51 " directly after start; \n");
52 printf("-t, --target=TARGET(S) - selects the target or targets to \n"
53 " output logs. Possible targets are: \n"
54 " stderr, dlog, file. You can also \n"
55 " select multiple targets using coma. \n"
57 " > sound_pool_test -t stderr,file \n"
58 " will output both in standard \n"
59 " error and file; \n");
60 printf("-f, --file=FILEPATH - selects file for file output logging.\n"
61 " Works only in -t argument value \n"
62 " includes 'file' target; \n");
65 _cmd_history_t *add_cmd_to_history(const char *cmd)
70 if (!_cmd_history_tail) {
71 _cmd_history_tail = malloc(sizeof(_cmd_history_t));
72 if (!_cmd_history_tail)
74 _cmd_history_tail->prev = NULL;
75 _cmd_history_tail->next = NULL;
77 _cmd_history_tail->next = malloc(sizeof(_cmd_history_t));
78 if (!_cmd_history_tail->next)
80 _cmd_history_tail->next->prev = _cmd_history_tail;
81 _cmd_history_tail->next->next = NULL;
82 _cmd_history_tail = _cmd_history_tail->next;
85 strncpy(_cmd_history_tail->cmd, cmd, MAX_COMMAND_LINE_LEN - 1);
86 _cmd_history_tail->cmd[MAX_COMMAND_LINE_LEN - 1] = '\0';
87 _cmd_history_curp = _cmd_history_tail;
88 return _cmd_history_tail;
91 const char *pop_up_curp_from_cmd_history()
93 if (!_cmd_history_curp)
96 const char *curp_cmd = _cmd_history_curp->cmd;
97 if (_cmd_history_curp->prev)
98 _cmd_history_curp = _cmd_history_curp->prev;
102 const char *pop_down_curp_from_cmd_history()
104 if (!_cmd_history_curp)
107 const char *curp_cmd = _cmd_history_curp->cmd;
108 if (_cmd_history_curp->next)
109 _cmd_history_curp = _cmd_history_curp->next;
113 /* Searches the same part of two strings from the start and returns the length
114 of this part. For example, for the strings "Big Troll" and "Big troll" value
115 4 will be returned (the same part is "Big "). */
116 size_t get_identical_start_len(const char *str1, const char *str2)
118 size_t len1 = strnlen(str1, MAX_COMMAND_LINE_LEN);
119 size_t len2 = strnlen(str2, MAX_COMMAND_LINE_LEN);
121 while (idx < len1 && idx < len2) {
122 if (str1[idx] != str2[idx])
129 size_t auto_fill(const char *cmd_start, char fill_hint[MAX_COMMAND_LINE_LEN])
132 size_t cmd_start_len = strnlen(cmd_start, MAX_COMMAND_LINE_LEN);
134 char auto_fill[MAX_COMMAND_LINE_LEN] = { '\0' };
135 size_t fill_found = 0;
137 for (; idx < CMD_COUNT; ++idx) {
138 if (strncmp(cmd_start, cmd_list[idx], cmd_start_len) == 0) {
140 strncpy(auto_fill, cmd_list[idx], MAX_COMMAND_LINE_LEN);
141 fill_found = strnlen(auto_fill, MAX_COMMAND_LINE_LEN);
143 fill_found = get_identical_start_len(auto_fill, cmd_list[idx]);
144 auto_fill[fill_found] = '\0';
150 strncpy(fill_hint, cmd_start, MAX_COMMAND_LINE_LEN);
152 strncpy(fill_hint, auto_fill, MAX_COMMAND_LINE_LEN);
157 size_t print_cmd_prompt()
159 const char *prompt = "sound-pool-ts-cmd > ";
160 printf("%s", prompt);
161 /* Return the length of comand prompt c-string */
162 return strnlen(prompt, MAX_COMMAND_LINE_LEN);
167 struct termios oldattr, newattr;
169 tcgetattr(STDIN_FILENO, &oldattr);
171 newattr.c_lflag &= ~(ICANON | ECHO);
172 tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
174 tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
178 size_t ts_getline(char *cmd_line)
185 /* Read whole line */
189 while ((c = getch()) != EOF && (cc = (char)c) != '\n' &&
190 ci < MAX_COMMAND_LINE_LEN - 1) {
191 if (cc == KEY_CODE_ESCAPE) { /* Arrow keys */
196 cc = (char)c; /* Here is particular arrow code */
197 const char *curp_cmd = NULL;
198 if (cc == KEY_CODE_ARROW_UP) {
199 if ((curp_cmd = pop_up_curp_from_cmd_history())) {
202 printf("%s", curp_cmd);
203 strncpy(cmd_line, curp_cmd, MAX_COMMAND_LINE_LEN);
204 ci = strnlen(curp_cmd, MAX_COMMAND_LINE_LEN);
206 } else if (cc == KEY_CODE_ARROW_DOWN) {
207 if ((curp_cmd = pop_down_curp_from_cmd_history())) {
210 printf("%s", curp_cmd);
211 strncpy(cmd_line, curp_cmd, MAX_COMMAND_LINE_LEN);
212 ci = strnlen(curp_cmd, MAX_COMMAND_LINE_LEN);
217 if (cc == KEY_CODE_BACKSPACE) { /* Backspace key */
220 cmd_line[ci--] = '\0';
224 if (cc == KEY_CODE_TAB) { /* Tab key */
225 size_t hint_len = auto_fill(cmd_line, cmd_line);
226 while (ci < hint_len)
227 printf("%c", cmd_line[ci++]);
241 int main(int argc, char* argv[])
243 _logger_set_log_tag("TIZEN_SOUND_POOL_TEST_SUITE");
244 _logger_set_logging_mode(LOG_MODE_STDERR | LOG_MODE_DLOG);
246 # define OPTIONS "s:f:t:h"
247 struct option options[] = {
248 { "script", required_argument, NULL, 's' },
249 { "logfile", required_argument, NULL, 'f' },
250 { "target", required_argument, NULL, 't' },
251 { "help", no_argument, NULL, 'h' },
255 char commands[MAX_COMMAND_LINE_LEN] = { '\0' };
256 size_t last_cmd_end = 0;
259 int do_print_usage = 0;
261 while ((opt = getopt_long(argc, argv, OPTIONS, options, NULL)) != -1) {
264 snprintf(commands + last_cmd_end, MAX_COMMAND_LINE_LEN,
265 "%s %s\n", CMD_EXECUTE_SCRIPT, optarg);
267 strnlen(commands + last_cmd_end, MAX_COMMAND_LINE_LEN) + 1;
271 _logger_set_file(optarg);
276 int log_mode = LOG_MODE_NONE;
277 char *token = strtok(optarg, ",");
279 while (token != NULL) {
280 if (strncmp(token, "stderr", MAX_MSG_LEN) == 0) {
281 log_mode |= LOG_MODE_STDERR;
282 } else if (strncmp(token, "file", MAX_MSG_LEN) == 0) {
283 log_mode |= LOG_MODE_FILE;
285 } else if (strncmp(token, "dlog", MAX_MSG_LEN) == 0) {
286 log_mode |= LOG_MODE_DLOG;
288 _printf(CMD_COLOR_RED, "%s target is not supported! "
289 "Use one (or combination separated by coma) "
290 "from the list: dlog, stderr, and dlog.\n", token);
292 token = strtok(NULL, ", ");
295 if (log_mode != LOG_MODE_NONE)
296 _logger_set_logging_mode(log_mode);
309 if (do_print_usage != 0)
313 _logger_set_file(OUTPUT_LOG_FILE);
315 char *ccmd = commands;
317 while (ccmd[0] != '\0') {
318 if (_exec_cmd(ccmd) == UCMD) {
319 _printf(CMD_COLOR_RED,
320 "Unknown command! Type 'help' for command help!\n");
322 ccmd += strnlen(ccmd, MAX_COMMAND_LINE_LEN) + 1;
325 char cmd_line[MAX_COMMAND_LINE_LEN];
326 size_t cmd_line_len = 0;
328 while ((cmd_line_len = ts_getline(cmd_line))) {
329 if (cmd_line_len == 1)
332 /* Try to execute commands parsed from the line: */
333 int res = _exec_cmd(cmd_line);
338 _printf(CMD_COLOR_RED,
339 "Unknown command! Type 'help' for command help!\n");
341 add_cmd_to_history(cmd_line);