From f02befd91518565718bdadb8960f373cefec3095 Mon Sep 17 00:00:00 2001 From: Ben Wiley Sittler Date: Fri, 7 Nov 2008 08:33:03 +0000 Subject: [PATCH] better argument handling, and allow logo-caca.png as a special file argument --- caca-php/examples/www/img2txt.php | 41 ++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/caca-php/examples/www/img2txt.php b/caca-php/examples/www/img2txt.php index e7543e5..87551f7 100644 --- a/caca-php/examples/www/img2txt.php +++ b/caca-php/examples/www/img2txt.php @@ -25,17 +25,18 @@ $img2txt_php = isset($_SERVER['SCRIPT_NAME']) 'img2txt.php'; $argv = array(basename($img2txt_php)); -$args = ''; +$file = isset($_FILES['file']) ? $_FILES['file']['tmp_name'] : NULL; +$filename = isset($_FILES['file']) ? $_FILES['file']['name'] : NULL; +$args = NULL; if(isset($_REQUEST['args'])) { $args = $_REQUEST['args']; -} -$args=trim($args); -if(strlen($args)) -{ - foreach(explode(' ', $args) as $arg) + if(strlen($args)) { - $argv[] = $arg; + foreach(explode(' ', $args) as $arg) + { + $argv[] = $arg; + } } } $argc = count($argv); @@ -223,7 +224,7 @@ function usage($argc, $argv) global $stderr; $stderr .= sprintf("Usage: %s [OPTIONS]... \n", $argv[0]); $stderr .= sprintf("Convert IMAGE to any text based available format.\n"); - $stderr .= sprintf("Example : %s -w 80 -f ansi ./caca.png\n\n", $argv[0]); + $stderr .= sprintf("Example : -W 80 -f html logo-caca.png\n\n", $argv[0]); $stderr .= sprintf("Options:\n"); $stderr .= sprintf(" -h, --help\t\t\tThis help\n"); $stderr .= sprintf(" -v, --version\t\t\tVersion of the program\n"); @@ -347,6 +348,14 @@ function main() return 2; } + global $file, $filename; + + if((! $file) && ($argc == 2) && ($argv[1] == 'logo-caca.png')) + { + $file = 'logo-caca.png'; + $argc = 1; + } + if($argc > 1) { $stderr .= sprintf("%s: too many arguments\n", $argv[0]); @@ -354,9 +363,6 @@ function main() return 1; } - $file = isset($_FILES['file']) ? $_FILES['file']['tmp_name'] : NULL; - $filename = isset($_FILES['file']) ? $_FILES['file']['name'] : NULL; - if(! $file) { $stderr .= sprintf("%s: no image was provided\n", $argv[0]); @@ -465,7 +471,12 @@ function main() return 0; } -if(main() || strlen($stdout) || strlen($stderr)) +$ret = 1; +if(isset($_REQUEST['args']) || $file) +{ + $ret = main(); +} +if($ret || strlen($stdout) || strlen($stderr)) { header('Content-Type: text/html; charset=UTF-8'); ?> @@ -483,7 +494,7 @@ if(main() || strlen($stdout) || strlen($stderr))
- +
@@ -491,12 +502,12 @@ if(main() || strlen($stdout) || strlen($stderr)) ; if(strlen($stderr)) { - ?>
$1', htmlspecialchars($stderr)) ?>
$2$3', preg_replace('!(\s|^)(http://[-.:_/0-9a-zA-Z%?=&;#]+)(\s|$)!', '$1$2$3', htmlspecialchars($stdout))) ?>