summaryrefslogtreecommitdiff
path: root/ncl/ncl.c
blob: 53a0de3019da394c468643a9e47857d02ecbfbab (plain)
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
/*
 *     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 <unistd.h>

#include <glib.h>

#ifdef HAVE_LIBEDIT
#include <editline/readline.h>
#else
#include <readline/readline.h>
#include <readline/history.h>
#endif

#include "ncl.h"
#include "ncl_cmd.h"

#define NCL_PROMPT		"NCL> "
#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 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 *cmd)
{
	NCLError ret = NCLERR_PARSING_PARAMETERS;
	GError *gerr = NULL;
	ncl_cmd_func func = NULL;
	char **argv = NULL;
	int argc;

	if (!g_shell_parse_argv(g_strstrip(cmd), &argc, &argv, &gerr))
		goto exit;

	if (!(func = ncl_prv_find_func(argv[0]))) {
		NCL_CMD_PRINTERR("'%s': Not NCL command, trying shell\n", cmd);
		g_spawn_command_line_async(cmd, &gerr);
		goto exit;
	}

	if ((ret = func(argc, argv)) != NCLERR_NOERROR)
		NCL_CMD_PRINTERR("'%s': %s\n", cmd, ncl_error_get_text(ret));
exit:
	if (gerr) {
		if (gerr->code == G_SHELL_ERROR_EMPTY_STRING)
			ret = NCLERR_NOERROR;
		if (ret != NCLERR_NOERROR)
			NCL_CMD_PRINTERR("%s\n", gerr->message);
		g_error_free(gerr);
	}
	g_strfreev(argv);
	return ret;
}

static void ncl_parse_line(char *line)
{
	add_history(line);
	ncl_exec(line);
	free(line);
}

static gboolean ncl_prv_kbinput_cb(GIOChannel *source, GIOCondition condition,
				   gpointer data)
{
	rl_callback_read_char();
#ifdef HAVE_LIBEDIT
	/* Editline bug workaround: handler install with the original prompt
	   corrects EL_UNBUFFERED state without side-effects. */
	rl_callback_handler_install(NCL_PROMPT, ncl_parse_line);
#endif
	return TRUE;
}

void ncl_prompt(void)
{
	NCL_CMD_PRINT(NCL_PROMPT);
}

NCLContext *ncl_get_ctx(void)
{
	return &gNclCtx;
}


void ncl_finalize(void)
{
	NCL_CMD_PRINTIN();

	/* Freeing command line interpretor context */
	ncl_cmd_finalize();

	rl_callback_handler_remove();

	if (gNclCtx.main_loop)
		g_main_loop_unref(gNclCtx.main_loop);
}

static NCLError ncl_prv_init(char *execCmdLineStr)
{
	/* Initialize Test App context */
	memset(&gNclCtx, 0, sizeof(gNclCtx));
	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 *filename)
{
	char *line = NULL;
	size_t len = 0;
	FILE *file = fopen(filename, "r");

	if (file == NULL) {
		gNclCtx.errOnExit = NCLERR_GLOBAL_ERROR;
		return;
	}

	while (getline(&line, &len, file) != -1) {

		NCL_CMD_PRINT("$$$$$$$$$$$$$$$$$$$$$$$$$'\n");
		NCL_CMD_PRINT("Executing '%s'", line);
		NCL_CMD_PRINT("$$$$$$$$$$$$$$$$$$$$$$$$$'\n");

		if ((gNclCtx.errOnExit = ncl_exec(line)) != NCLERR_NOERROR)
			break;

		while (g_main_context_pending(NULL))
			g_main_context_iteration(NULL, FALSE);
	}
	g_free(line);
	fclose(file);
}

int main(int argc, char *argv[])
{
	NCLError	err;
	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 */
			gNclCtx.channel = g_io_channel_unix_new(STDIN_FILENO);
			gNclCtx.tag = g_io_add_watch(gNclCtx.channel, G_IO_IN,
					(GIOFunc) ncl_prv_kbinput_cb, &gNclCtx);
			g_io_channel_unref(gNclCtx.channel);
			/* Invoking 'help' command to display commands line
			 * list */
			ncl_exec(LISTCMD_NAME);

			rl_callback_handler_install(NCL_PROMPT, ncl_parse_line);
			/* 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;
}