void _pf_punt_init() /* Initialize punt/catch system. Mostly just redirects the * errAbort handler. */ { punter.message = dyStringNew(0); punter.source = dyStringNew(0); pushWarnHandler(puntWarnHandler); pushAbortHandler(puntAbortHandler); }
boolean errCatchPushHandlers(struct errCatch *errCatch) /* Push error handlers. Not usually called directly. */ { pushAbortHandler(errCatchAbortHandler); pushWarnHandler(errCatchWarnHandler); struct errCatch **pErrCatchStack = getStack(); slAddHead(pErrCatchStack, errCatch); return TRUE; }
void hDumpStackPushAbortHandler(void) /* push the stack dump abort handler on the stack if it's enabled. This should be pushed * after the warn handle that will do the actual reporting */ { if (hDumpStackEnabled()) { errAbortDebugnPushPopErr(); pushAbortHandler(hDumpStackAbortHandler); } }
void webPushErrHandlers(void) /* Push warn and abort handler for errAbort(). */ { if (webInTextMode) pushWarnHandler(textVaWarn); else pushWarnHandler(webVaWarn); pushAbortHandler(softAbort); hDumpStackPushAbortHandler(); }
static void cartJsonPushErrHandlers() /* Push warn and abort handlers for errAbort. */ { if (dyWarn == NULL) dyWarn = dyStringNew(0); else dyStringClear(dyWarn); pushWarnHandler(cartJsonVaWarn); pushAbortHandler(cartJsonAbort); }
static char *getSearchTermUpperCase() /* If we don't have the SEARCH_TERM cgi param, exit with an HTTP Bad Request response. * If we do, convert it to upper case for case-insensitive matching and return it. */ { pushAbortHandler(htmlVaBadRequestAbort); char *term = cgiOptionalString(SEARCH_TERM); touppers(term); if (isEmpty(term)) errAbort("Missing required CGI parameter %s", SEARCH_TERM); popAbortHandler(); return term; }
struct pipeline *textOutInit(char *fileName, char *compressType) /* Set up stdout to be HTTP text, file (if fileName is specified), or * compressed file (if both fileName and compressType are specified -- * see textOut.h for supported compression types). * Return NULL if no compression, otherwise a pipeline handle on which * textOutClose should be called when we're done writing stdout. */ { struct pipeline *compressPipeline = NULL; trimSpaces(fileName); if (isEmpty(fileName)) { printf("Content-Type: text/plain\n\n"); } else if (isEmpty(compressType) || sameWord(compressType, textOutCompressNone)) { printf("Content-Type: application/octet-stream\n"); printf("Content-Disposition: attachment; filename=%s\n\n", fileName); } else { char *suffix = getCompressSuffix(compressType); printf("Content-Type: application/x-%s\n", compressType); if (endsWith(fileName, suffix)) printf("Content-Disposition: attachment; filename=%s\n\n", fileName); else printf("Content-Disposition: attachment; filename=%s%s\n\n", fileName, suffix); /* Send the Content header uncompressed! */ fflush(stdout); /* Make sure no environment variables interfere with compressor. */ cleanEnvVars(compressType); /* Redirect stdout to compressor pipeline object. */ compressPipeline = pipelineOpen1(getCompressor(compressType), pipelineWrite, NULL, NULL); if (-1 == dup2(pipelineFd(compressPipeline), STDOUT_FILENO)) errnoAbort("dup2(pipelineFd %d, stdout %d) failed in textOpen()", pipelineFd(compressPipeline), STDOUT_FILENO); } pushWarnHandler(textOutWarnHandler); pushAbortHandler(textOutAbortHandler); return(compressPipeline); }
static void plProcSetup(struct plProc* proc, int stdinFd, int stdoutFd, int stderrFd) /* setup signal, error handling, and file descriptors after fork */ { int fd; struct sigaction sigAct; /* make sure abort handler exits */ pushWarnAbort(); pushAbortHandler(childAbortHandler); /* treat a closed pipe as an EOF rather than getting SIGPIPE */ ZeroVar(&sigAct); sigAct.sa_handler = SIG_IGN; if (sigaction(SIGPIPE, &sigAct, NULL) != 0) errnoAbort("failed to set SIGPIPE to SIG_IGN"); /* child, first setup stdio files */ if (stdinFd != STDIN_FILENO) { if (dup2(stdinFd, STDIN_FILENO) < 0) errnoAbort("can't dup2 to stdin"); } if (stdoutFd != STDOUT_FILENO) { if (dup2(stdoutFd, STDOUT_FILENO) < 0) errnoAbort("can't dup2 to stdout"); } if (stderrFd != STDERR_FILENO) { if (dup2(stderrFd, STDERR_FILENO) < 0) errnoAbort("can't dup2 to stderr"); } /* close other file descriptors */ for (fd = STDERR_FILENO+1; fd < 64; fd++) close(fd); }
char *checkParams(char *database, char *prefix, char *type) /* If we don't have valid CGI parameters, quit with a Bad Request HTTP response. */ { pushWarnHandler(htmlVaBadRequestAbort); pushAbortHandler(htmlVaBadRequestAbort); if(prefix == NULL || database == NULL) errAbort("%s", "Missing prefix and/or db CGI parameter"); if (! hDbIsActive(database)) errAbort("'%s' is not a valid, active database", htmlEncode(database)); if (isNotEmpty(type) && differentString(type, ALT_OR_PATCH)) errAbort("'%s' is not a valid type", type); char *table = NULL; if (! sameOk(type, ALT_OR_PATCH)) { struct sqlConnection *conn = hAllocConn(database); table = connGeneSuggestTable(conn); hFreeConn(&conn); if(table == NULL) errAbort("gene autosuggest is not supported for db '%s'", database); } popWarnHandler(); popAbortHandler(); return table; }
void pushDebugAbort() /* Push abort handler that will invoke debugger. */ { pushAbortHandler(debugAbort); }
struct pipeline *textOutInit(char *fileName, char *compressType, int *saveStdout) /* Set up stdout to be HTTP text, file (if fileName is specified), or * compressed file (if both fileName and compressType are specified -- * see textOut.h for supported compression types). * Return NULL if no compression, otherwise a pipeline handle on which * textOutClose should be called when we're done writing stdout. */ { struct pipeline *compressPipeline = NULL; // if path contains a slash, we are outputting to a local file boolean outToFile = (strchr(fileName, '/') != NULL); if (outToFile) { FILE *f; f = fopen(fileName, "w"); /* We want to capture stdout output to a file */ fflush(stdout); int tempOut = dup(STDOUT_FILENO); if (saveStdout) *saveStdout = tempOut; dup2(fileno(f),STDOUT_FILENO); /* closes STDOUT before setting it again */ fclose(f); } trimSpaces(fileName); if (isEmpty(fileName)) { printf("Content-Type: text/plain\n\n"); } else if (isEmpty(compressType) || sameWord(compressType, textOutCompressNone)) { if (!outToFile) { printf("Content-Type: application/octet-stream\n"); printf("Content-Disposition: attachment; filename=%s\n\n", fileName); } } else { if (!outToFile) { char *suffix = getCompressSuffix(compressType); printf("Content-Type: application/x-%s\n", compressType); if (endsWith(fileName, suffix)) printf("Content-Disposition: attachment; filename=%s\n\n", fileName); else printf("Content-Disposition: attachment; filename=%s%s\n\n", fileName, suffix); /* Send the Content header uncompressed! */ fflush(stdout); } /* Make sure no environment variables interfere with compressor. */ cleanEnvVars(compressType); /* Redirect stdout to compressor pipeline object. */ compressPipeline = pipelineOpen1(getCompressor(compressType), pipelineWrite, NULL, NULL); if (-1 == dup2(pipelineFd(compressPipeline), STDOUT_FILENO)) errnoAbort("dup2(pipelineFd %d, stdout %d) failed in textOpen()", pipelineFd(compressPipeline), STDOUT_FILENO); } pushWarnHandler(textOutWarnHandler); pushAbortHandler(textOutAbortHandler); return(compressPipeline); }
int main(int argc, char *argv[]) { long enteredMainTime = clock1000(); struct dyString *output = newDyString(10000); setUdcCacheDir(); cgiSpoof(&argc, argv); pushWarnHandler(htmlVaBadRequestAbort); pushAbortHandler(htmlVaBadRequestAbort); char *database = cgiString("db"); char *cmd = cgiString("cmd"); char *jsonp = cgiOptionalString("jsonp"); if (!hDbExists(database)) errAbort("Invalid database '%s'", database); if (!strcmp(cmd, "defaultPos")) { dyStringPrintf(output, "{\"pos\": \"%s\"}", hDefaultPos(database)); } else if (!strcmp(cmd, "metaDb")) { // Return list of values for given metaDb var // e.g. http://genome.ucsc.edu/hgApi?db=hg18&cmd=metaDb&var=cell struct sqlConnection *conn = hAllocConn(database); boolean metaDbExists = sqlTableExists(conn, "metaDb"); if (metaDbExists) { char *var = cgiOptionalString("var"); if (!var) errAbort("Missing var parameter"); boolean fileSearch = (cgiOptionalInt("fileSearch",0) == 1); struct slPair *pairs = mdbValLabelSearch(conn, var, MDB_VAL_STD_TRUNCATION, FALSE, !fileSearch, fileSearch); struct slPair *pair; dyStringPrintf(output, "[\n"); for (pair = pairs; pair != NULL; pair = pair->next) { if (pair != pairs) dyStringPrintf(output, ",\n"); dyStringPrintf(output, "['%s','%s']", javaScriptLiteralEncode(mdbPairLabel(pair)), javaScriptLiteralEncode(mdbPairVal(pair))); } dyStringPrintf(output, "\n]\n"); } else errAbort("Assembly does not support metaDb"); } // TODO: move to lib since hgTracks and hgApi share #define METADATA_VALUE_PREFIX "hgt_mdbVal" else if (startsWith(METADATA_VALUE_PREFIX, cmd)) { // Returns metaDb value control: drop down or free text, with or without help link. // e.g. http://genome.ucsc.edu/hgApi?db=hg18&cmd=hgt_mdbVal3&var=cell // TODO: Move guts to lib, so that hgTracks::searchTracks.c and hgApi.c can share struct sqlConnection *conn = hAllocConn(database); boolean metaDbExists = sqlTableExists(conn, "metaDb"); if (metaDbExists) { char *var = cgiOptionalString("var"); if (!var) errAbort("Missing var parameter"); int ix = atoi(cmd+strlen(METADATA_VALUE_PREFIX)); // 1 based index if (ix == 0) // errAbort("Unsupported 'cmd' parameter"); enum cvSearchable searchBy = cvSearchMethod(var); char name[128]; safef(name,sizeof name,"%s%i",METADATA_VALUE_PREFIX,ix); if (searchBy == cvSearchBySingleSelect || searchBy == cvSearchByMultiSelect) { boolean fileSearch = (cgiOptionalInt("fileSearch",0) == 1); struct slPair *pairs = mdbValLabelSearch(conn, var, MDB_VAL_STD_TRUNCATION, FALSE, !fileSearch, fileSearch); if (slCount(pairs) > 0) { char *dropDownHtml = cgiMakeSelectDropList((searchBy == cvSearchByMultiSelect), name, pairs, NULL, ANYLABEL, "mdbVal", "style='min-width: 200px; font-size: .9em;' " "onchange='findTracksMdbValChanged(this);'"); if (dropDownHtml) { dyStringAppend(output,dropDownHtml); freeMem(dropDownHtml); } slPairFreeList(&pairs); } } else if (searchBy == cvSearchByFreeText) { dyStringPrintf(output,"<input type='text' name='%s' value='' class='mdbVal freeText' " "onchange='findTracksMdbValChanged(this);' style='max-width:310px; " "width:310px; font-size:.9em;'>", name); } else if (searchBy == cvSearchByWildList) { dyStringPrintf(output,"<input type='text' name='%s' value='' class='mdbVal wildList' " "title='enter comma separated list of values' " "onchange='findTracksMdbValChanged(this);' style='max-width:310px; " "width:310px; font-size:.9em;'>", name); } else if (searchBy == cvSearchByDateRange || searchBy == cvSearchByIntegerRange) { // TO BE IMPLEMENTED } else errAbort("Metadata variable not searchable"); dyStringPrintf(output,"<span id='helpLink%i'> </span>",ix); } else errAbort("Assembly does not support metaDb"); } else if (!strcmp(cmd, "tableMetadata")) { // returns an html table with metadata for a given track char *trackName = cgiOptionalString("track"); boolean showLonglabel = (NULL != cgiOptionalString("showLonglabel")); boolean showShortLabel = (NULL != cgiOptionalString("showShortLabel")); if (trackName != NULL) { // hTrackDbForTrackAndAncestors avoids overhead of getting whole track list! struct trackDb *tdb = hTrackDbForTrackAndAncestors(database, trackName); if (tdb != NULL) { char * html = metadataAsHtmlTable(database,tdb,showLonglabel,showShortLabel); if (html) { dyStringAppend(output,html); freeMem(html); } else dyStringPrintf(output,"No metadata found for track %s.",trackName); } else dyStringPrintf(output,"Track %s not found",trackName); } else dyStringAppend(output,"No track variable found"); } else if (sameString(cmd, "codonToPos") || sameString(cmd, "exonToPos")) { char query[256]; struct sqlResult *sr; char **row; struct genePred *gp; char *name = cgiString("name"); char *table = cgiString("table"); int num = cgiInt("num"); struct sqlConnection *conn = hAllocConn(database); sqlSafef(query, sizeof(query), "select name, chrom, strand, txStart, txEnd, cdsStart, cdsEnd, exonCount, exonStarts, exonEnds from %s where name = '%s'", table, name); sr = sqlGetResult(conn, query); if ((row = sqlNextRow(sr)) != NULL) { gp = genePredLoad(row); boolean found; int start, end; if (sameString(cmd, "codonToPos")) found = codonToPos(gp, num, &start, &end); else found = exonToPos(gp, num, &start, &end); if (found) dyStringPrintf(output, "{\"pos\": \"%s:%d-%d\"}", gp->chrom, start + 1, end); else dyStringPrintf(output, "{\"error\": \"%d is an invalid %s for this gene\"}", num, sameString(cmd, "codonToPos") ? "codon" : "exon"); } else dyStringPrintf(output, "{\"error\": \"Couldn't find item: %s\"}", name); sqlFreeResult(&sr); hFreeConn(&conn); } else { warn("unknown cmd: %s",cmd); errAbort("Unsupported 'cmd' parameter"); } apiOut(dyStringContents(output), jsonp); cgiExitTime("hgApi", enteredMainTime); return 0; }
void pushRHandlers() { pushAbortHandler(R_abortHandler); pushWarnHandler(R_warnHandler); }
int main(int argc, char *argv[]) { long enteredMainTime = clock1000(); cgiSpoof(&argc, argv); char *prefix = cgiOptionalString("prefix"); char *database = cgiOptionalString("db"); initGenbankTableNames(database); int exact = cgiOptionalInt("exact", 0); struct sqlConnection *conn; char query[2048]; char **row; struct sqlResult *sr; int count = 0; boolean hasKnownCanonical; struct dyString *str = newDyString(10000); char *table; pushAbortHandler(htmlVaBadRequestAbort); if(prefix == NULL || database == NULL) errAbort("%s", "Missing prefix and/or db CGI parameter"); conn = hAllocConn(database); table = connGeneSuggestTable(conn); if(table == NULL) errAbort("gene autosuggest is not supported for db '%s'", database); popAbortHandler(); hasKnownCanonical = sameString(table, "knownCanonical"); puts("Content-Type:text/plain"); puts("\n"); dyStringPrintf(str, "[\n"); if(exact) { // NOTE that exact is no longer used by the UI as of v271, but there are still some robots using it so we still support it. if(hasKnownCanonical) sqlSafef(query, sizeof(query), "select x.geneSymbol, k.chrom, kg.txStart, kg.txEnd, x.kgID, x.description " "from knownCanonical k, knownGene kg, kgXref x where k.transcript = x.kgID and k.transcript = kg.name " "and x.geneSymbol = '%s' order by x.geneSymbol, k.chrom, kg.txEnd - kg.txStart desc", prefix); else sqlSafef(query, sizeof(query), "select r.name2, r.chrom, r.txStart, r.txEnd, r.name, d.name " "from %s r, %s g, %s d where r.name2 = '%s' and g.acc = r.name " "and g.description = d.id order by r.name2, r.chrom, r.txEnd - r.txStart desc", table, gbCdnaInfoTable, descriptionTable, prefix); } else { // We use a LIKE query b/c it uses the geneSymbol index (substr queries do not use indices in mysql). // Also note that we take advantage of the fact that searches are case-insensitive in mysql. // Unfortunately, knownCanonical sometimes has multiple entries for a given gene (e.g. 2 TTn's in mm9 knownCanonical; // 3 POU5F1's in hg19); we return all of them (#5962). if(hasKnownCanonical) sqlSafef(query, sizeof(query), "select x.geneSymbol, k.chrom, kg.txStart, kg.txEnd, x.kgID, x.description " "from knownCanonical k, knownGene kg, kgXref x where k.transcript = x.kgID and k.transcript = kg.name " "and x.geneSymbol LIKE '%s%%' order by x.geneSymbol, k.chrom, kg.txStart", prefix); else sqlSafef(query, sizeof(query), "select r.name2, r.chrom, r.txStart, r.txEnd, r.name, d.name " "from %s r, %s g, %s d where r.name2 LIKE '%s%%' and g.acc = r.name " "and g.description = d.id order by r.name2, r.chrom, r.txStart", table, gbCdnaInfoTable, descriptionTable, prefix); } sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) { // ignore funny chroms (e.g. _hap chroms. See redmine #4257. if(!strchr(row[1], '_')) { // We have some very long descriptions, e.g. 4277 chars for hg38 CLOCK, so truncate: const int maxDesc = 120; char *description = row[5]; if (strlen(description) > maxDesc + 4) strcpy(description + maxDesc, "..."); count++; dyStringPrintf(str, "%s{\"value\": \"%s (%s)\", " "\"id\": \"%s:%d-%s\", " "\"geneSymbol\": \"%s\", " "\"internalId\": \"%s\"}", count == 1 ? "" : ",\n", row[0], jsonStringEscape(description), row[1], atoi(row[2])+1, row[3], jsonStringEscape(row[0]), jsonStringEscape(row[4])); } } dyStringPrintf(str, "\n]\n"); puts(dyStringContents(str)); cgiExitTime("hgSuggest", enteredMainTime); return 0; }