]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.cpp
ccc04b69e2a26609573dedf8e3c790c7a16be0f4
[lyx.git] / src / support / os_unix.cpp
1 /**
2  * \file os_unix.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Ruurd A. Reitsma
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Various OS specific functions
11  */
12
13 #include <config.h>
14
15 #include "support/os.h"
16 #include "support/docstring.h"
17 #include "support/FileName.h"
18 #include "support/lstrings.h"
19
20 #include <limits.h>
21 #include <stdlib.h>
22
23 #ifdef __APPLE__
24 #include <Carbon/Carbon.h>
25 #endif
26
27 using namespace std;
28
29 namespace lyx {
30 namespace support {
31 namespace os {
32
33 namespace {
34
35 string stdoutdev_ = "/dev/stdout";
36 string stderrdev_ = "/dev/stderr";
37
38 } // namespace anon
39
40 void init(int, char *[])
41 {
42         // Check whether /dev/stdout and /dev/stderr are available,
43         // otherwise default to /dev/tty.
44         if (access(stdoutdev_.c_str(), W_OK) != 0
45             || access(stderrdev_.c_str(), W_OK) != 0) {
46                 stdoutdev_ = "/dev/tty";
47                 stderrdev_ = "/dev/tty";
48         }
49 }
50
51
52 string current_root()
53 {
54         return "/";
55 }
56
57
58 bool isFilesystemCaseSensitive()
59 {
60 #ifdef __APPLE__
61         return false;
62 #else
63         return true;
64 #endif
65 }
66
67
68 docstring::size_type common_path(docstring const & p1, docstring const & p2)
69 {
70         docstring::size_type i = 0;
71         docstring::size_type const p1_len = p1.length();
72         docstring::size_type const p2_len = p2.length();
73         while (i < p1_len && i < p2_len && p1[i] == p2[i])
74                 ++i;
75         if ((i < p1_len && i < p2_len)
76             || (i < p1_len && p1[i] != '/' && i == p2_len)
77             || (i < p2_len && p2[i] != '/' && i == p1_len))
78         {
79                 if (i)
80                         --i;     // here was the last match
81                 while (i && p1[i] != '/')
82                         --i;
83         }
84         return i;
85 }
86
87
88 string external_path(string const & p)
89 {
90         return p;
91 }
92
93
94 string internal_path(string const & p)
95 {
96         return p;
97 }
98
99
100 string external_path_list(string const & p)
101 {
102         return p;
103 }
104
105
106 string internal_path_list(string const & p)
107 {
108         return p;
109 }
110
111
112 string latex_path(string const & p)
113 {
114         return p;
115 }
116
117
118 bool is_valid_strftime(string const & p)
119 {
120         string::size_type pos = p.find_first_of('%');
121         while (pos != string::npos) {
122                 if (pos + 1 == string::npos)
123                         break;
124                 if (!containsOnly(p.substr(pos + 1, 1),
125                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
126                         return false;
127                 if (pos + 2 == string::npos)
128                       break;
129                 pos = p.find_first_of('%', pos + 2);
130         }
131         return true;
132 }
133
134
135 char const * popen_read_mode()
136 {
137         return "r";
138 }
139
140
141 string const & nulldev()
142 {
143         static string const nulldev_ = "/dev/null";
144         return nulldev_;
145 }
146
147
148 string const & stdoutdev()
149 {
150         return stdoutdev_;
151 }
152
153
154 string const & stderrdev()
155 {
156         return stderrdev_;
157 }
158
159
160 bool terminal_output()
161 {
162         return isatty(1) && isatty(2);
163 }
164
165
166 shell_type shell()
167 {
168         return UNIX;
169 }
170
171
172 char path_separator()
173 {
174         return ':';
175 }
176
177
178 void windows_style_tex_paths(bool)
179 {}
180
181 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
182 {
183 #ifdef __APPLE__
184 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
185         CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
186                                         (UInt8 *) ext.c_str(), ext.length(),
187                                         kCFStringEncodingISOLatin1, false);
188         // this is what we would like to do but it seems that the
189         // viewer for PDF is often quicktime...
190         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
191         (void)mode;
192         LSRolesMask role = kLSRolesAll;
193         FSRef outAppRef;
194         OSStatus status =
195                 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
196                                         cfs_ext, role, &outAppRef, NULL);
197         CFRelease(cfs_ext);
198
199         return status != kLSApplicationNotFoundErr;
200 #else
201         // silence compiler warnings
202         (void)ext;
203         (void)mode;
204
205         // currently, no default viewer is tried for non-windows system
206         // support for KDE/Gnome/Macintosh may be added later
207         return false;
208 #endif
209 }
210
211
212 bool autoOpenFile(string const & filename, auto_open_mode const mode)
213 {
214 #ifdef __APPLE__
215 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
216         FSRef fileref;
217         OSStatus status =
218                 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
219         if (status != 0)
220                 return false;
221
222         // this is what we would like to do but it seems that the
223         // viewer for PDF is often quicktime...
224         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
225         (void)mode;
226         LSRolesMask role = kLSRolesAll;
227         FSRef outAppRef;
228
229         status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
230         if (status == kLSApplicationNotFoundErr)
231                 return false;
232
233         LSLaunchFSRefSpec inLaunchSpec;
234         inLaunchSpec.appRef = &outAppRef;
235         inLaunchSpec.numDocs = 1;
236         inLaunchSpec.itemRefs = &fileref;
237         inLaunchSpec.passThruParams = NULL;
238         inLaunchSpec.launchFlags = kLSLaunchDefaults;
239         inLaunchSpec.asyncRefCon = NULL;
240         status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
241
242         return status != kLSApplicationNotFoundErr;
243 #else
244         // silence compiler warnings
245         (void)filename;
246         (void)mode;
247
248         // currently, no default viewer is tried for non-windows system
249         // support for KDE/Gnome/Macintosh may be added later
250         return false;
251 #endif
252 }
253
254
255 string real_path(string const & path)
256 {
257         char rpath[PATH_MAX + 1];
258         char * result = realpath(path.c_str(), rpath);
259         return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
260 }
261
262 } // namespace os
263 } // namespace support
264 } // namespace lyx