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: 65 86 75.6 %
Date: 2014-07-13 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 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           3 : static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err) /* {{{ */
      32             : {
      33           3 :         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           3 :         return('?');
      53             : }
      54             : /* }}} */
      55             : 
      56             : PHPAPI int php_optidx = -1;
      57             : 
      58     1872507 : 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             : 
      63     1872507 :         php_optidx = -1;
      64             : 
      65     1872507 :         if (*optind >= argc) {
      66       34549 :                 return(EOF);
      67             :         }
      68     1837958 :         if (!dash) {
      69     1837942 :                 if ((argv[*optind][0] !=  '-')) {
      70       23221 :                         return(EOF);
      71             :                 } else {
      72     1814721 :                         if (!argv[*optind][1])
      73             :                         {
      74             :                                 /*
      75             :                                 * use to specify stdin. Need to let pgm process this and
      76             :                                 * the following args
      77             :                                 */
      78           0 :                                 return(EOF);
      79             :                         }
      80             :                 }
      81             :         }
      82     1814785 :         if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) {
      83             :                 char *pos;
      84          74 :                 int arg_end = strlen(argv[*optind])-1;
      85             : 
      86             :                 /* '--' indicates end of args if not followed by a known long option name */
      87          74 :                 if (argv[*optind][2] == '\0') {
      88          24 :                         (*optind)++;
      89          24 :                         return(EOF);
      90             :                 }
      91             : 
      92          50 :                 arg_start = 2;
      93             : 
      94             :                 /* Check for <arg>=<val> */
      95          50 :                 if ((pos = php_memnstr(&argv[*optind][arg_start], "=", 1, argv[*optind]+arg_end)) != NULL) {
      96           2 :                         arg_end = pos-&argv[*optind][arg_start];
      97           2 :                         arg_start++;
      98             :                 } else {
      99          48 :                         arg_end--;
     100             :                 }
     101             : 
     102             :                 while (1) {
     103        1141 :                         php_optidx++;
     104        1141 :                         if (opts[php_optidx].opt_char == '-') {
     105           2 :                                 (*optind)++;
     106           2 :                                 return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
     107        1139 :                         } 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)) {
     108          48 :                                 break;
     109             :                         }
     110        1091 :                 }
     111             : 
     112          48 :                 optchr = 0;
     113          48 :                 dash = 0;
     114          48 :                 arg_start += strlen(opts[php_optidx].opt_name);
     115             :         } else {
     116     1814663 :                 if (!dash) {
     117     1814647 :                         dash = 1;
     118     1814647 :                         optchr = 1;
     119             :                 }
     120             :                 /* Check if the guy tries to do a -: kind of flag */
     121     1814663 :                 if (argv[*optind][optchr] == ':') {
     122           0 :                         dash = 0;
     123           0 :                         (*optind)++;
     124           0 :                         return (php_opt_error(argc, argv, *optind-1, optchr, OPTERRCOLON, show_err));
     125             :                 }
     126     1814663 :                 arg_start = 1 + optchr;
     127             :         }
     128     1814711 :         if (php_optidx < 0) {
     129             :                 while (1) {
     130     9902019 :                         php_optidx++;
     131     9902019 :                         if (opts[php_optidx].opt_char == '-') {
     132           0 :                                 int errind = *optind;
     133           0 :                                 int errchr = optchr;
     134             : 
     135           0 :                                 if (!argv[*optind][optchr+1]) {
     136           0 :                                         dash = 0;
     137           0 :                                         (*optind)++;
     138             :                                 } else {
     139           0 :                                         optchr++;
     140           0 :                                         arg_start++;
     141             :                                 }
     142           0 :                                 return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err));
     143     9902019 :                         } else if (argv[*optind][optchr] == opts[php_optidx].opt_char) {
     144     1814663 :                                 break;
     145             :                         }
     146     8087356 :                 }
     147             :         }
     148     1814711 :         if (opts[php_optidx].need_param) {
     149             :                 /* Check for cases where the value of the argument
     150             :                 is in the form -<arg> <val>, -<arg>=<varl> or -<arg><val> */
     151     1732910 :                 dash = 0;
     152     1732910 :                 if (!argv[*optind][arg_start]) {
     153     1732829 :                         (*optind)++;
     154     1732829 :                         if (*optind == argc) {
     155             :                                 /* Was the value required or is it optional? */
     156           1 :                                 if (opts[php_optidx].need_param == 1) {
     157           1 :                                         return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
     158             :                                 }
     159             :                         /* Optional value is not supported with -<arg> <val> style */
     160     1732828 :                         } else if (opts[php_optidx].need_param == 1) {
     161     1732826 :                                 *optarg = argv[(*optind)++];
     162             :                         }
     163          81 :                 } else if (argv[*optind][arg_start] == '=') {
     164           2 :                         arg_start++;
     165           2 :                         *optarg = &argv[*optind][arg_start];
     166           2 :                         (*optind)++;
     167             :                 } else {
     168          79 :                         *optarg = &argv[*optind][arg_start];
     169          79 :                         (*optind)++;
     170             :                 }
     171     1732909 :                 return opts[php_optidx].opt_char;
     172             :         } else {
     173             :                 /* multiple options specified as one (exclude long opts) */
     174      163592 :                 if (arg_start >= 2 && !((argv[*optind][0] == '-') && (argv[*optind][1] == '-'))) {
     175       81791 :                         if (!argv[*optind][optchr+1])
     176             :                         {
     177       81775 :                                 dash = 0;
     178       81775 :                                 (*optind)++;
     179             :                         } else {
     180          16 :                                 optchr++;
     181             :                         }
     182             :                 } else {
     183          10 :                         (*optind)++;
     184             :                 }
     185       81801 :                 return opts[php_optidx].opt_char;
     186             :         }
     187             :         assert(0);
     188             :         return(0);      /* never reached */
     189             : }
     190             : /* }}} */
     191             : 
     192             : /*
     193             :  * Local variables:
     194             :  * tab-width: 4
     195             :  * c-basic-offset: 4
     196             :  * End:
     197             :  * vim600: sw=4 ts=4 fdm=marker
     198             :  * vim<600: sw=4 ts=4
     199             :  */

Generated by: LCOV version 1.10

Generated at Sun, 13 Jul 2014 23:58:03 +0000 (9 days ago)

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