00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "heraia_types.h"
00039
00040
00041 static gboolean version(void);
00042 static gboolean usage(int status);
00043 static window_prop_t *init_window_properties(gint x, gint y, guint height, guint width, gboolean displayed);
00044 static heraia_struct_t *init_window_property_struct(heraia_struct_t *main_struct);
00045 static heraia_struct_t *heraia_init_main_struct(gchar *heraia_path);
00046 static HERAIA_ERROR init_heraia_plugin_system(heraia_struct_t *main_struct);
00047 static GList *init_heraia_location_list(gchar *heraia_path);
00048 static gboolean manage_command_line_options(Options *opt, int argc, char **argv);
00049
00050
00051
00052
00053
00054
00055 static heraia_struct_t *libheraia_main_struct = NULL;
00056
00057
00058
00059
00060
00061
00062
00063 heraia_struct_t *get_main_struct(void)
00064 {
00065 return libheraia_main_struct;
00066 }
00067
00068
00069
00070
00071
00072
00073 static gboolean version(void)
00074 {
00075 fprintf (stdout, Q_("heraia written by %s\n %s - Version %s\n License %s\n"), HERAIA_AUTHORS, HERAIA_DATE, HERAIA_VERSION, HERAIA_LICENSE);
00076 return TRUE;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 static gboolean usage(int status)
00090 {
00091 if (status == 0)
00092 {
00093 fprintf (stderr, Q_("Try `heraia --help' for more information.\n"));
00094
00095 return FALSE;
00096 }
00097 else
00098 {
00099 version();
00100 fprintf(stdout, Q_("\nheraia is a simple hexadecimal file editor and file analyser"));
00101 fprintf(stdout, Q_("\nUsage :\n heraia [options] filename(s)\n"));
00102 fprintf(stdout, Q_("\nOptions :\n"));
00103 fprintf(stdout, Q_(" -h, --help\tThis help.\n"));
00104 fprintf(stdout, Q_(" -v, --version\tProgram version information.\n"));
00105 fprintf(stdout, Q_(" -t, --tests=TESTS\tRuns some tests.\n"));
00106 fprintf(stdout, Q_(" TESTS might be :\n"));
00107 fprintf(stdout, Q_(" %d for coverage tests\n"), COVERAGE_TESTS);
00108 fprintf(stdout, Q_(" %d for loading files tests\n"), LOADING_TESTS);
00109
00110 return TRUE;
00111 }
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 static window_prop_t *init_window_properties(gint x, gint y, guint height, guint width, gboolean displayed)
00125 {
00126 window_prop_t *window_p;
00127
00128
00129 window_p = (window_prop_t *) g_malloc0(sizeof(window_prop_t));
00130
00131
00132 window_p->x = x;
00133 window_p->y = y;
00134 window_p->height = height;
00135 window_p->width = width;
00136 window_p->displayed = displayed;
00137
00138 return window_p;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147 static heraia_struct_t *init_window_property_struct(heraia_struct_t *main_struct)
00148 {
00149 all_window_prop_t *win_prop = NULL;
00150 window_prop_t *about_box = NULL;
00151 window_prop_t *data_interpretor = NULL;
00152 window_prop_t *log_box = NULL;
00153 window_prop_t *main_dialog = NULL;
00154 window_prop_t *plugin_list = NULL;
00155 window_prop_t *ldt = NULL;
00156 window_prop_t *main_pref_window = NULL;
00157 window_prop_t *goto_window = NULL;
00158 window_prop_t *result_window = NULL;
00159 window_prop_t *find_window = NULL;
00160 window_prop_t *fr_window = NULL;
00161
00162
00163 win_prop = (all_window_prop_t *) g_malloc0(sizeof(all_window_prop_t));
00164
00165
00166 about_box = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, FALSE);
00167 data_interpretor = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, H_DI_DISPLAYED);
00168 log_box = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, FALSE);
00169 main_dialog = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, TRUE);
00170 plugin_list = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, FALSE);
00171 ldt = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, FALSE);
00172 main_pref_window = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, FALSE);
00173 goto_window = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, FALSE);
00174 result_window = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, FALSE);
00175 find_window = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, FALSE);
00176 fr_window = init_window_properties(0, 0, WPT_DEFAULT_HEIGHT, WPT_DEFAULT_WIDTH, FALSE);
00177
00178
00179
00180 win_prop->about_box = about_box;
00181 win_prop->data_interpretor = data_interpretor;
00182 win_prop->log_box = log_box;
00183 win_prop->main_dialog = main_dialog;
00184 win_prop->plugin_list = plugin_list;
00185 win_prop->ldt = ldt;
00186 win_prop->main_pref_window = main_pref_window;
00187 win_prop->goto_window = goto_window;
00188 win_prop->result_window = result_window;
00189 win_prop->find_window = find_window;
00190 win_prop->fr_window = fr_window;
00191
00192
00193 main_struct->win_prop = win_prop;
00194
00195 return main_struct;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205 static heraia_struct_t *heraia_init_main_struct(gchar *heraia_path)
00206 {
00207 heraia_struct_t *main_struct = NULL;
00208 xml_t *xmls = NULL;
00209
00210 main_struct = (heraia_struct_t *) g_malloc0(sizeof(heraia_struct_t));
00211
00212 if (main_struct == NULL)
00213 {
00214 fprintf(stderr, Q_("Main structure could not be initialiazed !"));
00215 fprintf(stderr, Q_("Do you have a memory problem ?\n"));
00216 return NULL;
00217 }
00218
00219
00220 main_struct->prefs = NULL;
00221 init_preference_struct(main_struct);
00222 verify_preference_file(main_struct->prefs->pathname, main_struct->prefs->filename);
00223
00224
00225
00226
00227
00228 main_struct->debug = ENABLE_DEBUG;
00229
00230 main_struct->current_doc = NULL;
00231 main_struct->plugins_list = NULL;
00232 main_struct->location_list = init_heraia_location_list(heraia_path);
00233
00234
00235 xmls = (xml_t *) g_malloc0(sizeof(xml_t));
00236 xmls->main = NULL;
00237 main_struct->xmls = xmls;
00238
00239
00240 main_struct->current_DW = (data_window_t *) g_malloc0 (sizeof(data_window_t));
00241 main_struct->current_DW->diw = NULL;
00242 main_struct->current_DW->tab_displayed = 0;
00243
00244
00245 main_struct = init_window_property_struct(main_struct);
00246
00247
00248 main_struct->documents = g_ptr_array_new();
00249
00250
00251 main_struct->find_doc = NULL;
00252 main_struct->fr_find_doc = NULL;
00253 main_struct->fr_replace_doc = NULL;
00254 main_struct->results = g_ptr_array_new();
00255
00256
00257 libheraia_main_struct = main_struct;
00258
00259 return main_struct;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 static HERAIA_ERROR init_heraia_plugin_system(heraia_struct_t *main_struct)
00272 {
00273
00274
00275 if (plugin_capable() == TRUE)
00276 {
00277 log_message(main_struct, G_LOG_LEVEL_INFO, Q_("Enabling plugins"));
00278 load_plugins(main_struct);
00279
00280
00281 log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("Inits the plugin list window"));
00282 plugin_list_window_init_interface(main_struct);
00283
00284 return HERAIA_NOERR;
00285 }
00286 else
00287 {
00288 log_message(main_struct, G_LOG_LEVEL_WARNING, Q_("Plugins will be disabled"));
00289 return HERAIA_NO_PLUGINS;
00290 }
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301 static GList *init_heraia_location_list(gchar *heraia_path)
00302 {
00303 gchar *path = NULL;
00304 const gchar* const *system_dirs;
00305 guint i = 0;
00306 GList *location_list = NULL;
00307
00308
00309 path = g_get_current_dir();
00310 location_list = g_list_prepend(location_list, path);
00311
00312
00313 system_dirs = g_get_system_data_dirs();
00314 i = 0;
00315 while(system_dirs[i] != NULL)
00316 {
00317 path = g_build_path(G_DIR_SEPARATOR_S, system_dirs[i], "heraia", NULL);
00318 location_list = g_list_prepend(location_list, path);
00319 i++;
00320 }
00321
00322
00323 system_dirs = g_get_system_config_dirs();
00324 i = 0;
00325 while(system_dirs[i] != NULL)
00326 {
00327 path = g_build_path(G_DIR_SEPARATOR_S, system_dirs[i], "heraia", NULL);
00328 location_list = g_list_prepend(location_list, path);
00329 i++;
00330 }
00331
00332
00333 path = g_build_path(G_DIR_SEPARATOR_S, g_get_home_dir(), "heraia", NULL);
00334 location_list = g_list_prepend(location_list, path);
00335
00336
00337 path = g_build_path(G_DIR_SEPARATOR_S, g_get_user_data_dir(), "heraia", NULL);
00338 location_list = g_list_prepend(location_list, path);
00339
00340
00341 path = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), "heraia", NULL);
00342 location_list = g_list_prepend(location_list, path);
00343
00344
00345 path = g_build_path(G_DIR_SEPARATOR_S, heraia_path, "..", "share", "heraia", NULL);
00346 location_list = g_list_prepend(location_list, path);
00347
00348 return location_list;
00349 }
00350
00351
00352
00353
00354
00355 static void init_international_languages(void)
00356 {
00357 gchar *result = NULL;
00358
00359 setlocale(LC_ALL, "");
00360 result = bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
00361
00362 if (ENABLE_DEBUG == TRUE)
00363 {
00364 fprintf(stdout, Q_("Gettext package : %s\n"), GETTEXT_PACKAGE);
00365 fprintf(stdout, Q_("Locale dir : %s\n"), LOCALEDIR);
00366 fprintf(stdout, Q_("Bindtextdomain : %s\n"), result);
00367 }
00368 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
00369 textdomain(GETTEXT_PACKAGE);
00370 }
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 static gboolean do_heraia_coverage_tests(heraia_struct_t *main_struct)
00383 {
00384 heraia_struct_t *tmp_struct;
00385 gboolean exit_value = FALSE;
00386
00387 exit_value = version();
00388 exit_value = usage(0);
00389 exit_value = usage(1);
00390
00391 tmp_struct = get_main_struct();
00392 if (tmp_struct != main_struct)
00393 {
00394 fprintf(stderr, Q_("WARNING : tmp_struct is not equal to main_struct!\n"));
00395 }
00396
00397 exit_value = test_decode_functions();
00398 if (exit_value != TRUE)
00399 {
00400 fprintf(stderr, Q_("WARNING : Error while testing decode functions\n"));
00401 }
00402
00403 return TRUE;
00404 }
00405
00406
00407
00408
00409
00410
00411
00412 static gboolean do_heraia_loading_tests(heraia_struct_t *main_struct)
00413 {
00414 return TRUE;
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 static gboolean manage_command_line_options(Options *opt, int argc, char **argv)
00427 {
00428 int exit_value = TRUE;
00429 int c = 0;
00430 gchar *filename = NULL;
00431 int tests = 0;
00432
00433 while ((c = getopt_long(argc, argv, "vht", long_options, NULL)) != -1)
00434 {
00435 switch (c)
00436 {
00437 case 0:
00438 break;
00439
00440 case 'v':
00441 exit_value = version();
00442 opt->usage = TRUE;
00443 break;
00444
00445 case 'h':
00446 exit_value = usage(1);
00447 opt->usage = TRUE;
00448 break;
00449
00450 case 't':
00451 if (optarg)
00452 {
00453 if (sscanf(optarg, "%d", &tests) < 1)
00454 {
00455
00456 opt->tests = COVERAGE_TESTS;
00457 }
00458 else
00459 {
00460 opt->tests = tests;
00461 }
00462 }
00463 else
00464 {
00465 opt->tests = COVERAGE_TESTS;
00466 }
00467 exit_value = TRUE;
00468 break;
00469
00470 default:
00471 exit_value = usage(0);
00472 opt->usage = TRUE;
00473 }
00474 }
00475
00476 while (optind < argc)
00477 {
00478 filename = (char *) malloc (sizeof(char) * strlen(argv[optind]) + 1);
00479 strcpy(filename, argv[optind]);
00480 opt->filenames = g_list_prepend(opt->filenames, filename);
00481 optind++;
00482 }
00483
00484 return exit_value;
00485 }
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495 static Options *init_options_struct(void)
00496 {
00497 Options *opt = NULL;
00498
00499 opt = (Options *) g_malloc0(sizeof(Options));
00500
00501 opt->filenames = NULL;
00502 opt->usage = FALSE;
00503 opt->tests = NO_TESTS;
00504
00505 return opt;
00506 }
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 int main(int argc, char **argv)
00517 {
00518 Options *opt;
00519 gboolean exit_value = TRUE;
00520 heraia_struct_t *main_struct = NULL;
00521 GList *list = NULL;
00522 gchar *heraia_path = NULL;
00523
00524 if (argv != NULL && argv[0] != NULL)
00525 {
00526 heraia_path = g_path_get_dirname(argv[0]);
00527 }
00528 else
00529 {
00530 return -1;
00531 }
00532
00533 init_international_languages();
00534
00535 opt = init_options_struct();
00536
00537
00538
00539 main_struct = heraia_init_main_struct(heraia_path);
00540
00541 libheraia_initialize();
00542
00543 if (main_struct->debug == TRUE)
00544 {
00545 fprintf(stdout, Q_("Main struct initialized !\n"));
00546 }
00547
00548
00549 exit_value = manage_command_line_options(opt, argc, argv);
00550
00551 if (opt->usage != TRUE)
00552 {
00553 if (main_struct->debug == TRUE)
00554 {
00555 fprintf(stderr, Q_("Beginning things\n"));
00556 libheraia_test();
00557 }
00558
00559
00560 exit_value = gtk_init_check(&argc, &argv);
00561
00562 if (load_heraia_ui(main_struct) == TRUE)
00563 {
00564
00565 log_message(main_struct, G_LOG_LEVEL_INFO, Q_("Main interface loaded"));
00566 log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("Preference file is %s"), main_struct->prefs->filename);
00567 log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("data interpretor's tab is %d"), main_struct->current_DW->tab_displayed);
00568
00569 init_heraia_plugin_system(main_struct);
00570
00571 if (opt->filenames != NULL)
00572 {
00573 list = g_list_first(opt->filenames);
00574 while (list != NULL)
00575 {
00576 load_file_to_analyse(main_struct, list->data);
00577 list = g_list_next(list);
00578 }
00579 }
00580
00581 log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("main_struct : %p"), main_struct);
00582
00583 init_heraia_interface(main_struct);
00584
00585
00586 switch (opt->tests)
00587 {
00588 case COVERAGE_TESTS:
00589 exit_value = do_heraia_coverage_tests(main_struct);
00590 break;
00591
00592 case LOADING_TESTS:
00593 exit_value = do_heraia_loading_tests(main_struct);
00594 break;
00595
00596 default:
00597
00598 gtk_main();
00599 exit_value = TRUE;
00600 }
00601 }
00602 else
00603 {
00604 fprintf(stderr, Q_("File heraia.gtkbuilder not found !\n"));
00605 if (main_struct->debug == TRUE)
00606 {
00607 list = main_struct->location_list;
00608 while (list)
00609 {
00610
00611 heraia_path = (gchar *) list->data;
00612 fprintf(stdout, "\t%s\n", heraia_path);
00613 list = g_list_next(list);
00614 }
00615 }
00616 }
00617 }
00618
00619 libheraia_finalize();
00620
00621 return !exit_value;
00622 }