1 /**
2  * Open an online manual page.
3  *
4  * Copyright: Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
5  * Authors:   Walter Bright, http://www.digitalmars.com
6  * License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7  * Source:    $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/root/man.d, root/_man.d)
8  * Documentation:  https://dlang.org/phobos/dmd_root_man.html
9  * Coverage:    https://codecov.io/gh/dlang/dmd/src/master/src/dmd/root/man.d
10  */
11 
12 module dmd.root.man;
13 
14 import core.stdc.stdio;
15 import core.stdc.stdlib;
16 import core.stdc.string;
17 import core.sys.posix.unistd;
18 import core.sys.windows.shellapi;
19 import core.sys.windows.winuser;
20 
21 version (Windows)
22 {
23     extern (C++) void browse(const(char)* url) nothrow @nogc
24     in
25     {
26         assert(strncmp(url, "http://", 7) == 0 || strncmp(url, "https://", 8) == 0);
27     }
28     do
29     {
30         ShellExecuteA(null, "open", url, null, null, SW_SHOWNORMAL);
31     }
32 }
33 else version (OSX)
34 {
35     extern (C++) void browse(const(char)* url) nothrow @nogc
36     in
37     {
38         assert(strncmp(url, "http://", 7) == 0 || strncmp(url, "https://", 8) == 0);
39     }
40     do
41     {
42         pid_t childpid;
43         const(char)*[5] args;
44         char* browser = getenv("BROWSER");
45         if (browser)
46         {
47             browser = strdup(browser);
48             args[0] = browser;
49             args[1] = url;
50             args[2] = null;
51         }
52         else
53         {
54             args[0] = "open";
55             args[1] = url;
56             args[2] = null;
57         }
58         childpid = fork();
59         if (childpid == 0)
60         {
61             execvp(args[0], cast(char**)args);
62             perror(args[0]); // failed to execute
63             return;
64         }
65     }
66 }
67 else version (Posix)
68 {
69     extern (C++) void browse(const(char)* url) nothrow @nogc
70     in
71     {
72         assert(strncmp(url, "http://", 7) == 0 || strncmp(url, "https://", 8) == 0);
73     }
74     do
75     {
76         pid_t childpid;
77         const(char)*[3] args;
78         const(char)* browser = getenv("BROWSER");
79         if (browser)
80             browser = strdup(browser);
81         else
82             browser = "xdg-open";
83         args[0] = browser;
84         args[1] = url;
85         args[2] = null;
86         childpid = fork();
87         if (childpid == 0)
88         {
89             execvp(args[0], cast(char**)args);
90             perror(args[0]); // failed to execute
91             return;
92         }
93     }
94 }