File: launch_browser.c

package info (click to toggle)
quickplot 0.10.3-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,464 kB
  • sloc: ansic: 16,640; sh: 11,163; makefile: 395
file content (196 lines) | stat: -rw-r--r-- 3,969 bytes parent folder | download
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
/*
  Quickplot - an interactive 2D plotter

  Copyright (C) 1998-2011  Lance Arsenault


  This file is part of Quickplot.

  Quickplot is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation, either version 3 of the License,
  or (at your option) any later version.

  Quickplot is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Quickplot.  If not, see <http://www.gnu.org/licenses/>.

*/

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

#include "paths.h"



#include "quickplot.h"
#include "config.h"
#include "debug.h"
#include "list.h"
#include "channel.h"
#include "spew.h"
#include "channel_double.h"
#include "qp.h"

#ifdef DMALLOC
#  include "dmalloc.h"
#endif


static int LaunchBrowser(const char * const url)
{
  int i=0;
  pid_t pid;

  // now try the list of browsers
  const char * const browsers[] =
  {
    "firefox",
    "galeon",
    "konqueror",
    "mozilla",
    "epiphany",
    "opera",
    "netscape",
    "dillo",
    "seamonkey",
    /*
    "amaya",
    "browsex",
    "light",
    */
    NULL
  };

 
  /* Lynx is so cool I've got to try it */
  //execlp("lynx", "lynx", url, NULL);
  /* It worked. That was cool. */


  pid = vfork();
  if(pid < 0) //error
  {
    QP_WARN("vfork() failed");
    return 1;
  }

  if(pid > 0) // parent
    return 0;

  // I'm the child.

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

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

  browser =  getenv("QP_BROWSER");

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


  for(browser=browsers[i]; browser; browser=browsers[++i])
    {
      INFO("executing: %s %s\n", browser, url);
      execlp(browser, browser, url, NULL);
    }

  QP_WARN("Failed to launch a browser");

  exit(EXIT_FAILURE); // child exits
}

/* for finding the full path to
 * help.txt about.txt about.html help.html index.html
 * Returns an open fd or -1 on failure 
 * Returns fullpath from malloc() */
int qp_find_doc_file(const char *filename, char **fullpath_ret)
{
  int i;
  char *fullpath;
  char *htmldir[4];

  ASSERT(strlen(filename) > 5);

  htmldir[0] = getenv("QUICKPLOT_HTMLDIR");
  htmldir[1] = getenv("QUICKPLOT_DOCDIR");
  htmldir[2] = HTMLDIR;
  htmldir[3] = DOCDIR;

  for(i=0;i<4;++i)
  {
    int fd;

    if(!htmldir[i]) continue;

    fullpath = qp_malloc(strlen(filename) + 2 + strlen(htmldir[i]));
    sprintf(fullpath, "%s%c%s", htmldir[i], DIR_CHAR, filename);
    
    fd = open(fullpath, O_RDONLY);
    if(fd == -1)
    {
      QP_INFO("Can't open file \"%s\"\n", fullpath);
      free(fullpath);
      continue;
    }
    else
    {
      if(fullpath_ret)
        *fullpath_ret = fullpath;
      return fd;
    }
  }

  if(fullpath_ret)
    *fullpath_ret = NULL;

  {
    int is_txt;
    is_txt = strcmp(".txt", &filename[strlen(filename)-4])?0:1;

    QP_WARN("Can't open Quickplot documentation file \"%s\"\n"
        "Try setting environment variable QUICKPLOT_%s to\n"
        "the directory where this file was installed to.\n",
        filename, is_txt?"DOCDIR":"HTMLDIR");
  }
  return -1; /* failed */ 
}


int qp_launch_browser(const char *fileName)
// Returns 0 on success
{
  int ret = 1;
  int fd;
  char *fullpath = NULL;

  ASSERT(fileName);
  ASSERT(fileName[0]);
  ASSERT(fileName[0] != DIR_CHAR);

  fd = qp_find_doc_file(fileName, &fullpath);

  if(fullpath)
  {
    close(fd);
    ret = LaunchBrowser(fullpath);
    free(fullpath);
  }
  return ret;
}