Back to index

texmacs  1.0.7.15
web_files.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : web_files.cpp
00004 * DESCRIPTION: file handling via the web
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "file.hpp"
00013 #include "web_files.hpp"
00014 #include "sys_utils.hpp"
00015 #include "analyze.hpp"
00016 #include "hashmap.hpp"
00017 #include "scheme.hpp"
00018 
00019 #ifdef OS_WIN32
00020 #include <urlget.h>
00021 #endif
00022 
00023 #define MAX_CACHED 25
00024 static int web_nr=0;
00025 static array<tree> web_cache (MAX_CACHED);
00026 static hashmap<tree,tree> web_cache_resolve ("");
00027 
00028 /******************************************************************************
00029 * Caching
00030 ******************************************************************************/
00031 
00032 static url
00033 get_cache (url name) {
00034   if (web_cache_resolve->contains (name->t)) {
00035     int i, j;
00036     tree tmp= web_cache_resolve [name->t];
00037     for (i=0; i<MAX_CACHED; i++)
00038       if (web_cache[i] == name->t) {
00039        // cout << name << " in cache as " << tmp << " at " << i << "\n";
00040        for (j=i; ((j+1) % MAX_CACHED) != web_nr; j= (j+1) % MAX_CACHED)
00041          web_cache[j]= web_cache[(j+1) % MAX_CACHED];
00042        web_cache[j]= name->t;
00043        break;
00044       }
00045     return as_url (tmp); // url_system (tmp);
00046   }
00047   return url_none ();
00048 }
00049 
00050 static url
00051 set_cache (url name, url tmp) {
00052   web_cache_resolve->reset (web_cache [web_nr]);
00053   web_cache [web_nr]= name->t;
00054   web_cache_resolve (name->t)= tmp->t;
00055   web_nr= (web_nr+1) % MAX_CACHED;
00056   return tmp;
00057 }
00058 
00059 void
00060 web_cache_invalidate (url name) {
00061   for (int i=0; i<MAX_CACHED; i++)
00062     if (web_cache[i] == name->t) {
00063       web_cache[i]= tree ("");
00064       web_cache_resolve->reset (name->t);
00065     }
00066 }
00067 
00068 /******************************************************************************
00069 * Web files
00070 ******************************************************************************/
00071 
00072 static string
00073 web_encode (string s) {
00074   return tm_decode (s);
00075 }
00076 
00077 url
00078 get_from_web (url name) {
00079   if (!is_rooted_web (name)) return url_none ();
00080   url res= get_cache (name);
00081   if (!is_none (res)) return res;
00082 
00083 #ifdef OS_WIN32
00084   char *urlPath;
00085   char *tempFilePath;
00086   string urlString = as_string (name);
00087   url tmp = url_temp();
00088        
00089   if (starts (urlString, "www."))
00090     urlString = "http://" * urlString;
00091   else if (starts (urlString, "ftp."))
00092     urlString = "ftp://" * urlString;
00093   else if (starts (urlString, "ftp://"))
00094     urlPath = NULL;
00095   else if (starts (urlString, "http://"))
00096     urlPath = NULL;
00097   else
00098     urlString = "http://" * urlString;
00099   urlString= web_encode (urlString);
00100 
00101   urlPath = as_charp(urlString);
00102   tempFilePath = as_charp(as_string(tmp));
00103 
00104   if(!URL_Get(urlPath, tempFilePath)){
00105     tm_delete_array (urlPath);
00106     tm_delete_array (tempFilePath);
00107     return url_none();
00108   }
00109 
00110   else return set_cache (name, tmp);
00111 #else
00112   string test= var_eval_system ("which wget");
00113   if (!ends (test, "wget")) return url_none ();
00114   url tmp= url_temp ();
00115   string tmp_s= escape_sh (concretize (tmp));
00116   string cmd= "wget --header='User-Agent: TeXmacs-" TEXMACS_VERSION "' -q";
00117   cmd << " -O " << tmp_s << " " << escape_sh (web_encode (as_string (name)));
00118   // cout << cmd << "\n";
00119   system (cmd);
00120   // cout << "got " << name << " as " << tmp << "\n";
00121 
00122   if (var_eval_system ("cat " * tmp_s * " 2> /dev/null") == "") {
00123     remove (tmp);
00124     return url_none ();
00125   }
00126   else return set_cache (name, tmp);
00127 #endif
00128 }
00129 
00130 /******************************************************************************
00131 * Files from a hyperlink file system
00132 ******************************************************************************/
00133 
00134 url
00135 get_from_server (url u) {
00136   if (!is_rooted_tmfs (u)) return url_none ();
00137   url res= get_cache (u);
00138   if (!is_none (res)) return res;
00139 
00140   string name= as_string (u);
00141   if (ends (name, "~") || ends (name, "#")) return url_none ();
00142   string r= as_string (call ("tmfs-load", object (name)));
00143   if (r == "") return url_none ();
00144   url tmp= url_temp ();
00145   (void) save_string (tmp, r, true);
00146 
00147   return set_cache (u, tmp);
00148 }
00149 
00150 bool
00151 save_to_server (url u, string s) {
00152   if (!is_rooted_tmfs (u)) return true;
00153   string name= as_string (u);
00154   (void) call ("tmfs-save", object (name), object (s));
00155   return false;
00156 }
00157 
00158 /******************************************************************************
00159 * Ramdisc
00160 ******************************************************************************/
00161 
00162 url
00163 get_from_ramdisc (url u) {
00164   if (!is_ramdisc (u)) return url_none ();
00165   url res= get_cache (u);
00166   if (!is_none (res)) return (res);
00167   url tmp= url_temp (string (".") * suffix (u));
00168   save_string (tmp, u[1][2]->t->label);
00169   return set_cache (u, tmp);
00170 }