1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/*
* Copyright (C) 2007-2016 Tristan Heaven <tristan@tristanheaven.net>
*
* This file is part of GtkHash.
*
* GtkHash is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* GtkHash is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GtkHash. If not, see <https://gnu.org/licenses/gpl-2.0.txt>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <gtk/gtk.h>
#include "main.h"
#include "hash.h"
#include "gui.h"
#include "list.h"
#include "prefs.h"
#include "resources.h"
#include "check.h"
#include "uri-digest.h"
static struct {
const char *check;
const char **check_files;
const char *text;
const char **funcs;
const char **files;
gboolean version;
} opts = {
.check = NULL,
.check_files = NULL,
.text = NULL,
.funcs = NULL,
.files = NULL,
.version = false,
};
static void free_opts(void)
{
if (opts.check) {
g_free((void *)opts.check);
opts.check = NULL;
}
if (opts.check_files) {
g_strfreev((char **)opts.check_files);
opts.check_files = NULL;
}
if (opts.text) {
g_free((void *)opts.text);
opts.text = NULL;
}
if (opts.funcs) {
g_strfreev((char **)opts.funcs);
opts.funcs = NULL;
}
if (opts.files) {
g_strfreev((char **)opts.files);
opts.files = NULL;
}
}
static char *filename_arg_to_uri(const char * const arg)
{
GFile *file = g_file_new_for_commandline_arg(arg);
char *uri = g_file_get_uri(file);
g_object_unref(file);
return uri;
}
static void read_opts_preinit(int *argc, char ***argv)
{
GOptionEntry entries[] = {
{
"check", 'c', 0, G_OPTION_ARG_STRING, &opts.check,
C_(PACKAGE " --help",
"Check against the specified digest or checksum"),
C_(PACKAGE " --help", "DIGEST")
},
{
"check-file", 'C', 0, G_OPTION_ARG_STRING_ARRAY, &opts.check_files,
C_(PACKAGE " --help",
"Check digests or checksums from the specified file"),
C_(PACKAGE " --help", "FILE|URI")
},
{
"function", 'f', 0, G_OPTION_ARG_STRING_ARRAY, &opts.funcs,
C_(PACKAGE " --help",
"Enable the specified Hash Function (e.g. MD5)"),
C_(PACKAGE " --help", "FUNCTION")
},
{
"text", 't', 0, G_OPTION_ARG_STRING, &opts.text,
C_(PACKAGE " --help", "Hash the specified text"),
C_(PACKAGE " --help", "TEXT")
},
{
"version", 'v', 0, G_OPTION_ARG_NONE, &opts.version,
C_(PACKAGE " --help", "Show version information"), NULL
},
{
G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &opts.files,
NULL, C_(PACKAGE " --help", "[FILE|URI...]")
},
{ NULL, 0, 0, 0, NULL, NULL, NULL }
};
GOptionContext *context = g_option_context_new(NULL);
GError *error = NULL;
g_option_context_add_main_entries(context, entries, GETTEXT_PACKAGE);
g_option_context_add_group(context, gtk_get_option_group(false));
g_option_context_parse(context, argc, argv, &error);
g_option_context_free(context);
if (error) {
g_warning("%s", error->message);
g_error_free(error);
free_opts();
exit(EXIT_FAILURE);
}
if (opts.version) {
printf("%s\n", PACKAGE_STRING);
free_opts();
exit(EXIT_SUCCESS);
}
if (opts.funcs)
hash_funcs_enable_strv(opts.funcs);
}
static void read_opts_postinit(void)
{
if (opts.check && *opts.check)
gui_add_check(opts.check);
GSList *ud_list = NULL;
if (opts.check_files) {
for (int i = 0; opts.check_files[i]; i++) {
GFile *file = g_file_new_for_commandline_arg(opts.check_files[i]);
ud_list = check_file_load(ud_list, file);
g_object_unref(file);
}
}
if (opts.files) {
for (int i = 0; opts.files[i]; i++) {
struct uri_digest_s *ud =
uri_digest_new(filename_arg_to_uri(opts.files[i]), NULL);
ud_list = g_slist_prepend(ud_list, ud);
}
}
bool files_added = false;
if (ud_list) {
ud_list = g_slist_reverse(ud_list);
files_added = gui_add_ud_list(ud_list, GUI_VIEW_INVALID);
uri_digest_list_free_full(ud_list);
}
if (files_added) {
gui_update();
gui_start_hash();
} else if (opts.text) {
gui_add_text(opts.text);
} else if (GUI_VIEW_IS_VALID(gui.view)) {
// view was loaded from prefs
gui_update();
}
free_opts();
}
int main(int argc, char **argv)
{
#if ENABLE_NLS
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
textdomain(GETTEXT_PACKAGE);
#endif
hash_init();
atexit(hash_deinit);
read_opts_preinit(&argc, &argv);
gtk_init(NULL, NULL);
// Init gui using GResource data
resources_register_resource();
gui_init();
atexit(gui_deinit);
resources_unregister_resource();
list_init();
prefs_init();
atexit(prefs_deinit);
check_init();
atexit(check_deinit);
read_opts_postinit();
gui_run();
return EXIT_SUCCESS;
}
|