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"
30 #include <CoreServices/CoreServices.h>
46 void init(int argc, char * argv[])
51 // Set environment's default locale
52 setlocale(LC_ALL, "");
53 setlocale(LC_NUMERIC, "C");
57 string utf8_argv(int i)
59 LASSERT(i < argc_, return "");
60 return to_utf8(from_local8bit(argv_[i]));
64 void remove_internal_args(int, int)
74 bool isFilesystemCaseSensitive()
84 docstring::size_type common_path(docstring const & p1, docstring const & p2)
86 docstring::size_type i = 0;
87 docstring::size_type const p1_len = p1.length();
88 docstring::size_type const p2_len = p2.length();
90 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
93 while (i < p1_len && i < p2_len && p1[i] == p2[i])
96 if ((i < p1_len && i < p2_len)
97 || (i < p1_len && p1[i] != '/' && i == p2_len)
98 || (i < p2_len && p2[i] != '/' && i == p1_len))
101 --i; // here was the last match
102 while (i && p1[i] != '/')
109 bool path_prefix_is(string const & path, string const & pre)
112 return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
114 return prefixIs(path, pre);
119 bool path_prefix_is(string & path, string const & pre, path_case how)
122 docstring const p1 = from_utf8(path);
123 docstring const p2 = from_utf8(pre);
124 docstring::size_type const p1_len = p1.length();
125 docstring::size_type const p2_len = p2.length();
126 docstring::size_type common_len = common_path(p1, p2);
128 if (p2[p2_len - 1] == '/' && p1_len != p2_len)
131 if (common_len != p2_len)
134 if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
135 if (p1_len < common_len)
136 path = to_utf8(p2.substr(0, p1_len));
138 path = to_utf8(p2 + p1.substr(common_len,
139 p1_len - common_len));
144 // silence compiler warnings
147 return prefixIs(path, pre);
152 string external_path(string const & p)
158 string internal_path(string const & p)
164 string safe_internal_path(string const & p, file_access)
170 string external_path_list(string const & p)
176 string internal_path_list(string const & p)
182 string latex_path(string const & p)
188 string latex_path_list(string const & p)
194 bool is_valid_strftime(string const & p)
196 string::size_type pos = p.find_first_of('%');
197 while (pos != string::npos) {
198 if (pos + 1 == string::npos)
200 if (!containsOnly(p.substr(pos + 1, 1),
201 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
203 if (pos + 2 == string::npos)
205 pos = p.find_first_of('%', pos + 2);
211 char const * popen_read_mode()
217 string const & nulldev()
219 static string const nulldev_ = "/dev/null";
230 char path_separator(path_type)
236 void windows_style_tex_paths(bool)
239 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
242 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
243 CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
244 (UInt8 *) ext.c_str(), ext.length(),
245 kCFStringEncodingISOLatin1, false);
246 // this is what we would like to do but it seems that the
247 // viewer for PDF is often quicktime...
248 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
250 LSRolesMask role = kLSRolesAll;
253 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
254 cfs_ext, role, &outAppRef, NULL);
257 return status != kLSApplicationNotFoundErr;
259 // silence compiler warnings
263 // currently, no default viewer is tried for non-windows system
264 // support for KDE/Gnome/Macintosh may be added later
270 bool autoOpenFile(string const & filename, auto_open_mode const mode,
274 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
277 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
281 // this is what we would like to do but it seems that the
282 // viewer for PDF is often quicktime...
283 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
285 LSRolesMask role = kLSRolesAll;
288 status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
289 if (status == kLSApplicationNotFoundErr)
292 string const texinputs = os::latex_path_list(
293 replaceCurdirPath(path, lyxrc.texinputs_prefix));
294 string const oldval = getEnv("TEXINPUTS");
295 string const newval = ".:" + texinputs + ":" + oldval;
296 if (!path.empty() && !lyxrc.texinputs_prefix.empty())
297 setEnv("TEXINPUTS", newval);
299 LSLaunchFSRefSpec inLaunchSpec;
300 inLaunchSpec.appRef = &outAppRef;
301 inLaunchSpec.numDocs = 1;
302 inLaunchSpec.itemRefs = &fileref;
303 inLaunchSpec.passThruParams = NULL;
304 inLaunchSpec.launchFlags = kLSLaunchDefaults;
305 inLaunchSpec.asyncRefCon = NULL;
306 status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
308 if (!path.empty() && !lyxrc.texinputs_prefix.empty())
309 setEnv("TEXINPUTS", oldval);
311 return status != kLSApplicationNotFoundErr;
313 // silence compiler warnings
318 // currently, no default viewer is tried for non-windows system
319 // support for KDE/Gnome/Macintosh may be added later
325 string real_path(string const & path)
327 #ifdef HAVE_DEF_PATH_MAX
328 char rpath[PATH_MAX + 1];
329 char * result = realpath(path.c_str(), rpath);
330 return FileName::fromFilesystemEncoding(result ? rpath : path).absFileName();
332 char * result = realpath(path.c_str(), NULL);
333 string ret = FileName::fromFilesystemEncoding(result ? result : path).absFileName();
340 } // namespace support