The following path for cli version is concerned with passing arguments
to scripts. I found out that not all ways work correct and $PHP_SELF is not set
at all. The four example below explain the patch. All four execute:
echo "$PHP_SELF\n";
echo join($argv,',')."\n";

1) Execute a file using -f and pass 'a -b c' -> before patch scriptfile was 
set to a -> didn't work
$ php -f /t/temp/arg.php a -b c
/t/temp/arg.php
/t/temp/arg.php,a,-b,c

2) Execute a file using without and pass 'a -b c' -> before patch 
scriptfile was set to a -> didn't work
$ php /t/temp/arg.php a -b c
/t/temp/arg.php
/t/temp/arg.php,/t/temp/arg.php,a,-b,c

3) Execute code directly and pass 'a -b c' -> before patch scriptfile was 
set to a -> didn't work
$ php -r 'echo "$PHP_SELF\n".join($argv,",")."\n";' a -b c
-
-,a,-b,c

4) Execute stdin and pass 'a -b c' -> before patch passing arguments wasn't 
possible at all
$ echo '<?echo "$PHP_SELF\n".join($argv,",")."\n";?>' | php -- a -b c
-
-,a,-b,c

There was no need to change function ap_php_getopt as it does handle -- to 
end interpreting arguments.
But i added PHP_SELF and set it to either the executed script or "-" for 
stdin/run code. The
value for argv[0] seen by script is set to the same value to make argument 
enumerating consistent.

An opportunity would be setting argv[0] of script to argv[0] of cli. This 
way the user could get the
execution-path on some systems.

Any suggenstions? May i commit the change?

regards
marcus


diff -u -w -r1.9 php_cli.c
--- sapi/cli/php_cli.c  8 Mar 2002 09:55:58 -0000       1.9
+++ sapi/cli/php_cli.c  10 Mar 2002 14:53:17 -0000
@@ -240,7 +241,9 @@
                 prog = "php";
         }

-       php_printf("Usage: %s [-h] [-s] [-v] [-i] [-f <file>] |  {<file> 
[args...]}\n"
+       php_printf( "Usage: %s [options] [-f] <file> [args...]\n"
+                   "       %s [options] -r <code> [args...]\n"
+                   "       %s [options] [-- args...]\n"
                                 "  -s             Display colour syntax 
highlighted source.\n"
                                 "  -w             Display source with 
stripped comments and whitespace.\n"
                                 "  -f <file>      Parse <file>.\n"
@@ -253,8 +256,12 @@
                                 "  -l             Syntax check only (lint)\n"
                                 "  -m             Show compiled in modules\n"
                                 "  -i             PHP information\n"
-                               "  -r <code>      Run PHP <code>\n"
-                               "  -h             This help\n", prog);
+                               "  -r <code>      Run PHP <code> without 
using script tags <?..?>\n"
+                               "  -h             This help\n"
+                               "\n"
+                               "  args...        Arguments passed to 
script. Use -- args when first argument \n"
+                               "                 starts with - or script 
is read from stdin\n"
+                               , prog, prog, prog);
  }
  /* }}} */

@@ -301,6 +308,7 @@
         int no_headers=1;
         int orig_optind=ap_php_optind;
         char *orig_optarg=ap_php_optarg;
+       char *arg_free=NULL, **arg_excp=&arg_free;
         char *script_file=NULL;
         zend_llist global_vars;
         int interactive=0;
@@ -512,18 +520,32 @@
                         }
                 }

+               /* only set script_file if not set already and not in 
direct mode and not at end of parameter list */
+               if (argc > ap_php_optind && !script_file && !exec_direct && 
strcmp(argv[ap_php_optind-1],"--")) {
+                       script_file=argv[ap_php_optind];
+               }
+
                 CG(interactive) = interactive;
-               SG(request_info).argc=argc-ap_php_optind;
-               SG(request_info).argv=argv+ap_php_optind;

-               if (argc > ap_php_optind) {
-                       script_file=argv[ap_php_optind];
+               /* before registering argv to modulule exchange the *new* 
argv[0] */
+               /* we can achieve this without allocating more memory */
+               SG(request_info).argc=argc-ap_php_optind+1;
+               arg_excp = argv+ap_php_optind-1;
+               arg_free = argv[ap_php_optind-1];
+               if (script_file) {
+                       argv[ap_php_optind-1] = script_file;
+               } else {
+                       argv[ap_php_optind-1] = "-"; /* should be stdin */
                 }
+               SG(request_info).argv=argv+ap_php_optind-1;

                 if (php_request_startup(TSRMLS_C)==FAILURE) {
                         php_module_shutdown(TSRMLS_C);
+                       *arg_excp = arg_free;
                         return FAILURE;
                 }
+               *arg_excp = arg_free; /* reconstuct argv */
                 if (no_headers) {
                         SG(headers_sent) = 1;
                         SG(request_info).no_headers = 1;
@@ -535,6 +557,7 @@
                                 php_module_shutdown(TSRMLS_C);
                                 return FAILURE;
                         }
+                       php_register_variable("PHP_SELF", script_file, NULL 
TSRMLS_CC);
                         file_handle.filename = script_file;
                         /* #!php support */
                         c = fgetc(file_handle.handle.fp);
@@ -547,6 +570,7 @@
                                 rewind(file_handle.handle.fp);
                         }
                 } else {
+                       php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
                         file_handle.filename = "-";
                         file_handle.handle.fp = stdin;
                 }



--------->>> mailto:[EMAIL PROTECTED] <<<------------
"We are animals among animals, all children of matter,
save that we are the more disarmed. But since, unlike animals,
we know that we must die, let us prepare for that moment
by enjoying the life that has been given us by chance and for chance."
                        Umberto Eco, The island of the day before
--------------->>> http://www.marcus-boerger.de <<<-------------------


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to