3 * ====================================================================
4 * Copyright (c) 2000-2007 CollabNet. All rights reserved.
6 * This software is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at http://subversion.tigris.org/license-1.html.
9 * If newer versions of this license are posted there, you may use a
10 * newer version instead, at your option.
12 * This software consists of voluntary contributions made by many
13 * individuals. For exact contribution history, see the revision
14 * history and logs, available at http://subversion.tigris.org/.
15 * ====================================================================
19 * @brief Option and argument parsing for Subversion command lines
26 #include <apr_pools.h>
27 #include <apr_getopt.h>
29 #include "svn_types.h"
30 #include "svn_error.h"
34 #endif /* __cplusplus */
39 * All subcommand procedures in Subversion conform to this prototype.
41 * @a os is the apr option state after getopt processing has been run; in
42 * other words, it still contains the non-option arguments following
43 * the subcommand. See @a os->argv and @a os->ind.
45 * @a baton is anything you need it to be.
47 * @a pool is used for allocating errors, and for any other allocation
48 * unless the instance is explicitly documented to allocate from a
51 typedef svn_error_t
*(svn_opt_subcommand_t
)
52 (apr_getopt_t
*os
, void *baton
, apr_pool_t
*pool
);
55 /** The maximum number of aliases a subcommand can have. */
56 #define SVN_OPT_MAX_ALIASES 3
58 /** The maximum number of options that can be accepted by a subcommand. */
59 #define SVN_OPT_MAX_OPTIONS 50
61 /** Options that have no short option char should use an identifying
62 * integer equal to or greater than this.
64 #define SVN_OPT_FIRST_LONGOPT_ID 256
67 /** One element of a subcommand dispatch table.
71 typedef struct svn_opt_subcommand_desc2_t
73 /** The full name of this command. */
76 /** The function this command invokes. */
77 svn_opt_subcommand_t
*cmd_func
;
79 /** A list of alias names for this command (e.g., 'up' for 'update'). */
80 const char *aliases
[SVN_OPT_MAX_ALIASES
];
82 /** A brief string describing this command, for usage messages. */
85 /** A list of options accepted by this command. Each value in the
86 * array is a unique enum (the 2nd field in apr_getopt_option_t)
88 int valid_options
[SVN_OPT_MAX_OPTIONS
];
90 /** A list of option help descriptions, keyed by the option unique enum
91 * (the 2nd field in apr_getopt_option_t), which override the generic
92 * descriptions given in an apr_getopt_option_t on a per-subcommand basis.
94 struct { int optch
; const char *desc
; } desc_overrides
[SVN_OPT_MAX_OPTIONS
];
95 } svn_opt_subcommand_desc2_t
;
98 /** One element of a subcommand dispatch table.
100 * @deprecated Provided for backward compatibility with the 1.3 API.
102 * Like #svn_opt_subcommand_desc2_t but lacking the @c desc_overrides
105 typedef struct svn_opt_subcommand_desc_t
107 /** The full name of this command. */
110 /** The function this command invokes. */
111 svn_opt_subcommand_t
*cmd_func
;
113 /** A list of alias names for this command (e.g., 'up' for 'update'). */
114 const char *aliases
[SVN_OPT_MAX_ALIASES
];
116 /** A brief string describing this command, for usage messages. */
119 /** A list of options accepted by this command. Each value in the
120 * array is a unique enum (the 2nd field in apr_getopt_option_t)
122 int valid_options
[SVN_OPT_MAX_OPTIONS
];
124 } svn_opt_subcommand_desc_t
;
128 * Return the entry in @a table whose name matches @a cmd_name, or @c NULL if
129 * none. @a cmd_name may be an alias.
133 const svn_opt_subcommand_desc2_t
*
134 svn_opt_get_canonical_subcommand2(const svn_opt_subcommand_desc2_t
*table
,
135 const char *cmd_name
);
139 * Return the entry in @a table whose name matches @a cmd_name, or @c NULL if
140 * none. @a cmd_name may be an alias.
142 * Same as svn_opt_get_canonical_subcommand2(), but acts on
143 * #svn_opt_subcommand_desc_t.
145 * @deprecated Provided for backward compatibility with the 1.3 API.
147 const svn_opt_subcommand_desc_t
*
148 svn_opt_get_canonical_subcommand(const svn_opt_subcommand_desc_t
*table
,
149 const char *cmd_name
);
153 * Return pointer to an @c apr_getopt_option_t for the option whose
154 * option code is @a code, or @c NULL if no match. @a option_table must end
155 * with an element whose every field is zero. If @c command is non-NULL,
156 * then return the subcommand-specific option description instead of the
157 * generic one, if a specific description is defined.
159 * The returned value may be statically allocated, or allocated in @a pool.
163 const apr_getopt_option_t
*
164 svn_opt_get_option_from_code2(int code
,
165 const apr_getopt_option_t
*option_table
,
166 const svn_opt_subcommand_desc2_t
*command
,
171 * Return the first entry from @a option_table whose option code is @a code,
172 * or @c NULL if no match. @a option_table must end with an element whose
173 * every field is zero.
175 * @deprecated Provided for backward compatibility with the 1.3 API.
177 const apr_getopt_option_t
*
178 svn_opt_get_option_from_code(int code
,
179 const apr_getopt_option_t
*option_table
);
183 * Return @c TRUE iff subcommand @a command supports option @a
184 * option_code, else return @c FALSE. If @a global_options is
185 * non-NULL, it is a zero-terminated array, and all subcommands take
186 * the options listed in it.
191 svn_opt_subcommand_takes_option3(const svn_opt_subcommand_desc2_t
*command
,
193 const int *global_options
);
196 * Same as svn_opt_subcommand_takes_option3(), but with @c NULL for @a
199 * @deprecated Provided for backward compatibility with the 1.4 API.
202 svn_opt_subcommand_takes_option2(const svn_opt_subcommand_desc2_t
*command
,
207 * Return @c TRUE iff subcommand @a command supports option @a option_code,
208 * else return @c FALSE.
210 * Same as svn_opt_subcommand_takes_option2(), but acts on
211 * #svn_opt_subcommand_desc_t.
213 * @deprecated Provided for backward compatibility with the 1.3 API.
216 svn_opt_subcommand_takes_option(const svn_opt_subcommand_desc_t
*command
,
221 * Print a generic (not command-specific) usage message to @a stream.
223 * ### @todo Why is @a stream a stdio file instead of an svn stream?
225 * If @a header is non-NULL, print @a header followed by a newline. Then
226 * loop over @a cmd_table printing the usage for each command (getting
227 * option usages from @a opt_table). Then if @a footer is non-NULL, print
228 * @a footer followed by a newline.
230 * Use @a pool for temporary allocation.
235 svn_opt_print_generic_help2(const char *header
,
236 const svn_opt_subcommand_desc2_t
*cmd_table
,
237 const apr_getopt_option_t
*opt_table
,
244 * Same as svn_opt_print_generic_help2(), but acts on
245 * #svn_opt_subcommand_desc_t.
247 * @deprecated Provided for backward compatibility with the 1.3 API.
250 svn_opt_print_generic_help(const char *header
,
251 const svn_opt_subcommand_desc_t
*cmd_table
,
252 const apr_getopt_option_t
*opt_table
,
259 * Print an option @a opt nicely into a @a string allocated in @a pool.
260 * If @a doc is set, include the generic documentation string of @a opt,
261 * localized to the current locale if a translation is available.
264 svn_opt_format_option(const char **string
,
265 const apr_getopt_option_t
*opt
,
272 * Get @a subcommand's usage from @a table, and print it to @c stdout.
273 * Obtain option usage from @a options_table. If not @c NULL, @a
274 * global_options is a zero-terminated list of global options. Use @a
275 * pool for temporary allocation. @a subcommand may be a canonical
276 * command name or an alias. ### @todo Why does this only print to
277 * @c stdout, whereas svn_opt_print_generic_help() gives us a choice?
282 svn_opt_subcommand_help3(const char *subcommand
,
283 const svn_opt_subcommand_desc2_t
*table
,
284 const apr_getopt_option_t
*options_table
,
285 const int *global_options
,
289 * Same as svn_opt_subcommand_help3(), but with @a global_options
292 * @deprecated Provided for backward compatibility with the 1.4 API.
295 svn_opt_subcommand_help2(const char *subcommand
,
296 const svn_opt_subcommand_desc2_t
*table
,
297 const apr_getopt_option_t
*options_table
,
302 * Same as svn_opt_subcommand_help2(), but acts on
303 * #svn_opt_subcommand_desc_t.
305 * @deprecated Provided for backward compatibility with the 1.3 API.
308 svn_opt_subcommand_help(const char *subcommand
,
309 const svn_opt_subcommand_desc_t
*table
,
310 const apr_getopt_option_t
*options_table
,
315 /* Parsing revision and date options. */
318 * Various ways of specifying revisions.
321 * In contexts where local mods are relevant, the `working' kind
322 * refers to the uncommitted "working" revision, which may be modified
323 * with respect to its base revision. In other contexts, `working'
324 * should behave the same as `committed' or `current'.
326 enum svn_opt_revision_kind
{
327 /** No revision information given. */
328 svn_opt_revision_unspecified
,
330 /** revision given as number */
331 svn_opt_revision_number
,
333 /** revision given as date */
334 svn_opt_revision_date
,
336 /** rev of most recent change */
337 svn_opt_revision_committed
,
339 /** (rev of most recent change) - 1 */
340 svn_opt_revision_previous
,
342 /** .svn/entries current revision */
343 svn_opt_revision_base
,
345 /** current, plus local mods */
346 svn_opt_revision_working
,
348 /** repository youngest */
349 svn_opt_revision_head
353 * A revision value, which can be specified as a number or a date.
355 * @note This union was formerly an anonymous inline type in
356 * @c svn_opt_revision_t, and was converted to a named type just to
357 * make things easier for SWIG.
361 typedef union svn_opt_revision_value_t
363 /** The revision number */
366 /** the date of the revision */
368 } svn_opt_revision_value_t
;
370 /** A revision, specified in one of @c svn_opt_revision_kind ways. */
371 typedef struct svn_opt_revision_t
373 enum svn_opt_revision_kind kind
; /**< See svn_opt_revision_kind */
374 svn_opt_revision_value_t value
; /**< Extra data qualifying the @c kind */
375 } svn_opt_revision_t
;
377 /** A revision range, specified in one of @c svn_opt_revision_kind ways. */
378 typedef struct svn_opt_revision_range_t
380 /** The first revision in the range */
381 svn_opt_revision_t start
;
383 /** The last revision in the range */
384 svn_opt_revision_t end
;
385 } svn_opt_revision_range_t
;
388 * Set @a *start_revision and/or @a *end_revision according to @a arg,
389 * where @a arg is "N" or "N:M", like so:
391 * - If @a arg is "N", set @a *start_revision to represent N, and
392 * leave @a *end_revision untouched.
394 * - If @a arg is "N:M", set @a *start_revision and @a *end_revision
395 * to represent N and M respectively.
397 * N and/or M may be one of the special revision descriptors
398 * recognized by revision_from_word(), or a date in curly braces.
400 * If @a arg is invalid, return -1; else return 0.
401 * It is invalid to omit a revision (as in, ":", "N:" or ":M").
403 * @note It is typical, though not required, for @a *start_revision and
404 * @a *end_revision to be @c svn_opt_revision_unspecified kind on entry.
406 * Use @a pool for temporary allocations.
408 int svn_opt_parse_revision(svn_opt_revision_t
*start_revision
,
409 svn_opt_revision_t
*end_revision
,
414 * Parse @a arg, where @a arg is "N" or "N:M", into a
415 * @c svn_opt_revision_range_t and push that onto @a opt_ranges.
417 * - If @a arg is "N", set the @c start field of the
418 * @c svn_opt_revision_range_t to represent N and the @c end field
419 * to @c svn_opt_revision_unspecified.
421 * - If @a arg is "N:M", set the @c start field of the
422 * @c svn_opt_revision_range_t to represent N and the @c end field
425 * If @a arg is invalid, return -1; else return 0. It is invalid to omit
426 * a revision (as in, ":", "N:" or ":M").
428 * Use @a pool to allocate @c svn_opt_revision_range_t pushed to the array.
433 svn_opt_parse_revision_to_range(apr_array_header_t
*opt_ranges
,
438 * Resolve peg revisions and operational revisions in the following way:
440 * - If @a is_url is set and @a peg_rev->kind is
441 * @c svn_opt_revision_unspecified, @a peg_rev->kind defaults to
442 * @c svn_opt_revision_head.
444 * - If @a is_url is not set, and @a peg_rev->kind is
445 * @c svn_opt_revision_unspecified, @a peg_rev->kind defaults to
446 * @c svn_opt_revision_base.
448 * - If @a op_rev->kind is @c svn_opt_revision_unspecified, @a op_rev
449 * defaults to @a peg_rev.
451 * Both @a peg_rev and @a op_rev may be modified as a result of this
452 * function. @a is_url should be set if the path the revisions refer to is
453 * a url, and unset otherwise.
455 * If @a notice_local_mods is set, @c svn_opt_revision_working is used,
456 * instead of @c svn_opt_revision_base.
458 * Use @a pool for allocations.
463 svn_opt_resolve_revisions(svn_opt_revision_t
*peg_rev
,
464 svn_opt_revision_t
*op_rev
,
465 svn_boolean_t is_url
,
466 svn_boolean_t notice_local_mods
,
470 /* Parsing arguments. */
473 * Pull remaining target arguments from @a os into @a *targets_p,
474 * converting them to UTF-8, followed by targets from @a known_targets
475 * (which might come from, for example, the "--targets" command line
476 * option), which are already in UTF-8.
478 * On each URL target, do some IRI-to-URI encoding and some
479 * auto-escaping. On each local path, canonicalize case and path
482 * Allocate @a *targets_p and its elements in @a pool.
484 * If a path has the same name as a Subversion working copy
485 * administrative directory, return SVN_ERR_RESERVED_FILENAME_SPECIFIED;
486 * if multiple reserved paths are encountered, return a chain of
487 * errors, all of which are SVN_ERR_RESERVED_FILENAME_SPECIFIED. Do
488 * not return this type of error in a chain with any other type of
489 * error, and if this is the only type of error encountered, complete
490 * the operation before returning the error(s).
492 * @deprecated Provided for backward compatibility with the 1.5 API.
493 * @see svn_client_args_to_target_array()
496 svn_opt_args_to_target_array3(apr_array_header_t
**targets_p
,
498 apr_array_header_t
*known_targets
,
502 * This is the same as svn_opt_args_to_target_array3() except that it
503 * silently ignores paths that have the same name as a working copy
504 * administrative directory.
509 svn_opt_args_to_target_array2(apr_array_header_t
**targets_p
,
511 apr_array_header_t
*known_targets
,
516 * The same as svn_opt_args_to_target_array2() except that, in
517 * addition, if @a extract_revisions is set, then look for trailing
518 * "@rev" syntax on the first two paths. If the first target in @a
519 * *targets_p ends in "@rev", replace it with a canonicalized version of
520 * the part before "@rev" and replace @a *start_revision with the value
521 * of "rev". If the second target in @a *targets_p ends in "@rev",
522 * replace it with a canonicalized version of the part before "@rev"
523 * and replace @a *end_revision with the value of "rev". Ignore
524 * revision specifiers on any further paths. "rev" can be any form of
525 * single revision specifier, as accepted by svn_opt_parse_revision().
527 * @deprecated Provided for backward compatibility with the 1.1 API.
530 svn_opt_args_to_target_array(apr_array_header_t
**targets_p
,
532 apr_array_header_t
*known_targets
,
533 svn_opt_revision_t
*start_revision
,
534 svn_opt_revision_t
*end_revision
,
535 svn_boolean_t extract_revisions
,
540 * Parse revprop key/value pair from @a revprop_spec (name[=value]) into
541 * @a revprops, making copies of both with @a pool. If @a revprops is
542 * @c NULL, allocate a new apr_hash_t in it. @a revprops maps
543 * const char * revprop names to svn_string_t * revprop values for use
544 * with svn_repos_get_commit_editor5 and other get_commit_editor APIs.
549 svn_opt_parse_revprop(apr_hash_t
**revprops
, const char *revprop_spec
,
554 * If no targets exist in @a *targets, add `.' as the lone target.
556 * (Some commands take an implicit "." string argument when invoked
557 * with no arguments. Those commands make use of this function to
558 * add "." to the target array if the user passes no args.)
560 void svn_opt_push_implicit_dot_target(apr_array_header_t
*targets
,
565 * Parse @a num_args non-target arguments from the list of arguments in
566 * @a os->argv, return them as <tt>const char *</tt> in @a *args_p, without
567 * doing any UTF-8 conversion. Allocate @a *args_p and its values in @a pool.
570 svn_opt_parse_num_args(apr_array_header_t
**args_p
,
577 * Parse all remaining arguments from @a os->argv, return them as
578 * <tt>const char *</tt> in @a *args_p, without doing any UTF-8 conversion.
579 * Allocate @a *args_p and its values in @a pool.
582 svn_opt_parse_all_args(apr_array_header_t
**args_p
,
587 * Parse a working-copy or URL in @a path, extracting any trailing
588 * revision specifier of the form "@rev" from the last component of
591 * Some examples would be:
593 * "foo/bar" -> "foo/bar", (unspecified)
594 * "foo/bar@13" -> "foo/bar", (number, 13)
595 * "foo/bar@HEAD" -> "foo/bar", (head)
596 * "foo/bar@{1999-12-31}" -> "foo/bar", (date, 1999-12-31)
597 * "http://a/b@27" -> "http://a/b", (number, 27)
598 * "http://a/b@COMMITTED" -> "http://a/b", (committed) [*]
599 * "http://a/b@{1999-12-31} -> "http://a/b", (date, 1999-12-31)
600 * "http://a/b@%7B1999-12-31%7D -> "http://a/b", (date, 1999-12-31)
601 * "foo/bar@1:2" -> error
602 * "foo/bar@baz" -> error
603 * "foo/bar@" -> "foo/bar", (base)
604 * "foo/bar/@13" -> "foo/bar/", (number, 13)
605 * "foo/bar@@13" -> "foo/bar@", (number, 13)
606 * "foo/@bar@HEAD" -> "foo/@bar", (head)
607 * "foo@/bar" -> "foo@/bar", (unspecified)
608 * "foo@HEAD/bar" -> "foo@HEAD/bar", (unspecified)
610 * [*] Syntactically valid but probably not semantically useful.
612 * If a trailing revision specifier is found, parse it into @a *rev and
613 * put the rest of the path into @a *truepath, allocating from @a pool;
614 * or return an @c SVN_ERR_CL_ARG_PARSING_ERROR if the revision
615 * specifier is invalid. If no trailing revision specifier is found,
616 * set @a *truepath to @a path and @a rev->kind to @c
617 * svn_opt_revision_unspecified.
619 * This function does not require that @a path be in canonical form.
620 * No canonicalization is done and @a *truepath will only be in
621 * canonical form if @a path is in canonical form.
626 svn_opt_parse_path(svn_opt_revision_t
*rev
,
627 const char **truepath
,
632 * Central dispatcher function for various kinds of help message.
634 * * subcommand-specific help (svn_opt_subcommand_help)
635 * * generic help (svn_opt_print_generic_help)
637 * * simple usage complaint: "Type '@a pgm_name help' for usage."
639 * If @a os is not @c NULL and it contains arguments, then try
640 * printing help for them as though they are subcommands, using @a
641 * cmd_table and @a option_table for option information. If not @c
642 * NULL, @a global_options is a zero-terminated array of options taken
643 * by all subcommands.
645 * Else, if @a print_version is TRUE, then print version info, in
646 * brief form if @a quiet is also TRUE; if @a quiet is FALSE, then if
647 * @a version_footer is non-NULL, print it following the version
650 * Else, if @a os is not @c NULL and does not contain arguments, print
651 * generic help, via svn_opt_print_generic_help2() with the @a header,
652 * @a cmd_table, @a option_table, and @a footer arguments.
654 * Else, when @a os is @c NULL, print the simple usage complaint.
656 * Use @a pool for temporary allocations.
658 * Notes: The reason this function handles both version printing and
659 * general usage help is that a confused user might put both the
660 * --version flag *and* subcommand arguments on a help command line.
661 * The logic for handling such a situation should be in one place.
666 svn_opt_print_help3(apr_getopt_t
*os
,
667 const char *pgm_name
,
668 svn_boolean_t print_version
,
670 const char *version_footer
,
672 const svn_opt_subcommand_desc2_t
*cmd_table
,
673 const apr_getopt_option_t
*option_table
,
674 const int *global_options
,
679 * Same as svn_opt_print_help3(), but with @a global_options always @c
682 * @deprecated Provided for backward compatibility with the 1.4 API.
686 svn_opt_print_help2(apr_getopt_t
*os
,
687 const char *pgm_name
,
688 svn_boolean_t print_version
,
690 const char *version_footer
,
692 const svn_opt_subcommand_desc2_t
*cmd_table
,
693 const apr_getopt_option_t
*option_table
,
699 * Same as svn_opt_print_help2(), but acts on #svn_opt_subcommand_desc_t.
701 * @deprecated Provided for backward compatibility with the 1.3 API.
704 svn_opt_print_help(apr_getopt_t
*os
,
705 const char *pgm_name
,
706 svn_boolean_t print_version
,
708 const char *version_footer
,
710 const svn_opt_subcommand_desc_t
*cmd_table
,
711 const apr_getopt_option_t
*option_table
,
717 #endif /* __cplusplus */
719 #endif /* SVN_OPTS_H */