More locale-awareness for command line arguments

This commit is contained in:
Evan Miller
2021-01-18 18:15:30 -05:00
parent 1e35bddc62
commit d089c4072d
4 changed files with 32 additions and 14 deletions

View File

@@ -54,6 +54,8 @@ main(int argc, char **argv)
char *value; char *value;
size_t length; size_t length;
int ret; int ret;
char *locale = NULL;
char *table_name = NULL;
GOptionEntry entries[] = { GOptionEntry entries[] = {
{"no-header", 'H', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &header_row, "Suppress header row.", NULL}, {"no-header", 'H', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &header_row, "Suppress header row.", NULL},
@@ -78,6 +80,7 @@ main(int argc, char **argv)
opt_context = g_option_context_new("<file> <table> - export data from MDB file"); opt_context = g_option_context_new("<file> <table> - export data from MDB file");
g_option_context_add_main_entries(opt_context, entries, NULL /*i18n*/); g_option_context_add_main_entries(opt_context, entries, NULL /*i18n*/);
locale = setlocale(LC_CTYPE, "");
// g_option_context_set_strict_posix(opt_context, TRUE); /* options first, requires glib 2.44 */ // g_option_context_set_strict_posix(opt_context, TRUE); /* options first, requires glib 2.44 */
if (!g_option_context_parse (opt_context, &argc, &argv, &error)) if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{ {
@@ -91,6 +94,12 @@ main(int argc, char **argv)
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr); fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit(1); exit(1);
} }
table_name = g_locale_to_utf8(argv[2], -1, NULL, NULL, &error);
if (!table_name) {
fprintf(stderr, "argument parsing failed: %s\n", error->message);
exit(1);
}
setlocale(LC_CTYPE, locale);
/* Process options */ /* Process options */
if (quote_char) if (quote_char)
@@ -166,9 +175,9 @@ main(int argc, char **argv)
exit(1); exit(1);
} }
table = mdb_read_table_by_name(mdb, argv[2], MDB_TABLE); table = mdb_read_table_by_name(mdb, table_name, MDB_TABLE);
if (!table) { if (!table) {
fprintf(stderr, "Error: Table %s does not exist in this database.\n", argv[2]); fprintf(stderr, "Error: Table %s does not exist in this database.\n", table_name);
/* Don't bother clean up memory before exit */ /* Don't bother clean up memory before exit */
exit(1); exit(1);
} }
@@ -206,7 +215,7 @@ main(int argc, char **argv)
if (counter % batch_size == 0) { if (counter % batch_size == 0) {
counter = 0; // reset to 0, prevent overflow on extremely large data sets. counter = 0; // reset to 0, prevent overflow on extremely large data sets.
char *quoted_name; char *quoted_name;
quoted_name = mdb->default_backend->quote_schema_name(namespace, argv[2]); quoted_name = mdb->default_backend->quote_schema_name(namespace, table_name);
fprintf(outfile, "INSERT INTO %s (", quoted_name); fprintf(outfile, "INSERT INTO %s (", quoted_name);
free(quoted_name); free(quoted_name);
for (i = 0; i < table->num_cols; i++) { for (i = 0; i < table->num_cols; i++) {
@@ -260,7 +269,7 @@ main(int argc, char **argv)
if (insert_dialect) { if (insert_dialect) {
char *quoted_name; char *quoted_name;
quoted_name = mdb->default_backend->quote_schema_name(namespace, argv[2]); quoted_name = mdb->default_backend->quote_schema_name(namespace, table_name);
fprintf(outfile, "INSERT INTO %s (", quoted_name); fprintf(outfile, "INSERT INTO %s (", quoted_name);
free(quoted_name); free(quoted_name);
for (i = 0; i < table->num_cols; i++) { for (i = 0; i < table->num_cols; i++) {
@@ -338,6 +347,7 @@ main(int argc, char **argv)
g_free(escape_char); g_free(escape_char);
g_free(namespace); g_free(namespace);
g_free(str_bin_mode); g_free(str_bin_mode);
g_free(table_name);
return 0; return 0;
} }

View File

@@ -38,10 +38,6 @@ main(int argc, char **argv)
argv[0]); argv[0]);
return 1; return 1;
} }
if (argc < 4)
propColName = "LvProp";
else
propColName = argv[3];
mdb = mdb_open(argv[1], MDB_NOFLAGS); mdb = mdb_open(argv[1], MDB_NOFLAGS);
if (!mdb) { if (!mdb) {
@@ -50,14 +46,19 @@ main(int argc, char **argv)
locale = setlocale(LC_CTYPE, ""); locale = setlocale(LC_CTYPE, "");
table_name = g_locale_to_utf8(argv[2], -1, NULL, NULL, NULL); table_name = g_locale_to_utf8(argv[2], -1, NULL, NULL, NULL);
if (argc < 4)
propColName = g_strdup("LvProp");
else
propColName = g_locale_to_utf8(argv[3], -1, NULL, NULL, NULL);
setlocale(LC_CTYPE, locale); setlocale(LC_CTYPE, locale);
if (!table_name) { if (!table_name || !propColName) {
mdb_close(mdb); mdb_close(mdb);
return 1; return 1;
} }
table = mdb_read_table_by_name(mdb, "MSysObjects", MDB_ANY); table = mdb_read_table_by_name(mdb, "MSysObjects", MDB_ANY);
if (!table) { if (!table) {
g_free(table_name); g_free(table_name);
g_free(propColName);
mdb_close(mdb); mdb_close(mdb);
return 1; return 1;
} }
@@ -73,8 +74,9 @@ main(int argc, char **argv)
g_free(buf); g_free(buf);
mdb_free_tabledef(table); mdb_free_tabledef(table);
g_free(table_name); g_free(table_name);
g_free(propColName);
mdb_close(mdb); mdb_close(mdb);
printf("Column %s not found in MSysObjects!\n", argv[3]); printf("Column %s not found in MSysObjects!\n", propColName);
return 1; return 1;
} }
@@ -103,11 +105,12 @@ main(int argc, char **argv)
g_free(table_name); g_free(table_name);
if (!found) { if (!found) {
printf("Object %s not found in database file!\n", argv[2]); printf("Object %s not found in database file!\n", propColName);
return 1;
} }
return 0; g_free(propColName);
return !found;
} }
void dump_kkd(MdbHandle *mdb, void *kkd, size_t len) void dump_kkd(MdbHandle *mdb, void *kkd, size_t len)
{ {

View File

@@ -333,7 +333,7 @@ main(int argc, char **argv)
#endif #endif
char *delimiter = NULL; char *delimiter = NULL;
int in_from_colon_r = 0; int in_from_colon_r = 0;
char *locale = NULL;
GOptionEntry entries[] = { GOptionEntry entries[] = {
{ "delim", 'd', 0, G_OPTION_ARG_STRING, &delimiter, "Use this delimiter.", "char"}, { "delim", 'd', 0, G_OPTION_ARG_STRING, &delimiter, "Use this delimiter.", "char"},
@@ -350,12 +350,14 @@ main(int argc, char **argv)
opt_context = g_option_context_new("<file> - Run SQL"); opt_context = g_option_context_new("<file> - Run SQL");
g_option_context_add_main_entries(opt_context, entries, NULL /*i18n*/); g_option_context_add_main_entries(opt_context, entries, NULL /*i18n*/);
// g_option_context_set_strict_posix(opt_context, TRUE); /* options first, requires glib 2.44 */ // g_option_context_set_strict_posix(opt_context, TRUE); /* options first, requires glib 2.44 */
locale = setlocale(LC_CTYPE, "");
if (!g_option_context_parse (opt_context, &argc, &argv, &error)) if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{ {
fprintf(stderr, "option parsing failed: %s\n", error->message); fprintf(stderr, "option parsing failed: %s\n", error->message);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr); fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit (1); exit (1);
} }
setlocale(LC_CTYPE, locale);
if (argc > 2) { if (argc > 2) {
fputs("Wrong number of arguments.\n\n", stderr); fputs("Wrong number of arguments.\n\n", stderr);

View File

@@ -84,6 +84,7 @@ main (int argc, char **argv)
int show_type=0; int show_type=0;
int objtype = MDB_TABLE; int objtype = MDB_TABLE;
char *str_objtype = NULL; char *str_objtype = NULL;
char *locale = NULL;
GOptionEntry entries[] = { GOptionEntry entries[] = {
{ "system", 'S', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &skip_sys, "Include system tables", NULL}, { "system", 'S', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &skip_sys, "Include system tables", NULL},
@@ -99,6 +100,7 @@ main (int argc, char **argv)
opt_context = g_option_context_new("<file> - show MDB files tables/entries"); opt_context = g_option_context_new("<file> - show MDB files tables/entries");
g_option_context_add_main_entries(opt_context, entries, NULL /*i18n*/); g_option_context_add_main_entries(opt_context, entries, NULL /*i18n*/);
// g_option_context_set_strict_posix(opt_context, TRUE); /* options first, requires glib 2.44 */ // g_option_context_set_strict_posix(opt_context, TRUE); /* options first, requires glib 2.44 */
locale = setlocale(LC_CTYPE, "");
if (!g_option_context_parse (opt_context, &argc, &argv, &error)) if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{ {
fprintf(stderr, "option parsing failed: %s\n", error->message); fprintf(stderr, "option parsing failed: %s\n", error->message);
@@ -106,6 +108,7 @@ main (int argc, char **argv)
fprintf(stderr, "Valid types are: %s\n",valid_types()); fprintf(stderr, "Valid types are: %s\n",valid_types());
exit (1); exit (1);
} }
setlocale(LC_CTYPE, locale);
if (argc != 2) { if (argc != 2) {
fputs("Wrong number of arguments.\n\n", stderr); fputs("Wrong number of arguments.\n\n", stderr);