├── .gitignore ├── COPYING ├── Makefile ├── README.md ├── argp-ba.c ├── argp-eexist.c ├── argp-fmtstream.c ├── argp-fmtstream.h ├── argp-help.c ├── argp-parse.c ├── argp-pv.c ├── argp-pvh.c ├── argp-shims.c ├── argp-shims.h ├── argp.h ├── install.sh └── internal.h /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.lo 3 | *.a 4 | *.so 5 | *~ 6 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | 504 | 505 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | bindir=$(prefix)/bin 3 | includedir=$(prefix)/include 4 | libdir=$(prefix)/lib 5 | 6 | SRC = $(sort $(wildcard *.c)) 7 | OBJS = $(SRC:.c=.o) 8 | 9 | SHLIB_EXT=.so 10 | ALL_INCLUDES=argp.h 11 | ALL_LIBS=libargp$(SHLIB_EXT) libargp.a 12 | 13 | CFLAGS=-O0 -fPIC -Wall 14 | BUILDCFLAGS=$(CFLAGS) -I. 15 | LDFLAGS= 16 | 17 | AR ?= $(CROSS_COMPILE)ar 18 | RANLIB ?= $(CROSS_COMPILE)ranlib 19 | CC ?= $(CROSS_COMPILE)cc 20 | 21 | INSTALL ?= ./install.sh 22 | 23 | all: $(ALL_LIBS) 24 | 25 | install: $(ALL_LIBS:lib%=$(DESTDIR)$(libdir)/lib%) $(ALL_INCLUDES:%=$(DESTDIR)$(includedir)/%) 26 | 27 | clean: 28 | rm -f $(ALL_LIBS) 29 | rm -f $(OBJS) 30 | rm -f *.o *.so 31 | 32 | %.o: %.c 33 | $(CC) $(BUILDCFLAGS) -c -o $@ $< 34 | 35 | libargp.a: $(OBJS) 36 | rm -f $@ 37 | $(AR) rc $@ $(OBJS) 38 | $(RANLIB) $@ 39 | 40 | libargp$(SHLIB_EXT): $(OBJS) 41 | rm -f $@ 42 | $(CC) $(LDFLAGS) -shared -o $@ $(OBJS) $(LDFLAGS) 43 | 44 | $(DESTDIR)$(libdir)/%: % 45 | $(INSTALL) -D -m 755 $< $@ 46 | 47 | $(DESTDIR)$(includedir)/%.h: %.h 48 | $(INSTALL) -D -m 644 $< $@ 49 | 50 | .PHONY: all clean install 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libuargp 2 | 3 | extracted from (uclibc-ng)[https://github.com/wbx-github/uclibc-ng]. 4 | 5 | what happened to musl's intl & gnu's argp: 6 | 7 | + https://www.openwall.com/lists/musl/2017/03/24/10 8 | 9 | + http://lists.gnu.org/archive/html/bug-gnulib/2019-01/msg00046.html 10 | 11 | + https://github.com/coreutils/gnulib/commit/ff3fe55c7c95bdb9460a40747912b4b309519838#diff-e8acc63b1e238f3255c900eed37254b8 12 | 13 | + https://github.com/coreutils/gnulib/commit/5f985580b9473b951eff13d92db613d464884091#diff-e8acc63b1e238f3255c900eed37254b8 14 | 15 | **currently, gnu has fixed this bug.** 16 | 17 | so this project is to provide another choice than the old argp-standalone. 18 | 19 | # usage 20 | 21 | remove all files, and link the modified version: 22 | 23 | ``` 24 | truncate -s 0 gnulib/lib/argp* 25 | ln -sf /usr/include/argp.h gnulib/lib/argp.h 26 | LDFLAGS="$LDFLAGS -l:libargp.so.0" 27 | ``` 28 | 29 | or just overwrite, since argp symbols from gnulib are weak symbols: 30 | 31 | ``` 32 | CFLAGS="$CFLAGS -l:libargp.so.0" 33 | ``` 34 | -------------------------------------------------------------------------------- /argp-ba.c: -------------------------------------------------------------------------------- 1 | /* Default definition for ARGP_PROGRAM_BUG_ADDRESS. 2 | Copyright (C) 1996, 1997, 1999, 2004 Free Software Foundation, Inc. 3 | This file is part of the GNU C Library. 4 | Written by Miles Bader . 5 | The GNU C Library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public License as 7 | published by the Free Software Foundation; either version 2 of the 8 | License, or (at your option) any later version. 9 | The GNU C Library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | Library General Public License for more details. 13 | You should have received a copy of the GNU Library General Public 14 | License along with the GNU C Library; see the file COPYING.LIB. If not, 15 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 | Boston, MA 02111-1307, USA. */ 17 | 18 | /* If set by the user program, it should point to string that is the 19 | bug-reporting address for the program. It will be printed by argp_help if 20 | the ARGP_HELP_BUG_ADDR flag is set (as it is by various standard help 21 | messages), embedded in a sentence that says something like `Report bugs to 22 | ADDR.'. */ 23 | const char *argp_program_bug_address = 0; 24 | -------------------------------------------------------------------------------- /argp-eexist.c: -------------------------------------------------------------------------------- 1 | /* Default definition for ARGP_PROGRAM_VERSION_HOOK. 2 | Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc. 3 | This file is part of the GNU C Library. 4 | Written by Miles Bader . 5 | 6 | The GNU C Library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | The GNU C Library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with the GNU C Library; see the file COPYING.LIB. If 18 | not, see .  */ 19 | 20 | #ifndef _WIN32 21 | #include 22 | #else 23 | #define EX_USAGE 64 24 | #endif 25 | #include 26 | 27 | /* The exit status that argp will use when exiting due to a parsing error. 28 | If not defined or set by the user program, this defaults to EX_USAGE from 29 | . */ 30 | int argp_err_exit_status = EX_USAGE; 31 | -------------------------------------------------------------------------------- /argp-fmtstream.c: -------------------------------------------------------------------------------- 1 | /* Word-wrapping and line-truncating streams 2 | Copyright (C) 1997-1999,2001,2002,2003,2005 Free Software Foundation, Inc. 3 | This file is part of the GNU C Library. 4 | Written by Miles Bader . 5 | 6 | The GNU C Library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | The GNU C Library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with the GNU C Library; see the file COPYING.LIB. If 18 | not, see . 19 | 20 | Modified for uClibc by: Salvatore Cro 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include "argp-fmtstream.h" 29 | #include "argp-shims.h" 30 | 31 | #define INIT_BUF_SIZE 200 32 | #define PRINTF_SIZE_GUESS 150 33 | 34 | static void __argp_fmtstream_update (argp_fmtstream_t fs); 35 | 36 | /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines 37 | written on it with LMARGIN spaces and limits them to RMARGIN columns 38 | total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by 39 | replacing the whitespace before them with a newline and WMARGIN spaces. 40 | Otherwise, chars beyond RMARGIN are simply dropped until a newline. 41 | Returns NULL if there was an error. */ 42 | argp_fmtstream_t 43 | argp_make_fmtstream (FILE *stream, 44 | size_t lmargin, size_t rmargin, ssize_t wmargin) 45 | { 46 | argp_fmtstream_t fs; 47 | 48 | fs = (struct argp_fmtstream *) malloc (sizeof (struct argp_fmtstream)); 49 | if (fs != NULL) 50 | { 51 | fs->stream = stream; 52 | 53 | fs->lmargin = lmargin; 54 | fs->rmargin = rmargin; 55 | fs->wmargin = wmargin; 56 | fs->point_col = 0; 57 | fs->point_offs = 0; 58 | 59 | fs->buf = (char *) malloc (INIT_BUF_SIZE); 60 | if (! fs->buf) 61 | { 62 | free (fs); 63 | fs = 0; 64 | } 65 | else 66 | { 67 | fs->p = fs->buf; 68 | fs->end = fs->buf + INIT_BUF_SIZE; 69 | } 70 | } 71 | 72 | return fs; 73 | } 74 | 75 | /* Flush FS to its stream, and free it (but don't close the stream). */ 76 | void 77 | argp_fmtstream_free (argp_fmtstream_t fs) 78 | { 79 | __argp_fmtstream_update (fs); 80 | if (fs->p > fs->buf) 81 | { 82 | fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream); 83 | } 84 | free (fs->buf); 85 | free (fs); 86 | } 87 | 88 | /* Process FS's buffer so that line wrapping is done from POINT_OFFS to the 89 | end of its buffer. This code is mostly from glibc stdio/linewrap.c. */ 90 | static void 91 | __argp_fmtstream_update (argp_fmtstream_t fs) 92 | { 93 | char *buf, *nl; 94 | size_t len; 95 | 96 | /* Scan the buffer for newlines. */ 97 | buf = fs->buf + fs->point_offs; 98 | while (buf < fs->p) 99 | { 100 | size_t r; 101 | 102 | if (fs->point_col == 0 && fs->lmargin != 0) 103 | { 104 | /* We are starting a new line. Print spaces to the left margin. */ 105 | const size_t pad = fs->lmargin; 106 | if (fs->p + pad < fs->end) 107 | { 108 | /* We can fit in them in the buffer by moving the 109 | buffer text up and filling in the beginning. */ 110 | memmove (buf + pad, buf, fs->p - buf); 111 | fs->p += pad; /* Compensate for bigger buffer. */ 112 | memset (buf, ' ', pad); /* Fill in the spaces. */ 113 | buf += pad; /* Don't bother searching them. */ 114 | } 115 | else 116 | { 117 | /* No buffer space for spaces. Must flush. */ 118 | size_t i; 119 | for (i = 0; i < pad; i++) 120 | { 121 | putc_unlocked (' ', fs->stream); 122 | } 123 | } 124 | fs->point_col = pad; 125 | } 126 | 127 | len = fs->p - buf; 128 | nl = memchr (buf, '\n', len); 129 | 130 | if (fs->point_col < 0) 131 | fs->point_col = 0; 132 | 133 | if (!nl) 134 | { 135 | /* The buffer ends in a partial line. */ 136 | 137 | if (fs->point_col + len < fs->rmargin) 138 | { 139 | /* The remaining buffer text is a partial line and fits 140 | within the maximum line width. Advance point for the 141 | characters to be written and stop scanning. */ 142 | fs->point_col += len; 143 | break; 144 | } 145 | else 146 | /* Set the end-of-line pointer for the code below to 147 | the end of the buffer. */ 148 | nl = fs->p; 149 | } 150 | else if (fs->point_col + (nl - buf) < (ssize_t) fs->rmargin) 151 | { 152 | /* The buffer contains a full line that fits within the maximum 153 | line width. Reset point and scan the next line. */ 154 | fs->point_col = 0; 155 | buf = nl + 1; 156 | continue; 157 | } 158 | 159 | /* This line is too long. */ 160 | r = fs->rmargin - 1; 161 | 162 | if (fs->wmargin < 0) 163 | { 164 | /* Truncate the line by overwriting the excess with the 165 | newline and anything after it in the buffer. */ 166 | if (nl < fs->p) 167 | { 168 | memmove (buf + (r - fs->point_col), nl, fs->p - nl); 169 | fs->p -= buf + (r - fs->point_col) - nl; 170 | /* Reset point for the next line and start scanning it. */ 171 | fs->point_col = 0; 172 | buf += r + 1; /* Skip full line plus \n. */ 173 | } 174 | else 175 | { 176 | /* The buffer ends with a partial line that is beyond the 177 | maximum line width. Advance point for the characters 178 | written, and discard those past the max from the buffer. */ 179 | fs->point_col += len; 180 | fs->p -= fs->point_col - r; 181 | break; 182 | } 183 | } 184 | else 185 | { 186 | /* Do word wrap. Go to the column just past the maximum line 187 | width and scan back for the beginning of the word there. 188 | Then insert a line break. */ 189 | 190 | char *p, *nextline; 191 | int i; 192 | 193 | p = buf + (r + 1 - fs->point_col); 194 | while (p >= buf && !isblank (*p)) 195 | --p; 196 | nextline = p + 1; /* This will begin the next line. */ 197 | 198 | if (nextline > buf) 199 | { 200 | /* Swallow separating blanks. */ 201 | if (p >= buf) 202 | do 203 | --p; 204 | while (p >= buf && isblank (*p)); 205 | nl = p + 1; /* The newline will replace the first blank. */ 206 | } 207 | else 208 | { 209 | /* A single word that is greater than the maximum line width. 210 | Oh well. Put it on an overlong line by itself. */ 211 | p = buf + (r + 1 - fs->point_col); 212 | /* Find the end of the long word. */ 213 | do 214 | ++p; 215 | while (p < nl && !isblank (*p)); 216 | if (p == nl) 217 | { 218 | /* It already ends a line. No fussing required. */ 219 | fs->point_col = 0; 220 | buf = nl + 1; 221 | continue; 222 | } 223 | /* We will move the newline to replace the first blank. */ 224 | nl = p; 225 | /* Swallow separating blanks. */ 226 | do 227 | ++p; 228 | while (isblank (*p)); 229 | /* The next line will start here. */ 230 | nextline = p; 231 | } 232 | 233 | /* Note: There are a bunch of tests below for 234 | NEXTLINE == BUF + LEN + 1; this case is where NL happens to fall 235 | at the end of the buffer, and NEXTLINE is in fact empty (and so 236 | we need not be careful to maintain its contents). */ 237 | 238 | if ((nextline == buf + len + 1 239 | ? fs->end - nl < fs->wmargin + 1 240 | : nextline - (nl + 1) < fs->wmargin) 241 | && fs->p > nextline) 242 | { 243 | /* The margin needs more blanks than we removed. */ 244 | if (fs->end - fs->p > fs->wmargin + 1) 245 | /* Make some space for them. */ 246 | { 247 | size_t mv = fs->p - nextline; 248 | memmove (nl + 1 + fs->wmargin, nextline, mv); 249 | nextline = nl + 1 + fs->wmargin; 250 | len = nextline + mv - buf; 251 | *nl++ = '\n'; 252 | } 253 | else 254 | /* Output the first line so we can use the space. */ 255 | { 256 | if (nl > fs->buf) 257 | fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream); 258 | putc_unlocked ('\n', fs->stream); 259 | 260 | len += buf - fs->buf; 261 | nl = buf = fs->buf; 262 | } 263 | } 264 | else 265 | /* We can fit the newline and blanks in before 266 | the next word. */ 267 | *nl++ = '\n'; 268 | 269 | if (nextline - nl >= fs->wmargin 270 | || (nextline == buf + len + 1 && fs->end - nextline >= fs->wmargin)) 271 | /* Add blanks up to the wrap margin column. */ 272 | for (i = 0; i < fs->wmargin; ++i) 273 | *nl++ = ' '; 274 | else 275 | for (i = 0; i < fs->wmargin; ++i) 276 | putc_unlocked (' ', fs->stream); 277 | 278 | /* Copy the tail of the original buffer into the current buffer 279 | position. */ 280 | if (nl < nextline) 281 | memmove (nl, nextline, buf + len - nextline); 282 | len -= nextline - buf; 283 | 284 | /* Continue the scan on the remaining lines in the buffer. */ 285 | buf = nl; 286 | 287 | /* Restore bufp to include all the remaining text. */ 288 | fs->p = nl + len; 289 | 290 | /* Reset the counter of what has been output this line. If wmargin 291 | is 0, we want to avoid the lmargin getting added, so we set 292 | point_col to a magic value of -1 in that case. */ 293 | fs->point_col = fs->wmargin ? fs->wmargin : -1; 294 | } 295 | } 296 | 297 | /* Remember that we've scanned as far as the end of the buffer. */ 298 | fs->point_offs = fs->p - fs->buf; 299 | } 300 | 301 | /* Ensure that FS has space for AMOUNT more bytes in its buffer, either by 302 | growing the buffer, or by flushing it. True is returned iff we succeed. */ 303 | int 304 | argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount) 305 | { 306 | if ((size_t) (fs->end - fs->p) < amount) 307 | { 308 | ssize_t wrote; 309 | 310 | /* Flush FS's buffer. */ 311 | __argp_fmtstream_update (fs); 312 | 313 | wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream); 314 | if (wrote == fs->p - fs->buf) 315 | { 316 | fs->p = fs->buf; 317 | fs->point_offs = 0; 318 | } 319 | else 320 | { 321 | fs->p -= wrote; 322 | fs->point_offs -= wrote; 323 | memmove (fs->buf, fs->buf + wrote, fs->p - fs->buf); 324 | return 0; 325 | } 326 | 327 | if ((size_t) (fs->end - fs->buf) < amount) 328 | /* Gotta grow the buffer. */ 329 | { 330 | size_t old_size = fs->end - fs->buf; 331 | size_t new_size = old_size + amount; 332 | char *new_buf; 333 | 334 | if (new_size < old_size || ! (new_buf = realloc (fs->buf, new_size))) 335 | { 336 | errno = ENOMEM; 337 | return 0; 338 | } 339 | 340 | fs->buf = new_buf; 341 | fs->end = new_buf + new_size; 342 | fs->p = fs->buf; 343 | } 344 | } 345 | 346 | return 1; 347 | } 348 | 349 | ssize_t 350 | argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...) 351 | { 352 | int out; 353 | size_t avail; 354 | size_t size_guess = PRINTF_SIZE_GUESS; /* How much space to reserve. */ 355 | 356 | do 357 | { 358 | va_list args; 359 | 360 | if (! argp_fmtstream_ensure (fs, size_guess)) 361 | return -1; 362 | 363 | va_start (args, fmt); 364 | avail = fs->end - fs->p; 365 | out = vsnprintf (fs->p, avail, fmt, args); 366 | va_end (args); 367 | if ((size_t) out >= avail) 368 | size_guess = out + 1; 369 | } 370 | while ((size_t) out >= avail); 371 | 372 | fs->p += out; 373 | 374 | return out; 375 | } 376 | 377 | size_t 378 | argp_fmtstream_write (argp_fmtstream_t __fs, 379 | const char *__str, size_t __len) 380 | { 381 | if (__fs->p + __len <= __fs->end || argp_fmtstream_ensure (__fs, __len)) 382 | { 383 | memcpy (__fs->p, __str, __len); 384 | __fs->p += __len; 385 | return __len; 386 | } 387 | else 388 | return 0; 389 | } 390 | 391 | int 392 | argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str) 393 | { 394 | size_t __len = strlen (__str); 395 | if (__len) 396 | { 397 | size_t __wrote = argp_fmtstream_write (__fs, __str, __len); 398 | return __wrote == __len ? 0 : -1; 399 | } 400 | else 401 | return 0; 402 | } 403 | 404 | int 405 | argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch) 406 | { 407 | if (__fs->p < __fs->end || argp_fmtstream_ensure (__fs, 1)) 408 | return *__fs->p++ = __ch; 409 | else 410 | return EOF; 411 | } 412 | 413 | /* Set __FS's left margin to __LMARGIN and return the old value. */ 414 | size_t 415 | argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin) 416 | { 417 | size_t __old; 418 | if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs) 419 | __argp_fmtstream_update (__fs); 420 | __old = __fs->lmargin; 421 | __fs->lmargin = __lmargin; 422 | return __old; 423 | } 424 | 425 | /* Set __FS's right margin to __RMARGIN and return the old value. */ 426 | size_t 427 | argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin) 428 | { 429 | size_t __old; 430 | if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs) 431 | __argp_fmtstream_update (__fs); 432 | __old = __fs->rmargin; 433 | __fs->rmargin = __rmargin; 434 | return __old; 435 | } 436 | 437 | /* Set FS's wrap margin to __WMARGIN and return the old value. */ 438 | size_t 439 | argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin) 440 | { 441 | size_t __old; 442 | if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs) 443 | __argp_fmtstream_update (__fs); 444 | __old = __fs->wmargin; 445 | __fs->wmargin = __wmargin; 446 | return __old; 447 | } 448 | 449 | /* Return the column number of the current output point in __FS. */ 450 | size_t 451 | argp_fmtstream_point (argp_fmtstream_t __fs) 452 | { 453 | if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs) 454 | __argp_fmtstream_update (__fs); 455 | return __fs->point_col >= 0 ? __fs->point_col : 0; 456 | } 457 | -------------------------------------------------------------------------------- /argp-fmtstream.h: -------------------------------------------------------------------------------- 1 | /* Word-wrapping and line-truncating streams. 2 | Copyright (C) 1997 Free Software Foundation, Inc. 3 | This file is part of the GNU C Library. 4 | Written by Miles Bader . 5 | 6 | The GNU C Library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | The GNU C Library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with the GNU C Library; see the file COPYING.LIB. If 18 | not, see . 19 | 20 | Modified for uClibc by: Salvatore Cro 21 | */ 22 | 23 | /* This package emulates glibc `line_wrap_stream' semantics for systems that 24 | don't have that. If the system does have it, it is just a wrapper for 25 | that. This header file is only used internally while compiling argp, and 26 | shouldn't be installed. */ 27 | 28 | #ifndef _ARGP_FMTSTREAM_H 29 | #define _ARGP_FMTSTREAM_H 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #ifndef __attribute__ 36 | /* This feature is available in gcc versions 2.5 and later. */ 37 | # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || defined(__STRICT_ANSI__) 38 | # define __attribute__(Spec) /* empty */ 39 | # endif 40 | /* The __-protected variants of `format' and `printf' attributes 41 | are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */ 42 | # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || defined(__STRICT_ANSI__) 43 | # define __format__ format 44 | # define __printf__ printf 45 | # endif 46 | #endif 47 | 48 | struct argp_fmtstream 49 | { 50 | FILE *stream; /* The stream we're outputting to. */ 51 | 52 | size_t lmargin, rmargin; /* Left and right margins. */ 53 | ssize_t wmargin; /* Margin to wrap to, or -1 to truncate. */ 54 | 55 | /* Point in buffer to which we've processed for wrapping, but not output. */ 56 | size_t point_offs; 57 | /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */ 58 | ssize_t point_col; 59 | 60 | char *buf; /* Output buffer. */ 61 | char *p; /* Current end of text in BUF. */ 62 | char *end; /* Absolute end of BUF. */ 63 | }; 64 | 65 | typedef struct argp_fmtstream *argp_fmtstream_t; 66 | 67 | /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines 68 | written on it with LMARGIN spaces and limits them to RMARGIN columns 69 | total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by 70 | replacing the whitespace before them with a newline and WMARGIN spaces. 71 | Otherwise, chars beyond RMARGIN are simply dropped until a newline. 72 | Returns NULL if there was an error. */ 73 | extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream, 74 | size_t __lmargin, 75 | size_t __rmargin, 76 | ssize_t __wmargin); 77 | 78 | /* Flush __FS to its stream, and free it (but don't close the stream). */ 79 | extern void argp_fmtstream_free (argp_fmtstream_t __fs); 80 | 81 | extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs, 82 | const char *__fmt, ...) 83 | __attribute__ ((__format__ (printf, 2, 3))); 84 | 85 | extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch); 86 | 87 | extern int argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str); 88 | 89 | extern size_t argp_fmtstream_write (argp_fmtstream_t __fs, 90 | const char *__str, size_t __len); 91 | 92 | /* Access macros for various bits of state. */ 93 | #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin) 94 | #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin) 95 | #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin) 96 | 97 | /* Set __FS's left margin to LMARGIN and return the old value. */ 98 | extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, 99 | size_t __lmargin); 100 | 101 | /* Set __FS's right margin to __RMARGIN and return the old value. */ 102 | extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, 103 | size_t __rmargin); 104 | 105 | /* Set __FS's wrap margin to __WMARGIN and return the old value. */ 106 | extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, 107 | size_t __wmargin); 108 | 109 | /* Return the column number of the current output point in __FS. */ 110 | extern size_t argp_fmtstream_point (argp_fmtstream_t __fs); 111 | 112 | #endif 113 | -------------------------------------------------------------------------------- /argp-help.c: -------------------------------------------------------------------------------- 1 | /* Hierarchial argument parsing help output 2 | Copyright (C) 1995-2003, 2004, 2005, 2006, 2007 3 | Free Software Foundation, Inc. 4 | This file is part of the GNU C Library. 5 | Written by Miles Bader . 6 | 7 | The GNU C Library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | The GNU C Library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with the GNU C Library; see the file COPYING.LIB. If 19 | not, see . 20 | 21 | Modified for uClibc by: Salvatore Cro 22 | */ 23 | 24 | #ifndef _GNU_SOURCE 25 | #define _GNU_SOURCE 26 | #endif 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #ifndef _WIN32 36 | #include 37 | #include 38 | #endif 39 | #include 40 | #include 41 | #include 42 | #include "argp-fmtstream.h" 43 | #include "argp-shims.h" 44 | #include "internal.h" 45 | #include 46 | #include 47 | 48 | 49 | /* Return the input field for ARGP in the parser corresponding to STATE; used 50 | by the help routines. */ 51 | void * 52 | argp_input (const struct argp *argp, const struct argp_state *state) 53 | { 54 | if (state) 55 | { 56 | struct group *group; 57 | struct parser *parser = state->pstate; 58 | 59 | for (group = parser->groups; group < parser->egroup; group++) 60 | if (group->argp == argp) 61 | return group->input; 62 | } 63 | 64 | return 0; 65 | } 66 | 67 | #undef dgettext 68 | #define dgettext(domain, msgid)(msgid ? dcgettext(domain, msgid, LC_MESSAGES) : NULL) 69 | 70 | /* User-selectable (using an environment variable) formatting parameters. 71 | 72 | These may be specified in an environment variable called `ARGP_HELP_FMT', 73 | with a contents like: VAR1=VAL1,VAR2=VAL2,BOOLVAR2,no-BOOLVAR2 74 | Where VALn must be a positive integer. The list of variables is in the 75 | UPARAM_NAMES vector, below. */ 76 | 77 | /* Default parameters. */ 78 | #define DUP_ARGS 0 /* True if option argument can be duplicated. */ 79 | #define DUP_ARGS_NOTE 1 /* True to print a note about duplicate args. */ 80 | #define SHORT_OPT_COL 2 /* column in which short options start */ 81 | #define LONG_OPT_COL 6 /* column in which long options start */ 82 | #define DOC_OPT_COL 2 /* column in which doc options start */ 83 | #define OPT_DOC_COL 29 /* column in which option text starts */ 84 | #define HEADER_COL 1 /* column in which group headers are printed */ 85 | #define USAGE_INDENT 12 /* indentation of wrapped usage lines */ 86 | #define RMARGIN 79 /* right margin used for wrapping */ 87 | 88 | /* User-selectable (using an environment variable) formatting parameters. 89 | They must all be of type `int' for the parsing code to work. */ 90 | struct uparams 91 | { 92 | /* If true, arguments for an option are shown with both short and long 93 | options, even when a given option has both, e.g. `-x ARG, --longx=ARG'. 94 | If false, then if an option has both, the argument is only shown with 95 | the long one, e.g., `-x, --longx=ARG', and a message indicating that 96 | this really means both is printed below the options. */ 97 | int dup_args; 98 | 99 | /* This is true if when DUP_ARGS is false, and some duplicate arguments have 100 | been suppressed, an explanatory message should be printed. */ 101 | int dup_args_note; 102 | 103 | /* Various output columns. */ 104 | int short_opt_col; 105 | int long_opt_col; 106 | int doc_opt_col; 107 | int opt_doc_col; 108 | int header_col; 109 | int usage_indent; 110 | int rmargin; 111 | }; 112 | 113 | /* This is a global variable, as user options are only ever read once. */ 114 | static struct uparams uparams = { 115 | DUP_ARGS, DUP_ARGS_NOTE, 116 | SHORT_OPT_COL, LONG_OPT_COL, DOC_OPT_COL, OPT_DOC_COL, HEADER_COL, 117 | USAGE_INDENT, RMARGIN 118 | }; 119 | 120 | /* A particular uparam, and what the user name is. */ 121 | struct uparam_name 122 | { 123 | const char name[14]; /* User name. */ 124 | bool is_bool; /* Whether it's `boolean'. */ 125 | uint8_t uparams_offs; /* Location of the (int) field in UPARAMS. */ 126 | }; 127 | 128 | /* The name-field mappings we know about. */ 129 | static const struct uparam_name uparam_names[] = 130 | { 131 | { "dup-args", true, offsetof (struct uparams, dup_args) }, 132 | { "dup-args-note", true, offsetof (struct uparams, dup_args_note) }, 133 | { "short-opt-col", false, offsetof (struct uparams, short_opt_col) }, 134 | { "long-opt-col", false, offsetof (struct uparams, long_opt_col) }, 135 | { "doc-opt-col", false, offsetof (struct uparams, doc_opt_col) }, 136 | { "opt-doc-col", false, offsetof (struct uparams, opt_doc_col) }, 137 | { "header-col", false, offsetof (struct uparams, header_col) }, 138 | { "usage-indent", false, offsetof (struct uparams, usage_indent) }, 139 | { "rmargin", false, offsetof (struct uparams, rmargin) } 140 | }; 141 | #define nuparam_names (sizeof (uparam_names) / sizeof (uparam_names[0])) 142 | 143 | /* Read user options from the environment, and fill in UPARAMS appropiately. */ 144 | static void 145 | fill_in_uparams (const struct argp_state *state) 146 | { 147 | const char *var = getenv ("ARGP_HELP_FMT"); 148 | 149 | #define SKIPWS(p) do { while (isspace (*p)) p++; } while (0); 150 | 151 | if (var) 152 | /* Parse var. */ 153 | while (*var) 154 | { 155 | SKIPWS (var); 156 | 157 | if (isalpha (*var)) 158 | { 159 | size_t var_len; 160 | const struct uparam_name *un; 161 | int unspec = 0, val = 0; 162 | const char *arg = var; 163 | 164 | while (isalnum (*arg) || *arg == '-' || *arg == '_') 165 | arg++; 166 | var_len = arg - var; 167 | 168 | SKIPWS (arg); 169 | 170 | if (*arg == '\0' || *arg == ',') 171 | unspec = 1; 172 | else if (*arg == '=') 173 | { 174 | arg++; 175 | SKIPWS (arg); 176 | } 177 | 178 | if (unspec) 179 | { 180 | if (var[0] == 'n' && var[1] == 'o' && var[2] == '-') 181 | { 182 | val = 0; 183 | var += 3; 184 | var_len -= 3; 185 | } 186 | else 187 | val = 1; 188 | } 189 | else if (isdigit (*arg)) 190 | { 191 | val = atoi (arg); 192 | while (isdigit (*arg)) 193 | arg++; 194 | SKIPWS (arg); 195 | } 196 | 197 | un = uparam_names; 198 | size_t u; 199 | for (u = 0; u < nuparam_names; ++un, ++u) 200 | if (strlen (un->name) == var_len 201 | && strncmp (var, un->name, var_len) == 0) 202 | { 203 | if (unspec && !un->is_bool) 204 | argp_failure (state, 0, 0, 205 | dgettext (state == NULL ? NULL 206 | : state->root_argp->argp_domain, 207 | "\ 208 | %.*s: ARGP_HELP_FMT parameter requires a value"), 209 | (int) var_len, var); 210 | else 211 | *(int *)((char *)&uparams + un->uparams_offs) = val; 212 | break; 213 | } 214 | if (u == nuparam_names) 215 | argp_failure (state, 0, 0, 216 | dgettext (state == NULL ? NULL 217 | : state->root_argp->argp_domain, "\ 218 | %.*s: Unknown ARGP_HELP_FMT parameter"), 219 | (int) var_len, var); 220 | 221 | var = arg; 222 | if (*var == ',') 223 | var++; 224 | } 225 | else if (*var) 226 | { 227 | argp_failure (state, 0, 0, 228 | dgettext (state == NULL ? NULL 229 | : state->root_argp->argp_domain, 230 | "Garbage in ARGP_HELP_FMT: %s"), var); 231 | break; 232 | } 233 | } 234 | } 235 | 236 | /* Returns true if OPT hasn't been marked invisible. Visibility only affects 237 | whether OPT is displayed or used in sorting, not option shadowing. */ 238 | #define ovisible(opt) (! ((opt)->flags & OPTION_HIDDEN)) 239 | 240 | /* Returns true if OPT is an alias for an earlier option. */ 241 | #define oalias(opt) ((opt)->flags & OPTION_ALIAS) 242 | 243 | /* Returns true if OPT is an documentation-only entry. */ 244 | #define odoc(opt) ((opt)->flags & OPTION_DOC) 245 | 246 | /* Returns true if OPT is the end-of-list marker for a list of options. */ 247 | #define oend(opt) _option_is_end (opt) 248 | 249 | /* Returns true if OPT has a short option. */ 250 | #define oshort(opt) _option_is_short (opt) 251 | 252 | /* 253 | The help format for a particular option is like: 254 | 255 | -xARG, -yARG, --long1=ARG, --long2=ARG Documentation... 256 | 257 | Where ARG will be omitted if there's no argument, for this option, or 258 | will be surrounded by "[" and "]" appropiately if the argument is 259 | optional. The documentation string is word-wrapped appropiately, and if 260 | the list of options is long enough, it will be started on a separate line. 261 | If there are no short options for a given option, the first long option is 262 | indented slighly in a way that's supposed to make most long options appear 263 | to be in a separate column. 264 | 265 | For example, the following output (from ps): 266 | 267 | -p PID, --pid=PID List the process PID 268 | --pgrp=PGRP List processes in the process group PGRP 269 | -P, -x, --no-parent Include processes without parents 270 | -Q, --all-fields Don't elide unusable fields (normally if there's 271 | some reason ps can't print a field for any 272 | process, it's removed from the output entirely) 273 | -r, --reverse, --gratuitously-long-reverse-option 274 | Reverse the order of any sort 275 | --session[=SID] Add the processes from the session SID (which 276 | defaults to the sid of the current process) 277 | 278 | Here are some more options: 279 | -f ZOT, --foonly=ZOT Glork a foonly 280 | -z, --zaza Snit a zar 281 | 282 | -?, --help Give this help list 283 | --usage Give a short usage message 284 | -V, --version Print program version 285 | 286 | The struct argp_option array for the above could look like: 287 | 288 | { 289 | {"pid", 'p', "PID", 0, "List the process PID"}, 290 | {"pgrp", OPT_PGRP, "PGRP", 0, "List processes in the process group PGRP"}, 291 | {"no-parent", 'P', 0, 0, "Include processes without parents"}, 292 | {0, 'x', 0, OPTION_ALIAS}, 293 | {"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally" 294 | " if there's some reason ps can't" 295 | " print a field for any process, it's" 296 | " removed from the output entirely)" }, 297 | {"reverse", 'r', 0, 0, "Reverse the order of any sort"}, 298 | {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS}, 299 | {"session", OPT_SESS, "SID", OPTION_ARG_OPTIONAL, 300 | "Add the processes from the session" 301 | " SID (which defaults to the sid of" 302 | " the current process)" }, 303 | 304 | {0,0,0,0, "Here are some more options:"}, 305 | {"foonly", 'f', "ZOT", 0, "Glork a foonly"}, 306 | {"zaza", 'z', 0, 0, "Snit a zar"}, 307 | 308 | {0} 309 | } 310 | 311 | Note that the last three options are automatically supplied by argp_parse, 312 | unless you tell it not to with ARGP_NO_HELP. 313 | 314 | */ 315 | 316 | /* Returns true if CH occurs between BEG and END. */ 317 | static int 318 | find_char (char ch, char *beg, char *end) 319 | { 320 | while (beg < end) 321 | if (*beg == ch) 322 | return 1; 323 | else 324 | beg++; 325 | return 0; 326 | } 327 | 328 | struct hol_cluster; /* fwd decl */ 329 | 330 | struct hol_entry 331 | { 332 | /* First option. */ 333 | const struct argp_option *opt; 334 | /* Number of options (including aliases). */ 335 | unsigned num; 336 | 337 | /* A pointers into the HOL's short_options field, to the first short option 338 | letter for this entry. The order of the characters following this point 339 | corresponds to the order of options pointed to by OPT, and there are at 340 | most NUM. A short option recorded in a option following OPT is only 341 | valid if it occurs in the right place in SHORT_OPTIONS (otherwise it's 342 | probably been shadowed by some other entry). */ 343 | char *short_options; 344 | 345 | /* Entries are sorted by their group first, in the order: 346 | 1, 2, ..., n, 0, -m, ..., -2, -1 347 | and then alphabetically within each group. The default is 0. */ 348 | int group; 349 | 350 | /* The cluster of options this entry belongs to, or 0 if none. */ 351 | struct hol_cluster *cluster; 352 | 353 | /* The argp from which this option came. */ 354 | const struct argp *argp; 355 | }; 356 | 357 | /* A cluster of entries to reflect the argp tree structure. */ 358 | struct hol_cluster 359 | { 360 | /* A descriptive header printed before options in this cluster. */ 361 | const char *header; 362 | 363 | /* Used to order clusters within the same group with the same parent, 364 | according to the order in which they occurred in the parent argp's child 365 | list. */ 366 | int index; 367 | 368 | /* How to sort this cluster with respect to options and other clusters at the 369 | same depth (clusters always follow options in the same group). */ 370 | int group; 371 | 372 | /* The cluster to which this cluster belongs, or 0 if it's at the base 373 | level. */ 374 | struct hol_cluster *parent; 375 | 376 | /* The argp from which this cluster is (eventually) derived. */ 377 | const struct argp *argp; 378 | 379 | /* The distance this cluster is from the root. */ 380 | int depth; 381 | 382 | /* Clusters in a given hol are kept in a linked list, to make freeing them 383 | possible. */ 384 | struct hol_cluster *next; 385 | }; 386 | 387 | /* A list of options for help. */ 388 | struct hol 389 | { 390 | /* An array of hol_entry's. */ 391 | struct hol_entry *entries; 392 | /* The number of entries in this hol. If this field is zero, the others 393 | are undefined. */ 394 | unsigned num_entries; 395 | 396 | /* A string containing all short options in this HOL. Each entry contains 397 | pointers into this string, so the order can't be messed with blindly. */ 398 | char *short_options; 399 | 400 | /* Clusters of entries in this hol. */ 401 | struct hol_cluster *clusters; 402 | }; 403 | 404 | /* Create a struct hol from the options in ARGP. CLUSTER is the 405 | hol_cluster in which these entries occur, or 0, if at the root. */ 406 | static struct hol * 407 | make_hol (const struct argp *argp, struct hol_cluster *cluster) 408 | { 409 | char *so; 410 | const struct argp_option *o; 411 | const struct argp_option *opts = argp->options; 412 | struct hol_entry *entry; 413 | unsigned num_short_options = 0; 414 | struct hol *hol = malloc (sizeof (struct hol)); 415 | 416 | assert (hol); 417 | 418 | hol->num_entries = 0; 419 | hol->clusters = 0; 420 | 421 | if (opts) 422 | { 423 | int cur_group = 0; 424 | 425 | /* The first option must not be an alias. */ 426 | assert (! oalias (opts)); 427 | 428 | /* Calculate the space needed. */ 429 | for (o = opts; ! oend (o); o++) 430 | { 431 | if (! oalias (o)) 432 | hol->num_entries++; 433 | if (oshort (o)) 434 | num_short_options++; /* This is an upper bound. */ 435 | } 436 | 437 | hol->entries = malloc (sizeof (struct hol_entry) * hol->num_entries); 438 | hol->short_options = malloc (num_short_options + 1); 439 | 440 | assert (hol->entries && hol->short_options); 441 | #if SIZE_MAX <= UINT_MAX 442 | assert (hol->num_entries <= SIZE_MAX / sizeof (struct hol_entry)); 443 | #endif 444 | 445 | /* Fill in the entries. */ 446 | so = hol->short_options; 447 | for (o = opts, entry = hol->entries; ! oend (o); entry++) 448 | { 449 | entry->opt = o; 450 | entry->num = 0; 451 | entry->short_options = so; 452 | entry->group = cur_group = 453 | o->group 454 | ? o->group 455 | : ((!o->name && !o->key) 456 | ? cur_group + 1 457 | : cur_group); 458 | entry->cluster = cluster; 459 | entry->argp = argp; 460 | 461 | do 462 | { 463 | entry->num++; 464 | if (oshort (o) && ! find_char (o->key, hol->short_options, so)) 465 | /* O has a valid short option which hasn't already been used.*/ 466 | *so++ = o->key; 467 | o++; 468 | } 469 | while (! oend (o) && oalias (o)); 470 | } 471 | *so = '\0'; /* null terminated so we can find the length */ 472 | } 473 | 474 | return hol; 475 | } 476 | 477 | /* Add a new cluster to HOL, with the given GROUP and HEADER (taken from the 478 | associated argp child list entry), INDEX, and PARENT, and return a pointer 479 | to it. ARGP is the argp that this cluster results from. */ 480 | static struct hol_cluster * 481 | hol_add_cluster (struct hol *hol, int group, const char *header, int index, 482 | struct hol_cluster *parent, const struct argp *argp) 483 | { 484 | struct hol_cluster *cl = malloc (sizeof (struct hol_cluster)); 485 | if (cl) 486 | { 487 | cl->group = group; 488 | cl->header = header; 489 | 490 | cl->index = index; 491 | cl->parent = parent; 492 | cl->argp = argp; 493 | cl->depth = parent ? parent->depth + 1 : 0; 494 | 495 | cl->next = hol->clusters; 496 | hol->clusters = cl; 497 | } 498 | return cl; 499 | } 500 | 501 | /* Free HOL and any resources it uses. */ 502 | static void 503 | hol_free (struct hol *hol) 504 | { 505 | struct hol_cluster *cl = hol->clusters; 506 | 507 | while (cl) 508 | { 509 | struct hol_cluster *next = cl->next; 510 | free (cl); 511 | cl = next; 512 | } 513 | 514 | if (hol->num_entries > 0) 515 | { 516 | free (hol->entries); 517 | free (hol->short_options); 518 | } 519 | 520 | free (hol); 521 | } 522 | 523 | static int 524 | hol_entry_short_iterate (const struct hol_entry *entry, 525 | int (*func)(const struct argp_option *opt, 526 | const struct argp_option *real, 527 | const char *domain, void *cookie), 528 | const char *domain, void *cookie) 529 | { 530 | unsigned nopts; 531 | int val = 0; 532 | const struct argp_option *opt, *real = entry->opt; 533 | char *so = entry->short_options; 534 | 535 | for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--) 536 | if (oshort (opt) && *so == opt->key) 537 | { 538 | if (!oalias (opt)) 539 | real = opt; 540 | if (ovisible (opt)) 541 | val = (*func)(opt, real, domain, cookie); 542 | so++; 543 | } 544 | 545 | return val; 546 | } 547 | 548 | static __inline__ int 549 | __attribute__ ((always_inline)) 550 | hol_entry_long_iterate (const struct hol_entry *entry, 551 | int (*func)(const struct argp_option *opt, 552 | const struct argp_option *real, 553 | const char *domain, void *cookie), 554 | const char *domain, void *cookie) 555 | { 556 | unsigned nopts; 557 | int val = 0; 558 | const struct argp_option *opt, *real = entry->opt; 559 | 560 | for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--) 561 | if (opt->name) 562 | { 563 | if (!oalias (opt)) 564 | real = opt; 565 | if (ovisible (opt)) 566 | val = (*func)(opt, real, domain, cookie); 567 | } 568 | 569 | return val; 570 | } 571 | 572 | /* Iterator that returns true for the first short option. */ 573 | static __inline__ int 574 | until_short (const struct argp_option *opt, const struct argp_option *real, 575 | const char *domain, void *cookie) 576 | { 577 | return oshort (opt) ? opt->key : 0; 578 | } 579 | 580 | /* Returns the first valid short option in ENTRY, or 0 if there is none. */ 581 | static char 582 | hol_entry_first_short (const struct hol_entry *entry) 583 | { 584 | return hol_entry_short_iterate (entry, until_short, 585 | entry->argp->argp_domain, 0); 586 | } 587 | 588 | /* Returns the first valid long option in ENTRY, or 0 if there is none. */ 589 | static const char * 590 | hol_entry_first_long (const struct hol_entry *entry) 591 | { 592 | const struct argp_option *opt; 593 | unsigned num; 594 | for (opt = entry->opt, num = entry->num; num > 0; opt++, num--) 595 | if (opt->name && ovisible (opt)) 596 | return opt->name; 597 | return 0; 598 | } 599 | 600 | /* Returns the entry in HOL with the long option name NAME, or 0 if there is 601 | none. */ 602 | static struct hol_entry * 603 | hol_find_entry (struct hol *hol, const char *name) 604 | { 605 | struct hol_entry *entry = hol->entries; 606 | unsigned num_entries = hol->num_entries; 607 | 608 | while (num_entries-- > 0) 609 | { 610 | const struct argp_option *opt = entry->opt; 611 | unsigned num_opts = entry->num; 612 | 613 | while (num_opts-- > 0) 614 | if (opt->name && ovisible (opt) && strcmp (opt->name, name) == 0) 615 | return entry; 616 | else 617 | opt++; 618 | 619 | entry++; 620 | } 621 | 622 | return 0; 623 | } 624 | 625 | /* If an entry with the long option NAME occurs in HOL, set it's special 626 | sort position to GROUP. */ 627 | static void 628 | hol_set_group (struct hol *hol, const char *name, int group) 629 | { 630 | struct hol_entry *entry = hol_find_entry (hol, name); 631 | if (entry) 632 | entry->group = group; 633 | } 634 | 635 | /* Order by group: 0, 1, 2, ..., n, -m, ..., -2, -1. 636 | EQ is what to return if GROUP1 and GROUP2 are the same. */ 637 | static int 638 | group_cmp (int group1, int group2, int eq) 639 | { 640 | if (group1 == group2) 641 | return eq; 642 | else if ((group1 < 0 && group2 < 0) || (group1 >= 0 && group2 >= 0)) 643 | return group1 - group2; 644 | else 645 | return group2 - group1; 646 | } 647 | 648 | /* Compare clusters CL1 & CL2 by the order that they should appear in 649 | output. */ 650 | static int 651 | hol_cluster_cmp (const struct hol_cluster *cl1, const struct hol_cluster *cl2) 652 | { 653 | /* If one cluster is deeper than the other, use its ancestor at the same 654 | level, so that finding the common ancestor is straightforward. */ 655 | while (cl1->depth > cl2->depth) 656 | cl1 = cl1->parent; 657 | while (cl2->depth > cl1->depth) 658 | cl2 = cl2->parent; 659 | 660 | /* Now reduce both clusters to their ancestors at the point where both have 661 | a common parent; these can be directly compared. */ 662 | while (cl1->parent != cl2->parent) 663 | cl1 = cl1->parent, cl2 = cl2->parent; 664 | 665 | return group_cmp (cl1->group, cl2->group, cl2->index - cl1->index); 666 | } 667 | 668 | /* Return the ancestor of CL that's just below the root (i.e., has a parent 669 | of 0). */ 670 | static struct hol_cluster * 671 | hol_cluster_base (struct hol_cluster *cl) 672 | { 673 | while (cl->parent) 674 | cl = cl->parent; 675 | return cl; 676 | } 677 | 678 | /* Return true if CL1 is a child of CL2. */ 679 | static int 680 | hol_cluster_is_child (const struct hol_cluster *cl1, 681 | const struct hol_cluster *cl2) 682 | { 683 | while (cl1 && cl1 != cl2) 684 | cl1 = cl1->parent; 685 | return cl1 == cl2; 686 | } 687 | 688 | /* Given the name of a OPTION_DOC option, modifies NAME to start at the tail 689 | that should be used for comparisons, and returns true iff it should be 690 | treated as a non-option. */ 691 | static int 692 | canon_doc_option (const char **name) 693 | { 694 | int non_opt; 695 | /* Skip initial whitespace. */ 696 | while (isspace (**name)) 697 | (*name)++; 698 | /* Decide whether this looks like an option (leading `-') or not. */ 699 | non_opt = (**name != '-'); 700 | /* Skip until part of name used for sorting. */ 701 | while (**name && !isalnum (**name)) 702 | (*name)++; 703 | return non_opt; 704 | } 705 | 706 | /* Order ENTRY1 & ENTRY2 by the order which they should appear in a help 707 | listing. */ 708 | static int 709 | hol_entry_cmp (const struct hol_entry *entry1, 710 | const struct hol_entry *entry2) 711 | { 712 | /* The group numbers by which the entries should be ordered; if either is 713 | in a cluster, then this is just the group within the cluster. */ 714 | int group1 = entry1->group, group2 = entry2->group; 715 | 716 | if (entry1->cluster != entry2->cluster) 717 | { 718 | /* The entries are not within the same cluster, so we can't compare them 719 | directly, we have to use the appropiate clustering level too. */ 720 | if (! entry1->cluster) 721 | /* ENTRY1 is at the `base level', not in a cluster, so we have to 722 | compare it's group number with that of the base cluster in which 723 | ENTRY2 resides. Note that if they're in the same group, the 724 | clustered option always comes laster. */ 725 | return group_cmp (group1, hol_cluster_base (entry2->cluster)->group, -1); 726 | else if (! entry2->cluster) 727 | /* Likewise, but ENTRY2's not in a cluster. */ 728 | return group_cmp (hol_cluster_base (entry1->cluster)->group, group2, 1); 729 | else 730 | /* Both entries are in clusters, we can just compare the clusters. */ 731 | return hol_cluster_cmp (entry1->cluster, entry2->cluster); 732 | } 733 | else if (group1 == group2) 734 | /* The entries are both in the same cluster and group, so compare them 735 | alphabetically. */ 736 | { 737 | int short1 = hol_entry_first_short (entry1); 738 | int short2 = hol_entry_first_short (entry2); 739 | int doc1 = odoc (entry1->opt); 740 | int doc2 = odoc (entry2->opt); 741 | const char *long1 = hol_entry_first_long (entry1); 742 | const char *long2 = hol_entry_first_long (entry2); 743 | 744 | if (doc1) 745 | doc1 = long1 != NULL && canon_doc_option (&long1); 746 | if (doc2) 747 | doc2 = long2 != NULL && canon_doc_option (&long2); 748 | 749 | if (doc1 != doc2) 750 | /* `documentation' options always follow normal options (or 751 | documentation options that *look* like normal options). */ 752 | return doc1 - doc2; 753 | else if (!short1 && !short2 && long1 && long2) 754 | /* Only long options. */ 755 | return strcasecmp (long1, long2); 756 | else 757 | /* Compare short/short, long/short, short/long, using the first 758 | character of long options. Entries without *any* valid 759 | options (such as options with OPTION_HIDDEN set) will be put 760 | first, but as they're not displayed, it doesn't matter where 761 | they are. */ 762 | { 763 | char first1 = short1 ? short1 : long1 ? *long1 : 0; 764 | char first2 = short2 ? short2 : long2 ? *long2 : 0; 765 | #ifdef _tolower 766 | int lower_cmp = _tolower (first1) - _tolower (first2); 767 | #else 768 | int lower_cmp = tolower (first1) - tolower (first2); 769 | #endif 770 | /* Compare ignoring case, except when the options are both the 771 | same letter, in which case lower-case always comes first. */ 772 | return lower_cmp ? lower_cmp : first2 - first1; 773 | } 774 | } 775 | else 776 | /* Within the same cluster, but not the same group, so just compare 777 | groups. */ 778 | return group_cmp (group1, group2, 0); 779 | } 780 | 781 | /* Version of hol_entry_cmp with correct signature for qsort. */ 782 | static int 783 | hol_entry_qcmp (const void *entry1_v, const void *entry2_v) 784 | { 785 | return hol_entry_cmp (entry1_v, entry2_v); 786 | } 787 | 788 | /* Sort HOL by group and alphabetically by option name (with short options 789 | taking precedence over long). Since the sorting is for display purposes 790 | only, the shadowing of options isn't effected. */ 791 | static void 792 | hol_sort (struct hol *hol) 793 | { 794 | if (hol->num_entries > 0) 795 | qsort (hol->entries, hol->num_entries, sizeof (struct hol_entry), 796 | hol_entry_qcmp); 797 | } 798 | 799 | /* Append MORE to HOL, destroying MORE in the process. Options in HOL shadow 800 | any in MORE with the same name. */ 801 | static void 802 | hol_append (struct hol *hol, struct hol *more) 803 | { 804 | struct hol_cluster **cl_end = &hol->clusters; 805 | 806 | /* Steal MORE's cluster list, and add it to the end of HOL's. */ 807 | while (*cl_end) 808 | cl_end = &(*cl_end)->next; 809 | *cl_end = more->clusters; 810 | more->clusters = 0; 811 | 812 | /* Merge entries. */ 813 | if (more->num_entries > 0) 814 | { 815 | if (hol->num_entries == 0) 816 | { 817 | hol->num_entries = more->num_entries; 818 | hol->entries = more->entries; 819 | hol->short_options = more->short_options; 820 | more->num_entries = 0; /* Mark MORE's fields as invalid. */ 821 | } 822 | else 823 | /* Append the entries in MORE to those in HOL, taking care to only add 824 | non-shadowed SHORT_OPTIONS values. */ 825 | { 826 | unsigned left; 827 | char *so, *more_so; 828 | struct hol_entry *e; 829 | unsigned num_entries = hol->num_entries + more->num_entries; 830 | struct hol_entry *entries = 831 | malloc (num_entries * sizeof (struct hol_entry)); 832 | unsigned hol_so_len = strlen (hol->short_options); 833 | char *short_options = 834 | malloc (hol_so_len + strlen (more->short_options) + 1); 835 | 836 | assert (entries && short_options); 837 | #if SIZE_MAX <= UINT_MAX 838 | assert (num_entries <= SIZE_MAX / sizeof (struct hol_entry)); 839 | #endif 840 | 841 | mempcpy (mempcpy (entries, hol->entries, 842 | hol->num_entries * sizeof (struct hol_entry)), 843 | more->entries, 844 | more->num_entries * sizeof (struct hol_entry)); 845 | 846 | mempcpy (short_options, hol->short_options, hol_so_len); 847 | 848 | /* Fix up the short options pointers from HOL. */ 849 | for (e = entries, left = hol->num_entries; left > 0; e++, left--) 850 | e->short_options += (short_options - hol->short_options); 851 | 852 | /* Now add the short options from MORE, fixing up its entries 853 | too. */ 854 | so = short_options + hol_so_len; 855 | more_so = more->short_options; 856 | for (left = more->num_entries; left > 0; e++, left--) 857 | { 858 | int opts_left; 859 | const struct argp_option *opt; 860 | 861 | e->short_options = so; 862 | 863 | for (opts_left = e->num, opt = e->opt; opts_left; opt++, opts_left--) 864 | { 865 | int ch = *more_so; 866 | if (oshort (opt) && ch == opt->key) 867 | /* The next short option in MORE_SO, CH, is from OPT. */ 868 | { 869 | if (! find_char (ch, short_options, 870 | short_options + hol_so_len)) 871 | /* The short option CH isn't shadowed by HOL's options, 872 | so add it to the sum. */ 873 | *so++ = ch; 874 | more_so++; 875 | } 876 | } 877 | } 878 | 879 | *so = '\0'; 880 | 881 | free (hol->entries); 882 | free (hol->short_options); 883 | 884 | hol->entries = entries; 885 | hol->num_entries = num_entries; 886 | hol->short_options = short_options; 887 | } 888 | } 889 | 890 | hol_free (more); 891 | } 892 | 893 | /* Inserts enough spaces to make sure STREAM is at column COL. */ 894 | static void 895 | indent_to (argp_fmtstream_t stream, unsigned col) 896 | { 897 | int needed = col - argp_fmtstream_point (stream); 898 | while (needed-- > 0) 899 | argp_fmtstream_putc (stream, ' '); 900 | } 901 | 902 | /* Output to STREAM either a space, or a newline if there isn't room for at 903 | least ENSURE characters before the right margin. */ 904 | static void 905 | space (argp_fmtstream_t stream, size_t ensure) 906 | { 907 | if (argp_fmtstream_point (stream) + ensure 908 | >= argp_fmtstream_rmargin (stream)) 909 | argp_fmtstream_putc (stream, '\n'); 910 | else 911 | argp_fmtstream_putc (stream, ' '); 912 | } 913 | 914 | /* If the option REAL has an argument, we print it in using the printf 915 | format REQ_FMT or OPT_FMT depending on whether it's a required or 916 | optional argument. */ 917 | static void 918 | arg (const struct argp_option *real, const char *req_fmt, const char *opt_fmt, 919 | const char *domain, argp_fmtstream_t stream) 920 | { 921 | if (real->arg) 922 | { 923 | if (real->flags & OPTION_ARG_OPTIONAL) 924 | argp_fmtstream_printf (stream, opt_fmt, 925 | dgettext (domain, real->arg)); 926 | else 927 | argp_fmtstream_printf (stream, req_fmt, 928 | dgettext (domain, real->arg)); 929 | } 930 | } 931 | 932 | /* Helper functions for hol_entry_help. */ 933 | 934 | /* State used during the execution of hol_help. */ 935 | struct hol_help_state 936 | { 937 | /* PREV_ENTRY should contain the previous entry printed, or 0. */ 938 | struct hol_entry *prev_entry; 939 | 940 | /* If an entry is in a different group from the previous one, and SEP_GROUPS 941 | is true, then a blank line will be printed before any output. */ 942 | int sep_groups; 943 | 944 | /* True if a duplicate option argument was suppressed (only ever set if 945 | UPARAMS.dup_args is false). */ 946 | int suppressed_dup_arg; 947 | }; 948 | 949 | /* Some state used while printing a help entry (used to communicate with 950 | helper functions). See the doc for hol_entry_help for more info, as most 951 | of the fields are copied from its arguments. */ 952 | struct pentry_state 953 | { 954 | const struct hol_entry *entry; 955 | argp_fmtstream_t stream; 956 | struct hol_help_state *hhstate; 957 | 958 | /* True if nothing's been printed so far. */ 959 | int first; 960 | 961 | /* If non-zero, the state that was used to print this help. */ 962 | const struct argp_state *state; 963 | }; 964 | 965 | /* If a user doc filter should be applied to DOC, do so. */ 966 | static const char * 967 | filter_doc (const char *doc, int key, const struct argp *argp, 968 | const struct argp_state *state) 969 | { 970 | if (argp && argp->help_filter) 971 | /* We must apply a user filter to this output. */ 972 | { 973 | void *input = argp_input (argp, state); 974 | return (*argp->help_filter) (key, doc, input); 975 | } 976 | else 977 | /* No filter. */ 978 | return doc; 979 | } 980 | 981 | /* Prints STR as a header line, with the margin lines set appropiately, and 982 | notes the fact that groups should be separated with a blank line. ARGP is 983 | the argp that should dictate any user doc filtering to take place. Note 984 | that the previous wrap margin isn't restored, but the left margin is reset 985 | to 0. */ 986 | static void 987 | print_header (const char *str, const struct argp *argp, 988 | struct pentry_state *pest) 989 | { 990 | const char *tstr = dgettext (argp->argp_domain, str); 991 | const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_HEADER, argp, pest->state); 992 | 993 | if (fstr) 994 | { 995 | if (*fstr) 996 | { 997 | if (pest->hhstate->prev_entry) 998 | /* Precede with a blank line. */ 999 | argp_fmtstream_putc (pest->stream, '\n'); 1000 | indent_to (pest->stream, uparams.header_col); 1001 | argp_fmtstream_set_lmargin (pest->stream, uparams.header_col); 1002 | argp_fmtstream_set_wmargin (pest->stream, uparams.header_col); 1003 | argp_fmtstream_puts (pest->stream, fstr); 1004 | argp_fmtstream_set_lmargin (pest->stream, 0); 1005 | argp_fmtstream_putc (pest->stream, '\n'); 1006 | } 1007 | 1008 | pest->hhstate->sep_groups = 1; /* Separate subsequent groups. */ 1009 | } 1010 | 1011 | if (fstr != tstr) 1012 | free ((char *) fstr); 1013 | } 1014 | 1015 | /* Inserts a comma if this isn't the first item on the line, and then makes 1016 | sure we're at least to column COL. If this *is* the first item on a line, 1017 | prints any pending whitespace/headers that should precede this line. Also 1018 | clears FIRST. */ 1019 | static void 1020 | comma (unsigned col, struct pentry_state *pest) 1021 | { 1022 | if (pest->first) 1023 | { 1024 | const struct hol_entry *pe = pest->hhstate->prev_entry; 1025 | const struct hol_cluster *cl = pest->entry->cluster; 1026 | 1027 | if (pest->hhstate->sep_groups && pe && pest->entry->group != pe->group) 1028 | argp_fmtstream_putc (pest->stream, '\n'); 1029 | 1030 | if (cl && cl->header && *cl->header 1031 | && (!pe 1032 | || (pe->cluster != cl 1033 | && !hol_cluster_is_child (pe->cluster, cl)))) 1034 | /* If we're changing clusters, then this must be the start of the 1035 | ENTRY's cluster unless that is an ancestor of the previous one 1036 | (in which case we had just popped into a sub-cluster for a bit). 1037 | If so, then print the cluster's header line. */ 1038 | { 1039 | int old_wm = argp_fmtstream_wmargin (pest->stream); 1040 | print_header (cl->header, cl->argp, pest); 1041 | argp_fmtstream_set_wmargin (pest->stream, old_wm); 1042 | } 1043 | 1044 | pest->first = 0; 1045 | } 1046 | else 1047 | argp_fmtstream_puts (pest->stream, ", "); 1048 | 1049 | indent_to (pest->stream, col); 1050 | } 1051 | 1052 | /* Print help for ENTRY to STREAM. */ 1053 | static void 1054 | hol_entry_help (struct hol_entry *entry, const struct argp_state *state, 1055 | argp_fmtstream_t stream, struct hol_help_state *hhstate) 1056 | { 1057 | unsigned num; 1058 | const struct argp_option *real = entry->opt, *opt; 1059 | char *so = entry->short_options; 1060 | int have_long_opt = 0; /* We have any long options. */ 1061 | /* Saved margins. */ 1062 | int old_lm = argp_fmtstream_set_lmargin (stream, 0); 1063 | int old_wm = argp_fmtstream_wmargin (stream); 1064 | /* PEST is a state block holding some of our variables that we'd like to 1065 | share with helper functions. */ 1066 | struct pentry_state pest = { entry, stream, hhstate, 1, state }; 1067 | 1068 | if (! odoc (real)) 1069 | for (opt = real, num = entry->num; num > 0; opt++, num--) 1070 | if (opt->name && ovisible (opt)) 1071 | { 1072 | have_long_opt = 1; 1073 | break; 1074 | } 1075 | 1076 | /* First emit short options. */ 1077 | argp_fmtstream_set_wmargin (stream, uparams.short_opt_col); /* For truly bizarre cases. */ 1078 | for (opt = real, num = entry->num; num > 0; opt++, num--) 1079 | if (oshort (opt) && opt->key == *so) 1080 | /* OPT has a valid (non shadowed) short option. */ 1081 | { 1082 | if (ovisible (opt)) 1083 | { 1084 | comma (uparams.short_opt_col, &pest); 1085 | argp_fmtstream_putc (stream, '-'); 1086 | argp_fmtstream_putc (stream, *so); 1087 | if (!have_long_opt || uparams.dup_args) 1088 | arg (real, " %s", "[%s]", 1089 | state == NULL ? NULL : state->root_argp->argp_domain, 1090 | stream); 1091 | else if (real->arg) 1092 | hhstate->suppressed_dup_arg = 1; 1093 | } 1094 | so++; 1095 | } 1096 | 1097 | /* Now, long options. */ 1098 | if (odoc (real)) 1099 | /* A `documentation' option. */ 1100 | { 1101 | argp_fmtstream_set_wmargin (stream, uparams.doc_opt_col); 1102 | for (opt = real, num = entry->num; num > 0; opt++, num--) 1103 | if (opt->name && ovisible (opt)) 1104 | { 1105 | comma (uparams.doc_opt_col, &pest); 1106 | /* Calling gettext here isn't quite right, since sorting will 1107 | have been done on the original; but documentation options 1108 | should be pretty rare anyway... */ 1109 | argp_fmtstream_puts (stream, 1110 | dgettext (state == NULL ? NULL 1111 | : state->root_argp->argp_domain, 1112 | opt->name)); 1113 | } 1114 | } 1115 | else 1116 | /* A real long option. */ 1117 | { 1118 | argp_fmtstream_set_wmargin (stream, uparams.long_opt_col); 1119 | for (opt = real, num = entry->num; num > 0; opt++, num--) 1120 | if (opt->name && ovisible (opt)) 1121 | { 1122 | comma (uparams.long_opt_col, &pest); 1123 | argp_fmtstream_printf (stream, "--%s", opt->name); 1124 | arg (real, "=%s", "[=%s]", 1125 | state == NULL ? NULL : state->root_argp->argp_domain, stream); 1126 | } 1127 | } 1128 | 1129 | /* Next, documentation strings. */ 1130 | argp_fmtstream_set_lmargin (stream, 0); 1131 | 1132 | if (pest.first) 1133 | { 1134 | /* Didn't print any switches, what's up? */ 1135 | if (!oshort (real) && !real->name) 1136 | /* This is a group header, print it nicely. */ 1137 | print_header (real->doc, entry->argp, &pest); 1138 | else 1139 | /* Just a totally shadowed option or null header; print nothing. */ 1140 | goto cleanup; /* Just return, after cleaning up. */ 1141 | } 1142 | else 1143 | { 1144 | const char *tstr = real->doc ? dgettext (state == NULL ? NULL 1145 | : state->root_argp->argp_domain, 1146 | real->doc) : 0; 1147 | const char *fstr = filter_doc (tstr, real->key, entry->argp, state); 1148 | if (fstr && *fstr) 1149 | { 1150 | unsigned int col = argp_fmtstream_point (stream); 1151 | 1152 | argp_fmtstream_set_lmargin (stream, uparams.opt_doc_col); 1153 | argp_fmtstream_set_wmargin (stream, uparams.opt_doc_col); 1154 | 1155 | if (col > (unsigned int) (uparams.opt_doc_col + 3)) 1156 | argp_fmtstream_putc (stream, '\n'); 1157 | else if (col >= (unsigned int) uparams.opt_doc_col) 1158 | argp_fmtstream_puts (stream, " "); 1159 | else 1160 | indent_to (stream, uparams.opt_doc_col); 1161 | 1162 | argp_fmtstream_puts (stream, fstr); 1163 | } 1164 | if (fstr && fstr != tstr) 1165 | free ((char *) fstr); 1166 | 1167 | /* Reset the left margin. */ 1168 | argp_fmtstream_set_lmargin (stream, 0); 1169 | argp_fmtstream_putc (stream, '\n'); 1170 | } 1171 | 1172 | hhstate->prev_entry = entry; 1173 | 1174 | cleanup: 1175 | argp_fmtstream_set_lmargin (stream, old_lm); 1176 | argp_fmtstream_set_wmargin (stream, old_wm); 1177 | } 1178 | 1179 | /* Output a long help message about the options in HOL to STREAM. */ 1180 | static void 1181 | hol_help (struct hol *hol, const struct argp_state *state, 1182 | argp_fmtstream_t stream) 1183 | { 1184 | unsigned num; 1185 | struct hol_entry *entry; 1186 | struct hol_help_state hhstate = { 0, 0, 0 }; 1187 | 1188 | for (entry = hol->entries, num = hol->num_entries; num > 0; entry++, num--) 1189 | hol_entry_help (entry, state, stream, &hhstate); 1190 | 1191 | if (hhstate.suppressed_dup_arg && uparams.dup_args_note) 1192 | { 1193 | const char *tstr = dgettext (state == NULL ? NULL 1194 | : state->root_argp->argp_domain, "\ 1195 | Mandatory or optional arguments to long options are also mandatory or \ 1196 | optional for any corresponding short options."); 1197 | const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_DUP_ARGS_NOTE, 1198 | state ? state->root_argp : 0, state); 1199 | if (fstr && *fstr) 1200 | { 1201 | argp_fmtstream_putc (stream, '\n'); 1202 | argp_fmtstream_puts (stream, fstr); 1203 | argp_fmtstream_putc (stream, '\n'); 1204 | } 1205 | if (fstr && fstr != tstr) 1206 | free ((char *) fstr); 1207 | } 1208 | } 1209 | 1210 | /* Helper functions for hol_usage. */ 1211 | 1212 | /* If OPT is a short option without an arg, append its key to the string 1213 | pointer pointer to by COOKIE, and advance the pointer. */ 1214 | static int 1215 | add_argless_short_opt (const struct argp_option *opt, 1216 | const struct argp_option *real, 1217 | const char *domain, void *cookie) 1218 | { 1219 | char **snao_end = cookie; 1220 | if (!(opt->arg || real->arg) 1221 | && !((opt->flags | real->flags) & OPTION_NO_USAGE)) 1222 | *(*snao_end)++ = opt->key; 1223 | return 0; 1224 | } 1225 | 1226 | /* If OPT is a short option with an arg, output a usage entry for it to the 1227 | stream pointed at by COOKIE. */ 1228 | static int 1229 | usage_argful_short_opt (const struct argp_option *opt, 1230 | const struct argp_option *real, 1231 | const char *domain, void *cookie) 1232 | { 1233 | argp_fmtstream_t stream = cookie; 1234 | const char *arg = opt->arg; 1235 | int flags = opt->flags | real->flags; 1236 | 1237 | if (! arg) 1238 | arg = real->arg; 1239 | 1240 | if (arg && !(flags & OPTION_NO_USAGE)) 1241 | { 1242 | arg = dgettext (domain, arg); 1243 | 1244 | if (flags & OPTION_ARG_OPTIONAL) 1245 | argp_fmtstream_printf (stream, " [-%c[%s]]", opt->key, arg); 1246 | else 1247 | { 1248 | /* Manually do line wrapping so that it (probably) won't 1249 | get wrapped at the embedded space. */ 1250 | space (stream, 6 + strlen (arg)); 1251 | argp_fmtstream_printf (stream, "[-%c %s]", opt->key, arg); 1252 | } 1253 | } 1254 | 1255 | return 0; 1256 | } 1257 | 1258 | /* Output a usage entry for the long option opt to the stream pointed at by 1259 | COOKIE. */ 1260 | static int 1261 | usage_long_opt (const struct argp_option *opt, 1262 | const struct argp_option *real, 1263 | const char *domain, void *cookie) 1264 | { 1265 | argp_fmtstream_t stream = cookie; 1266 | const char *arg = opt->arg; 1267 | int flags = opt->flags | real->flags; 1268 | 1269 | if (! arg) 1270 | arg = real->arg; 1271 | 1272 | if (! (flags & OPTION_NO_USAGE)) 1273 | { 1274 | if (arg) 1275 | { 1276 | arg = dgettext (domain, arg); 1277 | if (flags & OPTION_ARG_OPTIONAL) 1278 | argp_fmtstream_printf (stream, " [--%s[=%s]]", opt->name, arg); 1279 | else 1280 | argp_fmtstream_printf (stream, " [--%s=%s]", opt->name, arg); 1281 | } 1282 | else 1283 | argp_fmtstream_printf (stream, " [--%s]", opt->name); 1284 | } 1285 | 1286 | return 0; 1287 | } 1288 | 1289 | /* Print a short usage description for the arguments in HOL to STREAM. */ 1290 | static void 1291 | hol_usage (struct hol *hol, argp_fmtstream_t stream) 1292 | { 1293 | if (hol->num_entries > 0) 1294 | { 1295 | unsigned nentries; 1296 | struct hol_entry *entry; 1297 | char *short_no_arg_opts = alloca (strlen (hol->short_options) + 1); 1298 | char *snao_end = short_no_arg_opts; 1299 | 1300 | /* First we put a list of short options without arguments. */ 1301 | for (entry = hol->entries, nentries = hol->num_entries 1302 | ; nentries > 0 1303 | ; entry++, nentries--) 1304 | hol_entry_short_iterate (entry, add_argless_short_opt, 1305 | entry->argp->argp_domain, &snao_end); 1306 | if (snao_end > short_no_arg_opts) 1307 | { 1308 | *snao_end++ = 0; 1309 | argp_fmtstream_printf (stream, " [-%s]", short_no_arg_opts); 1310 | } 1311 | 1312 | /* Now a list of short options *with* arguments. */ 1313 | for (entry = hol->entries, nentries = hol->num_entries 1314 | ; nentries > 0 1315 | ; entry++, nentries--) 1316 | hol_entry_short_iterate (entry, usage_argful_short_opt, 1317 | entry->argp->argp_domain, stream); 1318 | 1319 | /* Finally, a list of long options (whew!). */ 1320 | for (entry = hol->entries, nentries = hol->num_entries 1321 | ; nentries > 0 1322 | ; entry++, nentries--) 1323 | hol_entry_long_iterate (entry, usage_long_opt, 1324 | entry->argp->argp_domain, stream); 1325 | } 1326 | } 1327 | 1328 | /* Make a HOL containing all levels of options in ARGP. CLUSTER is the 1329 | cluster in which ARGP's entries should be clustered, or 0. */ 1330 | static struct hol * 1331 | argp_hol (const struct argp *argp, struct hol_cluster *cluster) 1332 | { 1333 | const struct argp_child *child = argp->children; 1334 | struct hol *hol = make_hol (argp, cluster); 1335 | if (child) 1336 | while (child->argp) 1337 | { 1338 | struct hol_cluster *child_cluster = 1339 | ((child->group || child->header) 1340 | /* Put CHILD->argp within its own cluster. */ 1341 | ? hol_add_cluster (hol, child->group, child->header, 1342 | child - argp->children, cluster, argp) 1343 | /* Just merge it into the parent's cluster. */ 1344 | : cluster); 1345 | hol_append (hol, argp_hol (child->argp, child_cluster)) ; 1346 | child++; 1347 | } 1348 | return hol; 1349 | } 1350 | 1351 | /* Calculate how many different levels with alternative args strings exist in 1352 | ARGP. */ 1353 | static size_t 1354 | argp_args_levels (const struct argp *argp) 1355 | { 1356 | size_t levels = 0; 1357 | const struct argp_child *child = argp->children; 1358 | 1359 | if (argp->args_doc && strchr (argp->args_doc, '\n')) 1360 | levels++; 1361 | 1362 | if (child) 1363 | while (child->argp) 1364 | levels += argp_args_levels ((child++)->argp); 1365 | 1366 | return levels; 1367 | } 1368 | 1369 | /* Print all the non-option args documented in ARGP to STREAM. Any output is 1370 | preceded by a space. LEVELS is a pointer to a byte vector the length 1371 | returned by argp_args_levels; it should be initialized to zero, and 1372 | updated by this routine for the next call if ADVANCE is true. True is 1373 | returned as long as there are more patterns to output. */ 1374 | static int 1375 | argp_args_usage (const struct argp *argp, const struct argp_state *state, 1376 | char **levels, int advance, argp_fmtstream_t stream) 1377 | { 1378 | char *our_level = *levels; 1379 | int multiple = 0; 1380 | const struct argp_child *child = argp->children; 1381 | const char *tdoc = dgettext (argp->argp_domain, argp->args_doc), *nl = 0; 1382 | const char *fdoc = filter_doc (tdoc, ARGP_KEY_HELP_ARGS_DOC, argp, state); 1383 | 1384 | if (fdoc) 1385 | { 1386 | const char *cp = fdoc; 1387 | nl = strchrnul (cp, '\n'); 1388 | if (*nl != '\0') 1389 | /* This is a `multi-level' args doc; advance to the correct position 1390 | as determined by our state in LEVELS, and update LEVELS. */ 1391 | { 1392 | int i; 1393 | multiple = 1; 1394 | for (i = 0; i < *our_level; i++) 1395 | cp = nl + 1, nl = strchrnul (cp, '\n'); 1396 | (*levels)++; 1397 | } 1398 | 1399 | /* Manually do line wrapping so that it (probably) won't get wrapped at 1400 | any embedded spaces. */ 1401 | space (stream, 1 + nl - cp); 1402 | 1403 | argp_fmtstream_write (stream, cp, nl - cp); 1404 | } 1405 | if (fdoc && fdoc != tdoc) 1406 | free ((char *)fdoc); /* Free user's modified doc string. */ 1407 | 1408 | if (child) 1409 | while (child->argp) 1410 | advance = !argp_args_usage ((child++)->argp, state, levels, advance, stream); 1411 | 1412 | if (advance && multiple) 1413 | { 1414 | /* Need to increment our level. */ 1415 | if (*nl) 1416 | /* There's more we can do here. */ 1417 | { 1418 | (*our_level)++; 1419 | advance = 0; /* Our parent shouldn't advance also. */ 1420 | } 1421 | else if (*our_level > 0) 1422 | /* We had multiple levels, but used them up; reset to zero. */ 1423 | *our_level = 0; 1424 | } 1425 | 1426 | return !advance; 1427 | } 1428 | 1429 | /* Print the documentation for ARGP to STREAM; if POST is false, then 1430 | everything preceeding a `\v' character in the documentation strings (or 1431 | the whole string, for those with none) is printed, otherwise, everything 1432 | following the `\v' character (nothing for strings without). Each separate 1433 | bit of documentation is separated a blank line, and if PRE_BLANK is true, 1434 | then the first is as well. If FIRST_ONLY is true, only the first 1435 | occurrence is output. Returns true if anything was output. */ 1436 | static int 1437 | argp_doc (const struct argp *argp, const struct argp_state *state, 1438 | int post, int pre_blank, int first_only, 1439 | argp_fmtstream_t stream) 1440 | { 1441 | const char *text; 1442 | const char *inp_text; 1443 | void *input = 0; 1444 | int anything = 0; 1445 | size_t inp_text_limit = 0; 1446 | const char *doc = dgettext (argp->argp_domain, argp->doc); 1447 | const struct argp_child *child = argp->children; 1448 | 1449 | if (doc) 1450 | { 1451 | char *vt = strchr (doc, '\v'); 1452 | inp_text = post ? (vt ? vt + 1 : 0) : doc; 1453 | inp_text_limit = (!post && vt) ? (vt - doc) : 0; 1454 | } 1455 | else 1456 | inp_text = 0; 1457 | 1458 | if (argp->help_filter) 1459 | /* We have to filter the doc strings. */ 1460 | { 1461 | if (inp_text_limit) 1462 | /* Copy INP_TEXT so that it's nul-terminated. */ 1463 | inp_text = strndup (inp_text, inp_text_limit); 1464 | input = argp_input (argp, state); 1465 | text = 1466 | (*argp->help_filter) (post 1467 | ? ARGP_KEY_HELP_POST_DOC 1468 | : ARGP_KEY_HELP_PRE_DOC, 1469 | inp_text, input); 1470 | } 1471 | else 1472 | text = (const char *) inp_text; 1473 | 1474 | if (text) 1475 | { 1476 | if (pre_blank) 1477 | argp_fmtstream_putc (stream, '\n'); 1478 | 1479 | if (text == inp_text && inp_text_limit) 1480 | argp_fmtstream_write (stream, inp_text, inp_text_limit); 1481 | else 1482 | argp_fmtstream_puts (stream, text); 1483 | 1484 | if (argp_fmtstream_point (stream) > argp_fmtstream_lmargin (stream)) 1485 | argp_fmtstream_putc (stream, '\n'); 1486 | 1487 | anything = 1; 1488 | } 1489 | 1490 | if (text && text != inp_text) 1491 | free ((char *) text); /* Free TEXT returned from the help filter. */ 1492 | if (inp_text && inp_text_limit && argp->help_filter) 1493 | free ((char *) inp_text); /* We copied INP_TEXT, so free it now. */ 1494 | 1495 | if (post && argp->help_filter) 1496 | /* Now see if we have to output a ARGP_KEY_HELP_EXTRA text. */ 1497 | { 1498 | text = (*argp->help_filter) (ARGP_KEY_HELP_EXTRA, 0, input); 1499 | if (text) 1500 | { 1501 | if (anything || pre_blank) 1502 | argp_fmtstream_putc (stream, '\n'); 1503 | argp_fmtstream_puts (stream, text); 1504 | free ((char *) text); 1505 | if (argp_fmtstream_point (stream) 1506 | > argp_fmtstream_lmargin (stream)) 1507 | argp_fmtstream_putc (stream, '\n'); 1508 | anything = 1; 1509 | } 1510 | } 1511 | 1512 | if (child) 1513 | while (child->argp && !(first_only && anything)) 1514 | anything |= 1515 | argp_doc ((child++)->argp, state, 1516 | post, anything || pre_blank, first_only, 1517 | stream); 1518 | 1519 | return anything; 1520 | } 1521 | 1522 | /* Output a usage message for ARGP to STREAM. If called from 1523 | argp_state_help, STATE is the relevent parsing state. FLAGS are from the 1524 | set ARGP_HELP_*. NAME is what to use wherever a `program name' is 1525 | needed. */ 1526 | static void 1527 | _help (const struct argp *argp, const struct argp_state *state, FILE *stream, 1528 | unsigned flags, char *name) 1529 | { 1530 | int anything = 0; /* Whether we've output anything. */ 1531 | struct hol *hol = 0; 1532 | argp_fmtstream_t fs; 1533 | 1534 | if (! stream) 1535 | return; 1536 | 1537 | #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE) 1538 | flockfile (stream); 1539 | #endif 1540 | 1541 | fill_in_uparams (state); 1542 | 1543 | fs = argp_make_fmtstream (stream, 0, uparams.rmargin, 0); 1544 | if (! fs) 1545 | { 1546 | #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE) 1547 | funlockfile (stream); 1548 | #endif 1549 | return; 1550 | } 1551 | 1552 | if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG)) 1553 | { 1554 | hol = argp_hol (argp, 0); 1555 | 1556 | /* If present, these options always come last. */ 1557 | hol_set_group (hol, "help", -1); 1558 | hol_set_group (hol, "version", -1); 1559 | 1560 | hol_sort (hol); 1561 | } 1562 | 1563 | if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE)) 1564 | /* Print a short `Usage:' message. */ 1565 | { 1566 | int first_pattern = 1, more_patterns; 1567 | size_t num_pattern_levels = argp_args_levels (argp); 1568 | char *pattern_levels = alloca (num_pattern_levels); 1569 | 1570 | memset (pattern_levels, 0, num_pattern_levels); 1571 | 1572 | do 1573 | { 1574 | int old_lm; 1575 | int old_wm = argp_fmtstream_set_wmargin (fs, uparams.usage_indent); 1576 | char *levels = pattern_levels; 1577 | 1578 | if (first_pattern) 1579 | argp_fmtstream_printf (fs, "%s %s", 1580 | dgettext (argp->argp_domain, "Usage:"), 1581 | name); 1582 | else 1583 | argp_fmtstream_printf (fs, "%s %s", 1584 | dgettext (argp->argp_domain, " or: "), 1585 | name); 1586 | 1587 | /* We set the lmargin as well as the wmargin, because hol_usage 1588 | manually wraps options with newline to avoid annoying breaks. */ 1589 | old_lm = argp_fmtstream_set_lmargin (fs, uparams.usage_indent); 1590 | 1591 | if (flags & ARGP_HELP_SHORT_USAGE) 1592 | /* Just show where the options go. */ 1593 | { 1594 | if (hol->num_entries > 0) 1595 | argp_fmtstream_puts (fs, dgettext (argp->argp_domain, 1596 | " [OPTION...]")); 1597 | } 1598 | else 1599 | /* Actually print the options. */ 1600 | { 1601 | hol_usage (hol, fs); 1602 | flags |= ARGP_HELP_SHORT_USAGE; /* But only do so once. */ 1603 | } 1604 | 1605 | more_patterns = argp_args_usage (argp, state, &levels, 1, fs); 1606 | 1607 | argp_fmtstream_set_wmargin (fs, old_wm); 1608 | argp_fmtstream_set_lmargin (fs, old_lm); 1609 | 1610 | argp_fmtstream_putc (fs, '\n'); 1611 | anything = 1; 1612 | 1613 | first_pattern = 0; 1614 | } 1615 | while (more_patterns); 1616 | } 1617 | 1618 | if (flags & ARGP_HELP_PRE_DOC) 1619 | anything |= argp_doc (argp, state, 0, 0, 1, fs); 1620 | 1621 | if (flags & ARGP_HELP_SEE) 1622 | { 1623 | argp_fmtstream_printf (fs, dgettext (argp->argp_domain, "\ 1624 | Try `%s --help' or `%s --usage' for more information.\n"), 1625 | name, name); 1626 | anything = 1; 1627 | } 1628 | 1629 | if (flags & ARGP_HELP_LONG) 1630 | /* Print a long, detailed help message. */ 1631 | { 1632 | /* Print info about all the options. */ 1633 | if (hol->num_entries > 0) 1634 | { 1635 | if (anything) 1636 | argp_fmtstream_putc (fs, '\n'); 1637 | hol_help (hol, state, fs); 1638 | anything = 1; 1639 | } 1640 | } 1641 | 1642 | if (flags & ARGP_HELP_POST_DOC) 1643 | /* Print any documentation strings at the end. */ 1644 | anything |= argp_doc (argp, state, 1, anything, 0, fs); 1645 | 1646 | if ((flags & ARGP_HELP_BUG_ADDR) && argp_program_bug_address) 1647 | { 1648 | if (anything) 1649 | argp_fmtstream_putc (fs, '\n'); 1650 | argp_fmtstream_printf (fs, dgettext (argp->argp_domain, 1651 | "Report bugs to %s.\n"), 1652 | argp_program_bug_address); 1653 | anything = 1; 1654 | } 1655 | 1656 | #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE) 1657 | funlockfile (stream); 1658 | #endif 1659 | 1660 | if (hol) 1661 | hol_free (hol); 1662 | 1663 | argp_fmtstream_free (fs); 1664 | } 1665 | 1666 | /* Output a usage message for ARGP to STREAM. FLAGS are from the set 1667 | ARGP_HELP_*. NAME is what to use wherever a `program name' is needed. */ 1668 | void argp_help (const struct argp *argp, FILE *stream, 1669 | unsigned flags, char *name) 1670 | { 1671 | _help (argp, 0, stream, flags, name); 1672 | } 1673 | 1674 | char * 1675 | __argp_short_program_name (void) 1676 | { 1677 | # ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__ 1678 | /* 1679 | * uClibc provides both program_invocation_name and 1680 | * program_invocation_short_name 1681 | */ 1682 | return (char *) program_invocation_short_name; 1683 | # else 1684 | /* FIXME: What now? Miles suggests that it is better to use NULL, 1685 | but currently the value is passed on directly to fputs_unlocked, 1686 | so that requires more changes. */ 1687 | return ""; 1688 | # endif 1689 | } 1690 | 1691 | /* Output, if appropriate, a usage message for STATE to STREAM. FLAGS are 1692 | from the set ARGP_HELP_*. */ 1693 | void 1694 | argp_state_help (const struct argp_state *state, FILE *stream, unsigned flags) 1695 | { 1696 | if ((!state || ! (state->flags & ARGP_NO_ERRS)) && stream) 1697 | { 1698 | if (state && (state->flags & ARGP_LONG_ONLY)) 1699 | flags |= ARGP_HELP_LONG_ONLY; 1700 | 1701 | _help (state ? state->root_argp : 0, state, stream, flags, 1702 | state ? state->name : __argp_short_program_name ()); 1703 | 1704 | if (!state || ! (state->flags & ARGP_NO_EXIT)) 1705 | { 1706 | if (flags & ARGP_HELP_EXIT_ERR) 1707 | exit (argp_err_exit_status); 1708 | if (flags & ARGP_HELP_EXIT_OK) 1709 | exit (0); 1710 | } 1711 | } 1712 | } 1713 | 1714 | /* Possibly output the standard usage message for ARGP to stderr and exit. */ 1715 | void 1716 | argp_usage (const struct argp_state *state) 1717 | { 1718 | argp_state_help (state, stderr, ARGP_HELP_STD_USAGE); 1719 | } 1720 | 1721 | /* If appropriate, print the printf string FMT and following args, preceded 1722 | by the program name and `:', to stderr, and followed by a `Try ... --help' 1723 | message, then exit (1). */ 1724 | void 1725 | argp_error (const struct argp_state *state, const char *fmt, ...) 1726 | { 1727 | if (!state || !(state->flags & ARGP_NO_ERRS)) 1728 | { 1729 | FILE *stream = state ? state->err_stream : stderr; 1730 | 1731 | if (stream) 1732 | { 1733 | va_list ap; 1734 | 1735 | #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE) 1736 | flockfile (stream); 1737 | #endif 1738 | 1739 | va_start (ap, fmt); 1740 | 1741 | #if defined _LIBC && defined USE_IN_LIBIO 1742 | char *buf; 1743 | 1744 | if (_IO_vasprintf (&buf, fmt, ap) < 0) 1745 | buf = NULL; 1746 | 1747 | __fxprintf (stream, "%s: %s\n", 1748 | state ? state->name : __argp_short_program_name (), buf); 1749 | 1750 | free (buf); 1751 | #else 1752 | fputs_unlocked (state ? state->name : __argp_short_program_name (), 1753 | stream); 1754 | putc_unlocked (':', stream); 1755 | putc_unlocked (' ', stream); 1756 | 1757 | vfprintf (stream, fmt, ap); 1758 | 1759 | putc_unlocked ('\n', stream); 1760 | #endif 1761 | 1762 | argp_state_help (state, stream, ARGP_HELP_STD_ERR); 1763 | 1764 | va_end (ap); 1765 | 1766 | #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE) 1767 | funlockfile (stream); 1768 | #endif 1769 | } 1770 | } 1771 | } 1772 | 1773 | /* Similar to the standard gnu error-reporting function error(), but will 1774 | respect the ARGP_NO_EXIT and ARGP_NO_ERRS flags in STATE, and will print 1775 | to STATE->err_stream. This is useful for argument parsing code that is 1776 | shared between program startup (when exiting is desired) and runtime 1777 | option parsing (when typically an error code is returned instead). The 1778 | difference between this function and argp_error is that the latter is for 1779 | *parsing errors*, and the former is for other problems that occur during 1780 | parsing but don't reflect a (syntactic) problem with the input. */ 1781 | void 1782 | argp_failure (const struct argp_state *state, int status, int errnum, 1783 | const char *fmt, ...) 1784 | { 1785 | if (!state || !(state->flags & ARGP_NO_ERRS)) 1786 | { 1787 | FILE *stream = state ? state->err_stream : stderr; 1788 | 1789 | if (stream) 1790 | { 1791 | #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE) 1792 | flockfile (stream); 1793 | #endif 1794 | 1795 | #if defined _LIBC && defined USE_IN_LIBIO 1796 | __fxprintf (stream, "%s", 1797 | state ? state->name : __argp_short_program_name ()); 1798 | #else 1799 | fputs_unlocked (state ? state->name : __argp_short_program_name (), 1800 | stream); 1801 | #endif 1802 | 1803 | if (fmt) 1804 | { 1805 | va_list ap; 1806 | 1807 | va_start (ap, fmt); 1808 | #if defined _LIBC && defined USE_IN_LIBIO 1809 | char *buf; 1810 | 1811 | if (_IO_vasprintf (&buf, fmt, ap) < 0) 1812 | buf = NULL; 1813 | 1814 | __fxprintf (stream, ": %s", buf); 1815 | 1816 | free (buf); 1817 | #else 1818 | putc_unlocked (':', stream); 1819 | putc_unlocked (' ', stream); 1820 | 1821 | vfprintf (stream, fmt, ap); 1822 | #endif 1823 | 1824 | va_end (ap); 1825 | } 1826 | 1827 | if (errnum) 1828 | { 1829 | #if (defined _LIBC && defined USE_IN_LIBIO) || defined HAVE_STRERROR_R 1830 | char buf[200]; 1831 | #endif 1832 | #if defined _LIBC && defined USE_IN_LIBIO 1833 | __fxprintf (stream, ": %s", 1834 | strerror_r (errnum, buf, sizeof (buf))); 1835 | #else 1836 | putc_unlocked (':', stream); 1837 | putc_unlocked (' ', stream); 1838 | # ifdef HAVE_STRERROR_R 1839 | fputs (strerror_r (errnum, buf, sizeof (buf)), stream); 1840 | # else 1841 | fputs (strerror (errnum), stream); 1842 | # endif 1843 | #endif 1844 | } 1845 | 1846 | #ifdef USE_IN_LIBIO 1847 | if (_IO_fwide (stream, 0) > 0) 1848 | putwc_unlocked (L'\n', stream); 1849 | else 1850 | #endif 1851 | putc_unlocked ('\n', stream); 1852 | 1853 | #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE) 1854 | funlockfile (stream); 1855 | #endif 1856 | 1857 | if (status && (!state || !(state->flags & ARGP_NO_EXIT))) 1858 | exit (status); 1859 | } 1860 | } 1861 | } 1862 | -------------------------------------------------------------------------------- /argp-parse.c: -------------------------------------------------------------------------------- 1 | /* Hierarchial argument parsing, layered over getopt 2 | Copyright (C) 1995-2000, 2002, 2003, 2004 Free Software Foundation, Inc. 3 | This file is part of the GNU C Library. 4 | Written by Miles Bader . 5 | 6 | The GNU C Library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | The GNU C Library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with the GNU C Library; see the file COPYING.LIB. If 18 | not, see . 19 | 20 | Modified for uClibc by: Salvatore Cro 21 | */ 22 | 23 | #ifndef _WIN32 24 | #include 25 | #include 26 | #endif 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include "internal.h" 36 | 37 | #define _GETOPT_DATA_INITIALIZER { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 } 38 | #define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0') 39 | 40 | /* For communication from `getopt' to the caller. 41 | When `getopt' finds an option that takes an argument, 42 | the argument value is returned here. 43 | Also, when `ordering' is RETURN_IN_ORDER, 44 | each non-option ARGV-element is returned here. */ 45 | 46 | char *optarg; 47 | 48 | /* Index in ARGV of the next element to be scanned. 49 | This is used for communication to and from the caller 50 | and for communication between successive calls to `getopt'. 51 | On entry to `getopt', zero means this is the first call; initialize. 52 | When `getopt' returns -1, this is the index of the first of the 53 | non-option elements that the caller should itself scan. 54 | Otherwise, `optind' communicates from one call to the next 55 | how much of ARGV has been scanned so far. */ 56 | 57 | /* 1003.2 says this must be 1 before any call. */ 58 | int optind = 1; 59 | 60 | /* Callers store zero here to inhibit the error message 61 | for unrecognized options. */ 62 | 63 | int opterr = 1; 64 | 65 | /* Set to an option character which was unrecognized. 66 | This must be initialized on some systems to avoid linking in the 67 | system's own getopt implementation. */ 68 | 69 | int optopt = '?'; 70 | 71 | /* Exchange two adjacent subsequences of ARGV. 72 | One subsequence is elements [first_nonopt,last_nonopt) 73 | which contains all the non-options that have been skipped so far. 74 | The other is elements [last_nonopt,optind), which contains all 75 | the options processed since those non-options were skipped. 76 | `first_nonopt' and `last_nonopt' are relocated so that they describe 77 | the new indices of the non-options in ARGV after they are moved. */ 78 | static void exchange (char **argv, struct getopt_data *d) 79 | { 80 | int bottom = d->__first_nonopt; 81 | int middle = d->__last_nonopt; 82 | int top = d->optind; 83 | char *tem; 84 | 85 | /* Exchange the shorter segment with the far end of the longer segment. 86 | That puts the shorter segment into the right place. 87 | It leaves the longer segment in the right place overall, 88 | but it consists of two parts that need to be swapped next. */ 89 | 90 | while (top > middle && middle > bottom) 91 | { 92 | if (top - middle > middle - bottom) 93 | { 94 | /* Bottom segment is the short one. */ 95 | int len = middle - bottom; 96 | register int i; 97 | 98 | /* Swap it with the top part of the top segment. */ 99 | for (i = 0; i < len; i++) 100 | { 101 | tem = argv[bottom + i]; 102 | argv[bottom + i] = argv[top - (middle - bottom) + i]; 103 | argv[top - (middle - bottom) + i] = tem; 104 | } 105 | /* Exclude the moved bottom segment from further swapping. */ 106 | top -= len; 107 | } 108 | else 109 | { 110 | /* Top segment is the short one. */ 111 | int len = top - middle; 112 | register int i; 113 | 114 | /* Swap it with the bottom part of the bottom segment. */ 115 | for (i = 0; i < len; i++) 116 | { 117 | tem = argv[bottom + i]; 118 | argv[bottom + i] = argv[middle + i]; 119 | argv[middle + i] = tem; 120 | } 121 | /* Exclude the moved top segment from further swapping. */ 122 | bottom += len; 123 | } 124 | } 125 | 126 | /* Update records for the slots the non-options now occupy. */ 127 | 128 | d->__first_nonopt += (d->optind - d->__last_nonopt); 129 | d->__last_nonopt = d->optind; 130 | } 131 | 132 | /* Initialize the internal data when the first call is made. */ 133 | 134 | static const char * 135 | _getopt_initialize (const char *optstring, struct getopt_data *d) 136 | { 137 | /* Start processing options with ARGV-element 1 (since ARGV-element 0 138 | is the program name); the sequence of previously skipped 139 | non-option ARGV-elements is empty. */ 140 | 141 | d->__first_nonopt = d->__last_nonopt = d->optind; 142 | 143 | d->__nextchar = NULL; 144 | 145 | d->__posixly_correct = !!getenv ("POSIXLY_CORRECT"); 146 | 147 | /* Determine how to handle the ordering of options and nonoptions. */ 148 | 149 | if (optstring[0] == '-') 150 | { 151 | d->__ordering = RETURN_IN_ORDER; 152 | ++optstring; 153 | } 154 | else if (optstring[0] == '+') 155 | { 156 | d->__ordering = REQUIRE_ORDER; 157 | ++optstring; 158 | } 159 | else if (d->__posixly_correct) 160 | d->__ordering = REQUIRE_ORDER; 161 | else 162 | d->__ordering = PERMUTE; 163 | 164 | return optstring; 165 | } 166 | 167 | /* Scan elements of ARGV (whose length is ARGC) for option characters 168 | given in OPTSTRING. 169 | If an element of ARGV starts with '-', and is not exactly "-" or "--", 170 | then it is an option element. The characters of this element 171 | (aside from the initial '-') are option characters. If `getopt' 172 | is called repeatedly, it returns successively each of the option characters 173 | from each of the option elements. 174 | If `getopt' finds another option character, it returns that character, 175 | updating `optind' and `nextchar' so that the next call to `getopt' can 176 | resume the scan with the following option character or ARGV-element. 177 | If there are no more option characters, `getopt' returns -1. 178 | Then `optind' is the index in ARGV of the first ARGV-element 179 | that is not an option. (The ARGV-elements have been permuted 180 | so that those that are not options now come last.) 181 | OPTSTRING is a string containing the legitimate option characters. 182 | If an option character is seen that is not listed in OPTSTRING, 183 | return '?' after printing an error message. If you set `opterr' to 184 | zero, the error message is suppressed but we still return '?'. 185 | If a char in OPTSTRING is followed by a colon, that means it wants an arg, 186 | so the following text in the same ARGV-element, or the text of the following 187 | ARGV-element, is returned in `optarg'. Two colons mean an option that 188 | wants an optional arg; if there is text in the current ARGV-element, 189 | it is returned in `optarg', otherwise `optarg' is set to zero. 190 | If OPTSTRING starts with `-' or `+', it requests different methods of 191 | handling the non-option ARGV-elements. 192 | See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. 193 | Long-named options begin with `--' instead of `-'. 194 | Their names may be abbreviated as long as the abbreviation is unique 195 | or is an exact match for some defined option. If they have an 196 | argument, it follows the option name in the same ARGV-element, separated 197 | from the option name by a `=', or else the in next ARGV-element. 198 | When `getopt' finds a long-named option, it returns 0 if that option's 199 | `flag' field is nonzero, the value of the option's `val' field 200 | if the `flag' field is zero. 201 | The elements of ARGV aren't really const, because we permute them. 202 | But we pretend they're const in the prototype to be compatible 203 | with other systems. 204 | LONGOPTS is a vector of `struct option' terminated by an 205 | element containing a name which is zero. 206 | LONGIND returns the index in LONGOPT of the long-named option found. 207 | It is only valid when a long-named option has been found by the most 208 | recent call. 209 | If LONG_ONLY is nonzero, '-' as well as '--' can introduce 210 | long-named options. */ 211 | 212 | static int _getopt_internal_r (int argc, char *const *argv, 213 | const char *optstring, const struct option *longopts, 214 | int *longind, int long_only, struct getopt_data *d) 215 | { 216 | int print_errors = d->opterr; 217 | if (optstring[0] == ':') 218 | print_errors = 0; 219 | 220 | if (argc < 1) 221 | return -1; 222 | 223 | d->optarg = NULL; 224 | 225 | if (d->optind == 0 || !d->__initialized) 226 | { 227 | if (d->optind == 0) 228 | d->optind = 1; /* Don't scan ARGV[0], the program name. */ 229 | optstring = _getopt_initialize (optstring, d); 230 | d->__initialized = 1; 231 | } 232 | 233 | /* Test whether ARGV[optind] points to a non-option argument. 234 | Either it does not have option syntax, or there is an environment flag 235 | from the shell indicating it is not an option. The later information 236 | is only used when the used in the GNU libc. */ 237 | 238 | if (d->__nextchar == NULL || *d->__nextchar == '\0') 239 | { 240 | /* Advance to the next ARGV-element. */ 241 | 242 | /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 243 | moved back by the user (who may also have changed the arguments). */ 244 | if (d->__last_nonopt > d->optind) 245 | d->__last_nonopt = d->optind; 246 | if (d->__first_nonopt > d->optind) 247 | d->__first_nonopt = d->optind; 248 | 249 | if (d->__ordering == PERMUTE) 250 | { 251 | /* If we have just processed some options following some non-options, 252 | exchange them so that the options come first. */ 253 | 254 | if (d->__first_nonopt != d->__last_nonopt 255 | && d->__last_nonopt != d->optind) 256 | exchange ((char **) argv, d); 257 | else if (d->__last_nonopt != d->optind) 258 | d->__first_nonopt = d->optind; 259 | 260 | /* Skip any additional non-options 261 | and extend the range of non-options previously skipped. */ 262 | 263 | while (d->optind < argc && NONOPTION_P) 264 | d->optind++; 265 | d->__last_nonopt = d->optind; 266 | } 267 | 268 | /* The special ARGV-element `--' means premature end of options. 269 | Skip it like a null option, 270 | then exchange with previous non-options as if it were an option, 271 | then skip everything else like a non-option. */ 272 | 273 | if (d->optind != argc && !strcmp (argv[d->optind], "--")) 274 | { 275 | d->optind++; 276 | 277 | if (d->__first_nonopt != d->__last_nonopt 278 | && d->__last_nonopt != d->optind) 279 | exchange ((char **) argv, d); 280 | else if (d->__first_nonopt == d->__last_nonopt) 281 | d->__first_nonopt = d->optind; 282 | d->__last_nonopt = argc; 283 | 284 | d->optind = argc; 285 | } 286 | 287 | /* If we have done all the ARGV-elements, stop the scan 288 | and back over any non-options that we skipped and permuted. */ 289 | 290 | if (d->optind == argc) 291 | { 292 | /* Set the next-arg-index to point at the non-options 293 | that we previously skipped, so the caller will digest them. */ 294 | if (d->__first_nonopt != d->__last_nonopt) 295 | d->optind = d->__first_nonopt; 296 | return -1; 297 | } 298 | 299 | /* If we have come to a non-option and did not permute it, 300 | either stop the scan or describe it to the caller and pass it by. */ 301 | 302 | if (NONOPTION_P) 303 | { 304 | if (d->__ordering == REQUIRE_ORDER) 305 | return -1; 306 | d->optarg = argv[d->optind++]; 307 | return 1; 308 | } 309 | 310 | /* We have found another option-ARGV-element. 311 | Skip the initial punctuation. */ 312 | 313 | d->__nextchar = (argv[d->optind] + 1 314 | + (longopts != NULL && argv[d->optind][1] == '-')); 315 | } 316 | 317 | /* Decode the current option-ARGV-element. */ 318 | 319 | /* Check whether the ARGV-element is a long option. 320 | If long_only and the ARGV-element has the form "-f", where f is 321 | a valid short option, don't consider it an abbreviated form of 322 | a long option that starts with f. Otherwise there would be no 323 | way to give the -f short option. 324 | On the other hand, if there's a long option "fubar" and 325 | the ARGV-element is "-fu", do consider that an abbreviation of 326 | the long option, just like "--fu", and not "-f" with arg "u". 327 | This distinction seems to be the most useful approach. */ 328 | 329 | if (longopts != NULL 330 | && (argv[d->optind][1] == '-' 331 | || (long_only && (argv[d->optind][2] 332 | || !strchr (optstring, argv[d->optind][1]))))) 333 | { 334 | char *nameend; 335 | const struct option *p; 336 | const struct option *pfound = NULL; 337 | int exact = 0; 338 | int ambig = 0; 339 | int indfound = -1; 340 | int option_index; 341 | 342 | for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++) 343 | /* Do nothing. */ ; 344 | 345 | /* Test all long options for either exact match 346 | or abbreviated matches. */ 347 | for (p = longopts, option_index = 0; p->name; p++, option_index++) 348 | if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar)) 349 | { 350 | if ((unsigned int) (nameend - d->__nextchar) 351 | == (unsigned int) strlen (p->name)) 352 | { 353 | /* Exact match found. */ 354 | pfound = p; 355 | indfound = option_index; 356 | exact = 1; 357 | break; 358 | } 359 | else if (pfound == NULL) 360 | { 361 | /* First nonexact match found. */ 362 | pfound = p; 363 | indfound = option_index; 364 | } 365 | else if (long_only 366 | || pfound->has_arg != p->has_arg 367 | || pfound->flag != p->flag 368 | || pfound->val != p->val) 369 | /* Second or later nonexact match found. */ 370 | ambig = 1; 371 | } 372 | 373 | if (ambig && !exact) 374 | { 375 | if (print_errors) 376 | { 377 | fprintf (stderr, "%s: option `%s' is ambiguous\n", 378 | argv[0], argv[d->optind]); 379 | } 380 | d->__nextchar += strlen (d->__nextchar); 381 | d->optind++; 382 | d->optopt = 0; 383 | return '?'; 384 | } 385 | 386 | if (pfound != NULL) 387 | { 388 | option_index = indfound; 389 | d->optind++; 390 | if (*nameend) 391 | { 392 | /* Don't test has_arg with >, because some C compilers don't 393 | allow it to be used on enums. */ 394 | if (pfound->has_arg) 395 | d->optarg = nameend + 1; 396 | else 397 | { 398 | if (print_errors) 399 | { 400 | if (argv[d->optind - 1][1] == '-') 401 | { 402 | /* --option */ 403 | fprintf (stderr, "\ 404 | %s: option `--%s' doesn't allow an argument\n", 405 | argv[0], pfound->name); 406 | } 407 | else 408 | { 409 | /* +option or -option */ 410 | fprintf (stderr, "\ 411 | %s: option `%c%s' doesn't allow an argument\n", 412 | argv[0], argv[d->optind - 1][0], 413 | pfound->name); 414 | } 415 | 416 | } 417 | 418 | d->__nextchar += strlen (d->__nextchar); 419 | 420 | d->optopt = pfound->val; 421 | return '?'; 422 | } 423 | } 424 | else if (pfound->has_arg == 1) 425 | { 426 | if (d->optind < argc) 427 | d->optarg = argv[d->optind++]; 428 | else 429 | { 430 | if (print_errors) 431 | { 432 | fprintf (stderr, 433 | "%s: option `%s' requires an argument\n", 434 | argv[0], argv[d->optind - 1]); 435 | } 436 | d->__nextchar += strlen (d->__nextchar); 437 | d->optopt = pfound->val; 438 | return optstring[0] == ':' ? ':' : '?'; 439 | } 440 | } 441 | d->__nextchar += strlen (d->__nextchar); 442 | if (longind != NULL) 443 | *longind = option_index; 444 | if (pfound->flag) 445 | { 446 | *(pfound->flag) = pfound->val; 447 | return 0; 448 | } 449 | return pfound->val; 450 | } 451 | 452 | /* Can't find it as a long option. If this is not getopt_long_only, 453 | or the option starts with '--' or is not a valid short 454 | option, then it's an error. 455 | Otherwise interpret it as a short option. */ 456 | if (!long_only || argv[d->optind][1] == '-' 457 | || strchr (optstring, *d->__nextchar) == NULL) 458 | { 459 | if (print_errors) 460 | { 461 | 462 | if (argv[d->optind][1] == '-') 463 | { 464 | /* --option */ 465 | fprintf (stderr, "%s: unrecognized option `--%s'\n", 466 | argv[0], d->__nextchar); 467 | } 468 | else 469 | { 470 | /* +option or -option */ 471 | fprintf (stderr, "%s: unrecognized option `%c%s'\n", 472 | argv[0], argv[d->optind][0], d->__nextchar); 473 | } 474 | 475 | } 476 | d->__nextchar = (char *) ""; 477 | d->optind++; 478 | d->optopt = 0; 479 | return '?'; 480 | } 481 | } 482 | 483 | /* Look at and handle the next short option-character. */ 484 | 485 | { 486 | char c = *d->__nextchar++; 487 | char *temp = strchr (optstring, c); 488 | 489 | /* Increment `optind' when we start to process its last character. */ 490 | if (*d->__nextchar == '\0') 491 | ++d->optind; 492 | 493 | if (temp == NULL || c == ':') 494 | { 495 | if (print_errors) 496 | { 497 | if (d->__posixly_correct) 498 | { 499 | /* 1003.2 specifies the format of this message. */ 500 | fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c); 501 | } 502 | else 503 | { 504 | fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c); 505 | } 506 | 507 | } 508 | d->optopt = c; 509 | return '?'; 510 | } 511 | if (temp[1] == ':') 512 | { 513 | if (temp[2] == ':') 514 | { 515 | /* This is an option that accepts an argument optionally. */ 516 | if (*d->__nextchar != '\0') 517 | { 518 | d->optarg = d->__nextchar; 519 | d->optind++; 520 | } 521 | else 522 | d->optarg = NULL; 523 | d->__nextchar = NULL; 524 | } 525 | else 526 | { 527 | /* This is an option that requires an argument. */ 528 | if (*d->__nextchar != '\0') 529 | { 530 | d->optarg = d->__nextchar; 531 | /* If we end this ARGV-element by taking the rest as an arg, 532 | we must advance to the next element now. */ 533 | d->optind++; 534 | } 535 | else if (d->optind == argc) 536 | { 537 | if (print_errors) 538 | { 539 | /* 1003.2 specifies the format of this message. */ 540 | fprintf (stderr, 541 | "%s: option requires an argument -- %c\n", 542 | argv[0], c); 543 | } 544 | d->optopt = c; 545 | if (optstring[0] == ':') 546 | c = ':'; 547 | else 548 | c = '?'; 549 | } 550 | else 551 | /* We already incremented `optind' once; 552 | increment it again when taking next ARGV-elt as argument. */ 553 | d->optarg = argv[d->optind++]; 554 | d->__nextchar = NULL; 555 | } 556 | } 557 | return c; 558 | } 559 | } 560 | 561 | static int _getopt_long_r (int argc, char *const *argv, const char *options, 562 | const struct option *long_options, int *opt_index, 563 | struct getopt_data *d) 564 | { 565 | return _getopt_internal_r (argc, argv, options, long_options, opt_index, 566 | 0, d); 567 | } 568 | 569 | static int _getopt_long_only_r (int argc, char *const *argv, const char *options, 570 | const struct option *long_options, int *opt_index, 571 | struct getopt_data *d) 572 | { 573 | return _getopt_internal_r (argc, argv, options, long_options, opt_index, 1, d); 574 | } 575 | 576 | #undef dgettext 577 | #define dgettext(domain, msgid) (msgid ? dcgettext(domain, msgid, LC_MESSAGES) : NULL) 578 | 579 | /* Getopt return values. */ 580 | #define KEY_END (-1) /* The end of the options. */ 581 | #define KEY_ARG 1 /* A non-option argument. */ 582 | #define KEY_ERR '?' /* An error parsing the options. */ 583 | 584 | /* The meta-argument used to prevent any further arguments being interpreted 585 | as options. */ 586 | #define QUOTE "--" 587 | 588 | /* The number of bits we steal in a long-option value for our own use. */ 589 | #define GROUP_BITS CHAR_BIT 590 | 591 | /* The number of bits available for the user value. */ 592 | #define USER_BITS ((sizeof ((struct option *)0)->val * CHAR_BIT) - GROUP_BITS) 593 | #define USER_MASK ((1 << USER_BITS) - 1) 594 | 595 | /* EZ alias for ARGP_ERR_UNKNOWN. */ 596 | #define EBADKEY ARGP_ERR_UNKNOWN 597 | 598 | /* Default options. */ 599 | 600 | /* When argp is given the --HANG switch, _ARGP_HANG is set and argp will sleep 601 | for one second intervals, decrementing _ARGP_HANG until it's zero. Thus 602 | you can force the program to continue by attaching a debugger and setting 603 | it to 0 yourself. */ 604 | static volatile int _argp_hang; 605 | 606 | #define OPT_PROGNAME -2 607 | #define OPT_USAGE -3 608 | #define OPT_HANG -4 609 | 610 | static const struct argp_option argp_default_options[] = 611 | { 612 | {"help", '?', 0, 0, "Give this help list", -1}, 613 | {"usage", OPT_USAGE, 0, 0, "Give a short usage message"}, 614 | {"program-name",OPT_PROGNAME,"NAME", OPTION_HIDDEN, "Set the program name"}, 615 | {"HANG", OPT_HANG, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN, 616 | "Hang for SECS seconds (default 3600)"}, 617 | {0, 0} 618 | }; 619 | 620 | static error_t 621 | argp_default_parser (int key, char *arg, struct argp_state *state) 622 | { 623 | switch (key) 624 | { 625 | case '?': 626 | argp_state_help (state, state->out_stream, ARGP_HELP_STD_HELP); 627 | break; 628 | case OPT_USAGE: 629 | argp_state_help (state, state->out_stream, 630 | ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK); 631 | break; 632 | 633 | case OPT_PROGNAME: /* Set the program name. */ 634 | #if defined _LIBC && defined(__UCLIBC_HAS_PROGRAM_INVOCATION_NAME__) 635 | program_invocation_name = arg; 636 | #endif 637 | /* [Note that some systems only have PROGRAM_INVOCATION_SHORT_NAME (aka 638 | __PROGNAME), in which case, PROGRAM_INVOCATION_NAME is just defined 639 | to be that, so we have to be a bit careful here.] */ 640 | 641 | /* Update what we use for messages. */ 642 | state->name = strrchr (arg, '/'); 643 | if (state->name) 644 | state->name++; 645 | else 646 | state->name = arg; 647 | 648 | #if defined _LIBC && defined(__UCLIBC_HAS_PROGRAM_INVOCATION_NAME__) 649 | program_invocation_short_name = state->name; 650 | #endif 651 | 652 | if ((state->flags & (ARGP_PARSE_ARGV0 | ARGP_NO_ERRS)) 653 | == ARGP_PARSE_ARGV0) 654 | /* Update what getopt uses too. */ 655 | state->argv[0] = arg; 656 | 657 | break; 658 | 659 | case OPT_HANG: 660 | _argp_hang = atoi (arg ? arg : "3600"); 661 | while (_argp_hang-- > 0) 662 | sleep (1); 663 | break; 664 | 665 | default: 666 | return EBADKEY; 667 | } 668 | return 0; 669 | } 670 | 671 | static const struct argp argp_default_argp = 672 | {argp_default_options, &argp_default_parser, NULL, NULL, NULL, NULL, "libc"}; 673 | 674 | 675 | static const struct argp_option argp_version_options[] = 676 | { 677 | {"version", 'V', 0, 0, "Print program version", -1}, 678 | {0, 0} 679 | }; 680 | 681 | static error_t 682 | argp_version_parser (int key, char *arg, struct argp_state *state) 683 | { 684 | switch (key) 685 | { 686 | case 'V': 687 | if (argp_program_version_hook) 688 | (*argp_program_version_hook) (state->out_stream, state); 689 | else if (argp_program_version) 690 | fprintf (state->out_stream, "%s\n", argp_program_version); 691 | else 692 | argp_error (state, dgettext (state->root_argp->argp_domain, 693 | "(PROGRAM ERROR) No version known!?")); 694 | if (! (state->flags & ARGP_NO_EXIT)) 695 | exit (0); 696 | break; 697 | default: 698 | return EBADKEY; 699 | } 700 | return 0; 701 | } 702 | 703 | static const struct argp argp_version_argp = 704 | {argp_version_options, &argp_version_parser, NULL, NULL, NULL, NULL, "libc"}; 705 | 706 | /* Returns the offset into the getopt long options array LONG_OPTIONS of a 707 | long option with called NAME, or -1 if none is found. Passing NULL as 708 | NAME will return the number of options. */ 709 | static int 710 | find_long_option (struct option *long_options, const char *name) 711 | { 712 | struct option *l = long_options; 713 | while (l->name != NULL) 714 | if (name != NULL && strcmp (l->name, name) == 0) 715 | return l - long_options; 716 | else 717 | l++; 718 | if (name == NULL) 719 | return l - long_options; 720 | else 721 | return -1; 722 | } 723 | 724 | 725 | /* Call GROUP's parser with KEY and ARG, swapping any group-specific info 726 | from STATE before calling, and back into state afterwards. If GROUP has 727 | no parser, EBADKEY is returned. */ 728 | static error_t 729 | group_parse (struct group *group, struct argp_state *state, int key, char *arg) 730 | { 731 | if (group->parser) 732 | { 733 | error_t err; 734 | state->hook = group->hook; 735 | state->input = group->input; 736 | state->child_inputs = group->child_inputs; 737 | state->arg_num = group->args_processed; 738 | err = (*group->parser)(key, arg, state); 739 | group->hook = state->hook; 740 | return err; 741 | } 742 | else 743 | return EBADKEY; 744 | } 745 | 746 | /* Converts all options in ARGP (which is put in GROUP) and ancestors 747 | into getopt options stored in SHORT_OPTS and LONG_OPTS; SHORT_END and 748 | CVT->LONG_END are the points at which new options are added. Returns the 749 | next unused group entry. CVT holds state used during the conversion. */ 750 | static struct group * 751 | convert_options (const struct argp *argp, 752 | struct group *parent, unsigned parent_index, 753 | struct group *group, struct parser_convert_state *cvt) 754 | { 755 | /* REAL is the most recent non-alias value of OPT. */ 756 | const struct argp_option *real = argp->options; 757 | const struct argp_child *children = argp->children; 758 | 759 | if (real || argp->parser) 760 | { 761 | const struct argp_option *opt; 762 | 763 | if (real) 764 | for (opt = real; !_option_is_end (opt); opt++) 765 | { 766 | if (! (opt->flags & OPTION_ALIAS)) 767 | /* OPT isn't an alias, so we can use values from it. */ 768 | real = opt; 769 | 770 | if (! (real->flags & OPTION_DOC)) 771 | /* A real option (not just documentation). */ 772 | { 773 | if (_option_is_short (opt)) 774 | /* OPT can be used as a short option. */ 775 | { 776 | *cvt->short_end++ = opt->key; 777 | if (real->arg) 778 | { 779 | *cvt->short_end++ = ':'; 780 | if (real->flags & OPTION_ARG_OPTIONAL) 781 | *cvt->short_end++ = ':'; 782 | } 783 | *cvt->short_end = '\0'; /* keep 0 terminated */ 784 | } 785 | 786 | if (opt->name 787 | && find_long_option (cvt->parser->long_opts, opt->name) < 0) 788 | /* OPT can be used as a long option. */ 789 | { 790 | cvt->long_end->name = opt->name; 791 | cvt->long_end->has_arg = 792 | (real->arg 793 | ? (real->flags & OPTION_ARG_OPTIONAL 794 | ? optional_argument 795 | : required_argument) 796 | : no_argument); 797 | cvt->long_end->flag = 0; 798 | /* we add a disambiguating code to all the user's 799 | values (which is removed before we actually call 800 | the function to parse the value); this means that 801 | the user loses use of the high 8 bits in all his 802 | values (the sign of the lower bits is preserved 803 | however)... */ 804 | cvt->long_end->val = 805 | ((opt->key | real->key) & USER_MASK) 806 | + (((group - cvt->parser->groups) + 1) << USER_BITS); 807 | 808 | /* Keep the LONG_OPTS list terminated. */ 809 | (++cvt->long_end)->name = NULL; 810 | } 811 | } 812 | } 813 | 814 | group->parser = argp->parser; 815 | group->argp = argp; 816 | group->short_end = cvt->short_end; 817 | group->args_processed = 0; 818 | group->parent = parent; 819 | group->parent_index = parent_index; 820 | group->input = 0; 821 | group->hook = 0; 822 | group->child_inputs = 0; 823 | 824 | if (children) 825 | /* Assign GROUP's CHILD_INPUTS field some space from 826 | CVT->child_inputs_end.*/ 827 | { 828 | unsigned num_children = 0; 829 | while (children[num_children].argp) 830 | num_children++; 831 | group->child_inputs = cvt->child_inputs_end; 832 | cvt->child_inputs_end += num_children; 833 | } 834 | 835 | parent = group++; 836 | } 837 | else 838 | parent = 0; 839 | 840 | if (children) 841 | { 842 | unsigned index = 0; 843 | while (children->argp) 844 | group = 845 | convert_options (children++->argp, parent, index++, group, cvt); 846 | } 847 | 848 | return group; 849 | } 850 | 851 | /* Find the merged set of getopt options, with keys appropiately prefixed. */ 852 | static void 853 | parser_convert (struct parser *parser, const struct argp *argp, int flags) 854 | { 855 | struct parser_convert_state cvt; 856 | 857 | cvt.parser = parser; 858 | cvt.short_end = parser->short_opts; 859 | cvt.long_end = parser->long_opts; 860 | cvt.child_inputs_end = parser->child_inputs; 861 | 862 | if (flags & ARGP_IN_ORDER) 863 | *cvt.short_end++ = '-'; 864 | else if (flags & ARGP_NO_ARGS) 865 | *cvt.short_end++ = '+'; 866 | *cvt.short_end = '\0'; 867 | 868 | cvt.long_end->name = NULL; 869 | 870 | parser->argp = argp; 871 | 872 | if (argp) 873 | parser->egroup = convert_options (argp, 0, 0, parser->groups, &cvt); 874 | else 875 | parser->egroup = parser->groups; /* No parsers at all! */ 876 | } 877 | 878 | /* For ARGP, increments the NUM_GROUPS field in SZS by the total number of 879 | argp structures descended from it, and the SHORT_LEN & LONG_LEN fields by 880 | the maximum lengths of the resulting merged getopt short options string and 881 | long-options array, respectively. */ 882 | static void 883 | calc_sizes (const struct argp *argp, struct parser_sizes *szs) 884 | { 885 | const struct argp_child *child = argp->children; 886 | const struct argp_option *opt = argp->options; 887 | 888 | if (opt || argp->parser) 889 | { 890 | szs->num_groups++; 891 | if (opt) 892 | { 893 | int num_opts = 0; 894 | while (!_option_is_end (opt++)) 895 | num_opts++; 896 | szs->short_len += num_opts * 3; /* opt + up to 2 `:'s */ 897 | szs->long_len += num_opts; 898 | } 899 | } 900 | 901 | if (child) 902 | while (child->argp) 903 | { 904 | calc_sizes ((child++)->argp, szs); 905 | szs->num_child_inputs++; 906 | } 907 | } 908 | 909 | 910 | extern char * __argp_short_program_name (void); 911 | /* Initializes PARSER to parse ARGP in a manner described by FLAGS. */ 912 | static error_t 913 | parser_init (struct parser *parser, const struct argp *argp, 914 | int argc, char **argv, int flags, void *input) 915 | { 916 | error_t err = 0; 917 | struct group *group; 918 | struct parser_sizes szs; 919 | struct getopt_data opt_data = _GETOPT_DATA_INITIALIZER; 920 | 921 | szs.short_len = (flags & ARGP_NO_ARGS) ? 0 : 1; 922 | szs.long_len = 0; 923 | szs.num_groups = 0; 924 | szs.num_child_inputs = 0; 925 | 926 | if (argp) 927 | calc_sizes (argp, &szs); 928 | 929 | /* Lengths of the various bits of storage used by PARSER. */ 930 | #define GLEN (szs.num_groups + 1) * sizeof (struct group) 931 | #define CLEN (szs.num_child_inputs * sizeof (void *)) 932 | #define LLEN ((szs.long_len + 1) * sizeof (struct option)) 933 | #define SLEN (szs.short_len + 1) 934 | 935 | parser->storage = malloc (GLEN + CLEN + LLEN + SLEN); 936 | if (! parser->storage) 937 | return ENOMEM; 938 | 939 | parser->groups = parser->storage; 940 | parser->child_inputs = parser->storage + GLEN; 941 | parser->long_opts = parser->storage + GLEN + CLEN; 942 | parser->short_opts = parser->storage + GLEN + CLEN + LLEN; 943 | parser->opt_data = opt_data; 944 | 945 | memset (parser->child_inputs, 0, szs.num_child_inputs * sizeof (void *)); 946 | parser_convert (parser, argp, flags); 947 | 948 | memset (&parser->state, 0, sizeof (struct argp_state)); 949 | parser->state.root_argp = parser->argp; 950 | parser->state.argc = argc; 951 | parser->state.argv = argv; 952 | parser->state.flags = flags; 953 | parser->state.err_stream = stderr; 954 | parser->state.out_stream = stdout; 955 | parser->state.next = 0; /* Tell getopt to initialize. */ 956 | parser->state.pstate = parser; 957 | 958 | parser->try_getopt = 1; 959 | 960 | /* Call each parser for the first time, giving it a chance to propagate 961 | values to child parsers. */ 962 | if (parser->groups < parser->egroup) 963 | parser->groups->input = input; 964 | for (group = parser->groups; 965 | group < parser->egroup && (!err || err == EBADKEY); 966 | group++) 967 | { 968 | if (group->parent) 969 | /* If a child parser, get the initial input value from the parent. */ 970 | group->input = group->parent->child_inputs[group->parent_index]; 971 | 972 | if (!group->parser 973 | && group->argp->children && group->argp->children->argp) 974 | /* For the special case where no parsing function is supplied for an 975 | argp, propagate its input to its first child, if any (this just 976 | makes very simple wrapper argps more convenient). */ 977 | group->child_inputs[0] = group->input; 978 | 979 | err = group_parse (group, &parser->state, ARGP_KEY_INIT, 0); 980 | } 981 | if (err == EBADKEY) 982 | err = 0; /* Some parser didn't understand. */ 983 | 984 | if (err) 985 | return err; 986 | 987 | if (parser->state.flags & ARGP_NO_ERRS) 988 | { 989 | parser->opt_data.opterr = 0; 990 | if (parser->state.flags & ARGP_PARSE_ARGV0) 991 | /* getopt always skips ARGV[0], so we have to fake it out. As long 992 | as OPTERR is 0, then it shouldn't actually try to access it. */ 993 | parser->state.argv--, parser->state.argc++; 994 | } 995 | else 996 | parser->opt_data.opterr = 1; /* Print error messages. */ 997 | 998 | if (parser->state.argv == argv && argv[0]) 999 | /* There's an argv[0]; use it for messages. */ 1000 | { 1001 | char *short_name = strrchr (argv[0], '/'); 1002 | parser->state.name = short_name ? short_name + 1 : argv[0]; 1003 | } 1004 | else 1005 | parser->state.name = __argp_short_program_name (); 1006 | 1007 | return 0; 1008 | } 1009 | 1010 | /* Free any storage consumed by PARSER (but not PARSER itself). */ 1011 | static error_t 1012 | parser_finalize (struct parser *parser, 1013 | error_t err, int arg_ebadkey, int *end_index) 1014 | { 1015 | struct group *group; 1016 | 1017 | if (err == EBADKEY && arg_ebadkey) 1018 | /* Suppress errors generated by unparsed arguments. */ 1019 | err = 0; 1020 | 1021 | if (! err) 1022 | { 1023 | if (parser->state.next == parser->state.argc) 1024 | /* We successfully parsed all arguments! Call all the parsers again, 1025 | just a few more times... */ 1026 | { 1027 | for (group = parser->groups; 1028 | group < parser->egroup && (!err || err==EBADKEY); 1029 | group++) 1030 | if (group->args_processed == 0) 1031 | err = group_parse (group, &parser->state, ARGP_KEY_NO_ARGS, 0); 1032 | for (group = parser->egroup - 1; 1033 | group >= parser->groups && (!err || err==EBADKEY); 1034 | group--) 1035 | err = group_parse (group, &parser->state, ARGP_KEY_END, 0); 1036 | 1037 | if (err == EBADKEY) 1038 | err = 0; /* Some parser didn't understand. */ 1039 | 1040 | /* Tell the user that all arguments are parsed. */ 1041 | if (end_index) 1042 | *end_index = parser->state.next; 1043 | } 1044 | else if (end_index) 1045 | /* Return any remaining arguments to the user. */ 1046 | *end_index = parser->state.next; 1047 | else 1048 | /* No way to return the remaining arguments, they must be bogus. */ 1049 | { 1050 | if (!(parser->state.flags & ARGP_NO_ERRS) 1051 | && parser->state.err_stream) 1052 | fprintf (parser->state.err_stream, 1053 | dgettext (parser->argp->argp_domain, 1054 | "%s: Too many arguments\n"), 1055 | parser->state.name); 1056 | err = EBADKEY; 1057 | } 1058 | } 1059 | 1060 | /* Okay, we're all done, with either an error or success; call the parsers 1061 | to indicate which one. */ 1062 | 1063 | if (err) 1064 | { 1065 | /* Maybe print an error message. */ 1066 | if (err == EBADKEY) 1067 | /* An appropriate message describing what the error was should have 1068 | been printed earlier. */ 1069 | argp_state_help (&parser->state, parser->state.err_stream, 1070 | ARGP_HELP_STD_ERR); 1071 | 1072 | /* Since we didn't exit, give each parser an error indication. */ 1073 | for (group = parser->groups; group < parser->egroup; group++) 1074 | group_parse (group, &parser->state, ARGP_KEY_ERROR, 0); 1075 | } 1076 | else 1077 | /* Notify parsers of success, and propagate back values from parsers. */ 1078 | { 1079 | /* We pass over the groups in reverse order so that child groups are 1080 | given a chance to do there processing before passing back a value to 1081 | the parent. */ 1082 | for (group = parser->egroup - 1 1083 | ; group >= parser->groups && (!err || err == EBADKEY) 1084 | ; group--) 1085 | err = group_parse (group, &parser->state, ARGP_KEY_SUCCESS, 0); 1086 | if (err == EBADKEY) 1087 | err = 0; /* Some parser didn't understand. */ 1088 | } 1089 | 1090 | /* Call parsers once more, to do any final cleanup. Errors are ignored. */ 1091 | for (group = parser->egroup - 1; group >= parser->groups; group--) 1092 | group_parse (group, &parser->state, ARGP_KEY_FINI, 0); 1093 | 1094 | if (err == EBADKEY) 1095 | err = EINVAL; 1096 | 1097 | free (parser->storage); 1098 | 1099 | return err; 1100 | } 1101 | 1102 | /* Call the user parsers to parse the non-option argument VAL, at the current 1103 | position, returning any error. The state NEXT pointer is assumed to have 1104 | been adjusted (by getopt) to point after this argument; this function will 1105 | adjust it correctly to reflect however many args actually end up being 1106 | consumed. */ 1107 | static error_t 1108 | parser_parse_arg (struct parser *parser, char *val) 1109 | { 1110 | /* Save the starting value of NEXT, first adjusting it so that the arg 1111 | we're parsing is again the front of the arg vector. */ 1112 | int index = --parser->state.next; 1113 | error_t err = EBADKEY; 1114 | struct group *group; 1115 | int key = 0; /* Which of ARGP_KEY_ARG[S] we used. */ 1116 | 1117 | /* Try to parse the argument in each parser. */ 1118 | for (group = parser->groups 1119 | ; group < parser->egroup && err == EBADKEY 1120 | ; group++) 1121 | { 1122 | parser->state.next++; /* For ARGP_KEY_ARG, consume the arg. */ 1123 | key = ARGP_KEY_ARG; 1124 | err = group_parse (group, &parser->state, key, val); 1125 | 1126 | if (err == EBADKEY) 1127 | /* This parser doesn't like ARGP_KEY_ARG; try ARGP_KEY_ARGS instead. */ 1128 | { 1129 | parser->state.next--; /* For ARGP_KEY_ARGS, put back the arg. */ 1130 | key = ARGP_KEY_ARGS; 1131 | err = group_parse (group, &parser->state, key, 0); 1132 | } 1133 | } 1134 | 1135 | if (! err) 1136 | { 1137 | if (key == ARGP_KEY_ARGS) 1138 | /* The default for ARGP_KEY_ARGS is to assume that if NEXT isn't 1139 | changed by the user, *all* arguments should be considered 1140 | consumed. */ 1141 | parser->state.next = parser->state.argc; 1142 | 1143 | if (parser->state.next > index) 1144 | /* Remember that we successfully processed a non-option 1145 | argument -- but only if the user hasn't gotten tricky and set 1146 | the clock back. */ 1147 | (--group)->args_processed += (parser->state.next - index); 1148 | else 1149 | /* The user wants to reparse some args, give getopt another try. */ 1150 | parser->try_getopt = 1; 1151 | } 1152 | 1153 | return err; 1154 | } 1155 | 1156 | /* Call the user parsers to parse the option OPT, with argument VAL, at the 1157 | current position, returning any error. */ 1158 | static error_t 1159 | parser_parse_opt (struct parser *parser, int opt, char *val) 1160 | { 1161 | /* The group key encoded in the high bits; 0 for short opts or 1162 | group_number + 1 for long opts. */ 1163 | int group_key = opt >> USER_BITS; 1164 | error_t err = EBADKEY; 1165 | 1166 | if (group_key == 0) 1167 | /* A short option. By comparing OPT's position in SHORT_OPTS to the 1168 | various starting positions in each group's SHORT_END field, we can 1169 | determine which group OPT came from. */ 1170 | { 1171 | struct group *group; 1172 | char *short_index = strchr (parser->short_opts, opt); 1173 | 1174 | if (short_index) 1175 | for (group = parser->groups; group < parser->egroup; group++) 1176 | if (group->short_end > short_index) 1177 | { 1178 | err = group_parse (group, &parser->state, opt, 1179 | parser->opt_data.optarg); 1180 | break; 1181 | } 1182 | } 1183 | else 1184 | /* A long option. We use shifts instead of masking for extracting 1185 | the user value in order to preserve the sign. */ 1186 | err = 1187 | group_parse (&parser->groups[group_key - 1], &parser->state, 1188 | (opt << GROUP_BITS) >> GROUP_BITS, 1189 | parser->opt_data.optarg); 1190 | 1191 | if (err == EBADKEY) 1192 | /* At least currently, an option not recognized is an error in the 1193 | parser, because we pre-compute which parser is supposed to deal 1194 | with each option. */ 1195 | { 1196 | static const char bad_key_err[] = 1197 | "(PROGRAM ERROR) Option should have been recognized!?"; 1198 | if (group_key == 0) 1199 | argp_error (&parser->state, "-%c: %s", opt, 1200 | dgettext (parser->argp->argp_domain, bad_key_err)); 1201 | else 1202 | { 1203 | struct option *long_opt = parser->long_opts; 1204 | while (long_opt->val != opt && long_opt->name) 1205 | long_opt++; 1206 | argp_error (&parser->state, "--%s: %s", 1207 | long_opt->name ? long_opt->name : "???", 1208 | dgettext (parser->argp->argp_domain, bad_key_err)); 1209 | } 1210 | } 1211 | 1212 | return err; 1213 | } 1214 | 1215 | /* Parse the next argument in PARSER (as indicated by PARSER->state.next). 1216 | Any error from the parsers is returned, and *ARGP_EBADKEY indicates 1217 | whether a value of EBADKEY is due to an unrecognized argument (which is 1218 | generally not fatal). */ 1219 | static error_t 1220 | parser_parse_next (struct parser *parser, int *arg_ebadkey) 1221 | { 1222 | int opt; 1223 | error_t err = 0; 1224 | 1225 | if (parser->state.quoted && parser->state.next < parser->state.quoted) 1226 | /* The next argument pointer has been moved to before the quoted 1227 | region, so pretend we never saw the quoting `--', and give getopt 1228 | another chance. If the user hasn't removed it, getopt will just 1229 | process it again. */ 1230 | parser->state.quoted = 0; 1231 | 1232 | if (parser->try_getopt && !parser->state.quoted) 1233 | /* Give getopt a chance to parse this. */ 1234 | { 1235 | /* Put it back in OPTIND for getopt. */ 1236 | parser->opt_data.optind = parser->state.next; 1237 | /* Distinguish KEY_ERR from a real option. */ 1238 | parser->opt_data.optopt = KEY_END; 1239 | if (parser->state.flags & ARGP_LONG_ONLY) 1240 | opt = _getopt_long_only_r (parser->state.argc, parser->state.argv, 1241 | parser->short_opts, parser->long_opts, 0, 1242 | &parser->opt_data); 1243 | else 1244 | opt = _getopt_long_r (parser->state.argc, parser->state.argv, 1245 | parser->short_opts, parser->long_opts, 0, 1246 | &parser->opt_data); 1247 | /* And see what getopt did. */ 1248 | parser->state.next = parser->opt_data.optind; 1249 | 1250 | if (opt == KEY_END) 1251 | /* Getopt says there are no more options, so stop using 1252 | getopt; we'll continue if necessary on our own. */ 1253 | { 1254 | parser->try_getopt = 0; 1255 | if (parser->state.next > 1 1256 | && strcmp (parser->state.argv[parser->state.next - 1], QUOTE) 1257 | == 0) 1258 | /* Not only is this the end of the options, but it's a 1259 | `quoted' region, which may have args that *look* like 1260 | options, so we definitely shouldn't try to use getopt past 1261 | here, whatever happens. */ 1262 | parser->state.quoted = parser->state.next; 1263 | } 1264 | else if (opt == KEY_ERR && parser->opt_data.optopt != KEY_END) 1265 | /* KEY_ERR can have the same value as a valid user short 1266 | option, but in the case of a real error, getopt sets OPTOPT 1267 | to the offending character, which can never be KEY_END. */ 1268 | { 1269 | *arg_ebadkey = 0; 1270 | return EBADKEY; 1271 | } 1272 | } 1273 | else 1274 | opt = KEY_END; 1275 | 1276 | if (opt == KEY_END) 1277 | { 1278 | /* We're past what getopt considers the options. */ 1279 | if (parser->state.next >= parser->state.argc 1280 | || (parser->state.flags & ARGP_NO_ARGS)) 1281 | /* Indicate that we're done. */ 1282 | { 1283 | *arg_ebadkey = 1; 1284 | return EBADKEY; 1285 | } 1286 | else 1287 | /* A non-option arg; simulate what getopt might have done. */ 1288 | { 1289 | opt = KEY_ARG; 1290 | parser->opt_data.optarg = parser->state.argv[parser->state.next++]; 1291 | } 1292 | } 1293 | 1294 | if (opt == KEY_ARG) 1295 | /* A non-option argument; try each parser in turn. */ 1296 | err = parser_parse_arg (parser, parser->opt_data.optarg); 1297 | else 1298 | err = parser_parse_opt (parser, opt, parser->opt_data.optarg); 1299 | 1300 | if (err == EBADKEY) 1301 | *arg_ebadkey = (opt == KEY_END || opt == KEY_ARG); 1302 | 1303 | return err; 1304 | } 1305 | 1306 | /* Parse the options strings in ARGC & ARGV according to the argp in ARGP. 1307 | FLAGS is one of the ARGP_ flags above. If END_INDEX is non-NULL, the 1308 | index in ARGV of the first unparsed option is returned in it. If an 1309 | unknown option is present, EINVAL is returned; if some parser routine 1310 | returned a non-zero value, it is returned; otherwise 0 is returned. */ 1311 | error_t 1312 | argp_parse (const struct argp *argp, int argc, char **argv, unsigned flags, 1313 | int *end_index, void *input) 1314 | { 1315 | error_t err; 1316 | struct parser parser; 1317 | 1318 | /* If true, then err == EBADKEY is a result of a non-option argument failing 1319 | to be parsed (which in some cases isn't actually an error). */ 1320 | int arg_ebadkey = 0; 1321 | 1322 | if (! (flags & ARGP_NO_HELP)) 1323 | /* Add our own options. */ 1324 | { 1325 | struct argp_child *child = alloca (4 * sizeof (struct argp_child)); 1326 | struct argp *top_argp = alloca (sizeof (struct argp)); 1327 | 1328 | /* TOP_ARGP has no options, it just serves to group the user & default 1329 | argps. */ 1330 | memset (top_argp, 0, sizeof (*top_argp)); 1331 | top_argp->children = child; 1332 | 1333 | memset (child, 0, 4 * sizeof (struct argp_child)); 1334 | 1335 | if (argp) 1336 | (child++)->argp = argp; 1337 | (child++)->argp = &argp_default_argp; 1338 | if (argp_program_version || argp_program_version_hook) 1339 | (child++)->argp = &argp_version_argp; 1340 | child->argp = 0; 1341 | 1342 | argp = top_argp; 1343 | } 1344 | 1345 | /* Construct a parser for these arguments. */ 1346 | err = parser_init (&parser, argp, argc, argv, flags, input); 1347 | 1348 | if (! err) 1349 | /* Parse! */ 1350 | { 1351 | while (! err) 1352 | err = parser_parse_next (&parser, &arg_ebadkey); 1353 | err = parser_finalize (&parser, err, arg_ebadkey, end_index); 1354 | } 1355 | 1356 | return err; 1357 | } 1358 | 1359 | int _option_is_short (const struct argp_option *__opt) 1360 | { 1361 | if (__opt->flags & OPTION_DOC) 1362 | return 0; 1363 | else 1364 | { 1365 | int __key = __opt->key; 1366 | return __key > 0 && __key <= UCHAR_MAX && isprint (__key); 1367 | } 1368 | } 1369 | 1370 | int _option_is_end (const struct argp_option *__opt) 1371 | { 1372 | return !__opt->key && !__opt->name && !__opt->doc && !__opt->group; 1373 | } 1374 | -------------------------------------------------------------------------------- /argp-pv.c: -------------------------------------------------------------------------------- 1 | /* Default definition for ARGP_PROGRAM_VERSION. 2 | Copyright (C) 1996, 1997, 1999, 2004 Free Software Foundation, Inc. 3 | This file is part of the GNU C Library. 4 | Written by Miles Bader . 5 | The GNU C Library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public License as 7 | published by the Free Software Foundation; either version 2 of the 8 | License, or (at your option) any later version. 9 | The GNU C Library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | Library General Public License for more details. 13 | You should have received a copy of the GNU Library General Public 14 | License along with the GNU C Library; see the file COPYING.LIB. If not, 15 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 | Boston, MA 02111-1307, USA. */ 17 | 18 | /* If set by the user program to a non-zero value, then a default option 19 | --version is added (unless the ARGP_NO_HELP flag is used), which will 20 | print this this string followed by a newline and exit (unless the 21 | ARGP_NO_EXIT flag is used). Overridden by ARGP_PROGRAM_VERSION_HOOK. */ 22 | const char *argp_program_version = 0; 23 | -------------------------------------------------------------------------------- /argp-pvh.c: -------------------------------------------------------------------------------- 1 | /* Default definition for ARGP_PROGRAM_VERSION. 2 | Copyright (C) 1996, 1997, 1999, 2004 Free Software Foundation, Inc. 3 | This file is part of the GNU C Library. 4 | Written by Miles Bader . 5 | The GNU C Library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public License as 7 | published by the Free Software Foundation; either version 2 of the 8 | License, or (at your option) any later version. 9 | The GNU C Library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | Library General Public License for more details. 13 | You should have received a copy of the GNU Library General Public 14 | License along with the GNU C Library; see the file COPYING.LIB. If not, 15 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 | Boston, MA 02111-1307, USA. */ 17 | 18 | #include 19 | 20 | /* If set by the user program to a non-zero value, then a default option 21 | --version is added (unless the ARGP_NO_HELP flag is used), which calls 22 | this function with a stream to print the version to and a pointer to the 23 | current parsing state, and then exits (unless the ARGP_NO_EXIT flag is 24 | used). This variable takes precedent over ARGP_PROGRAM_VERSION. */ 25 | void (*argp_program_version_hook) (FILE *stream, struct argp_state *state) = 0; 26 | -------------------------------------------------------------------------------- /argp-shims.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include "argp-shims.h" 5 | 6 | #ifdef _WIN32 7 | 8 | char * strndup (const char *s, size_t n) 9 | { 10 | size_t len = strnlen (s, n); 11 | char *new = (char *) malloc (len + 1); 12 | if (new == NULL) 13 | return NULL; 14 | new[len] = '\0'; 15 | return (char *) memcpy (new, s, len); 16 | } 17 | 18 | char *strchrnul(const char *s, int c) 19 | { 20 | char * pos = strchr(s, c); 21 | if (!pos) 22 | return pos + strlen(s); 23 | return pos; 24 | } 25 | 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /argp-shims.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _ARGP_SHIMS_H 3 | #define _ARGP_SHIMS_H 4 | 5 | #include 6 | 7 | #ifdef _WIN32 8 | 9 | #define fwrite_unlocked fwrite 10 | #define putc_unlocked putc 11 | #define fputc_unlocked fputc 12 | #define fputs_unlocked fputs 13 | 14 | char *strndup (const char *s, size_t n); 15 | char *strchrnul(const char *s, int c); 16 | 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /argp.h: -------------------------------------------------------------------------------- 1 | /* Hierarchial argument parsing, layered over getopt. 2 | Copyright(C) 1995-1999, 2003, 2004, 2005, 2006, 2007, 2009 3 | Free Software Foundation, Inc. 4 | This file is part of the GNU C Library. 5 | Written by Miles Bader . 6 | 7 | The GNU C Library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or(at your option) any later version. 11 | 12 | The GNU C Library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with the GNU C Library; see the file COPYING.LIB. If 19 | not, see . 20 | 21 | Modified for uClibc by: Salvatore Cro 22 | */ 23 | 24 | #ifndef _ARGP_H 25 | #define _ARGP_H 26 | 27 | #include 28 | #include 29 | #ifndef _WIN32 30 | #include 31 | #endif 32 | #include 33 | #include 34 | 35 | #ifndef __attribute__ 36 | /* This feature is available in gcc versions 2.5 and later. */ 37 | # if __GNUC__ < 2 ||(__GNUC__ == 2 && __GNUC_MINOR__ < 5) || defined(__STRICT_ANSI__) 38 | # define __attribute__(Spec) /* empty */ 39 | # endif 40 | /* The __-protected variants of `format' and `printf' attributes 41 | are accepted by gcc versions 2.6.4(effectively 2.7) and later. */ 42 | # if __GNUC__ < 2 ||(__GNUC__ == 2 && __GNUC_MINOR__ < 7) || defined(__STRICT_ANSI__) 43 | # define __format__ format 44 | # define __printf__ printf 45 | # endif 46 | #endif 47 | 48 | #ifndef defined_argp_error_t 49 | typedef int error_t; 50 | #define defined_argp_error_t 51 | #endif 52 | 53 | #ifdef __cplusplus 54 | extern "C" { 55 | #endif 56 | 57 | /* A description of a particular option. A pointer to an array of 58 | these is passed in the OPTIONS field of an argp structure. Each option 59 | entry can correspond to one long option and/or one short option; more 60 | names for the same option can be added by following an entry in an option 61 | array with options having the OPTION_ALIAS flag set. */ 62 | struct argp_option 63 | { 64 | /* The long option name. For more than one name for the same option, you 65 | can use following options with the OPTION_ALIAS flag set. */ 66 | const char *name; 67 | 68 | /* What key is returned for this option. If > 0 and printable, then it's 69 | also accepted as a short option. */ 70 | int key; 71 | 72 | /* If non-NULL, this is the name of the argument associated with this 73 | option, which is required unless the OPTION_ARG_OPTIONAL flag is set. */ 74 | const char *arg; 75 | 76 | /* OPTION_ flags. */ 77 | int flags; 78 | 79 | /* The doc string for this option. If both NAME and KEY are 0, This string 80 | will be printed outdented from the normal option column, making it 81 | useful as a group header(it will be the first thing printed in its 82 | group); in this usage, it's conventional to end the string with a `:'. */ 83 | const char *doc; 84 | 85 | /* The group this option is in. In a long help message, options are sorted 86 | alphabetically within each group, and the groups presented in the order 87 | 0, 1, 2, ..., n, -m, ..., -2, -1. Every entry in an options array with 88 | if this field 0 will inherit the group number of the previous entry, or 89 | zero if it's the first one, unless its a group header(NAME and KEY both 90 | 0), in which case, the previous entry + 1 is the default. Automagic 91 | options such as --help are put into group -1. */ 92 | int group; 93 | }; 94 | 95 | /* The argument associated with this option is optional. */ 96 | #define OPTION_ARG_OPTIONAL 0x1 97 | 98 | /* This option isn't displayed in any help messages. */ 99 | #define OPTION_HIDDEN 0x2 100 | 101 | /* This option is an alias for the closest previous non-alias option. This 102 | means that it will be displayed in the same help entry, and will inherit 103 | fields other than NAME and KEY from the aliased option. */ 104 | #define OPTION_ALIAS 0x4 105 | 106 | /* This option isn't actually an option(and so should be ignored by the 107 | actual option parser), but rather an arbitrary piece of documentation that 108 | should be displayed in much the same manner as the options. If this flag 109 | is set, then the option NAME field is displayed unmodified(e.g., no `--' 110 | prefix is added) at the left-margin(where a *short* option would normally 111 | be displayed), and the documentation string in the normal place. For 112 | purposes of sorting, any leading whitespace and punctuation is ignored, 113 | except that if the first non-whitespace character is not `-', this entry 114 | is displayed after all options(and OPTION_DOC entries with a leading `-') 115 | in the same group. */ 116 | #define OPTION_DOC 0x8 117 | 118 | /* This option shouldn't be included in `long' usage messages(but is still 119 | included in help messages). This is mainly intended for options that are 120 | completely documented in an argp's ARGS_DOC field, in which case including 121 | the option in the generic usage list would be redundant. For instance, 122 | if ARGS_DOC is "FOO BAR\n-x BLAH", and the `-x' option's purpose is to 123 | distinguish these two cases, -x should probably be marked 124 | OPTION_NO_USAGE. */ 125 | #define OPTION_NO_USAGE 0x10 126 | 127 | /* Valid only in conjunction with OPTION_DOC. This option disables translation 128 | of option name. */ 129 | #define OPTION_NO_TRANS 0x20 130 | 131 | struct argp; /* fwd declare this type */ 132 | struct argp_state; /* " */ 133 | struct argp_child; /* " */ 134 | 135 | /* The type of a pointer to an argp parsing function. */ 136 | typedef int(*argp_parser_t)(int __key, char *__arg, 137 | struct argp_state *__state); 138 | 139 | /* What to return for unrecognized keys. For special ARGP_KEY_ keys, such 140 | returns will simply be ignored. For user keys, this error will be turned 141 | into EINVAL(if the call to argp_parse is such that errors are propagated 142 | back to the user instead of exiting); returning EINVAL itself would result 143 | in an immediate stop to parsing in *all* cases. */ 144 | #define ARGP_ERR_UNKNOWN E2BIG /* Hurd should never need E2BIG. XXX */ 145 | 146 | /* Special values for the KEY argument to an argument parsing function. 147 | ARGP_ERR_UNKNOWN should be returned if they aren't understood. 148 | 149 | The sequence of keys to a parsing function is either(where each 150 | uppercased word should be prefixed by `ARGP_KEY_' and opt is a user key): 151 | 152 | INIT opt... NO_ARGS END SUCCESS -- No non-option arguments at all 153 | or INIT(opt | ARG)... END SUCCESS -- All non-option args parsed 154 | or INIT(opt | ARG)... SUCCESS -- Some non-option arg unrecognized 155 | 156 | The third case is where every parser returned ARGP_KEY_UNKNOWN for an 157 | argument, in which case parsing stops at that argument(returning the 158 | unparsed arguments to the caller of argp_parse if requested, or stopping 159 | with an error message if not). 160 | 161 | If an error occurs(either detected by argp, or because the parsing 162 | function returned an error value), then the parser is called with 163 | ARGP_KEY_ERROR, and no further calls are made. */ 164 | 165 | /* This is not an option at all, but rather a command line argument. If a 166 | parser receiving this key returns success, the fact is recorded, and the 167 | ARGP_KEY_NO_ARGS case won't be used. HOWEVER, if while processing the 168 | argument, a parser function decrements the NEXT field of the state it's 169 | passed, the option won't be considered processed; this is to allow you to 170 | actually modify the argument(perhaps into an option), and have it 171 | processed again. */ 172 | #define ARGP_KEY_ARG 0 173 | /* There are remaining arguments not parsed by any parser, which may be found 174 | starting at(STATE->argv + STATE->next). If success is returned, but 175 | STATE->next left untouched, it's assumed that all arguments were consume, 176 | otherwise, the parser should adjust STATE->next to reflect any arguments 177 | consumed. */ 178 | #define ARGP_KEY_ARGS 0x1000006 179 | /* There are no more command line arguments at all. */ 180 | #define ARGP_KEY_END 0x1000001 181 | /* Because it's common to want to do some special processing if there aren't 182 | any non-option args, user parsers are called with this key if they didn't 183 | successfully process any non-option arguments. Called just before 184 | ARGP_KEY_END(where more general validity checks on previously parsed 185 | arguments can take place). */ 186 | #define ARGP_KEY_NO_ARGS 0x1000002 187 | /* Passed in before any parsing is done. Afterwards, the values of each 188 | element of the CHILD_INPUT field, if any, in the state structure is 189 | copied to each child's state to be the initial value of the INPUT field. */ 190 | #define ARGP_KEY_INIT 0x1000003 191 | /* Use after all other keys, including SUCCESS & END. */ 192 | #define ARGP_KEY_FINI 0x1000007 193 | /* Passed in when parsing has successfully been completed(even if there are 194 | still arguments remaining). */ 195 | #define ARGP_KEY_SUCCESS 0x1000004 196 | /* Passed in if an error occurs. */ 197 | #define ARGP_KEY_ERROR 0x1000005 198 | 199 | /* An argp structure contains a set of options declarations, a function to 200 | deal with parsing one, documentation string, a possible vector of child 201 | argp's, and perhaps a function to filter help output. When actually 202 | parsing options, getopt is called with the union of all the argp 203 | structures chained together through their CHILD pointers, with conflicts 204 | being resolved in favor of the first occurrence in the chain. */ 205 | struct argp 206 | { 207 | /* An array of argp_option structures, terminated by an entry with both 208 | NAME and KEY having a value of 0. */ 209 | const struct argp_option *options; 210 | 211 | /* What to do with an option from this structure. KEY is the key 212 | associated with the option, and ARG is any associated argument(NULL if 213 | none was supplied). If KEY isn't understood, ARGP_ERR_UNKNOWN should be 214 | returned. If a non-zero, non-ARGP_ERR_UNKNOWN value is returned, then 215 | parsing is stopped immediately, and that value is returned from 216 | argp_parse(). For special(non-user-supplied) values of KEY, see the 217 | ARGP_KEY_ definitions below. */ 218 | argp_parser_t parser; 219 | 220 | /* A string describing what other arguments are wanted by this program. It 221 | is only used by argp_usage to print the `Usage:' message. If it 222 | contains newlines, the strings separated by them are considered 223 | alternative usage patterns, and printed on separate lines(lines after 224 | the first are prefix by ` or: ' instead of `Usage:'). */ 225 | const char *args_doc; 226 | 227 | /* If non-NULL, a string containing extra text to be printed before and 228 | after the options in a long help message(separated by a vertical tab 229 | `\v' character). */ 230 | const char *doc; 231 | 232 | /* A vector of argp_children structures, terminated by a member with a 0 233 | argp field, pointing to child argps should be parsed with this one. Any 234 | conflicts are resolved in favor of this argp, or early argps in the 235 | CHILDREN list. This field is useful if you use libraries that supply 236 | their own argp structure, which you want to use in conjunction with your 237 | own. */ 238 | const struct argp_child *children; 239 | 240 | /* If non-zero, this should be a function to filter the output of help 241 | messages. KEY is either a key from an option, in which case TEXT is 242 | that option's help text, or a special key from the ARGP_KEY_HELP_ 243 | defines, below, describing which other help text TEXT is. The function 244 | should return either TEXT, if it should be used as-is, a replacement 245 | string, which should be malloced, and will be freed by argp, or NULL, 246 | meaning `print nothing'. The value for TEXT is *after* any translation 247 | has been done, so if any of the replacement text also needs translation, 248 | that should be done by the filter function. INPUT is either the input 249 | supplied to argp_parse, or NULL, if argp_help was called directly. */ 250 | char *(*help_filter)(int __key, const char *__text, void *__input); 251 | 252 | /* If non-zero the strings used in the argp library are translated using 253 | the domain described by this string. Otherwise the currently installed 254 | default domain is used. */ 255 | const char *argp_domain; 256 | }; 257 | 258 | /* Possible KEY arguments to a help filter function. */ 259 | #define ARGP_KEY_HELP_PRE_DOC 0x2000001 /* Help text preceeding options. */ 260 | #define ARGP_KEY_HELP_POST_DOC 0x2000002 /* Help text following options. */ 261 | #define ARGP_KEY_HELP_HEADER 0x2000003 /* Option header string. */ 262 | #define ARGP_KEY_HELP_EXTRA 0x2000004 /* After all other documentation; 263 | TEXT is NULL for this key. */ 264 | /* Explanatory note emitted when duplicate option arguments have been 265 | suppressed. */ 266 | #define ARGP_KEY_HELP_DUP_ARGS_NOTE 0x2000005 267 | #define ARGP_KEY_HELP_ARGS_DOC 0x2000006 /* Argument doc string. */ 268 | 269 | /* When an argp has a non-zero CHILDREN field, it should point to a vector of 270 | argp_child structures, each of which describes a subsidiary argp. */ 271 | struct argp_child 272 | { 273 | /* The child parser. */ 274 | const struct argp *argp; 275 | 276 | /* Flags for this child. */ 277 | int flags; 278 | 279 | /* If non-zero, an optional header to be printed in help output before the 280 | child options. As a side-effect, a non-zero value forces the child 281 | options to be grouped together; to achieve this effect without actually 282 | printing a header string, use a value of "". */ 283 | const char *header; 284 | 285 | /* Where to group the child options relative to the other(`consolidated') 286 | options in the parent argp; the values are the same as the GROUP field 287 | in argp_option structs, but all child-groupings follow parent options at 288 | a particular group level. If both this field and HEADER are zero, then 289 | they aren't grouped at all, but rather merged with the parent options 290 | (merging the child's grouping levels with the parents). */ 291 | int group; 292 | }; 293 | 294 | /* Parsing state. This is provided to parsing functions called by argp, 295 | which may examine and, as noted, modify fields. */ 296 | struct argp_state 297 | { 298 | /* The top level ARGP being parsed. */ 299 | const struct argp *root_argp; 300 | 301 | /* The argument vector being parsed. May be modified. */ 302 | int argc; 303 | char **argv; 304 | 305 | /* The index in ARGV of the next arg that to be parsed. May be modified. */ 306 | int next; 307 | 308 | /* The flags supplied to argp_parse. May be modified. */ 309 | unsigned flags; 310 | 311 | /* While calling a parsing function with a key of ARGP_KEY_ARG, this is the 312 | number of the current arg, starting at zero, and incremented after each 313 | such call returns. At all other times, this is the number of such 314 | arguments that have been processed. */ 315 | unsigned arg_num; 316 | 317 | /* If non-zero, the index in ARGV of the first argument following a special 318 | `--' argument(which prevents anything following being interpreted as an 319 | option). Only set once argument parsing has proceeded past this point. */ 320 | int quoted; 321 | 322 | /* An arbitrary pointer passed in from the user. */ 323 | void *input; 324 | /* Values to pass to child parsers. This vector will be the same length as 325 | the number of children for the current parser. */ 326 | void **child_inputs; 327 | 328 | /* For the parser's use. Initialized to 0. */ 329 | void *hook; 330 | 331 | /* The name used when printing messages. This is initialized to ARGV[0], 332 | or PROGRAM_INVOCATION_NAME if that is unavailable. */ 333 | char *name; 334 | 335 | /* Streams used when argp prints something. */ 336 | FILE *err_stream; /* For errors; initialized to stderr. */ 337 | FILE *out_stream; /* For information; initialized to stdout. */ 338 | 339 | void *pstate; /* Private, for use by argp. */ 340 | }; 341 | 342 | /* Flags for argp_parse(note that the defaults are those that are 343 | convenient for program command line parsing): */ 344 | 345 | /* Don't ignore the first element of ARGV. Normally(and always unless 346 | ARGP_NO_ERRS is set) the first element of the argument vector is 347 | skipped for option parsing purposes, as it corresponds to the program name 348 | in a command line. */ 349 | #define ARGP_PARSE_ARGV0 0x01 350 | 351 | /* Don't print error messages for unknown options to stderr; unless this flag 352 | is set, ARGP_PARSE_ARGV0 is ignored, as ARGV[0] is used as the program 353 | name in the error messages. This flag implies ARGP_NO_EXIT(on the 354 | assumption that silent exiting upon errors is bad behaviour). */ 355 | #define ARGP_NO_ERRS 0x02 356 | 357 | /* Don't parse any non-option args. Normally non-option args are parsed by 358 | calling the parse functions with a key of ARGP_KEY_ARG, and the actual arg 359 | as the value. Since it's impossible to know which parse function wants to 360 | handle it, each one is called in turn, until one returns 0 or an error 361 | other than ARGP_ERR_UNKNOWN; if an argument is handled by no one, the 362 | argp_parse returns prematurely(but with a return value of 0). If all 363 | args have been parsed without error, all parsing functions are called one 364 | last time with a key of ARGP_KEY_END. This flag needn't normally be set, 365 | as the normal behavior is to stop parsing as soon as some argument can't 366 | be handled. */ 367 | #define ARGP_NO_ARGS 0x04 368 | 369 | /* Parse options and arguments in the same order they occur on the command 370 | line -- normally they're rearranged so that all options come first. */ 371 | #define ARGP_IN_ORDER 0x08 372 | 373 | /* Don't provide the standard long option --help, which causes usage and 374 | option help information to be output to stdout, and exit(0) called. */ 375 | #define ARGP_NO_HELP 0x10 376 | 377 | /* Don't exit on errors(they may still result in error messages). */ 378 | #define ARGP_NO_EXIT 0x20 379 | 380 | /* Use the gnu getopt `long-only' rules for parsing arguments. */ 381 | #define ARGP_LONG_ONLY 0x40 382 | 383 | /* Turns off any message-printing/exiting options. */ 384 | #define ARGP_SILENT (ARGP_NO_EXIT | ARGP_NO_ERRS | ARGP_NO_HELP) 385 | 386 | /* Parse the options strings in ARGC & ARGV according to the options in ARGP. 387 | FLAGS is one of the ARGP_ flags above. If ARG_INDEX is non-NULL, the 388 | index in ARGV of the first unparsed option is returned in it. If an 389 | unknown option is present, ARGP_ERR_UNKNOWN is returned; if some parser 390 | routine returned a non-zero value, it is returned; otherwise 0 is 391 | returned. This function may also call exit unless the ARGP_NO_HELP flag 392 | is set. INPUT is a pointer to a value to be passed in to the parser. */ 393 | extern int argp_parse(const struct argp *__restrict __argp, 394 | int argc, char **__restrict argv, 395 | unsigned __flags, int *__restrict __arg_index, 396 | void *__restrict __input); 397 | 398 | /* Global variables. */ 399 | 400 | /* If defined or set by the user program to a non-zero value, then a default 401 | option --version is added(unless the ARGP_NO_HELP flag is used), which 402 | will print this string followed by a newline and exit(unless the 403 | ARGP_NO_EXIT flag is used). Overridden by ARGP_PROGRAM_VERSION_HOOK. */ 404 | extern const char *argp_program_version; 405 | 406 | /* If defined or set by the user program to a non-zero value, then a default 407 | option --version is added(unless the ARGP_NO_HELP flag is used), which 408 | calls this function with a stream to print the version to and a pointer to 409 | the current parsing state, and then exits(unless the ARGP_NO_EXIT flag is 410 | used). This variable takes precedent over ARGP_PROGRAM_VERSION. */ 411 | extern void(*argp_program_version_hook)(FILE *__restrict __stream, 412 | struct argp_state *__restrict 413 | __state); 414 | 415 | /* If defined or set by the user program, it should point to string that is 416 | the bug-reporting address for the program. It will be printed by 417 | argp_help if the ARGP_HELP_BUG_ADDR flag is set(as it is by various 418 | standard help messages), embedded in a sentence that says something like 419 | `Report bugs to ADDR.'. */ 420 | extern const char *argp_program_bug_address; 421 | 422 | /* The exit status that argp will use when exiting due to a parsing error. 423 | If not defined or set by the user program, this defaults to EX_USAGE from 424 | . */ 425 | extern int argp_err_exit_status; 426 | 427 | /* Flags for argp_help. */ 428 | #define ARGP_HELP_USAGE 0x01 /* a Usage: message. */ 429 | #define ARGP_HELP_SHORT_USAGE 0x02 /* " but don't actually print options. */ 430 | #define ARGP_HELP_SEE 0x04 /* a `Try ... for more help' message. */ 431 | #define ARGP_HELP_LONG 0x08 /* a long help message. */ 432 | #define ARGP_HELP_PRE_DOC 0x10 /* doc string preceding long help. */ 433 | #define ARGP_HELP_POST_DOC 0x20 /* doc string following long help. */ 434 | #define ARGP_HELP_DOC (ARGP_HELP_PRE_DOC | ARGP_HELP_POST_DOC) 435 | #define ARGP_HELP_BUG_ADDR 0x40 /* bug report address */ 436 | #define ARGP_HELP_LONG_ONLY 0x80 /* modify output appropriately to 437 | reflect ARGP_LONG_ONLY mode. */ 438 | 439 | /* These ARGP_HELP flags are only understood by argp_state_help. */ 440 | #define ARGP_HELP_EXIT_ERR 0x100 /* Call exit(1) instead of returning. */ 441 | #define ARGP_HELP_EXIT_OK 0x200 /* Call exit(0) instead of returning. */ 442 | 443 | /* The standard thing to do after a program command line parsing error, if an 444 | error message has already been printed. */ 445 | #define ARGP_HELP_STD_ERR \ 446 | (ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR) 447 | /* The standard thing to do after a program command line parsing error, if no 448 | more specific error message has been printed. */ 449 | #define ARGP_HELP_STD_USAGE \ 450 | (ARGP_HELP_SHORT_USAGE | ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR) 451 | /* The standard thing to do in response to a --help option. */ 452 | #define ARGP_HELP_STD_HELP \ 453 | (ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG | ARGP_HELP_EXIT_OK \ 454 | | ARGP_HELP_DOC | ARGP_HELP_BUG_ADDR) 455 | 456 | /* Output a usage message for ARGP to STREAM. FLAGS are from the set 457 | ARGP_HELP_*. */ 458 | extern void argp_help(const struct argp *__restrict __argp, 459 | FILE *__restrict __stream, 460 | unsigned __flags, char *__restrict __name); 461 | 462 | /* The following routines are intended to be called from within an argp 463 | parsing routine(thus taking an argp_state structure as the first 464 | argument). They may or may not print an error message and exit, depending 465 | on the flags in STATE -- in any case, the caller should be prepared for 466 | them *not* to exit, and should return an appropiate error after calling 467 | them. [argp_usage & argp_error should probably be called argp_state_..., 468 | but they're used often enough that they should be short] */ 469 | 470 | /* Output, if appropriate, a usage message for STATE to STREAM. FLAGS are 471 | from the set ARGP_HELP_*. */ 472 | extern void argp_state_help(const struct argp_state *__restrict __state, 473 | FILE *__restrict __stream, 474 | unsigned int __flags); 475 | /* Possibly output the standard usage message for ARGP to stderr and exit. */ 476 | extern void argp_usage(const struct argp_state *__state); 477 | 478 | /* If appropriate, print the printf string FMT and following args, preceded 479 | by the program name and `:', to stderr, and followed by a `Try ... --help' 480 | message, then exit(1). */ 481 | extern void argp_error(const struct argp_state *__restrict __state, 482 | const char *__restrict __fmt, ...) 483 | __attribute__((__format__(__printf__, 2, 3))); 484 | /* Similar to the standard gnu error-reporting function error(), but will 485 | respect the ARGP_NO_EXIT and ARGP_NO_ERRS flags in STATE, and will print 486 | to STATE->err_stream. This is useful for argument parsing code that is 487 | shared between program startup(when exiting is desired) and runtime 488 | option parsing(when typically an error code is returned instead). The 489 | difference between this function and argp_error is that the latter is for 490 | *parsing errors*, and the former is for other problems that occur during 491 | parsing but don't reflect a(syntactic) problem with the input. */ 492 | extern void argp_failure(const struct argp_state *__restrict __state, 493 | int __status, int __errnum, 494 | const char *__restrict __fmt, ...) 495 | __attribute__((__format__(__printf__, 4, 5))); 496 | 497 | /* Returns true if the option OPT is a valid short option. */ 498 | extern int _option_is_short(const struct argp_option *__opt); 499 | 500 | /* Returns true if the option OPT is in fact the last(unused) entry in an 501 | options array. */ 502 | extern int _option_is_end(const struct argp_option *__opt); 503 | 504 | /* Setup standard display of the version information for the '--version' 505 | option. NAME is the canonical program name, and AUTHORS is a NULL- 506 | terminated array of author names. At least one author name must be 507 | given. 508 | 509 | If NAME is NULL, the package name (as given by the PACKAGE macro) 510 | is assumed to be the name of the program. 511 | 512 | This function is intended to be called before argp_parse(). 513 | */ 514 | extern void argp_version_setup (const char *name, const char * const *authors); 515 | 516 | #ifdef __cplusplus 517 | } 518 | #endif 519 | 520 | #endif /* argp.h */ 521 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Written by Rich Felker, originally as part of musl libc. 4 | # Multi-licensed under MIT, 0BSD, and CC0. 5 | # 6 | # This is an actually-safe install command which installs the new 7 | # file atomically in the new location, rather than overwriting 8 | # existing files. 9 | # 10 | 11 | usage() { 12 | printf "usage: %s [-D] [-l] [-m mode] src dest\n" "$0" 1>&2 13 | exit 1 14 | } 15 | 16 | mkdirp= 17 | symlink= 18 | mode=755 19 | 20 | while getopts Dlm: name ; do 21 | case "$name" in 22 | D) mkdirp=yes ;; 23 | l) symlink=yes ;; 24 | m) mode=$OPTARG ;; 25 | ?) usage ;; 26 | esac 27 | done 28 | shift $(($OPTIND - 1)) 29 | 30 | test "$#" -eq 2 || usage 31 | src=$1 32 | dst=$2 33 | tmp="$dst.tmp.$$" 34 | 35 | case "$dst" in 36 | */) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;; 37 | esac 38 | 39 | set -C 40 | set -e 41 | 42 | if test "$mkdirp" ; then 43 | umask 022 44 | case "$2" in 45 | */*) mkdir -p "${dst%/*}" ;; 46 | esac 47 | fi 48 | 49 | trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP 50 | 51 | umask 077 52 | 53 | if test "$symlink" ; then 54 | ln -s "$1" "$tmp" 55 | else 56 | cat < "$1" > "$tmp" 57 | chmod "$mode" "$tmp" 58 | fi 59 | 60 | mv -f "$tmp" "$2" 61 | test -d "$2" && { 62 | rm -f "$2/$tmp" 63 | printf "%s: %s is a directory\n" "$0" "$dst" 1>&2 64 | exit 1 65 | } 66 | 67 | exit 0 68 | -------------------------------------------------------------------------------- /internal.h: -------------------------------------------------------------------------------- 1 | #ifndef ARGP_INTERNAL 2 | #define ARGP_INTERNAL 3 | 4 | #include 5 | 6 | /* For __ordering member */ 7 | enum { 8 | REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER 9 | }; 10 | 11 | /* Data type for reentrant functions. */ 12 | 13 | struct getopt_data 14 | { 15 | /* These have exactly the same meaning as the corresponding global 16 | variables, except that they are used for the reentrant 17 | versions of getopt. */ 18 | int optind; 19 | int opterr; 20 | char *optarg; 21 | char optopt; /* we store characters here, a byte is enough */ 22 | 23 | /* Internal members. */ 24 | 25 | /* True if the internal members have been initialized. */ 26 | char __initialized; 27 | 28 | /* Describe how to deal with options that follow non-option ARGV-elements. 29 | 30 | If the caller did not specify anything, 31 | the default is REQUIRE_ORDER if the environment variable 32 | POSIXLY_CORRECT is defined, PERMUTE otherwise. 33 | 34 | REQUIRE_ORDER means don't recognize them as options; 35 | stop option processing when the first non-option is seen. 36 | This is what Unix does. 37 | This mode of operation is selected by either setting the environment 38 | variable POSIXLY_CORRECT, or using `+' as the first character 39 | of the list of option characters. 40 | 41 | PERMUTE is the default. We permute the contents of ARGV as we 42 | scan, so that eventually all the non-options are at the end. 43 | This allows options to be given in any order, even with programs 44 | that were not written to expect this. 45 | 46 | RETURN_IN_ORDER is an option available to programs that were 47 | written to expect options and other ARGV-elements in any order 48 | and that care about the ordering of the two. We describe each 49 | non-option ARGV-element as if it were the argument of an option 50 | with character code 1. Using `-' as the first character of the 51 | list of option characters selects this mode of operation. 52 | 53 | The special argument `--' forces an end of option-scanning regardless 54 | of the value of `ordering'. In the case of RETURN_IN_ORDER, only 55 | `--' can cause `getopt' to return -1 with `optind' != ARGC. */ 56 | char __ordering; 57 | 58 | /* If the POSIXLY_CORRECT environment variable is set. */ 59 | char __posixly_correct; 60 | 61 | /* The next char to be scanned in the option-element 62 | in which the last option character we returned was found. 63 | This allows us to pick up the scan where we left off. 64 | 65 | If this is zero, or a null string, it means resume the scan 66 | by advancing to the next ARGV-element. */ 67 | char *__nextchar; 68 | 69 | 70 | /* Handle permutation of arguments. */ 71 | 72 | /* Describe the part of ARGV that contains non-options that have 73 | been skipped. `first_nonopt' is the index in ARGV of the first 74 | of them; `last_nonopt' is the index after the last of them. */ 75 | 76 | int __first_nonopt; 77 | int __last_nonopt; 78 | }; 79 | 80 | 81 | struct parser 82 | { 83 | const struct argp *argp; 84 | 85 | /* SHORT_OPTS is the getopt short options string for the union of all the 86 | groups of options. */ 87 | char *short_opts; 88 | /* LONG_OPTS is the array of getop long option structures for the union of 89 | all the groups of options. */ 90 | struct option *long_opts; 91 | /* OPT_DATA is the getopt data used for the re-entrant getopt. */ 92 | struct getopt_data opt_data; 93 | 94 | /* States of the various parsing groups. */ 95 | struct group *groups; 96 | /* The end of the GROUPS array. */ 97 | struct group *egroup; 98 | /* An vector containing storage for the CHILD_INPUTS field in all groups. */ 99 | void **child_inputs; 100 | 101 | /* True if we think using getopt is still useful; if false, then 102 | remaining arguments are just passed verbatim with ARGP_KEY_ARG. This is 103 | cleared whenever getopt returns KEY_END, but may be set again if the user 104 | moves the next argument pointer backwards. */ 105 | int try_getopt; 106 | 107 | /* State block supplied to parsing routines. */ 108 | struct argp_state state; 109 | 110 | /* Memory used by this parser. */ 111 | void *storage; 112 | }; 113 | 114 | /* The state of a `group' during parsing. Each group corresponds to a 115 | particular argp structure from the tree of such descending from the top 116 | level argp passed to argp_parse. */ 117 | struct group 118 | { 119 | /* This group's parsing function. */ 120 | argp_parser_t parser; 121 | 122 | /* Which argp this group is from. */ 123 | const struct argp *argp; 124 | 125 | /* Points to the point in SHORT_OPTS corresponding to the end of the short 126 | options for this group. We use it to determine from which group a 127 | particular short options is from. */ 128 | char *short_end; 129 | 130 | /* The number of non-option args sucessfully handled by this parser. */ 131 | unsigned args_processed; 132 | 133 | /* This group's parser's parent's group. */ 134 | struct group *parent; 135 | unsigned parent_index; /* And the our position in the parent. */ 136 | 137 | /* These fields are swapped into and out of the state structure when 138 | calling this group's parser. */ 139 | void *input, **child_inputs; 140 | void *hook; 141 | }; 142 | 143 | /* The next usable entries in the various parser tables being filled in by 144 | convert_options. */ 145 | struct parser_convert_state 146 | { 147 | struct parser *parser; 148 | char *short_end; 149 | struct option *long_end; 150 | void **child_inputs_end; 151 | }; 152 | 153 | 154 | /* Lengths of various parser fields which we will allocated. */ 155 | struct parser_sizes 156 | { 157 | size_t short_len; /* Getopt short options string. */ 158 | size_t long_len; /* Getopt long options vector. */ 159 | size_t num_groups; /* Group structures we allocate. */ 160 | size_t num_child_inputs; /* Child input slots. */ 161 | }; 162 | 163 | #endif 164 | --------------------------------------------------------------------------------