command.c 12.2 KB
Newer Older
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
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
 *   part of this file is taken from libcli (libcli.sourceforge.net)       *
 *   Copyright (C) David Parrish (david@dparrish.com)                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "replacements.h"

#include "command.h"

#include "log.h"
#include "time_support.h"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>

oharboe's avatar
oharboe committed
41
42
#include <openocd_tcl.h>

oharboe's avatar
oharboe committed
43
int fast_and_dangerous = 0;
44
extern command_context_t *active_cmd_ctx;
oharboe's avatar
oharboe committed
45

46
int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
oharboe's avatar
oharboe committed
47
int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48
49
50
static void tcl_output(void *privData, const char *file, int line, const char *function, const char *string)
{		
	Jim_Obj *tclOutput=(Jim_Obj *)privData;
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
	Jim_AppendString(interp, tclOutput, string, strlen(string));
}

static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	/* the private data is stashed in the interp structure */
	command_t *c;
	command_context_t *context;
	int *retval;
	int i;
	int nwords;
	char **words;

	target_call_timer_callbacks_now();
	LOG_USER_N("%s", ""); /* Keep GDB connection alive*/ 
	
	c = interp->cmdPrivData;
	LOG_DEBUG("script_command - %s", c->name);

	nwords = argc;
	words = malloc(sizeof(char *) * nwords);
	for (i = 0; i < nwords; i++)
	{
		int len;

		words[i] = strdup(Jim_GetString(argv[i], &len));
		if (words[i] == NULL) 
		{
			return JIM_ERR;
		}
		LOG_DEBUG("script_command - %s, argv[%u]=%s", c->name, i, words[i]);
	}

	/* grab the command context from the associated data */
	context = Jim_GetAssocData(interp, "context");
	retval = Jim_GetAssocData(interp, "retval"); 
	if (context != NULL && retval != NULL)
	{
		/* capture log output and return it */
		Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
		log_add_callback(tcl_output, tclOutput);
		
		*retval = run_command(context, c, words, nwords);
		
		log_remove_callback(tcl_output, tclOutput);
		Jim_SetResult(interp, tclOutput);
		
	}
ntfreak's avatar
ntfreak committed
100

101
102
103
	for (i = 0; i < nwords; i++)
		free(words[i]);
	free(words);
104

105
106
	return (*retval==ERROR_OK)?JIM_OK:JIM_ERR;
}
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

command_t* register_command(command_context_t *context, command_t *parent, char *name, int (*handler)(struct command_context_s *context, char* name, char** args, int argc), enum command_mode mode, char *help)
{
	command_t *c, *p;
	
	if (!context || !name)
		return NULL;
				
	c = malloc(sizeof(command_t));
	
	c->name = strdup(name);
	c->parent = parent;
	c->children = NULL;
	c->handler = handler;
	c->mode = mode;
oharboe's avatar
oharboe committed
122
123
	if (!help)
		help="";
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
	c->next = NULL;
	
	/* place command in tree */
	if (parent)
	{
		if (parent->children)
		{
			/* find last child */
			for (p = parent->children; p && p->next; p = p->next);
			if (p)
				p->next = c;
		}
		else
		{
			parent->children = c;
		}
	}
	else
	{
		if (context->commands)
		{
			/* find last command */
			for (p = context->commands; p && p->next; p = p->next);
			if (p)
				p->next = c;
		}
		else
		{
			context->commands = c;
		}
	}
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
	
	/* just a placeholder, no handler */
	if (c->handler==NULL)
		return c;

	/* If this is a two level command, e.g. "flash banks", then the
	 * "unknown" proc in startup.tcl must redirect to  this command.
	 * 
	 * "flash banks" is translated by "unknown" to "flash_banks"
	 * if such a proc exists
	 */
	/* Print help for command */
	const char *t1="";
	const char *t2="";
	const char *t3="";
	/* maximum of two levels :-) */
	if (c->parent!=NULL)
	{
		t1=c->parent->name;
		t2="_";
	}
	t3=c->name;
	const char *full_name=alloc_printf("%s%s%s", t1, t2, t3);
	Jim_CreateCommand(interp, full_name, script_command, c, NULL);
	free((void *)full_name);
	
	
oharboe's avatar
oharboe committed
182
183
	/* accumulate help text in Tcl helptext list.  */
    Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
184
185
    if (Jim_IsShared(helptext))
        helptext = Jim_DuplicateObj(interp, helptext);
oharboe's avatar
oharboe committed
186
187
188
189
190
191
192
193
194
195
	Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
	
	Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);

	/* maximum of two levels :-) */
	if (c->parent!=NULL)
	{
		Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->parent->name, -1));
	} 
	Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->name, -1));
196
	
oharboe's avatar
oharboe committed
197
	Jim_ListAppendElement(interp, cmd_entry, cmd_list);
198
	Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
oharboe's avatar
oharboe committed
199
	Jim_ListAppendElement(interp, helptext, cmd_entry);
200
201
202
	return c;
}

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
int unregister_all_commands(command_context_t *context)
{
	command_t *c, *c2;
	
	if (context == NULL)
		return ERROR_OK;
	
	
	while(NULL != context->commands)
	{
		c = context->commands;
		
		while(NULL != c->children)
		{
			c2 = c->children;
			c->children = c->children->next;
			free(c2->name);
			c2->name = NULL;
			free(c2);
			c2 = NULL;
		}
		
		context->commands = context->commands->next;
		
		free(c->name);
		c->name = NULL;
		free(c);
		c = NULL;		
	}
	
	return ERROR_OK;
}

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
int unregister_command(command_context_t *context, char *name)
{
	command_t *c, *p = NULL, *c2;
	
	if ((!context) || (!name))
		return ERROR_INVALID_ARGUMENTS;
	
	/* find command */
	for (c = context->commands; c; c = c->next)
	{
		if (strcmp(name, c->name) == 0)
		{
			/* unlink command */
			if (p)
			{
				p->next = c->next;
			}
			else
			{
				context->commands = c->next;
			}
			
			/* unregister children */
			if (c->children)
			{
				for (c2 = c->children; c2; c2 = c2->next)
				{
					free(c2->name);
					free(c2);
				}
			}
			
			/* delete command */
			free(c->name);
			free(c);
		}
		
		/* remember the last command for unlinking */
		p = c;
	}
	
	return ERROR_OK;
}


ntfreak's avatar
ntfreak committed
281
void command_output_text(command_context_t *context, const char *data)
oharboe's avatar
oharboe committed
282
283
284
285
286
287
{
	if( context && context->output_handler && data  ){
		context->output_handler( context, data );
	}
}

288
void command_print_n(command_context_t *context, char *format, ...)
289
{
290
	char *string;
291
	
292
293
294
	va_list ap;
	va_start(ap, format);

oharboe's avatar
oharboe committed
295
	string = alloc_vprintf(format, ap);
296
	if (string != NULL)
297
	{
298
299
		context->output_handler(context, string);
		free(string);
300
301
	}

302
303
	va_end(ap);
}
304

305
306
void command_print(command_context_t *context, char *format, ...)
{
307
308
	char *string;

309
310
	va_list ap;
	va_start(ap, format);
311

oharboe's avatar
oharboe committed
312
	string = alloc_vprintf(format, ap);
313
314
	if (string != NULL)
	{
oharboe's avatar
oharboe committed
315
		strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
316
317
318
319
		context->output_handler(context, string);
		free(string);
	}

320
	va_end(ap);
321
322
}

323
int run_command(command_context_t *context, command_t *c, char *words[], int num_words)
324
325
{
	int start_word=0;
326
	if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode) ))
327
	{
328
329
		/* Config commands can not run after the config stage */
		return ERROR_FAIL;
330
	}
331
	
332
333
334
	int retval = c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
	if (retval == ERROR_COMMAND_SYNTAX_ERROR)
	{
335
336
337
338
339
340
341
342
343
344
345
346
		/* Print help for command */
		const char *t1="";
		const char *t2="";
		const char *t3="";
		/* maximum of two levels :-) */
		if (c->parent!=NULL)
		{
			t1=c->parent->name;
			t2=" ";
		}
		t3=c->name;
		command_run_linef(context, "help {%s%s%s}", t1, t2, t3);
347
348
349
350
351
352
353
354
355
356
357
358
	}
	else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
	{
		/* just fall through for a shutdown request */
	}
	else if (retval != ERROR_OK)
	{
		/* we do not print out an error message because the command *should*
		 * have printed out an error
		 */
		LOG_DEBUG("Command failed with error code %d", retval); 
	}
359
	
360
	return retval; 
361
362
}

363
int command_run_line(command_context_t *context, char *line)
364
{
365
366
367
368
369
	/* all the parent commands have been registered with the interpreter
	 * so, can just evaluate the line as a script and check for
	 * results
	 */
	/* run the line thru a script engine */
370
	int retval;
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
	int retcode;

	Jim_DeleteAssocData(interp, "context"); /* remove existing */
	retcode = Jim_SetAssocData(interp, "context", NULL, context);
	if (retcode != JIM_OK)
		return ERROR_FAIL;

	/* associated the return value */
	retval = ERROR_OK;
	Jim_DeleteAssocData(interp, "retval"); /* remove existing */
	retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
	if (retcode != JIM_OK)
		return ERROR_FAIL;

	retcode = Jim_Eval(interp, line);	
	if (retcode == JIM_ERR) {
		Jim_PrintErrorMessage(interp);
	} else if (retcode == JIM_EXIT) {
		/* ignore. */
		/* exit(Jim_GetExitCode(interp)); */
	} else {
		const char *result;
		int reslen;

		result = Jim_GetString(Jim_GetResult(interp), &reslen);
		if (reslen) {
			int i;
			char buff[256+1];
			for (i = 0; i < reslen; i += 256)
			{
				int chunk;
				chunk = reslen - i;
				if (chunk > 256)
					chunk = 256;
				strncpy(buff, result+i, chunk);
				buff[chunk] = 0; 
				LOG_USER_N("%s", buff);
			}
			LOG_USER_N("%s", "\n");
		}
ntfreak's avatar
ntfreak committed
411
	}
412
413
414
415
	return retval;
}


oharboe's avatar
oharboe committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
int command_run_linef(command_context_t *context, char *format, ...)
{
	int retval=ERROR_FAIL;
	char *string;
	va_list ap;
	va_start(ap, format);
	string = alloc_vprintf(format, ap);
	if (string!=NULL)
	{
		retval=command_run_line(context, string);
	}
	va_end(ap);
	return retval;
}

oharboe's avatar
oharboe committed
431
432


oharboe's avatar
oharboe committed
433
void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, const char* line), void *priv)
434
435
436
437
438
439
440
441
442
443
{
	context->output_handler = output_handler;
	context->output_handler_priv = priv;
}

command_context_t* copy_command_context(command_context_t* context)
{
	command_context_t* copy_context = malloc(sizeof(command_context_t));

	*copy_context = *context;
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
	return copy_context;
}

int command_done(command_context_t *context)
{
	free(context);
	context = NULL;
	
	return ERROR_OK;
}

command_context_t* command_init()
{
	command_context_t* context = malloc(sizeof(command_context_t));
	
	context->mode = COMMAND_EXEC;
	context->commands = NULL;
	context->current_target = 0;
	context->output_handler = NULL;
	context->output_handler_priv = NULL;
	
	register_command(context, NULL, "sleep", handle_sleep_command,
					 COMMAND_ANY, "sleep for <n> milliseconds");
	
oharboe's avatar
oharboe committed
469
470
471
	register_command(context, NULL, "fast", handle_fast_command,
					 COMMAND_ANY, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
	
472
473
474
	return context;
}

475
476
477
478
479
480
481
482
483
int command_context_mode(command_context_t *cmd_ctx, enum command_mode mode)
{
	if (!cmd_ctx)
		return ERROR_INVALID_ARGUMENTS;

	cmd_ctx->mode = mode;
	return ERROR_OK;
}

484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* sleep command sleeps for <n> miliseconds
 * this is useful in target startup scripts
 */
int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	unsigned long duration = 0;
	
	if (argc == 1)
	{
		duration = strtoul(args[0], NULL, 0);
		usleep(duration * 1000);
	}

	return ERROR_OK;
}

oharboe's avatar
oharboe committed
500
501
502
503
504
505
506
507
508
int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	if (argc!=1)
		return ERROR_COMMAND_SYNTAX_ERROR;
	
	fast_and_dangerous = strcmp("enable", args[0])==0;
	
	return ERROR_OK;
}