static int error(stream *out, char *str) { char *p; if (!out) out = GDKerr; if (str == NULL) return 0; if (mnstr_errnr(out)) return -1; while ((p = strchr(str, '\n')) != NULL) { p++; /* include newline */ if (*str !='!' && mnstr_write(out, "!", 1, 1) != 1) return -1; if (mnstr_write(out, str, p - str, 1) != 1) return -1; str = p; } if (str &&*str) { if (*str !='!' && mnstr_write(out, "!", 1, 1) != 1) return -1; if (mnstr_write(out, str, strlen(str), 1) != 1 || mnstr_write(out, "\n", 1, 1) != 1) return -1; } return 0; }
str mnstr_open_wastreamwrap(Stream *S, str *filename) { stream *s; if ((s = open_wastream(*filename)) == NULL || mnstr_errnr(s)) { int errnr = mnstr_errnr(s); if (s) mnstr_destroy(s); throw(IO, "streams.open", "could not open file '%s': %s", *filename, strerror(errnr)); } else { *(stream**)S = s; } return MAL_SUCCEED; }
str mnstr_socket_wastreamwrap(Stream *S, int *socket, str *name) { stream *s; if ((s = socket_wastream(*socket, *name)) == NULL || mnstr_errnr(s)) { int errnr = mnstr_errnr(s); if (s) mnstr_destroy(s); throw(IO, "streams.open", "could not open socket: %s", strerror(errnr)); } else { *(stream**)S = s; } return MAL_SUCCEED; }
/* COMMAND "getContent": Retrieve the file referenced * SIGNATURE: getContent(str) : str; */ str URLgetContent(str *retval, url *Str1) { stream *f; str retbuf = NULL; str oldbuf = NULL; char *buf[8096]; ssize_t len; size_t rlen; if ((f = open_urlstream(*Str1)) == NULL) throw(MAL, "url.getContent", "failed to open urlstream"); if (mnstr_errnr(f) != 0) { str err = createException(MAL, "url.getContent", "opening stream failed: %s", mnstr_error(f)); mnstr_destroy(f); *retval = NULL; return err; } rlen = 0; while ((len = mnstr_read(f, buf, 1, sizeof(buf))) > 0) { if (retbuf != NULL) { oldbuf = retbuf; retbuf = GDKrealloc(retbuf, rlen + len + 1); } else { retbuf = GDKmalloc(len + 1); } if (retbuf == NULL) { if (oldbuf != NULL) GDKfree(oldbuf); mnstr_destroy(f); throw(MAL, "url.getContent", "contents too large"); } oldbuf = NULL; (void)memcpy(retbuf + rlen, buf, len); rlen += len; } if (len < 0) { GDKfree(retbuf); throw(MAL, "url.getContent", "read error"); } retbuf[rlen] = '\0'; *retval = retbuf; return MAL_SUCCEED; }
/* * Locate a file with SQL commands and execute it. For the time being a 1MB * file limit is implicitly imposed. If the file can not be located in the * script library, we assume it is sufficiently self descriptive. * (Respecting the file system context where the call is executed ) */ str SQLinclude(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { stream *fd; bstream *bfd; str *name = getArgReference_str(stk, pci, 1); str msg = MAL_SUCCEED, fullname; str *expr; mvc *m; size_t sz; fullname = MSP_locate_sqlscript(*name, 0); if (fullname == NULL) fullname = *name; fd = open_rastream(fullname); if (mnstr_errnr(fd) == MNSTR_OPEN_ERROR) { mnstr_destroy(fd); throw(MAL, "sql.include", "could not open file: %s\n", *name); } sz = getFileSize(fd); if (sz > (size_t) 1 << 29) { mnstr_destroy(fd); throw(MAL, "sql.include", "file %s too large to process", fullname); } bfd = bstream_create(fd, sz == 0 ? (size_t) (128 * BLOCK) : sz); if (bstream_next(bfd) < 0) { bstream_destroy(bfd); throw(MAL, "sql.include", "could not read %s\n", *name); } expr = &bfd->buf; msg = SQLstatementIntern(cntxt, expr, "sql.include", TRUE, FALSE, NULL); bstream_destroy(bfd); m = ((backend *) cntxt->sqlcontext)->mvc; if (m->sa) sa_destroy(m->sa); m->sa = NULL; (void) mb; return msg; }
int main(int argc, char **argv) { int port = 0; char *user = NULL; char *passwd = NULL; char *host = NULL; char *dbname = NULL; int trace = 0; int describe = 0; int functions = 0; int useinserts = 0; int c; Mapi mid; int quiet = 0; stream *out; char user_set_as_flag = 0; char *table = NULL; static struct option long_options[] = { {"host", 1, 0, 'h'}, {"port", 1, 0, 'p'}, {"database", 1, 0, 'd'}, {"describe", 0, 0, 'D'}, {"functions", 0, 0, 'f'}, {"table", 1, 0, 't'}, {"inserts", 0, 0, 'N'}, {"Xdebug", 0, 0, 'X'}, {"user", 1, 0, 'u'}, {"quiet", 0, 0, 'q'}, {"help", 0, 0, '?'}, {0, 0, 0, 0} }; parse_dotmonetdb(&user, &passwd, NULL, NULL, NULL, NULL); while ((c = getopt_long(argc, argv, "h:p:d:Dft:NXu:q?", long_options, NULL)) != -1) { switch (c) { case 'u': if (user) free(user); user = strdup(optarg); user_set_as_flag = 1; break; case 'h': host = optarg; break; case 'p': assert(optarg != NULL); port = atoi(optarg); break; case 'd': dbname = optarg; break; case 'D': describe = 1; break; case 'N': useinserts = 1; break; case 'f': if (table) usage(argv[0], -1); functions = 1; break; case 't': if (table || functions) usage(argv[0], -1); table = optarg; break; case 'q': quiet = 1; break; case 'X': trace = MAPI_TRACE; break; case '?': /* a bit of a hack: look at the option that the current `c' is based on and see if we recognize it: if -? or --help, exit with 0, else with -1 */ usage(argv[0], strcmp(argv[optind - 1], "-?") == 0 || strcmp(argv[optind - 1], "--help") == 0 ? 0 : -1); default: usage(argv[0], -1); } } if (optind == argc - 1) dbname = argv[optind]; else if (optind != argc) usage(argv[0], -1); /* when config file would provide defaults */ if (user_set_as_flag) passwd = NULL; if (user == NULL) user = simple_prompt("user", BUFSIZ, 1, prompt_getlogin()); if (passwd == NULL) passwd = simple_prompt("password", BUFSIZ, 0, NULL); mid = mapi_connect(host, port, user, passwd, "sql", dbname); if (user) free(user); if (passwd) free(passwd); if (mid == NULL) { fprintf(stderr, "failed to allocate Mapi structure\n"); exit(2); } if (mapi_error(mid)) { mapi_explain(mid, stderr); exit(2); } if (!quiet) { char *motd = mapi_get_motd(mid); if (motd) fprintf(stderr, "%s", motd); } mapi_trace(mid, trace); mapi_cache_limit(mid, 10000); out = file_wastream(stdout, "stdout"); if (out == NULL) { fprintf(stderr, "failed to allocate stream\n"); exit(2); } if (!quiet) { char buf[27]; time_t t = time(0); char *p; #ifdef HAVE_CTIME_R3 ctime_r(&t, buf, sizeof(buf)); #else #ifdef HAVE_CTIME_R ctime_r(&t, buf); #else strncpy(buf, ctime(&t), sizeof(buf)); #endif #endif if ((p = strrchr(buf, '\n')) != NULL) *p = 0; mnstr_printf(out, "-- msqldump %s %s%s %s\n", describe ? "describe" : "dump", functions ? "functions" : table ? "table " : "database", table ? table : "", buf); dump_version(mid, out, "--"); } if (functions) c = dump_functions(mid, out, NULL, NULL); else if (table) c = dump_table(mid, NULL, table, out, describe, 1, useinserts); else c = dump_database(mid, out, describe, useinserts); mnstr_flush(out); mapi_destroy(mid); if (mnstr_errnr(out)) { fprintf(stderr, "%s: %s", argv[0], mnstr_error(out)); return 1; } mnstr_destroy(out); return c; }