astr astr_fgets(astr as, FILE *f) { int c; assert(as != NULL); astr_clear(as); if ((c = fgetc(f)) == EOF) return NULL; astr_append_char(as, c); while ((c = fgetc(f)) != EOF && c != '\n') astr_append_char(as, c); return as; }
/********************************************************************** Check that astr has enough size to hold n, and realloc to a bigger size if necessary. Here n must be big enough to include the trailing ascii-null if required. The requested n is stored in astr->n. The actual amount allocated may be larger than n, and is stored in astr->n_alloc. ***********************************************************************/ void astr_minsize(struct astring *astr, size_t n) { int n1; bool was_null = (astr->n == 0); assert(astr != NULL); astr->n = n; if (n <= astr->n_alloc) { return; } /* allocated more if this is only a small increase on before: */ n1 = (3*(astr->n_alloc+10)) / 2; astr->n_alloc = (n > n1) ? n : n1; astr->str = (char *)fc_realloc(astr->str, astr->n_alloc); if (was_null) { astr_clear(astr); } }
/************************************************************************** Report a lua error. **************************************************************************/ static int script_report(lua_State *L, int status, const char *code) { if (status) { const char *msg; static struct astring str = ASTRING_INIT; int lineno; if (!(msg = lua_tostring(L, -1))) { msg = "(error with no message)"; } astr_clear(&str); /* Add error message. */ astr_add_line(&str, "lua error:"); astr_add_line(&str, "\t%s", msg); if (code) { /* Add lines around the place the parse error is. */ if (sscanf(msg, "%*[^:]:%d:", &lineno) == 1) { const char *begin, *end; int i; astr_add(&str, "\n"); i = 1; for (begin = code; *begin != '\0';) { int len; end = strchr(begin, '\n'); if (end) { len = end - begin; } else { len = strlen(begin); } if (abs(lineno - i) <= 3) { const char *indicator; indicator = (lineno == i) ? "-->" : " "; astr_add_line(&str, "\t%s%3d:\t%*.*s", indicator, i, len, len, begin); } i++; if (end) { begin = end + 1; } else { break; } } astr_add(&str, "\n"); } } freelog(LOG_ERROR, "%s", str.str); astr_free(&str); lua_pop(L, 1); } return status; }