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

@@ -6,15 +6,16 @@ PRODUCT = MDBTools
dist_man_MANS = dist_man_MANS =
if ENABLE_MAN if ENABLE_MAN
dist_man_MANS += mdb-tables.1 mdb-ver.1 mdb-export.1 mdb-schema.1 mdb-sql.1 \ dist_man_MANS += mdb-tables.1 mdb-ver.1 mdb-export.1 mdb-schema.1 mdb-sql.1 \
mdb-array.1 mdb-header.1 mdb-hexdump.1 mdb-parsecsv.1 mdb-prop.1 gmdb2.1 mdb-array.1 mdb-header.1 mdb-hexdump.1 mdb-parsecsv.1 mdb-prop.1 mdb-import.1 \
gmdb2.1
endif endif
if ENABLE_DOCBOOK if ENABLE_DOCBOOK
dist_man_MANS += install.tgz dist_man_MANS += install.tgz
endif endif
CLEANFILES = ${dist_man_MANS} install install.tgz CLEANFILES = ${dist_man_MANS} install install.tgz
EXTRA_DIST = mdb-tables.txt mdb-ver.txt mdb-export.txt mdb-schema.txt mdb-sql.txt \ EXTRA_DIST = mdb-tables.txt mdb-ver.txt mdb-export.txt mdb-schema.txt mdb-sql.txt \
mdb-array.txt mdb-header.txt mdb-hexdump.txt mdb-parsecsv.txt mdb-prop.txt gmdb2.txt \ mdb-array.txt mdb-header.txt mdb-hexdump.txt mdb-parsecsv.txt mdb-prop.txt mdb-import.txt \
faq.html install.sgml gmdb2.txt faq.html install.sgml
.txt.1: .txt.1:
$(TXT2MAN) -t $* -r "$(PRODUCT) $(VERSION)" -s 1 -v "Executable programs or shell commands" $(srcdir)/$< > $@ $(TXT2MAN) -t $* -r "$(PRODUCT) $(VERSION)" -s 1 -v "Executable programs or shell commands" $(srcdir)/$< > $@

View File

@@ -23,7 +23,7 @@ ENVIRONMENT
SEE ALSO SEE ALSO
mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) mdb-array(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) mdb-array(1)
mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-import(1)
AUTHORS AUTHORS

View File

@@ -26,7 +26,7 @@ ENVIRONMENT
SEE ALSO SEE ALSO
gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1)
mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-import(1)
AUTHORS AUTHORS
The mdb-array utility was written by Brian Bruns. The mdb-array utility was written by Brian Bruns.

View File

@@ -2,7 +2,8 @@ NAME
mdb-export - Export data in an MDB database table to CSV format. mdb-export - Export data in an MDB database table to CSV format.
SYNOPSIS SYNOPSIS
mdb-export [-H] [-d delimiter] [-R row_delim] [[-Q] | [-q quote [-X escape]]] [-I backend] [-D format] [-N namespace] [-b strip|raw|octal] database table mdb-export [-H] [-d delim] [-R delim] [[-Q] | [-q char [-X char]]] [-I backend] [-D fmt] [-N prefix] [-b strip|raw|octal] database table
mdb-export -h|--help
DESCRIPTION DESCRIPTION
mdb-export is a utility program distributed with MDB Tools. mdb-export is a utility program distributed with MDB Tools.
@@ -10,17 +11,16 @@ DESCRIPTION
It produces a CSV (comma separated value) output for the given table. Such output is suitable for importation into databases or spreadsheets. It produces a CSV (comma separated value) output for the given table. Such output is suitable for importation into databases or spreadsheets.
OPTIONS OPTIONS
-H Suppress header row -H, --no-header Suppress header row.
-Q Don't wrap text-like fields (text, memo, date) in quotes. If not specified text fiels will be surrounded by " (double quote) characters. -Q, --no-quote Don't wrap text-like fields (text, memo, date) in quotes. If not specified text fiels will be surrounded by " (double quote) characters.
-d Specify an alternative column delimiter If no delimiter is specified, table names will be delimited by a , (comma) character. -d, --delimiter delim Specify an alternative column delimiter. Default is , (comma).
-R Specify a row delimiter -R, --row-delimiter delim Specify a row delimiter. Default is \n (ASCII value 10).
-I backend INSERT statements (instead of CSV). You must specify which SQL backend dialect to use. Allowed values are: access, sybase, oracle, postgres, mysql and sqlite. -I, --insert backend INSERT statements (instead of CSV). You must specify which SQL backend dialect to use. Allowed values are: access, sybase, oracle, postgres, mysql and sqlite.
-D Set the date format (see strftime(3) for details) -D, --date-format fmt Set the date format (see strftime(3) for details).
-q Use to wrap text-like fields. Default is ". -q, --quote char Use to wrap text-like fields. Default is " (double quote).
-X Use to escape quoted characters within a field. Default is doubling. -X, --escape char Use to escape quoted characters within a field. Default is doubling.
-N namespace Prefix identifiers with namespace. -N, --namespace prefix Prefix identifiers with prefix.
-b strip|raw|octal Binary export mode: strip binaries, export as-is, or output \ooo style octal data. -b, --bin strip|raw|octal Binary export mode: strip binaries, export as-is, or output \ooo style octal data.
NOTES NOTES
@@ -40,7 +40,7 @@ ENVIRONMENT
SEE ALSO SEE ALSO
gmdb2(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) mdb-array(1) gmdb2(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) mdb-array(1)
mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-import(1)
HISTORY HISTORY
mdb-export first appeared in MDB Tools 0.1. mdb-export first appeared in MDB Tools 0.1.

View File

@@ -31,7 +31,7 @@ EXIT STATUS
SEE ALSO SEE ALSO
gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1)
mdb-array(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-array(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-import(1)
AUTHORS AUTHORS
The mdb-header utility was written by Brian Bruns. The mdb-header utility was written by Brian Bruns.

View File

@@ -25,7 +25,7 @@ ENVIRONMENT
SEE ALSO SEE ALSO
gmdb2(1) mdb-export(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) mdb-array(1) gmdb2(1) mdb-export(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) mdb-array(1)
mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-import(1)
AUTHORS AUTHORS
The mdb-hexdump utility was written by Brian Bruns. The mdb-hexdump utility was written by Brian Bruns.

44
doc/mdb-import.txt Normal file
View File

@@ -0,0 +1,44 @@
NAME
mdb-import - Import CSV data into an MDB database.
SYNOPSIS
mdb-import [-H lines] [-d char] database table csvfile
mdb-import -h|--help
DESCRIPTION
mdb-import is a utility program distributed with MDB Tools.
It reads a CSV (comma separated value) file and add the data into table of database.
OPTIONS
-H, --header lines Skip lines of CSV header.
-d, --delimiter char Specify an alternative column delimiter. Default is , (comma).
NOTES
ENVIRONMENT
MDB_JET3_CHARSET Defines the charset of the JET3 (access 97) file. Default is CP1252. See iconv(1).
MDBICONV Defines the input charset to use for the SQL file. Default is UTF-8. mdbtools must have been compiled with iconv.
MDBOPTS semi-column separated list of options:
* use_index
* no_memo
* debug_like
* debug_write
* debug_usage
* debug_ole
* debug_row
* debug_props
* debug_all is a shortcut for all debug_* options
SEE ALSO
gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) mdb-array(1)
mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1)
HISTORY
mdb-import first appeared in MDB Tools 0.7.
AUTHORS
The mdb-import utility was written by Brian Bruns.
BUGS
mdb-import does not enforce any kind of checks. You can violate constraints.

View File

@@ -33,7 +33,7 @@ ENVIRONMENT
SEE ALSO SEE ALSO
gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1)
mdb-array(1) mdb-header(1) mdb-schema(1) mdb-tables(1) mdb-array(1) mdb-header(1) mdb-schema(1) mdb-tables(1) mdb-import(1)
AUTHORS AUTHORS
The mdb-parsecsv utility was written by Brian Bruns. The mdb-parsecsv utility was written by Brian Bruns.

View File

@@ -29,7 +29,7 @@ ENVIRONMENT
SEE ALSO SEE ALSO
gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-sql(1) mdb-ver(1) mdb-array(1) gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-sql(1) mdb-ver(1) mdb-array(1)
mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-import(1)
AUTHORS AUTHORS
The mdb-prop utility was written by Brian Bruns. The mdb-prop utility was written by Brian Bruns.

View File

@@ -2,7 +2,8 @@ NAME
mdb-schema - Generate schema creation DDL mdb-schema - Generate schema creation DDL
SYNOPSIS SYNOPSIS
mdb-schema [options] database [backend] mdb-schema [options] [-T tablename] [-N prefix] database [backend]
mdb-schema -h|--help
DESCRIPTION DESCRIPTION
mdb-schema is a utility program distributed with MDB Tools. mdb-schema is a utility program distributed with MDB Tools.
@@ -10,8 +11,8 @@ DESCRIPTION
It produces DDL (data definition language) output for the given database. This can be passed to another database to create a replica of the original access table format. It produces DDL (data definition language) output for the given database. This can be passed to another database to create a replica of the original access table format.
OPTIONS OPTIONS
-T <table>, --table <table> Single table option. Create schema for this table only. Default is to export all tables. -T, --table tablename Single table option. Create schema for this table only. Default is to export all tables.
-N namespace Prefix identifiers with namespace. -N, --namespace prefix Prefix identifiers with namespace.
--drop-table Issue DROP TABLE statement. --drop-table Issue DROP TABLE statement.
--no-drop-table Don't issue DROP TABLE statement. This is the default. --no-drop-table Don't issue DROP TABLE statement. This is the default.
--not-null Issue NOT NULL constraints. This is the default. --not-null Issue NOT NULL constraints. This is the default.
@@ -48,7 +49,7 @@ HISTORY
SEE ALSO SEE ALSO
gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1)
mdb-array(1) mdb-header(1) mdb-parsecsv(1) mdb-tables(1) mdb-array(1) mdb-header(1) mdb-parsecsv(1) mdb-tables(1) mdb-import(1)
AUTHORS AUTHORS
The mdb-schema utility was written by Brian Bruns and others. The mdb-schema utility was written by Brian Bruns and others.

View File

@@ -2,7 +2,8 @@ NAME
mdb-sql - SQL interface to MDB Tools mdb-sql - SQL interface to MDB Tools
SYNOPSIS SYNOPSIS
mdb-sql [-HFp] [-d delimiter] [-i file] [-o file] [database] mdb-sql [-HFp] [-d char] [-i file] [-o file] [database]
mdb-sql -h|--help
DESCRIPTION DESCRIPTION
mdb-sql is a utility program distributed with MDB Tools. mdb-sql is a utility program distributed with MDB Tools.
@@ -11,27 +12,25 @@ DESCRIPTION
language. language.
OPTIONS OPTIONS
-H Suppress header row. -H, --no-header Suppress header row.
-F Suppress footer row. -F, --no-footer Suppress footer row.
-p Turn off pretty printing. By default results are printed in an -p, --no-pretty-print Turn off pretty printing. By default results are printed in an
ascii table format which looks nice but is not conducive to manipulating the ascii table format which looks nice but is not conducive to manipulating the
output with unix tools. This option prints output plainly in a tab separated output with unix tools. This option prints output plainly in a tab separated
format. format.
-d Specify an alternative column delimiter. If no delimiter is -d, --delimiter char Specify an alternative column delimiter. If no delimiter is
specified, columns will be delimited by a tab character if pretty printing specified, columns will be delimited by a tab character if pretty printing
(-p) is turned off. If pretty printing is enabled this option is meaningless. (-p) is turned off. If pretty printing is enabled this option is meaningless.
-i Specify an input file. This option allows an input file containing the SQL to be passed to mdb-sql. See Notes. -i, --input file Specify an input file. This option allows an input file containing the SQL to be passed to mdb-sql. See Notes.
-o Specify an output file. This option allows the name of an output file to be used instead of stdout. -o, --output file Specify an output file. This option allows the name of an output file to be used instead of stdout.
COMMANDS COMMANDS
mdb-sql in interactive mode takes some special commands. mdb-sql in interactive mode takes some special commands.
connect to <database> If no database was specified on the command line this command is necessary before any querys are issued. It also allows the switching of databases once in the tool. connect to database If no database was specified on the command line this command is necessary before any querys are issued. It also allows the switching of databases once in the tool.
disconnect Will disconnect from the current database. disconnect Will disconnect from the current database.
go Each batch is sent to the parser using the 'go' command. go Each batch is sent to the parser using the 'go' command.
reset A batch can be cleared using the 'reset' command. reset A batch can be cleared using the 'reset' command.
list tables The list tables command will display a list of available tables in this database, similar to the mdb-tables utility on the command line. list tables The list tables command will display a list of available tables in this database, similar to the mdb-tables utility on the command line.
describe table <table> Will display the column information for the specified table. describe table <table> Will display the column information for the specified table.
quit Will exit the tool. quit Will exit the tool.
@@ -76,8 +75,8 @@ HISTORY
mdb-sql first appeared in MDB Tools 0.3. mdb-sql first appeared in MDB Tools 0.3.
SEE ALSO SEE ALSO
gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-ver(1) mdb-array(1) gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-import(1) mdb-prop(1) mdb-ver(1)
mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) isql(1) mdb-array(1) mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) isql(1)
AUTHORS AUTHORS
The mdb-sql utility was written by Brian Bruns. The mdb-sql utility was written by Brian Bruns.

View File

@@ -2,7 +2,8 @@ NAME
mdb-tables - Get listing of tables in an MDB database mdb-tables - Get listing of tables in an MDB database
SYNOPSIS SYNOPSIS
mdb-tables [-S] [-1 | -d delimiter] database mdb-tables [-S] [-1 | -d delim] [-t form|table|macro|systable|report|query|linkedtable|module|relationship|dbprop|any|all] [-T] database
mdb-tables -h|--help
DESCRIPTION DESCRIPTION
mdb-tables is a utility program distributed with MDB Tools. mdb-tables is a utility program distributed with MDB Tools.
@@ -10,11 +11,11 @@ DESCRIPTION
It produces a list of tables contained within an MDB database in a format suitable for use in shell scripts. It produces a list of tables contained within an MDB database in a format suitable for use in shell scripts.
OPTIONS OPTIONS
-S Show system tables. System tables are generally those beginning with 'MSys' -S, --system Show system tables. System tables are generally those beginning with 'MSys'.
-1, --single-column Specifies that the tables should be listed 1 per line. This is equivalent to --delim \n.
-1 specifies that the tables should be listed 1 per line. -d, --delimiter delim Specifies an alternative delimiter. If no delimiter is specified, table names will be delimited by a tab character, or by newline if the -1 option was specified.
-t, --type type Filters entries to show alternate types. Autorized values are form, table, macro, systable, report, query, linkedtable, module, relationship, dbprop, any, and all. Default value is table.
-d delimiter specifies an alternative delimiter. If no delimiter is specified, table names will be delimited by a tab character, or by newline if the -1 option was specified. -T, --showtype Display the entry type indentifier before each entry.
NOTES NOTES
@@ -36,7 +37,7 @@ HISTORY
mdb-tables first appeared in MDB Tools 0.3. mdb-tables first appeared in MDB Tools 0.3.
SEE ALSO SEE ALSO
gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-ver(1) gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-import(1) mdb-prop(1) mdb-sql(1) mdb-ver(1)
mdb-array(1) mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-array(1) mdb-header(1) mdb-parsecsv(1) mdb-schema(1)
AUTHORS AUTHORS

View File

@@ -4,6 +4,7 @@ NAME
SYNOPSIS SYNOPSIS
mdb-ver database mdb-ver database
mdb-ver -M mdb-ver -M
mdb-ver -h|--help
DESCRIPTION DESCRIPTION
mdb-ver is a utility program distributed with MDB Tools. mdb-ver is a utility program distributed with MDB Tools.
@@ -12,7 +13,7 @@ DESCRIPTION
OPTIONS OPTIONS
-M Prints the version of MDB Tools itself instead of the MDB file. -M, --mdbtools Prints the version of MDB Tools itself instead of the MDB file.
NOTES NOTES
Access changed its format between Jet 3 used in Access 97 and Jet 4 used for Access 2000 and XP. The nature of the changes included moving the page size from 2K to 4K and added support for unicode. MDB Tools actively supports both formats. Newer version are very much like Jet4. Access changed its format between Jet 3 used in Access 97 and Jet 4 used for Access 2000 and XP. The nature of the changes included moving the page size from 2K to 4K and added support for unicode. MDB Tools actively supports both formats. Newer version are very much like Jet4.
@@ -35,8 +36,8 @@ HISTORY
mdb-ver first appeared in MDB Tools 0.4. mdb-ver first appeared in MDB Tools 0.4.
SEE ALSO SEE ALSO
gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-prop(1) mdb-sql(1) mdb-array(1) gmdb2(1) mdb-export(1) mdb-hexdump(1) mdb-import(1) mdb-prop(1) mdb-sql(1)
mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1) mdb-array(1) mdb-header(1) mdb-parsecsv(1) mdb-schema(1) mdb-tables(1)
AUTHORS AUTHORS
The mdb-ver utility was written by Brian Bruns. The mdb-ver utility was written by Brian Bruns.

View File

@@ -87,118 +87,103 @@ main(int argc, char **argv)
char *row_delimiter = NULL; char *row_delimiter = NULL;
char *quote_char = NULL; char *quote_char = NULL;
char *escape_char = NULL; char *escape_char = NULL;
char header_row = 1; int header_row = 1;
char quote_text = 1; int quote_text = 1;
char *insert_dialect = NULL; char *insert_dialect = NULL;
char *date_fmt = NULL;
char *namespace = NULL; char *namespace = NULL;
char *str_bin_mode = NULL;
int bin_mode = MDB_BINEXPORT_RAW; int bin_mode = MDB_BINEXPORT_RAW;
int opt;
char *value; char *value;
size_t length; size_t length;
while ((opt=getopt(argc, argv, "HQq:X:d:D:R:I:N:b:"))!=-1) { GOptionEntry entries[] = {
switch (opt) { { "no-header", 'H', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &header_row, "Suppress header row.", NULL},
case 'H': { "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 (argc != 3) {
fputs("Wrong number of arguments.\n\n", stderr);
fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
exit(1);
}
/* 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; header_row = 0;
break;
case 'Q': if (date_fmt)
quote_text = 0; mdb_set_date_fmt(date_fmt);
break;
case 'q': if (str_bin_mode) {
quote_char = (char *) g_strdup(optarg); if (!strcmp(str_bin_mode, "strip"))
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; bin_mode = MDB_BINEXPORT_STRIP;
else if (!strcmp(optarg, "raw")) else if (!strcmp(str_bin_mode, "raw"))
bin_mode = MDB_BINEXPORT_RAW; bin_mode = MDB_BINEXPORT_RAW;
else if (!strcmp(optarg, "octal")) else if (!strcmp(str_bin_mode, "octal"))
bin_mode = MDB_BINEXPORT_OCTAL; bin_mode = MDB_BINEXPORT_OCTAL;
else { else {
fprintf(stderr, "Invalid binary mode\n"); fputs("Invalid binary mode\n", stderr);
exit(1); exit(1);
} }
break;
default:
break;
}
}
if (!quote_char) {
quote_char = (char *) g_strdup("\"");
}
if (!delimiter) {
delimiter = (char *) g_strdup(",");
}
if (!row_delimiter) {
row_delimiter = (char *) g_strdup("\n");
} }
/* /* Open file */
** optind is now the position of the first non-option arg, if (!(mdb = mdb_open(argv[1], MDB_NOFLAGS))) {
** see getopt(3) /* Don't bother clean up memory before exit */
*/
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);
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);
exit(1); exit(1);
} }
if (insert_dialect) if (insert_dialect)
if (!mdb_set_default_backend(mdb, insert_dialect)) { if (!mdb_set_default_backend(mdb, insert_dialect)) {
fprintf(stderr, "Invalid backend type\n"); fputs("Invalid backend type\n", stderr);
if (escape_char) g_free (escape_char); /* Don't bother clean up memory before exit */
exit(1); 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) { if (!table) {
fprintf(stderr, "Error: Table %s does not exist in this database.\n", argv[argc-1]); fprintf(stderr, "Error: Table %s does not exist in this database.\n", argv[2]);
g_free (delimiter); /* Don't bother clean up memory before exit */
g_free (row_delimiter);
g_free (quote_char);
if (escape_char) g_free (escape_char);
mdb_close(mdb);
exit(1); exit(1);
} }
@@ -227,7 +212,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[optind + 1]); quoted_name = mdb->default_backend->quote_schema_name(namespace, argv[2]);
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++) {
@@ -272,12 +257,18 @@ main(int argc, char **argv)
g_free(bound_lens); g_free(bound_lens);
mdb_free_tabledef(table); mdb_free_tabledef(table);
mdb_close(mdb);
g_option_context_free(opt_context);
// g_free ignores NULL
g_free(quote_char);
g_free(delimiter); g_free(delimiter);
g_free(row_delimiter); g_free(row_delimiter);
g_free (quote_char); g_free(insert_dialect);
if (escape_char) g_free (escape_char); g_free(date_fmt);
g_free(escape_char);
mdb_close(mdb); g_free(namespace);
g_free(str_bin_mode);
return 0; return 0;
} }
@@ -285,6 +276,7 @@ static char *escapes(char *s)
{ {
char *d = (char *) g_strdup(s); char *d = (char *) g_strdup(s);
char *t = d; char *t = d;
char *orig = s;
unsigned char encode = 0; unsigned char encode = 0;
for (;*s; s++) { for (;*s; s++) {
@@ -303,5 +295,6 @@ static char *escapes(char *s)
} }
} }
*t='\0'; *t='\0';
g_free(orig);
return d; return d;
} }

View File

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

View File

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

View File

@@ -356,13 +356,41 @@ int line = 0;
char *mybuf; char *mybuf;
unsigned int bufsz; unsigned int bufsz;
MdbSQL *sql; MdbSQL *sql;
int opt;
FILE *in = NULL, *out = NULL; FILE *in = NULL, *out = NULL;
char *filename_in=NULL, *filename_out=NULL;
char *home = getenv("HOME"); char *home = getenv("HOME");
char *histpath; char *histpath;
char *delimiter = NULL; 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 #ifdef HAVE_READLINE_HISTORY
if (home) { if (home) {
histpath = (char *) g_strconcat(home, "/", HISTFILE, NULL); histpath = (char *) g_strconcat(home, "/", HISTFILE, NULL);
@@ -374,44 +402,26 @@ char *delimiter = NULL;
if (!isatty(fileno(stdin))) { if (!isatty(fileno(stdin))) {
in = stdin; in = stdin;
} }
if (filename_in) {
while ((opt=getopt(argc, argv, "HFpd:i:o:"))!=-1) { if (!strcmp(filename_in, "stdin"))
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; in = stdin;
else if (!(in = fopen(optarg, "r"))) { else if (!(in = fopen(filename_in, "r"))) {
fprintf(stderr,"Unable to open file %s\n", optarg); fprintf(stderr, "Unable to open file %s\n", filename_in);
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); 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 */ /* initialize the SQL engine */
sql = mdb_sql_init(); sql = mdb_sql_init();
if (argc>optind) { if (argc == 2) {
mdb_sql_open(sql, argv[optind]); mdb_sql_open(sql, argv[1]);
} }
/* give the buffer an initial size */ /* give the buffer an initial size */
@@ -473,7 +483,6 @@ char *delimiter = NULL;
mdb_sql_exit(sql); mdb_sql_exit(sql);
g_free(mybuf); g_free(mybuf);
g_free(delimiter);
if (s) free(s); if (s) free(s);
if (out) fclose(out); if (out) fclose(out);
if ((in) && (in != stdin)) fclose(in); if ((in) && (in != stdin)) fclose(in);
@@ -487,6 +496,11 @@ char *delimiter = NULL;
} }
#endif #endif
g_option_context_free(opt_context);
g_free(delimiter);
g_free(filename_in);
g_free(filename_out);
return 0; return 0;
} }
#else #else

View File

@@ -86,44 +86,50 @@ main (int argc, char **argv)
int line_break=0; int line_break=0;
int skip_sys=1; int skip_sys=1;
int show_type=0; int show_type=0;
int opt;
int objtype = MDB_TABLE; 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) { opt_context = g_option_context_new("<file> - show MDB files tables/entries");
fprintf (stderr, "Usage: %s [-S] [-1 | -d<delimiter>] [-t <type>] [-T] <file>\n",argv[0]); 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()); fprintf(stderr, "Valid types are: %s\n",valid_types());
exit (1); exit (1);
} }
while ((opt=getopt(argc, argv, "S1Td:t:"))!=-1) { if (argc != 2) {
switch (opt) { fputs("Wrong number of arguments.\n\n", stderr);
case 'S': fputs(g_option_context_get_help(opt_context, TRUE, NULL), stderr);
skip_sys = 0; fprintf(stderr, "Valid types are: %s\n",valid_types());
break; exit(1);
case 'T': }
show_type = 1;
break; if (str_objtype) {
case '1': if (!get_obj_type(str_objtype, &objtype)) {
line_break = 1;
break;
case 't':
if (!get_obj_type(optarg, &objtype)) {
fprintf(stderr,"Invalid type name.\n"); fprintf(stderr,"Invalid type name.\n");
fprintf (stderr, "Valid types are: %s\n",valid_types()); fprintf (stderr, "Valid types are: %s\n",valid_types());
exit(1); exit(1);
} }
break;
case 'd':
delimiter = (char *) g_strdup(optarg);
break;
} }
} if (!delimiter)
delimiter = g_strdup(" ");
/* open the database */ /* 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"); fprintf(stderr,"Couldn't open database.\n");
exit(1); exit(1);
} }
@@ -160,7 +166,9 @@ main (int argc, char **argv)
fprintf (stdout, "\n"); fprintf (stdout, "\n");
mdb_close(mdb); mdb_close(mdb);
g_option_context_free(opt_context);
g_free(delimiter); g_free(delimiter);
g_free(str_objtype);
return 0; return 0;
} }

View File

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

View File

@@ -7,12 +7,16 @@ _mdb_export()
cur=${COMP_WORDS[COMP_CWORD]} cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]} 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 return 0
elif [[ "$prev" == -I ]] ; then elif [[ "$prev" == -I ]] ; then
COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql' -- $cur ) ) COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql' -- $cur ) )
elif [[ "$prev" == -@(b|-bin) ]] ; then
COMPREPLY=( $( compgen -W 'strip raw octal' -- $cur ) )
elif [[ "$cur" == -* ]]; then 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 elif [[ "$prev" == *@(mdb|mdw|accdb) ]] ; then
local dbname local dbname
local tablenames local tablenames
@@ -88,16 +92,18 @@ _mdb_schema()
cur=${COMP_WORDS[COMP_CWORD]} cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]} prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -@(T|table|N) ]] ; then if [[ "$prev" == -@(T|-table|N|-namespace) ]] ; then
return 0 return 0
elif [[ "$cur" == -* ]]; then elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-T --table -N \ COMPREPLY=( $( compgen -W '-T --table \
-N --namespace \
--drop-table --no-drop-table \ --drop-table --no-drop-table \
--not-null --no-not-null \ --not-null --no-not-null \
--default-values --no-default-values \ --default-values --no-default-values \
--not-empty --no-not-empty \ --not-empty --no-not-empty \
--indexes --no-indexes \ --indexes --no-indexes \
--relations --no-relations' -- $cur ) ) --relations --no-relations
-h --help' -- $cur ) )
elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then elif [[ "$prev" == @(*mdb|*mdw|*accdb) ]]; then
COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql' -- $cur ) ) COMPREPLY=( $( compgen -W 'access sybase oracle postgres mysql' -- $cur ) )
else else
@@ -118,10 +124,16 @@ _mdb_sql()
if [[ "$prev" == -d ]] ; then if [[ "$prev" == -d ]] ; then
return 0 return 0
elif [[ "$prev" == -@(i|o) ]] ; then elif [[ "$prev" == -@(i|-input|o|-output) ]] ; then
_filedir _filedir
elif [[ "$cur" == -* ]]; then 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 else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi
@@ -138,13 +150,18 @@ _mdb_tables()
cur=${COMP_WORDS[COMP_CWORD]} cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]} prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ "$prev" == -d ]]; then if [[ "$prev" == -@(d|-delimiter) ]]; then
return 0 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 ) ) COMPREPLY=( $( compgen -W 'form table macro systable report query linkedtable module relationship dbprop any all' -- $cur ) )
return 0 return 0
elif [[ "$cur" == -* ]]; then 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 else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi
@@ -161,10 +178,39 @@ _mdb_ver()
cur=${COMP_WORDS[COMP_CWORD]} cur=${COMP_WORDS[COMP_CWORD]}
if [[ "$cur" == -* ]]; then if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-M' -- $cur ) ) COMPREPLY=( $( compgen -W '-M -h --help' -- $cur ) )
else else
_filedir '@(mdb|mdw|accdb)' _filedir '@(mdb|mdw|accdb)'
fi fi
return 0 return 0
} && } &&
complete -F _mdb_ver mdb-ver 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