Check-in [cb3d40f15b]
Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Reworked wrapper script to allow for piping and `-` placeholder parameters, and array functions be called (implicit JSON input/output). |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
cb3d40f15b80d1e6b86b16ce894aa190 |
User & Date: | mario 2014-12-12 19:41:59 |
Context
2014-12-12
| ||
20:10 | Use array_shift for multiple positional - - - stdin/json placeholders. Don't populate $args from optional args. check-in: e4e8e5030f user: mario tags: trunk | |
19:41 | Reworked wrapper script to allow for piping and `-` placeholder parameters, and array functions be called (implicit JSON input/output). check-in: cb3d40f15b user: mario tags: trunk | |
2014-03-21
| ||
01:28 | Shortened. check-in: 63d55e97c1 user: mario tags: trunk | |
Changes
Added php/Makefile.
> > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | LINK = ln -s HANDLER = pipefunc.php FUNCS = addcslashes addslashes all array_pop array_push \ base64_decode base64_encode base_convert convert_uudecode crc32 \ dechex deserialize gmdate gmstrftime gzcompress gzdecode gzdeflate \ gzencode gzinflate gzuncompress hex2bin hexbin hexdec \ htmlspecialchars htmlspecialchars_decode json_decode json_encode \ ltrim Makefile md5 ord preg_match preg_replace print_r rand \ rawurlencode rawurlencode2 rtrim serialize strftime stripcslashes \ stripslashes stristr strlen strnatcasecmp strncasecmp strncmp strpos \ strripos str_rot13 strrpos str_split strstr strtok strtolower \ strtotime strtoupper trim ucfirst ucwords unserialize urldecode \ urlencode var_dump version_compare wordwrap \ all: php php: for F in $(FUNCS); do $(LINK) $(HANDLER) $$F ; done |
Name change from php/PhpFunctionCall to php/pipefunc.php.
1 2 3 | #!/usr/bin/php -qC <?php /** | | | > > > | > > > > > > > > | > > > > > > > > > > > > > | > | > > > > > > > > > | > > > > > > > > > > > > > > > | | | > > > > > > | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #!/usr/bin/php -qC <?php /** * title: Shell PHP functions * description: Pipe from and to PHP functions from the shell. * type: cli * version: 0.8 * depends: php:cli * * Wrapper script which allows PHP functions to be called directly from the * shell. Parameters can be supplied directly as arguments, or piped in via * STDIN - which either becomes the first parameter, or substitutes any * "-" argument placeholder. * * strtolower ABCDE * echo "x?? 2" | urlencode * echo "p" | strpos "inpos" - * * The simple invocation forms are Most useful with string functions. * But arrays can be passed in, and results are returned as JSON. * * echo [1,2,3] | array_push - 4 | print_r * * Mostly function return values are output, but for &$result signatures * the reference-parameter will be assumed as primary result value. * * preg_match /A+/ AAABBBCCC * * Setup requires just a few symlinks * e.g. * ln -s PhpFunctionCall strtolower * ln -s PhpFunctionCall base64_decode * ln -s PhpFunctionCall unserialize * * The included Makefile will create the most useful shortcuts. * */ #-- Function name $func = basename(array_shift($_SERVER["argv"])); set_error_handler("stderr_color"); #-- Arguments used as function params $args = $_SERVER["argv"]; $dashes = count(array_intersect($args, ["-"])); #-- STDIN can be input parameter(s) if (!posix_isatty(STDIN) and is_string($stdin = rtrim(fread(STDIN, 1<<24), "\n")) or ($dashes and is_null($stdin = NULL))) { // Unpack JSON if (preg_match("/^ \s* [\[\{] .+ [\]\}] \s* $/smix", $stdin)) { $stdin = json_decode($stdin, TRUE); } // Allot array values to multiple params if (is_array($stdin) and ($dashes >= 2)) { while (count($stdin)) { $args[array_search("-", $args, TRUE)] = array_pop($stdin); } } // Substitute `-` args elseif ($dashes == 1) { $args[array_search("-", $args, TRUE)] = $stdin; } // Just use as first param else { $args = array_merge([$stdin], $args); } } #print_r($args); #-- Run target function, capture result value list($fref, $i_result) = reflect_args($func, $args); #$func =$fref; if (is_int($i_result)) { $_ = $fref->invokeArgs($args); $r = $args[$i_result]; } else { $r = call_user_func_array($func, $args); #var_dump($r); } #-- Output result if (in_array($func, str_getcsv("print_r,print,var_dump")) && !is_string($r)) { // as errno exit(intval($r)); } else { // string or JSON struct print (is_scalar($r) || is_null($r)) ? strval($r) : json_encode($r, JSON_PRETTY_PRINT); } // print PHP errors/warnings to STDERR function stderr_color($errno, $errstr, $errfile=NULL, $errline=NULL, $errcontext=NULL) { $errname = array_search($errno, get_defined_constants()); global $func; fwrite(STDERR, "\x1b[31m$errname \x1b[0m(\x1b[32m$func\x1b[0m): \x1b[33m$errstr\x1b[0m\n"); } // Apply & references, if one of the function parameters expects one, keep that as result parameter index function reflect_args($func, & $args) { $fref = new ReflectionFunction($func); foreach ($fref->getParameters() as $i => $param) { $args[$i] = & $args[$i]; if ($param->isPassedByReference()) { return [$fref, $i]; } } return [$fref, false]; } ?> |