static void set_proto_hdlc(int eth) { unsigned int enc = 0, par = 0; raw_hdlc_proto raw; memset(&raw, 0, sizeof(raw)); while (argc > 0) { if (!enc) if (!checktab(hdlc_enc, &enc)) continue; if (!par) if (!checktab(hdlc_par, &par)) continue; error("Invalid parameter: %s\n", argv[0]); } if (!enc) raw.encoding = ENCODING_DEFAULT; else raw.encoding = enc; if (!par) raw.parity = ENCODING_DEFAULT; else raw.parity = par; req.ifr_settings.ifs_ifsu.raw_hdlc = &raw; req.ifr_settings.size = sizeof(raw); if (ioctl(sock, SIOCWANDEV, &req)) error("Unable to set HDLC%s protocol information: %s\n", eth ? "-ETH" : "", strerror(errno)); }
/* ** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever ** possible, copy in increasing order, which is better for rehashing. ** "possible" means destination after original range, or smaller ** than origin, or copying to another table. */ static int tmove (lua_State *L) { lua_Integer f = luaL_checkinteger(L, 2); lua_Integer e = luaL_checkinteger(L, 3); lua_Integer t = luaL_checkinteger(L, 4); int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ checktab(L, 1, TAB_R); checktab(L, tt, TAB_W); if (e >= f) { /* otherwise, nothing to move */ lua_Integer n, i; luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, "too many elements to move"); n = e - f + 1; /* number of elements to move */ luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, "destination wrap around"); if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) { for (i = 0; i < n; i++) { lua_geti(L, 1, f + i); lua_seti(L, tt, t + i); } } else { for (i = n - 1; i >= 0; i--) { lua_geti(L, 1, f + i); lua_seti(L, tt, t + i); } } } lua_pushvalue(L, tt); /* return destination table */ return 1; }
static void set_iface(void) { int orig_argc = argc; te1_settings te1; memset(&te1, 0, sizeof(te1)); req.ifr_settings.type = IF_IFACE_SYNC_SERIAL; while (argc > 0) { if (req.ifr_settings.type == IF_IFACE_SYNC_SERIAL) if (!checktab(ifaces, &req.ifr_settings.type)) continue; if (!te1.clock_type) if (!checkkey("clock")) { if (!checktab(clocks, &te1.clock_type)) continue; error("Invalid clock type\n"); } if (!te1.clock_rate && (te1.clock_type == CLOCK_INT || te1.clock_type == CLOCK_TXINT)) if (!match("rate", &te1.clock_rate, 1, 0xFFFFFFFF)) continue; if (!te1.loopback) { if (!checkkey("loopback") || !checkkey("lb")) { te1.loopback = 1; continue; } } /* slotmap goes here */ if (orig_argc == argc) return; /* not an iface definition */ error("Invalid parameter: %s\n", argv[0]); } if (!te1.clock_rate && (te1.clock_type == CLOCK_INT || te1.clock_type == CLOCK_TXINT)) te1.clock_rate = 64000; /* FIXME stupid hack, will remove it later */ req.ifr_settings.ifs_ifsu.te1 = &te1; if (req.ifr_settings.type == IF_IFACE_E1 || req.ifr_settings.type == IF_IFACE_T1) req.ifr_settings.size = sizeof(te1_settings); else req.ifr_settings.size = sizeof(sync_serial_settings); if (ioctl(sock, SIOCWANDEV, &req)) error("Unable to set interface information: %s\n", strerror(errno)); exit(0); }
static void set_pvc(void) { char *op = argv[0]; parsertab ops[] = {{ "create", IF_PROTO_FR_ADD_PVC }, { "delete", IF_PROTO_FR_DEL_PVC }, { NULL, 0 }}; fr_proto_pvc pvc; memset(&pvc, 0, sizeof(pvc)); if (checktab(ops, &req.ifr_settings.type)) return; #ifdef IF_PROTO_FR_ETH_PVC if (!match("ether", &pvc.dlci, 0, 1023)) { if (req.ifr_settings.type == IF_PROTO_FR_ADD_PVC) req.ifr_settings.type = IF_PROTO_FR_ADD_ETH_PVC; else req.ifr_settings.type = IF_PROTO_FR_DEL_ETH_PVC; } else #endif if (match(NULL, &pvc.dlci, 0, 1023)) return; if (argc != 0) return; req.ifr_settings.ifs_ifsu.fr_pvc = &pvc; req.ifr_settings.size = sizeof(pvc); if (ioctl(sock, SIOCWANDEV, &req)) error("Unable to %s PVC: %s\n", op, strerror(errno)); exit(0); }
static void set_proto(void) { if (checktab(protos, &req.ifr_settings.type)) return; switch(req.ifr_settings.type) { case IF_PROTO_HDLC: set_proto_hdlc(0); break; #ifdef IF_PROTO_HDLC_ETH case IF_PROTO_HDLC_ETH: set_proto_hdlc(1); break; #endif case IF_PROTO_CISCO: set_proto_cisco(); break; case IF_PROTO_FR: set_proto_fr(); break; case IF_PROTO_PPP: case IF_PROTO_X25: req.ifr_settings.ifs_ifsu.sync = NULL; /* FIXME */ req.ifr_settings.size = 0; if (!ioctl(sock, SIOCWANDEV, &req)) break; error("Unable to set %s protocol information: %s\n", req.ifr_settings.type == IF_PROTO_PPP ? "PPP" : "X.25", strerror(errno)); default: error("Unknown protocol %u\n", req.ifr_settings.type); } if (argc > 0) error("Unexpected parameter: %s\n", argv[0]); close(sock); exit(0); }
static int treverse (lua_State *L) { lua_Integer begin, end; checktab(L, 1, TAB_RW); begin = luaL_optinteger(L, 2, 1); end = luaL_opt(L, luaL_checkinteger, 3, check_n(L, 1)); lua_settop(L, 1); reverse(L, begin, end); return 0; }
int checkline(char *tetri, int *tab) { if (tetri[4] != '\n' || tetri[9] != '\n' || tetri[14] != '\n' || tetri[19] != '\n' || !(checktab(tab)) || (ft_strlen(tetri) == 21 && tetri[20] != '\n')) { free(tab); return (0); } free(tab); return (1); }
static int tshuffle (lua_State *L) { lua_Integer begin, end; checktab(L, 1, TAB_RW); begin = luaL_optinteger(L, 2, 1); end = luaL_opt(L, luaL_checkinteger, 3, check_n(L, 1)); while (end >= begin) { double f = l_rand() * (1.0/(L_RANDMAX+1.0)); lua_Integer j = begin + (lua_Integer)(f * (end-begin+1)); lua_geti(L, 1, end); lua_geti(L, 1, j); lua_seti(L, 1, end); lua_seti(L, 1, j); --end; } return 0; }
static int unpack (lua_State *L) { TabA ta; lua_Integer i, e; lua_Unsigned n; checktab(L, 1, &ta); i = luaL_optinteger(L, 2, 1); e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) return luaL_error(L, "too many results to unpack"); do { /* must have at least one element */ (*ta.geti)(L, 1, i); /* push arg[i..e] */ } while (i++ < e); return (int)n; }
static int tconcat (lua_State *L) { TabA ta; luaL_Buffer b; size_t lsep; lua_Integer i, last; const char *sep = luaL_optlstring(L, 2, "", &lsep); checktab(L, 1, &ta); i = luaL_optinteger(L, 3, 1); last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1)); luaL_buffinit(L, &b); for (; i < last; i++) { addfield(L, &b, &ta, i); luaL_addlstring(&b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ addfield(L, &b, &ta, i); luaL_pushresult(&b); return 1; }
static int trotate (lua_State *L) { lua_Integer n, begin, end; checktab(L, 1, TAB_RW); n = -luaL_checkinteger(L, 2); begin = luaL_optinteger(L, 3, 1); end = luaL_opt(L, luaL_checkinteger, 4, check_n(L, 1)); if (end > begin) { n %= end - begin + 1; if (n < 0) n += end - begin + 1; if (n != 0) { lua_settop(L, 1); reverse(L, begin, begin+n-1); reverse(L, begin+n, end); reverse(L, begin, end); } } return 0; }
static void collectstrings (lua_State *L, int all) { int i; for (i=0; i<L->strt.size; i++) { /* for each list */ TString **p = &L->strt.hash[i]; TString *next; while ((next = *p) != NULL) { if (next->marked && !all) { /* preserve? */ if (next->marked < FIXMARK) /* does not change FIXMARKs */ next->marked = 0; p = &next->nexthash; } else { /* collect */ *p = next->nexthash; L->strt.nuse--; L->nblocks -= sizestring(next->len); luaM_free(L, next); } } } checktab(L, &L->strt); }
static void collectudata (lua_State *L, int all) { int i; for (i=0; i<L->udt.size; i++) { /* for each list */ TString **p = &L->udt.hash[i]; TString *next; while ((next = *p) != NULL) { LUA_ASSERT(next->marked <= 1, "udata cannot be fixed"); if (next->marked && !all) { /* preserve? */ next->marked = 0; p = &next->nexthash; } else { /* collect */ int tag = next->u.d.tag; *p = next->nexthash; next->nexthash = L->TMtable[tag].collected; /* chain udata */ L->TMtable[tag].collected = next; L->nblocks -= sizestring(next->len); L->udt.nuse--; } } } checktab(L, &L->udt); }
static int treplace (lua_State *L) { lua_Integer len, tpos, start, end, start2, end2, i; len = aux_getn(L, 1, TAB_RW); if (lua_type(L, 2) == LUA_TNUMBER) { start = luaL_checkinteger(L, 2); luaL_argcheck(L, start >= 1 && start <= len+1, 2, "index out of bounds"); if (lua_type(L, 3) == LUA_TNUMBER) { end = luaL_checkinteger(L, 3); luaL_argcheck(L, end >= start-1 && end <= len, 3, "invalid end index"); tpos = 4; } else { end = start; if (end > len) end = len; tpos = 3; } } else { start = len+1; end = len; tpos = 2; } checktab(L, tpos, TAB_R); start2 = luaL_optinteger(L, tpos+1, 1); end2 = luaL_opt(L, luaL_checkinteger, tpos+2, check_n(L, tpos)); luaL_argcheck(L, end2 >= start2-1, tpos+2, "invalid end index"); if (end2-start2 > end-start) { /* array needs to grow */ lua_pushinteger(L, len+end2-start2-end+start); lua_setfield(L, 1, "n"); /* t.n = number of elements */ } if (start <= len) { /* replace values */ lua_Integer shift = end2-start2-end+start; if (shift < 0) { /* shift to left */ for (i = end+1; i <= len; ++i) { lua_geti(L, 1, i); lua_seti(L, 1, i+shift); } for (i = len; i > len+shift; --i) { lua_pushnil(L); lua_seti(L, 1, i); } } else if (shift != 0) { /* shift to right */ for (i = len-shift+1; i <= len; ++i) { lua_geti(L, 1, i); lua_seti(L, 1, i+shift); } for (i = len-shift; i > end; --i) { lua_geti(L, 1, i); lua_seti(L, 1, i+shift); } } } /* copy from list2 to list1 */ for (i = start2; i <= end2; ++i) { lua_geti(L, tpos, i); lua_seti(L, 1, start+i-start2); } /* array must shrink */ if (end2-start2 < end-start) { lua_pushinteger(L, len+end2-start2-end+start); lua_setfield(L, 1, "n"); /* t.n = number of elements */ } return 0; }
static void set_proto_fr(void) { unsigned int lmi_type = 0; fr_proto fr; memset(&fr, 0, sizeof(fr)); while (argc > 0) { if (!lmi_type) if (!checkkey("lmi")) { if (!checktab(lmi, &lmi_type)) continue; error("Invalid LMI type: %s\n", argv[0]); } if (lmi_type && lmi_type != LMI_NONE) { if (!fr.dce) if (!checkkey("dce")) { fr.dce = 1; continue; } if (!fr.t391) if (!match("t391", &fr.t391, 1, 1000)) continue; if (!fr.t392) if (!match("t392", &fr.t392, 1, 1000)) continue; if (!fr.n391) if (!match("n391", &fr.n391, 1, 1000)) continue; if (!fr.n392) if (!match("n392", &fr.n392, 1, 1000)) continue; if (!fr.n393) if (!match("n393", &fr.n393, 1, 1000)) continue; } error("Invalid parameter: %s\n", argv[0]); } /* polling verification timer*/ if (!fr.t391) fr.t391 = 10; /* link integrity verification polling timer */ if (!fr.t392) fr.t392 = 15; /* full status polling counter*/ if (!fr.n391) fr.n391 = 6; /* error threshold */ if (!fr.n392) fr.n392 = 3; /* monitored events count */ if (!fr.n393) fr.n393 = 4; if (!lmi_type) fr.lmi = LMI_DEFAULT; else fr.lmi = lmi_type; req.ifr_settings.ifs_ifsu.fr = &fr; req.ifr_settings.size = sizeof(fr); if (ioctl(sock, SIOCWANDEV, &req)) error("Unable to set FR protocol information: %s\n", strerror(errno)); }