PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/getopt.php

https://github.com/fabriceb/symfttpd
PHP | 368 lines | 257 code | 36 blank | 75 comment | 98 complexity | 81050047425c5f6168e32222ae4b9794 MD5 | raw file
  1. <?php
  2. function _getopt ( ) {
  3. $er = error_reporting();
  4. error_reporting(0);
  5. /* _getopt(): Ver. 1.3 2009/05/30
  6. My page: http://www.ntu.beautifulworldco.com/weblog/?p=526
  7. Usage: _getopt ( [$flag,] $short_option [, $long_option] );
  8. Note that another function split_para() is required, which can be found in the same
  9. page.
  10. _getopt() fully simulates getopt() which is described at
  11. http://us.php.net/manual/en/function.getopt.php , including long options for PHP
  12. version under 5.3.0. (Prior to 5.3.0, long options was only available on few systems)
  13. Besides legacy usage of getopt(), I also added a new option to manipulate your own
  14. argument lists instead of those from command lines. This new option can be a string
  15. or an array such as
  16. $flag = "-f value_f -ab --required 9 --optional=PK --option -v test -k";
  17. or
  18. $flag = array ( "-f", "value_f", "-ab", "--required", "9", "--optional=PK", "--option" );
  19. So there are four ways to work with _getopt(),
  20. 1. _getopt ( $short_option );
  21. it's a legacy usage, same as getopt ( $short_option ).
  22. 2. _getopt ( $short_option, $long_option );
  23. it's a legacy usage, same as getopt ( $short_option, $long_option ).
  24. 3. _getopt ( $flag, $short_option );
  25. use your own argument lists instead of command line arguments.
  26. 4. _getopt ( $flag, $short_option, $long_option );
  27. use your own argument lists instead of command line arguments.
  28. */
  29. if ( func_num_args() == 1 ) {
  30. $flag = $flag_array = $GLOBALS['argv'];
  31. $short_option = func_get_arg ( 0 );
  32. $long_option = array ();
  33. } else if ( func_num_args() == 2 ) {
  34. if ( is_array ( func_get_arg ( 1 ) ) ) {
  35. $flag = $GLOBALS['argv'];
  36. $short_option = func_get_arg ( 0 );
  37. $long_option = func_get_arg ( 1 );
  38. } else {
  39. $flag = func_get_arg ( 0 );
  40. $short_option = func_get_arg ( 1 );
  41. $long_option = array ();
  42. }
  43. } else if ( func_num_args() == 3 ) {
  44. $flag = func_get_arg ( 0 );
  45. $short_option = func_get_arg ( 1 );
  46. $long_option = func_get_arg ( 2 );
  47. } else {
  48. exit ( "wrong options\n" );
  49. }
  50. $short_option = trim ( $short_option );
  51. $short_no_value = array();
  52. $short_required_value = array();
  53. $short_optional_value = array();
  54. $long_no_value = array();
  55. $long_required_value = array();
  56. $long_optional_value = array();
  57. $options = array();
  58. for ( $i = 0; $i < strlen ( $short_option ); ) {
  59. if ( $short_option{$i} != ":" ) {
  60. if ( $i == strlen ( $short_option ) - 1 ) {
  61. $short_no_value[] = $short_option{$i};
  62. break;
  63. } else if ( $short_option{$i+1} != ":" ) {
  64. $short_no_value[] = $short_option{$i};
  65. $i++;
  66. continue;
  67. } else if ( $short_option{$i+1} == ":" && $short_option{$i+2} != ":" ) {
  68. $short_required_value[] = $short_option{$i};
  69. $i += 2;
  70. continue;
  71. } else if ( $short_option{$i+1} == ":" && $short_option{$i+2} == ":" ) {
  72. $short_optional_value[] = $short_option{$i};
  73. $i += 3;
  74. continue;
  75. }
  76. } else {
  77. continue;
  78. }
  79. }
  80. foreach ( $long_option as $a ) {
  81. if ( substr( $a, -2 ) == "::" ) {
  82. $long_optional_value[] = substr( $a, 0, -2);
  83. continue;
  84. } else if ( substr( $a, -1 ) == ":" ) {
  85. $long_required_value[] = substr( $a, 0, -1 );
  86. continue;
  87. } else {
  88. $long_no_value[] = $a;
  89. continue;
  90. }
  91. }
  92. if ( is_array ( $flag ) )
  93. $flag_array = $flag;
  94. else {
  95. $flag = "- $flag";
  96. $flag_array = split_para( $flag );
  97. }
  98. for ( $i = 0; $i < count( $flag_array ); ) {
  99. if ( $i >= count ( $flag_array ) )
  100. break;
  101. if ( ! $flag_array[$i] || $flag_array[$i] == "-" ) {
  102. $i++;
  103. continue;
  104. }
  105. if ( $flag_array[$i]{0} != "-" ) {
  106. $i++;
  107. continue;
  108. }
  109. if ( substr( $flag_array[$i], 0, 2 ) == "--" ) {
  110. if (strpos($flag_array[$i], '=') != false) {
  111. list($key, $value) = explode('=', substr($flag_array[$i], 2), 2);
  112. if ( in_array ( $key, $long_required_value ) || in_array ( $key, $long_optional_value ) )
  113. $options[$key][] = $value;
  114. $i++;
  115. continue;
  116. }
  117. if (strpos($flag_array[$i], '=') == false) {
  118. $key = substr( $flag_array[$i], 2 );
  119. if ( in_array( substr( $flag_array[$i], 2 ), $long_required_value ) ) {
  120. $options[$key][] = $flag_array[$i+1];
  121. $i += 2;
  122. continue;
  123. } else if ( in_array( substr( $flag_array[$i], 2 ), $long_optional_value ) ) {
  124. if ( $flag_array[$i+1] != "" && $flag_array[$i+1]{0} != "-" ) {
  125. $options[$key][] = $flag_array[$i+1];
  126. $i += 2;
  127. } else {
  128. $options[$key][] = FALSE;
  129. $i ++;
  130. }
  131. continue;
  132. } else if ( in_array( substr( $flag_array[$i], 2 ), $long_no_value ) ) {
  133. $options[$key][] = FALSE;
  134. $i++;
  135. continue;
  136. } else {
  137. $i++;
  138. continue;
  139. }
  140. }
  141. } else if ( $flag_array[$i]{0} == "-" && $flag_array[$i]{1} != "-" ) {
  142. for ( $j=1; $j < strlen($flag_array[$i]); $j++ ) {
  143. if ( in_array( $flag_array[$i]{$j}, $short_required_value ) || in_array( $flag_array[$i]{$j}, $short_optional_value )) {
  144. if ( $j == strlen($flag_array[$i]) - 1 ) {
  145. if ( in_array( $flag_array[$i]{$j}, $short_required_value ) ) {
  146. $options[$flag_array[$i]{$j}][] = $flag_array[$i+1];
  147. $i += 2;
  148. } else if ( in_array( $flag_array[$i]{$j}, $short_optional_value ) && $flag_array[$i+1] != "" && $flag_array[$i+1]{0} != "-" ) {
  149. $options[$flag_array[$i]{$j}][] = $flag_array[$i+1];
  150. $i += 2;
  151. } else {
  152. $options[$flag_array[$i]{$j}][] = FALSE;
  153. $i ++;
  154. }
  155. $plus_i = 0;
  156. break;
  157. } else {
  158. $options[$flag_array[$i]{$j}][] = substr ( $flag_array[$i], $j + 1 );
  159. $i ++;
  160. $plus_i = 0;
  161. break;
  162. }
  163. } else if ( in_array ( $flag_array[$i]{$j}, $short_no_value ) ) {
  164. $options[$flag_array[$i]{$j}][] = FALSE;
  165. $plus_i = 1;
  166. continue;
  167. } else {
  168. $plus_i = 1;
  169. break;
  170. }
  171. }
  172. $i += $plus_i;
  173. continue;
  174. }
  175. $i++;
  176. continue;
  177. }
  178. foreach ( $options as $key => $value ) {
  179. if ( count ( $value ) == 1 ) {
  180. $options[ $key ] = $value[0];
  181. }
  182. }
  183. return $options;
  184. error_reporting($er);
  185. }
  186. function split_para ( $pattern ) {
  187. /* split_para() version 1.0 2008/08/19
  188. My page: http://www.ntu.beautifulworldco.com/weblog/?p=526
  189. This function is to parse parameters and split them into smaller pieces.
  190. preg_split() does similar thing but in our function, besides "space", we
  191. also take the three symbols " (double quote), '(single quote),
  192. and \ (backslash) into consideration because things in a pair of " or '
  193. should be grouped together.
  194. As an example, this parameter list
  195. -f "test 2" -ab --required "t\"est 1" --optional="te'st 3" --option -v 'test 4'
  196. will be splited into
  197. -f
  198. t"est 2
  199. -ab
  200. --required
  201. test 1
  202. --optional=te'st 3
  203. --option
  204. -v
  205. test 4
  206. see the code below,
  207. $pattern = "-f \"test 2\" -ab --required \"t\\\"est 1\" --optional=\"te'st 3\" --option -v 'test 4'";
  208. $result = split_para( $pattern );
  209. echo "ORIGINAL PATTERN: $pattern\n\n";
  210. var_dump( $result );
  211. */
  212. $begin=0;
  213. $backslash = 0;
  214. $quote = "";
  215. $quote_mark = array();
  216. $result = array();
  217. $pattern = trim ( $pattern );
  218. for ( $end = 0; $end < strlen ( $pattern ) ; ) {
  219. if ( ! in_array ( $pattern{$end}, array ( " ", "\"", "'", "\\" ) ) ) {
  220. $backslash = 0;
  221. $end ++;
  222. continue;
  223. }
  224. if ( $pattern{$end} == "\\" ) {
  225. $backslash++;
  226. $end ++;
  227. continue;
  228. } else if ( $pattern{$end} == "\"" ) {
  229. if ( $backslash % 2 == 1 || $quote == "'" ) {
  230. $backslash = 0;
  231. $end ++;
  232. continue;
  233. }
  234. if ( $quote == "" ) {
  235. $quote_mark[] = $end - $begin;
  236. $quote = "\"";
  237. } else if ( $quote == "\"" ) {
  238. $quote_mark[] = $end - $begin;
  239. $quote = "";
  240. }
  241. $backslash = 0;
  242. $end ++;
  243. continue;
  244. } else if ( $pattern{$end} == "'" ) {
  245. if ( $backslash % 2 == 1 || $quote == "\"" ) {
  246. $backslash = 0;
  247. $end ++;
  248. continue;
  249. }
  250. if ( $quote == "" ) {
  251. $quote_mark[] = $end - $begin;
  252. $quote = "'";
  253. } else if ( $quote == "'" ) {
  254. $quote_mark[] = $end - $begin;
  255. $quote = "";
  256. }
  257. $backslash = 0;
  258. $end ++;
  259. continue;
  260. } else if ( $pattern{$end} == " " ) {
  261. if ( $quote != "" ) {
  262. $backslash = 0;
  263. $end ++;
  264. continue;
  265. } else {
  266. $backslash = 0;
  267. $cand = substr( $pattern, $begin, $end-$begin );
  268. for ( $j = 0; $j < strlen ( $cand ); $j ++ ) {
  269. if ( in_array ( $j, $quote_mark ) )
  270. continue;
  271. $cand1 .= $cand{$j};
  272. }
  273. if ( $cand1 ) {
  274. eval( "\$cand1 = \"$cand1\";" );
  275. $result[] = $cand1;
  276. }
  277. $quote_mark = array();
  278. $cand1 = "";
  279. $end ++;
  280. $begin = $end;
  281. continue;
  282. }
  283. }
  284. }
  285. $cand = substr( $pattern, $begin, $end-$begin );
  286. for ( $j = 0; $j < strlen ( $cand ); $j ++ ) {
  287. if ( in_array ( $j, $quote_mark ) )
  288. continue;
  289. $cand1 .= $cand{$j};
  290. }
  291. eval( "\$cand1 = \"$cand1\";" );
  292. if ( $cand1 )
  293. $result[] = $cand1;
  294. return $result;
  295. }