Decrease POSIX required level to 1

One can now compile with CFLAGS="-std=c99 -D_POSIX_C_SOURCE=1 -pedantic"

Trade getopt.h function for glib equivalents:
This mean all utilities now have long option names.
Adjust manuals and bash_completion accordingly.

Added missing manual and bash_completion for mdb-import.
This commit is contained in:
Nirgal Vourgère
2014-12-29 13:10:01 +01:00
parent 2a70e16a8b
commit 072f7c6518
20 changed files with 480 additions and 414 deletions

View File

@@ -87,118 +87,103 @@ main(int argc, char **argv)
char *row_delimiter = NULL;
char *quote_char = NULL;
char *escape_char = NULL;
char header_row = 1;
char quote_text = 1;
int header_row = 1;
int quote_text = 1;
char *insert_dialect = NULL;
char *date_fmt = NULL;
char *namespace = NULL;
char *str_bin_mode = NULL;
int bin_mode = MDB_BINEXPORT_RAW;
int opt;
char *value;
size_t length;
while ((opt=getopt(argc, argv, "HQq:X:d:D:R:I:N:b:"))!=-1) {
switch (opt) {
case 'H':
header_row = 0;
break;
case 'Q':
quote_text = 0;
break;
case 'q':
quote_char = (char *) g_strdup(optarg);
break;
case 'd':
delimiter = escapes(optarg);
break;
case 'R':
row_delimiter = escapes(optarg);
break;
case 'I':
insert_dialect = (char*) g_strdup(optarg);
header_row = 0;
break;
case 'D':
mdb_set_date_fmt(optarg);
break;
case 'X':
escape_char = (char *) g_strdup(optarg);
break;
case 'N':
namespace = (char *) g_strdup(optarg);
break;
case 'b':
if (!strcmp(optarg, "strip"))
bin_mode = MDB_BINEXPORT_STRIP;
else if (!strcmp(optarg, "raw"))
bin_mode = MDB_BINEXPORT_RAW;
else if (!strcmp(optarg, "octal"))
bin_mode = MDB_BINEXPORT_OCTAL;
else {
fprintf(stderr, "Invalid binary mode\n");
exit(1);
}
break;
default:
break;
}
GOptionEntry entries[] = {
{ "no-header", 'H', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &header_row, "Suppress header row.", NULL},
{ "no-quote", 'Q', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &quote_text, "Don't wrap text-like fields in quotes.", NULL},
{ "delimiter", 'd', 0, G_OPTION_ARG_STRING, &delimiter, "Specify an alternative column delimiter. Default is comma.", "char"},
{ "row-delimiter", 'R', 0, G_OPTION_ARG_STRING, &row_delimiter, "Specify a row delimiter", "char"},
{ "quote", 'q', 0, G_OPTION_ARG_STRING, &quote_char, "Use <char> to wrap text-like fields. Default is double quote.", "char"},
{ "backend", 'I', 0, G_OPTION_ARG_STRING, &insert_dialect, "INSERT statements (instead of CSV)", "backend"},
{ "date_format", 'D', 0, G_OPTION_ARG_STRING, &date_fmt, "Set the date format (see strftime(3) for details)", "format"},
{ "escape", 'X', 0, G_OPTION_ARG_STRING, &escape_char, "Use <char> to escape quoted characters within a field. Default is doubling.", "format"},
{ "namespace", 'N', 0, G_OPTION_ARG_STRING, &namespace, "Prefix identifiers with namespace", "namespace"},
{ "bin", 'b', 0, G_OPTION_ARG_STRING, &str_bin_mode, "Binary export mode", "strip|raw|octal"},
{ NULL },
};
GError *error = NULL;
GOptionContext *opt_context;
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_set_strict_posix(opt_context, TRUE); /* options first, requires glib 2.44 */
if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{
fprintf(stderr, "option parsing failed: %s\n", error->message);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit (1);
}
if (!quote_char) {
quote_char = (char *) g_strdup("\"");
}
if (!delimiter) {
delimiter = (char *) g_strdup(",");
}
if (!row_delimiter) {
row_delimiter = (char *) g_strdup("\n");
}
/*
** optind is now the position of the first non-option arg,
** see getopt(3)
*/
if (argc-optind < 2) {
fprintf(stderr,"Usage: %s [options] <file> <table>\n",argv[0]);
fprintf(stderr,"where options are:\n");
fprintf(stderr," -H suppress header row\n");
fprintf(stderr," -Q don't wrap text-like fields in quotes\n");
fprintf(stderr," -d <delimiter> specify a column delimiter\n");
fprintf(stderr," -R <delimiter> specify a row delimiter\n");
fprintf(stderr," -I <backend> INSERT statements (instead of CSV)\n");
fprintf(stderr," -D <format> set the date format (see strftime(3) for details)\n");
fprintf(stderr," -q <char> Use <char> to wrap text-like fields. Default is \".\n");
fprintf(stderr," -X <char> Use <char> to escape quoted characters within a field. Default is doubling.\n");
fprintf(stderr," -N <namespace> Prefix identifiers with namespace\n");
fprintf(stderr," -b strip|raw|octal Binary export mode.\n");
g_free (delimiter);
g_free (row_delimiter);
g_free (quote_char);
if (escape_char) g_free (escape_char);
if (argc != 3) {
fputs("Wrong number of arguments.\n\n", stderr);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit(1);
}
if (!(mdb = mdb_open(argv[optind], MDB_NOFLAGS))) {
g_free (delimiter);
g_free (row_delimiter);
g_free (quote_char);
if (escape_char) g_free (escape_char);
/* Process options */
if (quote_char)
quote_char = escapes(quote_char);
else
quote_char = g_strdup("\"");
if (delimiter)
delimiter = escapes(delimiter);
else
delimiter = g_strdup(",");
if (row_delimiter)
row_delimiter = escapes(row_delimiter);
else
row_delimiter = g_strdup("\n");
if (escape_char)
escape_char = escapes(escape_char);
if (insert_dialect)
header_row = 0;
if (date_fmt)
mdb_set_date_fmt(date_fmt);
if (str_bin_mode) {
if (!strcmp(str_bin_mode, "strip"))
bin_mode = MDB_BINEXPORT_STRIP;
else if (!strcmp(str_bin_mode, "raw"))
bin_mode = MDB_BINEXPORT_RAW;
else if (!strcmp(str_bin_mode, "octal"))
bin_mode = MDB_BINEXPORT_OCTAL;
else {
fputs("Invalid binary mode\n", stderr);
exit(1);
}
}
/* Open file */
if (!(mdb = mdb_open(argv[1], MDB_NOFLAGS))) {
/* Don't bother clean up memory before exit */
exit(1);
}
if (insert_dialect)
if (!mdb_set_default_backend(mdb, insert_dialect)) {
fprintf(stderr, "Invalid backend type\n");
if (escape_char) g_free (escape_char);
fputs("Invalid backend type\n", stderr);
/* Don't bother clean up memory before exit */
exit(1);
}
table = mdb_read_table_by_name(mdb, argv[argc-1], MDB_TABLE);
table = mdb_read_table_by_name(mdb, argv[2], MDB_TABLE);
if (!table) {
fprintf(stderr, "Error: Table %s does not exist in this database.\n", argv[argc-1]);
g_free (delimiter);
g_free (row_delimiter);
g_free (quote_char);
if (escape_char) g_free (escape_char);
mdb_close(mdb);
fprintf(stderr, "Error: Table %s does not exist in this database.\n", argv[2]);
/* Don't bother clean up memory before exit */
exit(1);
}
@@ -227,7 +212,7 @@ main(int argc, char **argv)
if (insert_dialect) {
char *quoted_name;
quoted_name = mdb->default_backend->quote_schema_name(namespace, argv[optind + 1]);
quoted_name = mdb->default_backend->quote_schema_name(namespace, argv[2]);
fprintf(outfile, "INSERT INTO %s (", quoted_name);
free(quoted_name);
for (i=0;i<table->num_cols;i++) {
@@ -272,12 +257,18 @@ main(int argc, char **argv)
g_free(bound_lens);
mdb_free_tabledef(table);
g_free (delimiter);
g_free (row_delimiter);
g_free (quote_char);
if (escape_char) g_free (escape_char);
mdb_close(mdb);
g_option_context_free(opt_context);
// g_free ignores NULL
g_free(quote_char);
g_free(delimiter);
g_free(row_delimiter);
g_free(insert_dialect);
g_free(date_fmt);
g_free(escape_char);
g_free(namespace);
g_free(str_bin_mode);
return 0;
}
@@ -285,6 +276,7 @@ static char *escapes(char *s)
{
char *d = (char *) g_strdup(s);
char *t = d;
char *orig = s;
unsigned char encode = 0;
for (;*s; s++) {
@@ -303,5 +295,6 @@ static char *escapes(char *s)
}
}
*t='\0';
g_free(orig);
return d;
}

View File

@@ -155,43 +155,43 @@ main(int argc, char **argv)
char line[MAX_ROW_SIZE];
int num_fields;
/* doesn't handle tables > 256 columns. Can that happen? */
int opt;
FILE *in;
char delimiter[2] = ",";
char header_rows = 0;
char *delimiter;
int header_rows = 0;
while ((opt=getopt(argc, argv, "H:d:"))!=-1) {
switch (opt) {
case 'H':
header_rows = atol(optarg);
break;
case 'd':
delimiter[0] = optarg[0];
break;
default:
break;
}
GOptionEntry entries[] = {
{ "header", 'H', 0, G_OPTION_ARG_INT, &header_rows, "skip <rows> header rows", "row"},
{ "delimiter", 'd', 0, G_OPTION_ARG_STRING, &delimiter, "Specify a column delimiter", "char"},
{ NULL },
};
GError *error = NULL;
GOptionContext *opt_context;
opt_context = g_option_context_new("<mdbfile> <table> <csvfile> - import data into MDB file");
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 */
if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{
fprintf(stderr, "option parsing failed: %s\n", error->message);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit (1);
}
/*
** optind is now the position of the first non-option arg,
** see getopt(3)
*/
if (argc-optind < 3) {
fprintf(stderr,"Usage: %s [options] <database> <table> <csv file>\n",argv[0]);
fprintf(stderr,"where options are:\n");
fprintf(stderr," -H <rows> skip <rows> header rows\n");
fprintf(stderr," -d <delimiter> specify a column delimiter\n");
if (!delimiter)
delimiter = g_strdup(",");
if (argc != 4) {
fputs("Wrong number of arguments.\n\n", stderr);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit(1);
}
if (!(mdb = mdb_open(argv[optind], MDB_WRITABLE))) {
if (!(mdb = mdb_open(argv[1], MDB_WRITABLE))) {
exit(1);
}
table = mdb_read_table_by_name(mdb, argv[argc-2], MDB_TABLE);
table = mdb_read_table_by_name(mdb, argv[2], MDB_TABLE);
if (!table) {
fprintf(stderr,"Table %s not found in database\n", argv[argc-2]);
fprintf(stderr, "Table %s not found in database\n", argv[2]);
exit(1);
}
mdb_read_columns(table);
@@ -201,9 +201,9 @@ main(int argc, char **argv)
/*
* open the CSV file and read any header rows
*/
in = fopen(argv[argc-1], "r");
in = fopen(argv[3], "r");
if (!in) {
fprintf(stderr, "Can not open file %s\n", argv[argc-1]);
fprintf(stderr, "Can not open file %s\n", argv[3]);
exit(1);
}
for (i=0;i<header_rows;i++)
@@ -228,6 +228,9 @@ main(int argc, char **argv)
mdb_free_tabledef(table);
fclose(in);
mdb_close(mdb);
g_option_context_free(opt_context);
g_free(delimiter);
return 0;
}

View File

@@ -17,8 +17,6 @@
*/
/* this utility dumps the schema for an existing database */
#include <ctype.h>
#include <getopt.h>
#include "mdbtools.h"
#ifdef DMALLOC
@@ -31,127 +29,62 @@ main (int argc, char **argv)
MdbHandle *mdb;
char *tabname = NULL;
char *namespace = NULL;
guint32 export_options = MDB_SHEXP_DEFAULT;
int opt;
guint32 export_options;
int opt_drop_table = MDB_SHEXP_DEFAULT & MDB_SHEXP_DROPTABLE;
int opt_not_null = MDB_SHEXP_DEFAULT & MDB_SHEXP_CST_NOTNULL;
int opt_def_values = MDB_SHEXP_DEFAULT & MDB_SHEXP_DEFVALUES;
int opt_not_empty = MDB_SHEXP_DEFAULT & MDB_SHEXP_CST_NOTEMPTY;
int opt_comments = MDB_SHEXP_DEFAULT & MDB_SHEXP_COMMENTS;
int opt_indexes = MDB_SHEXP_DEFAULT & MDB_SHEXP_INDEXES;
int opt_relations = MDB_SHEXP_DEFAULT & MDB_SHEXP_RELATIONS;
if (argc < 2) {
fprintf (stderr, "Usage: %s [options] <file> [<backend>]\n",argv[0]);
fprintf (stderr, "where options are:\n");
fprintf (stderr, " -T <table> Only create schema for named table\n");
fprintf (stderr, " -N <namespace> Prefix identifiers with namespace\n");
GOptionEntry entries[] = {
{ "table", 'T', 0, G_OPTION_ARG_STRING, &tabname, "Only create schema for named table", "table"},
{ "namespace", 'N', 0, G_OPTION_ARG_STRING, &namespace, "Prefix identifiers with namespace", "namespace"},
{ "drop-table", 0, 0, G_OPTION_ARG_NONE, &opt_drop_table, "Include DROP TABLE statements", NULL},
{ "no-drop-table", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_drop_table, "Don't include DROP TABLE statements", NULL},
{ "not-null", 0, 0, G_OPTION_ARG_NONE, &opt_not_null, "Include NOT NULL constraints", NULL},
{ "no-not-null", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_not_null, "Don't include NOT NULL constraints", NULL},
{ "default-values", 0, 0, G_OPTION_ARG_NONE, &opt_def_values, "Include default values", NULL},
{ "no-default-values", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_def_values, "Don't include default values", NULL},
{ "not-empty", 0, 0, G_OPTION_ARG_NONE, &opt_not_empty, "Include not empty constraints", NULL},
{ "no-not_empty", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_not_empty, "Don't include not empty constraints", NULL},
{ "comments", 0, 0, G_OPTION_ARG_NONE, &opt_comments, "Include comments", NULL},
{ "no-comments", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_comments, "Don't include comments", NULL},
{ "indexes", 0, 0, G_OPTION_ARG_NONE, &opt_indexes, "Include indexes", NULL},
{ "no-indexes", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_indexes, "Don't include indexes", NULL},
{ "relations", 0, 0, G_OPTION_ARG_NONE, &opt_relations, "Include foreign key constraints", NULL},
{ "no-relations", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_relations, "Don't include foreign key constraints", NULL},
{ NULL },
};
GError *error = NULL;
GOptionContext *opt_context;
opt_context = g_option_context_new("<file> [<backend>] - Dump schema");
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 */
if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{
fprintf(stderr, "option parsing failed: %s\n", error->message);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit (1);
}
while (1) {
//int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"table", 1, NULL, 'T'},
{"namespace", 1, NULL, 'N'},
{"drop-table", 0, NULL, 0},
{"no-drop-table", 0, NULL, 0},
{"default-values", 0, NULL, 0},
{"no-default-values", 0, NULL, 0},
{"not-null", 0, NULL, 0},
{"no-not-null", 0, NULL, 0},
{"not-empty", 0, NULL, 0},
{"no-not-empty", 0, NULL, 0},
{"description", 0, NULL, 0},
{"no-description", 0, NULL, 0},
{"indexes", 0, NULL, 0},
{"no-indexes", 0, NULL, 0},
{"relations", 0, NULL, 0},
{"no-relations", 0, NULL, 0},
{NULL, 0, NULL, 0},
};
opt = getopt_long(argc, argv, "T:N:", long_options, &option_index);
if (opt == -1)
break;
switch (opt) {
case 0:
if (!strcmp(long_options[option_index].name, "drop-table")) {
export_options |= MDB_SHEXP_DROPTABLE;
break;
}
if (!strcmp(long_options[option_index].name, "no-drop-table")) {
export_options &= ~MDB_SHEXP_DROPTABLE;
break;
}
if (!strcmp(long_options[option_index].name, "not-null")) {
export_options |= MDB_SHEXP_CST_NOTNULL;
break;
}
if (!strcmp(long_options[option_index].name, "no-not-null")) {
export_options &= ~MDB_SHEXP_CST_NOTNULL;
break;
}
if (!strcmp(long_options[option_index].name, "default-values")) {
export_options |= MDB_SHEXP_DEFVALUES;
break;
}
if (!strcmp(long_options[option_index].name, "no-default-values")) {
export_options &= ~MDB_SHEXP_DEFVALUES;
break;
}
if (!strcmp(long_options[option_index].name, "not-empty")) {
export_options |= MDB_SHEXP_CST_NOTEMPTY;
break;
}
if (!strcmp(long_options[option_index].name, "no-not-empty")) {
export_options &= ~MDB_SHEXP_CST_NOTEMPTY;
break;
}
if (!strcmp(long_options[option_index].name, "description")) {
export_options |= MDB_SHEXP_COMMENTS;
break;
}
if (!strcmp(long_options[option_index].name, "no-description")) {
export_options &= ~MDB_SHEXP_COMMENTS;
break;
}
if (!strcmp(long_options[option_index].name, "indexes")) {
export_options |= MDB_SHEXP_INDEXES;
break;
}
if (!strcmp(long_options[option_index].name, "no-indexes")) {
export_options &= ~MDB_SHEXP_INDEXES;
break;
}
if (!strcmp(long_options[option_index].name, "relations")) {
export_options |= MDB_SHEXP_RELATIONS;
break;
}
if (!strcmp(long_options[option_index].name, "no-relations")) {
export_options &= ~MDB_SHEXP_RELATIONS;
break;
}
fprintf(stderr, "unimplemented option %s", long_options[option_index].name);
if (optarg)
fprintf(stderr, " with arg %s", optarg);
fputc('\n', stderr);
exit(1);
break;
case 'T':
tabname = (char *) g_strdup(optarg);
break;
case 'N':
namespace = (char *) g_strdup(optarg);
break;
}
if (argc < 2 || argc > 3) {
fputs("Wrong number of arguments.\n\n", stderr);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit(1);
}
/* open the database */
mdb = mdb_open (argv[optind], MDB_NOFLAGS);
mdb = mdb_open (argv[1], MDB_NOFLAGS);
if (!mdb) {
fprintf(stderr, "Could not open file\n");
exit(1);
}
if (argc - optind >= 2) {
if (!mdb_set_default_backend(mdb, argv[optind + 1])) {
if (argc == 3) {
if (!mdb_set_default_backend(mdb, argv[2])) {
fprintf(stderr, "Invalid backend type\n");
exit(1);
}
@@ -159,16 +92,32 @@ main (int argc, char **argv)
/* read the catalog */
if (!mdb_read_catalog (mdb, MDB_TABLE)) {
fprintf(stderr, "File does not appear to be an Access database\n");
fputs("File does not appear to be an Access database\n", stderr);
exit(1);
}
export_options = 0;
if (opt_drop_table)
export_options |= MDB_SHEXP_DROPTABLE;
if (opt_not_null)
export_options |= MDB_SHEXP_CST_NOTNULL;
if (opt_def_values)
export_options |= MDB_SHEXP_DEFVALUES;
if (opt_not_empty)
export_options |= MDB_SHEXP_CST_NOTEMPTY;
if (opt_comments)
export_options |= MDB_SHEXP_COMMENTS;
if (opt_indexes)
export_options |= MDB_SHEXP_INDEXES;
if (opt_relations)
export_options |= MDB_SHEXP_RELATIONS;
mdb_print_schema(mdb, stdout, tabname, namespace, export_options);
g_free(namespace);
g_free(tabname);
mdb_close (mdb);
g_option_context_free(opt_context);
g_free(namespace);
g_free(tabname);
return 0;
}

View File

@@ -350,19 +350,47 @@ dump_results_pp(FILE *out, MdbSQL *sql)
int
main(int argc, char **argv)
{
char *s = NULL;
char prompt[20];
int line = 0;
char *mybuf;
unsigned int bufsz;
MdbSQL *sql;
int opt;
FILE *in = NULL, *out = NULL;
char *home = getenv("HOME");
char *histpath;
char *delimiter = NULL;
char *s = NULL;
char prompt[20];
int line = 0;
char *mybuf;
unsigned int bufsz;
MdbSQL *sql;
FILE *in = NULL, *out = NULL;
char *filename_in=NULL, *filename_out=NULL;
char *home = getenv("HOME");
char *histpath;
char *delimiter = NULL;
GOptionEntry entries[] = {
{ "delim", 'd', 0, G_OPTION_ARG_STRING, &delimiter, "Use this delimiter.", "char"},
{ "no-pretty-print", 'P', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &pretty_print, "Don't pretty print", NULL},
{ "no-header", 'H', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &headers, "Don't print header", NULL},
{ "no-footer", 'F', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &footers, "Don't print footer", NULL},
{ "input", 'i', 0, G_OPTION_ARG_STRING, &filename_in, "Read SQL from specified file", "file"},
{ "output", 'o', 0, G_OPTION_ARG_STRING, &filename_out, "Write result to specified file", "file"},
{ NULL },
};
GError *error = NULL;
GOptionContext *opt_context;
opt_context = g_option_context_new("<file> - Run SQL");
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 */
if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{
fprintf(stderr, "option parsing failed: %s\n", error->message);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit (1);
}
if (argc > 2) {
fputs("Wrong number of arguments.\n\n", stderr);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit(1);
}
#ifdef HAVE_READLINE_HISTORY
if (home) {
histpath = (char *) g_strconcat(home, "/", HISTFILE, NULL);
@@ -374,44 +402,26 @@ char *delimiter = NULL;
if (!isatty(fileno(stdin))) {
in = stdin;
}
while ((opt=getopt(argc, argv, "HFpd:i:o:"))!=-1) {
switch (opt) {
case 'd':
delimiter = (char *) g_strdup(optarg);
break;
case 'p':
pretty_print=0;
break;
case 'H':
headers=0;
break;
case 'F':
footers=0;
break;
case 'i':
if (!strcmp(optarg, "stdin"))
in = stdin;
else if (!(in = fopen(optarg, "r"))) {
fprintf(stderr,"Unable to open file %s\n", optarg);
exit(1);
}
break;
case 'o':
if (!(out = fopen(optarg, "w"))) {
fprintf(stderr,"Unable to open file %s\n", optarg);
exit(1);
}
break;
default:
fprintf(stdout,"Unknown option.\nUsage: %s [-HFp] [-d <delimiter>] [-i <file>] [-o <file>] [<database>]\n", argv[0]);
exit(1);
if (filename_in) {
if (!strcmp(filename_in, "stdin"))
in = stdin;
else if (!(in = fopen(filename_in, "r"))) {
fprintf(stderr, "Unable to open file %s\n", filename_in);
exit(1);
}
}
if (filename_out) {
if (!(out = fopen(filename_out, "w"))) {
fprintf(stderr,"Unable to open file %s\n", filename_out);
exit(1);
}
}
/* initialize the SQL engine */
sql = mdb_sql_init();
if (argc>optind) {
mdb_sql_open(sql, argv[optind]);
if (argc == 2) {
mdb_sql_open(sql, argv[1]);
}
/* give the buffer an initial size */
@@ -470,10 +480,9 @@ char *delimiter = NULL;
strcat(mybuf,"\n");
}
}
mdb_sql_exit(sql);
mdb_sql_exit(sql);
g_free(mybuf);
g_free(delimiter);
if (s) free(s);
if (out) fclose(out);
if ((in) && (in != stdin)) fclose(in);
@@ -487,6 +496,11 @@ char *delimiter = NULL;
}
#endif
g_option_context_free(opt_context);
g_free(delimiter);
g_free(filename_in);
g_free(filename_out);
return 0;
}
#else

View File

@@ -86,44 +86,50 @@ main (int argc, char **argv)
int line_break=0;
int skip_sys=1;
int show_type=0;
int opt;
int objtype = MDB_TABLE;
char *str_objtype = NULL;
GOptionEntry entries[] = {
{ "system", 'S', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &skip_sys, "Include system tables", NULL},
{ "single-column", '1', 0, G_OPTION_ARG_NONE, &line_break, "One table name per line", NULL},
{ "delimiter", 'd', 0, G_OPTION_ARG_STRING, &delimiter, "Table name delimiter", "char"},
{ "type", 't', 0, G_OPTION_ARG_STRING, &str_objtype, "Type of entry", "type"},
{ "showtype", 'T', 0, G_OPTION_ARG_NONE, &show_type, "Show type", NULL},
{ NULL },
};
GError *error = NULL;
GOptionContext *opt_context;
if (argc < 2) {
fprintf (stderr, "Usage: %s [-S] [-1 | -d<delimiter>] [-t <type>] [-T] <file>\n",argv[0]);
fprintf (stderr, " Valid types are: %s\n",valid_types());
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_set_strict_posix(opt_context, TRUE); /* options first, requires glib 2.44 */
if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{
fprintf(stderr, "option parsing failed: %s\n", error->message);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
fprintf(stderr, "Valid types are: %s\n",valid_types());
exit (1);
}
while ((opt=getopt(argc, argv, "S1Td:t:"))!=-1) {
switch (opt) {
case 'S':
skip_sys = 0;
break;
case 'T':
show_type = 1;
break;
case '1':
line_break = 1;
break;
case 't':
if (!get_obj_type(optarg, &objtype)) {
fprintf(stderr,"Invalid type name.\n");
fprintf (stderr, "Valid types are: %s\n",valid_types());
exit(1);
}
break;
case 'd':
delimiter = (char *) g_strdup(optarg);
break;
}
if (argc != 2) {
fputs("Wrong number of arguments.\n\n", stderr);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
fprintf(stderr, "Valid types are: %s\n",valid_types());
exit(1);
}
if (str_objtype) {
if (!get_obj_type(str_objtype, &objtype)) {
fprintf(stderr,"Invalid type name.\n");
fprintf (stderr, "Valid types are: %s\n",valid_types());
exit(1);
}
}
if (!delimiter)
delimiter = g_strdup(" ");
/* open the database */
if (!(mdb = mdb_open (argv[optind], MDB_NOFLAGS))) {
if (!(mdb = mdb_open (argv[1], MDB_NOFLAGS))) {
fprintf(stderr,"Couldn't open database.\n");
exit(1);
}
@@ -160,7 +166,9 @@ main (int argc, char **argv)
fprintf (stdout, "\n");
mdb_close(mdb);
g_option_context_free(opt_context);
g_free(delimiter);
g_free(str_objtype);
return 0;
}

View File

@@ -30,37 +30,42 @@ main(int argc, char **argv)
{
MdbHandle *mdb;
int print_mdbver = 0;
int opt;
/* setlocale (LC_ALL, ""); */
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
while ((opt=getopt(argc, argv, "M"))!=-1) {
switch (opt) {
case 'M':
print_mdbver = 1;
break;
default:
break;
}
GOptionEntry entries[] = {
{ "mdbtools", 'M', 0, G_OPTION_ARG_NONE, &print_mdbver, "Show MDBtools version", NULL},
{ NULL },
};
GError *error = NULL;
GOptionContext *opt_context;
opt_context = g_option_context_new("<file> - display MDB file version");
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 */
if (!g_option_context_parse (opt_context, &argc, &argv, &error))
{
fprintf(stderr, "option parsing failed: %s\n", error->message);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit (1);
}
if (print_mdbver) {
fprintf(stdout,"%s\n", MDB_FULL_VERSION);
if (argc-optind < 1) exit(0);
if (argc == 1)
exit(0);
}
/*
** optind is now the position of the first non-option arg,
** see getopt(3)
*/
if (argc-optind < 1) {
fprintf(stderr,_("Usage: %s [-M] <file>\n"),argv[0]);
if (argc != 2) {
fputs("Wrong number of arguments.\n\n", stderr);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit(1);
}
if (!(mdb = mdb_open(argv[optind], MDB_NOFLAGS))) {
fprintf(stderr,_("Error: unable to open file %s\n"),argv[optind]);
if (!(mdb = mdb_open(argv[1], MDB_NOFLAGS))) {
fprintf(stderr,_("Error: unable to open file %s\n"), argv[1]);
exit(1);
}
switch(mdb->f->jet_version) {
@@ -82,6 +87,7 @@ main(int argc, char **argv)
}
mdb_close(mdb);
g_option_context_free(opt_context);
return 0;
}

View File

@@ -7,12 +7,16 @@ _mdb_export()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -@(d|R|q|X|D|N) ]] ; then
if [[ "$prev" == -@(d|-delimiter|R|-row-delimiter|q|-quote|X|-escape|D|-date-format|N|-namespace|h|-help) ]] ; then
return 0
elif [[ "$prev" == -I ]] ; then
COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql' -- $cur ) )
elif [[ "$prev" == -@(b|-bin) ]] ; then
COMPREPLY=( $( compgen -W 'strip raw octal' -- $cur ) )
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-T -H -d -R -Q -q -X -I -D -N' -- $cur ) )
COMPREPLY=( $( compgen -W '-H -d -R -Q -q -X -I -D -N -b -h \
--no-header --no-quote --delimiter --row-delimiter --insert \
--date-format --quote --escape --namespace --bin --help' -- $cur ) )
elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then
local dbname
local tablenames
@@ -88,16 +92,18 @@ _mdb_schema()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -@(T|table|N) ]] ; then
if [[ "$prev" == -@(T|-table|N|-namespace) ]] ; then
return 0
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-T --table -N \
COMPREPLY=( $( compgen -W '-T --table \
-N --namespace \
--drop-table --no-drop-table \
--not-null --no-not-null \
--default-values --no-default-values \
--not-empty --no-not-empty \
--indexes --no-indexes \
--relations --no-relations' -- $cur ) )
--relations --no-relations
-h --help' -- $cur ) )
elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then
COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql' -- $cur ) )
else
@@ -118,10 +124,16 @@ _mdb_sql()
if [[ "$prev" == -d ]] ; then
return 0
elif [[ "$prev" == -@(i|o) ]] ; then
elif [[ "$prev" == -@(i|-input|o|-output) ]] ; then
_filedir
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-H -f -p -d -i -o' -- $cur ) )
COMPREPLY=( $( compgen -W '-H --no-header \
-F --no-footer \
-p --no-pretty-print \
-d --delimiter \
-i --input \
-o --output \
-h --help' -- $cur ) )
else
_filedir '@(mdb|mdw|accdb)'
fi
@@ -138,13 +150,18 @@ _mdb_tables()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -d ]]; then
if [[ "$prev" == -@(d|-delimiter) ]]; then
return 0
elif [[ "$prev" == -t ]]; then
elif [[ "$prev" == -@(t|-type) ]]; then
COMPREPLY=( $( compgen -W 'form table macro systable report query linkedtable module relationship dbprop any all' -- $cur ) )
return 0
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-S -1 -d -t' -- $cur ) )
COMPREPLY=( $( compgen -W '-S --system\
-1 --single-column \
-d --delimiter \
-t --type \
-T --showtype \
-h --help' -- $cur ) )
else
_filedir '@(mdb|mdw|accdb)'
fi
@@ -161,10 +178,39 @@ _mdb_ver()
cur=${COMP_WORDS[COMP_CWORD]}
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-M' -- $cur ) )
COMPREPLY=( $( compgen -W '-M -h --help' -- $cur ) )
else
_filedir '@(mdb|mdw|accdb)'
fi
return 0
} &&
complete -F _mdb_ver mdb-ver
have mdb-import &&
_mdb_import()
{
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -@(d|-delimiter) ]]; then
return 0
elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-H --header \
-d --delimiter \
-h --help' -- $cur ) )
elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then
local dbname
local tablenames
dbname=$prev
__expand_tilde_by_ref dbname
tablenames=$(eval mdb-tables -S -d / "${dbname}" 2>/dev/null)
COMPREPLY=( $( IFS=/ compgen -W "${tablenames}" -- $cur ) )
else
_filedir '@(mdb|mdw|accdb|txt|csv)'
fi
return 0
} &&
complete -F _mdb_import mdb-import