Пример #1
0
Cell *program(Node **a, int n)	/* execute an awk program */
{				/* a[0] = BEGIN, a[1] = body, a[2] = END */
	Cell *x;

	if (setjmp(env) != 0)
		goto ex;
	if (a[0]) {		/* BEGIN */
		x = execute(a[0]);
		if (isexit(x))
			return(true);
		if (isjump(x))
			ERROR "illegal break, continue, next or nextfile from BEGIN" FATAL;
		tempfree(x);
	}
	if (a[1] || a[2])
		while (getrec(record) > 0) {
			x = execute(a[1]);
			if (isexit(x))
				break;
			tempfree(x);
		}
  ex:
	if (setjmp(env) != 0)	/* handles exit within END */
		goto ex1;
	if (a[2]) {		/* END */
		x = execute(a[2]);
		if (isbreak(x) || isnext(x) || iscont(x))
			ERROR "illegal break, continue, next or nextfile from END" FATAL;
		tempfree(x);
	}
  ex1:
	return(true);
}
Пример #2
0
static int iter_aux (lua_State *L) {
    size_t len;
    const char *s = luaL_checklstring(L, 1, &len);
    lua_Integer n = lua_tointeger(L, 2) - 1;
    if (n < 0)  /* first iteration? */
        n = 0;  /* start from here */
    else if (n < (lua_Integer)len) {
        n++;  /* skip current byte */
        while (iscont(s + n)) n++;  /* and its continuations */
    }
    if (n >= (lua_Integer)len)
        return 0;  /* no more codepoints */
    else {
        int code;
        const char *next = utf8_decode(s + n, &code);
        if (next == NULL || iscont(next))
            return luaL_error(L, "invalid UTF-8 code");
        lua_pushinteger(L, n + 1);
        lua_pushinteger(L, code);
        return 2;
    }
}
Пример #3
0
/*
** offset(s, n, [i])  -> index where n-th character counting from
**   position 'i' starts; 0 means character at 'i'.
*/
static int byteoffset (lua_State *L) {
    size_t len;
    const char *s = luaL_checklstring(L, 1, &len);
    lua_Integer n  = luaL_checkinteger(L, 2);
    lua_Integer posi = (n >= 0) ? 1 : len + 1;
    posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
    luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
                  "position out of range");
    if (n == 0) {
        /* find beginning of current byte sequence */
        while (posi > 0 && iscont(s + posi)) posi--;
    }
    else {
        if (iscont(s + posi))
            luaL_error(L, "initial position is a continuation byte");
        if (n < 0) {
            while (n < 0 && posi > 0) {  /* move back */
                do {  /* find beginning of previous character */
                    posi--;
                } while (posi > 0 && iscont(s + posi));
                n++;
            }
        }
        else {
            n--;  /* do not move for 1st character */
            while (n > 0 && posi < (lua_Integer)len) {
                do {  /* find beginning of next character */
                    posi++;
                } while (iscont(s + posi));  /* (cannot pass final '\0') */
                n--;
            }
        }
    }
    if (n == 0)  /* did it find given character? */
        lua_pushinteger(L, posi + 1);
    else  /* no such character */
        lua_pushnil(L);
    return 1;
}