2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
14 static FILE *config_file
;
15 static const char *config_file_name
;
16 static int config_linenr
;
17 static int config_file_eof
;
19 const char *config_exclusive_filename
= NULL
;
21 static int get_next_char(void)
27 if ((f
= config_file
) != NULL
) {
30 /* DOS like systems */
47 static char *parse_value(void)
49 static char value
[1024];
50 int quote
= 0, comment
= 0, len
= 0, space
= 0;
53 int c
= get_next_char();
54 if (len
>= sizeof(value
) - 1)
64 if (isspace(c
) && !quote
) {
69 if (c
== ';' || c
== '#') {
93 /* Some characters escape as themselves */
96 /* Reject unknown escape sequences */
111 static inline int iskeychar(int c
)
113 return isalnum(c
) || c
== '-';
116 static int get_value(config_fn_t fn
, void *data
, char *name
, unsigned int len
)
121 /* Get the full name */
128 name
[len
++] = tolower(c
);
133 while (c
== ' ' || c
== '\t')
140 value
= parse_value();
144 return fn(name
, value
, data
);
147 static int get_extended_base_var(char *name
, int baselen
, int c
)
153 } while (isspace(c
));
155 /* We require the format to be '[base "extension"]' */
158 name
[baselen
++] = '.';
161 int c
= get_next_char();
172 if (baselen
> MAXNAME
/ 2)
177 if (get_next_char() != ']')
182 static int get_base_var(char *name
)
187 int c
= get_next_char();
193 return get_extended_base_var(name
, baselen
, c
);
194 if (!iskeychar(c
) && c
!= '.')
196 if (baselen
> MAXNAME
/ 2)
198 name
[baselen
++] = tolower(c
);
202 static int perf_parse_file(config_fn_t fn
, void *data
)
206 static char var
[MAXNAME
];
208 /* U+FEFF Byte Order Mark in UTF8 */
209 static const unsigned char *utf8_bom
= (unsigned char *) "\xef\xbb\xbf";
210 const unsigned char *bomptr
= utf8_bom
;
213 int c
= get_next_char();
214 if (bomptr
&& *bomptr
) {
215 /* We are at the file beginning; skip UTF8-encoded BOM
216 * if present. Sane editors won't put this in on their
217 * own, but e.g. Windows Notepad will do it happily. */
218 if ((unsigned char) c
== *bomptr
) {
222 /* Do not tolerate partial BOM. */
223 if (bomptr
!= utf8_bom
)
225 /* No BOM at file beginning. Cool. */
235 if (comment
|| isspace(c
))
237 if (c
== '#' || c
== ';') {
242 baselen
= get_base_var(var
);
245 var
[baselen
++] = '.';
251 var
[baselen
] = tolower(c
);
252 if (get_value(fn
, data
, var
, baselen
+1) < 0)
255 die("bad config file line %d in %s", config_linenr
, config_file_name
);
258 static int parse_unit_factor(const char *end
, unsigned long *val
)
262 else if (!strcasecmp(end
, "k")) {
266 else if (!strcasecmp(end
, "m")) {
270 else if (!strcasecmp(end
, "g")) {
271 *val
*= 1024 * 1024 * 1024;
277 static int perf_parse_long(const char *value
, long *ret
)
279 if (value
&& *value
) {
281 long val
= strtol(value
, &end
, 0);
282 unsigned long factor
= 1;
283 if (!parse_unit_factor(end
, &factor
))
291 int perf_parse_ulong(const char *value
, unsigned long *ret
)
293 if (value
&& *value
) {
295 unsigned long val
= strtoul(value
, &end
, 0);
296 if (!parse_unit_factor(end
, &val
))
304 static void die_bad_config(const char *name
)
306 if (config_file_name
)
307 die("bad config value for '%s' in %s", name
, config_file_name
);
308 die("bad config value for '%s'", name
);
311 int perf_config_int(const char *name
, const char *value
)
314 if (!perf_parse_long(value
, &ret
))
315 die_bad_config(name
);
319 unsigned long perf_config_ulong(const char *name
, const char *value
)
322 if (!perf_parse_ulong(value
, &ret
))
323 die_bad_config(name
);
327 int perf_config_bool_or_int(const char *name
, const char *value
, int *is_bool
)
334 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") || !strcasecmp(value
, "on"))
336 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no") || !strcasecmp(value
, "off"))
339 return perf_config_int(name
, value
);
342 int perf_config_bool(const char *name
, const char *value
)
345 return !!perf_config_bool_or_int(name
, value
, &discard
);
348 int perf_config_string(const char **dest
, const char *var
, const char *value
)
351 return config_error_nonbool(var
);
352 *dest
= strdup(value
);
356 static int perf_default_core_config(const char *var
, const char *value
)
358 /* Add other config variables here and to Documentation/config.txt. */
362 int perf_default_config(const char *var
, const char *value
, void *dummy
)
364 if (!prefixcmp(var
, "core."))
365 return perf_default_core_config(var
, value
);
367 /* Add other config variables here and to Documentation/config.txt. */
371 int perf_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
374 FILE *f
= fopen(filename
, "r");
379 config_file_name
= filename
;
382 ret
= perf_parse_file(fn
, data
);
384 config_file_name
= NULL
;
389 const char *perf_etc_perfconfig(void)
391 static const char *system_wide
;
393 system_wide
= system_path(ETC_PERFCONFIG
);
397 static int perf_env_bool(const char *k
, int def
)
399 const char *v
= getenv(k
);
400 return v
? perf_config_bool(k
, v
) : def
;
403 int perf_config_system(void)
405 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
408 int perf_config_global(void)
410 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
413 int perf_config(config_fn_t fn
, void *data
)
415 int ret
= 0, found
= 0;
416 char *repo_config
= NULL
;
417 const char *home
= NULL
;
419 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
420 if (config_exclusive_filename
)
421 return perf_config_from_file(fn
, config_exclusive_filename
, data
);
422 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK
)) {
423 ret
+= perf_config_from_file(fn
, perf_etc_perfconfig(),
428 home
= getenv("HOME");
429 if (perf_config_global() && home
) {
430 char *user_config
= strdup(mkpath("%s/.perfconfig", home
));
431 if (!access(user_config
, R_OK
)) {
432 ret
+= perf_config_from_file(fn
, user_config
, data
);
438 repo_config
= perf_pathdup("config");
439 if (!access(repo_config
, R_OK
)) {
440 ret
+= perf_config_from_file(fn
, repo_config
, data
);
450 * Find all the stuff for perf_config_set() below.
453 #define MAX_MATCHES 512
459 regex_t
* value_regex
;
461 size_t offset
[MAX_MATCHES
];
462 enum { START
, SECTION_SEEN
, SECTION_END_SEEN
, KEY_SEEN
} state
;
466 static int matches(const char* key
, const char* value
)
468 return !strcmp(key
, store
.key
) &&
469 (store
.value_regex
== NULL
||
470 (store
.do_not_match
^
471 !regexec(store
.value_regex
, value
, 0, NULL
, 0)));
474 static int store_aux(const char* key
, const char* value
, void *cb
)
479 switch (store
.state
) {
481 if (matches(key
, value
)) {
482 if (store
.seen
== 1 && store
.multi_replace
== 0) {
483 warning("%s has multiple values", key
);
484 } else if (store
.seen
>= MAX_MATCHES
) {
485 error("too many matches for %s", key
);
489 store
.offset
[store
.seen
] = ftell(config_file
);
495 * What we are looking for is in store.key (both
496 * section and var), and its section part is baselen
497 * long. We found key (again, both section and var).
498 * We would want to know if this key is in the same
499 * section as what we are looking for. We already
500 * know we are in the same section as what should
503 ep
= strrchr(key
, '.');
504 section_len
= ep
- key
;
506 if ((section_len
!= store
.baselen
) ||
507 memcmp(key
, store
.key
, section_len
+1)) {
508 store
.state
= SECTION_END_SEEN
;
513 * Do not increment matches: this is no match, but we
514 * just made sure we are in the desired section.
516 store
.offset
[store
.seen
] = ftell(config_file
);
518 case SECTION_END_SEEN
:
520 if (matches(key
, value
)) {
521 store
.offset
[store
.seen
] = ftell(config_file
);
522 store
.state
= KEY_SEEN
;
525 if (strrchr(key
, '.') - key
== store
.baselen
&&
526 !strncmp(key
, store
.key
, store
.baselen
)) {
527 store
.state
= SECTION_SEEN
;
528 store
.offset
[store
.seen
] = ftell(config_file
);
535 static int store_write_section(int fd
, const char* key
)
539 struct strbuf sb
= STRBUF_INIT
;
541 dot
= memchr(key
, '.', store
.baselen
);
543 strbuf_addf(&sb
, "[%.*s \"", (int)(dot
- key
), key
);
544 for (i
= dot
- key
+ 1; i
< store
.baselen
; i
++) {
545 if (key
[i
] == '"' || key
[i
] == '\\')
546 strbuf_addch(&sb
, '\\');
547 strbuf_addch(&sb
, key
[i
]);
549 strbuf_addstr(&sb
, "\"]\n");
551 strbuf_addf(&sb
, "[%.*s]\n", store
.baselen
, key
);
554 success
= write_in_full(fd
, sb
.buf
, sb
.len
) == sb
.len
;
560 static int store_write_pair(int fd
, const char* key
, const char* value
)
563 int length
= strlen(key
+ store
.baselen
+ 1);
564 const char *quote
= "";
565 struct strbuf sb
= STRBUF_INIT
;
568 * Check to see if the value needs to be surrounded with a dq pair.
569 * Note that problematic characters are always backslash-quoted; this
570 * check is about not losing leading or trailing SP and strings that
571 * follow beginning-of-comment characters (i.e. ';' and '#') by the
572 * configuration parser.
576 for (i
= 0; value
[i
]; i
++)
577 if (value
[i
] == ';' || value
[i
] == '#')
579 if (i
&& value
[i
- 1] == ' ')
582 strbuf_addf(&sb
, "\t%.*s = %s",
583 length
, key
+ store
.baselen
+ 1, quote
);
585 for (i
= 0; value
[i
]; i
++)
588 strbuf_addstr(&sb
, "\\n");
591 strbuf_addstr(&sb
, "\\t");
595 strbuf_addch(&sb
, '\\');
597 strbuf_addch(&sb
, value
[i
]);
600 strbuf_addf(&sb
, "%s\n", quote
);
602 success
= write_in_full(fd
, sb
.buf
, sb
.len
) == sb
.len
;
608 static ssize_t
find_beginning_of_line(const char* contents
, size_t size
,
609 size_t offset_
, int* found_bracket
)
611 size_t equal_offset
= size
, bracket_offset
= size
;
615 for (offset
= offset_
-2; offset
> 0
616 && contents
[offset
] != '\n'; offset
--)
617 switch (contents
[offset
]) {
618 case '=': equal_offset
= offset
; break;
619 case ']': bracket_offset
= offset
; break;
621 if (offset
> 0 && contents
[offset
-1] == '\\') {
625 if (bracket_offset
< equal_offset
) {
627 offset
= bracket_offset
+1;
634 int perf_config_set(const char* key
, const char* value
)
636 return perf_config_set_multivar(key
, value
, NULL
, 0);
640 * If value==NULL, unset in (remove from) config,
641 * if value_regex!=NULL, disregard key/value pairs where value does not match.
642 * if multi_replace==0, nothing, or only one matching key/value is replaced,
643 * else all matching key/values (regardless how many) are removed,
644 * before the new pair is written.
646 * Returns 0 on success.
648 * This function does this:
650 * - it locks the config file by creating ".perf/config.lock"
652 * - it then parses the config using store_aux() as validator to find
653 * the position on the key/value pair to replace. If it is to be unset,
654 * it must be found exactly once.
656 * - the config file is mmap()ed and the part before the match (if any) is
657 * written to the lock file, then the changed part and the rest.
659 * - the config file is removed and the lock file rename()d to it.
662 int perf_config_set_multivar(const char* key
, const char* value
,
663 const char* value_regex
, int multi_replace
)
668 char* config_filename
;
669 const char* last_dot
= strrchr(key
, '.');
671 if (config_exclusive_filename
)
672 config_filename
= strdup(config_exclusive_filename
);
674 config_filename
= perf_pathdup("config");
677 * Since "key" actually contains the section name and the real
678 * key name separated by a dot, we have to know where the dot is.
681 if (last_dot
== NULL
) {
682 error("key does not contain a section: %s", key
);
686 store
.baselen
= last_dot
- key
;
688 store
.multi_replace
= multi_replace
;
691 * Validate the key and while at it, lower case it for matching.
693 store
.key
= malloc(strlen(key
) + 1);
695 for (i
= 0; key
[i
]; i
++) {
696 unsigned char c
= key
[i
];
699 /* Leave the extended basename untouched.. */
700 if (!dot
|| i
> store
.baselen
) {
701 if (!iskeychar(c
) || (i
== store
.baselen
+1 && !isalpha(c
))) {
702 error("invalid key: %s", key
);
708 } else if (c
== '\n') {
709 error("invalid key (newline): %s", key
);
719 * If .perf/config does not exist yet, write a minimal version.
721 in_fd
= open(config_filename
, O_RDONLY
);
725 if ( ENOENT
!= errno
) {
726 error("opening %s: %s", config_filename
,
728 ret
= 3; /* same as "invalid config file" */
731 /* if nothing to unset, error out */
737 store
.key
= (char*)key
;
738 if (!store_write_section(fd
, key
) ||
739 !store_write_pair(fd
, key
, value
))
744 size_t contents_sz
, copy_begin
, copy_end
;
747 if (value_regex
== NULL
)
748 store
.value_regex
= NULL
;
750 if (value_regex
[0] == '!') {
751 store
.do_not_match
= 1;
754 store
.do_not_match
= 0;
756 store
.value_regex
= (regex_t
*)malloc(sizeof(regex_t
));
757 if (regcomp(store
.value_regex
, value_regex
,
759 error("invalid pattern: %s", value_regex
);
760 free(store
.value_regex
);
771 * After this, store.offset will contain the *end* offset
772 * of the last match, or remain at 0 if no match was found.
773 * As a side effect, we make sure to transform only a valid
774 * existing config file.
776 if (perf_config_from_file(store_aux
, config_filename
, NULL
)) {
777 error("invalid config file %s", config_filename
);
779 if (store
.value_regex
!= NULL
) {
780 regfree(store
.value_regex
);
781 free(store
.value_regex
);
788 if (store
.value_regex
!= NULL
) {
789 regfree(store
.value_regex
);
790 free(store
.value_regex
);
793 /* if nothing to unset, or too many matches, error out */
794 if ((store
.seen
== 0 && value
== NULL
) ||
795 (store
.seen
> 1 && multi_replace
== 0)) {
801 contents_sz
= xsize_t(st
.st_size
);
802 contents
= mmap(NULL
, contents_sz
, PROT_READ
,
803 MAP_PRIVATE
, in_fd
, 0);
809 for (i
= 0, copy_begin
= 0; i
< store
.seen
; i
++) {
810 if (store
.offset
[i
] == 0) {
811 store
.offset
[i
] = copy_end
= contents_sz
;
812 } else if (store
.state
!= KEY_SEEN
) {
813 copy_end
= store
.offset
[i
];
815 copy_end
= find_beginning_of_line(
816 contents
, contents_sz
,
817 store
.offset
[i
]-2, &new_line
);
819 if (copy_end
> 0 && contents
[copy_end
-1] != '\n')
822 /* write the first part of the config */
823 if (copy_end
> copy_begin
) {
824 if (write_in_full(fd
, contents
+ copy_begin
,
825 copy_end
- copy_begin
) <
826 copy_end
- copy_begin
)
829 write_in_full(fd
, "\n", 1) != 1)
832 copy_begin
= store
.offset
[i
];
835 /* write the pair (value == NULL means unset) */
837 if (store
.state
== START
) {
838 if (!store_write_section(fd
, key
))
841 if (!store_write_pair(fd
, key
, value
))
845 /* write the rest of the config */
846 if (copy_begin
< contents_sz
)
847 if (write_in_full(fd
, contents
+ copy_begin
,
848 contents_sz
- copy_begin
) <
849 contents_sz
- copy_begin
)
852 munmap(contents
, contents_sz
);
858 free(config_filename
);
867 * Call this to report error for your variable that should not
868 * get a boolean value (i.e. "[my] var" means "true").
870 int config_error_nonbool(const char *var
)
872 return error("Missing value for '%s'", var
);