Skip to content

Commit

Permalink
confir_from_stdin_add
Browse files Browse the repository at this point in the history
  • Loading branch information
hyphop committed May 23, 2018
1 parent a199980 commit 6d8f074
Showing 1 changed file with 71 additions and 16 deletions.
87 changes: 71 additions & 16 deletions lib/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static gboolean opt_debug_callback(const gchar *option_name, const gchar *value,

static GOptionEntry basic_options[] =
{
{ "config", '\0', 0, G_OPTION_ARG_FILENAME, &opt_config, "Load configuration from a file", "PATH" },
{ "config", 'c', 0, G_OPTION_ARG_FILENAME, &opt_config, "Load configuration from a file", "PATH" },
{ "ignore-config-file", '\0', 0, G_OPTION_ARG_NONE, &opt_no_config, "Disable loading " MEGA_RC_FILENAME, NULL },
{ "debug", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, opt_debug_callback, "Enable debugging output", "OPTS" },
{ "version", '\0', 0, G_OPTION_ARG_NONE, &opt_version, "Show version information", NULL },
Expand Down Expand Up @@ -411,36 +411,91 @@ void tool_init(gint* ac, gchar*** av, const gchar* tool_name, GOptionEntry* tool

print_version();

if (!opt_no_config || opt_config)
{
gboolean status = TRUE;
if (!opt_no_config || opt_config) {
// gboolean status = TRUE;
gboolean status = FALSE;
gc_key_file_unref GKeyFile* kf = g_key_file_new();

if (opt_config)
{
if (!g_key_file_load_from_file(kf, opt_config, 0, &local_err))
{
g_printerr("ERROR: Failed to open config file: %s: %s\n", opt_config, local_err->message);
g_clear_error(&local_err);
exit(1);
}
}
else

//////// UGLY )))

if ( opt_config ) {
if ( opt_config[0] == '-' ) {

fprintf(stderr, "[i] try config from %s\n", "STDIN");

#define BUF_SIZE 4096
char config_str[BUF_SIZE];
config_str[0] = '\0';
// char* config_str = malloc(BUF_SIZE);

int len;
while(len =read( 0 , config_str, BUF_SIZE) > 0)
{
status = g_key_file_load_from_file(kf, MEGA_RC_FILENAME, 0, NULL);
//fprintf(stderr, "> %d %d %s\n", len, strlen(config_str), config_str);
}

// if (
status = g_key_file_load_from_data (kf, config_str, strlen(config_str)-1 ,
// 0, &local_err))
0, NULL)
;

fprintf(stderr, "[i] config status %d\n", status);

// )
// {
// g_printerr("ERROR: Failed to open config file: %s: %s\n", opt_config, local_err->message);
// g_clear_error(&local_err);
// exit(1);
// }
} else {

fprintf(stderr, "[i] try config form %s\n", opt_config);


//if (
status = g_key_file_load_from_file(kf, opt_config, 0, NULL);
//)
// {
// g_printerr("ERROR: Failed to open config file: %s: %s\n", opt_config, local_err->message);
// g_clear_error(&local_err);
// exit(1);
// }
fprintf(stderr, "[i] config status %d\n", status);

}


}

if (!status) {

fprintf(stderr, "[i] try config form %s\n", MEGA_RC_FILENAME);
status = g_key_file_load_from_file(kf,MEGA_RC_FILENAME , 0, NULL);
fprintf(stderr, "[i] config status %d\n", status);

if (!status)
{
gc_free gchar* tmp = g_build_filename(g_get_home_dir(), MEGA_RC_FILENAME, NULL);
fprintf(stderr, "[i] try config form %s\n", tmp);
status = g_key_file_load_from_file(kf, tmp, 0, NULL);

fprintf(stderr, "[i] config status %d\n", status);

}
}

}


if (status)
{
// load username/password from ini file
if (!opt_username)
opt_username = g_key_file_get_string(kf, "Login", "Username", NULL);

// fprintf(stderr, "[i] %s\n", opt_username);

if (!opt_password)
opt_password = g_key_file_get_string(kf, "Login", "Password", NULL);

Expand Down

0 comments on commit 6d8f074

Please sign in to comment.