2009-07-31
be more style(9) compliant
| src/lua_sysctl.c | file | annotate | diff | revisions |
1.1 --- a/src/lua_sysctl.c Sat Jul 11 19:59:30 2009 +0200 1.2 +++ b/src/lua_sysctl.c Fri Jul 31 01:46:16 2009 +0200 1.3 @@ -64,440 +64,442 @@ 1.4 #include "lualib.h" 1.5 1.6 1.7 -static int name2oid(char *name, int *oidp); 1.8 -static int oidfmt(int *oid, int len, char *fmt, size_t fmtsiz, u_int *kind); 1.9 -static int set_IK(char *str, int *val); 1.10 +static int name2oid(char *name, int *oidp); 1.11 +static int oidfmt(int *oid, int len, char *fmt, size_t fmtsiz, u_int *kind); 1.12 +static int set_IK(char *str, int *val); 1.13 1.14 1.15 static int 1.16 S_clockinfo(lua_State *L, int l2, void *p) 1.17 { 1.18 - struct clockinfo *ci = (struct clockinfo *)p; 1.19 + struct clockinfo *ci = (struct clockinfo *)p; 1.20 1.21 - if (l2 != sizeof(*ci)) 1.22 - return (luaL_error(L, "S_clockinfo %d != %d", l2, sizeof(*ci))); 1.23 + if (l2 != sizeof(*ci)) 1.24 + return (luaL_error(L, "S_clockinfo %d != %d", l2, sizeof(*ci))); 1.25 1.26 - lua_newtable(L); 1.27 + lua_newtable(L); 1.28 1.29 - lua_pushinteger(L, ci->hz); 1.30 - lua_setfield(L, -2, "hz"); 1.31 - lua_pushinteger(L, ci->tick); 1.32 - lua_setfield(L, -2, "tick"); 1.33 - lua_pushinteger(L, ci->profhz); 1.34 - lua_setfield(L, -2, "profhz"); 1.35 - lua_pushinteger(L, ci->stathz); 1.36 - lua_setfield(L, -2, "stathz"); 1.37 + lua_pushinteger(L, ci->hz); 1.38 + lua_setfield(L, -2, "hz"); 1.39 + lua_pushinteger(L, ci->tick); 1.40 + lua_setfield(L, -2, "tick"); 1.41 + lua_pushinteger(L, ci->profhz); 1.42 + lua_setfield(L, -2, "profhz"); 1.43 + lua_pushinteger(L, ci->stathz); 1.44 + lua_setfield(L, -2, "stathz"); 1.45 1.46 - return (1); 1.47 + return (1); 1.48 } 1.49 1.50 1.51 static int 1.52 S_loadavg(lua_State *L, int l2, void *p) 1.53 { 1.54 - struct loadavg *la = (struct loadavg *)p; 1.55 - int i; 1.56 + struct loadavg *la = (struct loadavg *)p; 1.57 + int i; 1.58 1.59 - if (l2 != sizeof(*la)) 1.60 - return (luaL_error(L, "S_loadavg %d != %d", l2, sizeof(*la))); 1.61 + if (l2 != sizeof(*la)) 1.62 + return (luaL_error(L, "S_loadavg %d != %d", l2, sizeof(*la))); 1.63 1.64 - lua_newtable(L); 1.65 + lua_newtable(L); 1.66 1.67 - for (i = 0; i < 3; i++) { 1.68 - lua_pushinteger(L, i + 1); 1.69 - lua_pushnumber(L, (double)la->ldavg[i]/(double)la->fscale); 1.70 - lua_settable(L, -3); 1.71 - } 1.72 + for (i = 0; i < 3; i++) { 1.73 + lua_pushinteger(L, i + 1); 1.74 + lua_pushnumber(L, (double)la->ldavg[i] / (double)la->fscale); 1.75 + lua_settable(L, -3); 1.76 + } 1.77 1.78 - return (1); 1.79 + return (1); 1.80 } 1.81 1.82 1.83 static int 1.84 S_timeval(lua_State *L, int l2, void *p) 1.85 { 1.86 - struct timeval *tv = (struct timeval *)p; 1.87 + struct timeval *tv = (struct timeval *)p; 1.88 1.89 - if (l2 != sizeof(*tv)) 1.90 - return (luaL_error(L, "S_timeval %d != %d", l2, sizeof(*tv))); 1.91 + if (l2 != sizeof(*tv)) 1.92 + return (luaL_error(L, "S_timeval %d != %d", l2, sizeof(*tv))); 1.93 1.94 - lua_newtable(L); 1.95 + lua_newtable(L); 1.96 1.97 - lua_pushinteger(L, tv->tv_sec); 1.98 - lua_setfield(L, -2, "sec"); 1.99 - lua_pushinteger(L, tv->tv_usec); 1.100 - lua_setfield(L, -2, "usec"); 1.101 + lua_pushinteger(L, tv->tv_sec); 1.102 + lua_setfield(L, -2, "sec"); 1.103 + lua_pushinteger(L, tv->tv_usec); 1.104 + lua_setfield(L, -2, "usec"); 1.105 1.106 - return (1); 1.107 + return (1); 1.108 } 1.109 1.110 1.111 static int 1.112 S_vmtotal(lua_State *L, int l2, void *p) 1.113 { 1.114 - struct vmtotal *v = (struct vmtotal *)p; 1.115 - int pageKilo = getpagesize() / 1024; 1.116 + struct vmtotal *v = (struct vmtotal *)p; 1.117 + int pageKilo = getpagesize() / 1024; 1.118 1.119 - if (l2 != sizeof(*v)) 1.120 - return (luaL_error(L, "S_vmtotal %d != %d", l2, sizeof(*v))); 1.121 + if (l2 != sizeof(*v)) 1.122 + return (luaL_error(L, "S_vmtotal %d != %d", l2, sizeof(*v))); 1.123 1.124 - lua_newtable(L); 1.125 + lua_newtable(L); 1.126 1.127 - lua_pushinteger(L, v->t_rq); 1.128 - lua_setfield(L, -2, "rq"); 1.129 - lua_pushinteger(L, v->t_dw); 1.130 - lua_setfield(L, -2, "dw"); 1.131 - lua_pushinteger(L, v->t_pw); 1.132 - lua_setfield(L, -2, "pw"); 1.133 - lua_pushinteger(L, v->t_sl); 1.134 - lua_setfield(L, -2, "sl"); 1.135 + lua_pushinteger(L, v->t_rq); 1.136 + lua_setfield(L, -2, "rq"); 1.137 + lua_pushinteger(L, v->t_dw); 1.138 + lua_setfield(L, -2, "dw"); 1.139 + lua_pushinteger(L, v->t_pw); 1.140 + lua_setfield(L, -2, "pw"); 1.141 + lua_pushinteger(L, v->t_sl); 1.142 + lua_setfield(L, -2, "sl"); 1.143 1.144 - lua_pushinteger(L, v->t_vm * pageKilo); 1.145 - lua_setfield(L, -2, "vm"); 1.146 - lua_pushinteger(L, v->t_avm * pageKilo); 1.147 - lua_setfield(L, -2, "avm"); 1.148 + lua_pushinteger(L, v->t_vm * pageKilo); 1.149 + lua_setfield(L, -2, "vm"); 1.150 + lua_pushinteger(L, v->t_avm * pageKilo); 1.151 + lua_setfield(L, -2, "avm"); 1.152 1.153 - lua_pushinteger(L, v->t_rm * pageKilo); 1.154 - lua_setfield(L, -2, "rm"); 1.155 - lua_pushinteger(L, v->t_arm * pageKilo); 1.156 - lua_setfield(L, -2, "arm"); 1.157 + lua_pushinteger(L, v->t_rm * pageKilo); 1.158 + lua_setfield(L, -2, "rm"); 1.159 + lua_pushinteger(L, v->t_arm * pageKilo); 1.160 + lua_setfield(L, -2, "arm"); 1.161 1.162 - lua_pushinteger(L, v->t_vmshr * pageKilo); 1.163 - lua_setfield(L, -2, "vmshr"); 1.164 - lua_pushinteger(L, v->t_avmshr * pageKilo); 1.165 - lua_setfield(L, -2, "avmshr"); 1.166 + lua_pushinteger(L, v->t_vmshr * pageKilo); 1.167 + lua_setfield(L, -2, "vmshr"); 1.168 + lua_pushinteger(L, v->t_avmshr * pageKilo); 1.169 + lua_setfield(L, -2, "avmshr"); 1.170 1.171 - lua_pushinteger(L, v->t_rmshr * pageKilo); 1.172 - lua_setfield(L, -2, "rmshr"); 1.173 - lua_pushinteger(L, v->t_armshr * pageKilo); 1.174 - lua_setfield(L, -2, "armshr"); 1.175 + lua_pushinteger(L, v->t_rmshr * pageKilo); 1.176 + lua_setfield(L, -2, "rmshr"); 1.177 + lua_pushinteger(L, v->t_armshr * pageKilo); 1.178 + lua_setfield(L, -2, "armshr"); 1.179 1.180 - lua_pushinteger(L, v->t_free * pageKilo); 1.181 - lua_setfield(L, -2, "free"); 1.182 + lua_pushinteger(L, v->t_free * pageKilo); 1.183 + lua_setfield(L, -2, "free"); 1.184 1.185 - return (1); 1.186 + return (1); 1.187 } 1.188 1.189 1.190 static int 1.191 T_dev_t(lua_State *L, int l2, void *p) 1.192 { 1.193 - dev_t *d = (dev_t *)p; 1.194 + dev_t *d = (dev_t *)p; 1.195 1.196 - if (l2 != sizeof(*d)) 1.197 - return (luaL_error(L, "T_dev_t %d != %d", l2, sizeof(*d))); 1.198 + if (l2 != sizeof(*d)) 1.199 + return (luaL_error(L, "T_dev_t %d != %d", l2, sizeof(*d))); 1.200 1.201 - lua_newtable(L); 1.202 + lua_newtable(L); 1.203 1.204 - if ((int)(*d) != -1) { 1.205 - lua_pushinteger(L, minor(*d)); 1.206 - lua_setfield(L, -2, "minor"); 1.207 - lua_pushinteger(L, major(*d)); 1.208 - lua_setfield(L, -2, "major"); 1.209 - } 1.210 + if ((int)(*d) != -1) { 1.211 + lua_pushinteger(L, minor(*d)); 1.212 + lua_setfield(L, -2, "minor"); 1.213 + lua_pushinteger(L, major(*d)); 1.214 + lua_setfield(L, -2, "major"); 1.215 + } 1.216 1.217 - return (1); 1.218 + return (1); 1.219 } 1.220 1.221 1.222 static int 1.223 set_T_dev_t(lua_State *L, char *path, void **val, size_t *size) 1.224 { 1.225 - struct stat statb; 1.226 - int rc; 1.227 + struct stat statb; 1.228 + int rc; 1.229 1.230 - if (strcmp(path, "none") != 0 && strcmp(path, "off") != 0) { 1.231 - rc = stat(path, &statb); 1.232 - if (rc) 1.233 - return (luaL_error(L, "cannot stat %s", path)); 1.234 + if (strcmp(path, "none") != 0 && strcmp(path, "off") != 0) { 1.235 + rc = stat(path, &statb); 1.236 + if (rc) 1.237 + return (luaL_error(L, "cannot stat %s", path)); 1.238 1.239 - if (!S_ISCHR(statb.st_mode)) 1.240 - return (luaL_error(L, "must specify a device special file.")); 1.241 - } 1.242 - else 1.243 - statb.st_rdev = NODEV; 1.244 + if (!S_ISCHR(statb.st_mode)) 1.245 + return (luaL_error(L, "must specify a device special file.")); 1.246 + } else 1.247 + statb.st_rdev = NODEV; 1.248 1.249 - *val = &statb.st_rdev; 1.250 - *size = sizeof(statb.st_rdev); 1.251 - 1.252 - return (1); 1.253 + *val = &statb.st_rdev; 1.254 + *size = sizeof(statb.st_rdev); 1.255 + return (1); 1.256 } 1.257 1.258 1.259 static int 1.260 luaA_sysctl_set(lua_State *L) 1.261 { 1.262 - int i, len, intval, mib[CTL_MAXNAME]; 1.263 - unsigned int uintval; 1.264 - long longval; 1.265 - unsigned long ulongval; 1.266 - quad_t quadval; 1.267 - size_t s, newsize = 0; 1.268 - u_int kind; 1.269 - char fmt[BUFSIZ], key[BUFSIZ], nvalbuf[BUFSIZ]; 1.270 - const char *errmsg; 1.271 - void *newval = NULL; 1.272 + int i; 1.273 + int len; 1.274 + int intval; 1.275 + int mib[CTL_MAXNAME]; 1.276 + unsigned int uintval; 1.277 + u_int kind; 1.278 + long longval; 1.279 + unsigned long ulongval; 1.280 + size_t s; 1.281 + size_t newsize = 0; 1.282 + quad_t quadval; 1.283 + char fmt[BUFSIZ]; 1.284 + char key[BUFSIZ]; 1.285 + char nvalbuf[BUFSIZ]; 1.286 + const char *errmsg; 1.287 + void *newval = NULL; 1.288 1.289 - /* get first argument from lua */ 1.290 - s = strlcpy(key, luaL_checkstring(L, 1), sizeof(key)); 1.291 - if (s >= sizeof(key)) 1.292 - return (luaL_error(L, "first arg too long")); 1.293 - /* get second argument from lua */ 1.294 - s = strlcpy(nvalbuf, luaL_checkstring(L, 2), sizeof(nvalbuf)); 1.295 - if (s >= sizeof(nvalbuf)) 1.296 - return (luaL_error(L, "second arg too long")); 1.297 - newval = nvalbuf; 1.298 - newsize = s; 1.299 + /* get first argument from lua */ 1.300 + s = strlcpy(key, luaL_checkstring(L, 1), sizeof(key)); 1.301 + if (s >= sizeof(key)) 1.302 + return (luaL_error(L, "first arg too long")); 1.303 + /* get second argument from lua */ 1.304 + s = strlcpy(nvalbuf, luaL_checkstring(L, 2), sizeof(nvalbuf)); 1.305 + if (s >= sizeof(nvalbuf)) 1.306 + return (luaL_error(L, "second arg too long")); 1.307 + newval = nvalbuf; 1.308 + newsize = s; 1.309 1.310 - len = name2oid(key, mib); 1.311 - if (len < 0) 1.312 - return (luaL_error(L, "unknown iod '%s'", key)); 1.313 + len = name2oid(key, mib); 1.314 + if (len < 0) 1.315 + return (luaL_error(L, "unknown iod '%s'", key)); 1.316 + if (oidfmt(mib, len, fmt, sizeof(fmt), &kind) != 0) 1.317 + return (luaL_error(L, "couldn't find format of oid '%s'", key)); 1.318 + if ((kind & CTLTYPE) == CTLTYPE_NODE) 1.319 + return (luaL_error(L, "oid '%s' isn't a leaf node", key)); 1.320 + if (!(kind & CTLFLAG_WR)) { 1.321 + return (luaL_error(L, "oid '%s' is %s", key, 1.322 + (kind & CTLFLAG_TUN) ? "read only tunable" : "read only")); 1.323 + } 1.324 + if ((kind & CTLTYPE) == CTLTYPE_INT || 1.325 + (kind & CTLTYPE) == CTLTYPE_UINT || 1.326 + (kind & CTLTYPE) == CTLTYPE_LONG || 1.327 + (kind & CTLTYPE) == CTLTYPE_ULONG || 1.328 + (kind & CTLTYPE) == CTLTYPE_QUAD) { 1.329 + if (strlen(newval) == 0) 1.330 + return (luaL_error(L, "empty numeric value")); 1.331 + } 1.332 1.333 - if (oidfmt(mib, len, fmt, sizeof(fmt), &kind) != 0) 1.334 - return (luaL_error(L, "couldn't find format of oid '%s'", key)); 1.335 + switch (kind & CTLTYPE) { 1.336 + case CTLTYPE_INT: 1.337 + if (strcmp(fmt, "IK") == 0) { 1.338 + if (!set_IK(newval, &intval)) 1.339 + return (luaL_error(L, "invalid value '%s'", (char *)newval)); 1.340 + } else { 1.341 + intval = (int)strtonum(newval, INT_MIN, INT_MAX, &errmsg); 1.342 + if (errmsg) { 1.343 + return (luaL_error(L, "bad integer: %s (%s)", 1.344 + (char *)newval, errmsg)); 1.345 + } 1.346 + } 1.347 + newval = &intval; 1.348 + newsize = sizeof(intval); 1.349 + break; 1.350 + case CTLTYPE_UINT: 1.351 + uintval = (unsigned int)strtonum(newval, 0, UINT_MAX, &errmsg); 1.352 + if (errmsg) { 1.353 + return (luaL_error(L, "bad unsigned integer: %s (%s)", 1.354 + (char *)newval, errmsg)); 1.355 + } 1.356 + newval = &uintval; 1.357 + newsize = sizeof(uintval); 1.358 + break; 1.359 + case CTLTYPE_LONG: 1.360 + longval = (long)strtonum(newval, LONG_MIN, LONG_MAX, &errmsg); 1.361 + if (errmsg) { 1.362 + return (luaL_error(L,"bad long integer: %s (%s)", 1.363 + (char *)newval, errmsg)); 1.364 + } 1.365 + newval = &longval; 1.366 + newsize = sizeof(longval); 1.367 + break; 1.368 + case CTLTYPE_ULONG: 1.369 + ulongval = (unsigned long)strtonum(newval, 0, ULONG_MAX, &errmsg); 1.370 + if (errmsg) { 1.371 + return (luaL_error(L, "bad unsigned long integer: %s (%s)", 1.372 + (char *)newval, errmsg)); 1.373 + } 1.374 + newval = &ulongval; 1.375 + newsize = sizeof(ulongval); 1.376 + break; 1.377 + case CTLTYPE_STRING: 1.378 + break; 1.379 + case CTLTYPE_QUAD: 1.380 + quadval = (quad_t)strtonum(newval, LLONG_MIN, LLONG_MAX, &errmsg); 1.381 + if (errmsg) { 1.382 + return (luaL_error(L, "bad quad_t integer: %s (%s)", 1.383 + (char *)newval, errmsg)); 1.384 + } 1.385 + newval = &quadval; 1.386 + newsize = sizeof(quadval); 1.387 + break; 1.388 + case CTLTYPE_OPAQUE: 1.389 + if (strcmp(fmt, "T,dev_t") == 0) { 1.390 + set_T_dev_t(L, newval, &newval, &newsize); 1.391 + break; 1.392 + } 1.393 + /* FALLTHROUGH */ 1.394 + default: 1.395 + return (luaL_error(L, "oid '%s' is type %d, cannot set that", 1.396 + key, kind & CTLTYPE)); 1.397 + } 1.398 1.399 - if ((kind & CTLTYPE) == CTLTYPE_NODE) 1.400 - return (luaL_error(L, "oid '%s' isn't a leaf node", key)); 1.401 - 1.402 - if (!(kind & CTLFLAG_WR)) { 1.403 - return (luaL_error(L, "oid '%s' is %s", key, 1.404 - (kind & CTLFLAG_TUN) ? "read only tunable" : "read only")); 1.405 - } 1.406 - 1.407 - if ((kind & CTLTYPE) == CTLTYPE_INT || 1.408 - (kind & CTLTYPE) == CTLTYPE_UINT || 1.409 - (kind & CTLTYPE) == CTLTYPE_LONG || 1.410 - (kind & CTLTYPE) == CTLTYPE_ULONG || 1.411 - (kind & CTLTYPE) == CTLTYPE_QUAD) { 1.412 - if (strlen(newval) == 0) 1.413 - return (luaL_error(L, "empty numeric value")); 1.414 - } 1.415 - 1.416 - switch (kind & CTLTYPE) { 1.417 - case CTLTYPE_INT: 1.418 - if (strcmp(fmt, "IK") == 0) { 1.419 - if (!set_IK(newval, &intval)) 1.420 - return (luaL_error(L, "invalid value '%s'", (char *)newval)); 1.421 - } 1.422 - else { 1.423 - intval = (int)strtonum(newval, INT_MIN, INT_MAX, &errmsg); 1.424 - if (errmsg) { 1.425 - return (luaL_error(L, "bad integer: %s (%s)", 1.426 - (char *)newval, errmsg)); 1.427 - } 1.428 - } 1.429 - newval = &intval; 1.430 - newsize = sizeof(intval); 1.431 - break; 1.432 - case CTLTYPE_UINT: 1.433 - uintval = (unsigned int)strtonum(newval, 0, UINT_MAX, &errmsg); 1.434 - if (errmsg) { 1.435 - return (luaL_error(L, "bad unsigned integer: %s (%s)", 1.436 - (char *)newval, errmsg)); 1.437 - } 1.438 - newval = &uintval; 1.439 - newsize = sizeof(uintval); 1.440 - break; 1.441 - case CTLTYPE_LONG: 1.442 - longval = (long)strtonum(newval, LONG_MIN, LONG_MAX, &errmsg); 1.443 - if (errmsg) { 1.444 - return (luaL_error(L,"bad long integer: %s (%s)", 1.445 - (char *)newval, errmsg)); 1.446 - } 1.447 - newval = &longval; 1.448 - newsize = sizeof(longval); 1.449 - break; 1.450 - case CTLTYPE_ULONG: 1.451 - ulongval = (unsigned long)strtonum(newval, 0, ULONG_MAX, &errmsg); 1.452 - if (errmsg) { 1.453 - return (luaL_error(L, "bad unsigned long integer: %s (%s)", 1.454 - (char *)newval, errmsg)); 1.455 - } 1.456 - newval = &ulongval; 1.457 - newsize = sizeof(ulongval); 1.458 - break; 1.459 - case CTLTYPE_STRING: 1.460 - break; 1.461 - case CTLTYPE_QUAD: 1.462 - quadval = (quad_t)strtonum(newval, LLONG_MIN, LLONG_MAX, &errmsg); 1.463 - if (errmsg) { 1.464 - return (luaL_error(L, "bad quad_t integer: %s (%s)", 1.465 - (char *)newval, errmsg)); 1.466 - } 1.467 - newval = &quadval; 1.468 - newsize = sizeof(quadval); 1.469 - break; 1.470 - case CTLTYPE_OPAQUE: 1.471 - if (strcmp(fmt, "T,dev_t") == 0) { 1.472 - set_T_dev_t(L, newval, &newval, &newsize); 1.473 - break; 1.474 - } 1.475 - /* FALLTHROUGH */ 1.476 - default: 1.477 - return (luaL_error(L, "oid '%s' is type %d, cannot set that", 1.478 - key, kind & CTLTYPE)); 1.479 - } 1.480 - 1.481 - if (sysctl(mib, len, NULL, NULL, newval, newsize) == -1) { 1.482 - switch (errno) { 1.483 - case EOPNOTSUPP: 1.484 - return (luaL_error(L, "%s: value is not available", key)); 1.485 - case ENOTDIR: 1.486 - return (luaL_error(L, "%s: specification is incomplete", key)); 1.487 - case ENOMEM: 1.488 - /* really? with ENOMEM !?! */ 1.489 - return (luaL_error(L, "%s: type is unknown to this program", key)); 1.490 - default: 1.491 - i = strerror_r(errno, nvalbuf, sizeof(nvalbuf)); 1.492 - if (i != 0) 1.493 - return (luaL_error(L, "strerror_r(3) failed")); 1.494 - else 1.495 - return (luaL_error(L, "%s: %s", key, nvalbuf)); 1.496 - } 1.497 - } 1.498 - 1.499 - return (0); 1.500 + if (sysctl(mib, len, NULL, NULL, newval, newsize) == -1) { 1.501 + switch (errno) { 1.502 + case EOPNOTSUPP: 1.503 + return (luaL_error(L, "%s: value is not available", key)); 1.504 + case ENOTDIR: 1.505 + return (luaL_error(L, "%s: specification is incomplete", key)); 1.506 + case ENOMEM: 1.507 + /* really? with ENOMEM !?! */ 1.508 + return (luaL_error(L, "%s: type is unknown to this program", key)); 1.509 + default: 1.510 + i = strerror_r(errno, nvalbuf, sizeof(nvalbuf)); 1.511 + if (i != 0) 1.512 + return (luaL_error(L, "strerror_r(3) failed")); 1.513 + else 1.514 + return (luaL_error(L, "%s: %s", key, nvalbuf)); 1.515 + } 1.516 + /* NOTREACHED */ 1.517 + } 1.518 + return (0); 1.519 } 1.520 1.521 1.522 static int 1.523 luaA_sysctl_get(lua_State *L) 1.524 { 1.525 - int nlen, i, oid[CTL_MAXNAME]; 1.526 - size_t len, intlen; 1.527 - char fmt[BUFSIZ], buf[BUFSIZ], *val, *oval, *p; 1.528 - u_int kind; 1.529 - int (*func)(lua_State *, int, void *); 1.530 - uintmax_t umv; 1.531 - intmax_t mv; 1.532 + int nlen; 1.533 + int i; 1.534 + int oid[CTL_MAXNAME]; 1.535 + u_int kind; 1.536 + size_t len; 1.537 + size_t intlen; 1.538 + uintmax_t umv; 1.539 + intmax_t mv; 1.540 + char fmt[BUFSIZ]; 1.541 + char buf[BUFSIZ]; 1.542 + char *val, *oval, *p; 1.543 + int (*func)(lua_State *, int, void *); 1.544 1.545 - bzero(fmt, BUFSIZ); 1.546 - bzero(buf, BUFSIZ); 1.547 + bzero(fmt, BUFSIZ); 1.548 + bzero(buf, BUFSIZ); 1.549 1.550 - len = strlcpy(buf, luaL_checkstring(L, 1), sizeof(buf)); /* get first argument from lua */ 1.551 - if (len >= sizeof(buf)) 1.552 - return (luaL_error(L, "first arg too long")); 1.553 + /* get first argument from lua */ 1.554 + len = strlcpy(buf, luaL_checkstring(L, 1), sizeof(buf)); 1.555 + if (len >= sizeof(buf)) 1.556 + return (luaL_error(L, "first arg too long")); 1.557 1.558 - nlen = name2oid(buf, oid); 1.559 - if (nlen < 0) 1.560 - return (luaL_error(L, "unknown iod `%s'", buf)); 1.561 + nlen = name2oid(buf, oid); 1.562 + if (nlen < 0) 1.563 + return (luaL_error(L, "unknown iod `%s'", buf)); 1.564 + if (oidfmt(oid, nlen, fmt, sizeof(fmt), &kind) != 0) 1.565 + return (luaL_error(L, "couldn't find format of oid `%s'", buf)); 1.566 + if ((kind & CTLTYPE) == CTLTYPE_NODE) 1.567 + return (luaL_error(L, "can't handle CTLTYPE_NODE")); 1.568 1.569 - if (oidfmt(oid, nlen, fmt, sizeof(fmt), &kind) != 0) 1.570 - return (luaL_error(L, "couldn't find format of oid `%s'", buf)); 1.571 + /* find an estimate of how much we need for this var */ 1.572 + len = 0; 1.573 + (void)sysctl(oid, nlen, NULL, &len, NULL, 0); 1.574 + len += len; /* we want to be sure :-) */ 1.575 + val = oval = malloc(len + 1); 1.576 + if (val == NULL) 1.577 + return (luaL_error(L, "malloc(3) failed")); 1.578 1.579 - if ((kind & CTLTYPE) == CTLTYPE_NODE) 1.580 - return (luaL_error(L, "can't handle CTLTYPE_NODE")); 1.581 + i = sysctl(oid, nlen, val, &len, NULL, 0); 1.582 + if (i || !len) { 1.583 + free(oval); 1.584 + return (luaL_error(L, "sysctl(3) failed")); 1.585 + } 1.586 + val[len] = '\0'; 1.587 1.588 - /* find an estimate of how much we need for this var */ 1.589 - len = 0; 1.590 - (void)sysctl(oid, nlen, NULL, &len, NULL, 0); 1.591 - len += len; /* we want to be sure :-) */ 1.592 + p = val; 1.593 + switch (*fmt) { 1.594 + case 'A': 1.595 + lua_pushstring(L, p); 1.596 + break; 1.597 + case 'I': 1.598 + case 'L': 1.599 + case 'Q': 1.600 + switch (*fmt) { 1.601 + case 'I': intlen = sizeof(int); break; 1.602 + case 'L': intlen = sizeof(long); break; 1.603 + case 'Q': intlen = sizeof(quad_t); break; 1.604 + default: 1.605 + return (luaL_error(L, "lua_sysctl internal error (bug)")); 1.606 + /* NOTREACHED */ 1.607 + } 1.608 + i = 0; 1.609 + lua_newtable(L); 1.610 + while (len >= intlen) { 1.611 + i++; 1.612 + switch (*fmt) { 1.613 + case 'I': 1.614 + umv = *(u_int *)p; 1.615 + mv = *(int *)p; 1.616 + break; 1.617 + case 'L': 1.618 + umv = *(u_long *)p; 1.619 + mv = *(long *)p; 1.620 + break; 1.621 + case 'Q': 1.622 + umv = *(u_quad_t *)p; 1.623 + mv = *(quad_t *)p; 1.624 + break; 1.625 + default: 1.626 + return (luaL_error(L, "lua_sysctl internal error (bug)")); 1.627 + /* NOTREACHED */ 1.628 + } 1.629 + lua_pushinteger(L, i); 1.630 + switch (*fmt) { 1.631 + case 'I': 1.632 + case 'L': 1.633 + if (fmt[1] == 'U') 1.634 + lua_pushinteger(L, umv); 1.635 + else 1.636 + lua_pushinteger(L, mv); 1.637 + break; 1.638 + case 'Q': 1.639 + if (fmt[1] == 'U') 1.640 + lua_pushnumber(L, umv); 1.641 + else 1.642 + lua_pushnumber(L, mv); 1.643 + break; 1.644 + default: 1.645 + return (luaL_error(L, "lua_sysctl internal error (bug)")); 1.646 + /* NOTREACHED */ 1.647 + } 1.648 + lua_settable(L, -3); 1.649 + len -= intlen; 1.650 + p += intlen; 1.651 + } 1.652 + if (i == 1) { 1.653 + lua_pushinteger(L, i); 1.654 + lua_gettable(L, -2); 1.655 + lua_remove(L, lua_gettop(L) - 1); /* remove table */ 1.656 + } 1.657 + break; 1.658 + case 'T': 1.659 + case 'S': 1.660 + if (strcmp(fmt, "S,clockinfo") == 0) 1.661 + func = S_clockinfo; 1.662 + else if (strcmp(fmt, "S,loadavg") == 0) 1.663 + func = S_loadavg; 1.664 + else if (strcmp(fmt, "S,timeval") == 0) 1.665 + func = S_timeval; 1.666 + else if (strcmp(fmt, "S,vmtotal") == 0) 1.667 + func = S_vmtotal; 1.668 + else if (strcmp(fmt, "T,dev_t") == 0) 1.669 + func = T_dev_t; 1.670 + else 1.671 + func = NULL; 1.672 1.673 - val = oval = malloc(len + 1); 1.674 - if (val == NULL) 1.675 - return (luaL_error(L, "malloc(3) failed")); 1.676 - 1.677 - i = sysctl(oid, nlen, val, &len, NULL, 0); 1.678 - if (i || !len) { 1.679 - free(oval); 1.680 - return (luaL_error(L, "sysctl(3) failed")); 1.681 - } 1.682 - val[len] = '\0'; 1.683 - 1.684 - p = val; 1.685 - switch (*fmt) { 1.686 - case 'A': 1.687 - lua_pushstring(L, p); 1.688 - break; 1.689 - case 'I': 1.690 - case 'L': 1.691 - case 'Q': 1.692 - switch (*fmt) { 1.693 - case 'I': intlen = sizeof(int); break; 1.694 - case 'L': intlen = sizeof(long); break; 1.695 - case 'Q': intlen = sizeof(quad_t); break; 1.696 - default: 1.697 - return (luaL_error(L, "lua_sysctl internal error (bug)")); 1.698 - } 1.699 - i = 0; 1.700 - lua_newtable(L); 1.701 - while (len >= intlen) { 1.702 - i++; 1.703 - switch (*fmt) { 1.704 - case 'I': 1.705 - umv = *(u_int *)p; 1.706 - mv = *(int *)p; 1.707 - break; 1.708 - case 'L': 1.709 - umv = *(u_long *)p; 1.710 - mv = *(long *)p; 1.711 - break; 1.712 - case 'Q': 1.713 - umv = *(u_quad_t *)p; 1.714 - mv = *(quad_t *)p; 1.715 - break; 1.716 - default: 1.717 - return (luaL_error(L, "lua_sysctl internal error (bug)")); 1.718 - } 1.719 - 1.720 - lua_pushinteger(L, i); 1.721 - switch (*fmt) { 1.722 - case 'I': 1.723 - case 'L': 1.724 - if (fmt[1] == 'U') 1.725 - lua_pushinteger(L, umv); 1.726 - else 1.727 - lua_pushinteger(L, mv); 1.728 - break; 1.729 - case 'Q': 1.730 - if (fmt[1] == 'U') 1.731 - lua_pushnumber(L, umv); 1.732 - else 1.733 - lua_pushnumber(L, mv); 1.734 - break; 1.735 - default: 1.736 - return (luaL_error(L, "lua_sysctl internal error (bug)")); 1.737 - } 1.738 - lua_settable(L, -3); 1.739 - 1.740 - len -= intlen; 1.741 - p += intlen; 1.742 - } 1.743 - if (i == 1) { 1.744 - lua_pushinteger(L, i); 1.745 - lua_gettable(L, -2); 1.746 - lua_remove(L, lua_gettop(L) - 1); /* remove table */ 1.747 - } 1.748 - break; 1.749 - case 'T': 1.750 - case 'S': 1.751 - if (strcmp(fmt, "S,clockinfo") == 0) 1.752 - func = S_clockinfo; 1.753 - else if (strcmp(fmt, "S,loadavg") == 0) 1.754 - func = S_loadavg; 1.755 - else if (strcmp(fmt, "S,timeval") == 0) 1.756 - func = S_timeval; 1.757 - else if (strcmp(fmt, "S,vmtotal") == 0) 1.758 - func = S_vmtotal; 1.759 - else if (strcmp(fmt, "T,dev_t") == 0) 1.760 - func = T_dev_t; 1.761 - else 1.762 - func = NULL; 1.763 - 1.764 - if (func) { 1.765 - (*func)(L, len, val); 1.766 - break; 1.767 - } 1.768 - /* FALLTHROUGH */ 1.769 - default: 1.770 - free(oval); 1.771 - return (luaL_error(L, "unknown CTLTYPE: fmt=%s kind=%d", 1.772 - fmt, (kind & CTLTYPE))); 1.773 - } 1.774 - 1.775 - free(oval); 1.776 - lua_pushstring(L, fmt); 1.777 - return (2); /* two returned value */ 1.778 + if (func) { 1.779 + (*func)(L, len, val); 1.780 + break; 1.781 + } 1.782 + /* FALLTHROUGH */ 1.783 + default: 1.784 + free(oval); 1.785 + return (luaL_error(L, "unknown CTLTYPE: fmt=%s kind=%d", 1.786 + fmt, (kind & CTLTYPE))); 1.787 + } 1.788 + free(oval); 1.789 + lua_pushstring(L, fmt); 1.790 + return (2); /* two returned value */ 1.791 } 1.792 1.793 1.794 @@ -505,8 +507,8 @@ 1.795 luaA_sysctl_IK2celsius(lua_State *L) 1.796 { 1.797 1.798 - lua_pushnumber(L, (luaL_checkinteger(L, 1) - 2732.0) / 10); 1.799 - return (1); /* one returned value */ 1.800 + lua_pushnumber(L, (luaL_checkinteger(L, 1) - 2732.0) / 10); 1.801 + return (1); /* one returned value */ 1.802 } 1.803 1.804 1.805 @@ -514,8 +516,8 @@ 1.806 luaA_sysctl_IK2farenheit(lua_State *L) 1.807 { 1.808 1.809 - lua_pushnumber(L, (luaL_checkinteger(L, 1) / 10.0) * 1.8 - 459.67); 1.810 - return (1); /* one returned value */ 1.811 + lua_pushnumber(L, (luaL_checkinteger(L, 1) / 10.0) * 1.8 - 459.67); 1.812 + return (1); /* one returned value */ 1.813 } 1.814 1.815 1.816 @@ -526,11 +528,11 @@ 1.817 1.818 static const luaL_reg lua_sysctl[] = 1.819 { 1.820 - {"get", luaA_sysctl_get}, 1.821 - {"set", luaA_sysctl_set}, 1.822 - {"IK2celsius", luaA_sysctl_IK2celsius}, 1.823 - {"IK2farenheit", luaA_sysctl_IK2farenheit}, 1.824 - {NULL, NULL} 1.825 + {"get", luaA_sysctl_get}, 1.826 + {"set", luaA_sysctl_set}, 1.827 + {"IK2celsius", luaA_sysctl_IK2celsius}, 1.828 + {"IK2farenheit", luaA_sysctl_IK2farenheit}, 1.829 + {NULL, NULL} 1.830 }; 1.831 1.832 /* 1.833 @@ -539,37 +541,39 @@ 1.834 LUALIB_API int 1.835 luaopen_sysctl_core(lua_State *L) 1.836 { 1.837 - luaL_openlib(L, "sysctl", lua_sysctl, 0); 1.838 - return (1); 1.839 + 1.840 + luaL_openlib(L, "sysctl", lua_sysctl, 0); 1.841 + return (1); 1.842 } 1.843 1.844 1.845 static int 1.846 set_IK(char *str, int *val) 1.847 { 1.848 - float temp; 1.849 - int len, kelv; 1.850 - char *p, *endptr, unit; 1.851 + float temp; 1.852 + int len; 1.853 + int kelv; 1.854 + char *p, *endptr, unit; 1.855 1.856 - if ((len = strlen(str)) == 0) 1.857 - return (0); 1.858 - p = &str[len - 1]; 1.859 - if (*p == 'C' || *p == 'F') { 1.860 - unit = *p; 1.861 - *p = '\0'; 1.862 - temp = strtof(str, &endptr); 1.863 - if (endptr == str || *endptr != '\0') 1.864 - return (0); 1.865 - if (unit == 'F') 1.866 - temp = (temp - 32) * 5 / 9; 1.867 - kelv = temp * 10 + 2732; 1.868 - } else { 1.869 - kelv = (int)strtol(str, &endptr, 10); 1.870 - if (endptr == str || *endptr != '\0') 1.871 - return (0); 1.872 - } 1.873 - *val = kelv; 1.874 - return (1); 1.875 + if ((len = strlen(str)) == 0) 1.876 + return (0); 1.877 + p = &str[len - 1]; 1.878 + if (*p == 'C' || *p == 'F') { 1.879 + unit = *p; 1.880 + *p = '\0'; 1.881 + temp = strtof(str, &endptr); 1.882 + if (endptr == str || *endptr != '\0') 1.883 + return (0); 1.884 + if (unit == 'F') 1.885 + temp = (temp - 32) * 5 / 9; 1.886 + kelv = temp * 10 + 2732; 1.887 + } else { 1.888 + kelv = (int)strtol(str, &endptr, 10); 1.889 + if (endptr == str || *endptr != '\0') 1.890 + return (0); 1.891 + } 1.892 + *val = kelv; 1.893 + return (1); 1.894 } 1.895 1.896 /* 1.897 @@ -584,45 +588,45 @@ 1.898 static int 1.899 name2oid(char *name, int *oidp) 1.900 { 1.901 - int oid[2]; 1.902 - int i; 1.903 - size_t j; 1.904 + int oid[2]; 1.905 + int i; 1.906 + size_t j; 1.907 1.908 - oid[0] = 0; 1.909 - oid[1] = 3; 1.910 + oid[0] = 0; 1.911 + oid[1] = 3; 1.912 1.913 - j = CTL_MAXNAME * sizeof(int); 1.914 - i = sysctl(oid, 2, oidp, &j, name, strlen(name)); 1.915 - if (i < 0) 1.916 - return (i); 1.917 - j /= sizeof(int); 1.918 - return (j); 1.919 + j = CTL_MAXNAME * sizeof(int); 1.920 + i = sysctl(oid, 2, oidp, &j, name, strlen(name)); 1.921 + if (i < 0) 1.922 + return (i); 1.923 + j /= sizeof(int); 1.924 + return (j); 1.925 } 1.926 1.927 1.928 static int 1.929 oidfmt(int *oid, int len, char *fmt, size_t fmtsiz, u_int *kind) 1.930 { 1.931 - int qoid[CTL_MAXNAME+2]; 1.932 - u_char buf[BUFSIZ]; 1.933 - int i; 1.934 - size_t j; 1.935 + int qoid[CTL_MAXNAME+2]; 1.936 + u_char buf[BUFSIZ]; 1.937 + int i; 1.938 + size_t j; 1.939 1.940 - qoid[0] = 0; 1.941 - qoid[1] = 4; 1.942 - memcpy(qoid + 2, oid, len * sizeof(int)); 1.943 + qoid[0] = 0; 1.944 + qoid[1] = 4; 1.945 + memcpy(qoid + 2, oid, len * sizeof(int)); 1.946 1.947 - j = sizeof(buf); 1.948 - i = sysctl(qoid, len + 2, buf, &j, 0, 0); 1.949 - if (i) 1.950 - return (1); 1.951 + j = sizeof(buf); 1.952 + i = sysctl(qoid, len + 2, buf, &j, 0, 0); 1.953 + if (i) 1.954 + return (1); 1.955 1.956 - if (kind) 1.957 - *kind = *(u_int *)buf; 1.958 + if (kind) 1.959 + *kind = *(u_int *)buf; 1.960 1.961 - if (fmt) { 1.962 - if (strlcpy(fmt, (char *)(buf + sizeof(u_int)), fmtsiz) >= fmtsiz) 1.963 - return (1); 1.964 - } 1.965 - return (0); 1.966 + if (fmt) { 1.967 + if (strlcpy(fmt, (char *)(buf + sizeof(u_int)), fmtsiz) >= fmtsiz) 1.968 + return (1); 1.969 + } 1.970 + return (0); 1.971 }