File: launchBrowser.cpp

package info (click to toggle)
quickplot 0.8.6-1.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,548 kB
  • ctags: 1,019
  • sloc: cpp: 10,052; sh: 7,597; makefile: 176
file content (205 lines) | stat: -rw-r--r-- 4,129 bytes parent folder | download | duplicates (2)
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
/* Copyright (c) 1998, 1999, 2003, 2004  Lance Arsenault, (GNU GPL (v2+))
 */
#include "config.h"

#ifdef MINGW
#include <stdio.h>
#include <Windows.h>
#include <winbase.h>
char *browsers[] =
{
  "mozilla.exe",
  "explorer.exe",
  NULL
};
 

int launchBrowser(const char *url)
{
  return 0;
}

int sLaunchBrowser(const char *doc, const char *fileName_in="")
{
  STARTUPINFO si;
  PROCESS_INFORMATION pi;
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );


  int i=0;
  for(;browsers[i];i++)
  {
    char command[128];
    snprintf(command, 128, "%s %s", browsers[i], fileName_in);
     if(
         CreateProcess( NULL,  // No module name (use command line). 
        command,          // Command line. 
        NULL,             // Process handle not inheritable. 
        NULL,             // Thread handle not inheritable. 
        FALSE,            // Set handle inheritance to FALSE. 
        CREATE_NEW_CONSOLE, // creation flags. 
        NULL,             // Use parent's environment block. 
        NULL,             // Use parent's starting directory. 
        &si,              // Pointer to STARTUPINFO structure.
        &pi )             // Pointer to PROCESS_INFORMATION structure.
        )
        {
        

        printf("ran: %s\n", command);
        
        
       break;
       }
   }
   
    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

  return 0;
}

#else // #if MINGW

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#define TMP_FORMAT  "%s/Quickplot_%s_XXXXXXXX"


char *browsers[] =
{
  "galeon",
  "konqueror",
  "mozilla",
  "epiphany",
  "netscape",
  "dillo",
  /*
  "amaya",
  "browsex",
  "firefox",
  "light",
  */
  NULL
};
  

int launchBrowser(const char *url)
{
  pid_t pid = vfork();
  if(pid < 0) //error
  {
    perror("Quickplot ERROR: vfork() failed");
    return -1;
  }
  if(pid > 0) // parent
    return 0;

  // I'm the child.

  // First see if the user has set a prefered broswer in the BROWSER
  // environment variable.
  char *browser = getenv("BROWSER");

  if(browser)
    execlp(browser, browser, url, NULL);

  // now try the list of browsers
  int i=0;
  for(browser=browsers[i]; browser; browser=browsers[++i])
    execlp(browser, browser, url, NULL);
  
  exit(1);
  
  return 0; //success
}

int sLaunchBrowser(const char *doc, const char *fileName_in="")
{
  size_t len =
    strlen(HTMLDIR)+
    L_tmpnam+
    ((fileName_in)?strlen(fileName_in):0)+
    strlen(TMP_FORMAT);
  
  char *filename = (char *) malloc(sizeof(char)*len);
  filename[0] = '\0';

  if(fileName_in && fileName_in[0])
  {
    // See if we can find the html file in HTMLDIR the doc
    // installation directory.
    snprintf(filename, len, "%s/%s", HTMLDIR, fileName_in);
  
    // first we try to stat the installed html file filename.
    struct stat buf;
    int rt = stat(filename, &buf);
    if(!(rt == 0 && (S_ISREG(buf.st_mode) || S_ISLNK(buf.st_mode)))) 
    {
      // So much for that idea.
      filename[0] = '\0';
    }
  }

  if(!(filename[0]))
  {
      
    // installed docs won't work. Make a tmp file.
    snprintf(filename, len, TMP_FORMAT, P_tmpdir, fileName_in);
  
    int fd = mkstemp(filename);
    if(fd < 0)
    {
      // We can't make a temp file.  we're screwed now.
      perror("Quickplot ERROR: mkstemp() failed");
      free(filename);
      return -1;
    }
    
    write(fd, doc, strlen(doc)+1);
    close(fd);
  }

  launchBrowser(filename);
  
  free(filename);
  return 0; //success
}
#endif

//#define test
#ifdef test

int main(int argc, char **argv)
{
  if(argc < 2)
  {
    const char *src =
      "<html>\n"
      "<head>\n"
      "<title>Test Page</title>\n"
      "</head>\n"
      "<body>\n"
      "Hello World.\n"
      "</body>\n";
    
    printf("sLaunchBrowser()=%d\n", sLaunchBrowser(src));
  }
  else
  {
    printf("launchBrowser(\"%s\")=%d\n",
           argv[1], launchBrowser(argv[1]));
  }

  return 0;
}
#endif // #else // #if MINGW