3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Ruurd A. Reitsma
8 * Full author contact details are available in file CREDITS.
10 * Various OS specific functions
17 #include "support/os.h"
18 #include "support/docstring.h"
19 #include "support/environment.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/lassert.h"
29 #include <CoreServices/CoreServices.h>
45 void init(int argc, char * argv[])
50 // Set environment's default locale
51 setlocale(LC_ALL, "");
52 setlocale(LC_NUMERIC, "C");
56 string utf8_argv(int i)
58 LASSERT(i < argc_, return "");
59 return to_utf8(from_local8bit(argv_[i]));
63 void remove_internal_args(int, int)
73 bool isFilesystemCaseSensitive()
83 docstring::size_type common_path(docstring const & p1, docstring const & p2)
85 docstring::size_type i = 0;
86 docstring::size_type const p1_len = p1.length();
87 docstring::size_type const p2_len = p2.length();
89 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
92 while (i < p1_len && i < p2_len && p1[i] == p2[i])
95 if ((i < p1_len && i < p2_len)
96 || (i < p1_len && p1[i] != '/' && i == p2_len)
97 || (i < p2_len && p2[i] != '/' && i == p1_len))
100 --i; // here was the last match
101 while (i && p1[i] != '/')
108 bool path_prefix_is(string const & path, string const & pre)
111 return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
113 return prefixIs(path, pre);
118 bool path_prefix_is(string & path, string const & pre, path_case how)
121 docstring const p1 = from_utf8(path);
122 docstring const p2 = from_utf8(pre);
123 docstring::size_type const p1_len = p1.length();
124 docstring::size_type const p2_len = p2.length();
125 docstring::size_type common_len = common_path(p1, p2);
127 if (p2[p2_len - 1] == '/' && p1_len != p2_len)
130 if (common_len != p2_len)
133 if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
134 if (p1_len < common_len)
135 path = to_utf8(p2.substr(0, p1_len));
137 path = to_utf8(p2 + p1.substr(common_len,
138 p1_len - common_len));
143 // silence compiler warnings
146 return prefixIs(path, pre);
151 string external_path(string const & p)
157 string internal_path(string const & p)
163 string safe_internal_path(string const & p, file_access)
169 string external_path_list(string const & p)
175 string internal_path_list(string const & p)
181 string latex_path(string const & p)
187 string latex_path_list(string const & p)
193 bool is_valid_strftime(string const & p)
195 string::size_type pos = p.find_first_of('%');
196 while (pos != string::npos) {
197 if (pos + 1 == string::npos)
199 if (!containsOnly(p.substr(pos + 1, 1),
200 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
202 if (pos + 2 == string::npos)
204 pos = p.find_first_of('%', pos + 2);
210 char const * popen_read_mode()
216 string const & nulldev()
218 static string const nulldev_ = "/dev/null";
229 char path_separator(path_type)
235 void windows_style_tex_paths(bool)
238 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
241 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
242 CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
243 (UInt8 *) ext.c_str(), ext.length(),
244 kCFStringEncodingISOLatin1, false);
245 // this is what we would like to do but it seems that the
246 // viewer for PDF is often quicktime...
247 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
249 LSRolesMask role = kLSRolesAll;
252 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
253 cfs_ext, role, &outAppRef, NULL);
256 return status != kLSApplicationNotFoundErr;
258 // silence compiler warnings
262 // currently, no default viewer is tried for non-windows system
263 // support for KDE/Gnome/Macintosh may be added later
269 bool autoOpenFile(string const & filename, auto_open_mode const mode,
273 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
276 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
280 // this is what we would like to do but it seems that the
281 // viewer for PDF is often quicktime...
282 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
284 LSRolesMask role = kLSRolesAll;
287 status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
288 if (status == kLSApplicationNotFoundErr)
291 string const texinputs = os::latex_path_list(
292 replaceCurdirPath(path, lyxrc.texinputs_prefix));
293 string const oldval = getEnv("TEXINPUTS");
294 string const newval = ".:" + texinputs + ":" + oldval;
295 if (!path.empty() && !lyxrc.texinputs_prefix.empty())
296 setEnv("TEXINPUTS", newval);
298 LSLaunchFSRefSpec inLaunchSpec;
299 inLaunchSpec.appRef = &outAppRef;
300 inLaunchSpec.numDocs = 1;
301 inLaunchSpec.itemRefs = &fileref;
302 inLaunchSpec.passThruParams = NULL;
303 inLaunchSpec.launchFlags = kLSLaunchDefaults;
304 inLaunchSpec.asyncRefCon = NULL;
305 status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
307 if (!path.empty() && !lyxrc.texinputs_prefix.empty())
308 setEnv("TEXINPUTS", oldval);
310 return status != kLSApplicationNotFoundErr;
312 // silence compiler warnings
317 // currently, no default viewer is tried for non-windows system
318 // support for KDE/Gnome/Macintosh may be added later
324 string real_path(string const & path)
326 #ifdef HAVE_DEF_PATH_MAX
327 char rpath[PATH_MAX + 1];
328 char * result = realpath(path.c_str(), rpath);
329 return FileName::fromFilesystemEncoding(result ? rpath : path).absFileName();
331 char * result = realpath(path.c_str(), NULL);
332 string ret = FileName::fromFilesystemEncoding(result ? result : path).absFileName();
339 } // namespace support