1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
/*
* NEARDAL Tester command line interpreter
*
* Copyright 2012-2014 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#define _GNU_SOURCE
#define _POSIX_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <glib.h>
#include "ncl.h"
#include "ncl_cmd.h"
#define PROMPT_PREFIX "NCL> "
#define NB_MAX_PARAMETERS 20 /* Max number of parameters in a
command */
#define NB_COLUMN 16
NCLContext gNclCtx;
static void ncl_trace_prv_dump_data_as_binary_format(char *bufToReadP,
int remainingSize,
GString *bufDestP,
int nbColumn)
{
int offset = 0;
while (offset < nbColumn && offset < remainingSize) {
g_string_append_printf(bufDestP, "%02hhX ", bufToReadP[offset]);
offset++;
}
/* Adding space to align ascii format */
if (offset < nbColumn) {
/* 3 space because each byte in binary format as 2 digit and
1 space */
remainingSize = (nbColumn - offset) * 3;
offset = 0;
while ((offset++) < remainingSize)
g_string_append_c(bufDestP, ' ');
}
}
static void ncl_trace_prv_dump_data_as_ascii_format(char *bufToReadP,
int remainingSize,
GString *bufDestP, int nbColumn)
{
int offset = 0;
while (offset < nbColumn && offset < remainingSize) {
if (g_ascii_isprint(((unsigned char) bufToReadP[offset])))
g_string_append_c(bufDestP,
((unsigned char) bufToReadP[offset]));
else
g_string_append_c(bufDestP, '.');
offset++;
}
/* Adding space to finish ascii column */
if (offset < nbColumn) {
remainingSize = nbColumn - offset;
offset = 0;
while ((offset++) < remainingSize)
g_string_append_c(bufDestP, '.');
}
}
void ncl_trace_dump_mem(char *bufToReadP, int size)
{
char *memP = bufToReadP;
int len = size;
int offset = 0;
GString *bufTrace;
if (!memP || size <= 0)
return;
offset = 0;
bufTrace = g_string_new(NULL);
while (len > 0) {
g_string_append_printf(bufTrace, "%08lX : ",
(unsigned long) (&bufToReadP[offset]));
ncl_trace_prv_dump_data_as_binary_format(&bufToReadP[offset],
len, bufTrace,
NB_COLUMN);
ncl_trace_prv_dump_data_as_ascii_format(&bufToReadP[offset],
len, bufTrace,
NB_COLUMN);
NCL_CMD_PRINT("%s\n", bufTrace->str);
len -= NB_COLUMN;
offset += NB_COLUMN;
g_string_truncate(bufTrace, 0);
}
g_string_free(bufTrace, TRUE);
}
char *ncl_error_get_text(NCLError ec)
{
switch (ec) {
case NCLERR_NOERROR:
return "Success";
case NCLERR_GLOBAL_ERROR:
return "General error";
case NCLERR_PARSING_PARAMETERS:
return "Invalid parameter";
case NCLERR_MEM_ALLOC:
return "Memory allocation error";
case NCLERR_INIT:
return "Error while initializing command line interpretor";
case NCLERR_LIB_ERROR:
return "Error from Linked Library";
default:
return "UNKNOWN ERROR !!!";
}
}
static NCLError ncl_prv_split_cmdLine(gchar *cmdLine, int *iArgc,
char *iArgv[])
{
NCLError err = NCLERR_NOERROR;
char **argv = NULL;
int *argc;
char *argEnd;
char *argStart;
bool inQuotes;
gssize argSize;
int endParsing = FALSE;
/* Test input parameters */
if (!cmdLine || !iArgc || !iArgv)
return NCLERR_PARSING_PARAMETERS;
/* Splitting parameters list like argc/argv style */
argv = iArgv;
argc = iArgc;
*argc = 0;
inQuotes = false;
argStart = argEnd = cmdLine;
while (endParsing == FALSE && *argc < NB_MAX_PARAMETERS && *argEnd) {
while (*argEnd != ' ' && *argEnd != '"' && *argEnd != '\0')
argEnd++;
if (*argEnd == '"') {
if (inQuotes == false)
argStart = argEnd + 1;
inQuotes = !inQuotes;
}
if (inQuotes == false) {
if (*argEnd == '\0')
endParsing = TRUE;
*argEnd = '\0';
argSize = argEnd - argStart;
if (argSize > 0)
((char **)(argv))[(*argc)++] = argStart;
argEnd++;
argStart = argEnd;
} else
argEnd++;
}
return err;
}
static ncl_cmd_func ncl_prv_find_func(char *cmd)
{
int index;
NCLCmdInterpretor *it; /* commands interpretor array */
int nbClCmd;
it = ncl_cmd_get_list(&nbClCmd);
if (it == NULL || cmd == NULL)
return NULL;
for (index = 0; index < nbClCmd; index++)
if (!strncmp(it[index].cmdName, cmd, strlen(it[index].cmdName)))
return it[index].func;
return NULL;
}
NCLError ncl_exec(char *cmdName)
{
NCLError ret = NCLERR_NOERROR;
GError *gerror = NULL;
ncl_cmd_func funcList = NULL;
char *cmd = NULL;
char *argv[NB_MAX_PARAMETERS];
int argc;
/* Duplicate Command line before split */
cmd = g_strdup(cmdName);
if (cmd == NULL)
return NCLERR_MEM_ALLOC;
/* Invoking 'list' command to display interpretor commands list */
memset(argv, 0, sizeof(argv));
ret = ncl_prv_split_cmdLine(cmd, &argc, argv);
if (ret != NCLERR_NOERROR) {
NCL_CMD_PRINTERR("Error while parsing '%s'\n", cmdName);
goto error;
}
funcList = ncl_prv_find_func(argv[0]);
if (funcList != NULL) {
ret = (*funcList)(argc, argv);
if (ret < NCLERR_NOERROR)
NCL_CMD_PRINTERR(
"Error command '%s' return err %d (%s)\n",
argv[0], ret, ncl_error_get_text(ret));
} else {
NCL_CMD_PRINTERR("Unknow NCL function '%s', trying shell...\n",
cmdName);
g_spawn_command_line_async(cmdName, &gerror);
if (gerror != NULL) {
NCL_CMD_PRINTERR("Shell return error %d:%s\n",
gerror->code, gerror->message);
g_error_free(gerror);
}
}
g_free(cmd);
return ret;
error:
if (cmd != NULL)
g_free(cmd);
return ret;
}
static gboolean ncl_prv_kbinput_cb(GIOChannel *source, GIOCondition condition,
gpointer data)
{
NCLContext *nclCtx = (NCLContext *) data;
GError *error = NULL;
switch (condition) {
case G_IO_IN: {
gsize terminator_pos;
GIOStatus status;
NCLCmdContext *nclCmdCtx;
nclCmdCtx = ncl_cmd_get_ctx();
if (!nclCmdCtx)
return FALSE;
status = g_io_channel_read_line_string(source,
nclCmdCtx->clBuf,
&terminator_pos,
&error);
(void) status; /* Remove warning */
if (nclCmdCtx->clBuf->str) {
nclCmdCtx->clBuf->str[terminator_pos] = '\0';
if (nclCmdCtx->clBuf->str[0] != '\0') {
nclCtx->errOnExit = NCLERR_PARSING_PARAMETERS;
nclCtx->errOnExit = ncl_exec(
nclCmdCtx->clBuf->str);
}
g_string_erase(nclCmdCtx->clBuf, 0, -1);
g_string_append_c(nclCmdCtx->clBuf, '\0');
} else
NCL_CMD_PRINTERR("buf is NULL!!!\n");
}
break;
case G_IO_PRI:
case G_IO_ERR:
case G_IO_HUP:
case G_IO_NVAL:
default:
NCL_CMD_PRINTERR("unhandled condition (%d)\n", condition);
break;
}
ncl_prompt();
return TRUE;
}
void ncl_prompt(void)
{
NCL_CMD_PRINT(PROMPT_PREFIX);
}
NCLContext *ncl_get_ctx(void)
{
return &gNclCtx;
}
void ncl_finalize(void)
{
NCL_CMD_PRINTIN();
/* Freeing command line interpretor context */
ncl_cmd_finalize();
if (gNclCtx.channel) {
g_io_channel_unref(gNclCtx.channel);
g_io_channel_shutdown(gNclCtx.channel, TRUE, NULL);
g_source_remove(gNclCtx.tag);
}
if (gNclCtx.main_loop)
g_main_loop_unref(gNclCtx.main_loop);
}
/*
static void signal_handler(int signum)
{
NCL_CMD_PRINTERR("Receive signal %d\n", signum);
}
*/
static NCLError ncl_prv_init(char *execCmdLineStr)
{
/* struct sigaction sa;
int err = NCLERR_NOERROR; */
/* Initialize Test App context... */
memset(&gNclCtx, 0, sizeof(gNclCtx));
/*
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGINT, &sa, NULL) == -1)
{
NCL_CMD_PRINTERR("Unable to handle system signals\n");
return NCLERR_INIT;
}
*/
gNclCtx.main_loop = g_main_loop_new(NULL, FALSE);
/* Initialize command line interpretor context */
return ncl_cmd_init(execCmdLineStr);
}
static void ncl_prv_parse_script_file(char *scriptFileStr)
{
FILE *scriptFile;
char *cmdLineStr = NULL;
size_t cmdLineSize;
ssize_t nbRead;
/* Opening file */
scriptFile = fopen(scriptFileStr, "r");
if (scriptFile == NULL) {
gNclCtx.errOnExit = NCLERR_GLOBAL_ERROR;
return;
}
do {
/* Reading command line script file */
nbRead = getline(&cmdLineStr, &cmdLineSize, scriptFile);
if (nbRead > 0 && strlen(cmdLineStr) > 1) {
cmdLineSize = strlen(cmdLineStr);
if (cmdLineStr[0] != '#') {
if (cmdLineStr[cmdLineSize - 1] == '\n')
cmdLineStr[cmdLineSize - 1] = '\0';
/* Executing command line */
NCL_CMD_PRINT("$$$$$$$$$$$$$$$$$$$$$$$$$'\n");
NCL_CMD_PRINT("Executing '%s'\n", cmdLineStr);
NCL_CMD_PRINT("$$$$$$$$$$$$$$$$$$$$$$$$$'\n");
gNclCtx.errOnExit = ncl_exec(cmdLineStr);
/* Main loop */
do {
g_main_context_iteration(NULL, false);
} while (g_main_context_pending(NULL));
}
}
/* Freeing command line */
if (cmdLineStr != NULL) {
free(cmdLineStr);
cmdLineStr = NULL;
}
} while (nbRead > 0 && gNclCtx.errOnExit == NCLERR_NOERROR);
fclose(scriptFile);
}
int main(int argc, char *argv[])
{
NCLError err;
int fd;
GOptionContext *context;
GError *error = NULL;
static char *execCmdLineStr;
static char *scriptFileStr;
gboolean opt_keep_running = FALSE;
GOptionEntry options[] = {
{ "exec" , 'e', 0, G_OPTION_ARG_STRING, &execCmdLineStr,
"Execute specific command line function", "Command Line" },
{ "script", 's', 0, G_OPTION_ARG_STRING , &scriptFileStr
, "Script file to execute", "filename" },
{ "keep", 'k', 0, G_OPTION_ARG_NONE, &opt_keep_running,
"Keep running after command execution" },
{ NULL, 0, 0, 0, NULL, NULL, NULL} /* End of List */
};
execCmdLineStr = NULL;
scriptFileStr = NULL;
NCL_CMD_PRINTIN();
NCL_CMD_PRINT("Compiled at %s : %s\n\n", __DATE__, __TIME__);
context = g_option_context_new("");
g_option_context_add_main_entries(context, options, "");
if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
if (error != NULL) {
NCL_CMD_PRINTERR("%s\n", error->message);
g_error_free(error);
} else
NCL_CMD_PRINTERR("An unknown error occurred\n");
return NCLERR_INIT;
}
g_option_context_free(context);
err = ncl_prv_init(execCmdLineStr);
if (err != NCLERR_NOERROR) {
ncl_finalize();
return NCLERR_INIT;
}
/* Do we have a command line in parameters list ? */
if (scriptFileStr == NULL) {
/* No, test application executed without a command line in
parameter. Do we have a command in parameters list ? */
if (execCmdLineStr == NULL) {
keep_running:
/* No, test application executed without a command
line in parameter */
/* Wrap stdin (keyboard) on callback */
fd = fileno(stdin);
gNclCtx.channel = g_io_channel_unix_new(fd);
g_io_channel_set_encoding(gNclCtx.channel, NULL, NULL);
g_io_channel_set_buffered(gNclCtx.channel, TRUE);
gNclCtx.tag = g_io_add_watch(gNclCtx.channel, G_IO_IN,
(GIOFunc) ncl_prv_kbinput_cb,
&gNclCtx);
/* Invoking 'help' command to display commands line
* list */
ncl_exec(LISTCMD_NAME);
ncl_prompt();
/* Launch main-loop */
g_main_loop_run(gNclCtx.main_loop);
} else {
int eventsCount = 0;
/* Yes, test application executed with a command line
* in parameter */
NCL_CMD_PRINTF("Executing command ('%s')...\n",
execCmdLineStr);
gNclCtx.errOnExit = ncl_exec(execCmdLineStr);
NCL_CMD_PRINT(
"Command executed('%s'), processing events...",
execCmdLineStr);
do {
NCL_CMD_PRINT("*");
eventsCount++;
} while (g_main_context_iteration(NULL, false) == true);
NCL_CMD_PRINT("\n");
NCL_CMD_PRINTF("All events have been processed (%d).\n",
eventsCount);
g_free(execCmdLineStr);
execCmdLineStr = NULL;
if (opt_keep_running)
goto keep_running;
}
} else
ncl_prv_parse_script_file(scriptFileStr);
err = gNclCtx.errOnExit;
ncl_finalize();
if (err != NCLERR_NOERROR)
NCL_CMD_PRINTERR("Exit with error %d\n", err);
return err;
}
|