mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-07-15 08:19:08 +08:00
mdb-sql: Simplify handling files loaded by ":r"
:r is an undocumented command to load SQL files from the msb-sql prompt
This commit is contained in:
parent
eab60a6060
commit
833985043f
@ -156,40 +156,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)
|
||||||
{
|
{
|
||||||
@ -370,6 +336,7 @@ main(int argc, char **argv)
|
|||||||
char *home = getenv("HOME");
|
char *home = getenv("HOME");
|
||||||
char *histpath;
|
char *histpath;
|
||||||
char *delimiter = NULL;
|
char *delimiter = NULL;
|
||||||
|
int in_from_colon_r = 0;
|
||||||
|
|
||||||
|
|
||||||
GOptionEntry entries[] = {
|
GOptionEntry entries[] = {
|
||||||
@ -443,14 +410,19 @@ main(int argc, char **argv)
|
|||||||
if (s) free(s);
|
if (s) free(s);
|
||||||
|
|
||||||
if (in) {
|
if (in) {
|
||||||
s=malloc(bufsz);
|
s=calloc(bufsz, 1);
|
||||||
if (!fgets(s, bufsz, in)) {
|
if (!fgets(s, bufsz, in)) {
|
||||||
// Backwards compatibility with older MDBTools
|
// Backwards compatibility with older MDBTools
|
||||||
// Files read from the command line had an
|
// Files read from the command line had an
|
||||||
// implicit "go" at the end
|
// implicit "go" at the end
|
||||||
if (strlen(mybuf))
|
if (!in_from_colon_r && strlen(mybuf))
|
||||||
strcpy(s, "go");
|
strcpy(s, "go");
|
||||||
else
|
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')
|
} else if (s[strlen(s)-1]=='\n')
|
||||||
s[strlen(s)-1]=0;
|
s[strlen(s)-1]=0;
|
||||||
@ -475,7 +447,19 @@ 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 {
|
||||||
|
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 {
|
} else {
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
@ -484,8 +468,10 @@ main(int argc, char **argv)
|
|||||||
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, or lines read from files */
|
/* don't record blank lines, or lines read from files
|
||||||
if (!in && 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 */
|
||||||
|
Loading…
Reference in New Issue
Block a user