GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
octave-config.in.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2008-2013 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 #include <config.h>
25 #endif
26 
27 #include <string>
28 #include <map>
29 #include <iostream>
30 #include <algorithm>
31 #include <cstdlib>
32 
33 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
34 #include <windows.h>
35 #endif
36 
37 static bool initialized = false;
38 static std::map<std::string,std::string> vars;
39 static std::string OCTAVE_HOME, PREFIX;
40 static std::string usage_msg = "usage: octave-config [options]";
41 static std::string help_msg =
42 "\n"
43 "Options:\n"
44 "\n"
45 " -h, -?, --help Print this message.\n"
46 "\n"
47 " --m-site-dir Print the name of the directory where Octave\n"
48 " expects to find locally installed .m files.\n"
49 "\n"
50 " --oct-site-dir Print the name of the directory where Octave\n"
51 " expects to find locally installed .oct files.\n"
52 "\n"
53 " -p VAR, --print VAR Print the value of the given configuration\n"
54 " variable VAR. Recognized variables are:\n"
55 "\n"
56 " API_VERSION LOCALAPIARCHLIBDIR\n"
57 " CANONICAL_HOST_TYPE LOCALAPIFCNFILEDIR\n"
58 " DEFAULT_PAGER LOCALAPIOCTFILEDIR\n"
59 " ARCHLIBDIR LOCALARCHLIBDIR\n"
60 " BINDIR LOCALFCNFILEDIR\n"
61 " DATADIR LOCALOCTFILEDIR\n"
62 " DATAROOTDIR LOCALSTARTUPFILEDIR\n"
63 " EXEC_PREFIX LOCALVERARCHLIBDIR\n"
64 " FCNFILEDIR LOCALVERFCNFILEDIR\n"
65 " LOCALVEROCTFILEDIR MAN1DIR\n"
66 " IMAGEDIR MANDIR\n"
67 " MAN1EXT OCTFILEDIR\n"
68 " INCLUDEDIR OCTINCLUDEDIR\n"
69 " INFODIR OCTLIBDIR\n"
70 " INFOFILE PREFIX\n"
71 " LIBDIR STARTUPFILEDIR\n"
72 " LIBEXECDIR VERSION\n"
73 "\n"
74 " -v, --version Print the Octave version number.\n"
75 "\n";
76 
77 static std::string
78 substitute_prefix (const std::string& s, const std::string& prefix,
79  const std::string new_prefix)
80 {
81  std::string retval = s;
82 
83  if (!prefix.empty () && new_prefix != prefix)
84  {
85  int len = prefix.length ();
86  if (retval.find (prefix) == 0)
87  retval.replace (0, len, new_prefix);
88  }
89 
90 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
91  std::replace (retval.begin (), retval.end (), '/', '\\');
92 #endif
93 
94  return retval;
95 }
96 
97 static void
98 initialize (void)
99 {
100  if (initialized)
101  return;
102 
103  initialized = true;
104 
105  const char *homestr = getenv ("OCTAVE_HOME");
106  OCTAVE_HOME = (homestr ? homestr : "");
107  PREFIX = %OCTAVE_PREFIX%;
108 
109 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
110  int n = 1024;
111 
112  std::string bin_dir (n, '\0');
113 
114  while (true)
115  {
116  int status = GetModuleFileName (0, &bin_dir[0], n);
117 
118  if (status < n)
119  {
120  bin_dir.resize (status);
121  break;
122  }
123  else
124  {
125  n *= 2;
126  bin_dir.resize (n);
127  }
128  }
129 
130  if (! bin_dir.empty ())
131  {
132  size_t pos = bin_dir.rfind ("\\bin\\");
133 
134  if (pos != std::string::npos)
135  OCTAVE_HOME = bin_dir.substr (0, pos);
136  }
137 #endif
138 
139  vars["API_VERSION"] = %OCTAVE_API_VERSION%;
140  vars["CANONICAL_HOST_TYPE"] = %OCTAVE_CANONICAL_HOST_TYPE%;
141  vars["DEFAULT_PAGER"] = %OCTAVE_DEFAULT_PAGER%;
142  vars["ARCHLIBDIR"] = substitute_prefix (%OCTAVE_ARCHLIBDIR%,
144  vars["BINDIR"] = substitute_prefix (%OCTAVE_BINDIR%, PREFIX, OCTAVE_HOME);
145  vars["DATADIR"] = substitute_prefix (%OCTAVE_DATADIR%, PREFIX, OCTAVE_HOME);
146  vars["DATAROOTDIR"] = substitute_prefix (%OCTAVE_DATAROOTDIR%,
148  vars["EXEC_PREFIX"] = substitute_prefix (%OCTAVE_EXEC_PREFIX%,
150  vars["FCNFILEDIR"] = substitute_prefix (%OCTAVE_FCNFILEDIR%,
152  vars["IMAGEDIR"] = substitute_prefix (%OCTAVE_IMAGEDIR%, PREFIX, OCTAVE_HOME);
153  vars["INCLUDEDIR"] = substitute_prefix (%OCTAVE_INCLUDEDIR%,
155  vars["INFODIR"] = substitute_prefix (%OCTAVE_INFODIR%, PREFIX, OCTAVE_HOME);
156  vars["INFOFILE"] = substitute_prefix (%OCTAVE_INFOFILE%, PREFIX, OCTAVE_HOME);
157  vars["LIBDIR"] = substitute_prefix (%OCTAVE_LIBDIR%, PREFIX, OCTAVE_HOME);
158  vars["LIBEXECDIR"] = substitute_prefix (%OCTAVE_LIBEXECDIR%,
160  vars["LOCALAPIARCHLIBDIR"] = substitute_prefix (%OCTAVE_LOCALAPIARCHLIBDIR%,
162  vars["LOCALAPIFCNFILEDIR"] = substitute_prefix (%OCTAVE_LOCALAPIFCNFILEDIR%,
164  vars["LOCALAPIOCTFILEDIR"] = substitute_prefix (%OCTAVE_LOCALAPIOCTFILEDIR%,
166  vars["LOCALARCHLIBDIR"] = substitute_prefix (%OCTAVE_LOCALARCHLIBDIR%,
168  vars["LOCALFCNFILEDIR"] = substitute_prefix (%OCTAVE_LOCALFCNFILEDIR%,
170  vars["LOCALOCTFILEDIR"] = substitute_prefix (%OCTAVE_LOCALOCTFILEDIR%,
172  vars["LOCALSTARTUPFILEDIR"] = substitute_prefix (%OCTAVE_LOCALSTARTUPFILEDIR%,
174  vars["LOCALVERARCHLIBDIR"] = substitute_prefix (%OCTAVE_LOCALVERARCHLIBDIR%,
176  vars["LOCALVERFCNFILEDIR"] = substitute_prefix (%OCTAVE_LOCALVERFCNFILEDIR%,
178  vars["LOCALVEROCTFILEDIR"] = substitute_prefix (%OCTAVE_LOCALVEROCTFILEDIR%,
180  vars["MAN1DIR"] = substitute_prefix (%OCTAVE_MAN1DIR%, PREFIX, OCTAVE_HOME);
181  vars["MAN1EXT"] = %OCTAVE_MAN1EXT%;
182  vars["MANDIR"] = substitute_prefix (%OCTAVE_MANDIR%, PREFIX, OCTAVE_HOME);
183  vars["OCTFILEDIR"] = substitute_prefix (%OCTAVE_OCTFILEDIR%,
185  vars["OCTINCLUDEDIR"] = substitute_prefix (%OCTAVE_OCTINCLUDEDIR%,
187  vars["OCTLIBDIR"] = substitute_prefix (%OCTAVE_OCTLIBDIR%,
189  vars["PREFIX"] = (OCTAVE_HOME.empty () ? PREFIX : OCTAVE_HOME);
190  vars["STARTUPFILEDIR"] = substitute_prefix (%OCTAVE_STARTUPFILEDIR%,
192  vars["VERSION"] = %OCTAVE_VERSION%;
193 }
194 
195 int
196 main (int argc, char **argv)
197 {
198  initialize ();
199 
200  if (argc == 1)
201  {
202  std::cout << usage_msg << std::endl;
203  return 1;
204  }
205 
206  for (int i = 1; i < argc; i++)
207  {
208  std::string arg (argv[i]);
209 
210  if (arg == "-h" || arg == "-?" || arg == "--help")
211  {
212  std::cout << usage_msg << std::endl;
213  std::cout << help_msg;
214  return 0;
215  }
216  else if (arg == "--m-site-dir")
217  std::cout << vars["LOCALVERFCNFILEDIR"] << std::endl;
218  else if (arg == "--oct-site-dir")
219  std::cout << vars["LOCALVEROCTFILEDIR"] << std::endl;
220  else if (arg == "-v" || arg == "--version")
221  std::cout << vars["VERSION"] << std::endl;
222  else if (arg == "-p" || arg == "--print")
223  {
224  if (i < argc-1)
225  {
226  arg = argv[++i];
227  std::cout << vars[arg] << std::endl;
228  }
229  else
230  {
231  std::cerr << "octave-config: " << arg
232  << " options requires argument" << std::endl;
233  return 1;
234  }
235  }
236  else
237  {
238  std::cerr << "octave-config: unrecognized argument " << arg
239  << std::endl;
240  return 1;
241  }
242  }
243 
244  return 0;
245 }