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-21 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-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     2245059 : 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     2245059 :         php_optidx = -1;
      64             : 
      65     2245059 :         if (*optind >= argc) {
      66       38130 :                 return(EOF);
      67             :         }
      68     2206929 :         if (!dash) {
      69     2206913 :                 if ((argv[*optind][0] !=  '-')) {
      70       25320 :                         return(EOF);
      71             :                 } else {
      72     2181593 :                         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     2181657 :         if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) {
      83             :                 char *pos;
      84          73 :                 int arg_end = strlen(argv[*optind])-1;
      85             : 
      86             :                 /* '--' indicates end of args if not followed by a known long option name */
      87          73 :                 if (argv[*optind][2] == '\0') {
      88          24 :                         (*optind)++;
      89          24 :                         return(EOF);
      90             :                 }
      91             : 
      92          49 :                 arg_start = 2;
      93             : 
      94             :                 /* Check for <arg>=<val> */
      95          49 :                 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          47 :                         arg_end--;
     100             :                 }
     101             : 
     102             :                 while (1) {
     103        1197 :                         php_optidx++;
     104        1197 :                         if (opts[php_optidx].opt_char == '-') {
     105           1 :                                 (*optind)++;
     106           1 :                                 return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
     107        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)) {
     108          48 :                                 break;
     109             :                         }
     110        1148 :                 }
     111             : 
     112          48 :                 optchr = 0;
     113          48 :                 dash = 0;
     114          48 :                 arg_start += strlen(opts[php_optidx].opt_name);
     115             :         } else {
     116     2181536 :                 if (!dash) {
     117     2181520 :                         dash = 1;
     118     2181520 :                         optchr = 1;
     119             :                 }
     120             :                 /* Check if the guy tries to do a -: kind of flag */
     121     2181536 :                 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     2181536 :                 arg_start = 1 + optchr;
     127             :         }
     128     2181584 :         if (php_optidx < 0) {
     129             :                 while (1) {
     130    11815585 :                         php_optidx++;
     131    11815585 :                         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    11815585 :                         } else if (argv[*optind][optchr] == opts[php_optidx].opt_char) {
     144     2181536 :                                 break;
     145             :                         }
     146     9634049 :                 }
     147             :         }
     148     2181584 :         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     2091967 :                 dash = 0;
     152     2091967 :                 if (!argv[*optind][arg_start]) {
     153     2091886 :                         (*optind)++;
     154     2091886 :                         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     2091885 :                         } else if (opts[php_optidx].need_param == 1) {
     161     2091883 :                                 *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     2091966 :                 return opts[php_optidx].opt_char;
     172             :         } else {
     173             :                 /* multiple options specified as one (exclude long opts) */
     174      179224 :                 if (arg_start >= 2 && !((argv[*optind][0] == '-') && (argv[*optind][1] == '-'))) {
     175       89607 :                         if (!argv[*optind][optchr+1])
     176             :                         {
     177       89591 :                                 dash = 0;
     178       89591 :                                 (*optind)++;
     179             :                         } else {
     180          16 :                                 optchr++;
     181             :                         }
     182             :                 } else {
     183          10 :                         (*optind)++;
     184             :                 }
     185       89617 :                 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 Tue, 22 Jul 2014 01:33:25 +0000 (10 days ago)

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