PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - main - getopt.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 69 90 76.7 %
Date: 2014-11-22 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Marcus Boerger <helly@php.net>                               |
      16             :    +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include <stdio.h>
      22             : #include <string.h>
      23             : #include <assert.h>
      24             : #include <stdlib.h>
      25             : #include "php_getopt.h"
      26             : 
      27             : #define OPTERRCOLON (1)
      28             : #define OPTERRNF (2)
      29             : #define OPTERRARG (3)
      30             : 
      31           2 : static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err) /* {{{ */
      32             : {
      33           2 :         if (show_err)
      34             :         {
      35           0 :                 fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
      36           0 :                 switch(err)
      37             :                 {
      38             :                 case OPTERRCOLON:
      39           0 :                         fprintf(stderr, ": in flags\n");
      40           0 :                         break;
      41             :                 case OPTERRNF:
      42           0 :                         fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
      43           0 :                         break;
      44             :                 case OPTERRARG:
      45           0 :                         fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
      46           0 :                         break;
      47             :                 default:
      48           0 :                         fprintf(stderr, "unknown\n");
      49             :                         break;
      50             :                 }
      51             :         }
      52           2 :         return('?');
      53             : }
      54             : /* }}} */
      55             : 
      56             : PHPAPI int php_optidx = -1;
      57             : 
      58     2164469 : PHPAPI int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err, int arg_start) /* {{{ */
      59             : {
      60             :         static int optchr = 0;
      61             :         static int dash = 0; /* have already seen the - */
      62             :         static char **prev_optarg = NULL;
      63             : 
      64     2164469 :         php_optidx = -1;
      65             : 
      66     2164469 :         if(prev_optarg && prev_optarg != optarg) {
      67             :                 /* reset the state */
      68       20109 :                 optchr = 0;
      69       20109 :                 dash = 0;
      70             :         }
      71     2164469 :         prev_optarg = optarg;
      72             : 
      73     2164469 :         if (*optind >= argc) {
      74       37152 :                 return(EOF);
      75             :         }
      76     2127317 :         if (!dash) {
      77     2127301 :                 if ((argv[*optind][0] !=  '-')) {
      78       24048 :                         return(EOF);
      79             :                 } else {
      80     2103253 :                         if (!argv[*optind][1])
      81             :                         {
      82             :                                 /*
      83             :                                 * use to specify stdin. Need to let pgm process this and
      84             :                                 * the following args
      85             :                                 */
      86           0 :                                 return(EOF);
      87             :                         }
      88             :                 }
      89             :         }
      90     2103317 :         if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) {
      91             :                 const char *pos;
      92          73 :                 int arg_end = (int)strlen(argv[*optind])-1;
      93             : 
      94             :                 /* '--' indicates end of args if not followed by a known long option name */
      95          73 :                 if (argv[*optind][2] == '\0') {
      96          24 :                         (*optind)++;
      97          24 :                         return(EOF);
      98             :                 }
      99             : 
     100          49 :                 arg_start = 2;
     101             : 
     102             :                 /* Check for <arg>=<val> */
     103          98 :                 if ((pos = php_memnstr(&argv[*optind][arg_start], "=", 1, argv[*optind]+arg_end)) != NULL) {
     104           2 :                         arg_end = pos-&argv[*optind][arg_start];
     105           2 :                         arg_start++;
     106             :                 } else {
     107          47 :                         arg_end--;
     108             :                 }
     109             : 
     110             :                 while (1) {
     111        1197 :                         php_optidx++;
     112        1197 :                         if (opts[php_optidx].opt_char == '-') {
     113           1 :                                 (*optind)++;
     114           1 :                                 return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
     115        1196 :                         } else if (opts[php_optidx].opt_name && !strncmp(&argv[*optind][2], opts[php_optidx].opt_name, arg_end) && arg_end == strlen(opts[php_optidx].opt_name)) {
     116          48 :                                 break;
     117             :                         }
     118        1148 :                 }
     119             : 
     120          48 :                 optchr = 0;
     121          48 :                 dash = 0;
     122          48 :                 arg_start += (int)strlen(opts[php_optidx].opt_name);
     123             :         } else {
     124     2103196 :                 if (!dash) {
     125     2103180 :                         dash = 1;
     126     2103180 :                         optchr = 1;
     127             :                 }
     128             :                 /* Check if the guy tries to do a -: kind of flag */
     129     2103196 :                 if (argv[*optind][optchr] == ':') {
     130           0 :                         dash = 0;
     131           0 :                         (*optind)++;
     132           0 :                         return (php_opt_error(argc, argv, *optind-1, optchr, OPTERRCOLON, show_err));
     133             :                 }
     134     2103196 :                 arg_start = 1 + optchr;
     135             :         }
     136     2103244 :         if (php_optidx < 0) {
     137             :                 while (1) {
     138    11389205 :                         php_optidx++;
     139    11389205 :                         if (opts[php_optidx].opt_char == '-') {
     140           0 :                                 int errind = *optind;
     141           0 :                                 int errchr = optchr;
     142             : 
     143           0 :                                 if (!argv[*optind][optchr+1]) {
     144           0 :                                         dash = 0;
     145           0 :                                         (*optind)++;
     146             :                                 } else {
     147           0 :                                         optchr++;
     148           0 :                                         arg_start++;
     149             :                                 }
     150           0 :                                 return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err));
     151    11389205 :                         } else if (argv[*optind][optchr] == opts[php_optidx].opt_char) {
     152     2103196 :                                 break;
     153             :                         }
     154     9286009 :                 }
     155             :         }
     156     2103244 :         if (opts[php_optidx].need_param) {
     157             :                 /* Check for cases where the value of the argument
     158             :                 is in the form -<arg> <val>, -<arg>=<varl> or -<arg><val> */
     159     2017161 :                 dash = 0;
     160     2017161 :                 if (!argv[*optind][arg_start]) {
     161     2017080 :                         (*optind)++;
     162     2017080 :                         if (*optind == argc) {
     163             :                                 /* Was the value required or is it optional? */
     164           1 :                                 if (opts[php_optidx].need_param == 1) {
     165           1 :                                         return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
     166             :                                 }
     167             :                         /* Optional value is not supported with -<arg> <val> style */
     168     2017079 :                         } else if (opts[php_optidx].need_param == 1) {
     169     2017077 :                                 *optarg = argv[(*optind)++];
     170             :                         }
     171          81 :                 } else if (argv[*optind][arg_start] == '=') {
     172           2 :                         arg_start++;
     173           2 :                         *optarg = &argv[*optind][arg_start];
     174           2 :                         (*optind)++;
     175             :                 } else {
     176          79 :                         *optarg = &argv[*optind][arg_start];
     177          79 :                         (*optind)++;
     178             :                 }
     179     2017160 :                 return opts[php_optidx].opt_char;
     180             :         } else {
     181             :                 /* multiple options specified as one (exclude long opts) */
     182      172156 :                 if (arg_start >= 2 && !((argv[*optind][0] == '-') && (argv[*optind][1] == '-'))) {
     183       86073 :                         if (!argv[*optind][optchr+1])
     184             :                         {
     185       86057 :                                 dash = 0;
     186       86057 :                                 (*optind)++;
     187             :                         } else {
     188          16 :                                 optchr++;
     189             :                         }
     190             :                 } else {
     191          10 :                         (*optind)++;
     192             :                 }
     193       86083 :                 return opts[php_optidx].opt_char;
     194             :         }
     195             :         assert(0);
     196             :         return(0);      /* never reached */
     197             : }
     198             : /* }}} */
     199             : 
     200             : /*
     201             :  * Local variables:
     202             :  * tab-width: 4
     203             :  * c-basic-offset: 4
     204             :  * End:
     205             :  * vim600: sw=4 ts=4 fdm=marker
     206             :  * vim<600: sw=4 ts=4
     207             :  */

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:30 +0000 (46 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.