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[])
52 string utf8_argv(int i)
54 LASSERT(i < argc_, /**/);
55 return to_utf8(from_local8bit(argv_[i]));
59 void remove_internal_args(int, int)
69 bool isFilesystemCaseSensitive()
79 docstring::size_type common_path(docstring const & p1, docstring const & p2)
81 docstring::size_type i = 0;
82 docstring::size_type const p1_len = p1.length();
83 docstring::size_type const p2_len = p2.length();
85 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
88 while (i < p1_len && i < p2_len && p1[i] == p2[i])
91 if ((i < p1_len && i < p2_len)
92 || (i < p1_len && p1[i] != '/' && i == p2_len)
93 || (i < p2_len && p2[i] != '/' && i == p1_len))
96 --i; // here was the last match
97 while (i && p1[i] != '/')
104 bool path_prefix_is(string const & path, string const & pre)
107 return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
109 return prefixIs(path, pre);
114 bool path_prefix_is(string & path, string const & pre, path_case how)
117 docstring const p1 = from_utf8(path);
118 docstring const p2 = from_utf8(pre);
119 docstring::size_type const p1_len = p1.length();
120 docstring::size_type const p2_len = p2.length();
121 docstring::size_type common_len = common_path(p1, p2);
123 if (p2[p2_len - 1] == '/' && p1_len != p2_len)
126 if (common_len != p2_len)
129 if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
130 if (p1_len < common_len)
131 path = to_utf8(p2.substr(0, p1_len));
133 path = to_utf8(p2 + p1.substr(common_len,
134 p1_len - common_len));
139 // silence compiler warnings
142 return prefixIs(path, pre);
147 string external_path(string const & p)
153 string internal_path(string const & p)
159 string safe_internal_path(string const & p, file_access)
165 string external_path_list(string const & p)
171 string internal_path_list(string const & p)
177 string latex_path(string const & p)
183 string latex_path_list(string const & p)
189 bool is_valid_strftime(string const & p)
191 string::size_type pos = p.find_first_of('%');
192 while (pos != string::npos) {
193 if (pos + 1 == string::npos)
195 if (!containsOnly(p.substr(pos + 1, 1),
196 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
198 if (pos + 2 == string::npos)
200 pos = p.find_first_of('%', pos + 2);
206 char const * popen_read_mode()
212 string const & nulldev()
214 static string const nulldev_ = "/dev/null";
225 char path_separator(path_type)
231 void windows_style_tex_paths(bool)
234 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
237 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
238 CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
239 (UInt8 *) ext.c_str(), ext.length(),
240 kCFStringEncodingISOLatin1, false);
241 // this is what we would like to do but it seems that the
242 // viewer for PDF is often quicktime...
243 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
245 LSRolesMask role = kLSRolesAll;
248 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
249 cfs_ext, role, &outAppRef, NULL);
252 return status != kLSApplicationNotFoundErr;
254 // silence compiler warnings
258 // currently, no default viewer is tried for non-windows system
259 // support for KDE/Gnome/Macintosh may be added later
265 bool autoOpenFile(string const & filename, auto_open_mode const mode,
269 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
272 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
276 // this is what we would like to do but it seems that the
277 // viewer for PDF is often quicktime...
278 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
280 LSRolesMask role = kLSRolesAll;
283 status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
284 if (status == kLSApplicationNotFoundErr)
287 string const texinputs = os::latex_path_list(
288 replaceCurdirPath(path, lyxrc.texinputs_prefix));
289 string const oldval = getEnv("TEXINPUTS");
290 string const newval = ".:" + texinputs + ":" + oldval;
291 if (!path.empty() && !lyxrc.texinputs_prefix.empty())
292 setEnv("TEXINPUTS", newval);
294 LSLaunchFSRefSpec inLaunchSpec;
295 inLaunchSpec.appRef = &outAppRef;
296 inLaunchSpec.numDocs = 1;
297 inLaunchSpec.itemRefs = &fileref;
298 inLaunchSpec.passThruParams = NULL;
299 inLaunchSpec.launchFlags = kLSLaunchDefaults;
300 inLaunchSpec.asyncRefCon = NULL;
301 status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
303 if (!path.empty() && !lyxrc.texinputs_prefix.empty())
304 setEnv("TEXINPUTS", oldval);
306 return status != kLSApplicationNotFoundErr;
308 // silence compiler warnings
313 // currently, no default viewer is tried for non-windows system
314 // support for KDE/Gnome/Macintosh may be added later
320 string real_path(string const & path)
322 #ifdef HAVE_DEF_PATH_MAX
323 char rpath[PATH_MAX + 1];
324 char * result = realpath(path.c_str(), rpath);
325 return FileName::fromFilesystemEncoding(result ? rpath : path).absFileName();
327 char * result = realpath(path.c_str(), NULL);
328 string ret = FileName::fromFilesystemEncoding(result ? result : path).absFileName();
335 } // namespace support