Back to index

texmacs  1.0.7.15
init_texmacs.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : init_texmacs.cpp
00004 * DESCRIPTION: Initialization of TeXmacs
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 "boot.hpp"
00013 #include "file.hpp"
00014 #include "sys_utils.hpp"
00015 #include "analyze.hpp"
00016 #include "convert.hpp"
00017 #include "merge_sort.hpp"
00018 #include "drd_std.hpp"
00019 #include "language.hpp"
00020 
00021 tree texmacs_settings = tuple ();
00022 int  install_status   = 0;
00023 bool use_which        = false;
00024 bool use_locate       = false;
00025 
00026 extern void setup_tex (); // from Plugins/Metafont/tex_init.cpp
00027 extern void init_tex  (); // from Plugins/Metafont/tex_init.cpp
00028 
00029 /******************************************************************************
00030 * Subroutines for paths
00031 ******************************************************************************/
00032 
00033 static url
00034 get_env_path (string which) {
00035   return url ("$" * which);
00036 }
00037 
00038 static void
00039 set_env_path (string which, url val) {
00040   //cout << which << " := " << val << "\n";
00041   if ((!is_none (val)) && (val->t != ""))
00042     set_env (which, as_string (val));
00043 }
00044 
00045 static url
00046 get_env_path (string which, url def) {
00047   url val= get_env_path (which);
00048   if (is_none (val) || (val->t == "")) {
00049     set_env_path (which, def);
00050     return def;
00051   }
00052   return val;
00053 }
00054 
00055 static url
00056 plugin_path (string which) {
00057   url base= "$TEXMACS_HOME_PATH:/etc/TeXmacs:$TEXMACS_PATH:/usr/share/TeXmacs";
00058   url search= base * "plugins" * url_wildcard ("*") * which;
00059   return expand (complete (search, "r"));
00060 }
00061 
00062 scheme_tree
00063 plugin_list () {
00064   bool flag;
00065   array<string> a= read_directory ("$TEXMACS_PATH/plugins", flag);
00066   a << read_directory ("/etc/TeXmacs/plugins", flag);
00067   a << read_directory ("$TEXMACS_HOME_PATH/plugins", flag);
00068   a << read_directory ("/usr/share/TeXmacs/plugins", flag);
00069   merge_sort (a);
00070   int i, n= N(a);
00071   tree t (TUPLE);
00072   for (i=0; i<n; i++)
00073     if ((a[i] != ".") && (a[i] != "..") && ((i==0) || (a[i] != a[i-1])))
00074       t << a[i];
00075   return t;
00076 }
00077 
00078 /******************************************************************************
00079 * Initialize main paths
00080 ******************************************************************************/
00081 
00082 static void
00083 init_main_paths () {
00084 #ifdef __MINGW32__
00085   if (is_none (get_env_path ("TEXMACS_HOME_PATH", get_env ("APPDATA") * "/TeXmacs"))) {
00086 #else
00087   if (is_none (get_env_path ("TEXMACS_HOME_PATH", "~/.TeXmacs"))) {
00088 #endif
00089     cerr << "\nTeXmacs]\n";
00090     cerr << "TeXmacs] Installation problem: please send a bug report.\n";
00091     cerr << "TeXmacs] 'TEXMACS_HOME_PATH' could not be set to '~/.TeXmacs'.\n";
00092     cerr << "TeXmacs] You may try to set this environment variable manually\n";
00093     cerr << "TeXmacs]\n";
00094     FAILED ("installation problem");
00095     exit (1);
00096   }
00097 }
00098 
00099 /******************************************************************************
00100 * Make user directories
00101 ******************************************************************************/
00102 
00103 static void
00104 make_dir (url which) {
00105   if (!is_directory (which))
00106     mkdir (which);
00107 }
00108 
00109 static void
00110 init_user_dirs () {
00111   make_dir ("$TEXMACS_HOME_PATH");
00112   make_dir ("$TEXMACS_HOME_PATH/bin");
00113   make_dir ("$TEXMACS_HOME_PATH/doc");
00114   make_dir ("$TEXMACS_HOME_PATH/doc/about");
00115   make_dir ("$TEXMACS_HOME_PATH/doc/about/changes");
00116   make_dir ("$TEXMACS_HOME_PATH/fonts");
00117   make_dir ("$TEXMACS_HOME_PATH/fonts/enc");
00118   make_dir ("$TEXMACS_HOME_PATH/fonts/error");
00119   make_dir ("$TEXMACS_HOME_PATH/fonts/pk");
00120   make_dir ("$TEXMACS_HOME_PATH/fonts/tfm");
00121   make_dir ("$TEXMACS_HOME_PATH/fonts/truetype");
00122   make_dir ("$TEXMACS_HOME_PATH/fonts/type1");
00123   make_dir ("$TEXMACS_HOME_PATH/fonts/virtual");
00124   make_dir ("$TEXMACS_HOME_PATH/langs");
00125   make_dir ("$TEXMACS_HOME_PATH/langs/mathematical");
00126   make_dir ("$TEXMACS_HOME_PATH/langs/mathematical/syntax");
00127   make_dir ("$TEXMACS_HOME_PATH/langs/natural");
00128   make_dir ("$TEXMACS_HOME_PATH/langs/natural/dic");
00129   make_dir ("$TEXMACS_HOME_PATH/langs/natural/hyphen");
00130   make_dir ("$TEXMACS_HOME_PATH/langs/programming");
00131   make_dir ("$TEXMACS_HOME_PATH/misc");
00132   make_dir ("$TEXMACS_HOME_PATH/misc/patterns");
00133   make_dir ("$TEXMACS_HOME_PATH/misc/pixmaps");
00134   make_dir ("$TEXMACS_HOME_PATH/packages");
00135   make_dir ("$TEXMACS_HOME_PATH/plugins");
00136   make_dir ("$TEXMACS_HOME_PATH/progs");
00137   make_dir ("$TEXMACS_HOME_PATH/styles");
00138   make_dir ("$TEXMACS_HOME_PATH/system");
00139   make_dir ("$TEXMACS_HOME_PATH/system/bib");
00140   make_dir ("$TEXMACS_HOME_PATH/system/cache");
00141   make_dir ("$TEXMACS_HOME_PATH/system/tmp");
00142   make_dir ("$TEXMACS_HOME_PATH/texts");
00143   change_mode ("$TEXMACS_HOME_PATH/system", 7 << 6);
00144   remove (url ("$TEXMACS_HOME_PATH/system/tmp") * url_wildcard ("*"));
00145 }
00146 
00147 /******************************************************************************
00148 * Detection of guile
00149 ******************************************************************************/
00150 
00151 static void
00152 init_guile () {
00153   url guile_path= "$TEXMACS_PATH/progs:$GUILE_LOAD_PATH";
00154   if (!exists (guile_path * "init-texmacs.scm")) {
00155     cerr << "\nTeXmacs]\n";
00156     cerr << "TeXmacs] Installation problem: please send a bug report.\n";
00157     cerr << "TeXmacs] The initialization file init-texmacs.scm"
00158         << " could not be found.\n";
00159     cerr << "TeXmacs] Please check the values of the environment variables\n";
00160     cerr << "TeXmacs] TEXMACS_PATH and GUILE_LOAD_PATH."
00161         << " init-texmacs.scm should\n";
00162     cerr << "TeXmacs] be readable and in the directory $TEXMACS_PATH/progs\n";
00163     cerr << "TeXmacs] or in the directory $GUILE_LOAD_PATH\n";
00164     cerr << "TeXmacs]\n";
00165     FAILED ("guile could not be found");
00166   }
00167 
00168   /*
00169   if (!exists ("$GUILE_LOAD_PATH/ice-9/boot-9.scm")) {
00170     int i;
00171     string guile_data    = var_eval_system ("guile-config info datadir");
00172     string guile_version = var_eval_system ("guile --version");
00173     for (i=0; i<N(guile_version); i++)
00174       if (guile_version[i] == '\n') break;
00175     guile_version= guile_version (0, i);
00176     for (i=N(guile_version); i>0; i--)
00177       if (guile_version[i-1] == ' ') break;
00178     guile_version= guile_version (i, N (guile_version));
00179     if (guile_version == "") {
00180       var_eval_system ("guile-config info top_srcdir");
00181       for (i=N(guile_version); i>0; i--)
00182        if (guile_version[i-1] == '-') break;
00183       guile_version= guile_version (i, N (guile_version));
00184       for (i=0; i<N(guile_version); i++)
00185        if ((guile_version[i] == '/') || (guile_version[i] == '\\')) {
00186          guile_version= guile_version (0, i);
00187          break;
00188        }
00189     }
00190     url guile_dir= url_system (guile_data) * url ("guile", guile_version);
00191     guile_path= guile_path | guile_dir;
00192     set_env_path ("GUILE_LOAD_PATH", guile_path);
00193     if (!exists ("$GUILE_LOAD_PATH/ice-9/boot-9.scm")) {
00194       cerr << "\nGUILE_LOAD_PATH=" << guile_path << "\n";
00195       FAILED ("guile seems not to be installed on your system");
00196     }
00197   }
00198   */
00199 
00200   guile_path= guile_path | "$TEXMACS_HOME_PATH/progs" | plugin_path ("progs");
00201   set_env_path ("GUILE_LOAD_PATH", guile_path);
00202 }
00203 
00204 /******************************************************************************
00205 * Set additional environment variables
00206 ******************************************************************************/
00207 
00208 static void
00209 init_env_vars () {
00210   // Handle binary, library and guile paths for plugins
00211   url bin_path= get_env_path ("PATH") | plugin_path ("bin");
00212   set_env_path ("PATH", bin_path);
00213   url lib_path= get_env_path ("LD_LIBRARY_PATH") | plugin_path ("lib");
00214   set_env_path ("LD_LIBRARY_PATH", lib_path);
00215 
00216   // Get TeXmacs style and package paths
00217   url style_root=
00218     get_env_path ("TEXMACS_STYLE_ROOT",
00219                 "$TEXMACS_HOME_PATH/styles:$TEXMACS_PATH/styles" |
00220                 plugin_path ("styles"));
00221   url package_root=
00222     get_env_path ("TEXMACS_PACKAGE_ROOT",
00223                 "$TEXMACS_HOME_PATH/packages:$TEXMACS_PATH/packages" |
00224                 plugin_path ("packages"));
00225   url all_root= style_root | package_root;
00226   url style_path=
00227     get_env_path ("TEXMACS_STYLE_PATH",
00228                 expand (complete (all_root * url_wildcard (), "dr")));
00229   url text_root=
00230     get_env_path ("TEXMACS_TEXT_ROOT",
00231                 "$TEXMACS_HOME_PATH/texts:$TEXMACS_PATH/texts" |
00232                 plugin_path ("texts"));
00233   url text_path=
00234     get_env_path ("TEXMACS_TEXT_PATH",
00235                 expand (complete (text_root * url_wildcard (), "dr")));
00236 
00237   // Get other data paths
00238   (void) get_env_path ("TEXMACS_FILE_PATH",text_path | style_path);
00239   (void) set_env_path ("TEXMACS_DOC_PATH",
00240                      get_env_path ("TEXMACS_DOC_PATH") |
00241                      "$TEXMACS_HOME_PATH/doc:$TEXMACS_PATH/doc" |
00242                      plugin_path ("doc"));
00243   (void) set_env_path ("TEXMACS_SECURE_PATH",
00244                      get_env_path ("TEXMACS_SECURE_PATH") |
00245                      "$TEXMACS_PATH:$TEXMACS_HOME_PATH");
00246   (void) get_env_path ("TEXMACS_PATTERN_PATH",
00247                      "$TEXMACS_HOME_PATH/misc/patterns" |
00248                      url ("$TEXMACS_PATH/misc/patterns") |
00249                      plugin_path ("misc/patterns"));
00250   (void) get_env_path ("TEXMACS_PIXMAP_PATH",
00251                      "$TEXMACS_HOME_PATH/misc/pixmaps" |
00252                      url ("$TEXMACS_PATH/misc/pixmaps/modern/24x24/main") |
00253                      url ("$TEXMACS_PATH/misc/pixmaps/modern/20x20/mode") |
00254                      url ("$TEXMACS_PATH/misc/pixmaps/modern/16x16/focus") |
00255                      url ("$TEXMACS_PATH/misc/pixmaps/traditional/--x17") |
00256                      plugin_path ("misc/pixmaps"));
00257   (void) get_env_path ("TEXMACS_DIC_PATH",
00258                      "$TEXMACS_HOME_PATH/langs/natural/dic" |
00259                      url ("$TEXMACS_PATH/langs/natural/dic") |
00260                      plugin_path ("langs/natural/dic"));
00261 #ifdef OS_WIN32
00262   set_env ("TEXMACS_SOURCE_PATH", "");
00263 #else
00264   set_env ("TEXMACS_SOURCE_PATH", TEXMACS_SOURCES);
00265 #endif
00266 }
00267 
00268 /******************************************************************************
00269 * Miscellaneous initializations
00270 ******************************************************************************/
00271 
00272 static void
00273 init_misc () {
00274   // Test whether 'which' works
00275 #if defined(__MINGW__) || defined(__MINGW32__) || defined (OS_WIN32)
00276   use_which = false;
00277 #else
00278   use_which = (var_eval_system ("which texmacs 2> /dev/null") != "");
00279 #endif
00280   //string loc= var_eval_system ("locate bin/locate 2> /dev/null");
00281   //use_locate= (search_forwards ("bin/locate", loc) > 0);
00282 
00283   // Set extra environment variables for Cygwin
00284 #ifdef OS_CYGWIN
00285   set_env ("CYGWIN", "check_case:strict");
00286   set_env ("COMSPEC", "");
00287   set_env ("ComSpec", "");
00288 #endif
00289 }
00290 
00291 /******************************************************************************
00292 * Deprecated initializations
00293 ******************************************************************************/
00294 
00295 static void
00296 init_deprecated () {
00297 #ifndef OS_WIN32
00298   // Check for Macaulay 2
00299   if (get_env ("M2HOME") == "")
00300     if (exists_in_path ("M2")) {
00301       string where= concretize (resolve_in_path ("M2"));
00302       string s    = var_eval_system ("grep 'M2HOME=' " * where);
00303       string dir  = s (search_forwards ("=", s) + 1, N(s));
00304       if (dir != "") set_env ("M2HOME", dir);
00305     }
00306 #endif
00307 }
00308 
00309 /******************************************************************************
00310 * Subroutines for the TeXmacs settings
00311 ******************************************************************************/
00312 
00313 string
00314 get_setting (string var, string def) {
00315   int i, n= N (texmacs_settings);
00316   for (i=0; i<n; i++)
00317     if (is_tuple (texmacs_settings[i], var, 1)) {
00318       return scm_unquote (as_string (texmacs_settings[i][1]));
00319     }
00320   return def;
00321 }
00322 
00323 void
00324 set_setting (string var, string val) {
00325   int i, n= N (texmacs_settings);
00326   for (i=0; i<n; i++)
00327     if (is_tuple (texmacs_settings[i], var, 1)) {
00328       texmacs_settings[i][1]= scm_quote (val);
00329       return;
00330     }
00331   texmacs_settings << tuple (var, scm_quote (val));
00332 }
00333 
00334 /******************************************************************************
00335 * First installation
00336 ******************************************************************************/
00337 
00338 void
00339 setup_texmacs () {
00340   url settings_file= "$TEXMACS_HOME_PATH/system/settings.scm";
00341   cerr << "Welcome to TeXmacs " TEXMACS_VERSION "\n";
00342   cerr << HRULE;
00343 
00344   set_setting ("VERSION", TEXMACS_VERSION);
00345   setup_tex ();
00346   
00347   string s= scheme_tree_to_block (texmacs_settings);
00348   //cout << "settings_t= " << texmacs_settings << "\n";
00349   //cout << "settings_s= " << s << "\n";
00350   if (save_string (settings_file, s) || load_string (settings_file, s, false)) {
00351     cerr << HRULE;
00352     cerr << "I could not save or reload the file\n\n";
00353     cerr << "\t" << settings_file << "\n\n";
00354     cerr << "Please give me full access control over this file and\n";
00355     cerr << "rerun 'TeXmacs'.\n";
00356     cerr << HRULE;
00357     FAILED ("unable to write settings");
00358   }
00359   
00360   cerr << HRULE;
00361   cerr << "Installation completed successfully !\n";
00362   cerr << "I will now start up the editor\n";
00363   cerr << HRULE;
00364 }
00365 
00366 /******************************************************************************
00367 * Initialization of TeXmacs
00368 ******************************************************************************/
00369 
00370 void
00371 init_texmacs () {
00372   init_succession_status_table ();
00373   init_std_drd ();
00374   init_main_paths ();
00375   init_user_dirs ();
00376   init_guile ();
00377   init_env_vars ();
00378   init_misc ();
00379   init_deprecated ();
00380 }
00381 
00382 /******************************************************************************
00383 * Initialization of built-in plug-ins
00384 ******************************************************************************/
00385 
00386 void
00387 init_plugins () {
00388   install_status= 0;
00389   url old_settings= "$TEXMACS_HOME_PATH/system/TEX_PATHS";
00390   url new_settings= "$TEXMACS_HOME_PATH/system/settings.scm";
00391   string s;
00392   if (load_string (new_settings, s, false)) {
00393     if (load_string (old_settings, s, false)) {
00394       setup_texmacs ();
00395       install_status= 1;
00396     }
00397     else get_old_settings (s);
00398   }
00399   else texmacs_settings= block_to_scheme_tree (s);
00400   if (get_setting ("VERSION") != TEXMACS_VERSION) {
00401     init_upgrade ();
00402     url ch ("$TEXMACS_HOME_PATH/doc/about/changes/changes-recent.en.tm");
00403     install_status= exists (ch)? 2: 0;
00404   }
00405   init_tex ();
00406 }