Merge branch 'dev' of github.com:mdbtools/mdbtools

This commit is contained in:
Evan Miller 2020-09-19 14:01:23 -04:00
commit 9d0c40905d
2 changed files with 64 additions and 48 deletions

2
README
View File

@ -1,3 +1 @@
Readme moved to Readme.md Readme moved to Readme.md
# Keeping this file as it's required by automake

View File

@ -152,40 +152,6 @@ do_set_cmd(MdbSQL *sql, char *s)
} }
} }
int
read_file(char *s, int line, unsigned int *bufsz, char *mybuf)
{
char *fname;
FILE *in;
char buf[256];
unsigned int cursz = 0;
int lines = 0;
fname = s;
while (*fname && *fname==' ') fname++;
if (! (in = fopen(fname, "r"))) {
fprintf(stderr,"Unable to open file %s\n", fname);
mybuf[0]=0;
return 0;
}
while (fgets(buf, 255, in)) {
cursz += strlen(buf) + 1;
if (cursz > (*bufsz)) {
(*bufsz) *= 2;
mybuf = (char *) realloc(mybuf, *bufsz);
}
strcat(mybuf, buf);
#ifdef HAVE_READLINE_HISTORY
/* don't record blank lines */
if (strlen(buf)) add_history(buf);
#endif
strcat(mybuf, "\n");
lines++;
printf("%d => %s",line+lines, buf);
}
return lines;
}
void void
run_query(FILE *out, MdbSQL *sql, char *mybuf, char *delimiter) run_query(FILE *out, MdbSQL *sql, char *mybuf, char *delimiter)
{ {
@ -330,6 +296,28 @@ dump_results_pp(FILE *out, MdbSQL *sql)
} }
} }
static char *
find_sql_terminator(char *s)
{
char *sp;
int len = strlen(s);
if (len == 0) {
return NULL;
}
sp = &s[len-1];
while (sp > s && isspace(*sp)) {
sp--;
}
if (*sp == ';') {
return sp;
}
return NULL;
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -346,6 +334,7 @@ main(int argc, char **argv)
char *histpath; char *histpath;
#endif #endif
char *delimiter = NULL; char *delimiter = NULL;
int in_from_colon_r = 0;
GOptionEntry entries[] = { GOptionEntry entries[] = {
@ -419,15 +408,21 @@ main(int argc, char **argv)
if (s) free(s); if (s) free(s);
if (in) { if (in) {
s=malloc(256); s=calloc(bufsz, 1);
if ((!s) || (!fgets(s, 256, in))) { if (!fgets(s, bufsz, in)) {
/* if we have something in the buffer, run it */ // Backwards compatibility with older MDBTools
if (strlen(mybuf)) // Files read from the command line had an
run_query((out) ? out : stdout, // implicit "go" at the end
sql, mybuf, delimiter); if (!in_from_colon_r && strlen(mybuf))
strcpy(s, "go");
else if (in_from_colon_r) {
line = 0;
fclose(in);
in = NULL;
in_from_colon_r = 0;
} else
break; break;
} } else if (s[strlen(s)-1]=='\n')
if (s[strlen(s)-1]=='\n')
s[strlen(s)-1]=0; s[strlen(s)-1]=0;
} else { } else {
sprintf(prompt, "%d => ", line); sprintf(prompt, "%d => ", line);
@ -450,19 +445,42 @@ main(int argc, char **argv)
line = 0; line = 0;
mybuf[0]='\0'; mybuf[0]='\0';
} else if (!strncmp(s,":r",2)) { } else if (!strncmp(s,":r",2)) {
line += read_file(&s[2], line, &bufsz, mybuf); char *fname = &s[2];
if (in) {
fprintf(stderr, "Can not handle nested opens\n");
} else { } else {
while (*fname && isspace(*fname))
fname++;
if (!(in = fopen(fname, "r"))) {
fprintf(stderr,"Unable to open file %s\n", fname);
mybuf[0]=0;
} else {
in_from_colon_r = 1;
}
}
} else {
char *p;
while (strlen(mybuf) + strlen(s) > bufsz) { while (strlen(mybuf) + strlen(s) > bufsz) {
bufsz *= 2; bufsz *= 2;
mybuf = (char *) g_realloc(mybuf, bufsz); mybuf = (char *) g_realloc(mybuf, bufsz);
} }
#ifdef HAVE_READLINE_HISTORY #ifdef HAVE_READLINE_HISTORY
/* don't record blank lines */ /* don't record blank lines, or lines read from files
if (strlen(s)) add_history(s); * specified on the command line */
if ((!in || in_from_colon_r) && strlen(s))
add_history(s);
#endif #endif
strcat(mybuf,s); strcat(mybuf,s);
/* preserve line numbering for the parser */ /* preserve line numbering for the parser */
strcat(mybuf,"\n"); strcat(mybuf,"\n");
if ((p = find_sql_terminator(mybuf))) {
*p = '\0';
line = 0;
run_query((out) ? out : stdout, sql, mybuf, delimiter);
mybuf[0]='\0';
}
} }
} }
mdb_sql_exit(sql); mdb_sql_exit(sql);