/* 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; }
str mnstr_read_stringwrap(str *res, Stream *S) { stream *s = *(stream **)S; ssize_t len = 0; size_t size = CHUNK + 1; char *buf = GDKmalloc(size), *start = buf; while ((len = mnstr_read(s, start, 1, CHUNK)) > 0) { size += len; buf = GDKrealloc(buf, size); start = buf + size - CHUNK - 1; *start = '\0'; } if (len < 0) throw(IO, "streams.readStr", "failed to read string"); start += len; *start = '\0'; *res = buf; return MAL_SUCCEED; }
int main(int argc, char **argv) { ssize_t n; size_t len, buflen; char *host = NULL; int portnr = 0; char *uri = NULL; char *user = NULL; char *password = NULL; char buf[BUFSIZ], *buffer, *e, *response; int done = 0; EventRecord event; static struct option long_options[15] = { { "dbname", 1, 0, 'd' }, { "user", 1, 0, 'u' }, { "port", 1, 0, 'p' }, { "password", 1, 0, 'P' }, { "host", 1, 0, 'h' }, { "help", 0, 0, '?' }, { "output", 1, 0, 'o' }, { "queries", 1, 0, 'q' }, { "debug", 0, 0, 'D' }, { 0, 0, 0, 0 } }; /* parse config file first, command line options override */ parse_dotmonetdb(&user, &password, &dbname, NULL, NULL, NULL, NULL); while (1) { int option_index = 0; int c = getopt_long(argc, argv, "d:u:p:P:h:?:o:q:D", long_options, &option_index); if (c == -1) break; switch (c) { case 'D': debug = 1; break; case 'd': if (dbname) free(dbname); dbname = strdup(optarg); break; case 'u': if (user) free(user); user = strdup(optarg); /* force password prompt */ if (password) free(password); password = NULL; break; case 'P': if (password) free(password); password = strdup(optarg); break; case 'p': if (optarg) portnr = atoi(optarg); break; case 'q': if (optarg) querypool = atoi(optarg) > 0? atoi(optarg):1; break; case 'h': host = optarg; break; case '?': usageTachograph(); /* 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 */ exit(strcmp(argv[optind - 1], "-?") == 0 || strcmp(argv[optind - 1], "--help") == 0 ? 0 : -1); default: usageTachograph(); exit(-1); } } if( dbname == NULL){ usageTachograph(); exit(-1); } if (dbname != NULL && strncmp(dbname, "mapi:monetdb://", 15) == 0) { uri = dbname; dbname = NULL; } #ifdef SIGPIPE signal(SIGPIPE, stopListening); #endif #ifdef SIGHUP signal(SIGHUP, stopListening); #endif #ifdef SIGQUIT signal(SIGQUIT, stopListening); #endif signal(SIGINT, stopListening); signal(SIGTERM, stopListening); close(0); if (user == NULL) user = simple_prompt("user", BUFSIZ, 1, prompt_getlogin()); if (password == NULL) password = simple_prompt("password", BUFSIZ, 0, NULL); /* our hostname, how remote servers have to contact us */ gethostname(hostname, sizeof(hostname)); /* set up the profiler */ if (uri) dbh = mapi_mapiuri(uri, user, password, "mal"); else dbh = mapi_mapi(host, portnr, user, password, "mal", dbname); if (dbh == NULL || mapi_error(dbh)) die(dbh, hdl); mapi_reconnect(dbh); if (mapi_error(dbh)) die(dbh, hdl); host = strdup(mapi_get_host(dbh)); if(debug) fprintf(stderr,"-- connection with server %s\n", uri ? uri : host); snprintf(buf,BUFSIZ-1,"profiler.setheartbeat(0);"); if( debug) fprintf(stderr,"-- %s\n",buf); doQ(buf); snprintf(buf, BUFSIZ, " profiler.openstream(0);"); if( debug) fprintf(stderr,"-- %s\n",buf); doQ(buf); len = 0; buflen = BUFSIZ; buffer = (char *) malloc(buflen); if( buffer == NULL){ fprintf(stderr,"Could not create input buffer\n"); exit(-1); } conn = mapi_get_from(dbh); while ((n = mnstr_read(conn, buffer + len, 1, buflen - len-1)) >= 0) { if (n == 0 && (n = mnstr_read(conn, buffer + len, 1, buflen - len-1)) <= 0) break; buffer[len + n] = 0; response = buffer; while ((e = strchr(response, '\n')) != NULL) { *e = 0; if(debug) printf("%s\n", response); done= keyvalueparser(response, &event); if( done == 1){ update(&event); } else if( done == 0){ if (debug ) fprintf(stderr, "PARSE %d:%s\n", done, response); } response = e + 1; } /* handle the case that the line is too long to * fit in the buffer */ if( response == buffer){ char *new = (char *) realloc(buffer, buflen + BUFSIZ); if( new == NULL){ fprintf(stderr,"Could not extend input buffer\n"); exit(-1); } buffer = new; buflen += BUFSIZ; len += n; } /* handle the case the buffer contains more than one * line, and the last line is not completely read yet. * Copy the first part of the incomplete line to the * beginning of the buffer */ else if (*response) {