Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 1b271808 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

MISC: Less spagetti code with the FAIL_{IF,UNLESS} macros

parent 62d8af83
Branches
Étiquettes
1 requête de fusion!144Resolve "Write MKV tags without mkvpropedit as a dependency for the AppImage"
Pipeline #2122 réussi avec des avertissements
......@@ -49,7 +49,12 @@
LOG_DEBUG("DEBUG", "%s", msg); \
goto label; \
}
#define FAIL_IF(cond, ...) { \
if (cond) { \
LOG_FATAL(__VA_ARGS__); \
}}
#define FAIL_UNLESS(cond, ...) FAIL_IF(!(cond), __VA_ARGS__)
#define GOTO_UNLESS(cond, msg, label) GOTO_IF(!(cond), msg, label)
#define RETURN_UNLESS(cond, msg, ret) RETURN_IF(!(cond), msg, ret)
#define NOTHING /* Usefull to return nothing in previous macros */
......
......@@ -61,15 +61,11 @@ error:
__attribute__((destructor))
static void ___tty_enable_echo(void)
{
if (!___tty_must_be_restored) {
LOG_DEBUG("INIT", "No need to restore the tty");
return;
}
if (tcsetattr(STDIN_FILENO, TCSANOW, &___tty_save) == -1)
LOG_ERROR("INIT", "Failed to reset termios flags");
RETURN_UNLESS(___tty_must_be_restored, "No need to restore the tty", NOTHING);
RETURN_IF(tcsetattr(STDIN_FILENO, TCSANOW, &___tty_save) == -1,
"Failed to reset termios flags", NOTHING);
errno = 0;
if (fflush(stdin) != 0)
LOG_ERROR("INIT", "Failed to flush stdin: %s", strerror(errno));
RETURN_IF(fflush(stdin) != 0, "Failed to flush stdin", NOTHING);
}
/* Recursive mkdir, where the last word of the string is a file, not a folder. */
......@@ -319,43 +315,31 @@ main(int argc, char *argv[])
memset(&srv, 0, sizeof(struct lkt_state));
srv.kara_prefix = kara_dir;
if (lkt_queue_new(&srv.queue)) {
LOG_ERROR("INIT", "Faield to create server queue");
exit(EXIT_FAILURE);
}
/* Initialize the system. */
if (!database_new(&srv.db)) {
LOG_ERROR("INIT", "Failed to init memory db");
exit(EXIT_FAILURE);
}
FAIL_IF(lkt_queue_new(&srv.queue), "Failed to create server queue");
FAIL_UNLESS(database_new(&srv.db), "Failed to init memory database");
/* Read or create default config file. */
int retry_config_once = 0;
retry_config:
if (conf_file[0] == '\0' && config_detect_file(conf_file, PATH_MAX)) {
if (retry_config_once) {
LOG_ERROR("INIT", "Failed to find a config file");
exit(EXIT_FAILURE);
} else {
FAIL_IF(retry_config_once, "Failed to find a config file");
LOG_INFO("INIT", "Creating default config file");
config_default_file(conf_file, PATH_MAX);
__mkdir(conf_file); /* Create the folder for the file. */
FILE *file_desc = fopen(conf_file, "w+");
if (NULL == file_desc)
LOG_FATAL("Failed to open default config file '%s' to initialize it", conf_file);
FAIL_UNLESS(file_desc, "Failed to open default config file '%s' to initialize it",
conf_file);
config_default(file_desc);
fclose(file_desc);
LOG_INFO("INIT", "Default configuration file has been writen to %s", conf_file);
retry_config_once = 1;
goto retry_config;
}
}
if (config_new(srv.db, conf_file)) {
LOG_ERROR("INIT", "Failed to read the config");
exit(EXIT_FAILURE);
}
FAIL_IF(config_new(srv.db, conf_file), "Failed to read the config");
/* Dump and abort here, if we are dumping informations about the server */
if (dump_and_abort) {
......@@ -374,15 +358,13 @@ retry_config:
database_config_get(srv.db, "server", "host", srv.host, HOST_NAME_MAX);
database_config_get(srv.db, "server", "port", srv.port, 5);
/* Quick check with an explicit error message, to remide the users to create the kara folder */
if (access(kara_dir, R_OK | W_OK)) {
LOG_ERROR("INIT", "No access in read / write for folder %s", kara_dir);
exit(EXIT_FAILURE);
}
/* Quick check with an explicit error message, to remide the users to
* create the kara folder */
FAIL_IF(access(kara_dir, R_OK | W_OK), "No access in read / write for folder %s", kara_dir);
/* Finish to initialize. */
database_config_get(srv.db, "database", "db_path", db_path, PATH_MAX);
RETURN_UNLESS(database_open(srv.db, db_path, check_exclusive), "Can't open database", 1);
FAIL_UNLESS(database_open(srv.db, db_path, check_exclusive), "Can't open database");
if (kara_dir[strlen(kara_dir) - 1] != '/')
strncat(kara_dir, "/", PATH_MAX - 1);
......@@ -396,22 +378,18 @@ retry_config:
lkt_queue_make_available(&srv.queue, lkt_event_prop);
lkt_queue_make_available(&srv.queue, lkt_event_update);
{ /* Module initialization */
char *module = safe_malloc(sizeof(char) * PATH_MAX);
if (!module) {
LOG_ERROR("INIT", "Out of memory");
return 1;
}
/* Module initialization */
{
char module[PATH_MAX];
database_config_get(srv.db, "player", "module", module, PATH_MAX);
reg_import(module, &srv.window_mod.reg, &srv.window_mod.handle);
database_config_get(srv.db, "repo", "module", module, PATH_MAX);
reg_import(module, &srv.repo_mod.reg, &srv.repo_mod.handle);
free(module);
if (MOD_CALL(srv.repo_mod, "new", &srv.queue, srv.db))
return 100;
if (MOD_CALL(srv.window_mod, "new", &srv.queue, srv.db))
return 101;
FAIL_IF(MOD_CALL(srv.repo_mod, "new", &srv.queue, srv.db), "Can't init repo module");
FAIL_IF(MOD_CALL(srv.window_mod, "new", &srv.queue, srv.db), "Can't init player module");
}
/* Get ENV */
......@@ -424,10 +402,9 @@ retry_config:
}
}
LOG_INFO("INIT", "Lektor was %s",
env_get(LKT_ENV_RESTART) ? "restarted" : "started");
LOG_INFO("INIT", "Lektor was %s", env_get(LKT_ENV_RESTART) ? "restarted" : "started");
config_handle_hook(srv.db, "launched");
lkt_listen(&srv);
LOG_FATAL("The lkt_listen function returned, it shouldn't happen");
return EXIT_FAILURE;
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter