1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
//
// "$Id$"
//
// fl_open_uri() code for FLTK.
//
// Test with:
//
// gcc -I/fltk/dir -I/fltk/dir/src -DTEST -o fl_open_uri fl_open_uri.cxx -lfltk
//
// Copyright 2003-2010 by Michael R Sweet
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
//
//
// Include necessary headers...
//
#include <FL/filename.H>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include "flstring.h"
#ifdef WIN32
# include <windows.h>
# include <shellapi.h>
#else
# include <sys/wait.h>
# include <signal.h>
# include <fcntl.h>
# include <unistd.h>
#endif // WIN32
//
// Local functions...
//
#if !defined(WIN32) && !defined(__APPLE__)
static char *path_find(const char *program, char *filename, int filesize);
#endif // !WIN32 && !__APPLE__
#ifndef WIN32
static int run_program(const char *program, char **argv, char *msg, int msglen);
#endif // !WIN32
/** \addtogroup filenames
@{ */
/**
* Opens the specified Uniform Resource Identifier (URI).
* Uses an operating-system dependent program or interface. For URIs
* using the "ftp", "http", or "https" schemes, the system default web
* browser is used to open the URI, while "mailto" and "news" URIs are
* typically opened using the system default mail reader and "file" URIs
* are opened using the file system navigator.
*
* On success, the (optional) msg buffer is filled with the command that
* was run to open the URI; on Windows, this will always be "open uri".
*
* On failure, the msg buffer is filled with an English error message.
*
* \note
* \b Platform \b Specific \b Issues: \b Windows \n
* With "file:" based URIs on Windows, you may encounter issues with
* anchors being ignored. Example: "file:///c:/some/index.html#anchor"
* may open in the browser without the "#anchor" suffix. The behavior
* seems to vary across different Windows versions. Workaround: open a link
* to a separate html file that redirects to the desired "file:" URI.
*
* \b Example
* \code
* #include <FL/filename.H>
* [..]
* char errmsg[512];
* if ( !fl_open_uri("http://google.com/", errmsg, sizeof(errmsg)) ) {
* char warnmsg[768];
* sprintf(warnmsg, "Error: %s", errmsg);
* fl_alert(warnmsg);
* }
* \endcode
*
* @param uri The URI to open
* @param msg Optional buffer which contains the command or error message
* @param msglen Length of optional buffer
* @return 1 on success, 0 on failure
*/
int
fl_open_uri(const char *uri, char *msg, int msglen) {
// Supported URI schemes...
static const char * const schemes[] = {
"file://",
"ftp://",
"http://",
"https://",
"mailto:",
"news://",
NULL
};
// Validate the URI scheme...
int i;
for (i = 0; schemes[i]; i ++)
if (!strncmp(uri, schemes[i], strlen(schemes[i])))
break;
if (!schemes[i]) {
if (msg) {
char scheme[255];
if (sscanf(uri, "%254[^:]", scheme) == 1) {
snprintf(msg, msglen, "URI scheme \"%s\" not supported.", scheme);
} else {
snprintf(msg, msglen, "Bad URI \"%s\"", uri);
}
}
return 0;
}
#ifdef WIN32
if (msg) snprintf(msg, msglen, "open %s", uri);
return (int)(ShellExecute(HWND_DESKTOP, "open", uri, NULL, NULL, SW_SHOW) > (void *)32);
#elif defined(__APPLE__)
char *argv[3]; // Command-line arguments
argv[0] = (char*)"open";
argv[1] = (char*)uri;
argv[2] = (char*)0;
if (msg) snprintf(msg, msglen, "open %s", uri);
return run_program("/usr/bin/open", argv, msg, msglen) != 0;
#else // !WIN32 && !__APPLE__
// Run any of several well-known commands to open the URI.
//
// We give preference to the Portland group's xdg-utils
// programs which run the user's preferred web browser, etc.
// based on the current desktop environment in use. We fall
// back on older standards and then finally test popular programs
// until we find one we can use.
//
// Note that we specifically do not support the MAILER and
// BROWSER environment variables because we have no idea whether
// we need to run the listed commands in a terminal program.
char command[FL_PATH_MAX], // Command to run...
*argv[4], // Command-line arguments
remote[1024]; // Remote-mode command...
const char * const *commands; // Array of commands to check...
static const char * const browsers[] = {
"xdg-open", // Portland
"htmlview", // Freedesktop.org
"firefox",
"mozilla",
"netscape",
"konqueror", // KDE
"opera",
"hotjava", // Solaris
"mosaic",
NULL
};
static const char * const readers[] = {
"xdg-email", // Portland
"thunderbird",
"mozilla",
"netscape",
"evolution", // GNOME
"kmailservice", // KDE
NULL
};
static const char * const managers[] = {
"xdg-open", // Portland
"fm", // IRIX
"dtaction", // CDE
"nautilus", // GNOME
"konqueror", // KDE
NULL
};
// Figure out which commands to check for...
if (!strncmp(uri, "file://", 7)) commands = managers;
else if (!strncmp(uri, "mailto:", 7) ||
!strncmp(uri, "news:", 5)) commands = readers;
else commands = browsers;
// Find the command to run...
for (i = 0; commands[i]; i ++)
if (path_find(commands[i], command, sizeof(command))) break;
if (!commands[i]) {
if (msg) {
snprintf(msg, msglen, "No helper application found for \"%s\"", uri);
}
return 0;
}
// Handle command-specific arguments...
argv[0] = (char *)commands[i];
if (!strcmp(commands[i], "firefox") ||
!strcmp(commands[i], "mozilla") ||
!strcmp(commands[i], "netscape") ||
!strcmp(commands[i], "thunderbird")) {
// program -remote openURL(uri)
snprintf(remote, sizeof(remote), "openURL(%s)", uri);
argv[1] = (char *)"-remote";
argv[2] = remote;
argv[3] = 0;
} else if (!strcmp(commands[i], "dtaction")) {
// dtaction open uri
argv[1] = (char *)"open";
argv[2] = (char *)uri;
argv[3] = 0;
} else {
// program uri
argv[1] = (char *)uri;
argv[2] = 0;
}
if (msg) {
strlcpy(msg, argv[0], msglen);
for (i = 1; argv[i]; i ++) {
strlcat(msg, " ", msglen);
strlcat(msg, argv[i], msglen);
}
}
return run_program(command, argv, msg, msglen) != 0;
#endif // WIN32
}
/** Decodes a URL-encoded string.
In a Uniform Resource Identifier (URI), all non-ASCII bytes and several others (e.g., '<', '%', ' ')
are URL-encoded using 3 bytes by "%XY" where XY is the hexadecimal value of the byte. This function
decodes the URI restoring its original UTF-8 encoded content. Decoding is done in-place.
*/
void fl_decode_uri(char *uri)
{
char *last = uri + strlen(uri);
while (uri < last-2) {
if (*uri == '%') {
int h;
if ( sscanf(uri+1, "%2X", &h) != 1 ) break;
*uri = h;
memmove(uri+1, uri+3, last - (uri+2));
last -= 2;
}
uri++;
}
}
/** @} */
#if !defined(WIN32) && !defined(__APPLE__)
// Find a program in the path...
static char *path_find(const char *program, char *filename, int filesize) {
const char *path; // Search path
char *ptr, // Pointer into filename
*end; // End of filename buffer
if ((path = getenv("PATH")) == NULL) path = "/bin:/usr/bin";
for (ptr = filename, end = filename + filesize - 1; *path; path ++) {
if (*path == ':') {
if (ptr > filename && ptr[-1] != '/' && ptr < end) *ptr++ = '/';
strlcpy(ptr, program, end - ptr + 1);
if (!access(filename, X_OK)) return filename;
ptr = filename;
} else if (ptr < end) *ptr++ = *path;
}
if (ptr > filename) {
if (ptr[-1] != '/' && ptr < end) *ptr++ = '/';
strlcpy(ptr, program, end - ptr + 1);
if (!access(filename, X_OK)) return filename;
}
return 0;
}
#endif // !WIN32 && !__APPLE__
#ifndef WIN32
// Run the specified program, returning 1 on success and 0 on failure
static int
run_program(const char *program, char **argv, char *msg, int msglen) {
pid_t pid; // Process ID of first child
int status; // Exit status from first child
sigset_t set, oldset; // Signal masks
// Block SIGCHLD while we run the program...
//
// Note that I only use the POSIX signal APIs, however older operating
// systems may either not support POSIX signals or have side effects.
// IRIX, for example, provides three separate and incompatible signal
// APIs, so it is possible that an application setting a signal handler
// via signal() or sigset() will not have its SIGCHLD signals blocked...
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_BLOCK, &set, &oldset);
// Create child processes that actually run the program for us...
if ((pid = fork()) == 0) {
// First child comes here, fork a second child and exit...
if (!fork()) {
// Second child comes here, redirect stdin/out/err to /dev/null...
close(0);
open("/dev/null", O_RDONLY);
close(1);
open("/dev/null", O_WRONLY);
close(2);
open("/dev/null", O_WRONLY);
// Detach from the current process group...
setsid();
// Run the program...
execv(program, argv);
_exit(0);
} else {
// First child gets here, exit immediately...
_exit(0);
}
} else if (pid < 0) {
// Restore signal handling...
sigprocmask(SIG_SETMASK, &oldset, NULL);
// Return indicating failure...
return 0;
}
// Wait for the first child to exit...
while (waitpid(pid, &status, 0) < 0) {
if (errno != EINTR) {
// Someone else grabbed the child status...
if (msg) snprintf(msg, msglen, "waitpid(%ld) failed: %s", (long)pid,
strerror(errno));
// Restore signal handling...
sigprocmask(SIG_SETMASK, &oldset, NULL);
// Return indicating failure...
return 0;
}
}
// Restore signal handling...
sigprocmask(SIG_SETMASK, &oldset, NULL);
// Return indicating success...
return 1;
}
#endif // !WIN32
#ifdef TEST
//
// Test code...
//
// Open the URI on the command-line...
int main(int argc, char **argv) {
char msg[1024];
if (argc != 2) {
puts("Usage: fl_open_uri URI");
return 1;
}
if (!fl_open_uri(argv[1], msg, sizeof(msg))) {
puts(msg);
return 1;
} else return 0;
}
#endif // TEST
//
// End of "$Id$".
//
|