bool is_rooted_web (url u) { return is_root_web (u) || (is_concat (u) && is_rooted_web (u[1])) || (is_or (u) && is_rooted_web (u[1]) && is_rooted_web (u[2])); }
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 }
string concretize (url u) { // This routine transforms a resolved url into a system file name. // In the case of distant files from the web, a local copy is created. if (is_rooted (u, "default") || is_rooted (u, "file") || is_rooted (u, "blank")) return as_string (reroot (u, "default")); if (is_rooted_web (u)) return concretize (get_from_web (u)); if (is_rooted_tmfs (u)) return concretize (get_from_server (u)); if (is_ramdisc (u)) return concretize (get_from_ramdisc (u)); if (is_here (u)) return as_string (url_pwd ()); if (is_parent (u)) return as_string (url_pwd () * url_parent ()); if (is_wildcard (u, 1)) return u->t[1]->label; std_warning << "Couldn't concretize " << u->t << LF; // failed_error << "u= " << u << LF; // FAILED ("url has no root"); return "xxx"; }
url complete (url base, url u, string filter, bool flag) { // cout << "complete " << base << " |||| " << u << LF; if (!is_rooted(u)) { if (is_none (base)) return base; if (is_none (u)) return u; if ((!is_root (base)) && (!is_rooted_name (base))) { failed_error << "base= " << base << LF; FAILED ("invalid base url"); } } if (is_name (u) || (is_concat (u) && is_root (u[1]) && is_name (u[2]))) { url comp= base * u; if (is_rooted (comp, "default") || is_rooted (comp, "file")) { if (is_of_type (comp, filter)) return reroot (u, "default"); return url_none (); } if (is_rooted_web (comp) || is_rooted_tmfs (comp) || is_ramdisc (comp)) { if (is_of_type (comp, filter)) return u; return url_none (); } failed_error << "base= " << base << LF; failed_error << "u= " << u << LF; ASSERT (is_rooted (comp), "unrooted url"); FAILED ("bad protocol in url"); } if (is_root (u)) { // FIXME: test filter flags here return u; } if (is_concat (u) && is_wildcard (u[1], 0) && is_wildcard (u[2], 1)) { // FIXME: ret= ret | ... is unefficient (quadratic) in main loop if (!(is_rooted (base, "default") || is_rooted (base, "file"))) { failed_error << "base= " << base << LF; FAILED ("wildcards only implemented for files"); } url ret= url_none (); bool error_flag; array<string> dir= read_directory (base, error_flag); int i, n= N(dir); for (i=0; i<n; i++) { if ((!is_none (ret)) && flag) return ret; if ((dir[i] == ".") || (dir[i] == "..")) continue; if (starts (dir[i], "http://") || starts (dir[i], "ftp://")) if (is_directory (base * dir[i])) continue; ret= ret | (dir[i] * complete (base * dir[i], u, filter, flag)); if (match_wildcard (dir[i], u[2][1]->t->label)) ret= ret | complete (base, dir[i], filter, flag); } return ret; } if (is_concat (u)) { url sub= complete (base, u[1], "", false); // "" should often be faster than the more correct "d" here return complete (base, sub, u[2], filter, flag); } if (is_or (u)) { url res1= complete (base, u[1], filter, flag); if ((!is_none (res1)) && flag) return res1; return res1 | complete (base, u[2], filter, flag); } if (is_wildcard (u)) { // FIXME: ret= ret | ... is unefficient (quadratic) in main loop if (!(is_rooted (base, "default") || is_rooted (base, "file"))) { failed_error << "base= " << base << LF; FAILED ("wildcards only implemented for files"); } url ret= url_none (); if (is_wildcard (u, 0) && is_of_type (base, filter)) ret= url_here (); bool error_flag; array<string> dir= read_directory (base, error_flag); int i, n= N(dir); for (i=0; i<n; i++) { if ((!is_none (ret)) && flag) return ret; if ((dir[i] == ".") || (dir[i] == "..")) continue; if (starts (dir[i], "http://") || starts (dir[i], "ftp://")) if (is_directory (base * dir[i])) continue; if (is_wildcard (u, 0)) ret= ret | (dir[i] * complete (base * dir[i], u, filter, flag)); else if (match_wildcard (dir[i], u[1]->t->label)) ret= ret | complete (base, dir[i], filter, flag); } return ret; } failed_error << "url= " << u << LF; FAILED ("bad url"); return u; }