b12b97dacb96fe2ca6e0c5e1fc275ae70b3e3614 kent Fri Feb 13 12:19:12 2015 -0800 Refactored part of menuBar function into menuBarAddUiVars for reuse with another menu bar. diff --git src/hg/lib/web.c src/hg/lib/web.c index af1773a..1040c8d 100644 --- src/hg/lib/web.c +++ src/hg/lib/web.c @@ -1231,90 +1231,103 @@ } // overrides for default context specific help link. char *contextSpecificHelpLink = NULL; char *contextSpecificHelpLabel = NULL; void setContextSpecificHelp(char *link, char *label) // Override default behavior for the context specific help link { if(link) contextSpecificHelpLink = cloneString(link); if(label) contextSpecificHelpLabel = cloneString(label); } +char *menuBarAddUiVars(char *oldString, char *cgiPrefix, char *uiVars) +/* Look for CGI program calls in oldString, and add session vars hgsid to them */ +{ +int len = strlen(oldString); +char buf[4096]; + +/* Create a regular expression and compile it */ +regex_t re; +regmatch_t match[2]; +safef(buf, sizeof(buf), "%s[A-Za-z]+(%c%c?)", cgiPrefix, '\\', '?'); +int err = regcomp(&re, buf, REG_EXTENDED); +if(err) + errAbort("regcomp failed; err: %d", err); + +/* Search through oldString with regex, and build up new string in dy */ +struct dyString *dy = newDyString(0); +int offset; +for(offset = 0; offset < len && !regexec(&re, oldString + offset, ArraySize(match), match, 0); + offset += match[0].rm_eo) + { + dyStringAppendN(dy, oldString + offset, match[0].rm_eo); + if(match[1].rm_so == match[1].rm_eo) + dyStringAppend(dy, "?"); + dyStringAppend(dy, uiVars); + if(match[1].rm_so != match[1].rm_eo) + dyStringAppend(dy, "&"); + } +if(offset < len) + dyStringAppend(dy, oldString + offset); +return dyStringCannibalize(&dy); +} + char *menuBar(struct cart *cart, char *db) // Return HTML for the menu bar (read from a configuration file); // we fixup internal CGI's to add hgsid's and include the appropriate js and css files. // // Note this function is also called by hgTracks which extends the menu bar // with a View menu defined in hgTracks/menu.c { char *docRoot = hDocumentRoot(); char *menuStr, buf[4096], uiVars[128]; FILE *fd; -int len, offset, err; char *navBarFile = "inc/globalNavBar.inc"; struct stat statBuf; -regex_t re; -regmatch_t match[2]; char *scriptName = cgiScriptName(); if (cart) safef(uiVars, sizeof(uiVars), "%s=%s", cartSessionVarName(), cartSessionId(cart)); else uiVars[0] = 0; if(docRoot == NULL) // tolerate missing docRoot (i.e. don't bother with menu when running from command line) return NULL; jsIncludeFile("jquery.js", NULL); jsIncludeFile("jquery.plugins.js", NULL); webIncludeResourceFile("nice_menu.css"); // Read in menu bar html safef(buf, sizeof(buf), "%s/%s", docRoot, navBarFile); fd = mustOpen(buf, "r"); fstat(fileno(fd), &statBuf); -len = statBuf.st_size; +int len = statBuf.st_size; menuStr = needMem(len + 1); mustRead(fd, menuStr, statBuf.st_size); menuStr[len] = 0; carefulClose(&fd); if (cart) { - // fixup internal CGIs to have hgsid - safef(buf, sizeof(buf), "/cgi-bin/hg[A-Za-z]+(%c%c?)", '\\', '?'); - err = regcomp(&re, buf, REG_EXTENDED); - if(err) - errAbort("regcomp failed; err: %d", err); - struct dyString *dy = newDyString(0); - for(offset = 0; offset < len && !regexec(&re, menuStr + offset, ArraySize(match), match, 0); offset += match[0].rm_eo) - { - dyStringAppendN(dy, menuStr + offset, match[0].rm_eo); - if(match[1].rm_so == match[1].rm_eo) - dyStringAppend(dy, "?"); - dyStringAppend(dy, uiVars); - if(match[1].rm_so != match[1].rm_eo) - dyStringAppend(dy, "&"); - } - if(offset < len) - dyStringAppend(dy, menuStr + offset); + char *newMenuStr = menuBarAddUiVars(menuStr, "/cgi-bin/hg", uiVars); freez(&menuStr); - menuStr = dyStringCannibalize(&dy); + menuStr = newMenuStr; } if(scriptName) { // Provide hgTables options for some CGIs. char hgTablesOptions[1024] = ""; char *track = (cart == NULL ? NULL : (endsWith(scriptName, "hgGene") ? cartOptionalString(cart, "hgg_type") : cartOptionalString(cart, "g"))); if (track && cart && db && (endsWith(scriptName, "hgc") || endsWith(scriptName, "hgTrackUi") || endsWith(scriptName, "hgGene"))) { struct trackDb *tdb = hTrackDbForTrack(db, track);