diff options
Diffstat (limited to 'src/lib/libast/string')
87 files changed, 9797 insertions, 0 deletions
diff --git a/src/lib/libast/string/base64.c b/src/lib/libast/string/base64.c new file mode 100644 index 0000000..fc07986 --- /dev/null +++ b/src/lib/libast/string/base64.c @@ -0,0 +1,312 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * mime base64 encode/decode + * + * Glenn Fowler + * David Korn + * AT&T Research + */ + +#include <ast.h> + +#define PAD '=' + +#define B64_UC 3 +#define B64_EC 4 +#define B64_CHUNK 15 +#define B64_PAD 64 +#define B64_SPC 65 +#define B64_IGN 66 + +static unsigned char map[UCHAR_MAX+1]; + +static const char alp[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/* + * mime base64 encode + */ + +ssize_t +base64encode(const void* fb, size_t fz, void** fn, void* tb, size_t tz, void** tn) +{ + register unsigned char* fp; + register unsigned char* tp; + register unsigned char* fe; + register unsigned char* te; + register unsigned char* tc; + register unsigned char* m; + register unsigned long b; + size_t n; + unsigned char tmp[B64_EC * B64_CHUNK]; + + m = (unsigned char*)alp; + fp = fe = (unsigned char*)fb; + if (fz >= 3) + { + n = fz % 3; + fe += fz - n; + fz = n; + } + if (tp = (unsigned char*)tb) + { + te = tp + tz - B64_EC + 1; + n = 0; + } + else + { + if (fn) + *fn = fp; + if (tn) + *tn = 0; + tp = tmp; + te = tp + sizeof(tmp) - B64_EC + 1; + n = 1; + } + for (;;) + { + tc = tp + B64_EC * B64_CHUNK; + do + { + if (fp >= fe) + goto done; + if (tp >= te) + { + if (fn) + *fn = fp; + if (tn) + *tn = tp; + n = tp - (unsigned char*)tb + 1; + tp = tmp; + te = tp + sizeof(tmp) - B64_EC + 1; + } + b = *fp++ << 16; + b |= *fp++ << 8; + b |= *fp++; + *tp++ = m[b >> 18]; + *tp++ = m[(b >> 12) & 077]; + *tp++ = m[(b >> 6) & 077]; + *tp++ = m[b & 077]; + } while (tp < tc); + if (n) + { + n += tp - tmp + (fp < fe); + tp = tmp; + } + else + *tp++ = '\n'; + } + done: + if (fz) + { + if (tp >= te) + { + if (fn) + *fn = fp; + if (tn) + *tn = tp; + n = tp - (unsigned char*)tb + 1; + tp = tmp; + te = tp + sizeof(tmp) - B64_EC + 1; + } + b = *fp++ << 16; + if (fz == 2) + b |= *fp++ << 8; + *tp++ = m[b >> 18]; + *tp++ = m[(b >> 12) & 077]; + *tp++ = (fz == 2) ? m[(b >> 6) & 077] : PAD; + *tp++ = PAD; + } + if (n) + n += (tp - tmp) - 1; + else + { + if (tp > (unsigned char*)tb && *(tp - 1) == '\n') + tp--; + if (tp < te) + *tp = 0; + n = tp - (unsigned char*)tb; + if (tn) + *tn = tp; + if (fn) + *fn = fp; + } + return n; +} + +/* + * mime base64 decode + */ + +ssize_t +base64decode(const void* fb, size_t fz, void** fn, void* tb, size_t tz, void** tn) +{ + register unsigned char* fp; + register unsigned char* tp; + register unsigned char* fe; + register unsigned char* te; + register unsigned char* tx; + register unsigned char* m; + register int c; + register int state; + register unsigned long v; + unsigned char* fc; + ssize_t n; + + if (!(m = map)[0]) + { + memset(m, B64_IGN, sizeof(map)); + for (tp = (unsigned char*)alp; c = *tp; tp++) + m[c] = tp - (unsigned char*)alp; + m[PAD] = B64_PAD; + m[' '] = m['\t'] = m['\n'] = B64_SPC; + } + fp = (unsigned char*)fb; + fe = fp + fz; + if (tp = (unsigned char*)tb) + { + te = tp + tz; + if (tz > 2) + tz = 2; + tx = te - tz; + n = 0; + } + else + { + te = tx = tp; + n = 1; + } + for (;;) + { + fc = fp; + state = 0; + v = 0; + while (fp < fe) + { + if ((c = m[*fp++]) < 64) + { + v = (v << 6) | c; + if (++state == 4) + { + if (tp >= tx) + { + if (n) + n += 3; + else + { + n = tp - (unsigned char*)tb + 4; + if (tp < te) + { + *tp++ = (v >> 16); + if (tp < te) + { + *tp++ = (v >> 8); + if (tp < te) + *tp++ = (v); + } + } + if (tn) + *tn = tp; + if (fn) + *fn = fc; + } + } + else + { + *tp++ = (v >> 16); + *tp++ = (v >> 8); + *tp++ = (v); + } + fc = fp; + state = 0; + v = 0; + } + } + else if (c == B64_PAD) + break; + } + switch (state) + { + case 0: + goto done; + case 2: + if (tp < te) + *tp++ = v >> 4; + else if (n) + n++; + else + { + n = tp - (unsigned char*)tb + 2; + if (tn) + *tn = tp; + if (fn) + *fn = fc; + } + break; + case 3: + if (tp < te) + { + *tp++ = v >> 10; + if (tp < te) + *tp++ = v >> 2; + else + { + n = tp - (unsigned char*)tb + 2; + if (tn) + *tn = tp; + if (fn) + *fn = fc; + } + } + else if (n) + n += 2; + else + { + n = tp - (unsigned char*)tb + 3; + if (tn) + *tn = tp; + if (fn) + *fn = fc; + } + break; + } + while (fp < fe && ((c = m[*fp++]) == B64_PAD || c == B64_SPC)); + if (fp >= fe || c >= 64) + break; + fp--; + } + done: + if (n) + n--; + else + { + if (tp < te) + *tp = 0; + n = tp - (unsigned char*)tb; + if (fn) + *fn = fp; + if (tn) + *tn = tp; + } + return n; +} diff --git a/src/lib/libast/string/ccmap.c b/src/lib/libast/string/ccmap.c new file mode 100644 index 0000000..a42ccb6 --- /dev/null +++ b/src/lib/libast/string/ccmap.c @@ -0,0 +1,746 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * Glenn Fowler + * AT&T Research + * + * dynamic single byte character code maps + */ + +#include <ast.h> +#include <ccode.h> + +#if !GENERATE + +/* + * catenated CC_ASCII to CC_* character code map table + * order determined by CC_* index definitions + * old definition values/indices must not change! + * each table is followed by its inverse + */ + +static const unsigned char tab[] = +{ + + /* ascii => ascii */ + + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, + + /* ascii => ascii */ + + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, + + /* ascii => ebcdic */ + + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, + 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, + 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, + 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, + 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x9a, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, + 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0x5f, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, + 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, + 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, + 0x8e, 0x8f, 0x90, 0x6a, 0x9b, 0x9c, 0x9d, 0x9e, + 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0x4a, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xa1, 0xbe, 0xbf, + 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb, + 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, + 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, + + /* ebcdic => ascii */ + + 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, + 0x97, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x9d, 0x85, 0x08, 0x87, + 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x0a, 0x17, 0x1b, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, + 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, + 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, + 0x20, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, + 0xa7, 0xa8, 0xd5, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, + 0x26, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x7e, + 0x2d, 0x2f, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xcb, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, + 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, + 0xc2, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, + 0xc3, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, + 0xca, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, + 0x71, 0x72, 0x5e, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, + 0xd1, 0xe5, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0xd2, 0xd3, 0xd4, 0x5b, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0x5d, 0xe6, 0xe7, + 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, + 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, + 0x51, 0x52, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, + 0x5c, 0x9f, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, + 0x59, 0x5a, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, + + /* ascii => ebcdic-i */ + + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, + 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, + 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, + 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, + 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, + 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, + 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, + 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, + 0x8e, 0x8f, 0x90, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, + 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb, + 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, + 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, + + /* ebcdic-i => ascii */ + + 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, + 0x97, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x9d, 0x85, 0x08, 0x87, + 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x0a, 0x17, 0x1b, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, + 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, + 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, + 0x20, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, + 0xa7, 0xa8, 0x4a, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, + 0x26, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, + 0x2d, 0x2f, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0x6a, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, + 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, + 0xc2, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, + 0xc3, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, + 0xca, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, + 0x71, 0x72, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, + 0xd1, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0xd2, 0xd3, 0xd4, 0x5b, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0x5d, 0xe6, 0xe7, + 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, + 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, + 0x51, 0x52, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, + 0x5c, 0x9f, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, + 0x59, 0x5a, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, + + /* ascii => ebcdic-o */ + + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, + 0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, + 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, + 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, + 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, + 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x06, 0x17, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, + 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xff, + 0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, + 0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, + 0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, + 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, + 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, + 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, + 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, + 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, + 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, + 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, + 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, + 0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf, + + /* ebcdic-o => ascii */ + + 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, + 0x97, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x9d, 0x0a, 0x08, 0x87, + 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x17, 0x1b, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, + 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, + 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, + 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, + 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, + 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, + 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, + 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, + 0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, + 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, + 0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, + 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, + 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, + 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, + 0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, + 0xac, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, + 0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7, + 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, + 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, + 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, + 0x5c, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, + 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0x9f, + + /* ascii => ebcdic-s */ + + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, + 0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, + 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, + 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, + 0xe7, 0xe8, 0xe9, 0xbb, 0xbc, 0xbd, 0x6a, 0x6d, + 0x4a, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, + 0xa7, 0xa8, 0xa9, 0xfb, 0x4f, 0xfd, 0xff, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x06, 0x17, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, + 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0x5f, + 0x41, 0xaa, 0xb0, 0xb1, 0x9f, 0xb2, 0xd0, 0xb5, + 0x79, 0xb4, 0x9a, 0x8a, 0xba, 0xca, 0xaf, 0xa1, + 0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, + 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, + 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, + 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, + 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, + 0x80, 0xe0, 0xfe, 0xdd, 0xfc, 0xad, 0xae, 0x59, + 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, + 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, + 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, + 0x70, 0xc0, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf, + + /* ebcdic-s => ascii */ + + 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, + 0x97, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x9d, 0x0a, 0x08, 0x87, + 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x17, 0x1b, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, + 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, + 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, + 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, + 0xe7, 0xf1, 0x60, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, + 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, + 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x9f, + 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, + 0xc7, 0xd1, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, + 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, + 0xcc, 0xa8, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, + 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, + 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, + 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, + 0xb5, 0xaf, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae, + 0xa2, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, + 0xbd, 0xbe, 0xac, 0x5b, 0x5c, 0x5d, 0xb4, 0xd7, + 0xf9, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, + 0xa6, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, + 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xdb, 0xfa, 0xff, + 0xd9, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, + 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0xb3, 0x7b, 0xdc, 0x7d, 0xda, 0x7e, + + /* ascii => ebcdic-h */ + + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, + 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, + 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, + 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, + 0xe7, 0xe8, 0xe9, 0xba, 0xe0, 0xbb, 0xb0, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, + 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, + 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xff, + 0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, + 0xbd, 0xb4, 0x9a, 0x8a, 0x5f, 0xca, 0xaf, 0xbc, + 0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, + 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, + 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, + 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, + 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, + 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xad, 0xae, 0x59, + 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, + 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, + 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, + 0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf, + + /* ebcdic-h => ascii */ + + 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, + 0x97, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x9d, 0x85, 0x08, 0x87, + 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x0a, 0x17, 0x1b, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, + 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, + 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, + 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, + 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, + 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, + 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0xac, + 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, + 0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, + 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, + 0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, + 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, + 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, + 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, + 0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae, + 0x5e, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, + 0xbd, 0xbe, 0x5b, 0x5d, 0xaf, 0xa8, 0xb4, 0xd7, + 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, + 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, + 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, + 0x5c, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, + 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0x9f, + + /* ascii => ebcdic-m */ + + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, + 0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, + 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, + 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, + 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, + 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x06, 0x17, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, + 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xdf, + 0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, + 0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, + 0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, + 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, + 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, + 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, + 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, + 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, + 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, + 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, + 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, + 0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xff, + + /* ebcdic-m => ascii */ + + 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, + 0x97, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x9d, 0x0a, 0x08, 0x87, + 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x17, 0x1b, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, + 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, + 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, + 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, + 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, + 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, + 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, + 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, + 0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, + 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, + 0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, + 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, + 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, + 0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, + 0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, + 0xac, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, + 0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7, + 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, + 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, + 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0x9f, + 0x5c, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, + 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0xff, + + /* ascii => ebcdic-u */ + + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, + 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x9f, + 0x10, 0x11, 0x12, 0x13, 0xb6, 0xb5, 0x32, 0x26, + 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, + 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, + 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, + 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, + 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, + 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, + 0x68, 0xdc, 0x51, 0x42, 0x43, 0x44, 0x47, 0x48, + 0x52, 0x53, 0x54, 0x57, 0x56, 0x58, 0x63, 0x67, + 0x71, 0x9c, 0x9e, 0xcb, 0xcc, 0xcd, 0xdb, 0xdd, + 0xdf, 0xec, 0xfc, 0xb0, 0xb1, 0xb2, 0x3e, 0xb4, + 0x45, 0x55, 0xce, 0xde, 0x49, 0x69, 0x9a, 0x9b, + 0xab, 0x0f, 0xba, 0xb8, 0xb7, 0xaa, 0x8a, 0x8b, + 0x3c, 0x3d, 0x62, 0x6a, 0x64, 0x65, 0x66, 0x20, + 0x21, 0x22, 0x70, 0x23, 0x72, 0x73, 0x74, 0xbe, + 0x76, 0x77, 0x78, 0x80, 0x24, 0x15, 0x8c, 0x8d, + 0x8e, 0x41, 0x06, 0x17, 0x28, 0x29, 0x9d, 0x2a, + 0x2b, 0x2c, 0x09, 0x0a, 0xac, 0x4a, 0xae, 0xaf, + 0x1b, 0x30, 0x31, 0xfa, 0x1a, 0x33, 0x34, 0x35, + 0x36, 0x59, 0x08, 0x38, 0xbc, 0x39, 0xa0, 0xbf, + 0xca, 0x3a, 0xfe, 0x3b, 0x04, 0xcf, 0xda, 0x14, + 0xe1, 0x8f, 0x46, 0x75, 0xfd, 0xeb, 0xee, 0xed, + 0x90, 0xef, 0xb3, 0xfb, 0xb9, 0xea, 0xbb, 0xff, + + /* ebcdic-u => ascii */ + + 0x00, 0x01, 0x02, 0x03, 0xec, 0x09, 0xca, 0x7f, + 0xe2, 0xd2, 0xd3, 0x0b, 0x0c, 0x0d, 0x0e, 0xa9, + 0x10, 0x11, 0x12, 0x13, 0xef, 0xc5, 0x08, 0xcb, + 0x18, 0x19, 0xdc, 0xd8, 0x1c, 0x1d, 0x1e, 0x1f, + 0xb7, 0xb8, 0xb9, 0xbb, 0xc4, 0x0a, 0x17, 0x1b, + 0xcc, 0xcd, 0xcf, 0xd0, 0xd1, 0x05, 0x06, 0x07, + 0xd9, 0xda, 0x16, 0xdd, 0xde, 0xdf, 0xe0, 0x04, + 0xe3, 0xe5, 0xe9, 0xeb, 0xb0, 0xb1, 0x9e, 0x1a, + 0x20, 0xc9, 0x83, 0x84, 0x85, 0xa0, 0xf2, 0x86, + 0x87, 0xa4, 0xd5, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, + 0x26, 0x82, 0x88, 0x89, 0x8a, 0xa1, 0x8c, 0x8b, + 0x8d, 0xe1, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, + 0x2d, 0x2f, 0xb2, 0x8e, 0xb4, 0xb5, 0xb6, 0x8f, + 0x80, 0xa5, 0xb3, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, + 0xba, 0x90, 0xbc, 0xbd, 0xbe, 0xf3, 0xc0, 0xc1, + 0xc2, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, + 0xc3, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0xae, 0xaf, 0xc6, 0xc7, 0xc8, 0xf1, + 0xf8, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, + 0x71, 0x72, 0xa6, 0xa7, 0x91, 0xce, 0x92, 0x0f, + 0xe6, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0xad, 0xa8, 0xd4, 0x5b, 0xd6, 0xd7, + 0x9b, 0x9c, 0x9d, 0xfa, 0x9f, 0x15, 0x14, 0xac, + 0xab, 0xfc, 0xaa, 0xfe, 0xe4, 0x5d, 0xbf, 0xe7, + 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0xe8, 0x93, 0x94, 0x95, 0xa2, 0xed, + 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, + 0x51, 0x52, 0xee, 0x96, 0x81, 0x97, 0xa3, 0x98, + 0x5c, 0xf0, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, + 0x59, 0x5a, 0xfd, 0xf5, 0x99, 0xf7, 0xf6, 0xf9, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0xdb, 0xfb, 0x9a, 0xf4, 0xea, 0xff, + +}; + +#define MAP 256 +#define MAPS (sizeof(tab)/MAP) + +struct Map_s; typedef struct Map_s Map_t; + +struct Map_s +{ + Map_t* next; + int op; + unsigned char map[MAP]; +}; + +static Map_t* maps; + +/* + * generate and return ccode map from i to o + */ + +unsigned char* +_ccmap(int i, int o) +{ + register unsigned char* a; + register unsigned char* m; + register unsigned char* z; + register Map_t* map; + register int n; + int op; + + if (!i && !o) + return (unsigned char*)tab; + if (CCCONVERT(i)) + { + o = CCOUT(i); + i = CCIN(i); + } + else if (CCCONVERT(o)) + { + i = CCIN(o); + o = CCOUT(o); + } + else + { + if (i == 0) + i = CC_NATIVE; + if (o == 0) + o = CC_NATIVE; + } + if (i == o || i < 1 || i > MAPS || o < 1 || o > MAPS) + return 0; + if (i == CC_ASCII) + return (unsigned char*)tab + MAP * (2 * (o - 1)); + if (o == CC_ASCII) + return (unsigned char*)tab + MAP * (2 * (i - 1) + 1); + op = CCOP(i, o); + for (map = maps; map; map = map->next) + if (map->op == op) + return map->map; + if (!(map = newof(0, Map_t, 1, 0))) + return 0; + map->op = op; + a = (unsigned char*)tab + MAP * (2 * (o - 1)); + z = (unsigned char*)tab + MAP * (2 * (i - 1) + 1); + m = map->map; + for (n = 0; n < MAP; n++) + m[n] = n; + for (n = MAP - 1; n >= 0; n--) + m[n] = a[z[n]]; + map->next = maps; + maps = map; + return m; +} + +void* +_ccmapcpy(register unsigned char* m, void* b, const void* a, size_t n) +{ + register unsigned char* ub; + register unsigned char* ue; + register unsigned char* ua; + + if (m) + { + ub = (unsigned char*)b; + ue = ub + n; + ua = (unsigned char*)a; + while (ub < ue) + *ub++ = m[*ua++]; + } + else + memcpy(b, a, n); + return b; +} + +void* +_ccmapstr(register unsigned char* m, void* b, size_t n) +{ + register unsigned char* s; + register unsigned char* e; + + if (m) + for (e = (s = (unsigned char*)b) + n; s < e; s++) + *s = m[*s]; + return b; +} + +#else + +static void +dump(int from, int to) +{ + register const unsigned char* m; + register int c; + register int i; + + m = ccmap(from, to); + sfprintf(sfstdout, "\n /* %s => %s */\n\n", ccmapname(from), ccmapname(to)); + for (c = i = 0; c <= UCHAR_MAX; c++) + { + sfprintf(sfstdout, " 0x%02x,", m ? m[c] : c); + if (!(++i & 0x7)) + { + i = 0; + sfprintf(sfstdout, "\n"); + } + } +} + +main(int argc, char** argv) +{ + register int i; + + for (i = 1; i <= CC_MAPS; i++) + { + dump(CC_ASCII, i); + dump(i, CC_ASCII); + } + return 0; +} + +#endif diff --git a/src/lib/libast/string/ccmapid.c b/src/lib/libast/string/ccmapid.c new file mode 100644 index 0000000..f18d46e --- /dev/null +++ b/src/lib/libast/string/ccmapid.c @@ -0,0 +1,173 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * Glenn Fowler + * AT&T Research + * + * 8 bit character code map name/id lookup support + */ + +#include <ast.h> +#include <ccode.h> +#include <ctype.h> + +static const Ccmap_t maps[] = +{ + { + "ascii", + "a|ascii|?(iso)?(-)646|?(iso)?(-)8859|latin", + "8 bit ascii", + "ISO-8859-%s", + "1", + CC_ASCII, + }, + + { + "ebcdic", + "e|ebcdic?(-)?([1e])", + "X/Open ebcdic", + "EBCDIC", + 0, + CC_EBCDIC_E, + }, + + { + "ebcdic-o", + "o|ebcdic?(-)[3o]|?(cp|ibm)1047|open?(-)edition", + "mvs OpenEdition ebcdic", + "EBCDIC-O", + 0, + CC_EBCDIC_O, + }, + + { + "ebcdic-h", + "h|ebcdic?(-)h|?(cp|ibm)?(00)37|[oa]s?(/-)400", + "ibm OS/400 AS/400 ebcdic", + "EBCDIC-H", + 0, + CC_EBCDIC_H, + }, + + { + "ebcdic-s", + "s|ebcdic?(-)s|siemens|posix-bc", + "siemens posix-bc ebcdic", + "EBCDIC-S", + 0, + CC_EBCDIC_S, + }, + + { + "ebcdic-i", + "i|ebcdic?(-)[2i]|ibm", + "X/Open ibm ebcdic (not idempotent)", + "EBCDIC-I", + 0, + CC_EBCDIC_I, + }, + + { + "ebcdic-m", + "m|ebcdic?(-)m|mvs", + "mvs ebcdic", + "EBCDIC-M", + 0, + CC_EBCDIC_M, + }, + + { + "ebcdic-u", + "u|ebcdic?(-)(u|mf)|microfocus", + "microfocus cobol ebcdic", + "EBCDIC-U", + 0, + CC_EBCDIC_U, + }, + + { + "native", + "n|native|local", + "native code set", + 0, + 0, + CC_NATIVE, + }, + + { 0 }, +}; + +/* + * ccode map list iterator + */ + +Ccmap_t* +ccmaplist(Ccmap_t* mp) +{ + return !mp ? (Ccmap_t*)maps : (++mp)->name ? mp : (Ccmap_t*)0; +} + +/* + * return ccode map id given name + */ + +int +ccmapid(const char* name) +{ + register const Ccmap_t* mp; + register int c; + const Ccmap_t* bp; + int n; + int sub[2]; + + bp = 0; + n = 0; + for (mp = maps; mp->name; mp++) + if (strgrpmatch(name, mp->match, sub, elementsof(sub) / 2, STR_MAXIMAL|STR_LEFT|STR_ICASE)) + { + if (!(c = name[sub[1]])) + return mp->ccode; + if (sub[1] > n && !isalpha(c)) + { + n = sub[1]; + bp = mp; + } + } + return bp ? bp->ccode : -1; +} + +/* + * return ccode map name given id + */ + +char* +ccmapname(register int id) +{ + register const Ccmap_t* mp; + + for (mp = maps; mp->name; mp++) + if (id == mp->ccode) + return (char*)mp->name; + return 0; +} diff --git a/src/lib/libast/string/ccnative.c b/src/lib/libast/string/ccnative.c new file mode 100644 index 0000000..500f165 --- /dev/null +++ b/src/lib/libast/string/ccnative.c @@ -0,0 +1,56 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * Glenn Fowler + * AT&T Research + * + * copy table with element size n + * indexed by CC_ASCII to table + * indexed by CC_NATIVE + */ + +#include <ast.h> +#include <ccode.h> + +void* +ccnative(void* b, const void* a, size_t n) +{ +#if CC_ASCII == CC_NATIVE + return memcpy(b, a, n * (UCHAR_MAX + 1)); +#else + register int c; + register const unsigned char* m; + register unsigned char* cb = (unsigned char*)b; + register unsigned char* ca = (unsigned char*)a; + + m = CCMAP(CC_ASCII, CC_NATIVE); + if (n == sizeof(char)) + for (c = 0; c <= UCHAR_MAX; c++) + cb[c] = ca[m[c]]; + else + for (c = 0; c <= UCHAR_MAX; c++) + memcpy(cb + n * c, ca + n * m[c], n); + return b; +#endif +} diff --git a/src/lib/libast/string/chresc.c b/src/lib/libast/string/chresc.c new file mode 100644 index 0000000..9dd3704 --- /dev/null +++ b/src/lib/libast/string/chresc.c @@ -0,0 +1,235 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * return the next character in the string s + * \ character constants are expanded + * *p is updated to point to the next character in s + * *m is 1 if return value is wide + */ + +#include <ast.h> +#include <ctype.h> + +#include <ccode.h> +#if !_PACKAGE_astsa +#include <regex.h> +#endif + +int +chrexp(register const char* s, char** p, int* m, register int flags) +{ + register const char* q; + register int c; + const char* e; + const char* b; + char* r; + int n; + int w; + + w = 0; + for (;;) + { + b = s; + switch (c = mbchar(s)) + { + case 0: + s--; + break; + case '\\': + switch (c = *s++) + { + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + if (!(flags & FMT_EXP_CHAR)) + goto noexpand; + c -= '0'; + q = s + 2; + while (s < q) + switch (*s) + { + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + c = (c << 3) + *s++ - '0'; + break; + default: + q = s; + break; + } + break; + case 'a': + if (!(flags & FMT_EXP_CHAR)) + goto noexpand; + c = CC_bel; + break; + case 'b': + if (!(flags & FMT_EXP_CHAR)) + goto noexpand; + c = '\b'; + break; + case 'c': /*DEPRECATED*/ + case 'C': + if (!(flags & FMT_EXP_CHAR)) + goto noexpand; + if (c = *s) + { + s++; + if (c == '\\') + { + c = chrexp(s - 1, &r, 0, flags); + s = (const char*)r; + } + if (islower(c)) + c = toupper(c); + c = ccmapc(c, CC_NATIVE, CC_ASCII); + c ^= 0x40; + c = ccmapc(c, CC_ASCII, CC_NATIVE); + } + break; + case 'e': /*DEPRECATED*/ + case 'E': + if (!(flags & FMT_EXP_CHAR)) + goto noexpand; + c = CC_esc; + break; + case 'f': + if (!(flags & FMT_EXP_CHAR)) + goto noexpand; + c = '\f'; + break; + case 'M': + if (!(flags & FMT_EXP_CHAR)) + goto noexpand; + if (*s == '-') + { + s++; + c = CC_esc; + } + break; + case 'n': + if (flags & FMT_EXP_NONL) + continue; + if (!(flags & FMT_EXP_LINE)) + goto noexpand; + c = '\n'; + break; + case 'r': + if (flags & FMT_EXP_NOCR) + continue; + if (!(flags & FMT_EXP_LINE)) + goto noexpand; + c = '\r'; + break; + case 't': + if (!(flags & FMT_EXP_CHAR)) + goto noexpand; + c = '\t'; + break; + case 'v': + if (!(flags & FMT_EXP_CHAR)) + goto noexpand; + c = CC_vt; + break; + case 'u': + case 'U': + case 'x': + if (q = c == 'u' ? (s + 4) : c == 'U' ? (s + 8) : (char*)0) + { + if (!(flags & FMT_EXP_WIDE)) + goto noexpand; + w = 1; + } + b = e = s; + n = 0; + c = 0; + while (!e || !q || s < q) + { + switch (*s) + { + case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': + c = (c << 4) + *s++ - 'a' + 10; + n++; + continue; + case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': + c = (c << 4) + *s++ - 'A' + 10; + n++; + continue; + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + c = (c << 4) + *s++ - '0'; + n++; + continue; + case '{': + case '[': + if (s != e) + break; + e = 0; + s++; + continue; + case '}': + case ']': + if (!e) + s++; + break; + default: + break; + } + break; + } + if (n <= 2 && !(flags & FMT_EXP_CHAR) || n > 2 && (w = 1) && !(flags & FMT_EXP_WIDE)) + { + c = '\\'; + s = b; + } + break; + case 0: + s--; + break; + } + break; + default: + if ((s - b) > 1) + w = 1; + break; + } + break; + } + normal: + if (p) + *p = (char*)s; + if (m) + *m = w; + return c; + noexpand: + c = '\\'; + s--; + goto normal; +} + +int +chresc(register const char* s, char** p) +{ + return chrexp(s, p, NiL, FMT_EXP_CHAR|FMT_EXP_LINE|FMT_EXP_WIDE); +} diff --git a/src/lib/libast/string/chrtoi.c b/src/lib/libast/string/chrtoi.c new file mode 100644 index 0000000..2d8a3e1 --- /dev/null +++ b/src/lib/libast/string/chrtoi.c @@ -0,0 +1,55 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * convert a 0 terminated character constant string to an int + */ + +#include <ast.h> + +int +chrtoi(register const char* s) +{ + register int c; + register int n; + register int x; + char* p; + + c = 0; + for (n = 0; n < sizeof(int) * CHAR_BIT; n += CHAR_BIT) + { + switch (x = *((unsigned char*)s++)) + { + case '\\': + x = chresc(s - 1, &p); + s = (const char*)p; + break; + case 0: + return(c); + } + c = (c << CHAR_BIT) | x; + } + return(c); +} diff --git a/src/lib/libast/string/fmtbase.c b/src/lib/libast/string/fmtbase.c new file mode 100644 index 0000000..a7262eb --- /dev/null +++ b/src/lib/libast/string/fmtbase.c @@ -0,0 +1,70 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * return base b representation for n + * if p!=0 then base prefix is included + * otherwise if n==0 or b==0 then output is signed base 10 + */ + +#include <ast.h> + +char* +fmtbase(intmax_t n, int b, int p) +{ + char* buf; + int z; + + if (!p) + { + if (!n) + return "0"; + if (!b) + return fmtint(n, 0); + if (b == 10) + return fmtint(n, 1); + } + buf = fmtbuf(z = 72); + sfsprintf(buf, z, p ? "%#..*I*u" : "%..*I*u", b, sizeof(n), n); + return buf; +} + +#if __OBSOLETE__ < 20140101 + +#undef fmtbasell + +#if defined(__EXPORT__) +#define extern __EXPORT__ +#endif + +extern char* +fmtbasell(intmax_t n, int b, int p) +{ + return fmtbase(n, b, p); +} + +#undef extern + +#endif diff --git a/src/lib/libast/string/fmtbuf.c b/src/lib/libast/string/fmtbuf.c new file mode 100644 index 0000000..bc96af6 --- /dev/null +++ b/src/lib/libast/string/fmtbuf.c @@ -0,0 +1,69 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> + +/* + * return small format buffer chunk of size n + * spin lock for thread access + * format buffers are short lived + * only one concurrent buffer with size > sizeof(buf) + */ + +static char buf[16 * 1024]; +static char* nxt = buf; +static int lck = -1; + +static char* big; +static size_t bigsiz; + +char* +fmtbuf(size_t n) +{ + register char* cur; + + while (++lck) + lck--; + if (n > (&buf[elementsof(buf)] - nxt)) + { + if (n > elementsof(buf)) + { + if (n > bigsiz) + { + bigsiz = roundof(n, 8 * 1024); + if (!(big = newof(big, char, bigsiz, 0))) + { + lck--; + return 0; + } + } + lck--; + return big; + } + nxt = buf; + } + cur = nxt; + nxt += n; + lck--; + return cur; +} diff --git a/src/lib/libast/string/fmtclock.c b/src/lib/libast/string/fmtclock.c new file mode 100644 index 0000000..6e54cfa --- /dev/null +++ b/src/lib/libast/string/fmtclock.c @@ -0,0 +1,63 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * return pointer to formatted clock() tics t + * return value length is at most 6 + */ + +#include <ast.h> +#include <tm.h> + +char* +fmtclock(register Sfulong_t t) +{ + register int u; + char* buf; + int z; + + static unsigned int clk_tck; + + if (!clk_tck) + { +#ifdef CLOCKS_PER_SEC + clk_tck = CLOCKS_PER_SEC; +#else + if (!(clk_tck = (unsigned int)strtoul(astconf("CLK_TCK", NiL, NiL), NiL, 10))) + clk_tck = 60; +#endif + } + if (t == 0) + return "0"; + if (t == ((Sfulong_t)~0)) + return "%"; + t = (t * 1000000) / clk_tck; + if (t < 1000) + u = 'u'; + else if ((t /= 1000) < 1000) + u = 'm'; + else + return fmtelapsed(t / 10, 100); + buf = fmtbuf(z = 7); + sfsprintf(buf, z, "%I*u%cs", sizeof(t), t, u); + return buf; +} diff --git a/src/lib/libast/string/fmtdev.c b/src/lib/libast/string/fmtdev.c new file mode 100644 index 0000000..9f20f5f --- /dev/null +++ b/src/lib/libast/string/fmtdev.c @@ -0,0 +1,60 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * st_dev formatter + */ + +#include <ast.h> +#include <ctype.h> +#include <ls.h> + +char* +fmtdev(struct stat* st) +{ + char* buf; + unsigned long mm; + unsigned int ma; + unsigned int mi; + int z; + + mm = (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) ? idevice(st) : st->st_dev; + ma = major(mm); + mi = minor(mm); + buf = fmtbuf(z = 17); + if (ma == '#' && isalnum(mi)) + { + /* + * Plan? Nein! + */ + + buf[0] = ma; + buf[1] = mi; + buf[2] = 0; + } + else + sfsprintf(buf, z, "%03d,%03d", ma, mi); + return buf; +} diff --git a/src/lib/libast/string/fmtelapsed.c b/src/lib/libast/string/fmtelapsed.c new file mode 100644 index 0000000..a1c5779 --- /dev/null +++ b/src/lib/libast/string/fmtelapsed.c @@ -0,0 +1,61 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * return pointer to formatted elapsed time for u 1/n secs + * compatible with strelapsed() + * return value length is at most 7 + */ + +#include <ast.h> + +char* +fmtelapsed(register unsigned long u, register int n) +{ + register unsigned long t; + char* buf; + int z; + + if (u == 0L) + return "0"; + if (u == ~0L) + return "%"; + buf = fmtbuf(z = 8); + t = u / n; + if (t < 60) + sfsprintf(buf, z, "%lu.%02lus", t, (u * 100 / n) % 100); + else if (t < 60*60) + sfsprintf(buf, z, "%lum%02lus", t / 60, t - (t / 60) * 60); + else if (t < 24*60*60) + sfsprintf(buf, z, "%luh%02lum", t / (60*60), (t - (t / (60*60)) * (60*60)) / 60); + else if (t < 7*24*60*60) + sfsprintf(buf, z, "%lud%02luh", t / (24*60*60), (t - (t / (24*60*60)) * (24*60*60)) / (60*60)); + else if (t < 31*24*60*60) + sfsprintf(buf, z, "%luw%02lud", t / (7*24*60*60), (t - (t / (7*24*60*60)) * (7*24*60*60)) / (24*60*60)); + else if (t < 365*24*60*60) + sfsprintf(buf, z, "%luM%02lud", (t * 12) / (365*24*60*60), ((t * 12) - ((t * 12) / (365*24*60*60)) * (365*24*60*60)) / (12*24*60*60)); + else if (t < (365UL*4UL+1UL)*24UL*60UL*60UL) + sfsprintf(buf, z, "%luY%02luM", t / (365*24*60*60), ((t - (t / (365*24*60*60)) * (365*24*60*60)) * 5) / (152 * 24 * 60 * 60)); + else + sfsprintf(buf, z, "%luY%02luM", (t * 4) / ((365UL*4UL+1UL)*24UL*60UL*60UL), (((t * 4) - ((t * 4) / ((365UL*4UL+1UL)*24UL*60UL*60UL)) * ((365UL*4UL+1UL)*24UL*60UL*60UL)) * 5) / ((4 * 152 + 1) * 24 * 60 * 60)); + return buf; +} diff --git a/src/lib/libast/string/fmterror.c b/src/lib/libast/string/fmterror.c new file mode 100644 index 0000000..eeeb5bc --- /dev/null +++ b/src/lib/libast/string/fmterror.c @@ -0,0 +1,37 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * Glenn Fowler + * AT&T Research + * + * return error message string given errno + */ + +#include <ast.h> + +char* +fmterror(int err) +{ + return strerror(err); +} diff --git a/src/lib/libast/string/fmtesc.c b/src/lib/libast/string/fmtesc.c new file mode 100644 index 0000000..753b195 --- /dev/null +++ b/src/lib/libast/string/fmtesc.c @@ -0,0 +1,248 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * return string with expanded escape chars + */ + +#include <ast.h> +#include <ccode.h> +#include <ctype.h> +#if _hdr_wchar && _hdr_wctype +#include <wchar.h> +#include <wctype.h> +#endif + +/* + * quote string as of length n with qb...qe + * (flags&FMT_ALWAYS) always quotes, otherwise quote output only if necessary + * qe and the usual suspects are \... escaped + * (flags&FMT_WIDE) doesn't escape 8 bit chars + * (flags&FMT_ESCAPED) doesn't \... escape the usual suspects + * (flags&FMT_SHELL) escape $`"#;~&|()<>[]*? + */ + +char* +fmtquote(const char* as, const char* qb, const char* qe, size_t n, int flags) +{ + register unsigned char* s = (unsigned char*)as; + register unsigned char* e = s + n; + register char* b; + register int c; + register int m; + register int escaped; + register int spaced; + register int doublequote; + register int singlequote; + int shell; + char* f; + char* buf; + + c = 4 * (n + 1); + if (qb) + c += strlen((char*)qb); + if (qe) + c += strlen((char*)qe); + b = buf = fmtbuf(c); + shell = 0; + doublequote = 0; + singlequote = 0; + if (qb) + { + if (qb[0] == '$' && qb[1] == '\'' && qb[2] == 0) + shell = 1; + else if ((flags & FMT_SHELL) && qb[1] == 0) + { + if (qb[0] == '"') + doublequote = 1; + else if (qb[0] == '\'') + singlequote = 1; + } + while (*b = *qb++) + b++; + } + else if (flags & FMT_SHELL) + doublequote = 1; + f = b; + escaped = spaced = !!(flags & FMT_ALWAYS); + while (s < e) + { + if ((m = mbsize(s)) > 1 && (s + m) <= e) + { +#if _hdr_wchar && _hdr_wctype + c = mbchar(s); + if (!spaced && !escaped && (iswspace(c) || iswcntrl(c))) + spaced = 1; + s -= m; +#endif + while (m--) + *b++ = *s++; + } + else + { + c = *s++; + if (!(flags & FMT_ESCAPED) && (iscntrl(c) || !isprint(c) || c == '\\')) + { + escaped = 1; + *b++ = '\\'; + switch (c) + { + case CC_bel: + c = 'a'; + break; + case '\b': + c = 'b'; + break; + case '\f': + c = 'f'; + break; + case '\n': + c = 'n'; + break; + case '\r': + c = 'r'; + break; + case '\t': + c = 't'; + break; + case CC_vt: + c = 'v'; + break; + case CC_esc: + c = 'E'; + break; + case '\\': + break; + default: + if (!(flags & FMT_WIDE) || !(c & 0200)) + { + *b++ = '0' + ((c >> 6) & 07); + *b++ = '0' + ((c >> 3) & 07); + c = '0' + (c & 07); + } + else + b--; + break; + } + } + else if (c == '\\') + { + escaped = 1; + *b++ = c; + if (*s) + c = *s++; + } + else if (qe && strchr(qe, c)) + { + if (singlequote && c == '\'') + { + spaced = 1; + *b++ = '\''; + *b++ = '\\'; + *b++ = '\''; + c = '\''; + } + else + { + escaped = 1; + *b++ = '\\'; + } + } + else if (c == '$' || c == '`') + { + if (c == '$' && (flags & FMT_PARAM) && (*s == '{' || *s == '(')) + { + if (singlequote || shell) + { + escaped = 1; + *b++ = '\''; + *b++ = c; + *b++ = *s++; + if (shell) + { + spaced = 1; + *b++ = '$'; + } + c = '\''; + } + else + { + escaped = 1; + *b++ = c; + c = *s++; + } + } + else if (doublequote) + *b++ = '\\'; + else if (singlequote || (flags & FMT_SHELL)) + spaced = 1; + } + else if (!spaced && !escaped && (isspace(c) || ((flags & FMT_SHELL) || shell) && (strchr("\";~&|()<>[]*?", c) || c == '#' && (b == f || isspace(*(b - 1)))))) + spaced = 1; + *b++ = c; + } + } + if (qb) + { + if (!escaped) + buf += shell + !spaced; + if (qe && (escaped || spaced)) + while (*b = *qe++) + b++; + } + *b = 0; + return buf; +} + +/* + * escape the usual suspects and quote chars in qs + * in length n string as + */ + +char* +fmtnesq(const char* as, const char* qs, size_t n) +{ + return fmtquote(as, NiL, qs, n, 0); +} + +/* + * escape the usual suspects and quote chars in qs + */ + +char* +fmtesq(const char* as, const char* qs) +{ + return fmtquote(as, NiL, qs, strlen((char*)as), 0); +} + +/* + * escape the usual suspects + */ + +char* +fmtesc(const char* as) +{ + return fmtquote(as, NiL, NiL, strlen((char*)as), 0); +} diff --git a/src/lib/libast/string/fmtfmt.c b/src/lib/libast/string/fmtfmt.c new file mode 100644 index 0000000..639355b --- /dev/null +++ b/src/lib/libast/string/fmtfmt.c @@ -0,0 +1,205 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * Glenn Fowler + * AT&T Research + * + * return printf(3) format signature given format string + * the format signature contains one char per format optionally preceded + * by the number of `*' args + * c char + * d double + * D long double + * f float + * h short + * i int + * j long long + * l long + * p void* + * s string + * t ptrdiff_t + * z size_t + * ? unknown + */ + +#include <ast.h> +#include <ctype.h> + +char* +fmtfmt(const char* as) +{ + register char* s = (char*)as; + char* buf; + int i; + int c; + int a; + int q; + int x; + int t; + int m; + int n; + int z; + char formats[256]; + unsigned int extra[elementsof(formats)]; + + z = 1; + i = m = 0; + for (;;) + { + switch (*s++) + { + case 0: + break; + case '%': + if (*s == '%') + continue; + n = 0; + a = 0; + q = 0; + t = '?'; + x = 0; + for (;;) + { + switch (c = *s++) + { + case 0: + s--; + break; + case '(': + q++; + continue; + case ')': + if (--q <= 0) + n = 0; + continue; + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + case '8': case '9': + n = n * 10 + (c - '0'); + continue; + case '$': + a = n; + n = 0; + continue; + case '*': + x++; + n = 0; + continue; + case 'h': + if (!q) + t = t == 'h' ? 'c' : 'h'; + continue; + case 'l': + if (!q) + t = t == 'l' ? 'j' : 'l'; + continue; + case 'j': + case 't': + case 'z': + if (!q) + t = c; + continue; + case 'c': + case 'p': + case 's': + if (!q) + { + t = c; + break; + } + continue; + case 'e': + case 'g': + if (!q) + { + switch (t) + { + case 'j': + t = 'D'; + break; + default: + t = 'd'; + break; + } + break; + } + continue; + case 'f': + if (!q) + { + switch (t) + { + case 'j': + t = 'D'; + break; + case 'l': + t = 'd'; + break; + default: + t = c; + break; + } + break; + } + continue; + default: + if (!q && isalpha(c)) + { + if (t == '?') + t = 'i'; + break; + } + n = 0; + continue; + } + break; + } + if (a) + i = a; + else + i++; + if (i < elementsof(formats)) + { + formats[i] = t; + if (extra[i] = x) + do z++; while (x /= 10); + if (m < i) + m = i; + } + continue; + default: + continue; + } + break; + } + s = buf = fmtbuf(m + z); + for (i = 1; i <= m; i++) + { + if (extra[i]) + s += sfsprintf(s, 10, "%d", extra[m]); + *s++ = formats[i]; + } + *s = 0; + return buf; +} diff --git a/src/lib/libast/string/fmtfs.c b/src/lib/libast/string/fmtfs.c new file mode 100644 index 0000000..1d89b11 --- /dev/null +++ b/src/lib/libast/string/fmtfs.c @@ -0,0 +1,100 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * return fs type string given stat + */ + +#include <ast.h> +#include <ls.h> +#include <mnt.h> + +#include "FEATURE/fs" + +#if _str_st_fstype + +char* +fmtfs(struct stat* st) +{ + return st->st_fstype; +} + +#else + +#include <cdt.h> + +typedef struct Id_s +{ + Dtlink_t link; + dev_t id; + char name[1]; +} Id_t; + +char* +fmtfs(struct stat* st) +{ + register Id_t* ip; + register void* mp; + register Mnt_t* mnt; + register char* s; + struct stat rt; + char* buf; + + static Dt_t* dict; + static Dtdisc_t disc; + + if (!dict) + { + disc.key = offsetof(Id_t, id); + disc.size = sizeof(dev_t); + dict = dtopen(&disc, Dtset); + } + else if (ip = (Id_t*)dtmatch(dict, &st->st_dev)) + return ip->name; + s = FS_default; + if (mp = mntopen(NiL, "r")) + { + while ((mnt = mntread(mp)) && (stat(mnt->dir, &rt) || rt.st_dev != st->st_dev)); + if (mnt && mnt->type) + s = mnt->type; + } + if (!dict || !(ip = newof(0, Id_t, 1, strlen(s)))) + { + if (!mp) + return s; + buf = fmtbuf(strlen(s) + 1); + strcpy(buf, s); + mntclose(mp); + return buf; + } + strcpy(ip->name, s); + if (mp) + mntclose(mp); + dtinsert(dict, ip); + return ip->name; +} + +#endif diff --git a/src/lib/libast/string/fmtgid.c b/src/lib/libast/string/fmtgid.c new file mode 100644 index 0000000..25ef158 --- /dev/null +++ b/src/lib/libast/string/fmtgid.c @@ -0,0 +1,101 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * cached gid number -> name + */ + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:hide getgrgid +#else +#define getgrgid ______getgrgid +#endif + +#include <ast.h> +#include <cdt.h> +#include <grp.h> + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:nohide getgrgid +#else +#undef getgrgid +#endif + +extern struct group* getgrgid(gid_t); + +typedef struct Id_s +{ + Dtlink_t link; + int id; + char name[1]; +} Id_t; + +/* + * return gid name given gid number + */ + +char* +fmtgid(int gid) +{ + register Id_t* ip; + register char* name; + register struct group* gr; + int z; + + static Dt_t* dict; + static Dtdisc_t disc; + + if (!dict) + { + disc.key = offsetof(Id_t, id); + disc.size = sizeof(int); + dict = dtopen(&disc, Dtset); + } + else if (ip = (Id_t*)dtmatch(dict, &gid)) + return ip->name; + if (gr = getgrgid(gid)) + { + name = gr->gr_name; +#if _WINIX + if (streq(name, "Administrators")) + name = "sys"; +#endif + } + else if (gid == 0) + name = "sys"; + else + { + name = fmtbuf(z = sizeof(gid) * 3 + 1); + sfsprintf(name, z, "%I*d", sizeof(gid), gid); + } + if (dict && (ip = newof(0, Id_t, 1, strlen(name)))) + { + ip->id = gid; + strcpy(ip->name, name); + dtinsert(dict, ip); + return ip->name; + } + return name; +} diff --git a/src/lib/libast/string/fmtident.c b/src/lib/libast/string/fmtident.c new file mode 100644 index 0000000..0ffab5c --- /dev/null +++ b/src/lib/libast/string/fmtident.c @@ -0,0 +1,77 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> +#include <ctype.h> + +#define IDENT 01 +#define USAGE 02 + +/* + * format what(1) and/or ident(1) string a + */ + +char* +fmtident(const char* a) +{ + register char* s = (char*)a; + register char* t; + char* buf; + int i; + + i = 0; + for (;;) + { + while (isspace(*s)) + s++; + if (s[0] == '[') + { + while (*++s && *s != '\n'); + i |= USAGE; + } + else if (s[0] == '@' && s[1] == '(' && s[2] == '#' && s[3] == ')') + s += 4; + else if (s[0] == '$' && s[1] == 'I' && s[2] == 'd' && s[3] == ':' && isspace(s[4])) + { + s += 5; + i |= IDENT; + } + else + break; + } + if (i) + { + i &= IDENT; + for (t = s; isprint(*t) && *t != '\n'; t++) + if (i && t[0] == ' ' && t[1] == '$') + break; + while (t > s && isspace(t[-1])) + t--; + i = t - s; + buf = fmtbuf(i + 1); + memcpy(buf, s, i); + s = buf; + s[i] = 0; + } + return s; +} diff --git a/src/lib/libast/string/fmtint.c b/src/lib/libast/string/fmtint.c new file mode 100644 index 0000000..932fe1e --- /dev/null +++ b/src/lib/libast/string/fmtint.c @@ -0,0 +1,122 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * format decimal integer + * David Korn + * AT&T Research + */ + +#include <ast.h> + +static const char table[]= +{ + "000001002003004005006007008009010011012013014015016017018019" + "020021022023024025026027028029030031032033034035036037038039" + "040041042043044045046047048049050051052053054055056057058059" + "060061062063064065066067068069070071072073074075076077078079" + "080081082083084085086087088089090091092093094095096097098099" + "100101102103104105106107108109110111112113114115116117118119" + "120121122123124125126127128129130131132133134135136137138139" + "140141142143144145146147148149150151152153154155156157158159" + "160161162163164165166167168169170171172173174175176177178179" + "180181182183184185186187188189190191192193194195196197198199" + "200201202203204205206207208209210211212213214215216217218219" + "220221222223224225226227228229230231232233234235236237238239" + "240241242243244245246247248249250251252253254255256257258259" + "260261262263264265266267268269270271272273274275276277278279" + "280281282283284285286287288289290291292293294295296297298299" + "300301302303304305306307308309310311312313314315316317318319" + "320321322323324325326327328329330331332333334335336337338339" + "340341342343344345346347348349350351352353354355356357358359" + "360361362363364365366367368369370371372373374375376377378379" + "380381382383384385386387388389390391392393394395396397398399" + "400401402403404405406407408409410411412413414415416417418419" + "420421422423424425426427428429430431432433434435436437438439" + "440441442443444445446447448449450451452453454455456457458459" + "460461462463464465466467468469470471472473474475476477478479" + "480481482483484485486487488489490491492493494495496497498499" + "500501502503504505506507508509510511512513514515516517518519" + "520521522523524525526527528529530531532533534535536537538539" + "540541542543544545546547548549550551552553554555556557558559" + "560561562563564565566567568569570571572573574575576577578579" + "580581582583584585586587588589590591592593594595596597598599" + "600601602603604605606607608609610611612613614615616617618619" + "620621622623624625626627628629630631632633634635636637638639" + "640641642643644645646647648649650651652653654655656657658659" + "660661662663664665666667668669670671672673674675676677678679" + "680681682683684685686687688689690691692693694695696697698699" + "700701702703704705706707708709710711712713714715716717718719" + "720721722723724725726727728729730731732733734735736737738739" + "740741742743744745746747748749750751752753754755756757758759" + "760761762763764765766767768769770771772773774775776777778779" + "780781782783784785786787788789790791792793794795796797798799" + "800801802803804805806807808809810811812813814815816817818819" + "820821822823824825826827828829830831832833834835836837838839" + "840841842843844845846847848849850851852853854855856857858859" + "860861862863864865866867868869870871872873874875876877878879" + "880881882883884885886887888889890891892893894895896897898899" + "900901902903904905906907908909910911912913914915916917918919" + "920921922923924925926927928929930931932933934935936937938939" + "940941942943944945946947948949950951952953954955956957958959" + "960961962963964965966967968969970971972973974975976977978979" + "980981982983984985986987988989990991992993994995996997998999" +}; + +char* +fmtint(intmax_t ll, int unsign) +{ + char *buff; + uintmax_t n,m; + int j=0,k=3*sizeof(ll); + if(unsign || ll>=0) + n = ll; + else + { + n = -ll; + j = 1; + } + if(n<10) + { + buff = fmtbuf(k=3); + buff[--k] = 0; + buff[--k] = '0' + n; + goto skip; + } + buff = fmtbuf(k); + buff[--k] = 0; + do + { + k -= 3; + if((m=n) >= 1000) + m = n%1000; + memcpy(buff+k,table+3*m,3); + n /= 1000; + } + while(n>0); + while(buff[k]=='0') + k++; +skip: + if(j) + buff[--k] = '-'; + return(&buff[k]); +} diff --git a/src/lib/libast/string/fmtip4.c b/src/lib/libast/string/fmtip4.c new file mode 100644 index 0000000..3920c7f --- /dev/null +++ b/src/lib/libast/string/fmtip4.c @@ -0,0 +1,43 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> + +/* + * format 4 byte local byte order ip address + * and optional prefix bits (if 0 <= bits <= 32) + */ + +char* +fmtip4(register uint32_t addr, int bits) +{ + char* buf; + int z; + int i; + + buf = fmtbuf(z = 20); + i = sfsprintf(buf, z, "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, (addr)&0xff); + if (bits >= 0 && bits <= 32) + sfsprintf(buf + i, z - i, "/%d", bits); + return buf; +} diff --git a/src/lib/libast/string/fmtip6.c b/src/lib/libast/string/fmtip6.c new file mode 100644 index 0000000..50bac43 --- /dev/null +++ b/src/lib/libast/string/fmtip6.c @@ -0,0 +1,175 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#if _PACKAGE_ast +#include <ast.h> +#endif + +#include <ip6.h> + +#if !_PACKAGE_ast + +/* + * return a pointer to n bytes from a circular re-use buffer + */ + +static char* +fmtbuf(int n) +{ + char* b; + + static char buf[1024]; + static char* p = buf; + + if ((&buf[sizeof(buf)] - p) < n) + p = buf; + b = p; + p += n; + return b; +} + +#endif + +/* + * copy p to s, then convert 0<=n<=999 to text + * next char in s returned + * caller ensures that s can take strlen(p)+3 bytes + */ + +static char* +dec(char* s, char* p, int n) +{ + while (*s = *p++) + s++; + if (n >= 100) + *s++ = '0' + ((n / 100) % 10); + if (n >= 10) + *s++ = '0' + ((n / 10) % 10); + *s++ = '0' + (n % 10); + return s; +} + +/* + * return pointer to normalized ipv6 address addr + * with optional prefix bits if 0 <= bits <= 128 + * return value in short-term circular buffer + */ + +char* +fmtip6(const unsigned char* addr, int bits) +{ + register const unsigned char* a = addr; + register int n = IP6ADDR; + register int i; + register int z; + register int k; + register int m; + unsigned char r[IP6ADDR]; + char* b; + char* s; + + static const char dig[] = "0123456789ABCDEF"; + + s = b = fmtbuf(44); + r[m = z = 0] = 0; + if (a[0] == 0x20 && a[1] == 0x02 && (a[2] || a[3] || a[4] || a[5])) + { + z = 6; + s = dec(s, "2002:", a[2]); + s = dec(s, ".", a[3]); + s = dec(s, ".", a[4]); + s = dec(s, ".", a[5]); + } + for (i = z; i < n; i += 2) + { + for (k = i; i < n - 1 && !a[i] && !a[i + 1]; i += 2); + if ((r[k] = i - k) > r[m] || r[k] == r[m] && i >= (n - 1)) + m = k; + } + if (!m) + switch (r[m]) + { + case 0: + m = -1; + break; + case 14: + if (!a[14] && a[15] <= 15) + break; + /*FALLTHROUGH*/ + case 12: + s = dec(s, "::", a[12]); + s = dec(s, ".", a[13]); + s = dec(s, ".", a[14]); + s = dec(s, ".", a[15]); + n = 0; + break; + case 10: + if (a[10] == 0xFF && a[11] == 0xFF) + { + s = dec(s, "::FFFF:", a[12]); + s = dec(s, ".", a[13]); + s = dec(s, ".", a[14]); + s = dec(s, ".", a[15]); + n = 0; + } + break; + } + for (i = z; i < n; i++) + { + if (i == m) + { + *s++ = ':'; + *s++ = ':'; + if ((i += r[m]) >= n) + { + z = 1; + break; + } + z = 0; + } + else if (i && !(i & 1)) + { + if (z) + z = 0; + else + *s++ = '0'; + *s++ = ':'; + } + if ((k = (a[i] >> 4) & 0xf) || z) + { + z = 1; + *s++ = dig[k]; + } + if ((k = a[i] & 0xf) || z) + { + z = 1; + *s++ = dig[k]; + } + } + if (!z && *(s - 1) == ':') + *s++ = '0'; + if (bits >= 0 && bits <= 128) + s = dec(s, "/", bits); + *s = 0; + return b; +} diff --git a/src/lib/libast/string/fmtls.c b/src/lib/libast/string/fmtls.c new file mode 100644 index 0000000..7b791dd --- /dev/null +++ b/src/lib/libast/string/fmtls.c @@ -0,0 +1,120 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * ls formatter + */ + +#include <ast.h> +#include <ls.h> +#include <tm.h> + +#ifndef LS_W_MAX +#define LS_W_MAX 128 +#endif + +/* + * ls formatter + * + * buf results placed here + * name file name + * st file stat buffer + * info optional info + * link link text if != 0 + * flags LS_* flags + * + * return end of formatted buf + */ + +char* +fmtls(char* buf, const char* name, register struct stat* st, const char* info, const char* link, register int flags) +{ + register char* s; + time_t tm; + Sfoff_t n; + + s = buf; + if (flags & LS_INUMBER) + s += sfsprintf(s, LS_W_MAX, "%*I*u ", LS_W_INUMBER - 1, sizeof(st->st_ino), st->st_ino); + if (flags & LS_BLOCKS) + { + n = iblocks(st); + s += sfsprintf(s, LS_W_MAX, "%*I*u ", LS_W_BLOCKS - 1, sizeof(n), n); + } + if (flags & LS_LONG) + { + s += sfsprintf(s, LS_W_MAX, "%s%3u", fmtmode(st->st_mode, flags & LS_EXTERNAL), (unsigned int)st->st_nlink); + if (!(flags & LS_NOUSER)) + { + if (flags & LS_NUMBER) + s += sfsprintf(s, LS_W_MAX, " %-*I*d", LS_W_NAME - 1, sizeof(st->st_uid), st->st_uid); + else + s += sfsprintf(s, LS_W_MAX, " %-*s", LS_W_NAME - 1, fmtuid(st->st_uid)); + } + if (!(flags & LS_NOGROUP)) + { + if (flags & LS_NUMBER) + s += sfsprintf(s, LS_W_MAX, " %-*I*d", LS_W_NAME - 1, sizeof(st->st_gid), st->st_gid); + else + s += sfsprintf(s, LS_W_MAX, " %-*s", LS_W_NAME - 1, fmtgid(st->st_gid)); + } + if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) + s += sfsprintf(s, LS_W_MAX, "%8s ", fmtdev(st)); + else + s += sfsprintf(s, LS_W_MAX, "%8I*u ", sizeof(st->st_size), st->st_size); + tm = (flags & LS_ATIME) ? st->st_atime : (flags & LS_CTIME) ? st->st_ctime : st->st_mtime; + s = tmfmt(s, LS_W_LONG / 2, "%?%QL", &tm); + *s++ = ' '; + } + if (info) + { + while (*s = *info++) + s++; + *s++ = ' '; + } + while (*s = *name++) + s++; + if (flags & LS_MARK) + { + if (S_ISDIR(st->st_mode)) + *s++ = '/'; +#ifdef S_ISLNK + else if (S_ISLNK(st->st_mode)) + *s++ = '@'; +#endif + else if (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) + *s++ = '*'; + } + if (link) + { + s += sfsprintf(s, LS_W_MAX, " %s %s", +#ifdef S_ISLNK + S_ISLNK(st->st_mode) ? "->" : +#endif + "==", link); + } + *s = 0; + return s; +} diff --git a/src/lib/libast/string/fmtmatch.c b/src/lib/libast/string/fmtmatch.c new file mode 100644 index 0000000..2daf2f7 --- /dev/null +++ b/src/lib/libast/string/fmtmatch.c @@ -0,0 +1,286 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * return strmatch() expression given REG_AUGMENTED RE + * 0 returned for invalid RE + */ + +#include <ast.h> + +char* +fmtmatch(const char* as) +{ + register char* s = (char*)as; + register int c; + register char* t; + register char** p; + register char* b; + char* x; + char* y; + char* z; + int a; + int e; + int n; + char* buf; + char* stack[32]; + + c = 3 * (strlen(s) + 1); + buf = fmtbuf(c); + t = b = buf + 3; + p = stack; + if (a = *s == '^') + s++; + e = 0; + for (;;) + { + switch (c = *s++) + { + case 0: + break; + case '\\': + if (!(c = *s++)) + return 0; + switch (*s) + { + case '*': + case '+': + case '?': + *t++ = *s++; + *t++ = '('; + *t++ = '\\'; + *t++ = c; + c = ')'; + break; + case '|': + case '&': + if (c == '(') + { + *t++ = c; + c = *s++; + goto logical; + } + break; + case '{': + case '}': + break; + default: + *t++ = '\\'; + break; + } + *t++ = c; + continue; + case '[': + x = t; + *t++ = c; + if ((c = *s++) == '^') + { + *t++ = '!'; + c = *s++; + } + else if (c == '!') + { + *t++ = '\\'; + *t++ = c; + c = *s++; + } + for (;;) + { + if (!(*t++ = c)) + return 0; + if (c == '\\') + *t++ = c; + if ((c = *s++) == ']') + { + *t++ = c; + break; + } + } + switch (*s) + { + case '*': + case '+': + case '?': + for (y = t + 2, t--; t >= x; t--) + *(t + 2) = *t; + *++t = *s++; + *++t = '('; + t = y; + *t++ = ')'; + break; + } + continue; + case '(': + if (p >= &stack[elementsof(stack)]) + return 0; + *p++ = t; + if (*s == '?') + { + s++; + if (*s == 'K' && *(s + 1) == ')') + { + s += 2; + p--; + while (*t = *s) + t++, s++; + continue; + } + *t++ = '~'; + } + else + *t++ = '@'; + *t++ = '('; + continue; + case ')': + if (p == stack) + return 0; + p--; + *t++ = c; + switch (*s) + { + case 0: + break; + case '*': + case '+': + case '?': + case '!': + **p = *s++; + if (*s == '?') + { + s++; + x = *p + 1; + for (y = ++t; y > x; y--) + *y = *(y - 1); + *x = '-'; + } + continue; + case '{': + for (z = s; *z != '}'; z++) + if (!*z) + return 0; + n = z - s; + if (*++z == '?') + n++; + x = *p + n; + for (y = t += n; y > x; y--) + *y = *(y - n); + for (x = *p; s < z; *x++ = *s++); + if (*s == '?') + { + s++; + *x++ = '-'; + } + continue; + default: + continue; + } + break; + case '.': + switch (*s) + { + case 0: + *t++ = '?'; + break; + case '*': + s++; + *t++ = '*'; + e = !*s; + continue; + case '+': + s++; + *t++ = '?'; + *t++ = '*'; + continue; + case '?': + s++; + *t++ = '?'; + *t++ = '('; + *t++ = '?'; + *t++ = ')'; + continue; + default: + *t++ = '?'; + continue; + } + break; + case '*': + case '+': + case '?': + case '{': + n = *(t - 1); + if (t == b || n == '(' || n == '|') + return 0; + *(t - 1) = c; + if (c == '{') + { + for (z = s; *z != '}'; z++) + if (!*z) + return 0; + for (; s <= z; *t++ = *s++); + } + if (*s == '?') + { + s++; + *t++ = '-'; + } + *t++ = '('; + *t++ = n; + *t++ = ')'; + continue; + case '|': + case '&': + if (t == b || *(t - 1) == '(') + return 0; + logical: + if (!*s || *s == ')') + return 0; + if (p == stack && b == buf + 3) + { + *--b = '('; + *--b = '@'; + } + *t++ = c; + continue; + case '$': + if (e = !*s) + break; + /*FALLTHROUGH*/ + default: + *t++ = c; + continue; + } + break; + } + if (p != stack) + return 0; + if (b != buf + 3) + *t++ = ')'; + if (!a && (*b != '*' || *(b + 1) == '(' || (*(b + 1) == '-' || *(b + 1) == '~') && *(b + 2) == '(')) + *--b = '*'; + if (!e) + *t++ = '*'; + *t = 0; + return b; +} diff --git a/src/lib/libast/string/fmtmode.c b/src/lib/libast/string/fmtmode.c new file mode 100644 index 0000000..2f9dc4f --- /dev/null +++ b/src/lib/libast/string/fmtmode.c @@ -0,0 +1,47 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * return ls -l style file mode string given file mode bits + * if external!=0 then mode is modex canonical + */ + +#include "modelib.h" + +char* +fmtmode(register int mode, int external) +{ + register char* s; + register struct modeop* p; + char* buf; + + if (!external) + mode = modex(mode); + s = buf = fmtbuf(MODELEN + 1); + for (p = modetab; p < &modetab[MODELEN]; p++) + *s++ = p->name[((mode & p->mask1) >> p->shift1) | ((mode & p->mask2) >> p->shift2)]; + *s = 0; + return buf; +} diff --git a/src/lib/libast/string/fmtnum.c b/src/lib/libast/string/fmtnum.c new file mode 100644 index 0000000..49fb9c3 --- /dev/null +++ b/src/lib/libast/string/fmtnum.c @@ -0,0 +1,92 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * return scaled number n + * string width is 5 chars or less + * if m>1 then n divided by m before scaling + */ + +#include <ast.h> + +char* +fmtnum(register unsigned long n, int m) +{ + register int i; + register unsigned long r; + char* buf; + int z; + + char suf[2]; + + if (m > 1) + { + r = n; + n /= m; + r -= n; + } + else + r = 0; + suf[1] = 0; + if (n < 1024) + suf[0] = 0; + else if (n < 1024 * 1024) + { + suf[0] = 'k'; + r = ((n % 1024) * 100) / 1024; + n /= 1024; + } + else if (n < 1024 * 1024 * 1024) + { + suf[0] = 'm'; + r = ((n % (1024 * 1024)) * 100) / (1024 * 1024); + n /= 1024 * 1024; + } + else + { + suf[0] = 'g'; + r = ((n % (1024 * 1024 * 1024)) * 100) / (1024 * 1024 * 1024); + n /= 1024 * 1024 * 1024; + } + if (r) + { + if (n >= 100) + r = 0; + else if (n >= 10) + { + i = 1; + if (r >= 10) + r /= 10; + } + else + i = 2; + } + buf = fmtbuf(z = 8); + if (r) + sfsprintf(buf, z, "%lu.%0*lu%s", n, i, r, suf); + else + sfsprintf(buf, z, "%lu%s", n, suf); + return buf; +} diff --git a/src/lib/libast/string/fmtperm.c b/src/lib/libast/string/fmtperm.c new file mode 100644 index 0000000..c10d379 --- /dev/null +++ b/src/lib/libast/string/fmtperm.c @@ -0,0 +1,91 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * return strperm() expression for perm + */ + +#include <ast.h> +#include <ls.h> + +char* +fmtperm(register int perm) +{ + register char* s; + char* buf; + + s = buf = fmtbuf(32); + + /* + * u + */ + + *s++ = 'u'; + *s++ = '='; + if (perm & S_ISVTX) + *s++ = 't'; + if (perm & S_ISUID) + *s++ = 's'; + if (perm & S_IRUSR) + *s++ = 'r'; + if (perm & S_IWUSR) + *s++ = 'w'; + if (perm & S_IXUSR) + *s++ = 'x'; + if ((perm & (S_ISGID|S_IXGRP)) == S_ISGID) + *s++ = 'l'; + + /* + * g + */ + + *s++ = ','; + *s++ = 'g'; + *s++ = '='; + if ((perm & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) + *s++ = 's'; + if (perm & S_IRGRP) + *s++ = 'r'; + if (perm & S_IWGRP) + *s++ = 'w'; + if (perm & S_IXGRP) + *s++ = 'x'; + + /* + * o + */ + + *s++ = ','; + *s++ = 'o'; + *s++ = '='; + if (perm & S_IROTH) + *s++ = 'r'; + if (perm & S_IWOTH) + *s++ = 'w'; + if (perm & S_IXOTH) + *s++ = 'x'; + *s = 0; + return buf; +} diff --git a/src/lib/libast/string/fmtre.c b/src/lib/libast/string/fmtre.c new file mode 100644 index 0000000..a4a887b --- /dev/null +++ b/src/lib/libast/string/fmtre.c @@ -0,0 +1,226 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * Glenn Fowler + * AT&T Research + * + * return RE expression given strmatch() pattern + * 0 returned for invalid RE + */ + +#include <ast.h> + +typedef struct Stack_s +{ + char* beg; + short len; + short min; +} Stack_t; + +char* +fmtre(const char* as) +{ + register char* s = (char*)as; + register int c; + register char* t; + register Stack_t* p; + char* x; + int n; + int end; + char* buf; + Stack_t stack[32]; + + end = 1; + c = 2 * strlen(s) + 1; + t = buf = fmtbuf(c); + p = stack; + if (*s != '*' || *(s + 1) == '(' || *(s + 1) == '-' && *(s + 2) == '(') + *t++ = '^'; + else + s++; + for (;;) + { + switch (c = *s++) + { + case 0: + break; + case '\\': + if (!(c = *s++) || c == '{' || c == '}') + return 0; + *t++ = '\\'; + if ((*t++ = c) == '(' && *s == '|') + { + *t++ = *s++; + goto logical; + } + continue; + case '[': + *t++ = c; + n = 0; + if ((c = *s++) == '!') + { + *t++ = '^'; + c = *s++; + } + else if (c == '^') + { + if ((c = *s++) == ']') + { + *(t - 1) = '\\'; + *t++ = '^'; + continue; + } + n = '^'; + } + for (;;) + { + if (!(*t++ = c)) + return 0; + if ((c = *s++) == ']') + { + if (n) + *t++ = n; + *t++ = c; + break; + } + } + continue; + case '{': + for (x = s; *x && *x != '}'; x++); + if (*x++ && (*x == '(' || *x == '-' && *(x + 1) == '(')) + { + if (p >= &stack[elementsof(stack)]) + return 0; + p->beg = s - 1; + s = x; + p->len = s - p->beg; + if (p->min = *s == '-') + s++; + p++; + *t++ = *s++; + } + else + *t++ = c; + continue; + case '*': + if (!*s) + { + end = 0; + break; + } + /*FALLTHROUGH*/ + case '?': + case '+': + case '@': + case '!': + case '~': + if (*s == '(' || c != '~' && *s == '-' && *(s + 1) == '(') + { + if (p >= &stack[elementsof(stack)]) + return 0; + p->beg = s - 1; + if (c == '~') + { + if (*(s + 1) == 'E' && *(s + 2) == ')') + { + for (s += 3; *t = *s; t++, s++); + continue; + } + p->len = 0; + p->min = 0; + *t++ = *s++; + *t++ = '?'; + } + else + { + p->len = c != '@'; + if (p->min = *s == '-') + s++; + *t++ = *s++; + } + p++; + } + else + { + switch (c) + { + case '*': + *t++ = '.'; + break; + case '?': + c = '.'; + break; + case '+': + case '!': + *t++ = '\\'; + break; + } + *t++ = c; + } + continue; + case '(': + if (p >= &stack[elementsof(stack)]) + return 0; + p->beg = s - 1; + p->len = 0; + p->min = 0; + p++; + *t++ = c; + continue; + case ')': + if (p == stack) + return 0; + *t++ = c; + p--; + for (c = 0; c < p->len; c++) + *t++ = p->beg[c]; + if (p->min) + *t++ = '?'; + continue; + case '^': + case '.': + case '$': + *t++ = '\\'; + *t++ = c; + continue; + case '|': + if (t == buf || *(t - 1) == '(') + return 0; + logical: + if (!*s || *s == ')') + return 0; + /*FALLTHROUGH*/ + default: + *t++ = c; + continue; + } + break; + } + if (p != stack) + return 0; + if (end) + *t++ = '$'; + *t = 0; + return buf; +} diff --git a/src/lib/libast/string/fmtscale.c b/src/lib/libast/string/fmtscale.c new file mode 100644 index 0000000..5a35577 --- /dev/null +++ b/src/lib/libast/string/fmtscale.c @@ -0,0 +1,94 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * return number n scaled to metric multiples of k { 1000 1024 } + * return string length is at most 5 chars + terminating nul + */ + +#include <ast.h> +#include <lclib.h> + +char* +fmtscale(register Sfulong_t n, int k) +{ + register Sfulong_t m; + int r; + int z; + const char* u; + char suf[3]; + char* s; + char* buf; + Lc_numeric_t* p = (Lc_numeric_t*)LCINFO(AST_LC_NUMERIC)->data; + + static const char scale[] = "bkMGTPE"; + + u = scale; + if (n < 1000) + r = 0; + else + { + m = 0; + while (n >= k && *(u + 1)) + { + m = n; + n /= k; + u++; + } + if ((r = (10 * (m % k) + (k / 2)) / k) > 9) + { + r = 0; + n++; + } + if (k == 1024 && n >= 1000) + { + n = 1; + r = 0; + u++; + } + } + buf = fmtbuf(z = 8); + s = suf; + if (u > scale) + { + if (k == 1024) + { + *s++ = *u == 'k' ? 'K' : *u; + *s++ = 'i'; + } + else + *s++ = *u; + } + *s = 0; + if (n > 0 && n < 10) + sfsprintf(buf, z, "%I*u%c%d%s", sizeof(n), n, p->decimal >= 0 ? p->decimal : '.', r, suf); + else + { + if (r >= 5) + n++; + sfsprintf(buf, z, "%I*u%s", sizeof(n), n, suf); + } + return buf; +} diff --git a/src/lib/libast/string/fmtsignal.c b/src/lib/libast/string/fmtsignal.c new file mode 100644 index 0000000..a802a1d --- /dev/null +++ b/src/lib/libast/string/fmtsignal.c @@ -0,0 +1,62 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * if sig>=0 then return signal text for signal sig + * otherwise return signal name for signal -sig + */ + +#include <ast.h> +#include <sig.h> + +char* +fmtsignal(register int sig) +{ + char* buf; + int z; + + if (sig >= 0) + { + if (sig <= sig_info.sigmax) + buf = sig_info.text[sig]; + else + { + buf = fmtbuf(z = 20); + sfsprintf(buf, z, "Signal %d", sig); + } + } + else + { + sig = -sig; + if (sig <= sig_info.sigmax) + buf = sig_info.name[sig]; + else + { + buf = fmtbuf(z = 20); + sfsprintf(buf, z, "%d", sig); + } + } + return buf; +} diff --git a/src/lib/libast/string/fmttime.c b/src/lib/libast/string/fmttime.c new file mode 100644 index 0000000..f2cfbce --- /dev/null +++ b/src/lib/libast/string/fmttime.c @@ -0,0 +1,41 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * return tmfmt() formatted time + */ + +#include "tm.h" + +char* +fmttime(const char* format, time_t clock) +{ + char* buf; + int z; + + buf = fmtbuf(z = 80); + tmfmt(buf, z, format, &clock); + return buf; +} diff --git a/src/lib/libast/string/fmttmx.c b/src/lib/libast/string/fmttmx.c new file mode 100644 index 0000000..1a427ac --- /dev/null +++ b/src/lib/libast/string/fmttmx.c @@ -0,0 +1,45 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <tmx.h> +#include <tv.h> + +/* + * Time_t fmttime() + */ + +char* +fmttmx(const char* fmt, Time_t t) +{ + char* b; + char* e; + int z; + + z = 0; + do + { + b = fmtbuf(z += 80); + e = tmxfmt(b, z, fmt, t); + } while (e == b + z); + return b; +} diff --git a/src/lib/libast/string/fmttv.c b/src/lib/libast/string/fmttv.c new file mode 100644 index 0000000..f4cdf1b --- /dev/null +++ b/src/lib/libast/string/fmttv.c @@ -0,0 +1,44 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <tv.h> +#include <tm.h> + +/* + * Tv_t fmttime() + */ + +char* +fmttv(const char* fmt, Tv_t* tv) +{ + char* s; + char* t; + int n; + + s = fmttime(fmt, (time_t)tv->tv_sec); + if (!tv->tv_nsec || tv->tv_nsec == TV_NSEC_IGNORE) + return s; + t = fmtbuf(n = strlen(s) + 11); + sfsprintf(t, n, "%s.%09lu", s, (unsigned long)tv->tv_nsec); + return t; +} diff --git a/src/lib/libast/string/fmtuid.c b/src/lib/libast/string/fmtuid.c new file mode 100644 index 0000000..44a1d44 --- /dev/null +++ b/src/lib/libast/string/fmtuid.c @@ -0,0 +1,101 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * uid number -> name + */ + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:hide getpwuid +#else +#define getpwuid ______getpwuid +#endif + +#include <ast.h> +#include <cdt.h> +#include <pwd.h> + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:nohide getpwuid +#else +#undef getpwuid +#endif + +extern struct passwd* getpwuid(uid_t); + +typedef struct Id_s +{ + Dtlink_t link; + int id; + char name[1]; +} Id_t; + +/* + * return uid name given uid number + */ + +char* +fmtuid(int uid) +{ + register Id_t* ip; + register char* name; + register struct passwd* pw; + int z; + + static Dt_t* dict; + static Dtdisc_t disc; + + if (!dict) + { + disc.key = offsetof(Id_t, id); + disc.size = sizeof(int); + dict = dtopen(&disc, Dtset); + } + else if (ip = (Id_t*)dtmatch(dict, &uid)) + return ip->name; + if (pw = getpwuid(uid)) + { + name = pw->pw_name; +#if _WINIX + if (streq(name, "Administrator")) + name = "root"; +#endif + } + else if (uid == 0) + name = "root"; + else + { + name = fmtbuf(z = sizeof(uid) * 3 + 1); + sfsprintf(name, z, "%I*d", sizeof(uid), uid); + } + if (dict && (ip = newof(0, Id_t, 1, strlen(name)))) + { + ip->id = uid; + strcpy(ip->name, name); + dtinsert(dict, ip); + return ip->name; + } + return name; +} diff --git a/src/lib/libast/string/fmtversion.c b/src/lib/libast/string/fmtversion.c new file mode 100644 index 0000000..10f2fc9 --- /dev/null +++ b/src/lib/libast/string/fmtversion.c @@ -0,0 +1,53 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * return formatted <magicid.h> version string + */ + +#include <ast.h> + +char* +fmtversion(register unsigned long v) +{ + register char* cur; + register char* end; + char* buf; + int n; + + buf = cur = fmtbuf(n = 18); + end = cur + n; + if (v >= 19700101L && v <= 29991231L) + sfsprintf(cur, end - cur, "%04lu-%02lu-%02lu", (v / 10000) % 10000, (v / 100) % 100, v % 100); + else + { + if (n = (v >> 24) & 0xff) + cur += sfsprintf(cur, end - cur, "%d.", n); + if (n = (v >> 16) & 0xff) + cur += sfsprintf(cur, end - cur, "%d.", n); + sfsprintf(cur, end - cur, "%ld.%ld", (v >> 8) & 0xff, v & 0xff); + } + return buf; +} diff --git a/src/lib/libast/string/memdup.c b/src/lib/libast/string/memdup.c new file mode 100644 index 0000000..9282405 --- /dev/null +++ b/src/lib/libast/string/memdup.c @@ -0,0 +1,44 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> + +#if _lib_memdup + +NoN(memdup) + +#else + +/* + * return a copy of s of n chars using malloc + */ + +void* +memdup(register const void* s, register size_t n) +{ + register void* t; + + return((t = (void*)newof(0, char, n, 0)) ? memcpy(t, s, n) : 0); +} + +#endif diff --git a/src/lib/libast/string/modedata.c b/src/lib/libast/string/modedata.c new file mode 100644 index 0000000..dc10d62 --- /dev/null +++ b/src/lib/libast/string/modedata.c @@ -0,0 +1,69 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * fmtmode() and strperm() readonly data + * for external format modes + */ + +#include "modelib.h" + +struct modeop modetab[MODELEN] = +{ + 0170000, 12, 0000000, 0, "-pc?d?b?-Cl?sDw?", + 0000400, 8, 0000000, 0, "-r", + 0000200, 7, 0000000, 0, "-w", + 0004000, 10, 0000100, 6, "-xSs", + 0000040, 5, 0000000, 0, "-r", + 0000020, 4, 0000000, 0, "-w", +#ifdef S_ICCTYP + 0003000, 8, 0000010, 3, "-x-xSs-x", +#else + 0002000, 9, 0000010, 3, "-xls", +#endif + 0000004, 2, 0000000, 0, "-r", + 0000002, 1, 0000000, 0, "-w", +#ifdef S_ICCTYP + 0003000, 8, 0000001, 0, "-xyY-xeE", +#else + 0001000, 8, 0000001, 0, "-xTt", +#endif +}; + +int permmap[PERMLEN] = +{ + S_ISUID, X_ISUID, + S_ISGID, X_ISGID, + S_ISVTX, X_ISVTX, + S_IRUSR, X_IRUSR, + S_IWUSR, X_IWUSR, + S_IXUSR, X_IXUSR, + S_IRGRP, X_IRGRP, + S_IWGRP, X_IWGRP, + S_IXGRP, X_IXGRP, + S_IROTH, X_IROTH, + S_IWOTH, X_IWOTH, + S_IXOTH, X_IXOTH +}; diff --git a/src/lib/libast/string/modei.c b/src/lib/libast/string/modei.c new file mode 100644 index 0000000..163d633 --- /dev/null +++ b/src/lib/libast/string/modei.c @@ -0,0 +1,55 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * mode_t representation support + */ + +#include "modelib.h" + +/* + * convert external mode to internal + * + * NOTE: X_IFMT ignored + */ + +#undef modei + +int +modei(register int x) +{ +#if _S_IDPERM + return(x & X_IPERM); +#else + register int i; + register int c; + + i = 0; + for (c = 0; c < PERMLEN; c += 2) + if (x & permmap[c + 1]) + i |= permmap[c]; + return(i); +#endif +} diff --git a/src/lib/libast/string/modelib.h b/src/lib/libast/string/modelib.h new file mode 100644 index 0000000..e654240 --- /dev/null +++ b/src/lib/libast/string/modelib.h @@ -0,0 +1,55 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * mode_t common definitions + */ + +#ifndef _MODELIB_H +#define _MODELIB_H + +#include <ast.h> +#include <ls.h> +#include <modex.h> + +#define MODELEN 10 +#define PERMLEN 24 + +#define modetab _mode_table_ /* data hiding */ +#define permmap _mode_permmap_ /* data hiding */ + +struct modeop /* ops for each char in mode string */ +{ + int mask1; /* first mask */ + int shift1; /* first shift count */ + int mask2; /* second mask */ + int shift2; /* second shift count */ + char* name; /* mode char using mask/shift as index */ +}; + +extern struct modeop modetab[]; +extern int permmap[]; + +#endif diff --git a/src/lib/libast/string/modex.c b/src/lib/libast/string/modex.c new file mode 100644 index 0000000..a5e501a --- /dev/null +++ b/src/lib/libast/string/modex.c @@ -0,0 +1,75 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * mode_t representation support + */ + +#include "modelib.h" + +/* + * convert internal mode to external + */ + +#undef modex + +int +modex(register int i) +{ +#if _S_IDPERM && _S_IDTYPE + return(i); +#else + register int x; + register int c; + + x = 0; +#if _S_IDPERM + x |= (i & 07777); +#else + for (c = 0; c < PERMLEN; c++) + if (i & permmap[c++]) + x |= permmap[c]; +#endif +#if _S_IDTYPE + x |= (i & X_IFMT); +#else + if (S_ISREG(i)) x |= X_IFREG; + else if (S_ISDIR(i)) x |= X_IFDIR; +#ifdef S_ISLNK + else if (S_ISLNK(i)) x |= X_IFLNK; +#endif + else if (S_ISBLK(i)) x |= X_IFBLK; + else if (S_ISCHR(i)) x |= X_IFCHR; +#ifdef S_ISCTG + else if (S_ISCTG(i)) x |= X_IFCTG; +#endif + else if (S_ISFIFO(i)) x |= X_IFIFO; +#ifdef S_ISSOCK + else if (S_ISSOCK(i)) x |= X_IFSOCK; +#endif +#endif + return(x); +#endif +} diff --git a/src/lib/libast/string/stracmp.c b/src/lib/libast/string/stracmp.c new file mode 100644 index 0000000..05b5401 --- /dev/null +++ b/src/lib/libast/string/stracmp.c @@ -0,0 +1,62 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * ccmapchr(ccmap(CC_NATIVE,CC_ASCII),c) and strcmp + */ + +#include <ast.h> +#include <ccode.h> + +#if _lib_stracmp + +NoN(stracmp) + +#else + +#include <ctype.h> + +int +stracmp(const char* aa, const char* ab) +{ + register unsigned char* a; + register unsigned char* b; + register unsigned char* m; + register int c; + register int d; + + if (!(m = ccmap(CC_NATIVE, CC_ASCII))) + return strcmp(aa, ab); + a = (unsigned char*)aa; + b = (unsigned char*)ab; + for (;;) + { + c = m[*a++]; + if (d = c - m[*b++]) + return d; + if (!c) + return 0; + } +} + +#endif diff --git a/src/lib/libast/string/strcopy.c b/src/lib/libast/string/strcopy.c new file mode 100644 index 0000000..de169e7 --- /dev/null +++ b/src/lib/libast/string/strcopy.c @@ -0,0 +1,36 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> + +/* + * copy t into s, return a pointer to the end of s ('\0') + */ + +char* +strcopy(register char* s, register const char* t) +{ + if (!t) return(s); + while (*s++ = *t++); + return(--s); +} diff --git a/src/lib/libast/string/strdup.c b/src/lib/libast/string/strdup.c new file mode 100644 index 0000000..7237fb7 --- /dev/null +++ b/src/lib/libast/string/strdup.c @@ -0,0 +1,60 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#undef VMDEBUG +#define VMDEBUG 0 + +#if defined(_MSVCRT_H) +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:hide strdup +#else +#define strdup ______strdup +#endif +#endif + +#include <ast.h> + +#if defined(_MSVCRT_H) +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:nohide strdup +#else +#undef strdup +#endif +#endif + +/* + * return a copy of s using malloc + */ + +#if defined(__EXPORT__) +#define extern __EXPORT__ +#endif + +extern char* +strdup(register const char* s) +{ + register char* t; + register int n; + + return (s && (t = newof(0, char, n = strlen(s) + 1, 0))) ? (char*)memcpy(t, s, n) : (char*)0; +} diff --git a/src/lib/libast/string/strelapsed.c b/src/lib/libast/string/strelapsed.c new file mode 100644 index 0000000..fa7d919 --- /dev/null +++ b/src/lib/libast/string/strelapsed.c @@ -0,0 +1,154 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * parse elapsed time in 1/n secs from s + * compatible with fmtelapsed() + * also handles ps [day-][hour:]min:sec + * also handles coshell % for 'infinity' + * if e!=0 then it is set to first unrecognized char + */ + +#include <ast.h> +#include <ctype.h> + +unsigned long +strelapsed(register const char* s, char** e, int n) +{ + register int c; + register unsigned long v; + unsigned long t = 0; + int f = 0; + int p = 0; + int z = 1; + int m; + const char* last; + + for (;;) + { + while (isspace(*s) || *s == '_') + s++; + if (!*(last = s)) + break; + if (z) + { + z = 0; + if (*s == '0' && (!(c = *(s + 1)) || isspace(c) || c == '_')) + { + last = s + 1; + break; + } + } + v = 0; + while ((c = *s++) >= '0' && c <= '9') + v = v * 10 + c - '0'; + v *= n; + if (c == '.') + for (m = n; (c = *s++) >= '0' && c <= '9';) + f += (m /= 10) * (c - '0'); + if (c == '%') + { + t = ~t; + last = s; + break; + } + if (s == last + 1) + break; + if (!p) + while (isspace(c) || c == '_') + c = *s++; + switch (c) + { + case 'S': + if (*s == 'E' || *s == 'e') + { + v += f; + f = 0; + } + else + v *= 20 * 12 * 4 * 7 * 24 * 60 * 60; + break; + case 'y': + case 'Y': + v *= 12 * 4 * 7 * 24 * 60 * 60; + break; + case 'M': + if (*s == 'I' || *s == 'i') + v *= 60; + else + v *= 4 * 7 * 24 * 60 * 60; + break; + case 'w': + v *= 7 * 24 * 60 * 60; + break; + case '-': + p = 1; + /*FALLTHROUGH*/ + case 'd': + v *= 24 * 60 * 60; + break; + case 'h': + v *= 60 * 60; + break; + case ':': + p = 1; + v *= strchr(s, ':') ? (60 * 60) : 60; + break; + case 'm': + if (*s == 'o') + v *= 4 * 7 * 24 * 60 * 60; + else + v *= 60; + break; + case 's': + if (*s == 'c') + { + v *= 20 * 12 * 4 * 7 * 24 * 60 * 60; + break; + } + v += f; + f = 0; + break; + case 0: + s--; + v += f; + break; + default: + if (p) + { + last = s - 1; + t += v + f; + } + goto done; + } + t += v; + while (isalpha(*s)) + s++; + } + done: + if (e) + *e = (char*)last; + return t; +} diff --git a/src/lib/libast/string/strerror.c b/src/lib/libast/string/strerror.c new file mode 100644 index 0000000..1664126 --- /dev/null +++ b/src/lib/libast/string/strerror.c @@ -0,0 +1,148 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * Glenn Fowler + * AT&T Research + * + * return error message string given errno + */ + +#include "lclib.h" + +#include "FEATURE/errno" + +#undef strerror + +#if !defined(sys_errlist) && !_def_errno_sys_errlist +#if _dat_sys_errlist +extern char* sys_errlist[]; +#else +#undef _dat_sys_nerr +char* sys_errlist[] = { 0 }; +#endif +#endif + +#if !defined(sys_nerr) && !_def_errno_sys_nerr +#if _dat_sys_nerr +extern int sys_nerr; +#else +#undef _dat_sys_nerr +int sys_nerr = 0; +#endif +#endif + +#if _lib_strerror +extern char* strerror(int); +#endif + +#if _PACKAGE_astsa + +#define fmtbuf(n) ((n),tmp) + +static char tmp[32]; + +#endif + +char* +_ast_strerror(int err) +{ + char* msg; + int z; + +#if _lib_strerror + z = errno; + msg = strerror(err); + errno = z; +#else + if (err > 0 && err <= sys_nerr) + msg = (char*)sys_errlist[err]; + else + msg = 0; +#endif + if (msg) + { +#if !_PACKAGE_astsa + if (ERROR_translating()) + { +#if _lib_strerror + static int sys; + + if (!sys) + { + char* s; + char* t; + char* p; + +#if _lib_strerror + /* + * stash the pending strerror() msg + */ + + msg = strcpy(fmtbuf(strlen(msg) + 1), msg); +#endif + + /* + * make sure that strerror() translates + */ + + if (!(s = strerror(1))) + sys = -1; + else + { + t = fmtbuf(z = strlen(s) + 1); + strcpy(t, s); + ast.locale.set |= AST_LC_internal; + p = setlocale(LC_MESSAGES, NiL); + setlocale(LC_MESSAGES, "C"); + sys = (s = strerror(1)) && strcmp(s, t) ? 1 : -1; + setlocale(LC_MESSAGES, p); + ast.locale.set &= ~AST_LC_internal; + } + } + if (sys > 0) + return msg; +#endif + return ERROR_translate(NiL, NiL, "errlist", msg); + } +#endif + return msg; + } + msg = fmtbuf(z = 32); + sfsprintf(msg, z, ERROR_translate(NiL, NiL, "errlist", "Error %d"), err); + return msg; +} + +#if !_lib_strerror + +#if defined(__EXPORT__) +#define extern __EXPORT__ +#endif + +extern char* +strerror(int err) +{ + return _ast_strerror(err); +} + +#endif diff --git a/src/lib/libast/string/stresc.c b/src/lib/libast/string/stresc.c new file mode 100644 index 0000000..59ec6c9 --- /dev/null +++ b/src/lib/libast/string/stresc.c @@ -0,0 +1,67 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * convert \X character constants in s in place + * the length of the converted s is returned (may have embedded \0's) + * wide chars absent locale guidance default to UTF-8 + * strexp() FMT_EXP_* flags passed to chrexp() for selective conversion + */ + +#include <ast.h> + +int +strexp(register char* s, int flags) +{ + register char* t; + register unsigned int c; + char* b; + char* e; + int w; + + b = t = s; + while (c = *s++) + { + if (c == '\\') + { + c = chrexp(s - 1, &e, &w, flags); + s = e; + if (w) + { + t += mbwide() ? mbconv(t, c) : wc2utf8(t, c); + continue; + } + } + *t++ = c; + } + *t = 0; + return t - b; +} + +int +stresc(register char* s) +{ + return strexp(s, FMT_EXP_CHAR|FMT_EXP_LINE|FMT_EXP_WIDE); +} diff --git a/src/lib/libast/string/streval.c b/src/lib/libast/string/streval.c new file mode 100644 index 0000000..d246184 --- /dev/null +++ b/src/lib/libast/string/streval.c @@ -0,0 +1,50 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * obsolete streval() interface to strexpr() + */ + +#include <ast.h> + +typedef long (*Old_convert_t)(const char*, char**); + +typedef long (*Convert_t)(const char*, char**, void*); + +typedef struct +{ + Old_convert_t convert; +} Handle_t; + +static long +userconv(const char* s, char** end, void* handle) +{ + return((*((Handle_t*)handle)->convert)(s, end)); +} + +long +streval(const char* s, char** end, Old_convert_t convert) +{ + Handle_t handle; + + return((handle.convert = convert) ? strexpr(s, end, userconv, &handle) : strexpr(s, end, (Convert_t)0, NiL)); +} diff --git a/src/lib/libast/string/strexpr.c b/src/lib/libast/string/strexpr.c new file mode 100644 index 0000000..23be2f5 --- /dev/null +++ b/src/lib/libast/string/strexpr.c @@ -0,0 +1,294 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * G. S. Fowler + * D. G. Korn + * AT&T Bell Laboratories + * + * long integer arithmetic expression evaluator + * long constants may be represented as: + * + * 0ooo octal + * 0[xX]hhh hexadecimal + * ddd decimal + * n#ccc base n, 2 <= b <= 36 + * + * NOTE: all operands are evaluated as both the parse + * and evaluation are done on the fly + */ + +#include <ast.h> +#include <ctype.h> + +#define getchr(ex) (*(ex)->nextchr++) +#define peekchr(ex) (*(ex)->nextchr) +#define ungetchr(ex) ((ex)->nextchr--) + +#define error(ex,msg) return(seterror(ex,msg)) + +typedef struct /* expression handle */ +{ + char* nextchr; /* next expression char */ + char* errchr; /* next char after error */ + char* errmsg; /* error message text */ + long (*convert)(const char*, char**, void*); + void* handle; /* user convert handle */ +} Expr_t; + +/* + * set error message string + */ + +static long +seterror(register Expr_t* ex, char* msg) +{ + if (!ex->errmsg) ex->errmsg = msg; + ex->errchr = ex->nextchr; + ex->nextchr = ""; + return(0); +} + +/* + * evaluate a subexpression with precedence + */ + +static long +expr(register Expr_t* ex, register int precedence) +{ + register int c; + register long n; + register long x; + char* pos; + int operand = 1; + + while (c = getchr(ex), isspace(c)); + switch (c) + { + case 0: + ungetchr(ex); + if (!precedence) return(0); + error(ex, "more tokens expected"); + case '-': + n = -expr(ex, 13); + break; + case '+': + n = expr(ex, 13); + break; + case '!': + n = !expr(ex, 13); + break; + case '~': + n = ~expr(ex, 13); + break; + default: + ungetchr(ex); + n = 0; + operand = 0; + break; + } + for (;;) + { + switch (c = getchr(ex)) + { + case 0: + goto done; + case ')': + if (!precedence) error(ex, "too many )'s"); + goto done; + case '(': + n = expr(ex, 1); + if (getchr(ex) != ')') + { + ungetchr(ex); + error(ex, "closing ) expected"); + } + gotoperand: + if (operand) error(ex, "operator expected"); + operand = 1; + continue; + case '?': + if (precedence > 1) goto done; + if (peekchr(ex) == ':') + { + getchr(ex); + x = expr(ex, 2); + if (!n) n = x; + } + else + { + x = expr(ex, 2); + if (getchr(ex) != ':') + { + ungetchr(ex); + error(ex, ": expected for ? operator"); + } + if (n) + { + n = x; + expr(ex, 2); + } + else n = expr(ex, 2); + } + break; + case ':': + goto done; + case '|': + if (peekchr(ex) == '|') + { + if (precedence > 2) goto done; + getchr(ex); + x = expr(ex, 3); + n = n || x; + } + else + { + if (precedence > 4) goto done; + x = expr(ex, 5); + n |= x; + } + break; + case '^': + if (precedence > 5) goto done; + x = expr(ex, 6); + n ^= x; + break; + case '&': + if (peekchr(ex) == '&') + { + if (precedence > 3) goto done; + getchr(ex); + x = expr(ex, 4); + n = n && x; + } + else + { + if (precedence > 6) goto done; + x = expr(ex, 7); + n &= x; + } + break; + case '=': + case '!': + if (peekchr(ex) != '=') error(ex, "operator syntax error"); + if (precedence > 7) goto done; + getchr(ex); + x = expr(ex, 8); + if (c == '=') n = n == x; + else n = n != x; + break; + case '<': + case '>': + if (peekchr(ex) == c) + { + if (precedence > 9) goto done; + getchr(ex); + x = expr(ex, 10); + if (c == '<') n <<= x; + else n >>= x; + } + else + { + if (precedence > 8) goto done; + if (peekchr(ex) == '=') + { + getchr(ex); + x = expr(ex, 9); + if (c == '<') n = n <= x; + else n = n >= x; + } + else + { + x = expr(ex, 9); + if (c == '<') n = n < x; + else n = n > x; + } + } + break; + case '+': + case '-': + if (precedence > 10) goto done; + x = expr(ex, 11); + if (c == '+') n += x; + else n -= x; + break; + case '*': + case '/': + case '%': + if (precedence > 11) goto done; + x = expr(ex, 12); + if (c == '*') n *= x; + else if (x == 0) error(ex, "divide by zero"); + else if (c == '/') n /= x; + else n %= x; + break; + default: + if (isspace(c)) continue; + pos = --ex->nextchr; + if (isdigit(c)) n = strton(ex->nextchr, &ex->nextchr, NiL, 0); + else if (ex->convert) n = (*ex->convert)(ex->nextchr, &ex->nextchr, ex->handle); + if (ex->nextchr == pos) error(ex, "syntax error"); + goto gotoperand; + } + if (ex->errmsg) return(0); + if (!operand) error(ex, "operand expected"); + } + done: + ungetchr(ex); + if (!operand) error(ex, "operand expected"); + return(n); +} + +/* + * evaluate an integer arithmetic expression in s + * + * (long)(*convert)(const char* string, char** end, void* handle) + * is a user supplied conversion routine that is called when unknown + * chars are encountered; string points to the part to be + * converted and end is adjusted to point to the next non-converted + * character; if string is 0 then end points to an error message string + * + * NOTE: (*convert)() may call strexpr(ex, ) + */ + +long +strexpr(const char* s, char** end, long(*convert)(const char*, char**, void*), void* handle) +{ + long n; + Expr_t ex; + + ex.nextchr = (char*)s; + ex.errmsg = 0; + ex.convert = convert; + ex.handle = handle; + n = expr(&ex, 0); + if (peekchr(&ex) == ':') + seterror(&ex, "invalid use of :"); + if (ex.errmsg) + { + if (convert) (*convert)(NiL, &ex.errmsg, handle); + ex.nextchr = ex.errchr; + n = 0; + } + if (end) *end = ex.nextchr; + return(n); +} diff --git a/src/lib/libast/string/strgid.c b/src/lib/libast/string/strgid.c new file mode 100644 index 0000000..55ebe23 --- /dev/null +++ b/src/lib/libast/string/strgid.c @@ -0,0 +1,121 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * gid name -> number + */ + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:hide getgrgid getgrnam getpwnam +#else +#define getgrgid ______getgrgid +#define getgrnam ______getgrnam +#define getpwnam ______getpwnam +#endif + +#include <ast.h> +#include <cdt.h> +#include <pwd.h> +#include <grp.h> + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:nohide getgrgid getgrnam getpwnam +#else +#undef getgrgid +#undef getgrnam +#undef getpwnam +#endif + +extern struct group* getgrgid(gid_t); +extern struct group* getgrnam(const char*); +extern struct passwd* getpwnam(const char*); + +typedef struct Id_s +{ + Dtlink_t link; + int id; + char name[1]; +} Id_t; + +/* + * return gid number given gid/uid name + * gid attempted first, then uid->pw_gid + * -1 on first error for a given name + * -2 on subsequent errors for a given name + */ + +int +strgid(const char* name) +{ + register Id_t* ip; + register struct group* gr; + register struct passwd* pw; + int id; + char* e; + + static Dt_t* dict; + static Dtdisc_t disc; + + if (!dict) + { + disc.key = offsetof(Id_t, name); + dict = dtopen(&disc, Dtset); + } + else if (ip = (Id_t*)dtmatch(dict, name)) + return ip->id; + if (gr = getgrnam(name)) + id = gr->gr_gid; + else if (pw = getpwnam(name)) + id = pw->pw_gid; + else + { + id = strtol(name, &e, 0); +#if _WINIX + if (!*e) + { + if (!getgrgid(id)) + id = -1; + } + else if (!streq(name, "sys")) + id = -1; + else if (gr = getgrnam("Administrators")) + id = gr->gr_gid; + else if (pw = getpwnam("Administrator")) + id = pw->pw_gid; + else + id = -1; +#else + if (*e || !getgrgid(id)) + id = -1; +#endif + } + if (dict && (ip = newof(0, Id_t, 1, strlen(name)))) + { + strcpy(ip->name, name); + ip->id = id >= 0 ? id : -2; + dtinsert(dict, ip); + } + return id; +} diff --git a/src/lib/libast/string/strlcat.c b/src/lib/libast/string/strlcat.c new file mode 100644 index 0000000..f5f17af --- /dev/null +++ b/src/lib/libast/string/strlcat.c @@ -0,0 +1,83 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * strlcat implementation + */ + +#define strlcat ______strlcat + +#include <ast.h> + +#undef strlcat + +#undef _def_map_ast +#include <ast_map.h> + +#if _lib_strlcat + +NoN(strlcat) + +#else + +/* + * append t onto s limiting total size of s to n + * s 0 terminated if n>0 + * min(n,strlen(s))+strlen(t) returned + */ + +#if defined(__EXPORT__) +#define extern __EXPORT__ +#endif + +extern size_t +strlcat(register char* s, register const char* t, register size_t n) +{ + register size_t m; + const char* o = t; + + if (m = n) + { + while (n && *s) + { + n--; + s++; + } + m -= n; + if (n) + do + { + if (!--n) + { + *s = 0; + break; + } + } while (*s++ = *t++); + else + *s = 0; + } + if (!n) + while (*t++); + return (t - o) + m - 1; +} + +#endif diff --git a/src/lib/libast/string/strlcpy.c b/src/lib/libast/string/strlcpy.c new file mode 100644 index 0000000..8410802 --- /dev/null +++ b/src/lib/libast/string/strlcpy.c @@ -0,0 +1,71 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * strlcpy implementation + */ + +#define strlcpy ______strlcpy + +#include <ast.h> + +#undef strlcpy + +#undef _def_map_ast +#include <ast_map.h> + +#if _lib_strlcpy + +NoN(strlcpy) + +#else + +/* + * copy at most n chars from t into s + * result 0 terminated if n>0 + * strlen(t) returned + */ + +#if defined(__EXPORT__) +#define extern __EXPORT__ +#endif + +extern size_t +strlcpy(register char* s, register const char* t, register size_t n) +{ + const char* o = t; + + if (n) + do + { + if (!--n) + { + *s = 0; + break; + } + } while (*s++ = *t++); + if (!n) + while (*t++); + return t - o - 1; +} + +#endif diff --git a/src/lib/libast/string/strlook.c b/src/lib/libast/string/strlook.c new file mode 100644 index 0000000..1868697 --- /dev/null +++ b/src/lib/libast/string/strlook.c @@ -0,0 +1,50 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + */ + +#include <ast.h> + +/* + * return pointer to name in tab with element size siz + * where the first member of each element is a char* + * + * the last name in tab must be 0 + * + * 0 returned if name not found + */ + +void* +strlook(const void* tab, size_t siz, register const char* name) +{ + register char* t = (char*)tab; + register char* s; + register int c = *name; + + for (; s = *((char**)t); t += siz) + if (*s == c && !strcmp(s, name)) + return (void*)t; + return 0; +} diff --git a/src/lib/libast/string/strmatch.c b/src/lib/libast/string/strmatch.c new file mode 100644 index 0000000..4114ee4 --- /dev/null +++ b/src/lib/libast/string/strmatch.c @@ -0,0 +1,171 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * D. G. Korn + * G. S. Fowler + * AT&T Research + * + * match shell file patterns + * this interface is a wrapper on regex + * + * sh pattern egrep RE description + * ---------- -------- ----------- + * * .* 0 or more chars + * ? . any single char + * [.] [.] char class + * [!.] [^.] negated char class + * [[:.:]] [[:.:]] ctype class + * [[=.=]] [[=.=]] equivalence class + * [[...]] [[...]] collation element + * *(.) (.)* 0 or more of + * +(.) (.)+ 1 or more of + * ?(.) (.)? 0 or 1 of + * (.) (.) 1 of + * @(.) (.) 1 of + * a|b a|b a or b + * \# () subgroup back reference [1-9] + * a&b a and b + * !(.) none of + * + * \ used to escape metacharacters + * + * *, ?, (, |, &, ), [, \ must be \'d outside of [...] + * only ] must be \'d inside [...] + * + */ + +#include <ast.h> +#include <regex.h> + +static struct State_s +{ + regmatch_t* match; + int nmatch; +} matchstate; + +/* + * subgroup match + * 0 returned if no match + * otherwise number of subgroups matched returned + * match group begin offsets are even elements of sub + * match group end offsets are odd elements of sub + * the matched string is from s+sub[0] up to but not + * including s+sub[1] + */ + +int +strgrpmatch(const char* b, const char* p, int* sub, int n, register int flags) +{ + register regex_t* re; + register int* end; + register int i; + register regflags_t reflags; + + /* + * 0 and empty patterns are special + */ + + if (!p || !b) + { + if (!p && !b) + regcache(NiL, 0, NiL); + return 0; + } + if (!*p) + { + if (sub && n > 0) + sub[0] = sub[1] = 0; + return *b == 0; + } + + /* + * convert flags + */ + + if (flags & REG_ADVANCE) + reflags = flags & ~REG_ADVANCE; + else + { + reflags = REG_SHELL|REG_AUGMENTED; + if (!(flags & STR_MAXIMAL)) + reflags |= REG_MINIMAL; + if (flags & STR_GROUP) + reflags |= REG_SHELL_GROUP; + if (flags & STR_LEFT) + reflags |= REG_LEFT; + if (flags & STR_RIGHT) + reflags |= REG_RIGHT; + if (flags & STR_ICASE) + reflags |= REG_ICASE; + } + if (!sub || n <= 0) + reflags |= REG_NOSUB; + if (!(re = regcache(p, reflags, NiL))) + return 0; + if (n > matchstate.nmatch) + { + if (!(matchstate.match = newof(matchstate.match, regmatch_t, n, 0))) + return 0; + matchstate.nmatch = n; + } + if (regexec(re, b, n, matchstate.match, reflags & ~(REG_MINIMAL|REG_SHELL_GROUP|REG_LEFT|REG_RIGHT|REG_ICASE))) + return 0; + if (!sub || n <= 0) + return 1; + i = re->re_nsub; + end = sub + n * 2; + for (n = 0; sub < end && n <= i; n++) + { + *sub++ = matchstate.match[n].rm_so; + *sub++ = matchstate.match[n].rm_eo; + } + return i + 1; +} + +/* + * compare the string s with the shell pattern p + * returns 1 for match 0 otherwise + */ + +int +strmatch(const char* s, const char* p) +{ + return strgrpmatch(s, p, NiL, 0, STR_MAXIMAL|STR_LEFT|STR_RIGHT); +} + +/* + * leading substring match + * first char after end of substring returned + * 0 returned if no match + * + * OBSOLETE: use strgrpmatch() + */ + +char* +strsubmatch(const char* s, const char* p, int flags) +{ + int match[2]; + + return strgrpmatch(s, p, match, 1, (flags ? STR_MAXIMAL : 0)|STR_LEFT) ? (char*)s + match[1] : (char*)0; +} diff --git a/src/lib/libast/string/strmode.c b/src/lib/libast/string/strmode.c new file mode 100644 index 0000000..8ec8749 --- /dev/null +++ b/src/lib/libast/string/strmode.c @@ -0,0 +1,51 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * G. S. Fowler + * AT&T Bell Laboratories + * + * return modex canonical representation of file mode bits + * given ls -l style file mode string + */ + +#include "modelib.h" + +int +strmode(register const char* s) +{ + register int c; + register char* t; + register struct modeop* p; + int mode; + + mode = 0; + for (p = modetab; (c = *s++) && p < &modetab[MODELEN]; p++) + for (t = p->name; *t; t++) + if (*t == c) + { + c = t - p->name; + mode |= (p->mask1 & (c << p->shift1)) | (p->mask2 & (c << p->shift2)); + break; + } + return(mode); +} diff --git a/src/lib/libast/string/strnacmp.c b/src/lib/libast/string/strnacmp.c new file mode 100644 index 0000000..4a6c9c6 --- /dev/null +++ b/src/lib/libast/string/strnacmp.c @@ -0,0 +1,66 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +/* + * ccmapc(c, CC_NATIVE, CC_ASCII) and strncmp + */ + +#include <ast.h> +#include <ccode.h> + +#if _lib_strnacmp + +NoN(strnacmp) + +#else + +#include <ctype.h> + +int +strnacmp(const char* a, const char* b, size_t n) +{ +#if CC_NATIVE == CC_ASCII + return strncmp(a, b, n); +#else + register unsigned char* ua = (unsigned char*)a; + register unsigned char* ub = (unsigned char*)b; + register unsigned char* ue; + register unsigned char* m; + register int c; + register int d; + + m = ccmap(CC_NATIVE, CC_ASCII); + ue = ua + n; + while (ua < ue) + { + c = m[*ua++]; + if (d = c - m[*ub++]) + return d; + if (!c) + return 0; + } + return 0; +#endif +} + +#endif diff --git a/src/lib/libast/string/strncopy.c b/src/lib/libast/string/strncopy.c new file mode 100644 index 0000000..65752ac --- /dev/null +++ b/src/lib/libast/string/strncopy.c @@ -0,0 +1,46 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> + +/* + * copy up to n bytes of string f into t + * trailing 0 always added to t, even if n==0 + * pointer to the copied 0 returned + */ + +char* +strncopy(register char* t, register const char* f, size_t n) +{ + register char* e = t + n - 1; + + do + { + if (t >= e) + { + *t = 0; + return t; + } + } while (*t++ = *f++); + return t - 1; +} diff --git a/src/lib/libast/string/strnpcmp.c b/src/lib/libast/string/strnpcmp.c new file mode 100644 index 0000000..e8a4172 --- /dev/null +++ b/src/lib/libast/string/strnpcmp.c @@ -0,0 +1,51 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> + +/* + * path prefix strncmp(3) -- longest first! + */ + +int +strnpcmp(register const char* a, register const char* b, size_t n) +{ + register const char* e; + + e = a + n; + for (;;) + { + if (a >= e) + return 0; + if (*a != *b) + break; + if (!*a++) + return 0; + b++; + } + if (*a == 0 && *b == '/') + return 1; + if (*a == '/' && *b == 0) + return -1; + return (a < b) ? -1 : 1; +} diff --git a/src/lib/libast/string/strntod.c b/src/lib/libast/string/strntod.c new file mode 100644 index 0000000..e04969b --- /dev/null +++ b/src/lib/libast/string/strntod.c @@ -0,0 +1,32 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * strntod() implementation + */ + +#include <ast.h> + +#define S2F_function strntod +#define S2F_type 1 +#define S2F_size 1 + +#include "sfstrtof.h" diff --git a/src/lib/libast/string/strntol.c b/src/lib/libast/string/strntol.c new file mode 100644 index 0000000..e71de8e --- /dev/null +++ b/src/lib/libast/string/strntol.c @@ -0,0 +1,31 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * strntol() implementation + */ + +#define S2I_function strntol +#define S2I_number long +#define S2I_unumber unsigned long +#define S2I_size 1 + +#include "strtoi.h" diff --git a/src/lib/libast/string/strntold.c b/src/lib/libast/string/strntold.c new file mode 100644 index 0000000..1b791e2 --- /dev/null +++ b/src/lib/libast/string/strntold.c @@ -0,0 +1,32 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * strntold() implementation + */ + +#include <ast.h> + +#define S2F_function strntold +#define S2F_type 2 +#define S2F_size 1 + +#include "sfstrtof.h" diff --git a/src/lib/libast/string/strntoll.c b/src/lib/libast/string/strntoll.c new file mode 100644 index 0000000..7eb4772 --- /dev/null +++ b/src/lib/libast/string/strntoll.c @@ -0,0 +1,31 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * strntoll() implementation + */ + +#define S2I_function strntoll +#define S2I_number intmax_t +#define S2I_unumber uintmax_t +#define S2I_size 1 + +#include "strtoi.h" diff --git a/src/lib/libast/string/strnton.c b/src/lib/libast/string/strnton.c new file mode 100644 index 0000000..5d6e1bf --- /dev/null +++ b/src/lib/libast/string/strnton.c @@ -0,0 +1,32 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * strnton() implementation + */ + +#define S2I_function strnton +#define S2I_number long +#define S2I_unumber unsigned long +#define S2I_multiplier 1 +#define S2I_size 1 + +#include "strtoi.h" diff --git a/src/lib/libast/string/strntonll.c b/src/lib/libast/string/strntonll.c new file mode 100644 index 0000000..d55b335 --- /dev/null +++ b/src/lib/libast/string/strntonll.c @@ -0,0 +1,32 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * strntonll() implementation + */ + +#define S2I_function strntonll +#define S2I_number intmax_t +#define S2I_unumber uintmax_t +#define S2I_multiplier 1 +#define S2I_size 1 + +#include "strtoi.h" diff --git a/src/lib/libast/string/strntoul.c b/src/lib/libast/string/strntoul.c new file mode 100644 index 0000000..f688985 --- /dev/null +++ b/src/lib/libast/string/strntoul.c @@ -0,0 +1,32 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * strntoul() implementation + */ + +#define S2I_function strntoul +#define S2I_number long +#define S2I_unumber unsigned long +#define S2I_size 1 +#define S2I_unsigned 1 + +#include "strtoi.h" diff --git a/src/lib/libast/string/strntoull.c b/src/lib/libast/string/strntoull.c new file mode 100644 index 0000000..9eae4f1 --- /dev/null +++ b/src/lib/libast/string/strntoull.c @@ -0,0 +1,32 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * strntoull() implementation + */ + +#define S2I_function strntoull +#define S2I_number intmax_t +#define S2I_unumber uintmax_t +#define S2I_size 1 +#define S2I_unsigned 1 + +#include "strtoi.h" diff --git a/src/lib/libast/string/strnvcmp.c b/src/lib/libast/string/strnvcmp.c new file mode 100644 index 0000000..41d2414 --- /dev/null +++ b/src/lib/libast/string/strnvcmp.c @@ -0,0 +1,86 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> +#include <ctype.h> + +/* + * version strncmp(3) + */ + +int +strnvcmp(register const char* a, register const char* b, size_t n) +{ + register const char* ae; + register const char* be; + register unsigned long na; + register unsigned long nb; + + ae = a + n; + be = b + n; + for (;;) + { + if (a >= ae) + { + if (b >= be) + return 0; + return 1; + } + else if (b >= be) + return -1; + if (isdigit(*a) && isdigit(*b)) + { + na = nb = 0; + while (a < ae && isdigit(*a)) + na = na * 10 + *a++ - '0'; + while (b < be && isdigit(*b)) + nb = nb * 10 + *b++ - '0'; + if (na < nb) + return -1; + if (na > nb) + return 1; + } + else if (*a != *b) + break; + else if (!*a) + return 0; + else + { + a++; + b++; + } + } + if (*a == 0) + return -1; + if (*b == 0) + return 1; + if (*a == '.') + return -1; + if (*b == '.') + return 1; + if (*a == '-') + return -1; + if (*b == '-') + return 1; + return *a < *b ? -1 : 1; +} diff --git a/src/lib/libast/string/stropt.c b/src/lib/libast/string/stropt.c new file mode 100644 index 0000000..91bd351 --- /dev/null +++ b/src/lib/libast/string/stropt.c @@ -0,0 +1,188 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + */ + +#include <ast.h> +#include <ctype.h> + +/* + * parse option expression in s using options in tab with element size siz + * first element in tab must be a char* + * options match + * + * [no]name[[:]=['"]value["']][, ]... + * + * f is called for each option + * + * (*f)(void* a, char* p, int n, char* v) + * + * a from stropt + * p matching tab entry, or name if no table + * n 0 if option had ``no'' prefix, -1 if :=, 1 otherwise + * v option value pointer + * + * for unmatched options p is 0 and v is the offending option + * + * names in s may be shorter than tab names + * longer names must have a prefix that matches a tab name + * the first match is returned + * \ escapes value using chresc() + */ + +int +stropt(const char* as, const void* tab, int siz, int(*f)(void*, const void*, int, const char*), void* a) +{ + register int c; + register char* s; + register char* v; + register char* t; + char** p; + char* u; + char* x; + char* e; + int n; + int ql; + int qr; + int qc; + + if (!as) n = 0; + else if (!(x = s = strdup(as))) n = -1; + else + { + for (;;) + { + while (isspace(*s) || *s == ',') s++; + if (*s == 'n' && *(s + 1) == 'o') + { + s += 2; + n = 0; + } + else n = 1; + if (!*s) + { + n = 0; + break; + } + if (tab) + { + for (p = (char**)tab; t = *p; p = (char**)((char*)p + siz)) + { + for (v = s; *t && *t++ == *v; v++); + if (!*t || isspace(*v) || *v == ',' || *v == '=') + break; + if (*v == ':' && *(v + 1) == '=') + { + v++; + n = -1; + break; + } + } + if (!t) + { + u = v = s; + p = 0; + } + } + else + { + p = (char**)(v = s); + t = 0; + } + while (*v && !isspace(*v) && *v != '=' && *v != ',') + if (*v++ == ':' && *v == '=') + { + if (!t) + *(v - 1) = 0; + n = -n; + break; + } + if (*v == '=') + { + if (!t) + *v = 0; + t = s = ++v; + ql = qr = 0; + while (c = *s++) + { + if (c == '\\') + { + *t++ = chresc(s - 1, &e); + s = e; + } + else if (c == qr) + { + if (qr != ql) + *t++ = c; + if (--qc <= 0) + qr = ql = 0; + } + else if (c == ql) + { + *t++ = c; + qc++; + } + else if (qr) + *t++ = c; + else if (c == ',' || isspace(c)) + break; + else if (c == '"' || c == '\'') + { + ql = qr = c; + qc = 1; + } + else + { + *t++ = c; + if (c == '{') + { + ql = c; + qr = '}'; + qc = 1; + } + else if (c == '(') + { + ql = c; + qr = ')'; + qc = 1; + } + } + } + *t = 0; + } + else + { + s = v; + c = *s; + *s++ = 0; + } + n = p ? (*f)(a, p, n, v) : (*f)(a, p, v - u, u); + if (n || !c) + break; + } + free(x); + } + return n; +} diff --git a/src/lib/libast/string/strpcmp.c b/src/lib/libast/string/strpcmp.c new file mode 100644 index 0000000..2c83d71 --- /dev/null +++ b/src/lib/libast/string/strpcmp.c @@ -0,0 +1,44 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> + +/* + * path prefix strcmp(3) -- longest first! + */ + +int +strpcmp(register const char* a, register const char* b) +{ + while (*a == *b) + { + if (!*a++) + return 0; + b++; + } + if (*a == 0 && *b == '/') + return 1; + if (*a == '/' && *b == 0) + return -1; + return (a < b) ? -1 : 1; +} diff --git a/src/lib/libast/string/strperm.c b/src/lib/libast/string/strperm.c new file mode 100644 index 0000000..455d232 --- /dev/null +++ b/src/lib/libast/string/strperm.c @@ -0,0 +1,267 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * apply file permission expression expr to perm + * + * each expression term must match + * + * [ugoa]*[-&+|^=]?[rwxst0-7]* + * + * terms may be combined using , + * + * if non-null, e points to the first unrecognized char in expr + */ + +#include <ast.h> +#include <ls.h> +#include <modex.h> + +int +strperm(const char* aexpr, char** e, register int perm) +{ + register char* expr = (char*)aexpr; + register int c; + register int typ; + register int who; + int num; + int op; + int mask; + int masked; + + if (perm == -1) + { + perm = 0; + masked = 1; + mask = ~0; + } + else + masked = 0; + for (;;) + { + op = num = who = typ = 0; + for (;;) + { + switch (c = *expr++) + { + case 'u': + who |= S_ISVTX|S_ISUID|S_IRWXU; + continue; + case 'g': + who |= S_ISVTX|S_ISGID|S_IRWXG; + continue; + case 'o': + who |= S_ISVTX|S_IRWXO; + continue; + case 'a': + who = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; + continue; + default: + if (c >= '0' && c <= '7') + { + if (!who) + who = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; + c = '='; + } + expr--; + /*FALLTHROUGH*/ + case '=': + if (who) + perm &= ~who; + else + perm = 0; + /*FALLTHROUGH*/ + case '+': + case '|': + case '-': + case '&': + case '^': + op = c; + for (;;) + { + switch (c = *expr++) + { + case 'r': + typ |= S_IRUSR|S_IRGRP|S_IROTH; + continue; + case 'w': + typ |= S_IWUSR|S_IWGRP|S_IWOTH; + continue; + case 'X': + if (!S_ISDIR(perm) && !(perm & (S_IXUSR|S_IXGRP|S_IXOTH))) + continue; + /*FALLTHROUGH*/ + case 'x': + typ |= S_IXUSR|S_IXGRP|S_IXOTH; + continue; + case 's': + typ |= S_ISUID|S_ISGID; + continue; + case 't': + typ |= S_ISVTX; + continue; + case 'l': + if (perm & S_IXGRP) + { + if (e) + *e = expr - 1; + return perm & S_IPERM; + } + typ |= S_ISGID; + continue; + case '=': + case '+': + case '|': + case '-': + case '&': + case '^': + case ',': + case 0: + if (who) + typ &= who; + else + switch (op) + { + case '=': + case '+': + case '|': + case '-': + case '&': + if (!masked) + { + masked = 1; + umask(mask = umask(0)); + mask = ~mask; + } + typ &= mask; + break; + } + switch (op) + { + default: + if (who) + perm &= ~who; + else + perm = 0; + /*FALLTHROUGH*/ + case '+': + case '|': + perm |= typ; + typ = 0; + break; + case '-': + perm &= ~typ; + typ = 0; + break; + case '&': + perm &= typ; + typ = 0; + break; + case '^': + if (typ &= perm) + { + /* + * propagate least restrictive to most restrictive + */ + + if (typ & S_IXOTH) + perm |= who & (S_IXUSR|S_IXGRP); + if (typ & S_IWOTH) + perm |= who & (S_IWUSR|S_IWGRP); + if (typ & S_IROTH) + perm |= who & (S_IRUSR|S_IRGRP); + if (typ & S_IXGRP) + perm |= who & S_IXUSR; + if (typ & S_IWGRP) + perm |= who & S_IWUSR; + if (typ & S_IRGRP) + perm |= who & S_IRUSR; + + /* + * if any execute then read => execute + */ + + if ((typ |= perm) & (S_IXUSR|S_IXGRP|S_IXOTH)) + { + if (typ & S_IRUSR) + perm |= who & S_IXUSR; + if (typ & S_IRGRP) + perm |= who & S_IXGRP; + if (typ & S_IROTH) + perm |= who & S_IXOTH; + } + typ = 0; + } + break; + } + switch (c) + { + case '=': + case '+': + case '|': + case '-': + case '&': + case '^': + op = c; + typ = 0; + continue; + } + if (c) + break; + /*FALLTHROUGH*/ + default: + if (c < '0' || c > '7') + { + if (e) + *e = expr - 1; + if (typ) + { + if (who) + { + typ &= who; + perm &= ~who; + } + perm |= typ; + } + return perm & S_IPERM; + } + num = (num << 3) | (c - '0'); + if (!who && (op == '+' || op == '-')) + who = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; + if (*expr < '0' || *expr > '7') + { + typ |= modei(num); + num = 0; + } + continue; + } + break; + } + break; + } + break; + } + } +} diff --git a/src/lib/libast/string/strpsearch.c b/src/lib/libast/string/strpsearch.c new file mode 100644 index 0000000..69637d5 --- /dev/null +++ b/src/lib/libast/string/strpsearch.c @@ -0,0 +1,125 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + */ + +#include <ast.h> +#include <ccode.h> +#include <ctype.h> + +#if CC_NATIVE == CC_ASCII +#define MAP(m,c) (c) +#else +#define MAP(m,c) m[c] +#endif + +/* + * return a pointer to the isalpha() identifier matching + * name in the CC_ASCII sorted tab of num elements of + * size siz where the first member of each + * element is a char* + * + * [xxx] brackets optional identifier characters + * * starts optional identifier characters + * + * 0 returned if name not found + * otherwise if next!=0 then it points to the next + * unmatched char in name + */ + +void* +strpsearch(const void* tab, size_t num, size_t siz, const char* name, char** next) +{ + register char* lo = (char*)tab; + register char* hi = lo + (num - 1) * siz; + register char* mid; +#if CC_NATIVE != CC_ASCII + register unsigned char* m; +#endif + register unsigned char* s; + register unsigned char* t; + register int c; + register int v; + int sequential = 0; + +#if CC_NATIVE != CC_ASCII + m = ccmap(CC_NATIVE, CC_ASCII); +#endif + c = MAP(m, *((unsigned char*)name)); + while (lo <= hi) + { + mid = lo + (sequential ? 0 : (((hi - lo) / siz) / 2) * siz); + if (!(v = c - MAP(m, *(s = *((unsigned char**)mid)))) || *s == '[' && !(v = c - MAP(m, *++s)) && (v = 1)) + { + t = (unsigned char*)name; + for (;;) + { + if (!v && (*s == '[' || *s == '*')) + { + v = 1; + s++; + } + else if (v && *s == ']') + { + v = 0; + s++; + } + else if (!isalpha(*t)) + { + if (v || !*s) + { + if (next) + *next = (char*)t; + return (void*)mid; + } + if (!sequential) + { + while ((mid -= siz) >= lo && (s = *((unsigned char**)mid)) && ((c == MAP(m, *s)) || *s == '[' && c == MAP(m, *(s + 1)))); + sequential = 1; + } + v = 1; + break; + } + else if (*t != *s) + { + v = MAP(m, *t) - MAP(m, *s); + break; + } + else + { + t++; + s++; + } + } + } + else if (sequential) + break; + if (v > 0) + lo = mid + siz; + else + hi = mid - siz; + } + return 0; +} diff --git a/src/lib/libast/string/strsearch.c b/src/lib/libast/string/strsearch.c new file mode 100644 index 0000000..56d78fc --- /dev/null +++ b/src/lib/libast/string/strsearch.c @@ -0,0 +1,57 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + */ + +#include <ast.h> + +/* + * return a pointer to the element matching + * name in the (*comparf*)() sorted tab of num elements of + * size siz where the first member of each + * element is a char* + * + * 0 returned if name not found + */ + +void* +strsearch(const void* tab, size_t num, size_t siz, Strcmp_f comparf, const char* name, void* context) +{ + register char* lo = (char*)tab; + register char* hi = lo + (num - 1) * siz; + register char* mid; + register int v; + + while (lo <= hi) + { + mid = lo + (((hi - lo) / siz) / 2) * siz; + if (!(v = context ? (*(Strcmp_context_f)comparf)(name, *((char**)mid), context) : (*comparf)(name, *((char**)mid)))) + return (void*)mid; + else if (v > 0) + lo = mid + siz; + else hi = mid - siz; + } + return 0; +} diff --git a/src/lib/libast/string/strsort.c b/src/lib/libast/string/strsort.c new file mode 100644 index 0000000..e7ab49a --- /dev/null +++ b/src/lib/libast/string/strsort.c @@ -0,0 +1,57 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * strsort - sort an array pointers using fn + * + * fn follows strcmp(3) conventions + * + * David Korn + * AT&T Bell Laboratories + * + * derived from Bourne Shell + */ + +#include <ast.h> + +void +strsort(char** argv, int n, int(*fn)(const char*, const char*)) +{ + register int i; + register int j; + register int m; + register char** ap; + char* s; + int k; + + for (j = 1; j <= n; j *= 2); + for (m = 2 * j - 1; m /= 2;) + for (j = 0, k = n - m; j < k; j++) + for (i = j; i >= 0; i -= m) + { + ap = &argv[i]; + if ((*fn)(ap[m], ap[0]) >= 0) break; + s = ap[m]; + ap[m] = ap[0]; + ap[0] = s; + } +} diff --git a/src/lib/libast/string/strtape.c b/src/lib/libast/string/strtape.c new file mode 100644 index 0000000..e31f227 --- /dev/null +++ b/src/lib/libast/string/strtape.c @@ -0,0 +1,148 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * local device pathname for portable tape unit specification is returned + * if e is non-null then it is set to the next unused char in s + * + * <unit><density>[<no-rewind>] + * {0-7}[l,m,h,u,c][n] + */ + +#include <ast.h> + +char* +strtape(register const char* s, register char** e) +{ + int mtunit = '0'; + int mtdensity = 0; + char mtrewind[2]; + char mtbehavior[2]; + + static char tapefile[sizeof("/dev/Xrmt/123456789")]; + + mtrewind[0] = mtrewind[1] = mtbehavior[0] = mtbehavior[1] = 0; + for (;;) + { + switch (*s) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + mtunit = *s++; + continue; + case 'b': + case 'v': + mtbehavior[0] = *s++; + continue; + case 'l': + case 'm': + case 'h': + case 'u': + case 'c': + mtdensity = *s++; + continue; + case 'n': + mtrewind[0] = *s++; + continue; + } + break; + } + if (e) *e = (char*)s; + if (!access("/dev/rmt/.", F_OK)) + { + /* + * system V + */ + + if (!mtdensity) mtdensity = 'm'; + sfsprintf(tapefile, sizeof(tapefile), "/dev/rmt/ctape%c%s", mtunit, mtrewind); + if (!access(tapefile, F_OK)) return(tapefile); + for (;;) + { + sfsprintf(tapefile, sizeof(tapefile), "/dev/rmt/%c%c%s%s", mtunit, mtdensity, mtbehavior, mtrewind); + if (!access(tapefile, F_OK)) return(tapefile); + if (!mtbehavior[0]) break; + mtbehavior[0] = 0; + } + } + else if (!access("/dev/nst0", F_OK)) + { + /* + * linux + */ + + sfsprintf(tapefile, sizeof(tapefile), "/dev/%sst%c", mtrewind, mtunit); + } + else if (!access("/dev/nrmt0", F_OK)) + { + /* + * 9th edition + */ + + switch (mtdensity) + { + case 'l': + mtunit = '0'; + break; + case 'm': + mtunit = '1'; + break; + case 'h': + mtunit = '2'; + break; + } + sfsprintf(tapefile, sizeof(tapefile), "/dev/%srmt%c", mtrewind, mtunit); + } + else + { + /* + * BSD + */ + + mtunit -= '0'; + switch (mtdensity) + { + case 'l': + break; + case 'h': + mtunit |= 020; + break; + default: + mtunit |= 010; + break; + } + switch (mtrewind[0]) + { + case 'n': + mtunit |= 040; + break; + } + sfsprintf(tapefile, sizeof(tapefile), "/dev/rmt%d", mtunit); + } + return(tapefile); +} diff --git a/src/lib/libast/string/strtoi.h b/src/lib/libast/string/strtoi.h new file mode 100644 index 0000000..b1155a5 --- /dev/null +++ b/src/lib/libast/string/strtoi.h @@ -0,0 +1,640 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * AT&T Research + * Glenn Fowler + * Phong Vo + * + * common header and implementation for + * + * strtol strtoul strton + * strtoll strtoull strtonll + * strntol strntoul strnton + * strntoll strntoull strntonll + * + * define these macros to instantiate an implementation: + * + * S2I_function the function name + * S2I_number the signed number type + * S2I_unumber the unsigned number type + * S2I_unsigned 1 for unsigned, 0 for signed + * S2I_qualifier 1 for optional qualifier suffix, 0 otherwise + * S2I_multiplier 1 for optional multiplier suffix, 0 otherwise + * S2I_size the second argument is the input string size + * + * convert string to number + * errno=ERANGE on overflow (LONG_MAX) or underflow (LONG_MIN) + * if non-null e will point to first unrecognized char in s + * if basep!=0 it points to the default base on input and + * will point to the explicit base on return + * a default base of 0 will determine the base from the input + * a default base of 1 will determine the base from the input using bb#* + * a base prefix in the string overrides *b + * *b will not be set if the string has no base prefix + * if m>1 and no multipler was specified then the result is multiplied by m + * if m<0 then multipliers are not consumed + * if a base arg or prefix is specified then multiplier is not consumed + * + * integer numbers are of the form: + * + * [sign][base][number[qualifier]][multiplier] + * + * base: nnn# base nnn + * 0[xX] hex + * 0 octal + * [1-9] decimal + * + * number: [0-9a-zA-Z]* + * + * qualifier: [lL] + * [uU] + * [uU][lL] + * [lL][uU] + * [lL][lL][uU] + * [uU][lL][lL] + * + * multiplier: . pseudo-float if m>1 + * [bB] block (512) + * [cC] char (1) + * [gG] giga (1000*1000*1000) + * [gG]i gibi (1024*1024*1024) + * [kK] kilo (1000) + * [kK]i kibi (1024) + * [mM] mega (1000*1000) + * [mM]i mibi (1024*1024) + */ + +#include <ast.h> +#include <ctype.h> + +#include "sfhdr.h" + +#if !__STD_C && !defined(const) +#define const +#endif + +#ifndef ERANGE +#define ERANGE EINVAL +#endif + +#define QL 01 +#define QU 02 + +#define S2I_umax (~((S2I_unumber)0)) + +#if S2I_unsigned +#define S2I_type S2I_unumber +#define S2I_min 0 +#define S2I_max S2I_umax +#else +#define S2I_type S2I_number +#define S2I_min (-S2I_max-1) +#define S2I_max (S2I_umax>>1) +#endif + +#if S2I_size +#define S2I_valid(s) ((s)<(z)) +#else +#define S2I_valid(s) 1 +#endif + +#define ADDOVER(n,c,s) ((S2I_umax-(n))<((S2I_unumber)((c)+(s)))) +#define MPYOVER(n,c) (((S2I_unumber)(n))>(S2I_umax/(c))) + +static const S2I_unumber mm[] = +{ + 0, + S2I_umax / 1, + S2I_umax / 2, + S2I_umax / 3, + S2I_umax / 4, + S2I_umax / 5, + S2I_umax / 6, + S2I_umax / 7, + S2I_umax / 8, + S2I_umax / 9, + S2I_umax / 10, + S2I_umax / 11, + S2I_umax / 12, + S2I_umax / 13, + S2I_umax / 14, + S2I_umax / 15, + S2I_umax / 16, + S2I_umax / 17, + S2I_umax / 18, + S2I_umax / 19, + S2I_umax / 20, + S2I_umax / 21, + S2I_umax / 22, + S2I_umax / 23, + S2I_umax / 24, + S2I_umax / 25, + S2I_umax / 26, + S2I_umax / 27, + S2I_umax / 28, + S2I_umax / 29, + S2I_umax / 30, + S2I_umax / 31, + S2I_umax / 32, + S2I_umax / 33, + S2I_umax / 34, + S2I_umax / 35, + S2I_umax / 36, + S2I_umax / 37, + S2I_umax / 38, + S2I_umax / 39, + S2I_umax / 40, + S2I_umax / 41, + S2I_umax / 42, + S2I_umax / 43, + S2I_umax / 44, + S2I_umax / 45, + S2I_umax / 46, + S2I_umax / 47, + S2I_umax / 48, + S2I_umax / 49, + S2I_umax / 50, + S2I_umax / 51, + S2I_umax / 52, + S2I_umax / 53, + S2I_umax / 54, + S2I_umax / 55, + S2I_umax / 56, + S2I_umax / 57, + S2I_umax / 58, + S2I_umax / 59, + S2I_umax / 60, + S2I_umax / 61, + S2I_umax / 62, + S2I_umax / 63, + S2I_umax / 64, +}; + +#if defined(__EXPORT__) +#define extern __EXPORT__ +#endif +extern S2I_type +#undef extern +#if S2I_size +#if S2I_multiplier +#if __STD_C +S2I_function(const char* a, size_t size, char** e, char* basep, int m) +#else +S2I_function(a, size, e, basep, m) const char* a; size_t size; char** e; char* basep; int m; +#endif +#else +#if __STD_C +S2I_function(const char* a, size_t size, char** e, int base) +#else +S2I_function(a, size, e, base) const char* a; size_t size; char** e; int base; +#endif +#endif +#else +#if S2I_multiplier +#if __STD_C +S2I_function(const char* a, char** e, char* basep, int m) +#else +S2I_function(a, e, basep, m) const char* a; char** e; char* basep; int m; +#endif +#else +#if __STD_C +S2I_function(const char* a, char** e, int base) +#else +S2I_function(a, e, base) const char* a; char** e; int base; +#endif +#endif +#endif +{ + register unsigned char* s = (unsigned char*)a; +#if S2I_size + register unsigned char* z = s + size; +#endif + register S2I_unumber n; + register S2I_unumber x; + register int c; + register int shift; + register unsigned char* p; + register unsigned char* cv; + unsigned char* b; + unsigned char* k; + S2I_unumber v; +#if S2I_multiplier + register int base; +#endif + int negative; + int overflow = 0; + int decimal = 0; + int thousand = 0; +#if !S2I_unsigned + int qualifier = 0; +#endif + +#if S2I_multiplier + base = basep ? *((unsigned char*)basep) : 0; +#else + if (base > 36 && base <= SF_RADIX) + { + static int conformance = -1; + + if (conformance < 0) + conformance = !strcmp(astconf("CONFORMANCE", NiL, NiL), "standard"); + if (conformance) + base = 1; + } +#endif + if (base && (base < 2 || base > SF_RADIX)) + { + errno = EINVAL; + return 0; + } + while (S2I_valid(s) && isspace(*s)) + s++; + if ((negative = S2I_valid(s) && (*s == '-')) || S2I_valid(s) && *s == '+') + k = ++s; + else + k = 0; + p = s; + if (!base) + { + if (S2I_valid(p) && (c = *p++) >= '0' && c <= '9') + { + n = c - '0'; + if (S2I_valid(p) && (c = *p) >= '0' && c <= '9') + { + n = (n << 3) + (n << 1) + c - '0'; + p++; + } + if (S2I_valid(p) && *p == '#') + { + if (n >= 2 && n <= 64) + { + k = s = p + 1; + base = n; + } + } + else if (base) + base = 0; + else if (S2I_valid(s) && *s == '0' && S2I_valid(s + 1)) + { + if ((c = *(s + 1)) == 'x' || c == 'X') + { + k = s += 2; + base = 16; + } + else if (c >= '0' && c <= '7') + { + s++; + base = 8; + } + } + } + if (!base) + base = 10; + else if (base < 2 || base > SF_RADIX) + { + errno = EINVAL; + return 0; + } +#if S2I_multiplier + else + { + if (basep) + *basep = base; + m = -1; + } +#endif + } +#if S2I_multiplier + else + m = -1; +#endif + + /* + * this part transcribed from sfvscanf() + */ + + SFSETLOCALE(&decimal, &thousand); + x = mm[base]; + n = 0; + if (base == 10) + { + b = s; + p = 0; + for (;;) + { + if (S2I_valid(s) && (c = *s++) >= '0' && c <= '9') + { + if (n > x) + overflow = 1; + else + { + n = (n << 3) + (n << 1); + c -= '0'; + if (ADDOVER(n, c, negative)) + overflow = 1; + n += c; + } + } + else if (p && (s - p) != (3 + S2I_valid(s))) + { + s = p; + n = v; + c = 0; + break; + } + else if (!S2I_valid(s) || c != thousand) + break; + else if (!p && (s - b) > 4) + { + if (e) + *e = (char*)s - 1; + if (overflow) + { + errno = ERANGE; +#if S2I_unsigned + n = S2I_max; +#else + n = negative ? S2I_min : S2I_max; +#endif + } + return n; + } + else + { + p = s; + v = n; + } + } + } + else + { + SFCVINIT(); + cv = base <= 36 ? _Sfcv36 : _Sfcv64; + if ((base & ~(base - 1)) == base) + { +#if !S2I_unsigned + qualifier |= QU; +#endif + if (base < 8) + shift = base < 4 ? 1 : 2; + else if (base < 32) + shift = base < 16 ? 3 : 4; + else + shift = base < 64 ? 5 : 6; + while (S2I_valid(s) && (c = cv[*s++]) < base) + { + if (n > x) + overflow = 1; + else + { + n <<= shift; + if (ADDOVER(n, c, negative)) + overflow = 1; + n += c; + } + } + } + else + while (S2I_valid(s) && (c = cv[*s++]) < base) + { + if (n > x) + overflow = 1; + else + { + n *= base; + if (ADDOVER(n, c, negative)) + overflow = 1; + n += c; + } + } + c = *(s - 1); + } + +#if S2I_qualifier + + /* + * optional qualifier suffix + */ + + if (S2I_valid(s) && s > (unsigned char*)(a + 1)) + { + base = 0; + for (;;) + { + if (!(base & QL) && (c == 'l' || c == 'L')) + { + base |= QL; + if (!S2I_valid(s)) + break; + c = *s++; + if (c == 'l' || c == 'L') + { + if (!S2I_valid(s)) + break; + c = *s++; + } + } + else if (!(base & QU) && (c == 'u' || c == 'U')) + { + base |= QU; +#if !S2I_unsigned + qualifier |= QU; +#endif + if (!S2I_valid(s)) + break; + c = *s++; + } + else + break; + } + } +#endif + if (S2I_valid(s)) + { +#if S2I_multiplier + /* + * optional multiplier suffix + */ + + if (m < 0 || s == (unsigned char*)(a + 1)) + s--; + else + { + x = m != 1; + switch (c) + { + case 'b': + case 'B': + shift = 9; + x = 0; + break; + case 'k': + case 'K': + shift = 10; + break; + case 'm': + case 'M': + shift = 20; + break; + case 'g': + case 'G': + shift = 30; + break; + case 't': + case 'T': + shift = 40; + break; + case 'p': + case 'P': + shift = 50; + break; + case 'e': + case 'E': + shift = 60; + break; + default: + if (m <= 1) + v = 0; + else if (c == decimal && S2I_valid(s)) + { + if (MPYOVER(n, m)) + overflow = 1; + n *= m; + v = 0; + while (S2I_valid(s) && (c = *s++) >= '0' && c <= '9') + v += (m /= 10) * (c - '0'); + if (ADDOVER(n, v, negative)) + overflow = 1; + n += v; + v = 0; + } + else + v = m; + s--; + shift = 0; + break; + } + if (shift) + { + if (S2I_valid(s)) + switch (*s) + { + case 'i': + case 'I': + s++; + x = 0; + break; + } + if (S2I_valid(s)) + switch (*s) + { + case 'b': + case 'B': + s++; + break; + } + if (x) + { + v = 1; + for (shift /= 10; shift; shift--) + { + if (v >= (S2I_max/1000)) + { + v = 0; + overflow = 1; + } + v *= 1000; + } + } + else +#if S2I_unsigned + if (shift >= (sizeof(S2I_type) * CHAR_BIT)) +#else + if (shift >= (sizeof(S2I_type) * CHAR_BIT - 1)) +#endif + { + v = 0; + overflow = 1; + } + else + v = ((S2I_unumber)1) << shift; + } + if (v) + { + if (MPYOVER(n, v)) + overflow = 1; + n *= v; + } + } +#else + s--; +#endif + } + if (s == k) + { + s--; +#if S2I_multiplier + if (basep) + *basep = 10; +#endif + } +#if !S2I_unsigned + else if (!(qualifier & QU)) + { + if (negative) + { + if (!n) + { + b = k; + do + { + if (b >= s) + { + negative = 0; + break; + } + } while (*b++ == '0'); + } + if (negative && (n - 1) > S2I_max) + overflow = 1; + } + else if (n > S2I_max) + overflow = 1; + } +#endif + if (e) + *e = (char*)s; + if (overflow) + { +#if !S2I_unsigned + if (negative) + { + if (x << 1) + errno = ERANGE; + return (S2I_type)S2I_min; + } +#endif + errno = ERANGE; + return (S2I_type)S2I_max; + } + return negative ? -n : n; +} diff --git a/src/lib/libast/string/strtoip4.c b/src/lib/libast/string/strtoip4.c new file mode 100644 index 0000000..6d4aed2 --- /dev/null +++ b/src/lib/libast/string/strtoip4.c @@ -0,0 +1,150 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> +#include <ctype.h> + +/* + * convert string to 4 byte local byte order ip address + * with optional prefix bits + * pointer to first unused char placed in *e, even on error + * return 0:ok <0:error + * + * valid addresses match the egrep RE: + * + * [0-9]{1,3}(\.[0-9]{1,3})*|0[xX][0-9a-fA-Z]+ + * + * valid bits/masks match the egrep RE: + * + * (/([0-9]+|[0-9]{1,3}(\.[0-9]{1,3})*))? + * + * if pbits!=0 and no bits/mask specified then trailing 0's in addr + * are used to compute the mask + */ + +int +strtoip4(register const char* s, char** e, uint32_t* paddr, unsigned char* pbits) +{ + register int c; + register unsigned int n; + register uint32_t addr; + register int part; + register unsigned char bits; + uint32_t z; + int old; + int r; + const char* b; + + r = -1; + while (isspace(*s)) + s++; + b = s; + addr = 0; + bits = 0; + part = 0; + do + { + n = 0; + while ((c = *s++) >= '0' && c <= '9') + n = n * 10 + (c - '0'); + if ((c == 'x' || c == 'X') && !part) + { + addr = n; + for (;;) + { + if ((c = *s++) >= '0' && c <= '9') + c -= '0'; + else if (c >= 'a' && c <= 'f') + c -= 'a' - 10; + else if (c >= 'A' && c <= 'F') + c -= 'F' - 10; + else + break; + addr = addr * 16 + c; + } + part = 4; + break; + } + if (n > 0xff) + goto done; + addr = (addr << 8) | n; + part++; + } while (c == '.'); + if ((s - b) == 1 && c != '/' || part > 4) + goto done; + if (old = part < 4) + while (part++ < 4) + addr <<= 8; + if (pbits) + { + if (c == '/') + { + part = 0; + z = 0; + for (;;) + { + n = 0; + while ((c = *s++) >= '0' && c <= '9') + n = n * 10 + (c - '0'); + z = (z << 8) | n; + part++; + if (c != '.') + break; + old = 1; + } + if (part > 4) + goto done; + if (z <= 32 && (!old || part < 2)) + bits = z; + else if (z) + { + if (part == 4 && (z & 0x8000001) == 1) + z = ~z; + while (!(z & 1)) + z >>= 1; + while (z & 1) + { + z >>= 1; + bits++; + } + } + } + else if ((z = (addr >> 24)) < 128) + bits = 8; + else if (z < 192) + bits = 16; + else + bits = 24; + if (*pbits = bits) + addr &= ~((((uint32_t)1)<<(32-bits))-1); + else + addr = 0; + } + if (paddr) + *paddr = addr; + r = 0; + done: + if (e) + *e = (char*)(s - 1); + return r; +} diff --git a/src/lib/libast/string/strtoip6.c b/src/lib/libast/string/strtoip6.c new file mode 100644 index 0000000..ff1c652 --- /dev/null +++ b/src/lib/libast/string/strtoip6.c @@ -0,0 +1,204 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#if _PACKAGE_ast +#include <ast.h> +#else +#include <stdint.h> +#endif + +#include <ctype.h> +#include <ip6.h> + +/* + * convert string to ipv6 network byte order ip address + * with optional prefix bits + * pointer to first unused char placed in *e, even on error + * return 0:ok <0:error + */ + +#define COL 16 +#define DOT 17 +#define END 18 +#define PFX 19 + +int +strtoip6(register const char* s, char** e, unsigned char* addr, unsigned char* bits) +{ + register unsigned char* b = addr; + register unsigned char* x = b + IP6ADDR; + register unsigned char* z; + register int c; + register uint32_t a; + + static unsigned char lex[256]; + + if (!lex[0]) + { + for (c = 0; c < sizeof(lex); ++c) + lex[c] = END; + lex['0'] = 0; + lex['1'] = 1; + lex['2'] = 2; + lex['3'] = 3; + lex['4'] = 4; + lex['5'] = 5; + lex['6'] = 6; + lex['7'] = 7; + lex['8'] = 8; + lex['9'] = 9; + lex['A'] = lex['a'] = 10; + lex['B'] = lex['b'] = 11; + lex['C'] = lex['c'] = 12; + lex['D'] = lex['d'] = 13; + lex['E'] = lex['e'] = 14; + lex['F'] = lex['f'] = 15; + lex[':'] = COL; + lex['.'] = DOT; + lex['/'] = PFX; + } + while (isspace(*s)) + s++; + z = 0; + a = 0; + if (*s) + for (;;) + { + switch (c = lex[*((unsigned char*)s++)]) + { + case END: + case PFX: + if ((x - b) < 2) + break; + *b++ = a>>8; + *b++ = a; + break; + case COL: + if ((x - b) < 2) + break; + *b++ = a>>8; + *b++ = a; + a = 0; + if (*s == ':') + { + if (z) + { + s--; + break; + } + z = b; + if ((c = lex[*((unsigned char*)++s)]) >= 16) + { + s++; + break; + } + } + continue; + case DOT: + if (b >= x) + { + s--; + break; + } + *b++ = ((a >> 8) & 0xf) * 100 + ((a >> 4) & 0xf) * 10 + (a & 0xf); + a = 0; + for (;;) + { + switch (c = lex[*((unsigned char*)s++)]) + { + case COL: + case END: + case PFX: + if (b < x) + *b++ = a; + a = 0; + break; + case DOT: + if (b >= x) + break; + *b++ = a; + a = 0; + continue; + default: + a = (a * 10) + c; + continue; + } + break; + } + if (c == COL) + { + if (*s == ':') + { + if (z) + { + s--; + break; + } + z = b; + if ((c = lex[*((unsigned char*)++s)]) >= 16) + { + s++; + break; + } + } + if ((b - addr) == 6 && addr[0] == 0x20 && addr[1] == 0x02) + continue; + } + break; + default: + a = (a << 4) | c; + continue; + } + break; + } + if (b == addr) + c = END + 1; + else + { + if (z) + { + while (b > z) + *--x = *--b; + while (x > z) + *--x = 0; + } + else + while (b < x) + *b++ = 0; + if (bits) + { + if (c == PFX) + { + a = 0; + while ((c = lex[*((unsigned char*)s++)]) < 10) + a = a * 10 + c; + } + else + a = 0xff; + *bits = a; + } + } + if (e) + *e = (char*)(s - 1); + return c == END ? 0 : -1; +} diff --git a/src/lib/libast/string/strton.c b/src/lib/libast/string/strton.c new file mode 100644 index 0000000..05fbd86 --- /dev/null +++ b/src/lib/libast/string/strton.c @@ -0,0 +1,31 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * strton() implementation + */ + +#define S2I_function strton +#define S2I_number long +#define S2I_unumber unsigned long +#define S2I_multiplier 1 + +#include "strtoi.h" diff --git a/src/lib/libast/string/strtonll.c b/src/lib/libast/string/strtonll.c new file mode 100644 index 0000000..22084dc --- /dev/null +++ b/src/lib/libast/string/strtonll.c @@ -0,0 +1,31 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +/* + * strtonll() implementation + */ + +#define S2I_function strtonll +#define S2I_number intmax_t +#define S2I_unumber uintmax_t +#define S2I_multiplier 1 + +#include "strtoi.h" diff --git a/src/lib/libast/string/struid.c b/src/lib/libast/string/struid.c new file mode 100644 index 0000000..9a26e8f --- /dev/null +++ b/src/lib/libast/string/struid.c @@ -0,0 +1,109 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Bell Laboratories + * + * uid name -> number + */ + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:hide getpwnam getpwuid +#else +#define getpwnam ______getpwnam +#define getpwuid ______getpwuid +#endif + +#include <ast.h> +#include <cdt.h> +#include <pwd.h> + +#if defined(__STDPP__directive) && defined(__STDPP__hide) +__STDPP__directive pragma pp:nohide getpwnam getpwuid +#else +#undef getpwnam +#undef getpwuid +#endif + +extern struct passwd* getpwnam(const char*); +extern struct passwd* getpwuid(uid_t); + +typedef struct Id_s +{ + Dtlink_t link; + int id; + char name[1]; +} Id_t; + +/* + * return uid number given uid name + * -1 on first error for a given name + * -2 on subsequent errors for a given name + */ + +int +struid(const char* name) +{ + register Id_t* ip; + register struct passwd* pw; + int id; + char* e; + + static Dt_t* dict; + static Dtdisc_t disc; + + if (!dict) + { + disc.key = offsetof(Id_t, name); + dict = dtopen(&disc, Dtset); + } + else if (ip = (Id_t*)dtmatch(dict, name)) + return ip->id; + if (pw = getpwnam(name)) + id = pw->pw_uid; + else + { + id = strtol(name, &e, 0); +#if _WINIX + if (!*e) + { + if (!getpwuid(id)) + id = -1; + } + else if (streq(name, "root") && (pw = getpwnam("Administrator"))) + id = pw->pw_uid; + else + id = -1; +#else + if (*e || !getpwuid(id)) + id = -1; +#endif + } + if (dict && (ip = newof(0, Id_t, 1, strlen(name)))) + { + strcpy(ip->name, name); + ip->id = id >= 0 ? id : -2; + dtinsert(dict, ip); + } + return id; +} diff --git a/src/lib/libast/string/struniq.c b/src/lib/libast/string/struniq.c new file mode 100644 index 0000000..498fd4e --- /dev/null +++ b/src/lib/libast/string/struniq.c @@ -0,0 +1,51 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * struniq - uniq a sorted argv + * 0 sentinel is neither expected nor restored + * + * Glenn Fowler + * David Korn + * AT&T Research + */ + +#include <ast.h> + +int +struniq(char** argv, int n) +{ + register char** ao; + register char** an; + register char** ae; + + ao = an = argv; + ae = ao + n; + while (++an < ae) + { + while (streq(*ao, *an)) + if (++an >= ae) + return ao - argv + 1; + *++ao = *an; + } + return ao - argv + 1; +} diff --git a/src/lib/libast/string/strvcmp.c b/src/lib/libast/string/strvcmp.c new file mode 100644 index 0000000..036460d --- /dev/null +++ b/src/lib/libast/string/strvcmp.c @@ -0,0 +1,74 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped + +#include <ast.h> +#include <ctype.h> + +/* + * version strcmp(3) + */ + +int +strvcmp(register const char* a, register const char* b) +{ + register unsigned long na; + register unsigned long nb; + + for (;;) + { + if (isdigit(*a) && isdigit(*b)) + { + na = nb = 0; + while (isdigit(*a)) + na = na * 10 + *a++ - '0'; + while (isdigit(*b)) + nb = nb * 10 + *b++ - '0'; + if (na < nb) + return -1; + if (na > nb) + return 1; + } + else if (*a != *b) + break; + else if (!*a) + return 0; + else + { + a++; + b++; + } + } + if (*a == 0) + return -1; + if (*b == 0) + return 1; + if (*a == '.') + return -1; + if (*b == '.') + return 1; + if (*a == '-') + return -1; + if (*b == '-') + return 1; + return *a < *b ? -1 : 1; +} diff --git a/src/lib/libast/string/swapget.c b/src/lib/libast/string/swapget.c new file mode 100644 index 0000000..b032bfc --- /dev/null +++ b/src/lib/libast/string/swapget.c @@ -0,0 +1,57 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * internal representation conversion support + */ + +#include <ast.h> +#include <swap.h> + +/* + * get int_n from b according to op + */ + +intmax_t +swapget(int op, const void* b, int n) +{ + register unsigned char* p; + register unsigned char* d; + intmax_t v; + unsigned char tmp[sizeof(intmax_t)]; + + if (n > sizeof(intmax_t)) + n = sizeof(intmax_t); + if (op) swapmem(op, b, d = tmp, n); + else d = (unsigned char*)b; + p = d + n; + v = 0; + while (d < p) + { + v <<= CHAR_BIT; + v |= *d++; + } + return v; +} diff --git a/src/lib/libast/string/swapmem.c b/src/lib/libast/string/swapmem.c new file mode 100644 index 0000000..7a96bf0 --- /dev/null +++ b/src/lib/libast/string/swapmem.c @@ -0,0 +1,109 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * internal representation conversion support + */ + +#include <ast.h> +#include <swap.h> + +/* + * swap n bytes according to op + * from==to is ok + */ + +void* +swapmem(int op, const void* from, void* to, register size_t n) +{ + register char* f = (char*)from; + register char* t = (char*)to; + register int c; + + switch (op & (n - 1)) + { + case 0: + if (t != f) + memcpy(t, f, n); + break; + case 1: + for (n >>= 1; n--; f += 2, t += 2) + { + c = f[0]; t[0] = f[1]; t[1] = c; + } + break; + case 2: + for (n >>= 2; n--; f += 4, t += 4) + { + c = f[0]; t[0] = f[2]; t[2] = c; + c = f[1]; t[1] = f[3]; t[3] = c; + } + break; + case 3: + for (n >>= 2; n--; f += 4, t += 4) + { + c = f[0]; t[0] = f[3]; t[3] = c; + c = f[1]; t[1] = f[2]; t[2] = c; + } + break; + case 4: + for (n >>= 3; n--; f += 8, t += 8) + { + c = f[0]; t[0] = f[4]; t[4] = c; + c = f[1]; t[1] = f[5]; t[5] = c; + c = f[2]; t[2] = f[6]; t[6] = c; + c = f[3]; t[3] = f[7]; t[7] = c; + } + break; + case 5: + for (n >>= 3; n--; f += 8, t += 8) + { + c = f[0]; t[0] = f[5]; t[5] = c; + c = f[1]; t[1] = f[4]; t[4] = c; + c = f[2]; t[2] = f[7]; t[7] = c; + c = f[3]; t[3] = f[6]; t[6] = c; + } + break; + case 6: + for (n >>= 3; n--; f += 8, t += 8) + { + c = f[0]; t[0] = f[6]; t[6] = c; + c = f[1]; t[1] = f[7]; t[7] = c; + c = f[2]; t[2] = f[4]; t[4] = c; + c = f[3]; t[3] = f[5]; t[5] = c; + } + break; + case 7: + for (n >>= 3; n--; f += 8, t += 8) + { + c = f[0]; t[0] = f[7]; t[7] = c; + c = f[1]; t[1] = f[6]; t[6] = c; + c = f[2]; t[2] = f[5]; t[5] = c; + c = f[3]; t[3] = f[4]; t[4] = c; + } + break; + } + return to; +} diff --git a/src/lib/libast/string/swapop.c b/src/lib/libast/string/swapop.c new file mode 100644 index 0000000..aefe687 --- /dev/null +++ b/src/lib/libast/string/swapop.c @@ -0,0 +1,59 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * internal representation conversion support + */ + +#include <ast.h> +#include <swap.h> + +/* + * return the swap operation for external to internal conversion + * if size<0 then (-size) used and (-size==4)&&(op==3) => op=7 + * this is a workaround for 4 byte magic predicting 8 byte swap + */ + +int +swapop(const void* internal, const void* external, int size) +{ + register int op; + register int z; + char tmp[sizeof(intmax_t)]; + + if ((z = size) < 0) + z = -z; + if (z <= 1) + return 0; + if (z <= sizeof(intmax_t)) + for (op = 0; op < z; op++) + if (!memcmp(internal, swapmem(op, external, tmp, z), z)) + { + if (size < 0 && z == 4 && op == 3) + op = 7; + return op; + } + return -1; +} diff --git a/src/lib/libast/string/swapput.c b/src/lib/libast/string/swapput.c new file mode 100644 index 0000000..66373ae --- /dev/null +++ b/src/lib/libast/string/swapput.c @@ -0,0 +1,50 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * internal representation conversion support + */ + +#include <ast.h> +#include <swap.h> + +/* + * put v of n chars into b according to op + */ + +void* +swapput(int op, void* b, int n, intmax_t v) +{ + register char* p = (char*)b + n; + + while (p > (char*)b) + { + *--p = v; + v >>= CHAR_BIT; + } + if (op) + swapmem(op, p, p, n); + return b; +} diff --git a/src/lib/libast/string/tok.c b/src/lib/libast/string/tok.c new file mode 100644 index 0000000..c781fb7 --- /dev/null +++ b/src/lib/libast/string/tok.c @@ -0,0 +1,190 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * token stream routines + */ + +#include <ast.h> +#include <tok.h> + +#define FLG_RESTORE 01 /* restore string on close */ +#define FLG_NEWLINE 02 /* return newline token next */ + +typedef struct Tok_s /* token stream state */ +{ + union + { + char* end; /* end ('\0') of last token */ + struct Tok_s* nxt; /* next in free list */ + } ptr; + char chr; /* replace *end with this */ + char flg; /* FLG_* */ +} Tok_t; + +static Tok_t* freelist; + +/* + * open a new token stream on s + * if f==0 then string is not restored + */ + +char* +tokopen(register char* s, int f) +{ + register Tok_t* p; + + if (p = freelist) + freelist = freelist->ptr.nxt; + else if (!(p = newof(0, Tok_t, 1, 0))) + return 0; + p->chr = *(p->ptr.end = s); + p->flg = f ? FLG_RESTORE : 0; + return (char*)p; +} + +/* + * close a token stream + * restore the string to its original state + */ + +void +tokclose(char* u) +{ + register Tok_t* p = (Tok_t*)u; + + if (p->flg == FLG_RESTORE && *p->ptr.end != p->chr) + *p->ptr.end = p->chr; + p->ptr.nxt = freelist; + freelist = p; +} + +/* + * return next space separated token + * "\n" is returned as a token + * 0 returned when no tokens remain + * "..." and '...' quotes are honored with \ escapes + */ + +char* +tokread(char* u) +{ + register Tok_t* p = (Tok_t*)u; + register char* s; + register char* r; + register int q; + register int c; + + /* + * restore string on each call + */ + + if (!p->chr) + return 0; + s = p->ptr.end; + switch (p->flg) + { + case FLG_NEWLINE: + p->flg = 0; + return "\n"; + case FLG_RESTORE: + if (*s != p->chr) + *s = p->chr; + break; + default: + if (!*s) + s++; + break; + } + + /* + * skip leading space + */ + + while (*s == ' ' || *s == '\t') + s++; + if (!*s) + { + p->ptr.end = s; + p->chr = 0; + return 0; + } + + /* + * find the end of this token + */ + + r = s; + q = 0; + for (;;) + switch (c = *r++) + { + case '\n': + if (!q) + { + if (s == (r - 1)) + { + if (!p->flg) + { + p->ptr.end = r; + return "\n"; + } + r++; + } + else if (!p->flg) + p->flg = FLG_NEWLINE; + } + /*FALLTHROUGH*/ + case ' ': + case '\t': + if (q) + break; + /*FALLTHROUGH*/ + case 0: + if (s == --r) + { + p->ptr.end = r; + p->chr = 0; + } + else + { + p->chr = *(p->ptr.end = r); + if (*r) + *r = 0; + } + return s; + case '\\': + if (*r) + r++; + break; + case '"': + case '\'': + if (c == q) + q = 0; + else if (!q) + q = c; + break; + } +} diff --git a/src/lib/libast/string/tokline.c b/src/lib/libast/string/tokline.c new file mode 100644 index 0000000..b572651 --- /dev/null +++ b/src/lib/libast/string/tokline.c @@ -0,0 +1,193 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * return an Sfio_t* to a file or string that + * + * splices \\n to single lines + * checks for "..." and '...' spanning newlines + * drops #...\n comments + * + * if <arg> is a file and first line matches + * #!!! <level> <message> !!! + * then error(<lev>,"%s: %s",<arg>,<msg>) called + * + * NOTE: seek disabled and string disciplines cannot be composed + * quoted \n translated to \r + */ + +#include <ast.h> +#include <error.h> +#include <tok.h> + +typedef struct +{ + Sfdisc_t disc; + Sfio_t* sp; + int quote; + int* line; +} Splice_t; + +/* + * the splicer + */ + +static int +spliceline(Sfio_t* s, int op, void* val, Sfdisc_t* ad) +{ + Splice_t* d = (Splice_t*)ad; + register char* b; + register int c; + register int n; + register int q; + register int j; + register char* e; + char* buf; + + NoP(val); + switch (op) + { + case SF_CLOSING: + sfclose(d->sp); + return 0; + case SF_DPOP: + free(d); + return 0; + case SF_READ: + do + { + if (!(buf = sfgetr(d->sp, '\n', 0)) && !(buf = sfgetr(d->sp, '\n', -1))) + return 0; + n = sfvalue(d->sp); + q = d->quote; + j = 0; + (*d->line)++; + if (n > 1 && buf[n - 2] == '\\') + { + j = 1; + n -= 2; + if (q == '#') + { + n = 0; + continue; + } + } + else if (q == '#') + { + q = 0; + n = 0; + continue; + } + if (n > 0) + { + e = (b = buf) + n; + while (b < e) + { + if ((c = *b++) == '\\') + b++; + else if (c == q) + q = 0; + else if (!q) + { + if (c == '\'' || c == '"') + q = c; + else if (c == '#' && (b == (buf + 1) || (c = *(b - 2)) == ' ' || c == '\t')) + { + if (buf[n - 1] != '\n') + { + q = '#'; + n = b - buf - 2; + } + else if (n = b - buf - 1) + buf[n - 1] = '\n'; + break; + } + } + } + if (n > 0) + { + if (!j && buf[n - 1] != '\n' && (s->_flags & SF_STRING)) + buf[n++] = '\n'; + if (q && buf[n - 1] == '\n') + buf[n - 1] = '\r'; + } + } + } while (n <= 0); + sfsetbuf(s, buf, n); + d->quote = q; + return 1; + default: + return 0; + } +} + +/* + * open a stream to parse lines + * + * flags: 0 arg: open Sfio_t* + * flags: SF_READ arg: file name + * flags: SF_STRING arg: null terminated char* + * + * if line!=0 then it points to a line count that starts at 0 + * and is incremented for each input line + */ + +Sfio_t* +tokline(const char* arg, int flags, int* line) +{ + Sfio_t* f; + Sfio_t* s; + Splice_t* d; + char* p; + char* e; + + static int hidden; + + if (!(d = newof(0, Splice_t, 1, 0))) + return 0; + if (!(s = sfopen(NiL, NiL, "s"))) + { + free(d); + return 0; + } + if (!(flags & (SF_STRING|SF_READ))) + f = (Sfio_t*)arg; + else if (!(f = sfopen(NiL, arg, (flags & SF_STRING) ? "s" : "r"))) + { + free(d); + sfclose(s); + return 0; + } + else if ((p = sfreserve(f, 0, 0)) && sfvalue(f) > 11 && strmatch(p, "#!!! +([-0-9]) *([!\n]) !!!\n*") && (e = strchr(p, '\n'))) + { + flags = strtol(p + 5, &p, 10); + error(flags, "%s:%-.*s", arg, e - p - 4, p); + } + d->disc.exceptf = spliceline; + d->sp = f; + *(d->line = line ? line : &hidden) = 0; + sfdisc(s, (Sfdisc_t*)d); + return s; +} diff --git a/src/lib/libast/string/tokscan.c b/src/lib/libast/string/tokscan.c new file mode 100644 index 0000000..34f6dfc --- /dev/null +++ b/src/lib/libast/string/tokscan.c @@ -0,0 +1,360 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * scan s for tokens in fmt + * s modified in place and not restored + * if nxt!=0 then it will point to the first unread char in s + * the number of scanned tokens is returned + * -1 returned if s was not empty and fmt failed to match + * + * ' ' in fmt matches 0 or more {space,tab} + * '\n' in fmt eats remainder of current line + * "..." and '...' quotes interpreted + * newline is equivalent to end of buf except when quoted + * \\ quotes following char + * + * message support for %s and %v data + * + * (5:12345) fixed length strings, ) may be \t + * (null) NiL + * + * "..." and '...' may span \n, and \\n is the line splice + * quoted '\r' translated to '\n' + * otherwise tokenizing is unconditionally terminated by '\n' + * + * a null arg pointer skips that arg + * + * %c char + * %[hl]d [short|int|long] base 10 + * %f double + * %g double + * %[hl]n [short|int|long] C-style base + * %[hl]o [short|int|long] base 8 + * %s string + * %[hl]u same as %[hl]n + * %v argv, elements + * %[hl]x [short|int|long] base 16 + * + * unmatched char args are set to "", int args to 0 + */ + +#include <ast.h> +#include <tok.h> + +static char empty[1]; + +/* + * get one string token into p + */ + +static char* +lextok(register char* s, register int c, char** p, int* n) +{ + register char* t; + register int q; + char* b; + char* u; + + if (*s == '(' && (!c || c == ' ' || c == '\n')) + { + q = strtol(s + 1, &b, 10); + if (*b == ':') + { + if (*(t = ++b + q) == ')' || *t == '\t') + { + s = t; + *s++ = 0; + goto end; + } + } + else if (strneq(b, "null)", 5)) + { + s = b + 5; + b = 0; + goto end; + } + } + b = s; + q = 0; + t = 0; + for (;;) + { + if (!*s || !q && *s == '\n') + { + if (!q) + { + if (!c || c == ' ' || c == '\n') (*n)++; + else + { + s = b; + b = empty; + break; + } + } + if (t) *t = 0; + break; + } + else if (*s == '\\') + { + u = s; + if (!*++s || *s == '\n' && (!*++s || *s == '\n')) continue; + if (p) + { + if (b == u) b = s; + else if (!t) t = u; + } + } + else if (q) + { + if (*s == q) + { + q = 0; + if (!t) t = s; + s++; + continue; + } + else if (*s == '\r') *s = '\n'; + } + else if (*s == '"' || *s == '\'') + { + q = *s++; + if (p) + { + if (b == (s - 1)) b = s; + else if (!t) t = s - 1; + } + continue; + } + else if (*s == c || c == ' ' && *s == '\t') + { + *s++ = 0; + if (t) *t = 0; + end: + if (c == ' ') while (*s == ' ' || *s == '\t') s++; + (*n)++; + break; + } + if (t) *t++ = *s; + s++; + } + if (p) *p = b; + return(s); +} + +/* + * scan entry + */ + +int +tokscan(register char* s, char** nxt, const char* fmt, ...) +{ + register int c; + register char* f; + int num = 0; + char* skip = 0; + int q; + int onum; + long val; + double dval; + va_list ap; + char* p_char; + double* p_double; + int* p_int; + long* p_long; + short* p_short; + char** p_string; + char* prv_f = 0; + va_list prv_ap; + + va_start(ap, fmt); + if (!*s || *s == '\n') + { + skip = s; + s = empty; + } + f = (char*)fmt; + for (;;) switch (c = *f++) + { + case 0: + if (f = prv_f) + { + prv_f = 0; + /* prv_ap value is guarded by prv_f */ + va_copy(ap, prv_ap); + continue; + } + goto done; + case ' ': + while (*s == ' ' || *s == '\t') s++; + break; + case '%': + onum = num; + switch (c = *f++) + { + case 'h': + case 'l': + q = c; + c = *f++; + break; + default: + q = 0; + break; + } + switch (c) + { + case 0: + case '%': + f--; + continue; + case ':': + prv_f = f; + f = va_arg(ap, char*); + va_copy(prv_ap, ap); + va_copy(ap, va_listval(va_arg(ap, va_listarg))); + continue; + case 'c': + p_char = va_arg(ap, char*); + if (!(c = *s) || c == '\n') + { + if (p_char) *p_char = 0; + } + else + { + if (p_char) *p_char = c; + s++; + num++; + } + break; + case 'd': + case 'n': + case 'o': + case 'u': + case 'x': + switch (c) + { + case 'd': + c = 10; + break; + case 'n': + case 'u': + c = 0; + break; + case 'o': + c = 8; + break; + case 'x': + c = 16; + break; + } + if (!*s || *s == '\n') + { + val = 0; + p_char = s; + } + else val = strtol(s, &p_char, c); + switch (q) + { + case 'h': + if (p_short = va_arg(ap, short*)) *p_short = (short)val; + break; + case 'l': + if (p_long = va_arg(ap, long*)) *p_long = val; + break; + default: + if (p_int = va_arg(ap, int*)) *p_int = (int)val; + break; + } + if (s != p_char) + { + s = p_char; + num++; + } + break; + case 'f': + case 'g': + if (!*s || *s == '\n') + { + dval = 0; + p_char = s; + } + else dval = strtod(s, &p_char); + if (p_double = va_arg(ap, double*)) *p_double = dval; + if (s != p_char) + { + s = p_char; + num++; + } + break; + case 's': + p_string = va_arg(ap, char**); + if (q = *f) f++; + if (!*s || *s == '\n') + { + if (p_string) *p_string = s; + } + else s = lextok(s, q, p_string, &num); + break; + case 'v': + p_string = va_arg(ap, char**); + c = va_arg(ap, int); + if (q = *f) f++; + if ((!*s || *s == '\n') && p_string) + { + *p_string = 0; + p_string = 0; + } + while (*s && *s != '\n' && --c > 0) + { + s = lextok(s, q, p_string, &num); + if (p_string) p_string++; + } + if (p_string) *p_string = 0; + break; + } + if (skip) num = onum; + else if (num == onum) + { + if (!num) num = -1; + skip = s; + s = empty; + } + break; + case '\n': + goto done; + default: + if ((*s++ != c) && !skip) + { + skip = s - 1; + s = empty; + } + break; + } + done: + va_end(ap); + if (*s == '\n') *s++ = 0; + if (nxt) *nxt = skip ? skip : s; + return(num); +} diff --git a/src/lib/libast/string/wc2utf8.c b/src/lib/libast/string/wc2utf8.c new file mode 100644 index 0000000..3b70391 --- /dev/null +++ b/src/lib/libast/string/wc2utf8.c @@ -0,0 +1,74 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* and is licensed under the * +* Eclipse Public License, Version 1.0 * +* by AT&T Intellectual Property * +* * +* A copy of the License is available at * +* http://www.eclipse.org/org/documents/epl-v10.html * +* (with md5 checksum b35adb5213ca9657e911e9befb180842) * +* * +* Information and Software Systems Research * +* AT&T Research * +* Florham Park NJ * +* * +* Glenn Fowler <gsf@research.att.com> * +* David Korn <dgk@research.att.com> * +* Phong Vo <kpv@research.att.com> * +* * +***********************************************************************/ +#pragma prototyped +/* + * Glenn Fowler + * AT&T Research + * + * convert wide character to utf8 in s + * s must have room for at least 6 bytes + * number of chars in placed in s returned + * thanks Tom Duff + */ + +#include <ast.h> + +typedef struct Utf8_s +{ + uint32_t range; + unsigned short prefix; + unsigned short shift; +} Utf8_t; + +static const Utf8_t ops[] = +{ + { 0x00000080, 0x00, 0 }, + { 0x00000800, 0xc0, 6 }, + { 0x00010000, 0xe0, 12 }, + { 0x00200000, 0xf0, 18 }, + { 0x04000000, 0xf8, 24 }, + { 0x80000000, 0xfc, 30 } +}; + +int +wc2utf8(register char* s, register uint32_t w) +{ + register int i; + char* b; + + for (i = 0; i < elementsof(ops); i++) + if (w < ops[i].range) + { + b = s; + *s++ = ops[i].prefix | (w >> ops[i].shift); + switch (ops[i].shift) + { + case 30: *s++ = 0x80 | ((w >> 24) & 0x3f); + case 24: *s++ = 0x80 | ((w >> 18) & 0x3f); + case 18: *s++ = 0x80 | ((w >> 12) & 0x3f); + case 12: *s++ = 0x80 | ((w >> 6) & 0x3f); + case 6: *s++ = 0x80 | (w & 0x3f); + } + return s - b; + } + return 0; +} |