1 /* backupfile.c -- make Emacs style backup file names
3 Copyright 2017 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "backup-internal.h"
27 /* Return the name of a backup file for the existing file FILE,
28 allocated with malloc. Report an error and exit if out of memory.
29 Do not call this function if backup_type == no_backups. */
32 find_backup_file_name (char const *file
, enum backup_type backup_type
)
34 char *result
= backupfile_internal (file
, backup_type
, false);
40 static char const *const backup_args
[] =
42 /* In a series of synonyms, present the most meaningful first, so
43 that argmatch_valid be more readable. */
51 static const enum backup_type backup_types
[] =
53 no_backups
, no_backups
,
54 simple_backups
, simple_backups
,
55 numbered_existing_backups
, numbered_existing_backups
,
56 numbered_backups
, numbered_backups
59 /* Ensure that these two vectors have the same number of elements,
60 not counting the final NULL in the first one. */
61 ARGMATCH_VERIFY (backup_args
, backup_types
);
63 /* Return the type of backup specified by VERSION.
64 If VERSION is NULL or the empty string, return numbered_existing_backups.
65 If VERSION is invalid or ambiguous, fail with a diagnostic appropriate
66 for the specified CONTEXT. Unambiguous abbreviations are accepted. */
69 get_version (char const *context
, char const *version
)
71 if (version
== 0 || *version
== 0)
72 return numbered_existing_backups
;
74 return XARGMATCH (context
, version
, backup_args
, backup_types
);
78 /* Return the type of backup specified by VERSION.
79 If VERSION is NULL, use the value of the envvar VERSION_CONTROL.
80 If the specified string is invalid or ambiguous, fail with a diagnostic
81 appropriate for the specified CONTEXT.
82 Unambiguous abbreviations are accepted. */
85 xget_version (char const *context
, char const *version
)
87 if (version
&& *version
)
88 return get_version (context
, version
);
90 return get_version ("$VERSION_CONTROL", getenv ("VERSION_CONTROL"));