Move the system config file search after option processing

For one, this allows us to use MSG_DEBUG in order to emit debug
messages (requires -v processing).

As another effect, if the -C conffile option was given, there is no
need at all to run through all the process of looking up a system
config file - it's right there already.

Also, move it after the logfile creation if -l logfile was given, so
the respective debug message can go to the logfile.
This commit is contained in:
Joerg Wunsch 2022-01-04 22:45:47 +01:00
parent eff180353e
commit 374861f62e
1 changed files with 128 additions and 125 deletions

View File

@ -362,6 +362,8 @@ int main(int argc, char * argv [])
setvbuf(stdout, (char*)NULL, _IOLBF, 0);
setvbuf(stderr, (char*)NULL, _IOLBF, 0);
sys_config[0] = '\0';
progname = strrchr(argv[0],'/');
#if defined (WIN32NATIVE)
@ -422,131 +424,6 @@ int main(int argc, char * argv [])
is_open = 0;
logfile = NULL;
/*
* EXECUTABLE ABSPATH
* ------------------
* Determine the absolute path to avrdude executable. This will be used to
* locate the 'avrdude.conf' file later.
*/
int executable_dirpath_len;
int executable_abspath_len = wai_getExecutablePath(
executable_abspath,
PATH_MAX,
&executable_dirpath_len
);
if (
(executable_abspath_len != -1) &&
(executable_abspath_len != 0) &&
(executable_dirpath_len != -1) &&
(executable_dirpath_len != 0)
) {
// All requirements satisfied, executable path was found
executable_abspath_found = true;
// Make sure the string is null terminated
executable_abspath[executable_abspath_len] = '\0';
// Replace all backslashes with forward slashes
i = 0;
while (true) {
if (executable_abspath[i] == '\0') {
break;
}
if (executable_abspath[i] == '\\') {
executable_abspath[i] = '/';
}
i++;
}
// Define 'executable_dirpath' to be the path to the parent folder of the
// executable.
strcpy(executable_dirpath, executable_abspath);
executable_dirpath[executable_dirpath_len] = '\0';
// Debug output
// avrdude_message(MSG_INFO, "executable_abspath = %s\n", executable_abspath);
// avrdude_message(MSG_INFO, "executable_abspath_len = %i\n", executable_abspath_len);
// avrdude_message(MSG_INFO, "executable_dirpath = %s\n", executable_dirpath);
// avrdude_message(MSG_INFO, "executable_dirpath_len = %i\n", executable_dirpath_len);
}
/*
* SYSTEM CONFIG
* -------------
* Determine the location of 'avrdude.conf'. Check in this order:
* 1. <dirpath of executable>/../etc/avrdude.conf
* 2. <dirpath of executable>/avrdude.conf
* 3. CONFIG_DIR/avrdude.conf
*
* When found, write the result into the 'sys_config' variable.
*/
if (executable_abspath_found) {
// 1. Check <dirpath of executable>/../etc/avrdude.conf
strcpy(sys_config, executable_dirpath);
sys_config[PATH_MAX - 1] = '\0';
i = strlen(sys_config);
if (i && (sys_config[i - 1] != '/'))
strcat(sys_config, "/");
strcat(sys_config, "../etc/avrdude.conf");
sys_config[PATH_MAX - 1] = '\0';
if (access(sys_config, F_OK) == 0) {
sys_config_found = true;
}
else {
// 2. Check <dirpath of executable>/avrdude.conf
strcpy(sys_config, executable_dirpath);
sys_config[PATH_MAX - 1] = '\0';
i = strlen(sys_config);
if (i && (sys_config[i - 1] != '/'))
strcat(sys_config, "/");
strcat(sys_config, "avrdude.conf");
sys_config[PATH_MAX - 1] = '\0';
if (access(sys_config, F_OK) == 0) {
sys_config_found = true;
}
}
}
if (!sys_config_found) {
// 3. Check CONFIG_DIR/avrdude.conf
#if defined(WIN32NATIVE)
win_sys_config_set(sys_config);
#else
strcpy(sys_config, CONFIG_DIR);
i = strlen(sys_config);
if (i && (sys_config[i - 1] != '/'))
strcat(sys_config, "/");
strcat(sys_config, "avrdude.conf");
#endif
if (access(sys_config, F_OK) == 0) {
sys_config_found = true;
}
}
// Debug output
// avrdude_message(MSG_INFO, "sys_config = %s\n", sys_config);
// avrdude_message(MSG_INFO, "sys_config_found = %s\n", sys_config_found ? "true" : "false");
// avrdude_message(MSG_INFO, "\n");
/*
* USER CONFIG
* -----------
* Determine the location of '.avrduderc'. Nothing changed here.
*/
#if defined(WIN32NATIVE)
win_usr_config_set(usr_config);
#else
usr_config[0] = 0;
homedir = getenv("HOME");
if (homedir != NULL) {
strcpy(usr_config, homedir);
i = strlen(usr_config);
if (i && (usr_config[i - 1] != '/'))
strcat(usr_config, "/");
strcat(usr_config, ".avrduderc");
}
#endif
len = strlen(progname) + 2;
for (i=0; i<len; i++)
progbuf[i] = ' ';
@ -764,6 +641,132 @@ int main(int argc, char * argv [])
}
}
/* search for system configuration file unless -C conffile was given */
if (strlen(sys_config) == 0) {
/*
* EXECUTABLE ABSPATH
* ------------------
* Determine the absolute path to avrdude executable. This will be used to
* locate the 'avrdude.conf' file later.
*/
int executable_dirpath_len;
int executable_abspath_len = wai_getExecutablePath(
executable_abspath,
PATH_MAX,
&executable_dirpath_len
);
if (
(executable_abspath_len != -1) &&
(executable_abspath_len != 0) &&
(executable_dirpath_len != -1) &&
(executable_dirpath_len != 0)
) {
// All requirements satisfied, executable path was found
executable_abspath_found = true;
// Make sure the string is null terminated
executable_abspath[executable_abspath_len] = '\0';
// Replace all backslashes with forward slashes
i = 0;
while (true) {
if (executable_abspath[i] == '\0') {
break;
}
if (executable_abspath[i] == '\\') {
executable_abspath[i] = '/';
}
i++;
}
// Define 'executable_dirpath' to be the path to the parent folder of the
// executable.
strcpy(executable_dirpath, executable_abspath);
executable_dirpath[executable_dirpath_len] = '\0';
// Debug output
avrdude_message(MSG_DEBUG, "executable_abspath = %s\n", executable_abspath);
avrdude_message(MSG_DEBUG, "executable_abspath_len = %i\n", executable_abspath_len);
avrdude_message(MSG_DEBUG, "executable_dirpath = %s\n", executable_dirpath);
avrdude_message(MSG_DEBUG, "executable_dirpath_len = %i\n", executable_dirpath_len);
}
/*
* SYSTEM CONFIG
* -------------
* Determine the location of 'avrdude.conf'. Check in this order:
* 1. <dirpath of executable>/../etc/avrdude.conf
* 2. <dirpath of executable>/avrdude.conf
* 3. CONFIG_DIR/avrdude.conf
*
* When found, write the result into the 'sys_config' variable.
*/
if (executable_abspath_found) {
// 1. Check <dirpath of executable>/../etc/avrdude.conf
strcpy(sys_config, executable_dirpath);
sys_config[PATH_MAX - 1] = '\0';
i = strlen(sys_config);
if (i && (sys_config[i - 1] != '/'))
strcat(sys_config, "/");
strcat(sys_config, "../etc/avrdude.conf");
sys_config[PATH_MAX - 1] = '\0';
if (access(sys_config, F_OK) == 0) {
sys_config_found = true;
}
else {
// 2. Check <dirpath of executable>/avrdude.conf
strcpy(sys_config, executable_dirpath);
sys_config[PATH_MAX - 1] = '\0';
i = strlen(sys_config);
if (i && (sys_config[i - 1] != '/'))
strcat(sys_config, "/");
strcat(sys_config, "avrdude.conf");
sys_config[PATH_MAX - 1] = '\0';
if (access(sys_config, F_OK) == 0) {
sys_config_found = true;
}
}
}
if (!sys_config_found) {
// 3. Check CONFIG_DIR/avrdude.conf
#if defined(WIN32NATIVE)
win_sys_config_set(sys_config);
#else
strcpy(sys_config, CONFIG_DIR);
i = strlen(sys_config);
if (i && (sys_config[i - 1] != '/'))
strcat(sys_config, "/");
strcat(sys_config, "avrdude.conf");
#endif
if (access(sys_config, F_OK) == 0) {
sys_config_found = true;
}
}
}
// Debug output
avrdude_message(MSG_DEBUG, "sys_config = %s\n", sys_config);
avrdude_message(MSG_DEBUG, "sys_config_found = %s\n", sys_config_found ? "true" : "false");
avrdude_message(MSG_DEBUG, "\n");
/*
* USER CONFIG
* -----------
* Determine the location of '.avrduderc'. Nothing changed here.
*/
#if defined(WIN32NATIVE)
win_usr_config_set(usr_config);
#else
usr_config[0] = 0;
homedir = getenv("HOME");
if (homedir != NULL) {
strcpy(usr_config, homedir);
i = strlen(usr_config);
if (i && (usr_config[i - 1] != '/'))
strcat(usr_config, "/");
strcat(usr_config, ".avrduderc");
}
#endif
if (quell_progress == 0) {
if (isatty (STDERR_FILENO))
update_progress = update_progress_tty;