build: cleanup src/helper directory

Change-Id: I71a312df783995e9083c345c25e73902d5aef59e
Signed-off-by: Spencer Oliver <spen@spen-soft.co.uk>
Reviewed-on: http://openocd.zylin.com/415
Tested-by: jenkins
__archive__
Spencer Oliver 2012-01-30 14:31:21 +00:00
parent 9ad57e96b3
commit 8b00e56e64
25 changed files with 969 additions and 1185 deletions

View File

@ -20,6 +20,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@ -27,39 +28,36 @@
#include "log.h"
#include "binarybuffer.h"
static 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
static 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
};
void* buf_cpy(const void *from, void *_to, unsigned size)
void *buf_cpy(const void *from, void *_to, unsigned size)
{
if (NULL == from || NULL == _to)
return NULL;
// copy entire buffer
/* copy entire buffer */
memcpy(_to, from, DIV_ROUND_UP(size, 8));
/* mask out bits that don't belong to the buffer */
unsigned trailing_bits = size % 8;
if (trailing_bits)
{
if (trailing_bits) {
uint8_t *to = _to;
to[size / 8] &= (1 << trailing_bits) - 1;
}
@ -94,15 +92,14 @@ bool buf_cmp(const void *_buf1, const void *_buf2, unsigned size)
}
bool buf_cmp_mask(const void *_buf1, const void *_buf2,
const void *_mask, unsigned size)
const void *_mask, unsigned size)
{
if (!_buf1 || !_buf2)
return _buf1 != _buf2 || _buf1 != _mask;
const uint8_t *buf1 = _buf1, *buf2 = _buf2, *mask = _mask;
unsigned last = size / 8;
for (unsigned i = 0; i < last; i++)
{
for (unsigned i = 0; i < last; i++) {
if (buf_cmp_masked(buf1[i], buf2[i], mask[i]))
return true;
}
@ -113,7 +110,7 @@ bool buf_cmp_mask(const void *_buf1, const void *_buf2,
}
void* buf_set_ones(void *_buf, unsigned size)
void *buf_set_ones(void *_buf, unsigned size)
{
uint8_t *buf = _buf;
if (!buf)
@ -128,12 +125,12 @@ void* buf_set_ones(void *_buf, unsigned size)
return buf;
}
void* buf_set_buf(const void *_src, unsigned src_start,
void *_dst, unsigned dst_start, unsigned len)
void *buf_set_buf(const void *_src, unsigned src_start,
void *_dst, unsigned dst_start, unsigned len)
{
const uint8_t *src = _src;
uint8_t *dst = _dst;
unsigned i,sb,db,sq,dq, lb,lq;
unsigned i, sb, db, sq, dq, lb, lq;
sb = src_start / 8;
db = dst_start / 8;
@ -148,33 +145,29 @@ void* buf_set_buf(const void *_src, unsigned src_start,
/* check if both buffers are on byte boundary and
* len is a multiple of 8bit so we can simple copy
* the buffer */
if ( (sq == 0) && (dq == 0) && (lq == 0) )
{
if ((sq == 0) && (dq == 0) && (lq == 0)) {
for (i = 0; i < lb; i++)
*dst++ = *src++;
return (uint8_t*)_dst;
return (uint8_t *)_dst;
}
/* fallback to slow bit copy */
for (i = 0; i < len; i++)
{
for (i = 0; i < len; i++) {
if (((*src >> (sq&7)) & 1) == 1)
*dst |= 1 << (dq&7);
else
*dst &= ~(1 << (dq&7));
if ( sq++ == 7 )
{
if (sq++ == 7) {
sq = 0;
src++;
}
if ( dq++ == 7 )
{
if (dq++ == 7) {
dq = 0;
dst++;
}
}
return (uint8_t*)_dst;
return (uint8_t *)_dst;
}
uint32_t flip_u32(uint32_t value, unsigned int num)
@ -197,27 +190,27 @@ static int ceil_f_to_u32(float x)
uint32_t y = x; /* cut off fraction */
if ((x - y) > 0.0) /* if there was a fractional part, increase by one */
if ((x - y) > 0.0) /* if there was a fractional part, increase by one */
y++;
return y;
}
char* buf_to_str(const void *_buf, unsigned buf_len, unsigned radix)
char *buf_to_str(const void *_buf, unsigned buf_len, unsigned radix)
{
float factor;
switch (radix) {
case 16:
factor = 2.0; /* log(256) / log(16) = 2.0 */
break;
case 10:
factor = 2.40824; /* log(256) / log(10) = 2.40824 */
break;
case 8:
factor = 2.66667; /* log(256) / log(8) = 2.66667 */
break;
default:
return NULL;
case 16:
factor = 2.0; /* log(256) / log(16) = 2.0 */
break;
case 10:
factor = 2.40824; /* log(256) / log(10) = 2.40824 */
break;
case 8:
factor = 2.66667; /* log(256) / log(8) = 2.66667 */
break;
default:
return NULL;
}
unsigned str_len = ceil_f_to_u32(DIV_ROUND_UP(buf_len, 8) * factor);
@ -225,15 +218,13 @@ char* buf_to_str(const void *_buf, unsigned buf_len, unsigned radix)
const uint8_t *buf = _buf;
int b256_len = DIV_ROUND_UP(buf_len, 8);
for (int i = b256_len - 1; i >= 0; i--)
{
for (int i = b256_len - 1; i >= 0; i--) {
uint32_t tmp = buf[i];
if (((unsigned)i == (buf_len / 8)) && (buf_len % 8))
tmp &= (0xff >> (8 - (buf_len % 8)));
/* base-256 digits */
for (unsigned j = str_len; j > 0; j--)
{
for (unsigned j = str_len; j > 0; j--) {
tmp += (uint32_t)str[j-1] * 256;
str[j-1] = (uint8_t)(tmp % radix);
tmp /= radix;
@ -247,48 +238,42 @@ char* buf_to_str(const void *_buf, unsigned buf_len, unsigned radix)
return str;
}
/// identify radix, and skip radix-prefix (0, 0x or 0X)
/* / identify radix, and skip radix-prefix (0, 0x or 0X) */
static void str_radix_guess(const char **_str, unsigned *_str_len,
unsigned *_radix)
unsigned *_radix)
{
unsigned radix = *_radix;
if (0 != radix)
return;
const char *str = *_str;
unsigned str_len = *_str_len;
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
{
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
radix = 16;
str += 2;
str_len -= 2;
}
else if ((str[0] == '0') && (str_len != 1))
{
} else if ((str[0] == '0') && (str_len != 1)) {
radix = 8;
str += 1;
str_len -= 1;
}
else
{
} else
radix = 10;
}
*_str = str;
*_str_len = str_len;
*_radix = radix;
}
int str_to_buf(const char *str, unsigned str_len,
void *_buf, unsigned buf_len, unsigned radix)
void *_buf, unsigned buf_len, unsigned radix)
{
str_radix_guess(&str, &str_len, &radix);
float factor;
if (radix == 16)
factor = 0.5; /* log(16) / log(256) = 0.5 */
factor = 0.5; /* log(16) / log(256) = 0.5 */
else if (radix == 10)
factor = 0.41524; /* log(10) / log(256) = 0.41524 */
factor = 0.41524; /* log(10) / log(256) = 0.41524 */
else if (radix == 8)
factor = 0.375; /* log(8) / log(256) = 0.375 */
factor = 0.375; /* log(8) / log(256) = 0.375 */
else
return 0;
@ -299,11 +284,10 @@ int str_to_buf(const char *str, unsigned str_len,
unsigned b256_len = ceil_f_to_u32(str_len * factor);
uint8_t *b256_buf = calloc(b256_len, 1);
/* go through zero terminated buffer */
/* input digits (ASCII) */
/* go through zero terminated buffer
* input digits (ASCII) */
unsigned i;
for (i = 0; charbuf[i]; i++)
{
for (i = 0; charbuf[i]; i++) {
uint32_t tmp = charbuf[i];
if ((tmp >= '0') && (tmp <= '9'))
tmp = (tmp - '0');
@ -311,14 +295,14 @@ int str_to_buf(const char *str, unsigned str_len,
tmp = (tmp - 'a' + 10);
else if ((tmp >= 'A') && (tmp <= 'F'))
tmp = (tmp - 'A' + 10);
else continue; /* skip characters other than [0-9,a-f,A-F] */
else
continue; /* skip characters other than [0-9,a-f,A-F] */
if (tmp >= radix)
continue; /* skip digits invalid for the current radix */
/* base-256 digits */
for (unsigned j = 0; j < b256_len; j++)
{
for (unsigned j = 0; j < b256_len; j++) {
tmp += (uint32_t)b256_buf[j] * radix;
b256_buf[j] = (uint8_t)(tmp & 0xFF);
tmp >>= 8;
@ -327,8 +311,7 @@ int str_to_buf(const char *str, unsigned str_len,
}
uint8_t *buf = _buf;
for (unsigned j = 0; j < DIV_ROUND_UP(buf_len, 8); j++)
{
for (unsigned j = 0; j < DIV_ROUND_UP(buf_len, 8); j++) {
if (j < b256_len)
buf[j] = b256_buf[j];
else

View File

@ -20,6 +20,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef BINARYBUFFER_H
#define BINARYBUFFER_H
@ -39,7 +40,7 @@
* @param value Up to 32 bits that will be copied to _buffer.
*/
static inline void buf_set_u32(void *_buffer,
unsigned first, unsigned num, uint32_t value)
unsigned first, unsigned num, uint32_t value)
{
uint8_t *buffer = (uint8_t *)_buffer;
@ -49,8 +50,7 @@ static inline void buf_set_u32(void *_buffer,
buffer[1] = (value >> 8) & 0xff;
buffer[0] = (value >> 0) & 0xff;
} else {
for (unsigned i = first; i < first + num; i++)
{
for (unsigned i = first; i < first + num; i++) {
if (((value >> (i - first)) & 1) == 1)
buffer[i / 8] |= 1 << (i % 8);
else
@ -68,19 +68,18 @@ static inline void buf_set_u32(void *_buffer,
* @returns Up to 32-bits that were read from @c _buffer.
*/
static inline uint32_t buf_get_u32(const void *_buffer,
unsigned first, unsigned num)
unsigned first, unsigned num)
{
uint8_t *buffer = (uint8_t *)_buffer;
if ((num == 32) && (first == 0)) {
return (((uint32_t)buffer[3]) << 24) |
(((uint32_t)buffer[2]) << 16) |
(((uint32_t)buffer[1]) << 8) |
(((uint32_t)buffer[0]) << 0);
(((uint32_t)buffer[2]) << 16) |
(((uint32_t)buffer[1]) << 8) |
(((uint32_t)buffer[0]) << 0);
} else {
uint32_t result = 0;
for (unsigned i = first; i < first + num; i++)
{
for (unsigned i = first; i < first + num; i++) {
if (((buffer[i / 8] >> (i % 8)) & 1) == 1)
result |= 1 << (i - first);
}
@ -109,7 +108,7 @@ bool buf_cmp_mask(const void *buf1, const void *buf2,
* @param to The buffer that will receive the copy of @c from.
* @param size The number of bits to copy.
*/
void* buf_cpy(const void *from, void *to, unsigned size);
void *buf_cpy(const void *from, void *to, unsigned size);
/**
* Set the contents of @c buf with @c count bits, all set to 1.
@ -117,14 +116,14 @@ void* buf_cpy(const void *from, void *to, unsigned size);
* @param size The number of bits.
* @returns The original buffer (@c buf).
*/
void* buf_set_ones(void *buf, unsigned size);
void *buf_set_ones(void *buf, unsigned size);
void* buf_set_buf(const void *src, unsigned src_start,
void *dst, unsigned dst_start, unsigned len);
void *buf_set_buf(const void *src, unsigned src_start,
void *dst, unsigned dst_start, unsigned len);
int str_to_buf(const char *str, unsigned len,
void *bin_buf, unsigned buf_size, unsigned radix);
char* buf_to_str(const void *buf, unsigned size, unsigned radix);
char *buf_to_str(const void *buf, unsigned size, unsigned radix);
/* read a uint32_t from a buffer in target memory endianness */
static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le)
@ -132,4 +131,4 @@ static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le)
return le ? le_to_h_u32(p) : be_to_h_u32(p);
}
#endif /* BINARYBUFFER_H */
#endif /* BINARYBUFFER_H */

File diff suppressed because it is too large Load Diff

View File

@ -20,6 +20,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef COMMAND_H
#define COMMAND_H
@ -43,8 +44,7 @@
#define PRINTF_ATTRIBUTE_FORMAT printf
#endif
enum command_mode
{
enum command_mode {
COMMAND_EXEC,
COMMAND_CONFIG,
COMMAND_ANY,
@ -52,12 +52,11 @@ enum command_mode
struct command_context;
/// The type signature for command context's output handler.
/* / The type signature for command context's output handler. */
typedef int (*command_output_handler_t)(struct command_context *context,
const char* line);
const char *line);
struct command_context
{
struct command_context {
Jim_Interp *interp;
enum command_mode mode;
struct command *commands;
@ -86,8 +85,8 @@ struct command_invocation {
* set provided by command.c. This macro uses C99 magic to allow
* defining all such derivative types using this macro.
*/
#define __COMMAND_HANDLER(name, extra...) \
int name(struct command_invocation *cmd, ##extra)
#define __COMMAND_HANDLER(name, extra ...) \
int name(struct command_invocation *cmd, ## extra)
/**
* Use this to macro to call a command helper (or a nested handler).
@ -102,8 +101,8 @@ struct command_invocation {
* helper function, or care must be taken to avoid redefining the same
* variables in intervening scope(s) by accident.
*/
#define CALL_COMMAND_HANDLER(name, extra...) \
name(cmd, ##extra)
#define CALL_COMMAND_HANDLER(name, extra ...) \
name(cmd, ## extra)
/**
* Always use this macro to define new command handler functions.
@ -111,46 +110,46 @@ struct command_invocation {
* they be can be used by other macros (e.g. COMMAND_PARSE_NUMBER).
* All command handler functions must be defined as static in scope.
*/
#define COMMAND_HANDLER(name) static __COMMAND_HANDLER(name)
#define COMMAND_HANDLER(name) \
static __COMMAND_HANDLER(name)
/**
* Similar to COMMAND_HANDLER, except some parameters are expected.
* A helper is globally-scoped because it may be shared between several
* source files (e.g. the s3c24xx device command helper).
*/
#define COMMAND_HELPER(name, extra...) __COMMAND_HANDLER(name, extra)
#define COMMAND_HELPER(name, extra ...) __COMMAND_HANDLER(name, extra)
/**
* Use this macro to access the context of the command being handled,
* rather than accessing the variable directly. It may be moved.
*/
#define CMD_CTX cmd->ctx
#define CMD_CTX (cmd->ctx)
/**
* Use this macro to access the number of arguments for the command being
* handled, rather than accessing the variable directly. It may be moved.
*/
#define CMD_ARGC cmd->argc
#define CMD_ARGC (cmd->argc)
/**
* Use this macro to access the arguments for the command being handled,
* rather than accessing the variable directly. It may be moved.
*/
#define CMD_ARGV cmd->argv
#define CMD_ARGV (cmd->argv)
/**
* Use this macro to access the name of the command being handled,
* rather than accessing the variable directly. It may be moved.
*/
#define CMD_NAME cmd->name
#define CMD_NAME (cmd->name)
/**
* Use this macro to access the current command being handled,
* rather than accessing the variable directly. It may be moved.
*/
#define CMD_CURRENT cmd->current
#define CMD_CURRENT (cmd->current)
/**
* Use this macro to access the invoked command handler's data pointer,
* rather than accessing the variable directly. It may be moved.
*/
#define CMD_DATA CMD_CURRENT->jim_handler_data
#define CMD_DATA (CMD_CURRENT->jim_handler_data)
/**
* The type signature for command handling functions. They are
@ -171,8 +170,7 @@ struct command_invocation {
*/
typedef __COMMAND_HANDLER((*command_handler_t));
struct command
{
struct command {
const char *name;
const char *help;
const char *usage;
@ -219,7 +217,7 @@ struct command_registration {
void *jim_handler_data;
enum command_mode mode;
const char *help;
/// a string listing the options and arguments, required or optional
/* / a string listing the options and arguments, required or optional */
const char *usage;
/**
@ -231,7 +229,7 @@ struct command_registration {
const struct command_registration *chain;
};
/// Use this as the last entry in an array of command_registration records.
/* / Use this as the last entry in an array of command_registration records. */
#define COMMAND_REGISTRATION_DONE { .name = NULL, .chain = NULL }
/**
@ -249,8 +247,8 @@ struct command_registration {
* command parameters.
* @returns The new command, if successful; otherwise, NULL.
*/
struct command* register_command(struct command_context *cmd_ctx,
struct command *parent, const struct command_registration *rec);
struct command *register_command(struct command_context *cmd_ctx,
struct command *parent, const struct command_registration *rec);
/**
* Register one or more commands in the specified context, as children
@ -304,7 +302,7 @@ struct command *command_find_in_parent(struct command *parent,
*/
void command_set_handler_data(struct command *c, void *p);
void command_set_output_handler(struct command_context* context,
void command_set_output_handler(struct command_context *context,
command_output_handler_t output_handler, void *priv);
@ -319,7 +317,7 @@ struct command_context *current_command_context(Jim_Interp *interp);
* the existing Jim interpreter, if any. If interp == NULL, then command_init
* creates a command interpreter.
*/
struct command_context* command_init(const char *startup_tcl, Jim_Interp *interp);
struct command_context *command_init(const char *startup_tcl, Jim_Interp *interp);
/**
* Creates a copy of an existing command context. This does not create
* a deep copy of the command list, so modifications in one context will
@ -328,7 +326,7 @@ struct command_context* command_init(const char *startup_tcl, Jim_Interp *interp
* @param cmd_ctx The command_context that will be copied.
* @returns A new command_context with the same state as the original.
*/
struct command_context* copy_command_context(struct command_context* cmd_ctx);
struct command_context *copy_command_context(struct command_context *cmd_ctx);
/**
* Frees the resources associated with a command context. The commands
* are not removed, so unregister_all_commands() must be called first.
@ -337,22 +335,22 @@ struct command_context* copy_command_context(struct command_context* cmd_ctx);
void command_done(struct command_context *context);
void command_print(struct command_context *context, const char *format, ...)
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
void command_print_sameline(struct command_context *context, const char *format, ...)
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
int command_run_line(struct command_context *context, char *line);
int command_run_linef(struct command_context *context, const char *format, ...)
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
void command_output_text(struct command_context *context, const char *data);
void process_jim_events(struct command_context *cmd_ctx);
#define ERROR_COMMAND_CLOSE_CONNECTION (-600)
#define ERROR_COMMAND_SYNTAX_ERROR (-601)
#define ERROR_COMMAND_NOTFOUND (-602)
#define ERROR_COMMAND_ARGUMENT_INVALID (-603)
#define ERROR_COMMAND_ARGUMENT_OVERFLOW (-604)
#define ERROR_COMMAND_ARGUMENT_UNDERFLOW (-605)
#define ERROR_COMMAND_CLOSE_CONNECTION (-600)
#define ERROR_COMMAND_SYNTAX_ERROR (-601)
#define ERROR_COMMAND_NOTFOUND (-602)
#define ERROR_COMMAND_ARGUMENT_INVALID (-603)
#define ERROR_COMMAND_ARGUMENT_OVERFLOW (-604)
#define ERROR_COMMAND_ARGUMENT_UNDERFLOW (-605)
int parse_ulong(const char *str, unsigned long *ul);
int parse_ullong(const char *str, unsigned long long *ul);
@ -361,7 +359,7 @@ int parse_long(const char *str, long *ul);
int parse_llong(const char *str, long long *ul);
#define DECLARE_PARSE_WRAPPER(name, type) \
int parse##name(const char *str, type *ul)
int parse ## name(const char *str, type * ul)
DECLARE_PARSE_WRAPPER(_uint, unsigned);
DECLARE_PARSE_WRAPPER(_u32, uint32_t);
@ -386,7 +384,7 @@ DECLARE_PARSE_WRAPPER(_s8, int8_t);
*/
#define COMMAND_PARSE_NUMBER(type, in, out) \
do { \
int retval_macro_tmp = parse_##type(in, &(out)); \
int retval_macro_tmp = parse_ ## type(in, &(out)); \
if (ERROR_OK != retval_macro_tmp) { \
command_print(CMD_CTX, stringify(out) \
" option value ('%s') is not valid", in); \
@ -417,14 +415,14 @@ DECLARE_PARSE_WRAPPER(_s8, int8_t);
int command_parse_bool_arg(const char *in, bool *out);
COMMAND_HELPER(handle_command_parse_bool, bool *out, const char *label);
/// parses an on/off command argument
/* / parses an on/off command argument */
#define COMMAND_PARSE_ON_OFF(in, out) \
COMMAND_PARSE_BOOL(in, out, "on", "off")
/// parses an enable/disable command argument
COMMAND_PARSE_BOOL(in, out, "on", "off")
/* / parses an enable/disable command argument */
#define COMMAND_PARSE_ENABLE(in, out) \
COMMAND_PARSE_BOOL(in, out, "enable", "disable")
COMMAND_PARSE_BOOL(in, out, "enable", "disable")
void script_debug(Jim_Interp *interp, const char *cmd,
unsigned argc, Jim_Obj *const *argv);
unsigned argc, Jim_Obj * const *argv);
#endif /* COMMAND_H */
#endif /* COMMAND_H */

View File

@ -27,17 +27,17 @@
#include "configuration.h"
#include "log.h"
static size_t num_config_files;
static char** config_file_names;
static char **config_file_names;
static size_t num_script_dirs;
static char** script_search_dirs;
static char **script_search_dirs;
void add_script_search_dir (const char *dir)
void add_script_search_dir(const char *dir)
{
num_script_dirs++;
script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs + 1) * sizeof (char *));
script_search_dirs =
(char **)realloc(script_search_dirs, (num_script_dirs + 1) * sizeof(char *));
script_search_dirs[num_script_dirs-1] = strdup(dir);
script_search_dirs[num_script_dirs] = NULL;
@ -45,10 +45,11 @@ void add_script_search_dir (const char *dir)
LOG_DEBUG("adding %s", dir);
}
void add_config_command (const char *cfg)
void add_config_command(const char *cfg)
{
num_config_files++;
config_file_names = (char **)realloc(config_file_names, (num_config_files + 1) * sizeof (char *));
config_file_names =
(char **)realloc(config_file_names, (num_config_files + 1) * sizeof(char *));
config_file_names[num_config_files-1] = strdup(cfg);
config_file_names[num_config_files] = NULL;
@ -60,7 +61,7 @@ char *find_file(const char *file)
FILE *fp = NULL;
char **search_dirs = script_search_dirs;
char *dir;
char const *mode="r";
char const *mode = "r";
char *full_path;
/* Check absolute and relative to current working dir first.
@ -68,8 +69,7 @@ char *find_file(const char *file)
full_path = alloc_printf("%s", file);
fp = fopen(full_path, mode);
while (!fp)
{
while (!fp) {
free(full_path);
full_path = NULL;
dir = *search_dirs++;
@ -81,8 +81,7 @@ char *find_file(const char *file)
fp = fopen(full_path, mode);
}
if (fp)
{
if (fp) {
fclose(fp);
LOG_DEBUG("found %s", full_path);
return full_path;
@ -95,11 +94,9 @@ char *find_file(const char *file)
FILE *open_file_from_path(const char *file, const char *mode)
{
if (mode[0]!='r')
{
if (mode[0] != 'r')
return fopen(file, mode);
} else
{
else {
char *full_path = find_file(file);
if (full_path == NULL)
return NULL;
@ -122,8 +119,7 @@ int parse_config_file(struct command_context *cmd_ctx)
cfg = config_file_names;
while (*cfg)
{
while (*cfg) {
retval = command_run_line(cmd_ctx, *cfg);
if (retval != ERROR_OK)
return retval;

View File

@ -20,6 +20,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
@ -40,4 +41,4 @@ FILE *open_file_from_path(const char *file, const char *mode);
char *find_file(const char *name);
#endif /* CONFIGURATION_H */
#endif /* CONFIGURATION_H */

View File

@ -23,6 +23,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@ -44,8 +45,7 @@ static inline int fileio_open_local(struct fileio_internal *fileio)
{
char file_access[4];
switch (fileio->access)
{
switch (fileio->access) {
case FILEIO_READ:
strcpy(file_access, "r");
break;
@ -70,18 +70,15 @@ static inline int fileio_open_local(struct fileio_internal *fileio)
#ifndef _WIN32
if (fileio->type == FILEIO_BINARY)
#endif
{
strcat(file_access, "b");
}
if (!(fileio->file = open_file_from_path (fileio->url, file_access)))
{
fileio->file = open_file_from_path(fileio->url, file_access);
if (!fileio->file) {
LOG_ERROR("couldn't open %s", fileio->url);
return ERROR_FILEIO_OPERATION_FAILED;
}
if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
{
if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE)) {
/* NB! Here we use fseek() instead of stat(), since stat is a
* more advanced operation that might not apply to e.g. a disk path
* that refers to e.g. a tftp client */
@ -93,21 +90,20 @@ static inline int fileio_open_local(struct fileio_internal *fileio)
result2 = fseek(fileio->file, 0, SEEK_SET);
if ((fileio->size < 0)||(result < 0)||(result2 < 0))
{
if ((fileio->size < 0) || (result < 0) || (result2 < 0)) {
fileio_close_local(fileio);
return ERROR_FILEIO_OPERATION_FAILED;
}
}
else
{
} else
fileio->size = 0x0;
}
return ERROR_OK;
}
int fileio_open(struct fileio *fileio_p, const char *url, enum fileio_access access_type, enum fileio_type type)
int fileio_open(struct fileio *fileio_p,
const char *url,
enum fileio_access access_type,
enum fileio_type type)
{
int retval = ERROR_OK;
@ -125,17 +121,12 @@ int fileio_open(struct fileio *fileio_p, const char *url, enum fileio_access acc
static inline int fileio_close_local(struct fileio_internal *fileio)
{
int retval;
if ((retval = fclose(fileio->file)) != 0)
{
int retval = fclose(fileio->file);
if (retval != 0) {
if (retval == EBADF)
{
LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
}
else
{
LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
}
return ERROR_FILEIO_OPERATION_FAILED;
}
@ -150,7 +141,7 @@ int fileio_close(struct fileio *fileio_p)
retval = fileio_close_local(fileio);
free((void*)fileio->url);
free((void *)fileio->url);
fileio->url = NULL;
free(fileio);
@ -163,8 +154,8 @@ int fileio_seek(struct fileio *fileio_p, size_t position)
{
int retval;
struct fileio_internal *fileio = fileio_p->fp;
if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
{
retval = fseek(fileio->file, position, SEEK_SET);
if (retval != 0) {
LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
return ERROR_FILEIO_OPERATION_FAILED;
}
@ -173,7 +164,7 @@ int fileio_seek(struct fileio *fileio_p, size_t position)
}
static int fileio_local_read(struct fileio_internal *fileio,
size_t size, void *buffer, size_t *size_read)
size_t size, void *buffer, size_t *size_read)
{
ssize_t retval = fread(buffer, 1, size, fileio->file);
*size_read = (retval >= 0) ? retval : 0;
@ -181,7 +172,7 @@ static int fileio_local_read(struct fileio_internal *fileio,
}
int fileio_read(struct fileio *fileio_p, size_t size, void *buffer,
size_t *size_read)
size_t *size_read)
{
struct fileio_internal *fileio = fileio_p->fp;
return fileio_local_read(fileio, size, buffer, size_read);
@ -201,7 +192,7 @@ int fileio_read_u32(struct fileio *fileio_p, uint32_t *data)
}
static int fileio_local_fgets(struct fileio_internal *fileio,
size_t size, void *buffer)
size_t size, void *buffer)
{
if (fgets(buffer, size, fileio->file) == NULL)
return ERROR_FILEIO_OPERATION_FAILED;
@ -216,7 +207,7 @@ int fileio_fgets(struct fileio *fileio_p, size_t size, void *buffer)
}
static int fileio_local_write(struct fileio_internal *fileio,
size_t size, const void *buffer, size_t *size_written)
size_t size, const void *buffer, size_t *size_written)
{
ssize_t retval = fwrite(buffer, 1, size, fileio->file);
*size_written = (retval >= 0) ? retval : 0;
@ -224,7 +215,7 @@ static int fileio_local_write(struct fileio_internal *fileio,
}
int fileio_write(struct fileio *fileio_p,
size_t size, const void *buffer, size_t *size_written)
size_t size, const void *buffer, size_t *size_written)
{
struct fileio_internal *fileio = fileio_p->fp;
int retval = fileio_local_write(fileio, size, buffer, size_written);

View File

@ -23,6 +23,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef FILEIO_H
#define FILEIO_H
@ -30,14 +31,12 @@
#define FILEIO_MAX_ERROR_STRING (128)
enum fileio_type
{
enum fileio_type {
FILEIO_TEXT,
FILEIO_BINARY,
};
enum fileio_access
{
enum fileio_access {
FILEIO_NONE, /* open without any access (invalid mode) */
FILEIO_READ, /* open for reading, position at beginning */
FILEIO_WRITE, /* open for writing, position at beginning */
@ -46,14 +45,13 @@ enum fileio_access
FILEIO_APPENDREAD, /* open for writing, position at end, allow reading */
};
struct fileio
{
struct fileio {
/* The structure is opaque */
struct fileio_internal *fp;
};
int fileio_open(struct fileio *fileio,
const char *url, enum fileio_access access_type, enum fileio_type type);
const char *url, enum fileio_access access_type, enum fileio_type type);
int fileio_close(struct fileio *fileio);
int fileio_seek(struct fileio *fileio, size_t position);
@ -68,11 +66,11 @@ int fileio_read_u32(struct fileio *fileio, uint32_t *data);
int fileio_write_u32(struct fileio *fileio, uint32_t data);
int fileio_size(struct fileio *fileio, int *size);
#define ERROR_FILEIO_LOCATION_UNKNOWN (-1200)
#define ERROR_FILEIO_NOT_FOUND (-1201)
#define ERROR_FILEIO_OPERATION_FAILED (-1202)
#define ERROR_FILEIO_ACCESS_NOT_SUPPORTED (-1203)
#define ERROR_FILEIO_RESOURCE_TYPE_UNKNOWN (-1204)
#define ERROR_FILEIO_LOCATION_UNKNOWN (-1200)
#define ERROR_FILEIO_NOT_FOUND (-1201)
#define ERROR_FILEIO_OPERATION_FAILED (-1202)
#define ERROR_FILEIO_ACCESS_NOT_SUPPORTED (-1203)
#define ERROR_FILEIO_RESOURCE_TYPE_UNKNOWN (-1204)
#define ERROR_FILEIO_OPERATION_NOT_SUPPORTED (-1205)
#endif /* FILEIO_H */
#endif /* FILEIO_H */

View File

@ -53,52 +53,45 @@
#endif
#endif
/* loads a file and returns a pointer to it in memory. The file contains
* a 0 byte(sentinel) after len bytes - the length of the file. */
int loadFile(const char *fileName, void **data, size_t *len)
{
// ensure returned length is always sane
/* ensure returned length is always sane */
*len = 0;
FILE * pFile;
pFile = fopen(fileName,"rb");
if (pFile == NULL)
{
FILE *pFile;
pFile = fopen(fileName, "rb");
if (pFile == NULL) {
LOG_ERROR("Can't open %s", fileName);
return ERROR_FAIL;
}
if (fseek(pFile, 0, SEEK_END) != 0)
{
if (fseek(pFile, 0, SEEK_END) != 0) {
LOG_ERROR("Can't open %s", fileName);
fclose(pFile);
return ERROR_FAIL;
}
long fsize = ftell(pFile);
if (fsize == -1)
{
if (fsize == -1) {
LOG_ERROR("Can't open %s", fileName);
fclose(pFile);
return ERROR_FAIL;
}
*len = fsize;
if (fseek(pFile, 0, SEEK_SET) != 0)
{
if (fseek(pFile, 0, SEEK_SET) != 0) {
LOG_ERROR("Can't open %s", fileName);
fclose(pFile);
return ERROR_FAIL;
}
*data = malloc(*len + 1);
if (*data == NULL)
{
if (*data == NULL) {
LOG_ERROR("Can't open %s", fileName);
fclose(pFile);
return ERROR_FAIL;
}
if (fread(*data, 1, *len, pFile)!=*len)
{
if (fread(*data, 1, *len, pFile) != *len) {
fclose(pFile);
free(*data);
LOG_ERROR("Can't open %s", fileName);
@ -106,7 +99,7 @@ int loadFile(const char *fileName, void **data, size_t *len)
}
fclose(pFile);
// 0-byte after buffer (not included in *len) serves as a sentinel
/* 0-byte after buffer (not included in *len) serves as a sentinel */
char *buf = (char *)*data;
buf[*len] = 0;
@ -116,24 +109,19 @@ int loadFile(const char *fileName, void **data, size_t *len)
COMMAND_HANDLER(handle_cat_command)
{
if (CMD_ARGC != 1)
{
return ERROR_COMMAND_SYNTAX_ERROR;
}
// NOTE!!! we only have line printing capability so we print the entire file as a single line.
/* NOTE!!! we only have line printing capability so we print the entire file as a single
* line. */
void *data;
size_t len;
int retval = loadFile(CMD_ARGV[0], &data, &len);
if (retval == ERROR_OK)
{
if (retval == ERROR_OK) {
command_print(CMD_CTX, "%s", (char *)data);
free(data);
}
else
{
} else
command_print(CMD_CTX, "%s not found", CMD_ARGV[0]);
}
return ERROR_OK;
}
@ -141,9 +129,7 @@ COMMAND_HANDLER(handle_cat_command)
COMMAND_HANDLER(handle_trunc_command)
{
if (CMD_ARGC != 1)
{
return ERROR_COMMAND_SYNTAX_ERROR;
}
FILE *config_file = NULL;
config_file = fopen(CMD_ARGV[0], "w");
@ -155,20 +141,16 @@ COMMAND_HANDLER(handle_trunc_command)
COMMAND_HANDLER(handle_meminfo_command)
{
static int prev = 0;
static int prev;
struct mallinfo info;
if (CMD_ARGC != 0)
{
return ERROR_COMMAND_SYNTAX_ERROR;
}
info = mallinfo();
if (prev > 0)
{
command_print(CMD_CTX, "Diff: %d", prev - info.fordblks);
}
prev = info.fordblks;
command_print(CMD_CTX, "Available ram: %d", info.fordblks);
@ -180,26 +162,21 @@ COMMAND_HANDLER(handle_meminfo_command)
COMMAND_HANDLER(handle_append_command)
{
if (CMD_ARGC < 1)
{
return ERROR_COMMAND_SYNTAX_ERROR;
}
int retval = ERROR_FAIL;
FILE *config_file = NULL;
config_file = fopen(CMD_ARGV[0], "a");
if (config_file != NULL)
{
if (config_file != NULL) {
fseek(config_file, 0, SEEK_END);
unsigned i;
for (i = 1; i < CMD_ARGC; i++)
{
for (i = 1; i < CMD_ARGC; i++) {
if (fwrite(CMD_ARGV[i], 1, strlen(CMD_ARGV[i]),
config_file) != strlen(CMD_ARGV[i]))
break;
if (i != CMD_ARGC - 1)
{
if (i != CMD_ARGC - 1) {
if (fwrite(" ", 1, 1, config_file) != 1)
break;
}
@ -213,16 +190,13 @@ COMMAND_HANDLER(handle_append_command)
return retval;
}
COMMAND_HANDLER(handle_cp_command)
{
if (CMD_ARGC != 2)
{
return ERROR_COMMAND_SYNTAX_ERROR;
}
// NOTE!!! we only have line printing capability so we print the entire file as a single line.
/* NOTE!!! we only have line printing capability so we print the entire file as a single
* line. */
void *data;
size_t len;
@ -235,22 +209,17 @@ COMMAND_HANDLER(handle_cp_command)
retval = ERROR_COMMAND_SYNTAX_ERROR;
size_t pos = 0;
for (;;)
{
for (;; ) {
size_t chunk = len - pos;
static const size_t maxChunk = 512 * 1024; // ~1/sec
static const size_t maxChunk = 512 * 1024; /* ~1/sec */
if (chunk > maxChunk)
{
chunk = maxChunk;
}
if ((retval == ERROR_OK) && (fwrite(((char *)data) + pos, 1, chunk, f) != chunk))
retval = ERROR_COMMAND_SYNTAX_ERROR;
if (retval != ERROR_OK)
{
break;
}
command_print(CMD_CTX, "%zu", len - pos);
@ -261,12 +230,9 @@ COMMAND_HANDLER(handle_cp_command)
}
if (retval == ERROR_OK)
{
command_print(CMD_CTX, "Copied %s to %s", CMD_ARGV[0], CMD_ARGV[1]);
} else
{
else
command_print(CMD_CTX, "copy failed");
}
if (data != NULL)
free(data);
@ -279,10 +245,7 @@ COMMAND_HANDLER(handle_cp_command)
return retval;
}
#define SHOW_RESULT(a, b) LOG_ERROR(#a " failed %d\n", (int)b)
#define SHOW_RESULT(a, b) LOG_ERROR(# a " failed %d\n", (int)b)
#define IOSIZE 512
void copyfile(char *name2, char *name1)
@ -301,29 +264,31 @@ void copyfile(char *name2, char *name1)
if (fd2 < 0)
SHOW_RESULT(open, fd2);
for (;;)
{
for (;; ) {
done = read(fd2, buf, IOSIZE);
if (done < 0)
{
if (done < 0) {
SHOW_RESULT(read, done);
break;
}
if (done == 0) break;
if (done == 0)
break;
wrote = write(fd1, buf, done);
if (wrote != done) SHOW_RESULT(write, wrote);
if (wrote != done)
SHOW_RESULT(write, wrote);
if (wrote != done) break;
if (wrote != done)
break;
}
err = close(fd1);
if (err < 0) SHOW_RESULT(close, err);
if (err < 0)
SHOW_RESULT(close, err);
err = close(fd2);
if (err < 0) SHOW_RESULT(close, err);
if (err < 0)
SHOW_RESULT(close, err);
}
/* utility fn to copy a directory */
@ -334,18 +299,15 @@ void copydir(char *name, char *destdir)
dirp = opendir(destdir);
if (dirp == NULL)
{
mkdir(destdir, 0777);
} else
{
else
err = closedir(dirp);
}
dirp = opendir(name);
if (dirp == NULL) SHOW_RESULT(opendir, -1);
if (dirp == NULL)
SHOW_RESULT(opendir, -1);
for (;;)
{
for (;; ) {
struct dirent *entry = readdir(dirp);
if (entry == NULL)
@ -363,8 +325,7 @@ void copydir(char *name, char *destdir)
strcat(fullPath, "/");
strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
if (stat(fullPath, &buf) == -1)
{
if (stat(fullPath, &buf) == -1) {
LOG_ERROR("unable to read status from %s", fullPath);
break;
}
@ -373,7 +334,7 @@ void copydir(char *name, char *destdir)
if (isDir)
continue;
// diag_printf("<INFO>: entry %14s",entry->d_name);
/* diag_printf("<INFO>: entry %14s",entry->d_name); */
char fullname[PATH_MAX];
char fullname2[PATH_MAX];
@ -384,19 +345,17 @@ void copydir(char *name, char *destdir)
strcpy(fullname2, destdir);
strcat(fullname2, "/");
strcat(fullname2, entry->d_name);
// diag_printf("from %s to %s\n", fullname, fullname2);
/* diag_printf("from %s to %s\n", fullname, fullname2); */
copyfile(fullname, fullname2);
// diag_printf("\n");
/* diag_printf("\n"); */
}
err = closedir(dirp);
if (err < 0) SHOW_RESULT(stat, err);
if (err < 0)
SHOW_RESULT(stat, err);
}
COMMAND_HANDLER(handle_rm_command)
{
if (CMD_ARGC != 1)
@ -411,39 +370,34 @@ COMMAND_HANDLER(handle_rm_command)
return del ? ERROR_OK : ERROR_FAIL;
}
static int
ioutil_Jim_Command_ls(Jim_Interp *interp,
int argc,
Jim_Obj * const *argv)
static int ioutil_Jim_Command_ls(Jim_Interp *interp,
int argc,
Jim_Obj * const *argv)
{
if (argc != 2)
{
if (argc != 2) {
Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
return JIM_ERR;
}
char *name = (char*) Jim_GetString(argv[1], NULL);
char *name = (char *) Jim_GetString(argv[1], NULL);
DIR *dirp = NULL;
dirp = opendir(name);
if (dirp == NULL)
{
return JIM_ERR;
}
Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
for (;;)
{
for (;; ) {
struct dirent *entry = NULL;
entry = readdir(dirp);
if (entry == NULL)
break;
if ((strcmp(".", entry->d_name) == 0)||(strcmp("..", entry->d_name) == 0))
if ((strcmp(".", entry->d_name) == 0) || (strcmp("..", entry->d_name) == 0))
continue;
Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
Jim_ListAppendElement(interp, objPtr,
Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
}
closedir(dirp);
@ -452,13 +406,11 @@ ioutil_Jim_Command_ls(Jim_Interp *interp,
return JIM_OK;
}
static int
ioutil_Jim_Command_peek(Jim_Interp *interp,
int argc,
Jim_Obj * const *argv)
static int ioutil_Jim_Command_peek(Jim_Interp *interp,
int argc,
Jim_Obj *const *argv)
{
if (argc != 2)
{
if (argc != 2) {
Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
return JIM_ERR;
}
@ -474,13 +426,11 @@ ioutil_Jim_Command_peek(Jim_Interp *interp,
return JIM_OK;
}
static int
ioutil_Jim_Command_poke(Jim_Interp *interp,
int argc,
Jim_Obj * const *argv)
static int ioutil_Jim_Command_poke(Jim_Interp *interp,
int argc,
Jim_Obj *const *argv)
{
if (argc != 3)
{
if (argc != 3) {
Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
return JIM_ERR;
}
@ -497,10 +447,9 @@ ioutil_Jim_Command_poke(Jim_Interp *interp,
return JIM_OK;
}
/* not so pretty code to fish out ip number*/
static int ioutil_Jim_Command_ip(Jim_Interp *interp, int argc,
Jim_Obj * const *argv)
Jim_Obj *const *argv)
{
#if !defined(__CYGWIN__)
Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
@ -508,12 +457,9 @@ static int ioutil_Jim_Command_ip(Jim_Interp *interp, int argc,
struct ifaddrs *ifa = NULL, *ifp = NULL;
if (getifaddrs(&ifp) < 0)
{
return JIM_ERR;
}
for (ifa = ifp; ifa; ifa = ifa->ifa_next)
{
for (ifa = ifp; ifa; ifa = ifa->ifa_next) {
char ip[200];
socklen_t salen;
@ -526,9 +472,7 @@ static int ioutil_Jim_Command_ip(Jim_Interp *interp, int argc,
if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0,
NI_NUMERICHOST) < 0)
{
continue;
}
Jim_AppendString(interp, tclOutput, ip, strlen(ip));
break;
@ -547,10 +491,8 @@ static int ioutil_Jim_Command_ip(Jim_Interp *interp, int argc,
/* not so pretty code to fish out eth0 mac address */
static int ioutil_Jim_Command_mac(Jim_Interp *interp, int argc,
Jim_Obj * const *argv)
Jim_Obj *const *argv)
{
struct ifreq *ifr, *ifend;
struct ifreq ifreq;
struct ifconf ifc;
@ -559,45 +501,39 @@ static int ioutil_Jim_Command_mac(Jim_Interp *interp, int argc,
SockFD = socket(AF_INET, SOCK_DGRAM, 0);
if (SockFD < 0)
{
return JIM_ERR;
}
ifc.ifc_len = sizeof(ifs);
ifc.ifc_req = ifs;
if (ioctl(SockFD, SIOCGIFCONF, &ifc) < 0)
{
if (ioctl(SockFD, SIOCGIFCONF, &ifc) < 0) {
close(SockFD);
return JIM_ERR;
}
ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
{
//if (ifr->ifr_addr.sa_family == AF_INET)
for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
/* if (ifr->ifr_addr.sa_family == AF_INET) */
{
if (strcmp("eth0", ifr->ifr_name) != 0)
continue;
strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
if (ioctl(SockFD, SIOCGIFHWADDR, &ifreq) < 0)
{
if (ioctl(SockFD, SIOCGIFHWADDR, &ifreq) < 0) {
close(SockFD);
return JIM_ERR;
}
close(SockFD);
Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
char buffer[256];
sprintf(buffer, "%02x-%02x-%02x-%02x-%02x-%02x",
ifreq.ifr_hwaddr.sa_data[0]&0xff,
ifreq.ifr_hwaddr.sa_data[1]&0xff,
ifreq.ifr_hwaddr.sa_data[2]&0xff,
ifreq.ifr_hwaddr.sa_data[3]&0xff,
ifreq.ifr_hwaddr.sa_data[4]&0xff,
ifreq.ifr_hwaddr.sa_data[5]&0xff);
ifreq.ifr_hwaddr.sa_data[0]&0xff,
ifreq.ifr_hwaddr.sa_data[1]&0xff,
ifreq.ifr_hwaddr.sa_data[2]&0xff,
ifreq.ifr_hwaddr.sa_data[3]&0xff,
ifreq.ifr_hwaddr.sa_data[4]&0xff,
ifreq.ifr_hwaddr.sa_data[5]&0xff);
Jim_AppendString(interp, tclOutput, buffer, strlen(buffer));
@ -618,14 +554,14 @@ static const struct command_registration ioutil_command_handlers[] = {
.handler = handle_cat_command,
.mode = COMMAND_ANY,
.help = "display text file content",
.usage= "file_name",
.usage = "file_name",
},
{
.name = "trunc",
.handler = handle_trunc_command,
.mode = COMMAND_ANY,
.help = "truncate a file to zero length",
.usage= "file_name",
.usage = "file_name",
},
{
.name = "cp",
@ -660,7 +596,7 @@ static const struct command_registration ioutil_command_handlers[] = {
* server-internal addresses.
*/
// jim handlers
/* jim handlers */
{
.name = "peek",
.mode = COMMAND_ANY,

View File

@ -24,4 +24,4 @@ struct command_context;
int ioutil_init(struct command_context *cmd_ctx);
#endif // HELPER_IOUTILS_H
#endif /* HELPER_IOUTILS_H */

View File

@ -45,337 +45,298 @@
#include <jim-nvp.h>
int Jim_GetNvp(Jim_Interp *interp,
Jim_Obj *objPtr, const Jim_Nvp * nvp_table, const Jim_Nvp ** result)
Jim_Obj *objPtr, const Jim_Nvp *nvp_table, const Jim_Nvp **result)
{
Jim_Nvp *n;
int e;
Jim_Nvp *n;
int e;
e = Jim_Nvp_name2value_obj(interp, nvp_table, objPtr, &n);
if (e == JIM_ERR) {
return e;
}
e = Jim_Nvp_name2value_obj(interp, nvp_table, objPtr, &n);
if (e == JIM_ERR)
return e;
/* Success? found? */
if (n->name) {
/* remove const */
*result = (Jim_Nvp *) n;
return JIM_OK;
}
else {
return JIM_ERR;
}
/* Success? found? */
if (n->name) {
/* remove const */
*result = (Jim_Nvp *) n;
return JIM_OK;
} else
return JIM_ERR;
}
Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp * p, const char *name)
Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp *p, const char *name)
{
while (p->name) {
if (0 == strcmp(name, p->name)) {
break;
}
p++;
}
return ((Jim_Nvp *) (p));
while (p->name) {
if (0 == strcmp(name, p->name))
break;
p++;
}
return (Jim_Nvp *) (p);
}
Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp * p, const char *name)
Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp *p, const char *name)
{
while (p->name) {
if (0 == strcasecmp(name, p->name)) {
break;
}
p++;
}
return ((Jim_Nvp *) (p));
while (p->name) {
if (0 == strcasecmp(name, p->name))
break;
p++;
}
return (Jim_Nvp *) (p);
}
int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp *p, Jim_Obj *o, Jim_Nvp **result)
{
return Jim_Nvp_name2value(interp, p, Jim_String(o), result);
return Jim_Nvp_name2value(interp, p, Jim_String(o), result);
}
int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp * _p, const char *name, Jim_Nvp ** result)
int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp *_p, const char *name, Jim_Nvp **result)
{
const Jim_Nvp *p;
const Jim_Nvp *p;
p = Jim_Nvp_name2value_simple(_p, name);
p = Jim_Nvp_name2value_simple(_p, name);
/* result */
if (result) {
*result = (Jim_Nvp *) (p);
}
/* result */
if (result)
*result = (Jim_Nvp *) (p);
/* found? */
if (p->name) {
return JIM_OK;
}
else {
return JIM_ERR;
}
/* found? */
if (p->name)
return JIM_OK;
else
return JIM_ERR;
}
int
Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** puthere)
int Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp,
const Jim_Nvp *p,
Jim_Obj *o,
Jim_Nvp **puthere)
{
return Jim_Nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
return Jim_Nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
}
int
Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp * _p, const char *name,
Jim_Nvp ** puthere)
int Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp *_p, const char *name,
Jim_Nvp **puthere)
{
const Jim_Nvp *p;
const Jim_Nvp *p;
p = Jim_Nvp_name2value_nocase_simple(_p, name);
p = Jim_Nvp_name2value_nocase_simple(_p, name);
if (puthere) {
*puthere = (Jim_Nvp *) (p);
}
/* found */
if (p->name) {
return JIM_OK;
}
else {
return JIM_ERR;
}
if (puthere)
*puthere = (Jim_Nvp *) (p);
/* found */
if (p->name)
return JIM_OK;
else
return JIM_ERR;
}
int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp *p, Jim_Obj *o, Jim_Nvp **result)
{
int e;;
jim_wide w;
int e;
jim_wide w;
e = Jim_GetWide(interp, o, &w);
if (e != JIM_OK) {
return e;
}
e = Jim_GetWide(interp, o, &w);
if (e != JIM_OK)
return e;
return Jim_Nvp_value2name(interp, p, w, result);
return Jim_Nvp_value2name(interp, p, w, result);
}
Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp * p, int value)
Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp *p, int value)
{
while (p->name) {
if (value == p->value) {
break;
}
p++;
}
return ((Jim_Nvp *) (p));
while (p->name) {
if (value == p->value)
break;
p++;
}
return (Jim_Nvp *) (p);
}
int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp * _p, int value, Jim_Nvp ** result)
int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp *_p, int value, Jim_Nvp **result)
{
const Jim_Nvp *p;
const Jim_Nvp *p;
p = Jim_Nvp_value2name_simple(_p, value);
p = Jim_Nvp_value2name_simple(_p, value);
if (result) {
*result = (Jim_Nvp *) (p);
}
if (result)
*result = (Jim_Nvp *) (p);
if (p->name) {
return JIM_OK;
}
else {
return JIM_ERR;
}
if (p->name)
return JIM_OK;
else
return JIM_ERR;
}
int Jim_GetOpt_Setup(Jim_GetOptInfo * p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
int Jim_GetOpt_Setup(Jim_GetOptInfo *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
memset(p, 0, sizeof(*p));
p->interp = interp;
p->argc = argc;
p->argv = argv;
memset(p, 0, sizeof(*p));
p->interp = interp;
p->argc = argc;
p->argv = argv;
return JIM_OK;
return JIM_OK;
}
void Jim_GetOpt_Debug(Jim_GetOptInfo * p)
void Jim_GetOpt_Debug(Jim_GetOptInfo *p)
{
int x;
int x;
fprintf(stderr, "---args---\n");
for (x = 0; x < p->argc; x++) {
fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
}
fprintf(stderr, "-------\n");
fprintf(stderr, "---args---\n");
for (x = 0; x < p->argc; x++)
fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
fprintf(stderr, "-------\n");
}
int Jim_GetOpt_Obj(Jim_GetOptInfo * goi, Jim_Obj **puthere)
int Jim_GetOpt_Obj(Jim_GetOptInfo *goi, Jim_Obj **puthere)
{
Jim_Obj *o;
Jim_Obj *o;
o = NULL; // failure
if (goi->argc) {
// success
o = goi->argv[0];
goi->argc -= 1;
goi->argv += 1;
}
if (puthere) {
*puthere = o;
}
if (o != NULL) {
return JIM_OK;
}
else {
return JIM_ERR;
}
o = NULL; /* failure */
if (goi->argc) {
/* success */
o = goi->argv[0];
goi->argc -= 1;
goi->argv += 1;
}
if (puthere)
*puthere = o;
if (o != NULL)
return JIM_OK;
else
return JIM_ERR;
}
int Jim_GetOpt_String(Jim_GetOptInfo * goi, char **puthere, int *len)
int Jim_GetOpt_String(Jim_GetOptInfo *goi, char **puthere, int *len)
{
int r;
Jim_Obj *o;
const char *cp;
int r;
Jim_Obj *o;
const char *cp;
r = Jim_GetOpt_Obj(goi, &o);
if (r == JIM_OK) {
cp = Jim_GetString(o, len);
if (puthere) {
/* remove const */
*puthere = (char *)(cp);
}
}
return r;
r = Jim_GetOpt_Obj(goi, &o);
if (r == JIM_OK) {
cp = Jim_GetString(o, len);
if (puthere) {
/* remove const */
*puthere = (char *)(cp);
}
}
return r;
}
int Jim_GetOpt_Double(Jim_GetOptInfo * goi, double *puthere)
int Jim_GetOpt_Double(Jim_GetOptInfo *goi, double *puthere)
{
int r;
Jim_Obj *o;
double _safe;
int r;
Jim_Obj *o;
double _safe;
if (puthere == NULL) {
puthere = &_safe;
}
if (puthere == NULL)
puthere = &_safe;
r = Jim_GetOpt_Obj(goi, &o);
if (r == JIM_OK) {
r = Jim_GetDouble(goi->interp, o, puthere);
if (r != JIM_OK) {
Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
}
}
return r;
r = Jim_GetOpt_Obj(goi, &o);
if (r == JIM_OK) {
r = Jim_GetDouble(goi->interp, o, puthere);
if (r != JIM_OK)
Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
}
return r;
}
int Jim_GetOpt_Wide(Jim_GetOptInfo * goi, jim_wide * puthere)
int Jim_GetOpt_Wide(Jim_GetOptInfo *goi, jim_wide *puthere)
{
int r;
Jim_Obj *o;
jim_wide _safe;
int r;
Jim_Obj *o;
jim_wide _safe;
if (puthere == NULL) {
puthere = &_safe;
}
if (puthere == NULL)
puthere = &_safe;
r = Jim_GetOpt_Obj(goi, &o);
if (r == JIM_OK) {
r = Jim_GetWide(goi->interp, o, puthere);
}
return r;
r = Jim_GetOpt_Obj(goi, &o);
if (r == JIM_OK)
r = Jim_GetWide(goi->interp, o, puthere);
return r;
}
int Jim_GetOpt_Nvp(Jim_GetOptInfo * goi, const Jim_Nvp * nvp, Jim_Nvp ** puthere)
int Jim_GetOpt_Nvp(Jim_GetOptInfo *goi, const Jim_Nvp *nvp, Jim_Nvp **puthere)
{
Jim_Nvp *_safe;
Jim_Obj *o;
int e;
Jim_Nvp *_safe;
Jim_Obj *o;
int e;
if (puthere == NULL) {
puthere = &_safe;
}
if (puthere == NULL)
puthere = &_safe;
e = Jim_GetOpt_Obj(goi, &o);
if (e == JIM_OK) {
e = Jim_Nvp_name2value_obj(goi->interp, nvp, o, puthere);
}
e = Jim_GetOpt_Obj(goi, &o);
if (e == JIM_OK)
e = Jim_Nvp_name2value_obj(goi->interp, nvp, o, puthere);
return e;
return e;
}
void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo * goi, const Jim_Nvp * nvptable, int hadprefix)
void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo *goi, const Jim_Nvp *nvptable, int hadprefix)
{
if (hadprefix) {
Jim_SetResult_NvpUnknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
}
else {
Jim_SetResult_NvpUnknown(goi->interp, NULL, goi->argv[-1], nvptable);
}
if (hadprefix)
Jim_SetResult_NvpUnknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
else
Jim_SetResult_NvpUnknown(goi->interp, NULL, goi->argv[-1], nvptable);
}
int Jim_GetOpt_Enum(Jim_GetOptInfo * goi, const char *const *lookup, int *puthere)
int Jim_GetOpt_Enum(Jim_GetOptInfo *goi, const char *const *lookup, int *puthere)
{
int _safe;
Jim_Obj *o;
int e;
int _safe;
Jim_Obj *o;
int e;
if (puthere == NULL) {
puthere = &_safe;
}
e = Jim_GetOpt_Obj(goi, &o);
if (e == JIM_OK) {
e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
}
return e;
if (puthere == NULL)
puthere = &_safe;
e = Jim_GetOpt_Obj(goi, &o);
if (e == JIM_OK)
e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
return e;
}
void
Jim_SetResult_NvpUnknown(Jim_Interp *interp,
Jim_Obj *param_name, Jim_Obj *param_value, const Jim_Nvp * nvp)
void Jim_SetResult_NvpUnknown(Jim_Interp *interp,
Jim_Obj *param_name, Jim_Obj *param_value, const Jim_Nvp *nvp)
{
if (param_name) {
Jim_SetResultFormatted(interp, "%#s: Unknown: %#s, try one of: ", param_name, param_value);
}
else {
Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
}
while (nvp->name) {
const char *a;
const char *b;
if (param_name)
Jim_SetResultFormatted(interp,
"%#s: Unknown: %#s, try one of: ",
param_name,
param_value);
else
Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
while (nvp->name) {
const char *a;
const char *b;
if ((nvp + 1)->name) {
a = nvp->name;
b = ", ";
}
else {
a = "or ";
b = nvp->name;
}
Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
nvp++;
}
if ((nvp + 1)->name) {
a = nvp->name;
b = ", ";
} else {
a = "or ";
b = nvp->name;
}
Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
nvp++;
}
}
const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
static Jim_Obj *debug_string_obj;
static Jim_Obj *debug_string_obj;
int x;
int x;
if (debug_string_obj) {
Jim_FreeObj(interp, debug_string_obj);
}
if (debug_string_obj)
Jim_FreeObj(interp, debug_string_obj);
debug_string_obj = Jim_NewEmptyStringObj(interp);
for (x = 0; x < argc; x++) {
Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
}
debug_string_obj = Jim_NewEmptyStringObj(interp);
for (x = 0; x < argc; x++)
Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
return Jim_String(debug_string_obj);
return Jim_String(debug_string_obj);
}
int Jim_nvpInit(Jim_Interp *interp)
{
/* This is really a helper library, not an extension, but this is the easy way */
return JIM_OK;
/* This is really a helper library, not an extension, but this is the easy way */
return JIM_OK;
}

View File

@ -83,34 +83,47 @@
*/
typedef struct {
const char *name;
int value;
int value;
} Jim_Nvp;
int Jim_GetNvp (Jim_Interp *interp,
Jim_Obj *objPtr,
const Jim_Nvp *nvp_table,
const Jim_Nvp **result);
int Jim_GetNvp(Jim_Interp *interp,
Jim_Obj *objPtr,
const Jim_Nvp *nvp_table,
const Jim_Nvp **result);
/* Name Value Pairs Operations */
Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp *nvp_table, const char *name);
Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp *nvp_table, const char *name);
Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp *nvp_table, int v);
int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp *nvp_table, const char *name, Jim_Nvp **result);
int Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp *nvp_table, const char *name, Jim_Nvp **result);
int Jim_Nvp_name2value(Jim_Interp *interp,
const Jim_Nvp *nvp_table,
const char *name,
Jim_Nvp **result);
int Jim_Nvp_name2value_nocase(Jim_Interp *interp,
const Jim_Nvp *nvp_table,
const char *name,
Jim_Nvp **result);
int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp *nvp_table, int value, Jim_Nvp **result);
int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *name_obj, Jim_Nvp **result);
int Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *name_obj, Jim_Nvp **result);
int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *value_obj, Jim_Nvp **result);
int Jim_Nvp_name2value_obj(Jim_Interp *interp,
const Jim_Nvp *nvp_table,
Jim_Obj *name_obj,
Jim_Nvp **result);
int Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp,
const Jim_Nvp *nvp_table,
Jim_Obj *name_obj,
Jim_Nvp **result);
int Jim_Nvp_value2name_obj(Jim_Interp *interp,
const Jim_Nvp *nvp_table,
Jim_Obj *value_obj,
Jim_Nvp **result);
/** prints a nice 'unknown' parameter error message to the 'result' */
void Jim_SetResult_NvpUnknown(Jim_Interp *interp,
Jim_Obj *param_name,
Jim_Obj *param_value,
const Jim_Nvp *nvp_table);
Jim_Obj *param_name,
Jim_Obj *param_value,
const Jim_Nvp *nvp_table);
/** Debug: convert argc/argv into a printable string for printf() debug
*
@ -145,10 +158,10 @@ const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *a
*/
typedef struct jim_getopt {
Jim_Interp *interp;
int argc;
Jim_Obj * const * argv;
int isconfigure; /* non-zero if configure */
Jim_Interp *interp;
int argc;
Jim_Obj *const *argv;
int isconfigure; /* non-zero if configure */
} Jim_GetOptInfo;
/** GetOpt - how to.
@ -208,9 +221,9 @@ typedef struct jim_getopt {
*/
int Jim_GetOpt_Setup(Jim_GetOptInfo *goi,
Jim_Interp *interp,
int argc,
Jim_Obj * const * argv);
Jim_Interp *interp,
int argc,
Jim_Obj *const *argv);
/** Debug - Dump parameters to stderr
@ -218,8 +231,6 @@ int Jim_GetOpt_Setup(Jim_GetOptInfo *goi,
*/
void Jim_GetOpt_Debug(Jim_GetOptInfo *goi);
/** Remove argv[0] from the list.
*
* \param goi - get opt info
@ -313,6 +324,6 @@ void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo *goi, const Jim_Nvp *lookup, int hadpr
* \param puthere - where param is put.
*
*/
int Jim_GetOpt_Enum(Jim_GetOptInfo *goi, const char * const * lookup, int *puthere);
int Jim_GetOpt_Enum(Jim_GetOptInfo *goi, const char *const *lookup, int *puthere);
#endif

View File

@ -23,12 +23,13 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "time_support.h"
// @todo the inclusion of server.h here is a layering violation
/* @todo the inclusion of server.h here is a layering violation */
#include <server/server.h>
#include <stdarg.h>
@ -43,72 +44,62 @@
int debug_level = -1;
static FILE* log_output;
static struct log_callback *log_callbacks = NULL;
static FILE *log_output;
static struct log_callback *log_callbacks;
static long long last_time;
static long long current_time;
static long long start;
static char *log_strings[5] =
{
static char *log_strings[5] = {
"User : ",
"Error: ",
"Warn : ", /* want a space after each colon, all same width, colons aligned */
"Warn : ", /* want a space after each colon, all same width, colons aligned */
"Info : ",
"Debug: "
};
static int count;
static int count = 0;
static struct store_log_forward *log_head;
static int log_forward_count;
static struct store_log_forward * log_head = NULL;
static int log_forward_count = 0;
struct store_log_forward
{
struct store_log_forward * next;
const char * file;
struct store_log_forward {
struct store_log_forward *next;
const char *file;
int line;
const char * function;
const char * string;
const char *function;
const char *string;
};
/* either forward the log to the listeners or store it for possible forwarding later */
static void log_forward(const char *file, unsigned line, const char *function, const char *string)
{
if (log_forward_count==0)
{
if (log_forward_count == 0) {
struct log_callback *cb, *next;
cb = log_callbacks;
/* DANGER!!!! the log callback can remove itself!!!! */
while (cb)
{
while (cb) {
next = cb->next;
cb->fn(cb->priv, file, line, function, string);
cb = next;
}
} else
{
struct store_log_forward *log = malloc(sizeof (struct store_log_forward));
} else {
struct store_log_forward *log = malloc(sizeof(struct store_log_forward));
log->file = strdup(file);
log->line = line;
log->function = strdup(function);
log->string = strdup(string);
log->next = NULL;
if (log_head==NULL)
if (log_head == NULL)
log_head = log;
else
{
else {
/* append to tail */
struct store_log_forward * t;
struct store_log_forward *t;
t = log_head;
while (t->next!=NULL)
{
while (t->next != NULL)
t = t->next;
}
t->next = log;
}
}
@ -124,11 +115,14 @@ static void log_forward(const char *file, unsigned line, const char *function, c
* target_request.c).
*
*/
static void log_puts(enum log_levels level, const char *file, int line, const char *function, const char *string)
static void log_puts(enum log_levels level,
const char *file,
int line,
const char *function,
const char *string)
{
char *f;
if (level == LOG_LVL_OUTPUT)
{
if (level == LOG_LVL_OUTPUT) {
/* do not prepend any headers, just print out what we were given and return */
fputs(string, log_output);
fflush(log_output);
@ -139,10 +133,8 @@ static void log_puts(enum log_levels level, const char *file, int line, const ch
if (f != NULL)
file = f + 1;
if (strlen(string) > 0)
{
if (debug_level >= LOG_LVL_DEBUG)
{
if (strlen(string) > 0) {
if (debug_level >= LOG_LVL_DEBUG) {
/* print with count and time information */
int t = (int)(timeval_ms()-start);
#ifdef _DEBUG_FREE_SPACE_
@ -151,37 +143,37 @@ static void log_puts(enum log_levels level, const char *file, int line, const ch
#endif
fprintf(log_output, "%s%d %d %s:%d %s()"
#ifdef _DEBUG_FREE_SPACE_
" %d"
" %d"
#endif
": %s", log_strings[level + 1], count, t, file, line, function,
": %s", log_strings[level + 1], count, t, file, line, function,
#ifdef _DEBUG_FREE_SPACE_
info.fordblks,
info.fordblks,
#endif
string);
}
else
{
string);
} else {
/* if we are using gdb through pipes then we do not want any output
* to the pipe otherwise we get repeated strings */
fprintf(log_output, "%s%s",
(level > LOG_LVL_USER)?log_strings[level + 1]:"", string);
(level > LOG_LVL_USER) ? log_strings[level + 1] : "", string);
}
} else
{
/* Empty strings are sent to log callbacks to keep e.g. gdbserver alive, here we do nothing. */
} else {
/* Empty strings are sent to log callbacks to keep e.g. gdbserver alive, here we do
*nothing. */
}
fflush(log_output);
/* Never forward LOG_LVL_DEBUG, too verbose and they can be found in the log if need be */
if (level <= LOG_LVL_INFO)
{
log_forward(file, line, function, string);
}
}
void log_printf(enum log_levels level, const char *file, unsigned line, const char *function, const char *format, ...)
void log_printf(enum log_levels level,
const char *file,
unsigned line,
const char *function,
const char *format,
...)
{
char *string;
va_list ap;
@ -193,8 +185,7 @@ void log_printf(enum log_levels level, const char *file, unsigned line, const ch
va_start(ap, format);
string = alloc_vprintf(format, ap);
if (string != NULL)
{
if (string != NULL) {
log_puts(level, file, line, function, string);
free(string);
}
@ -202,7 +193,12 @@ void log_printf(enum log_levels level, const char *file, unsigned line, const ch
va_end(ap);
}
void log_printf_lf(enum log_levels level, const char *file, unsigned line, const char *function, const char *format, ...)
void log_printf_lf(enum log_levels level,
const char *file,
unsigned line,
const char *function,
const char *format,
...)
{
char *string;
va_list ap;
@ -214,9 +210,9 @@ void log_printf_lf(enum log_levels level, const char *file, unsigned line, const
va_start(ap, format);
string = alloc_vprintf(format, ap);
if (string != NULL)
{
strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
if (string != NULL) {
strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one
*char longer */
log_puts(level, file, line, function, string);
free(string);
}
@ -226,18 +222,15 @@ void log_printf_lf(enum log_levels level, const char *file, unsigned line, const
COMMAND_HANDLER(handle_debug_level_command)
{
if (CMD_ARGC == 1)
{
if (CMD_ARGC == 1) {
int new_level;
COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], new_level);
if ((debug_level > LOG_LVL_DEBUG) || (new_level < LOG_LVL_SILENT))
{
if ((debug_level > LOG_LVL_DEBUG) || (new_level < LOG_LVL_SILENT)) {
LOG_ERROR("level must be between %d and %d", LOG_LVL_SILENT, LOG_LVL_DEBUG);
return ERROR_COMMAND_SYNTAX_ERROR;
}
debug_level = new_level;
}
else if (CMD_ARGC > 1)
} else if (CMD_ARGC > 1)
return ERROR_COMMAND_SYNTAX_ERROR;
command_print(CMD_CTX, "debug_level: %i", debug_level);
@ -247,14 +240,11 @@ COMMAND_HANDLER(handle_debug_level_command)
COMMAND_HANDLER(handle_log_output_command)
{
if (CMD_ARGC == 1)
{
FILE* file = fopen(CMD_ARGV[0], "w");
if (CMD_ARGC == 1) {
FILE *file = fopen(CMD_ARGV[0], "w");
if (file)
{
log_output = file;
}
}
return ERROR_OK;
@ -293,16 +283,13 @@ void log_init(void)
debug_level = LOG_LVL_INFO;
char *debug_env = getenv("OPENOCD_DEBUG_LEVEL");
if (NULL != debug_env)
{
if (NULL != debug_env) {
int value;
int retval = parse_int(debug_env, &value);
if (ERROR_OK == retval &&
debug_level >= LOG_LVL_SILENT &&
debug_level <= LOG_LVL_DEBUG)
{
debug_level = value;
}
debug_level = value;
}
if (log_output == NULL)
@ -323,14 +310,15 @@ int log_add_callback(log_callback_fn fn, void *priv)
struct log_callback *cb;
/* prevent the same callback to be registered more than once, just for sure */
for (cb = log_callbacks; cb; cb = cb->next)
{
for (cb = log_callbacks; cb; cb = cb->next) {
if (cb->fn == fn && cb->priv == priv)
return ERROR_COMMAND_SYNTAX_ERROR;
}
/* alloc memory, it is safe just to return in case of an error, no need for the caller to check this */
if ((cb = malloc(sizeof(struct log_callback))) == NULL)
/* alloc memory, it is safe just to return in case of an error, no need for the caller to
*check this */
cb = malloc(sizeof(struct log_callback));
if (cb == NULL)
return ERROR_BUF_TOO_SMALL;
/* add item to the beginning of the linked list */
@ -346,10 +334,8 @@ int log_remove_callback(log_callback_fn fn, void *priv)
{
struct log_callback *cb, **p;
for (p = &log_callbacks; (cb = *p); p = &(*p)->next)
{
if (cb->fn == fn && cb->priv == priv)
{
for (p = &log_callbacks; (cb = *p); p = &(*p)->next) {
if (cb->fn == fn && cb->priv == priv) {
*p = cb->next;
free(cb);
return ERROR_OK;
@ -420,8 +406,7 @@ char *alloc_printf(const char *format, ...)
void keep_alive()
{
current_time = timeval_ms();
if (current_time-last_time > 1000)
{
if (current_time-last_time > 1000) {
extern int gdb_actual_connections;
if (gdb_actual_connections)
@ -436,8 +421,7 @@ void keep_alive()
"trouble with GDB connections.",
current_time-last_time);
}
if (current_time-last_time > 500)
{
if (current_time-last_time > 500) {
/* this will keep the GDB connection alive */
LOG_USER_N("%s", "");
@ -464,8 +448,7 @@ void kept_alive()
void alive_sleep(uint64_t ms)
{
uint64_t napTime = 10;
for (uint64_t i = 0; i < ms; i += napTime)
{
for (uint64_t i = 0; i < ms; i += napTime) {
uint64_t sleep_a_bit = ms - i;
if (sleep_a_bit > napTime)
sleep_a_bit = napTime;
@ -478,8 +461,9 @@ void alive_sleep(uint64_t ms)
void busy_sleep(uint64_t ms)
{
uint64_t then = timeval_ms();
while (timeval_ms() - then < ms)
{
/* busy wait */
while (timeval_ms() - then < ms) {
/*
* busy wait
*/
}
}

View File

@ -23,6 +23,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef ERROR_H
#define ERROR_H
@ -48,8 +49,7 @@
* LOG_LVL_INFO - state information, etc.
* LOG_LVL_DEBUG - debug statements, execution trace
*/
enum log_levels
{
enum log_levels {
LOG_LVL_SILENT = -3,
LOG_LVL_OUTPUT = -2,
LOG_LVL_USER = -1,
@ -60,10 +60,10 @@ enum log_levels
};
void log_printf(enum log_levels level, const char *file, unsigned line,
const char *function, const char *format, ...)
const char *function, const char *format, ...)
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6)));
void log_printf_lf(enum log_levels level, const char *file, unsigned line,
const char *function, const char *format, ...)
const char *function, const char *format, ...)
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6)));
/**
@ -103,40 +103,40 @@ extern int debug_level;
#define LOG_LEVEL_IS(FOO) ((debug_level) >= (FOO))
#define LOG_DEBUG(expr ...) \
do { \
if (debug_level >= LOG_LVL_DEBUG) \
log_printf_lf(LOG_LVL_DEBUG, \
__FILE__, __LINE__, __func__, \
expr); \
} while (0)
do { \
if (debug_level >= LOG_LVL_DEBUG) \
log_printf_lf(LOG_LVL_DEBUG, \
__FILE__, __LINE__, __func__, \
expr); \
} while (0)
#define LOG_INFO(expr ...) \
log_printf_lf (LOG_LVL_INFO, __FILE__, __LINE__, __FUNCTION__, expr)
log_printf_lf(LOG_LVL_INFO, __FILE__, __LINE__, __func__, expr)
#define LOG_WARNING(expr ...) \
log_printf_lf (LOG_LVL_WARNING, __FILE__, __LINE__, __FUNCTION__, expr)
log_printf_lf(LOG_LVL_WARNING, __FILE__, __LINE__, __func__, expr)
#define LOG_ERROR(expr ...) \
log_printf_lf (LOG_LVL_ERROR, __FILE__, __LINE__, __FUNCTION__, expr)
log_printf_lf(LOG_LVL_ERROR, __FILE__, __LINE__, __func__, expr)
#define LOG_USER(expr ...) \
log_printf_lf (LOG_LVL_USER, __FILE__, __LINE__, __FUNCTION__, expr)
log_printf_lf(LOG_LVL_USER, __FILE__, __LINE__, __func__, expr)
#define LOG_USER_N(expr ...) \
log_printf (LOG_LVL_USER, __FILE__, __LINE__, __FUNCTION__, expr)
log_printf(LOG_LVL_USER, __FILE__, __LINE__, __func__, expr)
#define LOG_OUTPUT(expr ...) \
log_printf (LOG_LVL_OUTPUT, __FILE__, __LINE__, __FUNCTION__, expr)
log_printf(LOG_LVL_OUTPUT, __FILE__, __LINE__, __func__, expr)
/* general failures
* error codes < 100
*/
#define ERROR_OK (0)
#define ERROR_NO_CONFIG_FILE (-2)
#define ERROR_BUF_TOO_SMALL (-3)
#define ERROR_OK (0)
#define ERROR_NO_CONFIG_FILE (-2)
#define ERROR_BUF_TOO_SMALL (-3)
/* see "Error:" log entry for meaningful message to the user. The caller should
* make no assumptions about what went wrong and try to handle the problem.
*/
#define ERROR_FAIL (-4)
#define ERROR_FAIL (-4)
#endif /* LOG_H */
#endif /* LOG_H */

View File

@ -20,32 +20,32 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "configuration.h"
// @todo the inclusion of server.h here is a layering violation
/* @todo the inclusion of server.h here is a layering violation */
#include <server/server.h>
#include <getopt.h>
static int help_flag, version_flag;
static const struct option long_options[] =
{
{"help", no_argument, &help_flag, 1},
{"version", no_argument, &version_flag, 1},
{"debug", optional_argument, 0, 'd'},
{"file", required_argument, 0, 'f'},
{"search", required_argument, 0, 's'},
{"log_output", required_argument, 0, 'l'},
{"command", required_argument, 0, 'c'},
{"pipe", no_argument, 0, 'p'},
static const struct option long_options[] = {
{"help", no_argument, &help_flag, 1},
{"version", no_argument, &version_flag, 1},
{"debug", optional_argument, 0, 'd'},
{"file", required_argument, 0, 'f'},
{"search", required_argument, 0, 's'},
{"log_output", required_argument, 0, 'l'},
{"command", required_argument, 0, 'c'},
{"pipe", no_argument, 0, 'p'},
{0, 0, 0, 0}
};
int configuration_output_handler(struct command_context *context, const char* line)
int configuration_output_handler(struct command_context *context, const char *line)
{
LOG_USER_N("%s", line);
@ -64,8 +64,8 @@ static void add_default_dirs(void)
* target\at91eb40a.cfg
*/
{
char strExePath [MAX_PATH];
GetModuleFileName (NULL, strExePath, MAX_PATH);
char strExePath[MAX_PATH];
GetModuleFileName(NULL, strExePath, MAX_PATH);
/* Either this code will *always* work or it will SEGFAULT giving
* excellent information on the culprit.
*/
@ -84,11 +84,11 @@ static void add_default_dirs(void)
* share/openocd/scripts/target/at91eb40a.cfg
*/
{
char strExePath [MAX_PATH];
char strExePath[MAX_PATH];
char *p;
GetModuleFileName (NULL, strExePath, MAX_PATH);
GetModuleFileName(NULL, strExePath, MAX_PATH);
*strrchr(strExePath, '\\') = 0;
strcat(strExePath, "/../share/"PACKAGE"/scripts");
strcat(strExePath, "/../share/"PACKAGE "/scripts");
for (p = strExePath; *p; p++) {
if (*p == '\\')
*p = '/';
@ -104,14 +104,12 @@ static void add_default_dirs(void)
const char *home = getenv("HOME");
if (home)
{
if (home) {
char *path;
path = alloc_printf("%s/.openocd", home);
if (path)
{
if (path) {
add_script_search_dir(path);
free(path);
}
@ -127,8 +125,7 @@ int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
int c;
char command_buffer[128];
while (1)
{
while (1) {
/* getopt_long stores the option index here. */
int option_index = 0;
@ -138,56 +135,52 @@ int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
if (c == -1)
break;
switch (c)
{
switch (c) {
case 0:
break;
case 'h': /* --help | -h */
case 'h': /* --help | -h */
help_flag = 1;
break;
case 'v': /* --version | -v */
case 'v': /* --version | -v */
version_flag = 1;
break;
case 'f': /* --file | -f */
case 'f': /* --file | -f */
{
snprintf(command_buffer, 128, "script {%s}", optarg);
add_config_command(command_buffer);
break;
}
case 's': /* --search | -s */
case 's': /* --search | -s */
add_script_search_dir(optarg);
break;
case 'd': /* --debug | -d */
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)
{
case 'l': /* --log_output | -l */
if (optarg) {
snprintf(command_buffer, 128, "log_output %s", optarg);
command_run_line(cmd_ctx, command_buffer);
}
break;
case 'c': /* --command | -c */
case 'c': /* --command | -c */
if (optarg)
{
add_config_command(optarg);
}
add_config_command(optarg);
break;
case 'p':
/* to replicate the old syntax this needs to be synchronous
* otherwise the gdb stdin will overflow with the warning message */
command_run_line(cmd_ctx, "gdb_port pipe; log_output openocd.log");
LOG_WARNING("deprecated option: -p/--pipe. Use '-c \"gdb_port pipe; log_output openocd.log\"' instead.");
LOG_WARNING("deprecated option: -p/--pipe. Use '-c \"gdb_port pipe; "
"log_output openocd.log\"' instead.");
break;
}
}
if (help_flag)
{
if (help_flag) {
LOG_OUTPUT("Open On-Chip Debugger\nLicensed under GNU GPL v2\n");
LOG_OUTPUT("--help | -h\tdisplay this help\n");
LOG_OUTPUT("--version | -v\tdisplay OpenOCD version\n");
@ -199,10 +192,9 @@ int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
exit(-1);
}
if (version_flag)
{
if (version_flag) {
/* Nothing to do, version gets printed automatically. */
// It is not an error to request the VERSION number.
/* It is not an error to request the VERSION number. */
exit(0);
}

View File

@ -36,22 +36,19 @@ void *clear_malloc(size_t size)
{
void *t = malloc(size);
if (t != NULL)
{
memset(t, 0x00, size);
}
return t;
}
void *fill_malloc(size_t size)
{
void *t = malloc(size);
if (t != NULL)
{
/* We want to initialize memory to some known bad state. */
/* 0 and 0xff yields 0 and -1 as integers, which often */
/* have meaningful values. 0x5555... is not often a valid */
/* integer and is quite easily spotted in the debugger */
/* also it is almost certainly an invalid address */
if (t != NULL) {
/* We want to initialize memory to some known bad state.
* 0 and 0xff yields 0 and -1 as integers, which often
* have meaningful values. 0x5555... is not often a valid
* integer and is quite easily spotted in the debugger
* also it is almost certainly an invalid address */
memset(t, 0x55, size);
}
return t;
@ -88,22 +85,22 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
__int64 t;
static int tzflag;
if (tv)
{
if (tv) {
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
t = li.QuadPart; /* In 100-nanosecond intervals */
t -= EPOCHFILETIME; /* Offset to the Epoch time */
t /= 10; /* In microseconds */
t = li.QuadPart; /* In 100-nanosecond
*intervals */
t -= EPOCHFILETIME; /* Offset to the Epoch time
**/
t /= 10; /* In microseconds
**/
tv->tv_sec = (long)(t / 1000000);
tv->tv_usec = (long)(t % 1000000);
}
if (tz)
{
if (!tzflag)
{
if (tz) {
if (!tzflag) {
_tzset();
tzflag++;
}
@ -113,29 +110,29 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
return 0;
}
#endif /* _WIN32 */
#endif /* _WIN32 */
#endif /* HAVE_GETTIMEOFDAY */
#endif /* HAVE_GETTIMEOFDAY */
#ifndef HAVE_STRNLEN
size_t strnlen(const char *s, size_t maxlen)
{
const char *end= (const char *)memchr(s, '\0', maxlen);
const char *end = (const char *)memchr(s, '\0', maxlen);
return end ? (size_t) (end - s) : maxlen;
}
#endif
#ifndef HAVE_STRNDUP
char* strndup(const char *s, size_t n)
char *strndup(const char *s, size_t n)
{
size_t len = strnlen (s, n);
char *new = (char *) malloc (len + 1);
size_t len = strnlen(s, n);
char *new = (char *) malloc(len + 1);
if (new == NULL)
return NULL;
new[len] = '\0';
return (char *) memcpy (new, s, len);
return (char *) memcpy(new, s, len);
}
#endif
@ -152,12 +149,12 @@ int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct time
struct timeval tvslice;
int retcode;
#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
/* calculate how long we need to wait in milliseconds */
if (tv == NULL) {
if (tv == NULL)
ms_total = INFINITE;
} else {
else {
ms_total = tv->tv_sec * 1000;
ms_total += tv->tv_usec / 1000;
}
@ -173,18 +170,14 @@ int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct time
handles[n_handles] = (HANDLE)handle;
if (handles[n_handles] == INVALID_HANDLE_VALUE) {
/* socket */
if (SAFE_FD_ISSET(i, rfds)) {
if (SAFE_FD_ISSET(i, rfds))
FD_SET(i, &sock_read);
}
if (SAFE_FD_ISSET(i, wfds)) {
if (SAFE_FD_ISSET(i, wfds))
FD_SET(i, &sock_write);
}
if (SAFE_FD_ISSET(i, efds)) {
if (SAFE_FD_ISSET(i, efds))
FD_SET(i, &sock_except);
}
if (i > sock_max_fd) {
if (i > sock_max_fd)
sock_max_fd = i;
}
} else {
handle_slot_to_fd[n_handles] = i;
n_handles++;
@ -224,7 +217,11 @@ int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct time
/* check handles */
DWORD wret;
wret = MsgWaitForMultipleObjects(n_handles, handles, FALSE, retcode > 0 ? 0 : 100, QS_ALLEVENTS);
wret = MsgWaitForMultipleObjects(n_handles,
handles,
FALSE,
retcode > 0 ? 0 : 100,
QS_ALLEVENTS);
if (wret == WAIT_TIMEOUT) {
/* set retcode to 0; this is the default.
@ -233,31 +230,30 @@ int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct time
* does nothing */
;
} else if (wret == WAIT_FAILED) {
if (retcode == 0) {
if (retcode == 0)
retcode = -1;
}
} else {
if (retcode < 0) {
if (retcode < 0)
retcode = 0;
}
for (i = 0; i < n_handles; i++) {
if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
DWORD dwBytes;
intptr_t handle = (intptr_t) _get_osfhandle(handle_slot_to_fd[i]);
intptr_t handle = (intptr_t) _get_osfhandle(
handle_slot_to_fd[i]);
if (PeekNamedPipe((HANDLE)handle, NULL, 0, NULL, &dwBytes, NULL))
{
/* check to see if gdb pipe has data available */
if (dwBytes)
{
FD_SET(handle_slot_to_fd[i], &aread);
if (PeekNamedPipe((HANDLE)handle, NULL, 0,
NULL, &dwBytes, NULL)) {
/* check to see if gdb pipe has data
*available */
if (dwBytes) {
FD_SET(handle_slot_to_fd[i],
&aread);
retcode++;
}
}
else
{
FD_SET(handle_slot_to_fd[i], &aread);
} else {
FD_SET(handle_slot_to_fd[i],
&aread);
retcode++;
}
}
@ -275,15 +271,12 @@ int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct time
}
} while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
if (rfds) {
if (rfds)
*rfds = aread;
}
if (wfds) {
if (wfds)
*wfds = awrite;
}
if (efds) {
if (efds)
*efds = aexcept;
}
return retcode;
}

View File

@ -23,15 +23,16 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef REPLACEMENTS_H
#define REPLACEMENTS_H
/* MIN,MAX macros */
#ifndef MIN
#define MIN(a,b) (((a) < (b))?(a):(b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a,b) (((a) > (b))?(a):(b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
/* for systems that do not support ENOTSUP
@ -56,7 +57,7 @@ struct timeval {
long tv_usec;
};
#endif /* _TIMEVAL_DEFINED */
#endif /* _TIMEVAL_DEFINED */
#endif
@ -107,21 +108,21 @@ void *fill_malloc(size_t size);
* the following macros. Which is the default way.
*/
/* #define malloc(_a) clear_malloc(_a) */
/* #define malloc(_a) fill_malloc(_a) */
/* #define malloc(_a) clear_malloc(_a)
* #define malloc(_a) fill_malloc(_a) */
/* GNU extensions to the C library that may be missing on some systems */
#ifndef HAVE_STRNDUP
char* strndup(const char *s, size_t n);
#endif /* HAVE_STRNDUP */
char *strndup(const char *s, size_t n);
#endif /* HAVE_STRNDUP */
#ifndef HAVE_STRNLEN
size_t strnlen(const char *s, size_t maxlen);
#endif /* HAVE_STRNLEN */
#endif /* HAVE_STRNLEN */
#ifndef HAVE_USLEEP
#ifdef _WIN32
static __inline unsigned usleep(unsigned int usecs)
static inline unsigned usleep(unsigned int usecs)
{
Sleep((usecs/1000));
return 0;
@ -133,7 +134,7 @@ void usleep(int us);
#error no usleep defined for your platform
#endif
#endif
#endif /* HAVE_USLEEP */
#endif /* HAVE_USLEEP */
/* Windows specific */
#ifdef _WIN32
@ -149,29 +150,29 @@ void usleep(int us);
#endif
#if IS_MINGW == 1
static __inline unsigned char inb(unsigned short int port)
static inline unsigned char inb(unsigned short int port)
{
unsigned char _v;
__asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (port));
__asm__ __volatile__ ("inb %w1,%0" : "=a" (_v) : "Nd" (port));
return _v;
}
static __inline void outb(unsigned char value, unsigned short int port)
static inline void outb(unsigned char value, unsigned short int port)
{
__asm__ __volatile__ ("outb %b0,%w1": :"a" (value), "Nd" (port));
__asm__ __volatile__ ("outb %b0,%w1" : : "a" (value), "Nd" (port));
}
/* mingw does not have ffs, so use gcc builtin types */
#define ffs __builtin_ffs
#endif /* IS_MINGW */
#endif /* IS_MINGW */
int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv);
#endif /* _WIN32 */
#endif /* _WIN32 */
/* generic socket functions for Windows and Posix */
static __inline int write_socket(int handle, const void *buffer, unsigned int count)
static inline int write_socket(int handle, const void *buffer, unsigned int count)
{
#ifdef _WIN32
return send(handle, buffer, count, 0);
@ -180,7 +181,7 @@ static __inline int write_socket(int handle, const void *buffer, unsigned int co
#endif
}
static __inline int read_socket(int handle, void *buffer, unsigned int count)
static inline int read_socket(int handle, void *buffer, unsigned int count)
{
#ifdef _WIN32
return recv(handle, buffer, count, 0);
@ -189,7 +190,7 @@ static __inline int read_socket(int handle, void *buffer, unsigned int count)
#endif
}
static __inline int close_socket(int sock)
static inline int close_socket(int sock)
{
#ifdef _WIN32
return closesocket(sock);
@ -198,7 +199,7 @@ static __inline int close_socket(int sock)
#endif
}
static __inline void socket_nonblock(int fd)
static inline void socket_nonblock(int fd)
{
#ifdef _WIN32
unsigned long nonblock = 1;
@ -209,7 +210,11 @@ static __inline void socket_nonblock(int fd)
#endif
}
static __inline int socket_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
static inline int socket_select(int max_fd,
fd_set *rfds,
fd_set *wfds,
fd_set *efds,
struct timeval *tv)
{
#ifdef _WIN32
return win_select(max_fd, rfds, wfds, efds, tv);
@ -222,45 +227,43 @@ static __inline int socket_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set
#include <helper/types.h>
typedef uint32_t Elf32_Addr;
typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Off;
typedef int32_t Elf32_Sword;
typedef uint32_t Elf32_Word;
typedef uint32_t Elf32_Size;
typedef Elf32_Off Elf32_Hashelt;
typedef uint32_t Elf32_Addr;
typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Off;
typedef int32_t Elf32_Sword;
typedef uint32_t Elf32_Word;
typedef uint32_t Elf32_Size;
typedef Elf32_Off Elf32_Hashelt;
typedef struct
{
unsigned char e_ident[16]; /* Magic number and other info */
Elf32_Half e_type; /* Object file type */
Elf32_Half e_machine; /* Architecture */
Elf32_Word e_version; /* Object file version */
Elf32_Addr e_entry; /* Entry point virtual address */
Elf32_Off e_phoff; /* Program header table file offset */
Elf32_Off e_shoff; /* Section header table file offset */
Elf32_Word e_flags; /* Processor-specific flags */
Elf32_Half e_ehsize; /* ELF header size in bytes */
Elf32_Half e_phentsize; /* Program header table entry size */
Elf32_Half e_phnum; /* Program header table entry count */
Elf32_Half e_shentsize; /* Section header table entry size */
Elf32_Half e_shnum; /* Section header table entry count */
Elf32_Half e_shstrndx; /* Section header string table index */
typedef struct {
unsigned char e_ident[16]; /* Magic number and other info */
Elf32_Half e_type; /* Object file type */
Elf32_Half e_machine; /* Architecture */
Elf32_Word e_version; /* Object file version */
Elf32_Addr e_entry; /* Entry point virtual address */
Elf32_Off e_phoff; /* Program header table file offset */
Elf32_Off e_shoff; /* Section header table file offset */
Elf32_Word e_flags; /* Processor-specific flags */
Elf32_Half e_ehsize; /* ELF header size in bytes */
Elf32_Half e_phentsize; /* Program header table entry size */
Elf32_Half e_phnum; /* Program header table entry count */
Elf32_Half e_shentsize; /* Section header table entry size */
Elf32_Half e_shnum; /* Section header table entry count */
Elf32_Half e_shstrndx; /* Section header string table index */
} Elf32_Ehdr;
#define ELFMAG "\177ELF"
#define SELFMAG 4
#define ELFMAG "\177ELF"
#define SELFMAG 4
#define EI_CLASS 4 /* File class byte index */
#define ELFCLASS32 1 /* 32-bit objects */
#define ELFCLASS64 2 /* 64-bit objects */
#define EI_CLASS 4 /* File class byte index */
#define ELFCLASS32 1 /* 32-bit objects */
#define ELFCLASS64 2 /* 64-bit objects */
#define EI_DATA 5 /* Data encoding byte index */
#define ELFDATA2LSB 1 /* 2's complement, little endian */
#define ELFDATA2MSB 2 /* 2's complement, big endian */
#define EI_DATA 5 /* Data encoding byte index */
#define ELFDATA2LSB 1 /* 2's complement, little endian */
#define ELFDATA2MSB 2 /* 2's complement, big endian */
typedef struct
{
typedef struct {
Elf32_Word p_type; /* Segment type */
Elf32_Off p_offset; /* Segment file offset */
Elf32_Addr p_vaddr; /* Segment virtual address */
@ -271,8 +274,8 @@ typedef struct
Elf32_Size p_align; /* Segment alignment */
} Elf32_Phdr;
#define PT_LOAD 1 /* Loadable program segment */
#define PT_LOAD 1 /* Loadable program segment */
#endif /* HAVE_ELF_H */
#endif /* HAVE_ELF_H */
#endif /* REPLACEMENTS_H */
#endif /* REPLACEMENTS_H */

View File

@ -19,10 +19,11 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef SYSTEM_H
#define SYSTEM_H
// standard C library header files
/* standard C library header files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -30,7 +31,7 @@
#include <ctype.h>
#include <errno.h>
// +++ AC_HEADER_TIME +++
/* +++ AC_HEADER_TIME +++ */
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
@ -41,9 +42,9 @@
# include <time.h>
# endif
#endif
// --- AC_HEADER_TIME ---
/* --- AC_HEADER_TIME --- */
// +++ platform specific headers +++
/* +++ platform specific headers +++ */
#if BUILD_ECOSBOARD == 1
#include <pkgconf/system.h>
#endif
@ -53,7 +54,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#endif
// --- platform specific headers ---
/* --- platform specific headers --- */
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
@ -65,7 +66,7 @@
#ifdef __ECOS
/* missing from eCos */
#ifndef EFAULT
#define EFAULT 14 /* Bad address */
#define EFAULT 14 /* Bad address */
#endif
#endif
@ -73,10 +74,10 @@
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> /* select, FD_SET and friends (POSIX.1-2001) */
#include <sys/select.h> /* select, FD_SET and friends (POSIX.1-2001) */
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h> /* for MIN/MAX macros */
#include <sys/param.h> /* for MIN/MAX macros */
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
@ -86,8 +87,8 @@
#endif
#ifndef true
#define true 1
#define false 0
#define true 1
#define false 0
#endif
#endif // SYSTEM_H
#endif /* SYSTEM_H */

View File

@ -23,6 +23,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@ -32,8 +33,7 @@
/* 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)
{
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;
@ -56,8 +56,7 @@ int timeval_add_time(struct timeval *result, long sec, long usec)
result->tv_sec += sec;
result->tv_usec += usec;
while (result->tv_usec > 1000000)
{
while (result->tv_usec > 1000000) {
result->tv_usec -= 1000000;
result->tv_sec++;
}

View File

@ -23,6 +23,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef TIME_SUPPORT_H
#define TIME_SUPPORT_H
@ -40,23 +41,22 @@
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y);
int timeval_add_time(struct timeval *result, long sec, long usec);
/// @returns gettimeofday() timeval as 64-bit in ms
/* / @returns gettimeofday() timeval as 64-bit in ms */
int64_t timeval_ms(void);
struct duration
{
struct duration {
struct timeval start;
struct timeval elapsed;
};
/// Update the duration->start field to start the @a duration measurement.
/* / Update the duration->start field to start the @a duration measurement. */
int duration_start(struct duration *duration);
/// Update the duration->elapsed field to finish the @a duration measurment.
/* / Update the duration->elapsed field to finish the @a duration measurment. */
int duration_measure(struct duration *duration);
/// @returns Elapsed time in seconds.
/* / @returns Elapsed time in seconds. */
float duration_elapsed(struct duration *duration);
/// @returns KB/sec for the elapsed @a duration and @a count bytes.
/* / @returns KB/sec for the elapsed @a duration and @a count bytes. */
float duration_kbps(struct duration *duration, size_t count);
#endif /* TIME_SUPPORT_H */
#endif /* TIME_SUPPORT_H */

View File

@ -23,6 +23,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

View File

@ -23,6 +23,7 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@ -37,7 +38,7 @@ int64_t timeval_ms()
* profiling
*/
static const int ms_per_tick =
(CYGNUM_HAL_RTC_NUMERATOR / CYGNUM_HAL_RTC_DENOMINATOR) / 1000000;
(CYGNUM_HAL_RTC_NUMERATOR / CYGNUM_HAL_RTC_DENOMINATOR) / 1000000;
cyg_tick_count_t cur_time = cyg_current_time();
return ((int)cur_time) * ms_per_tick;
}

View File

@ -217,11 +217,11 @@ typedef uint64_t uintmax_t;
#define UINT64_MAX (__CONCAT(INT64_MAX, U) * 2ULL + 1ULL)
#endif
#ifndef LLONG_MAX
#ifndef LLONG_MAX
#define ULLONG_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
#define LLONG_MAX INT64_C(0x7FFFFFFFFFFFFFFF)
#define LLONG_MIN ULLONG_MAX
#endif
#endif
#define ULLONG_MAX 18446744073709551615

View File

@ -26,13 +26,11 @@
#include "log.h"
#include "time_support.h"
static int
util_Jim_Command_ms(Jim_Interp *interp,
int argc,
Jim_Obj * const *argv)
static int util_Jim_Command_ms(Jim_Interp *interp,
int argc,
Jim_Obj * const *argv)
{
if (argc != 1)
{
if (argc != 1) {
Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
return JIM_ERR;
}
@ -45,12 +43,13 @@ util_Jim_Command_ms(Jim_Interp *interp,
}
static const struct command_registration util_command_handlers[] = {
// jim handlers
/* jim handlers */
{
.name = "ms",
.mode = COMMAND_ANY,
.jim_handler = util_Jim_Command_ms,
.help = "Returns ever increasing milliseconds. Used to calculuate differences in time.",
.help =
"Returns ever increasing milliseconds. Used to calculuate differences in time.",
.usage = "",
},
COMMAND_REGISTRATION_DONE

View File

@ -24,4 +24,4 @@ struct command_context;
int util_init(struct command_context *cmd_ctx);
#endif // HELPER_UTILS_H
#endif /* HELPER_UTILS_H */