url get_from_web (url name) { if (!is_rooted_web (name)) return url_none (); url res= get_cache (name); if (!is_none (res)) return res; #ifdef OS_WIN32 string urlString = as_string (name); url tmp = url_temp(); if (starts (urlString, "www.")) urlString = "http://" * urlString; else if (starts (urlString, "ftp.")) urlString = "ftp://" * urlString; else if (starts (urlString, "ftp://")) urlPath = NULL; else if (starts (urlString, "http://")) urlPath = NULL; else urlString = "http://" * urlString; urlString= web_encode (urlString); c_string urlPath (urlString); c_string tempFilePath (as_string (tmp)); if(!URL_Get(urlPath, tempFilePath)) return url_none(); else return set_cache (name, tmp); #else string test= var_eval_system ("which wget"); if (!ends (test, "wget")) return url_none (); url tmp= url_temp (); string tmp_s= escape_sh (concretize (tmp)); string cmd= "wget --header='User-Agent: TeXmacs-" TEXMACS_VERSION "' -q"; cmd << " -O " << tmp_s << " " << escape_sh (web_encode (as_string (name))); // cout << cmd << "\n"; system (cmd); // cout << "got " << name << " as " << tmp << "\n"; if (var_eval_system ("cat " * tmp_s * " 2> /dev/null") == "") { remove (tmp); return url_none (); } else return set_cache (name, tmp); #endif }
std::string encode_field(const std::string& v, const std::string& local_domain_url) { std::string r; r.reserve(v.length() << 1); for (size_t i = 0; i < v.length(); ) { if (boost::istarts_with(v.c_str() + i, "ftp://") || boost::istarts_with(v.c_str() + i, "http://") || boost::istarts_with(v.c_str() + i, "https://") || boost::istarts_with(v.c_str() + i, "mailto:")) { size_t p = i; while (p < v.length() && !isspace(v[p] & 0xff) && v[p] != '\"' && v[p] != '<' && v[p] != '>') { p++; } if (v[p - 1] == '!' || v[p - 1] == ',' || v[p - 1] == '.' || v[p - 1] == '?') p--; if (v[p - 1] == ')') p--; std::string url = web_encode(v.substr(i, p - i)); if (boost::istarts_with(v.c_str() + i, "ftp.")) r += web_link(url, "ftp://" + url, false); else if (boost::istarts_with(v.c_str() + i, "www.")) r += web_link(url, "http://" + url, false); else r += web_link(boost::istarts_with(v.c_str() + i, "mailto:") ? url.substr(7) : encode_local_url(url, local_domain_url), url, false); i = p; } else { char c = v[i++]; switch (c) { case '<': r += "<"; break; case '&': r += "&"; break; default: r += c; } } } return r; }
static std::string web_link(str_ref title, str_ref link) { return "<a href=\"" + web_encode(link) + "\">" + web_encode(title.empty() ? link : title) + "</a>"; }
static std::string web_link(const std::string& link_title, const std::string& link, bool encode) { return encode ? web_link(web_encode(link_title), web_encode(link), false) : (boost::format("<a href=\"%s\">%s</a>") % link % (link_title.empty() ? link : link_title)).str(); }
void do_url1(FILE *fp, const struct mngmsg *m_msp, const struct pstruct *ppst, char *l_name, int n1, const struct a_struct *aln_p, const char *annot_var_s, const struct annot_str *q_annot_p, const struct annot_str *l_annot_p ) { char my_q_name[200], my_l_name[200], json_l_name[200]; char *db, *bp; char pgm[10], o_pgm[10], lib[MAX_LSTR]; char *tmp_annot_s, *q_domain_s, *l_domain_s, *tmp_domain_s, *etmp_domain_s; int n_tmp_annot_s, n_tmp_domain; long q_offset, l_offset; char *ref_url, *lbp=NULL; char *srch_url, *srch_url1, *dom_url; /* set the database */ if (m_msp->ldb_info.ldnaseq==SEQT_DNA) db="nucleotide"; else db="Protein"; /* set the program type */ if (strncmp(m_msp->f_id0,"rss",3)==0) { strncpy(pgm,"fa",sizeof(pgm)); } else if (strncmp(m_msp->f_id0,"rfx",3)==0) { strncpy(pgm,"fx",sizeof(pgm)); } else { strncpy(pgm,m_msp->f_id0,sizeof(pgm)); } SAFE_STRNCPY(o_pgm, pgm, sizeof(o_pgm)); /* get a library name (probably does not work for %, + abbreviations */ if (m_msp->lname[0]!='%') { SAFE_STRNCPY(lib,m_msp->lname,sizeof(lib)); } else { SAFE_STRNCPY(lib,"%25",sizeof(lib)); SAFE_STRNCAT(lib,&m_msp->lname[1],sizeof(lib)); } lib[sizeof(lib)-1]='\0'; if ((lbp = strchr(l_name,'|'))==NULL) { lbp = l_name; } else { lbp++; } SAFE_STRNCPY(my_q_name,m_msp->qtitle,sizeof(my_q_name)); if ((bp=strchr(my_q_name,' '))!=NULL) *bp='\0'; SAFE_STRNCPY(my_l_name,lbp,sizeof(my_l_name)); if (pgm[0]=='t' || !strcmp(pgm,"fx") || !strcmp(pgm,"fy")==0 ) { if ((lbp=strchr(my_l_name,':'))!=NULL) *lbp='\0'; lbp = &my_l_name[strlen(my_l_name)-2]; if ( *lbp == '_' ) *lbp = '\0'; } /* change the program name for fastx, tfastx, tfasta */ /* fastx returns proteins */ if (strcmp(pgm,"fx")==0 || strcmp(pgm,"fy")==0) {SAFE_STRNCPY(pgm,"fa",sizeof(pgm));} else if (strcmp(pgm,"ff")==0) {SAFE_STRNCPY(pgm,"fa",sizeof(pgm));} else if (pgm[0]=='t') { SAFE_STRNCPY(pgm,"fx",sizeof(pgm)); SAFE_STRNCPY(lib,DEF_PROT_LIB,sizeof(lib)); } fflush(fp); q_offset = aln_p->q_offset; l_offset = aln_p->l_offset; /* set up ref_url, srch_url, srch_url1, dom_url */ fflush(fp); ref_url = getenv("REF_URL"); srch_url = getenv("SRCH_URL"); srch_url1 = getenv("SRCH_URL1"); dom_url = NULL; dom_url = getenv("DOMAIN_PLOT_URL"); if (ref_url || srch_url || srch_url1 || dom_url) { fprintf(fp,"<!-- LINK_START %s -->",l_name); /* REF_URL should provide */ /* "<A HREF=\"http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=%s&fcmd=Search&doptcmd1=DocSum&term=%s\">Entrez lookup</A> " */ if (ref_url != NULL) {fprintf(fp,ref_url,db,my_l_name);} /* SRCH_URL should provide */ /* "<A HREF=\"http://localhost/fasta_www2/searchfa.cgi?query=%s&db=fasta_www.cgi&lib=%s&pgm=%s&start=%ld&stop=%ld&n1=%d&o_pgm=%s\">Re-search database</A> " */ if (srch_url != NULL) { fprintf(fp,srch_url,my_l_name,db,lib,pgm, l_offset+aln_p->amin1+1,l_offset+aln_p->amax1,n1,m_msp->f_id0); } /* SRCH_URL1 should provide: */ /* "<A HREF=\"http://localhost/fasta_www2/searchxf.cgi?query=%s&db=%s&lib=%s&pgm=%s&start=%ld&stop=%ld&n1=%d&o_pgm=%s\">General re-search</A>\n" */ if (srch_url1 != NULL) { fprintf(fp,srch_url1,my_l_name,db,lib,pgm, l_offset+aln_p->amin1+1,l_offset+aln_p->amax1,n1,m_msp->f_id0); } if (dom_url!=NULL) { if (annot_var_s && annot_var_s[0]) { tmp_annot_s = web_encode(annot_var_s); } else tmp_annot_s = ""; q_domain_s = l_domain_s = NULL; if (q_annot_p && q_annot_p->n_domains > 0 && (q_domain_s = display_domains('q',q_annot_p->s_annot_arr_p, q_annot_p->n_annot))!=NULL) { } if (l_annot_p && l_annot_p->n_domains > 0 && (l_domain_s = display_domains('l',l_annot_p->s_annot_arr_p, l_annot_p->n_annot))!=NULL) { } /* combine domain strings */ n_tmp_domain = 0; if (q_domain_s) n_tmp_domain += strlen(q_domain_s)+1; if (l_domain_s) n_tmp_domain += strlen(l_domain_s)+1; etmp_domain_s = ""; if (n_tmp_domain > 0) { if ((tmp_domain_s=(char *)calloc(n_tmp_domain,sizeof(char)))==NULL) { fprintf(stderr,"*** error [%s:%d] *** cannot allocate tmp_domain_s[%d]\n", __FILE__, __LINE__,n_tmp_domain); } else { tmp_domain_s[0] = '\0'; if (q_domain_s) SAFE_STRNCAT(tmp_domain_s, q_domain_s, n_tmp_domain); if (l_domain_s) SAFE_STRNCAT(tmp_domain_s, l_domain_s, n_tmp_domain); etmp_domain_s = web_encode(tmp_domain_s); } } /* appropriate format string: */ /* pgm=%s -- program abbrev that created alignment q_name=%s -- query info q_cstart=%ld q_cstop=%ld q_astart=%ld q_astop=%ld l_name=%s -- library info l_cstart=%ld l_cstop=%ld l_astart=%ld l_astop=%ld region=%s -- aligned domain and variant information doms=%s DOMAIN_PLOT_URL = "pgm=%s;q_name=%s;q_cstart=%ld;q_cstop=%ld&q_astart=%ld&q_astop=%ld&l_name=%s&l_cstart=%ld&l_cstop=%ld&l_astart=%ld&l_astop=%ld®ions=%s&doms=%s" */ /* think about the alternative of running a script rather than embedding it */ fprintf(fp,dom_url,o_pgm, my_q_name, q_offset+seq_pos(1,aln_p->qlrev,2),q_offset+seq_pos(m_msp->n0,aln_p->qlrev,2), q_offset+seq_pos(aln_p->amin0+1,aln_p->qlrev,1), q_offset+seq_pos(aln_p->amax0, aln_p->qlrev,2), my_l_name, l_offset+seq_pos(1,aln_p->llrev,2), l_offset+seq_pos(n1,aln_p->llrev,2), l_offset+seq_pos(aln_p->amin1+1,aln_p->llrev,1),l_offset+seq_pos(aln_p->amax1,aln_p->llrev,2), tmp_annot_s, etmp_domain_s); if (n_tmp_domain>0 && tmp_domain_s) { free(tmp_domain_s); free(etmp_domain_s); } if (l_annot_p && l_annot_p->n_domains && l_domain_s) { free(l_domain_s); } if (q_annot_p && q_annot_p->n_domains && q_domain_s) { free(q_domain_s); } if (annot_var_s && annot_var_s[0] && tmp_annot_s) free(tmp_annot_s); } fprintf(fp,"\n<!-- LINK_STOP -->"); fflush(fp); } /* if ((srch_url2 = getenv("SRCH_URL2"))==NULL) fprintf(fp,"<A HREF=\"http://fasta.bioch.virginia.edu/fasta/cgi/lalignx.cgi?seq1=\"%s\"&in_seq1=\"FASTA\"&seq2=\"%s\"&in_seq2=\"Accession\"&ssr2=%ld:%ld\">lalign</A>\n<p>\n",my_l_name,db,lib,pgm,l_offset+aln_p->amin1+1,l_offset+aln_p->amax1,n1); else fprintf(fp,srch_url1,my_l_name,db,lib,pgm, l_offset+aln_p->amin1+1,l_offset+aln_p->amax1,n1); */ if (getenv("JSON_HTML")) { /* replace '|' with '_' */ SAFE_STRNCPY(json_l_name, l_name, sizeof(json_l_name)); for (bp=strchr(json_l_name,'|'); bp; bp=strchr(bp+1,'|')) { *bp = '_'; } /* replace '.' with '_' */ for (bp=strchr(json_l_name,'.'); bp; bp=strchr(bp+1,'.')) { *bp = '_'; } fprintf(fp,"\n<script type=\"text/javascript\">\n//<![CDATA[\n var json_%s = {\n",json_l_name); encode_json_str(fp, "db", db, 1); encode_json_str(fp, "l_acc", l_name, 0); encode_json_str(fp, "acc", my_l_name, 0); encode_json_str(fp, "lib", lib, 0); encode_json_str(fp, "pgm", pgm, 0); encode_json_str(fp, "o_pgm", m_msp->f_id0, 0); encode_json_aln(fp, aln_p, q_offset, l_offset, 0); if (annot_var_s && annot_var_s[0]) { encode_json_lines(fp, "annot", annot_var_s, 0); } if (q_annot_p && q_annot_p->n_domains > 0) { encode_json_domains(fp, "q_domains", q_annot_p, 0); } if (l_annot_p && l_annot_p->n_domains > 0) { encode_json_domains(fp, "l_domains", l_annot_p, 0); } fprintf(fp, "\n}\n//]]>\n</script>"); fflush(fp); } }