- prepare OpenOCD for branching, created ./trunk/
git-svn-id: svn://svn.berlios.de/openocd/trunk@64 b42882b7-edfa-0310-969c-e2dbd0fdcd60
This commit is contained in:
6
src/helper/Makefile.am
Normal file
6
src/helper/Makefile.am
Normal file
@@ -0,0 +1,6 @@
|
||||
INCLUDES = $(all_includes)
|
||||
METASOURCES = AUTO
|
||||
noinst_LIBRARIES = libhelper.a
|
||||
libhelper_a_SOURCES = binarybuffer.c configuration.c log.c interpreter.c command.c time_support.c
|
||||
noinst_HEADERS = binarybuffer.h configuration.h types.h log.h command.h \
|
||||
interpreter.h time_support.h
|
||||
246
src/helper/binarybuffer.c
Normal file
246
src/helper/binarybuffer.c
Normal file
@@ -0,0 +1,246 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2004, 2005 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "log.h"
|
||||
|
||||
#include "binarybuffer.h"
|
||||
|
||||
int buf_set_u32(u8* buffer, unsigned int first, unsigned int num, u32 value);
|
||||
u32 buf_get_u32(u8* buffer, unsigned int first, unsigned int num);
|
||||
u32 flip_u32(u32 value, unsigned int num);
|
||||
|
||||
const unsigned char bit_reverse_table256[] =
|
||||
{
|
||||
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
|
||||
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
|
||||
0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
|
||||
0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
|
||||
0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
|
||||
0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
|
||||
0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
|
||||
0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
|
||||
0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
|
||||
0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
|
||||
0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
|
||||
0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
|
||||
0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
|
||||
0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
|
||||
0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
|
||||
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
|
||||
};
|
||||
|
||||
int buf_set_u32(u8* buffer, unsigned int first, unsigned int num, u32 value)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
if (!buffer)
|
||||
return ERROR_INVALID_ARGUMENTS;
|
||||
|
||||
for (i=first; i<first+num; i++)
|
||||
{
|
||||
if (((value >> (i-first))&1) == 1)
|
||||
buffer[i/8] |= 1 << (i%8);
|
||||
else
|
||||
buffer[i/8] &= ~(1 << (i%8));
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
u32 buf_get_u32(u8* buffer, unsigned int first, unsigned int num)
|
||||
{
|
||||
u32 result = 0;
|
||||
unsigned int i;
|
||||
|
||||
if (!buffer)
|
||||
{
|
||||
ERROR("buffer not initialized");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i=first; i<first+num; i++)
|
||||
{
|
||||
if (((buffer[i/8]>>(i%8))&1) == 1)
|
||||
result |= 1 << (i-first);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
u8* buf_cpy(u8 *from, u8 *to, int size)
|
||||
{
|
||||
int num_bytes = CEIL(size, 8);
|
||||
unsigned int i;
|
||||
|
||||
if (from == NULL)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < num_bytes; i++)
|
||||
to[i] = from[i];
|
||||
|
||||
return to;
|
||||
}
|
||||
|
||||
int buf_cmp(u8 *buf1, u8 *buf2, int size)
|
||||
{
|
||||
int num_bytes = CEIL(size, 8);
|
||||
int i;
|
||||
|
||||
if (!buf1 || !buf2)
|
||||
return 1;
|
||||
|
||||
for (i = 0; i < num_bytes; i++)
|
||||
{
|
||||
if (buf1[i] != buf2[i])
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int buf_cmp_mask(u8 *buf1, u8 *buf2, u8 *mask, int size)
|
||||
{
|
||||
int num_bytes = CEIL(size, 8);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < num_bytes; i++)
|
||||
{
|
||||
if ((buf1[i] & mask[i]) != (buf2[i] & mask[i]))
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u8* buf_set_ones(u8 *buf, int count)
|
||||
{
|
||||
int num_bytes = CEIL(count, 8);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < num_bytes; i++)
|
||||
{
|
||||
if (count >= 8)
|
||||
buf[i] = 0xff;
|
||||
else
|
||||
buf[i] = (1 << count) - 1;
|
||||
|
||||
count -= 8;
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
u8* buf_set_buf(u8 *src, int src_start, u8 *dst, int dst_start, int len)
|
||||
{
|
||||
int src_idx = src_start, dst_idx = dst_start;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
if (((src[src_idx/8] >> (src_idx % 8)) & 1) == 1)
|
||||
dst[dst_idx/8] |= 1 << (dst_idx%8);
|
||||
else
|
||||
dst[dst_idx/8] &= ~(1 << (dst_idx%8));
|
||||
dst_idx++;
|
||||
src_idx++;
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
u32 flip_u32(u32 value, unsigned int num)
|
||||
{
|
||||
u32 c;
|
||||
|
||||
c = (bit_reverse_table256[value & 0xff] << 24) |
|
||||
(bit_reverse_table256[(value >> 8) & 0xff] << 16) |
|
||||
(bit_reverse_table256[(value >> 16) & 0xff] << 8) |
|
||||
(bit_reverse_table256[(value >> 24) & 0xff]);
|
||||
|
||||
if (num < 32)
|
||||
c = c >> (32 - num);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
char* buf_to_char(u8 *buf, int size)
|
||||
{
|
||||
int char_len = CEIL(size, 8) * 2;
|
||||
char *char_buf = malloc(char_len + 1);
|
||||
int i;
|
||||
int bits_left = size;
|
||||
|
||||
char_buf[char_len] = 0;
|
||||
|
||||
for (i = 0; i < CEIL(size, 8); i++)
|
||||
{
|
||||
if (bits_left < 8)
|
||||
{
|
||||
buf[i] &= ((1 << bits_left) - 1);
|
||||
}
|
||||
|
||||
if (((buf[i] & 0x0f) >= 0) && ((buf[i] & 0x0f) <= 9))
|
||||
char_buf[char_len - 2*i - 1] = '0' + (buf[i] & 0xf);
|
||||
else
|
||||
char_buf[char_len - 2*i - 1] = 'a' + (buf[i] & 0xf) - 10;
|
||||
|
||||
if (((buf[i] & 0xf0) >> 4 >= 0) && ((buf[i] & 0xf0) >> 4 <= 9))
|
||||
char_buf[char_len - 2*i - 2] = '0' + ((buf[i] & 0xf0) >> 4);
|
||||
else
|
||||
char_buf[char_len - 2*i - 2] = 'a' + ((buf[i] & 0xf0) >> 4) - 10;
|
||||
|
||||
}
|
||||
|
||||
return char_buf;
|
||||
}
|
||||
|
||||
int char_to_buf(char *buf, int len, u8 *bin_buf, int buf_size)
|
||||
{
|
||||
int bin_len = CEIL(len, 2);
|
||||
int i;
|
||||
|
||||
if (buf_size < CEIL(bin_len, 8))
|
||||
return 0;
|
||||
|
||||
if (len % 2)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < strlen(buf); i++)
|
||||
{
|
||||
u32 tmp;
|
||||
sscanf(buf + 2*i, "%2x", &tmp);
|
||||
bin_buf[i] = tmp & 0xff;
|
||||
}
|
||||
|
||||
return bin_len * 8;
|
||||
}
|
||||
|
||||
int buf_to_u32_handler(u8 *in_buf, void *priv)
|
||||
{
|
||||
u32 *dest = priv;
|
||||
|
||||
*dest = buf_get_u32(in_buf, 0, 32);
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
48
src/helper/binarybuffer.h
Normal file
48
src/helper/binarybuffer.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2004, 2005 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#ifndef BINARYBUFFER_H
|
||||
#define BINARYBUFFER_H
|
||||
|
||||
#include "types.h"
|
||||
|
||||
/* support functions to access arbitrary bits in a byte array
|
||||
* flip_u32 inverses the bit order inside a 32-bit word (31..0 -> 0..31)
|
||||
*/
|
||||
|
||||
extern int buf_set_u32(u8* buffer, unsigned int first, unsigned int num, u32 value);
|
||||
extern u32 buf_get_u32(u8* buffer, unsigned int first, unsigned int num);
|
||||
|
||||
extern u32 flip_u32(u32 value, unsigned int num);
|
||||
|
||||
extern int buf_cmp(u8 *buf1, u8 *buf2, int size);
|
||||
extern int buf_cmp_mask(u8 *buf1, u8 *buf2, u8 *mask, int size);
|
||||
extern u8* buf_cpy(u8 *from, u8 *to, int size);
|
||||
|
||||
extern u8* buf_set_ones(u8 *buf, int count);
|
||||
extern u8* buf_set_buf(u8 *src, int src_start, u8 *dst, int dst_start, int len);
|
||||
|
||||
extern char* buf_to_char(u8 *buf, int size);
|
||||
extern int char_to_buf(char *buf, int len, u8 *bin_buf, int buf_size);
|
||||
|
||||
extern int buf_to_u32_handler(u8 *in_buf, void *priv);
|
||||
|
||||
#define CEIL(m, n) ((m + n - 1) / n)
|
||||
|
||||
#endif /* BINARYBUFFER_H */
|
||||
508
src/helper/command.c
Normal file
508
src/helper/command.c
Normal file
@@ -0,0 +1,508 @@
|
||||
/***************************************************************************
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#include "command.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
|
||||
|
||||
int build_unique_lenghts(command_context_t *context, command_t *commands)
|
||||
{
|
||||
command_t *c, *p;
|
||||
|
||||
/* iterate through all commands */
|
||||
for (c = commands; c; c = c->next)
|
||||
{
|
||||
/* find out how many characters are required to uniquely identify a command */
|
||||
for (c->unique_len = 1; c->unique_len <= strlen(c->name); c->unique_len++)
|
||||
{
|
||||
int foundmatch = 0;
|
||||
|
||||
/* for every command, see if the current length is enough */
|
||||
for (p = commands; p; p = p->next)
|
||||
{
|
||||
/* ignore the command itself */
|
||||
if (c == p)
|
||||
continue;
|
||||
|
||||
/* compare commands up to the current length */
|
||||
if (strncmp(p->name, c->name, c->unique_len) == 0)
|
||||
foundmatch++;
|
||||
}
|
||||
|
||||
/* when none of the commands matched, we've found the minimum length required */
|
||||
if (!foundmatch)
|
||||
break;
|
||||
}
|
||||
|
||||
/* if the current command has children, build the unique lengths for them */
|
||||
if (c->children)
|
||||
build_unique_lenghts(context, c->children);
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
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;
|
||||
if (help)
|
||||
c->help = strdup(help);
|
||||
else
|
||||
c->help = NULL;
|
||||
c->unique_len = 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/* update unique lengths */
|
||||
build_unique_lenghts(context, (parent) ? parent : context->commands);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
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);
|
||||
if (c2->help)
|
||||
free(c2->help);
|
||||
free(c2);
|
||||
}
|
||||
}
|
||||
|
||||
/* delete command */
|
||||
free(c->name);
|
||||
if (c->help)
|
||||
free(c->help);
|
||||
free(c);
|
||||
}
|
||||
|
||||
/* remember the last command for unlinking */
|
||||
p = c;
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int parse_line(char *line, char *words[], int max_words)
|
||||
{
|
||||
int nwords = 0;
|
||||
char *p = line;
|
||||
char *word_start = line;
|
||||
int inquote = 0;
|
||||
|
||||
while (nwords < max_words - 1)
|
||||
{
|
||||
/* check if we reached
|
||||
* a terminating NUL
|
||||
* a matching closing quote character " or '
|
||||
* we're inside a word but not a quote, and the current character is whitespace
|
||||
*/
|
||||
if (!*p || *p == inquote || (word_start && !inquote && isspace(*p)))
|
||||
{
|
||||
/* we're inside a word or quote, and reached its end*/
|
||||
if (word_start)
|
||||
{
|
||||
int len = p - word_start;
|
||||
|
||||
/* copy the word */
|
||||
memcpy(words[nwords] = malloc(len + 1), word_start, len);
|
||||
/* add terminating NUL */
|
||||
words[nwords++][len] = 0;
|
||||
}
|
||||
|
||||
/* we're done parsing the line */
|
||||
if (!*p)
|
||||
break;
|
||||
|
||||
/* skip over trailing quote or whitespace*/
|
||||
if (inquote || isspace(*p))
|
||||
p++;
|
||||
|
||||
inquote = 0;
|
||||
word_start = 0;
|
||||
}
|
||||
else if (*p == '"' || *p == '\'')
|
||||
{
|
||||
/* we've reached the beginning of a quote */
|
||||
inquote = *p++;
|
||||
word_start = p;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* we've reached the beginning of a new word */
|
||||
if (!word_start)
|
||||
word_start = p;
|
||||
|
||||
/* normal character, skip */
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
return nwords;
|
||||
}
|
||||
|
||||
void command_print(command_context_t *context, char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *buffer = NULL;
|
||||
int n, size = 0;
|
||||
char *p;
|
||||
|
||||
va_start(ap, format);
|
||||
|
||||
/* process format string */
|
||||
/* TODO: possible bug. va_list is undefined after the first call to vsnprintf */
|
||||
while (!buffer || (n = vsnprintf(buffer, size, format, ap)) >= size)
|
||||
{
|
||||
/* increase buffer until it fits the whole string */
|
||||
if (!(p = realloc(buffer, size += 4096)))
|
||||
return;
|
||||
|
||||
buffer = p;
|
||||
}
|
||||
|
||||
/* vsnprintf failed */
|
||||
if (n < 0)
|
||||
return;
|
||||
|
||||
p = buffer;
|
||||
|
||||
/* process lines in buffer */
|
||||
do {
|
||||
char *next = strchr(p, '\n');
|
||||
|
||||
if (next)
|
||||
*next++ = 0;
|
||||
|
||||
if (context->output_handler)
|
||||
context->output_handler(context, p);
|
||||
|
||||
p = next;
|
||||
} while (p);
|
||||
|
||||
if (buffer)
|
||||
free(buffer);
|
||||
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
int find_and_run_command(command_context_t *context, command_t *commands, char *words[], int num_words, int start_word)
|
||||
{
|
||||
command_t *c;
|
||||
|
||||
for (c = commands; c; c = c->next)
|
||||
{
|
||||
if (strncasecmp(c->name, words[start_word], c->unique_len))
|
||||
continue;
|
||||
|
||||
if (strncasecmp(c->name, words[start_word], strlen(words[start_word])))
|
||||
continue;
|
||||
|
||||
if ((c->mode == context->mode) || (c->mode == COMMAND_ANY))
|
||||
{
|
||||
if (!c->children)
|
||||
{
|
||||
if (!c->handler)
|
||||
{
|
||||
command_print(context, "No handler for command");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
return c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (start_word == num_words - 1)
|
||||
{
|
||||
command_print(context, "Incomplete command");
|
||||
break;
|
||||
}
|
||||
return find_and_run_command(context, c->children, words, num_words, start_word + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
command_print(context, "Command %s not found", words[start_word]);
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int command_run_line(command_context_t *context, char *line)
|
||||
{
|
||||
int nwords;
|
||||
char *words[128] = {0};
|
||||
int retval;
|
||||
int i;
|
||||
|
||||
if ((!context) || (!line))
|
||||
return ERROR_INVALID_ARGUMENTS;
|
||||
|
||||
/* skip preceding whitespace */
|
||||
while (isspace(*line))
|
||||
line++;
|
||||
|
||||
/* empty line, ignore */
|
||||
if (!*line)
|
||||
return ERROR_OK;
|
||||
|
||||
if (context->echo)
|
||||
{
|
||||
command_print(context, "%s", line);
|
||||
}
|
||||
|
||||
nwords = parse_line(line, words, sizeof(words) / sizeof(words[0]));
|
||||
|
||||
if (nwords > 0)
|
||||
retval = find_and_run_command(context, context->commands, words, nwords, 0);
|
||||
else
|
||||
return ERROR_INVALID_ARGUMENTS;
|
||||
|
||||
for (i = 0; i < nwords; i++)
|
||||
free(words[i]);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
int command_run_file(command_context_t *context, FILE *file, enum command_mode mode)
|
||||
{
|
||||
int retval;
|
||||
int old_command_mode;
|
||||
char buffer[4096];
|
||||
|
||||
old_command_mode = context->mode;
|
||||
context->mode = mode;
|
||||
|
||||
while (fgets(buffer, 4096, file))
|
||||
{
|
||||
char *p;
|
||||
char *cmd, *end;
|
||||
|
||||
/* stop processing line after a comment (#, !) or a LF, CR were encountered */
|
||||
if ((p = strpbrk(buffer, "#!\r\n")))
|
||||
*p = 0;
|
||||
|
||||
/* skip over leading whitespace */
|
||||
cmd = buffer;
|
||||
while (isspace(*cmd))
|
||||
cmd++;
|
||||
|
||||
/* empty (all whitespace) line? */
|
||||
if (!*cmd)
|
||||
continue;
|
||||
|
||||
/* search the end of the current line, ignore trailing whitespace */
|
||||
for (p = end = cmd; *p; p++)
|
||||
if (!isspace(*p))
|
||||
end = p;
|
||||
|
||||
/* terminate end */
|
||||
*++end = 0;
|
||||
if (strcasecmp(cmd, "quit") == 0)
|
||||
break;
|
||||
|
||||
/* run line */
|
||||
if (command_run_line(context, cmd) == ERROR_COMMAND_CLOSE_CONNECTION)
|
||||
break;
|
||||
}
|
||||
|
||||
context->mode = old_command_mode;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void command_print_help_line(command_context_t* context, struct command_s *command, int indent)
|
||||
{
|
||||
command_t *c;
|
||||
char indents[32] = {0};
|
||||
char *help = "no help available";
|
||||
char name_buf[64];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < indent; i+=2)
|
||||
{
|
||||
indents[i*2] = ' ';
|
||||
indents[i*2+1] = '-';
|
||||
}
|
||||
indents[i*2] = 0;
|
||||
|
||||
if ((command->mode == COMMAND_EXEC) || (command->mode == COMMAND_ANY))
|
||||
{
|
||||
if (command->help)
|
||||
help = command->help;
|
||||
|
||||
snprintf(name_buf, 64, command->name);
|
||||
strncat(name_buf, indents, 64);
|
||||
command_print(context, "%20s\t%s", name_buf, help);
|
||||
}
|
||||
|
||||
if (command->children)
|
||||
{
|
||||
for (c = command->children; c; c = c->next)
|
||||
{
|
||||
command_print_help_line(context, c, indent + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int command_print_help(command_context_t* context, char* name, char** args, int argc)
|
||||
{
|
||||
command_t *c;
|
||||
|
||||
for (c = context->commands; c; c = c->next)
|
||||
{
|
||||
command_print_help_line(context, c, 0);
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, char* line), void *priv)
|
||||
{
|
||||
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;
|
||||
|
||||
return copy_context;
|
||||
}
|
||||
|
||||
int command_done(command_context_t *context)
|
||||
{
|
||||
free(context);
|
||||
|
||||
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->echo = 0;
|
||||
context->output_handler = NULL;
|
||||
context->output_handler_priv = NULL;
|
||||
|
||||
register_command(context, NULL, "help", command_print_help,
|
||||
COMMAND_EXEC, "display this help");
|
||||
|
||||
register_command(context, NULL, "sleep", handle_sleep_command,
|
||||
COMMAND_ANY, "sleep for <n> milliseconds");
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
67
src/helper/command.h
Normal file
67
src/helper/command.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#ifndef COMMAND_H
|
||||
#define COMMAND_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
enum command_mode
|
||||
{
|
||||
COMMAND_EXEC,
|
||||
COMMAND_CONFIG,
|
||||
COMMAND_ANY,
|
||||
};
|
||||
|
||||
typedef struct command_context_s
|
||||
{
|
||||
enum command_mode mode;
|
||||
struct command_s *commands;
|
||||
int current_target;
|
||||
int echo;
|
||||
int (*output_handler)(struct command_context_s *context, char* line);
|
||||
void *output_handler_priv;
|
||||
} command_context_t;
|
||||
|
||||
typedef struct command_s
|
||||
{
|
||||
char *name;
|
||||
struct command_s *parent;
|
||||
struct command_s *children;
|
||||
int (*handler)(struct command_context_s *context, char* name, char** args, int argc);
|
||||
enum command_mode mode;
|
||||
char *help;
|
||||
int unique_len;
|
||||
struct command_s *next;
|
||||
} command_t;
|
||||
|
||||
extern 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);
|
||||
extern int unregister_command(command_context_t *context, char *name);
|
||||
extern void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, char* line), void *priv);
|
||||
extern command_context_t* copy_command_context(command_context_t* context);
|
||||
extern command_context_t* command_init();
|
||||
extern int command_done(command_context_t *context);
|
||||
extern void command_print(command_context_t *context, char *format, ...);
|
||||
extern int command_run_line(command_context_t *context, char *line);
|
||||
extern int command_run_file(command_context_t *context, FILE *file, enum command_mode mode);
|
||||
|
||||
|
||||
#define ERROR_COMMAND_CLOSE_CONNECTION (-600)
|
||||
|
||||
#endif /* COMMAND_H */
|
||||
131
src/helper/configuration.c
Normal file
131
src/helper/configuration.c
Normal file
@@ -0,0 +1,131 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2004, 2005 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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 "types.h"
|
||||
#include "command.h"
|
||||
#include "configuration.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <getopt.h>
|
||||
|
||||
char* config_file_name;
|
||||
|
||||
static int help_flag;
|
||||
|
||||
static struct option long_options[] =
|
||||
{
|
||||
{"help", no_argument, &help_flag, 1},
|
||||
|
||||
{"debug", optional_argument, 0, 'd'},
|
||||
{"file", required_argument, 0, 'f'},
|
||||
{"log_output", required_argument, 0, 'l'},
|
||||
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
int configuration_output_handler(struct command_context_s *context, char* line)
|
||||
{
|
||||
INFO(line);
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])
|
||||
{
|
||||
int c;
|
||||
char command_buffer[128];
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* getopt_long stores the option index here. */
|
||||
int option_index = 0;
|
||||
|
||||
c = getopt_long(argc, argv, "hd::l:f:", long_options, &option_index);
|
||||
|
||||
/* Detect the end of the options. */
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
case 'h': /* --help | -h */
|
||||
help_flag = 1;
|
||||
break;
|
||||
case 'f': /* --file | -f */
|
||||
config_file_name = optarg;
|
||||
break;
|
||||
case 'd': /* --debug | -d */
|
||||
if (optarg)
|
||||
snprintf(command_buffer, 128, "debug_level %s", optarg);
|
||||
else
|
||||
snprintf(command_buffer, 128, "debug_level 3");
|
||||
command_run_line(cmd_ctx, command_buffer);
|
||||
break;
|
||||
case 'l': /* --log_output | -l */
|
||||
if (optarg)
|
||||
{
|
||||
snprintf(command_buffer, 128, "log_output %s", optarg);
|
||||
command_run_line(cmd_ctx, command_buffer);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (help_flag)
|
||||
{
|
||||
printf("Open On-Chip Debugger\n(c) 2005 by Dominic Rath\n\n");
|
||||
printf("--help | -h\tdisplay this help\n");
|
||||
printf("--file | -f\tuse configuration file <name>\n");
|
||||
printf("--debug | -d\tset debug level <0-3>\n");
|
||||
printf("--log_output | -l\tredirect log output to file <name>\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int parse_config_file(struct command_context_s *cmd_ctx)
|
||||
{
|
||||
FILE *config_file;
|
||||
|
||||
if (!config_file_name)
|
||||
config_file_name = "openocd.cfg";
|
||||
|
||||
config_file = fopen(config_file_name, "r");
|
||||
if (!config_file)
|
||||
{
|
||||
ERROR("couldn't open config file");
|
||||
return ERROR_NO_CONFIG_FILE;
|
||||
}
|
||||
|
||||
command_run_file(cmd_ctx, config_file, COMMAND_CONFIG);
|
||||
|
||||
fclose(config_file);
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
31
src/helper/configuration.h
Normal file
31
src/helper/configuration.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2004, 2005 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#ifndef CONFIGURATION_H
|
||||
#define CONFIGURATION_H
|
||||
|
||||
#include "command.h"
|
||||
#include "types.h"
|
||||
|
||||
extern int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[]);
|
||||
extern int parse_config_file(struct command_context_s *cmd_ctx);
|
||||
extern int configuration_output_handler(struct command_context_s *context, char* line);
|
||||
|
||||
extern char* config_file_name;
|
||||
#endif /* CONFIGURATION_H */
|
||||
237
src/helper/interpreter.c
Normal file
237
src/helper/interpreter.c
Normal file
@@ -0,0 +1,237 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#include "interpreter.h"
|
||||
|
||||
#include "binarybuffer.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
var_t *variables = NULL;
|
||||
|
||||
int handle_var_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
|
||||
int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
|
||||
int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
|
||||
|
||||
int interpreter_register_commands(struct command_context_s *cmd_ctx)
|
||||
{
|
||||
register_command(cmd_ctx, NULL, "var", handle_var_command,
|
||||
COMMAND_ANY, "allocate, display or delete variable <name> [num_fields|'del'] [size1] ...");
|
||||
register_command(cmd_ctx, NULL, "field", handle_field_command,
|
||||
COMMAND_ANY, "display/modify variable field <var> <field> [value|'flip']");
|
||||
register_command(cmd_ctx, NULL, "script", handle_script_command,
|
||||
COMMAND_ANY, "execute commands from <file>");
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
var_t* get_var_by_num(int num)
|
||||
{
|
||||
int count = 0;
|
||||
var_t *var = variables;
|
||||
|
||||
if (var)
|
||||
{
|
||||
if (num == count)
|
||||
return var;
|
||||
while (var->next)
|
||||
{
|
||||
var = var->next;
|
||||
count++;
|
||||
if (num == count)
|
||||
return var;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
var_t* get_var_by_name(char *name)
|
||||
{
|
||||
var_t *var = variables;
|
||||
|
||||
if (var)
|
||||
{
|
||||
if (strcmp(var->name, name) == 0)
|
||||
return var;
|
||||
while (var->next)
|
||||
{
|
||||
var = var->next;
|
||||
if (strcmp(var->name, name) == 0)
|
||||
return var;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
var_t* get_var_by_namenum(char *namenum)
|
||||
{
|
||||
if ((namenum[0] >= '0') && (namenum[0] <= '9'))
|
||||
return get_var_by_num(strtol(namenum, NULL, 0));
|
||||
else
|
||||
return get_var_by_name(namenum);
|
||||
|
||||
}
|
||||
|
||||
int field_le_to_host(u8 *buffer, void *priv)
|
||||
{
|
||||
var_field_t *field = priv;
|
||||
field->value = buf_get_u32(buffer, 0, field->num_bits);
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int handle_var_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
|
||||
{
|
||||
var_t **last_var_p = &variables;
|
||||
int i;
|
||||
|
||||
if (argc >= 2)
|
||||
{
|
||||
while (*last_var_p)
|
||||
{
|
||||
if (strcmp((*last_var_p)->name, args[0]) == 0)
|
||||
{
|
||||
if (strcmp(args[1], "del") == 0)
|
||||
{
|
||||
var_t *next = (*last_var_p)->next;
|
||||
free ((*last_var_p)->fields);
|
||||
free (*last_var_p);
|
||||
*last_var_p = next;
|
||||
command_print(cmd_ctx, "variable %s deleted", args[0]);
|
||||
}
|
||||
else
|
||||
command_print(cmd_ctx, "variable of that name already exists");
|
||||
return ERROR_OK;
|
||||
}
|
||||
last_var_p = &((*last_var_p)->next);
|
||||
}
|
||||
|
||||
if ((args[0][0] >= 0) && (args[0][0] <= 9))
|
||||
{
|
||||
command_print(cmd_ctx, "invalid name specified (first character may not be a number)");
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
*last_var_p = malloc(sizeof(var_t));
|
||||
(*last_var_p)->name = strdup(args[0]);
|
||||
(*last_var_p)->num_fields = argc - 1;
|
||||
(*last_var_p)->next = NULL;
|
||||
|
||||
(*last_var_p)->fields = malloc(sizeof(var_field_t) * (*last_var_p)->num_fields);
|
||||
for (i = 0; i < (*last_var_p)->num_fields; i++)
|
||||
{
|
||||
(*last_var_p)->fields[i].num_bits = strtol(args[1+i], NULL, 0);
|
||||
(*last_var_p)->fields[i].value = 0x0;
|
||||
}
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
var_t *var = get_var_by_namenum(args[0]);
|
||||
if (var)
|
||||
{
|
||||
int i;
|
||||
command_print(cmd_ctx, "%s (%i fields):", var->name, var->num_fields);
|
||||
for (i = 0; i < (var->num_fields); i++)
|
||||
{
|
||||
command_print(cmd_ctx, "0x%x (/%i)", var->fields[i].value, var->fields[i].num_bits);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
command_print(cmd_ctx, "variable %s doesn't exist", args[0]);
|
||||
}
|
||||
}
|
||||
|
||||
if (argc == 0)
|
||||
{
|
||||
var_t *var = variables;
|
||||
int count = 0;
|
||||
while (var)
|
||||
{
|
||||
command_print(cmd_ctx, "%i: %s (%i fields)", count, var->name, var->num_fields);
|
||||
var = var->next;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
|
||||
{
|
||||
|
||||
if (argc < 2)
|
||||
command_print(cmd_ctx, "usage: field <var> <field> [value|'flip']");
|
||||
|
||||
if (argc >= 2)
|
||||
{
|
||||
var_t *var = get_var_by_namenum(args[0]);
|
||||
int field_num = strtol(args[1], NULL, 0);
|
||||
if (!var)
|
||||
{
|
||||
command_print(cmd_ctx, "variable %s doesn't exist", args[0]);
|
||||
return ERROR_OK;
|
||||
}
|
||||
if (field_num >= var->num_fields)
|
||||
command_print(cmd_ctx, "variable field %i is out of bounds (max. %i)", field_num, var->num_fields - 1);
|
||||
if ((var) && (field_num < var->num_fields))
|
||||
{
|
||||
if (argc > 2)
|
||||
{
|
||||
if (strcmp(args[2], "flip") == 0)
|
||||
var->fields[field_num].value = flip_u32(var->fields[field_num].value, var->fields[field_num].num_bits);
|
||||
else
|
||||
var->fields[field_num].value = strtoul(args[2], NULL, 0);
|
||||
}
|
||||
|
||||
command_print(cmd_ctx, "%s(%i): 0x%x (/%i)", var->name, field_num, var->fields[field_num].value, var->fields[field_num].num_bits);
|
||||
}
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
|
||||
{
|
||||
FILE *script_file;
|
||||
int echo;
|
||||
|
||||
if (argc != 1)
|
||||
command_print(cmd_ctx, "usage: script <file>");
|
||||
|
||||
script_file = fopen(args[0], "r");
|
||||
if (!script_file)
|
||||
{
|
||||
command_print(cmd_ctx, "couldn't open script file %s", args[0]);
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
echo = cmd_ctx->echo;
|
||||
cmd_ctx->echo = 1;
|
||||
|
||||
command_run_file(cmd_ctx, script_file, COMMAND_EXEC);
|
||||
|
||||
cmd_ctx->echo = echo;
|
||||
|
||||
fclose(script_file);
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
48
src/helper/interpreter.h
Normal file
48
src/helper/interpreter.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#ifndef INTERPRETER_H
|
||||
#define INTERPRETER_H
|
||||
|
||||
#include "types.h"
|
||||
#include "command.h"
|
||||
#include "log.h"
|
||||
|
||||
typedef struct var_field_s
|
||||
{
|
||||
int num_bits;
|
||||
u32 value;
|
||||
} var_field_t;
|
||||
|
||||
typedef struct var_s
|
||||
{
|
||||
char *name;
|
||||
int num_fields;
|
||||
var_field_t *fields;
|
||||
struct var_s *next;
|
||||
} var_t;
|
||||
|
||||
extern var_t *variables;
|
||||
|
||||
extern int field_le_to_host(u8 *buffer, void *priv);
|
||||
|
||||
extern var_t* get_var_by_namenum(char *namenum);
|
||||
extern int interpreter_register_commands(struct command_context_s *cmd_ctx);
|
||||
|
||||
#endif /* INTERPRETER_H */
|
||||
134
src/helper/log.c
Normal file
134
src/helper/log.c
Normal file
@@ -0,0 +1,134 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#include "log.h"
|
||||
#include "configuration.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
int debug_level = -1;
|
||||
|
||||
static FILE* log_output;
|
||||
|
||||
static char *log_strings[4] =
|
||||
{
|
||||
"Error: ",
|
||||
"Warning:",
|
||||
"Info: ",
|
||||
"Debug: ",
|
||||
};
|
||||
|
||||
void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
char buffer[512];
|
||||
|
||||
if (level > debug_level)
|
||||
return;
|
||||
|
||||
va_start(args, format);
|
||||
vsnprintf(buffer, 512, format, args);
|
||||
|
||||
fprintf(log_output, "%s %s:%d %s(): %s\n", log_strings[level], file, line, function, buffer);
|
||||
fflush(log_output);
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void short_log_printf(enum log_levels level, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
char buffer[512];
|
||||
|
||||
if (level > debug_level)
|
||||
return;
|
||||
|
||||
va_start(args, format);
|
||||
vsnprintf(buffer, 512, format, args);
|
||||
|
||||
fprintf(log_output, "%s %s\n", log_strings[level], buffer);
|
||||
fflush(log_output);
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
/* change the current debug level on the fly
|
||||
* 0: only ERRORS
|
||||
* 1: + WARNINGS
|
||||
* 2: + INFORMATIONAL MSGS
|
||||
* 3: + DEBUG MSGS
|
||||
*/
|
||||
int handle_debug_level_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
|
||||
{
|
||||
if (argc == 0)
|
||||
command_print(cmd_ctx, "debug_level: %i", debug_level);
|
||||
|
||||
if (argc > 0)
|
||||
debug_level = strtoul(args[0], NULL, 0);
|
||||
|
||||
if (debug_level < 0)
|
||||
debug_level = 0;
|
||||
|
||||
if (debug_level > 3)
|
||||
debug_level = 3;
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int handle_log_output_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
|
||||
{
|
||||
if (argc == 1)
|
||||
{
|
||||
FILE* file = fopen(args[0], "w");
|
||||
|
||||
if (file)
|
||||
{
|
||||
log_output = file;
|
||||
}
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int log_register_commands(struct command_context_s *cmd_ctx)
|
||||
{
|
||||
register_command(cmd_ctx, NULL, "log_output", handle_log_output_command,
|
||||
COMMAND_ANY, "redirect logging to <file> (default: stderr)");
|
||||
register_command(cmd_ctx, NULL, "debug_level", handle_debug_level_command,
|
||||
COMMAND_ANY, "adjust debug level <0-3>");
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
int log_init(struct command_context_s *cmd_ctx)
|
||||
{
|
||||
/* set defaults for daemon configuration, if not set by cmdline or cfgfile */
|
||||
if (debug_level == -1)
|
||||
debug_level = LOG_INFO;
|
||||
|
||||
if (log_output == NULL)
|
||||
{
|
||||
log_output = stderr;
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
96
src/helper/log.h
Normal file
96
src/helper/log.h
Normal file
@@ -0,0 +1,96 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2005 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#ifndef ERROR_H
|
||||
#define ERROR_H
|
||||
|
||||
#include "command.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
/* logging priorities
|
||||
* LOG_ERROR - fatal errors, that are likely to cause program abort
|
||||
* LOG_WARNING - non-fatal errors, that may be resolved later
|
||||
* LOG_INFO - state information, etc.
|
||||
* LOG_DEBUG - debug statements, execution trace
|
||||
*/
|
||||
enum log_levels
|
||||
{
|
||||
LOG_ERROR = 0,
|
||||
LOG_WARNING = 1,
|
||||
LOG_INFO = 2,
|
||||
LOG_DEBUG = 3
|
||||
};
|
||||
|
||||
extern void log_printf(enum log_levels level, const char *file, int line,
|
||||
const char *function, const char *format, ...);
|
||||
extern int log_register_commands(struct command_context_s *cmd_ctx);
|
||||
extern int log_init(struct command_context_s *cmd_ctx);
|
||||
|
||||
extern int debug_level;
|
||||
|
||||
#define DEBUG(expr ...) \
|
||||
do { \
|
||||
log_printf (LOG_DEBUG, __FILE__, __LINE__, __FUNCTION__, expr); \
|
||||
} while(0)
|
||||
|
||||
#define INFO(expr ...) \
|
||||
do { \
|
||||
log_printf (LOG_INFO, __FILE__, __LINE__, __FUNCTION__, expr); \
|
||||
} while(0)
|
||||
|
||||
#define WARNING(expr ...) \
|
||||
do { \
|
||||
log_printf (LOG_WARNING, __FILE__, __LINE__, __FUNCTION__, expr); \
|
||||
} while(0)
|
||||
|
||||
#define ERROR(expr ...) \
|
||||
do { \
|
||||
log_printf (LOG_ERROR, __FILE__, __LINE__, __FUNCTION__, expr); \
|
||||
} while(0)
|
||||
|
||||
#define SDEBUG(expr ...) \
|
||||
do { \
|
||||
short_log_printf (LOG_DEBUG, expr); \
|
||||
} while(0)
|
||||
|
||||
#define SINFO(expr ...) \
|
||||
do { \
|
||||
short_log_printf (LOG_INFO, expr); \
|
||||
} while(0)
|
||||
|
||||
#define SWARNING(expr ...) \
|
||||
do { \
|
||||
short_log_printf (LOG_WARNING, expr); \
|
||||
} while(0)
|
||||
|
||||
#define SERROR(expr ...) \
|
||||
do { \
|
||||
short_log_printf (LOG_ERROR, expr); \
|
||||
} while(0)
|
||||
|
||||
/* general failures
|
||||
* error codes < 100
|
||||
*/
|
||||
#define ERROR_OK (0)
|
||||
#define ERROR_INVALID_ARGUMENTS (-1)
|
||||
#define ERROR_NO_CONFIG_FILE (-2)
|
||||
#define ERROR_BUF_TOO_SMALL (-3)
|
||||
|
||||
#endif /* ERROR_H */
|
||||
82
src/helper/time_support.c
Normal file
82
src/helper/time_support.c
Normal file
@@ -0,0 +1,82 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2006 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#include "config.h"
|
||||
|
||||
#include "time_support.h"
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y);
|
||||
int timeval_add(struct timeval *result, struct timeval *x, struct timeval *y);
|
||||
int timeval_add_time(struct timeval *result, int sec, int usec);
|
||||
|
||||
/* calculate difference between two struct timeval values */
|
||||
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
|
||||
{
|
||||
if (x->tv_usec < y->tv_usec)
|
||||
{
|
||||
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
|
||||
y->tv_usec -= 1000000 * nsec;
|
||||
y->tv_sec += nsec;
|
||||
}
|
||||
if (x->tv_usec - y->tv_usec > 1000000) {
|
||||
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
|
||||
y->tv_usec += 1000000 * nsec;
|
||||
y->tv_sec -= nsec;
|
||||
}
|
||||
|
||||
result->tv_sec = x->tv_sec - y->tv_sec;
|
||||
result->tv_usec = x->tv_usec - y->tv_usec;
|
||||
|
||||
/* Return 1 if result is negative. */
|
||||
return x->tv_sec < y->tv_sec;
|
||||
}
|
||||
|
||||
/* add two struct timeval values */
|
||||
int timeval_add(struct timeval *result, struct timeval *x, struct timeval *y)
|
||||
{
|
||||
result->tv_sec = x->tv_sec + y->tv_sec;
|
||||
|
||||
result->tv_usec = x->tv_usec + y->tv_usec;
|
||||
|
||||
while (result->tv_usec > 1000000)
|
||||
{
|
||||
result->tv_usec -= 1000000;
|
||||
result->tv_sec++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int timeval_add_time(struct timeval *result, int sec, int usec)
|
||||
{
|
||||
result->tv_sec += sec;
|
||||
result->tv_usec += usec;
|
||||
|
||||
while (result->tv_usec > 1000000)
|
||||
{
|
||||
result->tv_usec -= 1000000;
|
||||
result->tv_sec++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
30
src/helper/time_support.h
Normal file
30
src/helper/time_support.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2006 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
#ifndef TIME_SUPPORT_H
|
||||
#define TIME_SUPPORT_H
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
extern int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y);
|
||||
extern int timeval_add(struct timeval *result, struct timeval *x, struct timeval *y);
|
||||
extern int timeval_add_time(struct timeval *result, int sec, int usec);
|
||||
|
||||
#endif /* TIME_SUPPORT_H */
|
||||
36
src/helper/types.h
Normal file
36
src/helper/types.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2004, 2005 by Dominic Rath *
|
||||
* Dominic.Rath@gmx.de *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef TYPES_H
|
||||
#define TYPES_H
|
||||
|
||||
#ifndef u8
|
||||
typedef unsigned char u8;
|
||||
#endif
|
||||
|
||||
#ifndef u16
|
||||
typedef unsigned short u16;
|
||||
#endif
|
||||
|
||||
#ifndef u32
|
||||
typedef unsigned int u32;
|
||||
#endif
|
||||
|
||||
#endif /* TYPES_H */
|
||||
Reference in New Issue
Block a user