diff options
author | Spencer Oliver <spen@spen-soft.co.uk> | 2012-01-30 14:31:21 +0000 |
---|---|---|
committer | Spencer Oliver <spen@spen-soft.co.uk> | 2012-02-06 10:49:52 +0000 |
commit | 8b00e56e6433cff3ff818835953b27765aaabab0 (patch) | |
tree | 168a9feec9fb8735c7561fccf1856d796587d9c8 | |
parent | 9ad57e96b3c15c3dfbaa908abc842f264648ab55 (diff) |
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
-rw-r--r-- | src/helper/binarybuffer.c | 145 | ||||
-rw-r--r-- | src/helper/binarybuffer.h | 29 | ||||
-rw-r--r-- | src/helper/command.c | 475 | ||||
-rw-r--r-- | src/helper/command.h | 88 | ||||
-rw-r--r-- | src/helper/configuration.c | 32 | ||||
-rw-r--r-- | src/helper/configuration.h | 3 | ||||
-rw-r--r-- | src/helper/fileio.c | 51 | ||||
-rw-r--r-- | src/helper/fileio.h | 24 | ||||
-rw-r--r-- | src/helper/ioutil.c | 222 | ||||
-rw-r--r-- | src/helper/ioutil.h | 2 | ||||
-rw-r--r-- | src/helper/jim-nvp.c | 467 | ||||
-rw-r--r-- | src/helper/jim-nvp.h | 61 | ||||
-rw-r--r-- | src/helper/log.c | 168 | ||||
-rw-r--r-- | src/helper/log.h | 42 | ||||
-rw-r--r-- | src/helper/options.c | 78 | ||||
-rw-r--r-- | src/helper/replacements.c | 111 | ||||
-rw-r--r-- | src/helper/replacements.h | 119 | ||||
-rw-r--r-- | src/helper/system.h | 23 | ||||
-rw-r--r-- | src/helper/time_support.c | 7 | ||||
-rw-r--r-- | src/helper/time_support.h | 16 | ||||
-rw-r--r-- | src/helper/time_support_common.c | 1 | ||||
-rw-r--r-- | src/helper/time_support_ecos.c | 3 | ||||
-rw-r--r-- | src/helper/types.h | 4 | ||||
-rw-r--r-- | src/helper/util.c | 15 | ||||
-rw-r--r-- | src/helper/util.h | 2 |
25 files changed, 986 insertions, 1202 deletions
diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index 4f2ed6eb..3f7a876b 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -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 diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h index ae0ee9a1..a289abd9 100644 --- a/src/helper/binarybuffer.h +++ b/src/helper/binarybuffer.h @@ -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 */ diff --git a/src/helper/command.c b/src/helper/command.c index 3ed8dc87..6f7dcadc 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -26,6 +26,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -35,7 +36,7 @@ #define JIM_EMBEDDED #endif -// @todo the inclusion of target.h here is a layering violation +/* @todo the inclusion of target.h here is a layering violation */ #include <jtag/jtag.h> #include <target/target.h> #include "command.h" @@ -44,11 +45,9 @@ #include "time_support.h" #include "jim-eventloop.h" - /* nice short description of source file */ #define __THIS__FILE__ "command.c" - static int run_command(struct command_context *context, struct command *c, const char *words[], unsigned num_words); @@ -58,7 +57,7 @@ struct log_capture_state { }; static void tcl_output(void *privData, const char *file, unsigned line, - const char *function, const char *string) + const char *function, const char *string) { struct log_capture_state *state = (struct log_capture_state *)privData; Jim_AppendString(state->interp, state->output, string, strlen(string)); @@ -108,10 +107,8 @@ static void command_log_capture_finish(struct log_capture_state *state) Jim_GetString(state->output, &length); if (length > 0) - { Jim_SetResult(state->interp, state->output); - } else - { + else { /* No output captured, use tcl return value (which could * be empty too). */ } @@ -134,18 +131,17 @@ extern struct command_context *global_cmd_ctx; /* dump a single line to the log for the command. * Do nothing in case we are not at debug level 3 */ void script_debug(Jim_Interp *interp, const char *name, - unsigned argc, Jim_Obj *const *argv) + unsigned argc, Jim_Obj * const *argv) { if (debug_level < LOG_LVL_DEBUG) return; - char * dbg = alloc_printf("command - %s", name); - for (unsigned i = 0; i < argc; i++) - { + char *dbg = alloc_printf("command - %s", name); + for (unsigned i = 0; i < argc; i++) { int len; const char *w = Jim_GetString(argv[i], &len); - char * t = alloc_printf("%s %s", dbg, w); - free (dbg); + char *t = alloc_printf("%s %s", dbg, w); + free(dbg); dbg = t; } LOG_DEBUG("%s", dbg); @@ -159,20 +155,18 @@ static void script_command_args_free(const char **words, unsigned nwords) free(words); } static const char **script_command_args_alloc( - unsigned argc, Jim_Obj *const *argv, unsigned *nwords) + unsigned argc, Jim_Obj * const *argv, unsigned *nwords) { const char **words = malloc(argc * sizeof(char *)); if (NULL == words) return NULL; unsigned i; - for (i = 0; i < argc; i++) - { + for (i = 0; i < argc; i++) { int len; const char *w = Jim_GetString(argv[i], &len); words[i] = strdup(w); - if (words[i] == NULL) - { + if (words[i] == NULL) { script_command_args_free(words, i); return NULL; } @@ -185,8 +179,7 @@ struct command_context *current_command_context(Jim_Interp *interp) { /* grab the command context from the associated data */ struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context"); - if (NULL == cmd_ctx) - { + if (NULL == cmd_ctx) { /* Tcl can invoke commands directly instead of via command_run_line(). This would * happen when the Jim Tcl interpreter is provided by eCos or if we are running * commands in a startup script. @@ -200,10 +193,10 @@ struct command_context *current_command_context(Jim_Interp *interp) } static int script_command_run(Jim_Interp *interp, - int argc, Jim_Obj *const *argv, struct command *c, bool capture) + int argc, Jim_Obj * const *argv, struct command *c, bool capture) { target_call_timer_callbacks_now(); - LOG_USER_N("%s", ""); /* Keep GDB connection alive*/ + LOG_USER_N("%s", ""); /* Keep GDB connection alive*/ unsigned nwords; const char **words = script_command_args_alloc(argc, argv, &nwords); @@ -247,20 +240,19 @@ static struct command *command_root(struct command *c) */ static struct command *command_find(struct command *head, const char *name) { - for (struct command *cc = head; cc; cc = cc->next) - { + for (struct command *cc = head; cc; cc = cc->next) { if (strcmp(cc->name, name) == 0) return cc; } return NULL; } struct command *command_find_in_context(struct command_context *cmd_ctx, - const char *name) + const char *name) { return command_find(cmd_ctx->commands, name); } struct command *command_find_in_parent(struct command *parent, - const char *name) + const char *name) { return command_find(parent->children, name); } @@ -274,8 +266,7 @@ struct command *command_find_in_parent(struct command *parent, static void command_add_child(struct command **head, struct command *c) { assert(head); - if (NULL == *head) - { + if (NULL == *head) { *head = c; return; } @@ -293,17 +284,16 @@ static void command_add_child(struct command **head, struct command *c) } static struct command **command_list_for_parent( - struct command_context *cmd_ctx, struct command *parent) + struct command_context *cmd_ctx, struct command *parent) { return parent ? &parent->children : &cmd_ctx->commands; } static void command_free(struct command *c) { - /// @todo if command has a handler, unregister its jim command! + /* / @todo if command has a handler, unregister its jim command! */ - while (NULL != c->children) - { + while (NULL != c->children) { struct command *tmp = c->children; c->children = tmp->next; command_free(tmp); @@ -312,26 +302,25 @@ static void command_free(struct command *c) if (c->name) free((void *)c->name); if (c->help) - free((void*)c->help); + free((void *)c->help); if (c->usage) - free((void*)c->usage); + free((void *)c->usage); free(c); } static struct command *command_new(struct command_context *cmd_ctx, - struct command *parent, const struct command_registration *cr) + struct command *parent, const struct command_registration *cr) { assert(cr->name); /* - If it is a non-jim command with no .usage specified, - log an error. - - strlen(.usage) == 0 means that the command takes no - arguments. + * If it is a non-jim command with no .usage specified, + * log an error. + * + * strlen(.usage) == 0 means that the command takes no + * arguments. */ - if ((cr->jim_handler == NULL) && - (cr->usage == NULL)) { + if ((cr->jim_handler == NULL) && (cr->usage == NULL)) { LOG_DEBUG("BUG: command '%s%s%s' does not have the " "'.usage' field filled out", parent && parent->name ? parent->name : "", @@ -370,7 +359,7 @@ command_new_error: static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv); static int register_command_handler(struct command_context *cmd_ctx, - struct command *c) + struct command *c) { Jim_Interp *interp = cmd_ctx->interp; const char *ocd_name = alloc_printf("ocd_%s", c->name); @@ -398,8 +387,8 @@ static int register_command_handler(struct command_context *cmd_ctx, return retval; } -struct command* register_command(struct command_context *context, - struct command *parent, const struct command_registration *cr) +struct command *register_command(struct command_context *context, + struct command *parent, const struct command_registration *cr) { if (!context || !cr->name) return NULL; @@ -407,13 +396,12 @@ struct command* register_command(struct command_context *context, const char *name = cr->name; struct command **head = command_list_for_parent(context, parent); struct command *c = command_find(*head, name); - if (NULL != c) - { + if (NULL != c) { /* TODO: originally we treated attempting to register a cmd twice as an error * Sometimes we need this behaviour, such as with flash banks. * http://www.mail-archive.com/openocd-development@lists.berlios.de/msg11152.html */ LOG_DEBUG("command '%s' is already registered in '%s' context", - name, parent ? parent->name : "<global>"); + name, parent ? parent->name : "<global>"); return c; } @@ -422,16 +410,13 @@ struct command* register_command(struct command_context *context, return NULL; int retval = ERROR_OK; - if (NULL != cr->jim_handler && NULL == parent) - { + if (NULL != cr->jim_handler && NULL == parent) { retval = Jim_CreateCommand(context->interp, cr->name, cr->jim_handler, cr->jim_handler_data, NULL); - } - else if (NULL != cr->handler || NULL != parent) + } else if (NULL != cr->handler || NULL != parent) retval = register_command_handler(context, command_root(c)); - if (ERROR_OK != retval) - { + if (ERROR_OK != retval) { unregister_command(context, parent, name); c = NULL; } @@ -439,34 +424,29 @@ struct command* register_command(struct command_context *context, } int register_commands(struct command_context *cmd_ctx, struct command *parent, - const struct command_registration *cmds) + const struct command_registration *cmds) { int retval = ERROR_OK; unsigned i; - for (i = 0; cmds[i].name || cmds[i].chain; i++) - { + for (i = 0; cmds[i].name || cmds[i].chain; i++) { const struct command_registration *cr = cmds + i; struct command *c = NULL; - if (NULL != cr->name) - { + if (NULL != cr->name) { c = register_command(cmd_ctx, parent, cr); - if (NULL == c) - { + if (NULL == c) { retval = ERROR_FAIL; break; } } - if (NULL != cr->chain) - { + if (NULL != cr->chain) { struct command *p = c ? : parent; retval = register_commands(cmd_ctx, p, cr->chain); if (ERROR_OK != retval) break; } } - if (ERROR_OK != retval) - { + if (ERROR_OK != retval) { for (unsigned j = 0; j < i; j++) unregister_command(cmd_ctx, parent, cmds[j].name); } @@ -474,14 +454,13 @@ int register_commands(struct command_context *cmd_ctx, struct command *parent, } int unregister_all_commands(struct command_context *context, - struct command *parent) + struct command *parent) { if (context == NULL) return ERROR_OK; struct command **head = command_list_for_parent(context, parent); - while (NULL != *head) - { + while (NULL != *head) { struct command *tmp = *head; *head = tmp->next; command_free(tmp); @@ -491,15 +470,14 @@ int unregister_all_commands(struct command_context *context, } int unregister_command(struct command_context *context, - struct command *parent, const char *name) + struct command *parent, const char *name) { if ((!context) || (!name)) return ERROR_COMMAND_SYNTAX_ERROR; struct command *p = NULL; struct command **head = command_list_for_parent(context, parent); - for (struct command *c = *head; NULL != c; p = c, c = c->next) - { + for (struct command *c = *head; NULL != c; p = c, c = c->next) { if (strcmp(name, c->name) != 0) continue; @@ -525,9 +503,8 @@ void command_set_handler_data(struct command *c, void *p) void command_output_text(struct command_context *context, const char *data) { - if (context && context->output_handler && data) { + if (context && context->output_handler && data) context->output_handler(context, data); - } } void command_print_sameline(struct command_context *context, const char *format, ...) @@ -538,16 +515,15 @@ void command_print_sameline(struct command_context *context, const char *format, va_start(ap, format); string = alloc_vprintf(format, ap); - if (string != NULL) - { + if (string != NULL) { /* we want this collected in the log + we also want to pick it up as a tcl return * value. * * The latter bit isn't precisely neat, but will do for now. */ LOG_USER_N("%s", string); - /* We already printed it above */ - /* command_output_text(context, string); */ + /* We already printed it above + * command_output_text(context, string); */ free(string); } @@ -562,17 +538,17 @@ void command_print(struct command_context *context, const char *format, ...) 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 */ /* we want this collected in the log + we also want to pick it up as a tcl return * value. * * The latter bit isn't precisely neat, but will do for now. */ LOG_USER_N("%s", string); - /* We already printed it above */ - /* command_output_text(context, string); */ + /* We already printed it above + * command_output_text(context, string); */ free(string); } @@ -584,11 +560,11 @@ static char *__command_name(struct command *c, char delim, unsigned extra) char *name; unsigned len = strlen(c->name); if (NULL == c->parent) { - // allocate enough for the name, child names, and '\0' + /* allocate enough for the name, child names, and '\0' */ name = malloc(len + extra + 1); strcpy(name, c->name); } else { - // parent's extra must include both the space and name + /* parent's extra must include both the space and name */ name = __command_name(c->parent, delim, 1 + len + extra); char dstr[2] = { delim, 0 }; strcat(name, dstr); @@ -607,33 +583,37 @@ static bool command_can_run(struct command_context *cmd_ctx, struct command *c) } static int run_command(struct command_context *context, - struct command *c, const char *words[], unsigned num_words) + struct command *c, const char *words[], unsigned num_words) { - if (!command_can_run(context, c)) - { + if (!command_can_run(context, c)) { /* Many commands may be run only before/after 'init' */ const char *when; switch (c->mode) { - case COMMAND_CONFIG: when = "before"; break; - case COMMAND_EXEC: when = "after"; break; - // handle the impossible with humor; it guarantees a bug report! - default: when = "if Cthulhu is summoned by"; break; + case COMMAND_CONFIG: + when = "before"; + break; + case COMMAND_EXEC: + when = "after"; + break; + /* handle the impossible with humor; it guarantees a bug report! */ + default: + when = "if Cthulhu is summoned by"; + break; } LOG_ERROR("The '%s' command must be used %s 'init'.", - c->name, when); + c->name, when); return ERROR_FAIL; } struct command_invocation cmd = { - .ctx = context, - .current = c, - .name = c->name, - .argc = num_words - 1, |