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, space
= 0;
54 int c
= get_next_char();
56 if (len
>= sizeof(value
) - 1)
66 if (isspace(c
) && !quote
) {
71 if (c
== ';' || c
== '#') {
95 /* Some characters escape as themselves */
98 /* Reject unknown escape sequences */
113 static inline int iskeychar(int c
)
115 return isalnum(c
) || c
== '-';
118 static int get_value(config_fn_t fn
, void *data
, char *name
, unsigned int len
)
123 /* Get the full name */
130 name
[len
++] = tolower(c
);
135 while (c
== ' ' || c
== '\t')
142 value
= parse_value();
146 return fn(name
, value
, data
);
149 static int get_extended_base_var(char *name
, int baselen
, int c
)
155 } while (isspace(c
));
157 /* We require the format to be '[base "extension"]' */
160 name
[baselen
++] = '.';
163 int ch
= get_next_char();
170 ch
= get_next_char();
174 name
[baselen
++] = ch
;
175 if (baselen
> MAXNAME
/ 2)
180 if (get_next_char() != ']')
185 static int get_base_var(char *name
)
190 int c
= get_next_char();
196 return get_extended_base_var(name
, baselen
, c
);
197 if (!iskeychar(c
) && c
!= '.')
199 if (baselen
> MAXNAME
/ 2)
201 name
[baselen
++] = tolower(c
);
205 static int perf_parse_file(config_fn_t fn
, void *data
)
209 static char var
[MAXNAME
];
211 /* U+FEFF Byte Order Mark in UTF8 */
212 static const unsigned char *utf8_bom
= (unsigned char *) "\xef\xbb\xbf";
213 const unsigned char *bomptr
= utf8_bom
;
216 int c
= get_next_char();
217 if (bomptr
&& *bomptr
) {
218 /* We are at the file beginning; skip UTF8-encoded BOM
219 * if present. Sane editors won't put this in on their
220 * own, but e.g. Windows Notepad will do it happily. */
221 if ((unsigned char) c
== *bomptr
) {
225 /* Do not tolerate partial BOM. */
226 if (bomptr
!= utf8_bom
)
228 /* No BOM at file beginning. Cool. */
238 if (comment
|| isspace(c
))
240 if (c
== '#' || c
== ';') {
245 baselen
= get_base_var(var
);
248 var
[baselen
++] = '.';
254 var
[baselen
] = tolower(c
);
255 if (get_value(fn
, data
, var
, baselen
+1) < 0)
258 die("bad config file line %d in %s", config_linenr
, config_file_name
);
261 static int parse_unit_factor(const char *end
, unsigned long *val
)
265 else if (!strcasecmp(end
, "k")) {
269 else if (!strcasecmp(end
, "m")) {
273 else if (!strcasecmp(end
, "g")) {
274 *val
*= 1024 * 1024 * 1024;
280 static int perf_parse_long(const char *value
, long *ret
)
282 if (value
&& *value
) {
284 long val
= strtol(value
, &end
, 0);
285 unsigned long factor
= 1;
286 if (!parse_unit_factor(end
, &factor
))
294 int perf_parse_ulong(const char *value
, unsigned long *ret
)
296 if (value
&& *value
) {
298 unsigned long val
= strtoul(value
, &end
, 0);
299 if (!parse_unit_factor(end
, &val
))
307 static void die_bad_config(const char *name
)
309 if (config_file_name
)
310 die("bad config value for '%s' in %s", name
, config_file_name
);
311 die("bad config value for '%s'", name
);
314 int perf_config_int(const char *name
, const char *value
)
317 if (!perf_parse_long(value
, &ret
))
318 die_bad_config(name
);
322 unsigned long perf_config_ulong(const char *name
, const char *value
)
325 if (!perf_parse_ulong(value
, &ret
))
326 die_bad_config(name
);
330 int perf_config_bool_or_int(const char *name
, const char *value
, int *is_bool
)
337 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") || !strcasecmp(value
, "on"))
339 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no") || !strcasecmp(value
, "off"))
342 return perf_config_int(name
, value
);
345 int perf_config_bool(const char *name
, const char *value
)
348 return !!perf_config_bool_or_int(name
, value
, &discard
);
351 int perf_config_string(const char **dest
, const char *var
, const char *value
)
354 return config_error_nonbool(var
);
355 *dest
= strdup(value
);
359 static int perf_default_core_config(const char *var __used
, const char *value __used
)
361 /* Add other config variables here and to Documentation/config.txt. */
365 int perf_default_config(const char *var
, const char *value
, void *dummy __used
)
367 if (!prefixcmp(var
, "core."))
368 return perf_default_core_config(var
, value
);
370 /* Add other config variables here and to Documentation/config.txt. */
374 int perf_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
377 FILE *f
= fopen(filename
, "r");
382 config_file_name
= filename
;
385 ret
= perf_parse_file(fn
, data
);
387 config_file_name
= NULL
;
392 const char *perf_etc_perfconfig(void)
394 static const char *system_wide
;
396 system_wide
= system_path(ETC_PERFCONFIG
);
400 static int perf_env_bool(const char *k
, int def
)
402 const char *v
= getenv(k
);
403 return v
? perf_config_bool(k
, v
) : def
;
406 int perf_config_system(void)
408 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
411 int perf_config_global(void)
413 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
416 int perf_config(config_fn_t fn
, void *data
)
418 int ret
= 0, found
= 0;
419 char *repo_config
= NULL
;
420 const char *home
= NULL
;
422 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
423 if (config_exclusive_filename
)
424 return perf_config_from_file(fn
, config_exclusive_filename
, data
);
425 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK
)) {
426 ret
+= perf_config_from_file(fn
, perf_etc_perfconfig(),
431 home
= getenv("HOME");
432 if (perf_config_global() && home
) {
433 char *user_config
= strdup(mkpath("%s/.perfconfig", home
));
434 if (!access(user_config
, R_OK
)) {
435 ret
+= perf_config_from_file(fn
, user_config
, data
);
441 repo_config
= perf_pathdup("config");
442 if (!access(repo_config
, R_OK
)) {
443 ret
+= perf_config_from_file(fn
, repo_config
, data
);
453 * Find all the stuff for perf_config_set() below.
456 #define MAX_MATCHES 512
462 regex_t
* value_regex
;
464 size_t offset
[MAX_MATCHES
];
465 enum { START
, SECTION_SEEN
, SECTION_END_SEEN
, KEY_SEEN
} state
;
469 static int matches(const char* key
, const char* value
)
471 return !strcmp(key
, store
.key
) &&
472 (store
.value_regex
== NULL
||
473 (store
.do_not_match
^
474 !regexec(store
.value_regex
, value
, 0, NULL
, 0)));
477 static int store_aux(const char* key
, const char* value
, void *cb __used
)
482 switch (store
.state
) {
484 if (matches(key
, value
)) {
485 if (store
.seen
== 1 && store
.multi_replace
== 0) {
486 warning("%s has multiple values", key
);
487 } else if (store
.seen
>= MAX_MATCHES
) {
488 error("too many matches for %s", key
);
492 store
.offset
[store
.seen
] = ftell(config_file
);
498 * What we are looking for is in store.key (both
499 * section and var), and its section part is baselen
500 * long. We found key (again, both section and var).
501 * We would want to know if this key is in the same
502 * section as what we are looking for. We already
503 * know we are in the same section as what should
506 ep
= strrchr(key
, '.');
507 section_len
= ep
- key
;
509 if ((section_len
!= store
.baselen
) ||
510 memcmp(key
, store
.key
, section_len
+1)) {
511 store
.state
= SECTION_END_SEEN
;
516 * Do not increment matches: this is no match, but we
517 * just made sure we are in the desired section.
519 store
.offset
[store
.seen
] = ftell(config_file
);
521 case SECTION_END_SEEN
:
523 if (matches(key
, value
)) {
524 store
.offset
[store
.seen
] = ftell(config_file
);
525 store
.state
= KEY_SEEN
;
528 if (strrchr(key
, '.') - key
== store
.baselen
&&
529 !strncmp(key
, store
.key
, store
.baselen
)) {
530 store
.state
= SECTION_SEEN
;
531 store
.offset
[store
.seen
] = ftell(config_file
);
540 static int store_write_section(int fd
, const char* key
)
544 struct strbuf sb
= STRBUF_INIT
;
546 dot
= memchr(key
, '.', store
.baselen
);
548 strbuf_addf(&sb
, "[%.*s \"", (int)(dot
- key
), key
);
549 for (i
= dot
- key
+ 1; i
< store
.baselen
; i
++) {
550 if (key
[i
] == '"' || key
[i
] == '\\')
551 strbuf_addch(&sb
, '\\');
552 strbuf_addch(&sb
, key
[i
]);
554 strbuf_addstr(&sb
, "\"]\n");
556 strbuf_addf(&sb
, "[%.*s]\n", store
.baselen
, key
);
559 success
= (write_in_full(fd
, sb
.buf
, sb
.len
) == (ssize_t
)sb
.len
);
565 static int store_write_pair(int fd
, const char* key
, const char* value
)
568 int length
= strlen(key
+ store
.baselen
+ 1);
569 const char *quote
= "";
570 struct strbuf sb
= STRBUF_INIT
;
573 * Check to see if the value needs to be surrounded with a dq pair.
574 * Note that problematic characters are always backslash-quoted; this
575 * check is about not losing leading or trailing SP and strings that
576 * follow beginning-of-comment characters (i.e. ';' and '#') by the
577 * configuration parser.
581 for (i
= 0; value
[i
]; i
++)
582 if (value
[i
] == ';' || value
[i
] == '#')
584 if (i
&& value
[i
- 1] == ' ')
587 strbuf_addf(&sb
, "\t%.*s = %s",
588 length
, key
+ store
.baselen
+ 1, quote
);
590 for (i
= 0; value
[i
]; i
++)
593 strbuf_addstr(&sb
, "\\n");
596 strbuf_addstr(&sb
, "\\t");
600 strbuf_addch(&sb
, '\\');
602 strbuf_addch(&sb
, value
[i
]);
605 strbuf_addf(&sb
, "%s\n", quote
);
607 success
= (write_in_full(fd
, sb
.buf
, sb
.len
) == (ssize_t
)sb
.len
);
613 static ssize_t
find_beginning_of_line(const char* contents
, size_t size
,
614 size_t offset_
, int* found_bracket
)
616 size_t equal_offset
= size
, bracket_offset
= size
;
620 for (offset
= offset_
-2; offset
> 0
621 && contents
[offset
] != '\n'; offset
--)
622 switch (contents
[offset
]) {
623 case '=': equal_offset
= offset
; break;
624 case ']': bracket_offset
= offset
; break;
627 if (offset
> 0 && contents
[offset
-1] == '\\') {
631 if (bracket_offset
< equal_offset
) {
633 offset
= bracket_offset
+1;
640 int perf_config_set(const char* key
, const char* value
)
642 return perf_config_set_multivar(key
, value
, NULL
, 0);
646 * If value==NULL, unset in (remove from) config,
647 * if value_regex!=NULL, disregard key/value pairs where value does not match.
648 * if multi_replace==0, nothing, or only one matching key/value is replaced,
649 * else all matching key/values (regardless how many) are removed,
650 * before the new pair is written.
652 * Returns 0 on success.
654 * This function does this:
656 * - it locks the config file by creating ".perf/config.lock"
658 * - it then parses the config using store_aux() as validator to find
659 * the position on the key/value pair to replace. If it is to be unset,
660 * it must be found exactly once.
662 * - the config file is mmap()ed and the part before the match (if any) is
663 * written to the lock file, then the changed part and the rest.
665 * - the config file is removed and the lock file rename()d to it.
668 int perf_config_set_multivar(const char* key
, const char* value
,
669 const char* value_regex
, int multi_replace
)
674 char* config_filename
;
675 const char* last_dot
= strrchr(key
, '.');
677 if (config_exclusive_filename
)
678 config_filename
= strdup(config_exclusive_filename
);
680 config_filename
= perf_pathdup("config");
683 * Since "key" actually contains the section name and the real
684 * key name separated by a dot, we have to know where the dot is.
687 if (last_dot
== NULL
) {
688 error("key does not contain a section: %s", key
);
692 store
.baselen
= last_dot
- key
;
694 store
.multi_replace
= multi_replace
;
697 * Validate the key and while at it, lower case it for matching.
699 store
.key
= malloc(strlen(key
) + 1);
701 for (i
= 0; key
[i
]; i
++) {
702 unsigned char c
= key
[i
];
705 /* Leave the extended basename untouched.. */
706 if (!dot
|| i
> store
.baselen
) {
707 if (!iskeychar(c
) || (i
== store
.baselen
+1 && !isalpha(c
))) {
708 error("invalid key: %s", key
);
714 } else if (c
== '\n') {
715 error("invalid key (newline): %s", key
);
725 * If .perf/config does not exist yet, write a minimal version.
727 in_fd
= open(config_filename
, O_RDONLY
);
731 if ( ENOENT
!= errno
) {
732 error("opening %s: %s", config_filename
,
734 ret
= 3; /* same as "invalid config file" */
737 /* if nothing to unset, error out */
743 store
.key
= (char*)key
;
744 if (!store_write_section(fd
, key
) ||
745 !store_write_pair(fd
, key
, value
))
750 ssize_t contents_sz
, copy_begin
, copy_end
;
753 if (value_regex
== NULL
)
754 store
.value_regex
= NULL
;
756 if (value_regex
[0] == '!') {
757 store
.do_not_match
= 1;
760 store
.do_not_match
= 0;
762 store
.value_regex
= (regex_t
*)malloc(sizeof(regex_t
));
763 if (regcomp(store
.value_regex
, value_regex
,
765 error("invalid pattern: %s", value_regex
);
766 free(store
.value_regex
);
777 * After this, store.offset will contain the *end* offset
778 * of the last match, or remain at 0 if no match was found.
779 * As a side effect, we make sure to transform only a valid
780 * existing config file.
782 if (perf_config_from_file(store_aux
, config_filename
, NULL
)) {
783 error("invalid config file %s", config_filename
);
785 if (store
.value_regex
!= NULL
) {
786 regfree(store
.value_regex
);
787 free(store
.value_regex
);
794 if (store
.value_regex
!= NULL
) {
795 regfree(store
.value_regex
);
796 free(store
.value_regex
);
799 /* if nothing to unset, or too many matches, error out */
800 if ((store
.seen
== 0 && value
== NULL
) ||
801 (store
.seen
> 1 && multi_replace
== 0)) {
807 contents_sz
= xsize_t(st
.st_size
);
808 contents
= mmap(NULL
, contents_sz
, PROT_READ
,
809 MAP_PRIVATE
, in_fd
, 0);
815 for (i
= 0, copy_begin
= 0; i
< store
.seen
; i
++) {
816 if (store
.offset
[i
] == 0) {
817 store
.offset
[i
] = copy_end
= contents_sz
;
818 } else if (store
.state
!= KEY_SEEN
) {
819 copy_end
= store
.offset
[i
];
821 copy_end
= find_beginning_of_line(
822 contents
, contents_sz
,
823 store
.offset
[i
]-2, &new_line
);
825 if (copy_end
> 0 && contents
[copy_end
-1] != '\n')
828 /* write the first part of the config */
829 if (copy_end
> copy_begin
) {
830 if (write_in_full(fd
, contents
+ copy_begin
,
831 copy_end
- copy_begin
) <
832 copy_end
- copy_begin
)
835 write_in_full(fd
, "\n", 1) != 1)
838 copy_begin
= store
.offset
[i
];
841 /* write the pair (value == NULL means unset) */
843 if (store
.state
== START
) {
844 if (!store_write_section(fd
, key
))
847 if (!store_write_pair(fd
, key
, value
))
851 /* write the rest of the config */
852 if (copy_begin
< contents_sz
)
853 if (write_in_full(fd
, contents
+ copy_begin
,
854 contents_sz
- copy_begin
) <
855 contents_sz
- copy_begin
)
858 munmap(contents
, contents_sz
);
864 free(config_filename
);
873 * Call this to report error for your variable that should not
874 * get a boolean value (i.e. "[my] var" means "true").
876 int config_error_nonbool(const char *var
)
878 return error("Missing value for '%s'", var
);