ffi: improve support for functions returning arrays
[m6w6/ext-psi] / psi.d / getopt.psi
index 5e1e427ce50cec30c22d678b954bb9815c4e131b..962ac970fb74ae66f126d2a78c554fec1ccb3c0d 100644 (file)
@@ -1,25 +1,40 @@
+#include <getopt.h>
+
 function psi\opterr(int $value) : void {
-       let _v = intval($value);
-       return void(opterr_set);
+       let opterr = intval($value);
+       return opterr_set(opterr) as void(opterr_set);
+}
+function psi\optind\get() : int {
+       return optind_get() as to_int(optind_get);
 }
-function psi\optind() : int {
-       return to_int(optind);
+function psi\optind\set(int $v) : void {
+       let optind = intval($v);
+       return optind_set(optind) as void(optind_set);
 }
 function psi\optopt() : int {
-       return to_int(optopt);
+       return optopt_get() as to_int(optopt_get);
 }
 function psi\optarg() : string {
-       return to_string(optarg);
+       return optarg_get() as to_string(optarg_get);
+}
+
+#ifdef _OPTRESET
+function psi\optreset() : void {
+       let _v = 1;
+       return optreset_set(_v) as void(optreset_set);
 }
+#endif
 
-function psi\getopt(array &$argv, string $options) : int {
+static function psi\getopt(array &$argv, string $options) : int {
        let argc = count($argv);
        let argv = &arrval($argv,
                *argv = strval($argv)
        );
        let optstring = strval($options);
-       return to_int(getopt); 
-       set $argv = to_array(argv,
-               to_string(argv)
+       return getopt(argc, argv, optstring) as to_int(getopt);
+       set $argv = to_array(
+               *argv,
+               argc,
+               to_string(*argv)
        );
 }