From 7ced9ccef7fae0b392858b1b7f44978e1e6df1cf Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Sat, 12 Aug 2017 17:46:55 +0200 Subject: Added a batch mode and a way to load binaries from command line. --- ChangeLog | 11 ++++ src/analysis/db/server.c | 15 +++-- src/gui/menus/project.c | 6 +- src/main.c | 146 +++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 148 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9cc2a11..2a0707c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 17-08-12 Cyrille Bagard + * src/analysis/db/server.c: + Do not block on server close. + + * src/gui/menus/project.c: + Fix a memory leak. + + * src/main.c: + Add a batch mode and a way to load binaries from command line. + +17-08-12 Cyrille Bagard + * plugins/pychrysa/glibext/configuration.c: * plugins/pychrysa/gtkext/blockdisplay.c: * plugins/pychrysa/gtkext/bufferdisplay.c: diff --git a/src/analysis/db/server.c b/src/analysis/db/server.c index 0270201..6155b6d 100644 --- a/src/analysis/db/server.c +++ b/src/analysis/db/server.c @@ -497,7 +497,7 @@ static void *g_db_server_listener(GDbServer *server) if (ret != 1) continue; /* Le canal est fermé, une sortie doit être demandée... */ - if (fds.revents & POLLNVAL) + if (fds.revents & POLLHUP) break; if (fds.revents & (POLLIN | POLLPRI)) @@ -802,14 +802,21 @@ bool g_db_server_start(GDbServer *server) void g_db_server_stop(GDbServer *server) { - if (server->fd != -1) + int ret; /* Bilan d'un appel */ + + if (server->fd == -1) return; - close(server->fd); - server->fd = -1; + ret = shutdown(server->fd, SHUT_RDWR); + if (ret == -1) perror("shutdown"); g_thread_join(server->listener); + ret = close(server->fd); + if (ret == -1) perror("close"); + + server->fd = -1; + /* TODO : s'occuper des archives ouvertes */ } diff --git a/src/gui/menus/project.c b/src/gui/menus/project.c index d36a322..8c60f72 100644 --- a/src/gui/menus/project.c +++ b/src/gui/menus/project.c @@ -283,7 +283,11 @@ static void mcb_project_add_binary_file(GtkMenuItem *menuitem, GMenuBar *bar) content = g_file_content_new(filename); - qck_study_new_content(content, PCS_ROOT); + if (content != NULL) + { + qck_study_new_content(content, PCS_ROOT); + g_object_unref(G_OBJECT(content)); + } g_free(filename); diff --git a/src/main.c b/src/main.c index 9002ed9..64d5d2b 100644 --- a/src/main.c +++ b/src/main.c @@ -30,7 +30,9 @@ #include #include +#include "analysis/loading.h" #include "analysis/project.h" +#include "analysis/contents/file.h" #include "analysis/db/server.h" #include "common/xdg.h" #include "core/core.h" @@ -49,6 +51,17 @@ static void show_chrysalide_help(const char *); /* Affiche des indications sur la version courante du programme. */ static void show_chrysalide_version(void); +/* Recharge le dernier projet ouvert s'il existe. */ +static gboolean load_last_project(GGenConfig *); + +/* Ouvre les éventuels fichiers fournis au démarrage. */ +static int open_binaries(char **, int); + + + +/* Espace de référencement global. // FIXME à déplacer */ +static GObject *_ref = NULL; + /****************************************************************************** @@ -65,9 +78,14 @@ static void show_chrysalide_version(void); static void show_chrysalide_help(const char *name) { + char *base; /* Version courte du nom */ + + base = basename(name); + printf("\n"); - printf("Usage: %s [--help] [--version]\n", name); + printf("Usage: %s [--help] [--version]\n", base); + printf(" %s [--batch] \n", base); printf("\n"); @@ -76,6 +94,10 @@ static void show_chrysalide_help(const char *name) printf("\n"); + printf("\t--batch\t\tExit after processing files.\n"); + + printf("\n"); + } @@ -127,6 +149,7 @@ int main(int argc, char **argv) int result; /* Bilan de l'exécution */ bool show_help; /* Affichage de l'aide ? */ bool show_version; /* Affichage de la version ? */ + bool batch_mode; /* Exécution sans GUI ? */ int index; /* Indice d'argument */ int ret; /* Bilan d'un appel */ GtkWidget *editor; /* Fenêtre graphique */ @@ -143,6 +166,7 @@ int main(int argc, char **argv) static struct option long_options[] = { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, 'v' }, + { "batch", no_argument, NULL, 'b' }, { NULL, 0, NULL, 0 } }; @@ -160,9 +184,11 @@ int main(int argc, char **argv) show_help = false; show_version = false; + batch_mode = false; + while (true) { - ret = getopt_long(argc, argv, "hv", long_options, &index); + ret = getopt_long(argc, argv, "hvb", long_options, &index); if (ret == -1) break; switch (ret) @@ -175,6 +201,10 @@ int main(int argc, char **argv) show_version = true; break; + case 'b': + batch_mode = true; + break; + } } @@ -219,7 +249,10 @@ int main(int argc, char **argv) editor = create_editor(); if (editor == NULL) goto failed_to_load_editor; - gtk_widget_show_now(editor); + _ref = G_OBJECT(editor); + + if (!batch_mode) + gtk_widget_show_now(editor); init_work_queue(get_global_status()); @@ -246,36 +279,26 @@ int main(int argc, char **argv) /* Charge le dernier projet ? */ - gboolean load_last_project(GGenConfig *cfg) - { - const char *filename; /* Chemin du dernier projet */ - GStudyProject *project; /* Nouveau projet courant */ - - if (!g_generic_config_get_value(cfg, MPK_LAST_PROJECT, &filename)) - filename = NULL; - - if (filename == NULL) project = g_study_project_new(G_OBJECT(editor)); - else project = g_study_project_open(G_OBJECT(editor), filename); - - set_current_project(project); - - return G_SOURCE_REMOVE; - - } - - g_generic_config_get_value(config, MPK_WELCOME_STARTUP, &welcome); + if (batch_mode) + welcome = true; + else + g_generic_config_get_value(config, MPK_WELCOME_STARTUP, &welcome); if (!welcome) g_idle_add((GSourceFunc)load_last_project, config); else - set_current_project(g_study_project_new(G_OBJECT(editor))); + set_current_project(g_study_project_new(_ref)); /* Exécution du programme */ - result = EXIT_SUCCESS; + result = open_binaries(argv + optind, argc - optind); + + if (batch_mode) + g_work_queue_wait_for_completion(get_work_queue(), DEFAULT_WORK_GROUP); - gtk_main(); + else + gtk_main(); g_db_server_stop(server); @@ -283,7 +306,8 @@ int main(int argc, char **argv) exit_all_plugins(); - gtk_widget_destroy(editor); + if (!batch_mode) /* FIXME */ + gtk_widget_destroy(editor); failed_to_load_editor: @@ -294,3 +318,75 @@ int main(int argc, char **argv) return result; } + + +/****************************************************************************** +* * +* Paramètres : cfg = configuration globale sur laquelle s'appuyer. * +* ref = espace de référencement global. * +* * +* Description : Recharge le dernier projet ouvert s'il existe. * +* * +* Retour : G_SOURCE_REMOVE pour ne pas répéter l'action. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static gboolean load_last_project(GGenConfig *cfg) +{ + const char *filename; /* Chemin du dernier projet */ + GStudyProject *project; /* Nouveau projet courant */ + + if (!g_generic_config_get_value(cfg, MPK_LAST_PROJECT, &filename)) + filename = NULL; + + if (filename == NULL) project = g_study_project_new(_ref); + else project = g_study_project_open(_ref, filename); + + set_current_project(project); + + return G_SOURCE_REMOVE; + +} + + +/****************************************************************************** +* * +* Paramètres : files = noms de fichier fournis en ligne de commande. * +* count = nombre d'arguments restant à traiter. * +* * +* Description : Ouvre les éventuels fichiers fournis au démarrage. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int open_binaries(char **files, int count) +{ + int result; /* Bilan à retourner */ + int i; /* Boucle de parcours */ + GBinContent *content; /* Contenu binaire à charger */ + + result = EXIT_SUCCESS; + + for (i = 0; i < count && result == EXIT_SUCCESS; i++) + { + content = g_file_content_new(files[i]); + + if (content != NULL) + { + qck_study_new_content(content, PCS_ROOT); + g_object_unref(G_OBJECT(content)); + } + + else + result = EXIT_FAILURE; + + } + + return result; + +} -- cgit v0.11.2-87-g4458