static int fdset_resize(fdset_t *set, unsigned size) { void *tmp = NULL; MEM_RESIZE(tmp, set->ctx, size * sizeof(void*)); MEM_RESIZE(tmp, set->pfd, size * sizeof(struct pollfd)); MEM_RESIZE(tmp, set->timeout, size * sizeof(timev_t)); set->size = size; return KNOT_EOK; }
/* * accumulates strings managing a buffer for them; * calls to snbuf() must not be interleaved; * used in: * - build() from inc.c to construct full paths; * - concat() from mcr.c to splice tokens and * - derror() from proc.c to collect following tokens */ char *(snbuf)(size_t len, int cp) { static char buf[64], /* size must be power of 2 */ *p = buf; static size_t blen = sizeof(buf); assert(p); if (len == (size_t)-1) { if (p != buf) MEM_FREE(p); } else if (len <= blen) return p; else { blen = (len + sizeof(buf)-1) & ~(sizeof(buf)-1); if (p == buf) { p = MEM_ALLOC(blen); if (cp) strcpy(p, buf); } else MEM_RESIZE(p, blen); } return p; }
/* * accepts #include; * cannot use snbuf() because of a call to inc_start() */ static lex_t *dinclude(const lmap_t *pos) { static char buf[64+1]; /* size must be (power of 2) + 1 */ lex_t *t; const lmap_t *hpos; const char *inc = NULL; char *pbuf = buf, *p; lst_assert(); lex_inc = 1; NEXTSP(t); /* consumes include */ hpos = t->pos; if (t->id == LEX_HEADER) { inc = LEX_SPELL(t); while (1) { NEXTSP(t); /* consumes header or current token */ if (t->id == LEX_NEWLINE) break; assert(t->id != LEX_EOI); if (t->id == LEX_ID && !t->f.blue && mcr_expand(t)) continue; t = xtratok(t); break; } } else { int st = 0; /* initial */ size_t blen, slen; const lmap_t *epos = NULL; while (t->id != LEX_NEWLINE) { assert(t->id != LEX_EOI); epos = t->pos; if (t->id == LEX_ID && !t->f.blue && mcr_expand(t)) { NEXTSP(t); /* consumes expanded id */ continue; } assert(t->id != LEX_NEWLINE); switch(st) { case 0: /* initial */ hpos = t->pos; switch(t->id) { case LEX_SCON: if (t->spell[0] == 'L') { default: SKIPNL(t); continue; } /* no break */ case LEX_HEADER: st = 1; inc = LEX_SPELL(t); break; case '<': assert(pbuf == buf); st = 2; p = pbuf; blen = sizeof(buf) - 1; *p++ = '<'; break; } break; case 1: /* extra tokens */ t = xtratok(t); continue; case 2: /* < seen */ slen = strlen(t->spell); if (slen > blen - (p-pbuf)) { const char *oldp = pbuf; blen += ((slen + NELEM(buf)-2) & ~(size_t)(NELEM(buf)-2)); if (pbuf == buf) { pbuf = MEM_ALLOC(blen + 1); strcpy(pbuf, oldp); } else MEM_RESIZE(pbuf, blen + 1); p = pbuf + (p - oldp); } strcpy(p, t->spell); p += slen; if (t->id == '>') { st = 1; inc = pbuf; err_dpos(lmap_range(hpos, t->pos), ERR_PP_COMBINEHDR); } break; default: assert(!"invalid state -- should never reach here"); break; } NEXTSP(t); /* consumes handled token */ } if (inc) hpos = lmap_range(hpos, epos); else err_dpos(lmap_after(pos), ERR_PP_NOHEADER); } if (!inc || !inc_start(inc, hpos)) in_nextline(); /* because lex_inc set */ else if (main_opt()->pponly) { t->f.sync = 1; lst_output(lst_copy(t, 0, strg_line)); } if (pbuf != buf) MEM_FREE(pbuf); lex_inc = 0; return t; }
/* * recognizes character constants; * ASSUMPTION: signed/unsigned integers are compatible on the host */ ux_t (clx_ccon)(lex_t *t, int *w) { int cbyte; sz_t len = 0; const char *ss, *s, *e; ux_t lim, c; #ifdef HAVE_ICONV iconv_t *cd; #endif /* HAVE_ICONV */ assert(t); assert(w); assert(BUFUNIT > 2); assert(ty_wuchartype); /* ensures types initialized */ assert(xgeu(xmxu, TG_UCHAR_MAX)); assert(xgeu(xmxu, TG_WUCHAR_MAX)); assert(ir_cur); ss = s = LEX_SPELL(t); if (*s == 'L') *w = 1, s++; e = ++s; /* skips ' */ if (*w) { cbyte = ty_wchartype->size; assert(cbyte <= BUFUNIT); lim = TG_WUCHAR_MAX; #ifdef HAVE_ICONV cd = main_ntow; #endif /* HAVE_ICONV */ } else { cbyte = 1; lim = TG_UCHAR_MAX; #ifdef HAVE_ICONV cd = main_ntoe; #endif /* HAVE_ICONV */ } switch(*e) { case '\'': /* empty; diagnosed elsewhere */ case '\0': /* unclosed; diagnosed elsewhere */ return xO; case '\\': /* escape sequences */ assert(sizeof(c) >= cbyte); c = lex_bs(t, ss, &e, lim, "character constant"); #if HAVE_ICONV if (cd && !(s[1] == 'x' || (s[1] >= '0' && s[1] <= '7'))) { char x = xnu(c); ICONV_DECL(&x, 1); assert(xe(xiu(x), c)); obuf = strg_sbuf, obufv = strg_sbuf; olen = strg_slen, olenv = strg_slen; ICONV_DO(cd, 1, {}); strg_sbuf = obuf, strg_slen = olen; /* for later reuse */ len = strg_slen - len - olenv; } else { #else /* !HAVE_ICONV */ { #endif /* HAVE_ICONV */ if (*w) memcpy(strg_sbuf, (char *)&c + ((LITTLE)? 0: sizeof(c)-cbyte), cbyte); else strg_sbuf[0] = xnu(c); len = cbyte; } break; default: /* ordinary chars */ #ifdef HAVE_ICONV if (cd) { do { e++; } while(!FIRSTUTF8(*e)); { ICONV_DECL((char *)s, e - s); obuf = strg_sbuf, obufv = strg_sbuf; olen = strg_slen, olenv = strg_slen; ICONV_DO(cd, 1, {}); strg_sbuf = obuf, strg_slen = olen; /* for later reuse */ len = strg_slen - len - olenv; } } else { #else /* !HAVE_ICONV */ { #endif /* HAVE_ICONV */ assert(TG_CHAR_BIT == CHAR_BIT); if (*w) { strg_sbuf[(LITTLE)? 0: cbyte-1] = *e; memset(strg_sbuf + ((LITTLE)? 1: 0), 0, cbyte-1); } else strg_sbuf[0] = *e; e++; len = cbyte; } break; } if (*e != '\'' && *e != '\0') { for (s = e; *e != '\0' && *e != '\''; e++) continue; err_dpos((FIRSTUTF8(*s))? lmap_spell(t, ss, s, e): t->pos, ERR_CONST_LARGECHAR); } else if (len != cbyte) { err_dpos(t->pos, (*w)? ERR_CONST_WIDENOTFIT: ERR_CONST_MBNOTFIT); return xO; } c = xO; memcpy(&c, strg_sbuf + ((LITTLE)? 0: sizeof(c)-cbyte), cbyte); if (*w) { switch(main_opt()->wchart) { case 0: /* long */ c = SYM_CROPSL(c); break; case 1: /* ushort */ c = SYM_CROPUS(c); break; case 2: /* int */ c = SYM_CROPSI(c); break; } } else /* int from char */ c = (main_opt()->uchar)? SYM_CROPUC(c): SYM_CROPSC(c); return c; } /* * recognizes string literals */ static sz_t scon(lex_t *t, int *w) { int cbyte; lex_t *n; sz_t clen = 0, len = 0; const char *ss, *s, *e; ux_t lim; #ifdef HAVE_ICONV iconv_t *cd; #endif assert(t); assert(w); assert(BUFUNIT > 2); assert(ty_wuchartype); /* ensures types initialized */ assert(xgeu(xmxu, TG_UCHAR_MAX)); assert(xgeu(xmxu, TG_WUCHAR_MAX)); assert(ir_cur); ss = s = LEX_SPELL(t); if (*s == 'L') *w = 1, s++; e = ++s; /* skips " */ while ((n = lst_peekns())->id == LEX_SCON) { if (*n->spell == 'L') { if (!*w) { /* mb + wide = wide */ err_dmpos(n->pos, ERR_CONST_MBWIDE, t->pos, NULL); err_dmpos(n->pos, ERR_CONST_MBWIDESTD, t->pos, NULL); *w = 2; /* silences warnings */ } } else if (*w == 1) { /* wide + mb = wide */ err_dmpos(n->pos, ERR_CONST_MBWIDE, t->pos, NULL); err_dmpos(n->pos, ERR_CONST_MBWIDESTD, t->pos, NULL); *w = 2; /* silences warnings */ } while ((t = lst_append(t, lst_next()))->id != LEX_SCON) continue; } clx_cpos = lmap_range(t->next->pos, t->pos); if (*w) { cbyte = ty_wchartype->size; assert(cbyte <= BUFUNIT); lim = TG_WUCHAR_MAX; #ifdef HAVE_ICONV cd = main_ntow; #endif /* HAVE_ICONV */ } else { cbyte = 1; lim = TG_UCHAR_MAX; #ifdef HAVE_ICONV cd = main_ntoe; #endif /* HAVE_ICONV */ } n = t->next; while (1) { while (1) { while (*e != '\\' && *e != '"' && *e != '\0') e++; if (e > s) { /* ordinary chars */ #ifdef HAVE_ICONV if (cd) { ICONV_DECL((char *)s, e - s); obuf = strg_sbuf, obufv = strg_sbuf + len; olen = strg_slen, olenv = strg_slen - len; ICONV_INIT(cd); ICONV_DO(cd, 0, {}); strg_sbuf = obuf, strg_slen = olen; /* for later reuse */ len += (strg_slen - len - olenv); } else { #else /* !HAVE_ICONV */ { #endif /* HAVE_ICONV */ sz_t d = e - s; assert(TG_CHAR_BIT == CHAR_BIT); while (len + (d*cbyte) > strg_slen) /* rarely iterates */ MEM_RESIZE(strg_sbuf, strg_slen += BUFUNIT); if (*w) { while (s < e) { strg_sbuf[len + ((ir_cur->f.little_endian)? 0: cbyte-1)] = *s++; memset(strg_sbuf + len + ((ir_cur->f.little_endian)? 1: 0), 0, cbyte-1); len += cbyte; } } else { memcpy(&strg_sbuf[len], s, d); len += d; } } for (; s < e; s++) if (FIRSTUTF8(*s)) clen++; } if (*e == '\\') { /* escape sequences */ ux_t c; assert(sizeof(c) >= cbyte); c = lex_bs(n, ss, &e, lim, "string literal"); #if HAVE_ICONV if (cd) { /* inserts initial seq before every esc seq */ ICONV_DECL(NULL, 0); UNUSED(ilenv); UNUSED(ibufv); obuf = strg_sbuf, obufv = strg_sbuf + len; olen = strg_slen, olenv = strg_slen - len; ICONV_INIT(cd); strg_sbuf = obuf, strg_slen = olen; /* for later reuse */ len += (strg_slen - len - olenv); } if (cd && !(s[1] == 'x' || (s[1] >= '0' && s[1] <= '7'))) { char x = xnu(c); ICONV_DECL(&x, 1); assert(xe(xiu(x), c)); obuf = strg_sbuf, obufv = strg_sbuf + len; olen = strg_slen, olenv = strg_slen - len; ICONV_DO(cd, 0, {}); strg_sbuf = obuf, strg_slen = olen; /* for later reuse */ len += (strg_slen - len - olenv); } else { #else /* !HAVE_ICONV */ { #endif /* HAVE_ICONV */ if (len + cbyte > strg_slen) MEM_RESIZE(strg_sbuf, strg_slen += BUFUNIT); if (*w) { if (LITTLE != ir_cur->f.little_endian) CHGENDIAN(c, sizeof(c)); memcpy(strg_sbuf+len, &c + ((ir_cur->f.little_endian)? 0: sizeof(c)-cbyte), cbyte); len += cbyte; } else strg_sbuf[len++] = xnu(c); } clen++; s = e; continue; } break; /* " or NUL */ } if (n == t) { if (len + cbyte > strg_slen) MEM_RESIZE(strg_sbuf, strg_slen += BUFUNIT); memset(strg_sbuf+len, 0, cbyte); len += cbyte; clen++; break; } while ((n = n->next)->id != LEX_SCON) if (n->id < 0) strg_free((arena_t *)n->spell); ss = s = LEX_SPELL(n); if (*s == 'L') s++; e = ++s; /* skips " */ } if (len % cbyte != 0) err_dpos(clx_cpos, ERR_CONST_WIDENOTFIT); if (*w) clen = (len /= cbyte); if (clen - 1 > TL_STR_STD) { /* -1 for NUL; note TL_STR_STD may warp around */ err_dpos(clx_cpos, ERR_CONST_LONGSTR); err_dpos(clx_cpos, ERR_CONST_LONGSTRSTD, (unsigned long)TL_STR_STD); } return len; } #define N 0 /* no suffix */ #define U 1 /* suffix: U */ #define L 2 /* suffix: L */ #define X 3 /* suffix: UL */ #ifdef SUPPORT_LL #define M 4 /* suffix: LL */ #define Z 5 /* suffix: ULL */ #endif /* SUPPORT_LL */ #define D 0 /* base: decimal */ #define H 1 /* base: octal or hexadecimal */ /* * determines the type of an integer constant */ static const char *icon(const char *cs, ux_t n, int ovf, int base, const lmap_t *pos) { static struct tab { ux_t limit; ty_t *type; #ifdef SUPPORT_LL } tab[Z+1][H+1][7]; #else /* !SUPPORT_LL */ } tab[X+1][H+1][5]; #endif /* SUPPORT_LL */ int suffix; struct tab *p; assert(cs); assert(pos); assert(ty_inttype); #ifdef SUPPORT_LL assert(xgeu(xmxu, TG_ULLONG_MAX)); #else /* !SUPPORT_LL */ assert(xgeu(xmxu, TG_ULONG_MAX)); #endif /* SUPPORT_LL */ if (xe(tab[N][D][0].limit, xO)) { /* no suffix, decimal; different in C90 */ tab[N][D][0].limit = TG_INT_MAX; tab[N][D][0].type = ty_inttype; tab[N][D][1].limit = TG_LONG_MAX; tab[N][D][1].type = ty_longtype; #ifdef SUPPORT_LL tab[N][D][2].limit = TG_LLONG_MAX; tab[N][D][2].type = ty_llongtype; tab[N][D][3].limit = TG_ULLONG_MAX; tab[N][D][3].type = ty_ullongtype; tab[N][D][4].limit = xmxu; tab[N][D][4].type = ty_inttype; #else /* SUPPORT_LL */ tab[N][D][2].limit = TG_ULONG_MAX; tab[N][D][2].type = ty_ulongtype; tab[N][D][3].limit = xmxu; tab[N][D][3].type = ty_inttype; #endif /* SUPPORT_LL */ /* no suffix, octal or hex */ tab[N][H][0].limit = TG_INT_MAX; tab[N][H][0].type = ty_inttype; tab[N][H][1].limit = TG_UINT_MAX; tab[N][H][1].type = ty_unsignedtype; tab[N][H][2].limit = TG_LONG_MAX; tab[N][H][2].type = ty_longtype; tab[N][H][3].limit = TG_ULONG_MAX; tab[N][H][3].type = ty_ulongtype; #ifdef SUPPORT_LL tab[N][H][4].limit = TG_LLONG_MAX; tab[N][H][4].type = ty_llongtype; tab[N][H][5].limit = TG_ULLONG_MAX; tab[N][H][5].type = ty_ullongtype; tab[N][H][6].limit = xmxu; tab[N][H][6].type = ty_inttype; #else /* !SUPPORT_LL */ tab[N][H][4].limit = xmxu; tab[N][H][4].type = ty_inttype; #endif /* SUPPORT_LL */ /* U, decimal, octal or hex */ tab[U][H][0].limit = tab[U][D][0].limit = TG_UINT_MAX; tab[U][H][0].type = tab[U][D][0].type = ty_unsignedtype; tab[U][H][1].limit = tab[U][D][1].limit = TG_ULONG_MAX; tab[U][H][1].type = tab[U][D][1].type = ty_ulongtype; #ifdef SUPPORT_LL tab[U][H][2].limit = tab[U][D][2].limit = TG_ULLONG_MAX; tab[U][H][2].type = tab[U][D][2].type = ty_ullongtype; tab[U][H][3].limit = tab[U][D][3].limit = xmxu; tab[U][H][3].type = tab[U][D][3].type = ty_inttype; #else /* !SUPPORT_LL */ tab[U][H][2].limit = tab[U][D][2].limit = xmxu; tab[U][H][2].type = tab[U][D][2].type = ty_inttype; #endif /* SUPPORT_LL */ /* L, decimal; different in C90 */ tab[L][D][0].limit = TG_LONG_MAX; tab[L][D][0].type = ty_longtype; #ifdef SUPPORT_LL tab[L][D][1].limit = TG_LLONG_MAX; tab[L][D][1].type = ty_llongtype; tab[L][D][2].limit = TG_ULLONG_MAX; tab[L][D][2].type = ty_ullongtype; tab[L][D][3].limit = xmxu; tab[L][D][3].type = ty_inttype; #else /* !SUPPORT_LL */ tab[L][D][1].limit = TG_ULONG_MAX; tab[L][D][1].type = ty_ulongtype; tab[L][D][2].limit = xmxu; tab[L][D][2].type = ty_inttype; #endif /* SUPPORT_LL */ /* L, octal or hex */ tab[L][H][0].limit = TG_LONG_MAX; tab[L][H][0].type = ty_longtype; tab[L][H][1].limit = TG_ULONG_MAX; tab[L][H][1].type = ty_ulongtype; #ifdef SUPPORT_LL tab[L][H][2].limit = TG_LLONG_MAX; tab[L][H][2].type = ty_llongtype; tab[L][H][3].limit = TG_ULLONG_MAX; tab[L][H][3].type = ty_ullongtype; tab[L][H][4].limit = xmxu; tab[L][H][4].type = ty_inttype; #else /* !SUPPORT_LL */ tab[L][H][2].limit = xmxu; tab[L][H][2].type = ty_inttype; #endif /* SUPPORT_LL */ /* UL, decimal, octal or hex */ tab[X][H][0].limit = tab[X][D][0].limit = TG_ULONG_MAX; tab[X][H][0].type = tab[X][D][0].type = ty_ulongtype; #ifdef SUPPORT_LL tab[X][H][1].limit = tab[X][D][1].limit = TG_ULLONG_MAX; tab[X][H][1].type = tab[X][D][1].type = ty_ullongtype; tab[X][H][2].limit = tab[X][D][2].limit = xmxu; tab[X][H][2].type = tab[X][D][2].type = ty_inttype; #else /* !SUPPORT_LL */ tab[X][H][1].limit = tab[X][D][1].limit = xmxu; tab[X][H][1].type = tab[X][D][1].type = ty_inttype; #endif /* SUPPORT_LL */ #ifdef SUPPORT_LL /* LL, decimal, octal or hex */ tab[M][H][0].limit = tab[M][D][0].limit = TG_LLONG_MAX; tab[M][H][0].type = tab[M][D][0].type = ty_llongtype; tab[M][H][1].limit = tab[M][D][1].limit = TG_ULLONG_MAX; tab[M][H][1].type = tab[M][D][1].type = ty_ullongtype; tab[M][H][2].limit = tab[M][D][2].limit = xmxu; tab[M][H][2].type = tab[M][D][2].type = ty_inttype; /* ULL, decimal, octal or hex */ tab[Z][H][0].limit = tab[Z][D][0].limit = TG_ULLONG_MAX; tab[Z][H][0].type = tab[Z][D][0].type = ty_ullongtype; tab[Z][H][1].limit = tab[Z][D][1].limit = xmxu; tab[Z][H][1].type = tab[Z][D][1].type = ty_inttype; #endif /* SUPPORT_LL */ } base = (base == 10)? D: H; suffix = N; if (tolower((unsigned char)cs[0]) == 'l') { #ifdef SUPPORT_LL if (cs[1] == cs[0]) cs += 2, suffix = M; else #endif /* SUPPORT_LL */ cs++, suffix = L; } if (tolower((unsigned char)cs[0]) == 'u') cs++, suffix++; if (suffix <= U && tolower((unsigned char)cs[0]) == 'l') { #ifdef SUPPORT_LL if (cs[1] == cs[0]) cs += 2, suffix += M; else #endif /* SUPPORT_LL */ cs++, suffix += L; } for (p = tab[suffix][base]; xgu(n, p->limit); p++) continue; if (ovf || (xe(p->limit, xmxu) && p->type == ty_inttype)) { err_dpos(pos, ERR_CONST_LARGEINT); #ifdef SUPPORT_LL n = TG_ULLONG_MAX; tval.type = ty_ullongtype; #else /* !SUPPORT_LL */ n = TG_ULONG_MAX; tval.type = ty_ulongtype; #endif /* SUPPORT_LL */ } else tval.type = p->type; #ifdef SUPPORT_LL if (suffix % 2 == 0 && base == D && TY_ISUNSIGN(p->type)) err_dpos(pos, ERR_CONST_LARGEUNSIGN); else if (tval.type == ty_llongtype && (suffix == N || suffix == L) && xleu(n, TG_ULONG_MAX)) { err_dpos(pos, ERR_CONST_UNSIGNINC90); if (main_opt()->std == 1) tval.type = ty_ulongtype; } if ((TY_ISLLONG(tval.type) || TY_ISULLONG(tval.type))) err_dpos(pos, ERR_CONST_LLONGINC90, tval.type); #endif /* SUPPORT_LL */ #ifdef SUPPORT_LL if (tval.type->op == TY_INT || tval.type->op == TY_LONG || tval.type->op == TY_LLONG) #else /* !SUPPORT_LL */ if (tval.type->op == TY_INT || tval.type->op == TY_LONG) #endif /* SUPPORT_LL */ tval.u.c.v.s = n; else tval.u.c.v.u = n; return cs; } #undef H #undef D #undef Z #undef M #undef X #undef L #undef U #undef N /* * determines the type of a floating constant; * ASSUMPTION: fp types of the host are same as those of the target */ static const char *fcon(const char *cs, long double ld, const lmap_t *pos) { assert(cs); assert(pos); assert(ty_floattype); /* ensures types initiailized */ switch(*cs) { case 'f': case 'F': cs++; /* skips f or F */ if ((OVF(ld) && errno == ERANGE) || ld > TG_FLT_MAX) { err_dpos(pos, ERR_CONST_LARGEFP); ld = TG_FLT_MAX; } else if ((ld == 0.0 && errno == ERANGE) || (ld > 0.0 && ld < TG_FLT_MIN)) { err_dpos(pos, ERR_CONST_TRUNCFP); ld = 0.0f; } tval.type = ty_floattype; tval.u.c.v.f = (float)ld; break; case 'l': case 'L': cs++; /* skips l or L */ if ((OVF(ld) && errno == ERANGE) || ld > TG_LDBL_MAX) { err_dpos(pos, ERR_CONST_LARGEFP); ld = TG_LDBL_MAX; } else if ((ld == 0.0 && errno == ERANGE) || (ld > 0.0 && ld < TG_LDBL_MIN)) err_dpos(pos, ERR_CONST_TRUNCFP); tval.type = ty_ldoubletype; tval.u.c.v.ld = (long double)ld; break; default: if ((OVF(ld) && errno == ERANGE) || ld > TG_DBL_MAX) { err_dpos(pos, ERR_CONST_LARGEFP); ld = (double)TG_DBL_MAX; } else if ((ld == 0.0 && errno == ERANGE) || (ld > 0.0 && ld < TG_DBL_MIN)) { err_dpos(pos, ERR_CONST_TRUNCFP); ld = 0.0; } tval.type = ty_doubletype; tval.u.c.v.d = (double)ld; break; } return cs; } /* * recognizes integer or floating constants; * ASSUMPTION: strtold() supported on the host */ static int ifcon(lex_t *t) { ux_t n; int b, d; long double ld; int err = 0, ovf = 0; const char *ss, *s; char *p = "0123456789abcdef"; /* no const for reuse with strtold() */ assert(t); ss = s = LEX_SPELL(t); if (*s == '.') goto fcon; n = xO; if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && isxdigit(((unsigned char *)s)[2])) { /* 0x[0-9] */ b = 16; s++; /* skips 0 */ while (isxdigit(*(unsigned char *)++s)) { d = strchr(p, tolower(*(unsigned char *)s)) - p; if (xt(xba(n, xbc(xsrl(xmxu, 4))))) ovf = 1; n = xau(xsl(n, 4), xiu(d)); } s = icon(s, n, ovf, b, t->pos); b = LEX_ICON; } else { /* 0 or other digits */ b = (*s == '0')? 8: 10; if (b == 8) while (isdigit(*(unsigned char *)s)) { d = *s++ - '0'; if (*s == '8' || *s == '9') p = (char *)s, err = 1; if (xt(xba(n, xbc(xsrl(xmxu, 3))))) ovf = 1; n = xau(xsl(n, 3), xiu(d)); } else /* b == 10 */ while (isdigit(*(unsigned char *)s)) { d = *s++ - '0'; if (xgu(n, xdu(xsu(xmxu, xiu(d)), xiu(10)))) ovf = 1; n = xau(xmu(xiu(10), n), xiu(d)); } fcon: if (b != 16 && (*s == '.' || *s == 'e' || *s == 'E')) { if (*s == '.') do { s++; /* skips . and digits */ } while(isdigit(*s)); if (*s == 'e' || *s == 'E') { if (*++s == '-' || *s == '+') /* skips e or E */ s++; /* skips - or + */ if (!isdigit(*s)) { err_dpos(lmap_spell(t, ss, s, s+1), ERR_CONST_NOEXPDIG); err = 1; } } if (!err) { errno = 0; ld = strtold(ss, &p); s = fcon((s = p), ld, t->pos); } b = LEX_FCON; } else { if (err) err_dpos(lmap_spell(t, ss, p, p+1), ERR_CONST_ILLOCTESC); else s = icon(s, n, ovf, b, t->pos); b = LEX_ICON; } } if (*s && !err) { for (p = (char *)s; *p; p++) continue; err_dpos(lmap_spell(t, ss, s, p), ERR_CONST_PPNUMSFX, s, b); err = 1; } clx_sym = (err)? NULL: &tval; return b; }
/* * reads a configuration file and constructs the configuration data * * A tricky construct using do-while for reading lines from a file is to allow for a configuration * file that does not end with a newline. Asserting that BUFLEN is greater than 1 is necessary to * prevent fgets() from returning an empty string even if there are still characters unread. * TODO: * - some adjustment on arguments to table_new() is necessary */ size_t (conf_init)(FILE *fp, int ctrl) { char *p; size_t buflen, len; size_t lineno, nlineno; const char *hkey; table_t *tab; struct valnode_t *pnode; assert(fp); assert(preset || !section); assert(!current); assert(BUFLEN > 1); if (!preset) { control = ctrl; /* if preset, ctrl ignored */ section = table_new(0, NULL, NULL); } tab = NULL; errcode = CONF_ERR_OK; lineno = nlineno = 0; len = 0; p = MEM_ALLOC(buflen=BUFLEN); *p = '\0'; do { assert(buflen - len > 1); fgets(p+len, buflen-len, fp); if (ferror(fp)) { errcode = CONF_ERR_IO; break; } len += strlen(p+len); if (len == 0) /* EOF and no remaining data */ break; if (len > 2 && p[len-2] == '\\' && p[len-1] == '\n') { /* line splicing */ int c; nlineno++; len -= 2; if (len > 0 && isspace(p[len-1])) { while (--len > 0 && isspace(p[len-1])) continue; p[len++] = ' '; } if ((c = getc(fp)) != EOF && c != '\n' && isspace(c)) { if (p[len-1] != ' ') p[len++] = ' '; while ((c = getc(fp)) != EOF && c != '\n' && isspace(c)) continue; } else if (c == EOF) { /* no following line for slicing */ lineno += nlineno; errcode = CONF_ERR_BSLASH; goto retcode; } ungetc(c, fp); p[len] = '\0'; /* at worst, backslash is replaced with space and newline with null, thus room for two characters, which guarantees buflen-len greater than 1 */ } else if (p[len-1] == '\n' || feof(fp)) { /* line completed */ int type; char *s, *t, *var, *val; lineno++; if (p[len-1] == '\n') p[--len] = '\0'; /* in fact, no need to adjust len */ var = triml(p); switch(*var) { case '[': /* section specification */ cmtrem(++var); s = strchr(var, ']'); if (!s) { errcode = CONF_ERR_LINE; goto retcode; } *s = '\0'; if (sepunit(var, &var) != CONF_ERR_OK) goto retcode; /* sepunit sets errcode */ hkey = hash_string((control & CONF_OPT_CASE)? var: lower(var)); tab = table_get(section, hkey); if (!tab) { if (preset) { errcode = CONF_ERR_SEC; goto retcode; } tab = table_new(0, NULL, NULL); } table_put(section, hkey, tab); /* no break */ case '\0': /* empty line */ case '#': /* comment-only line */ case ';': break; /* reuses existing buffer */ default: /* variable = value */ val = p + strcspn(p, "#;"); s = strchr(var, '='); if (!s || val < s) { errcode = CONF_ERR_LINE; goto retcode; } *s = '\0'; if (sepunit(var, &var) != CONF_ERR_OK) goto retcode; /* sepunit() sets errcode */ if (*(unsigned char *)var == '\0') { /* empty variable name */ errcode = CONF_ERR_VAR; goto retcode; } val = triml(++s); if (*val == '"' || *val == '\'') { /* quoted */ char end = *val; /* remembers for match */ t = s = ++val; /* starts copying from s to t */ do { switch(*s) { case '\\': /* escape sequence */ if (control & CONF_OPT_ESC) *(unsigned char *)t++ = escseq(*(unsigned char *)++s); else { *t++ = '\\'; *(unsigned char *)t++ = *(unsigned char *)++s; } break; case '\0': /* unclosed ' or " */ errcode = CONF_ERR_LINE; goto retcode; case '\'': case '\"': if (*s == end) { *t = '\0'; break; } /* no break */ default: /* literal copy */ *(unsigned char *)t++ = *(unsigned char *)s; break; } s++; } while(*(unsigned char *)t != '\0'); /* checks if any trailing invalid char */ cmtrem(s); trimt(s); if (*(unsigned char *)s != '\0') { errcode = CONF_ERR_LINE; goto retcode; } } else { /* unquoted */ cmtrem(val); trimt(val); } if (!tab) { /* global section */ hkey = hash_string(""); tab = table_get(section, hkey); if (!tab) { if (preset) { errcode = CONF_ERR_SEC; goto retcode; } tab = table_new(0, NULL, NULL); } table_put(section, hkey, tab); } hkey = hash_string((control & CONF_OPT_CASE)? var: lower(var)); if (preset) { pnode = table_get(tab, hkey); if (!pnode) { errcode = CONF_ERR_VAR; goto retcode; } type = pnode->type; } else type = CONF_TYPE_STR; MEM_NEW(pnode); pnode->freep = p; pnode->type = type; pnode->val = val; pnode = table_put(tab, hkey, pnode); if (pnode) { /* value overwritten, thus free */ MEM_FREE(pnode->freep); MEM_FREE(pnode); } p = MEM_ALLOC(buflen=BUFLEN); /* uses new buffer */ break; } len = 0; *p = '\0'; lineno += nlineno; /* adjusts line number */ nlineno = 0; } else /* expands buffer */ MEM_RESIZE(p, buflen+=BUFLEN); } while(!feof(fp)); retcode: MEM_FREE(p); return (errcode != CONF_ERR_OK)? lineno: 0; }