uri.c 4.84 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
18
19
20
21
22
23
24
 */
#include <stdlib.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
25
26
27
#ifdef __EMX__
#include <process.h>
#endif
Elliot Lee's avatar
Elliot Lee committed
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
#include "libgimp/gimp.h"

/* Author: Josh MacDonald. */

static void   query      (void);
static void   run        (char    *name,
                          int      nparams,
                          GParam  *param,
                          int     *nreturn_vals,
                          GParam **return_vals);
static gint32 load_image (char   *filename);

GPlugInInfo PLUG_IN_INFO =
{
  NULL,    /* init_proc */
  NULL,    /* quit_proc */
  query,   /* query_proc */
  run,     /* run_proc */
};

MAIN ()

static void
query ()
{
  static GParamDef load_args[] =
  {
    { PARAM_INT32, "run_mode", "Interactive, non-interactive" },
    { PARAM_STRING, "filename", "The name of the file to load" },
    { PARAM_STRING, "raw_filename", "The name entered" },
  };

  static GParamDef load_return_vals[] =
  {
    { PARAM_IMAGE, "image", "Output image" },
  };

  static int nload_args = sizeof (load_args) / sizeof (load_args[0]);
  static int nload_return_vals = sizeof (load_return_vals) / sizeof (load_return_vals[0]);

  gimp_install_procedure ("file_url_load",
                          "loads files given a URL",
                          "You need to have GNU Wget installed.",
                          "Spencer Kimball & Peter Mattis",
                          "Spencer Kimball & Peter Mattis",
                          "1995-1997",
                          "<Load>/URL",
			  NULL,
                          PROC_PLUG_IN,
                          nload_args, nload_return_vals,
                          load_args, load_return_vals);

  gimp_register_load_handler ("file_url_load", "", "http:,ftp:");
}

static void
run (char    *name,
     int      nparams,
     GParam  *param,
     int     *nreturn_vals,
     GParam **return_vals)
{
  static GParam values[2];
  GRunModeType run_mode;
  gint32 image_ID;

  run_mode = param[0].data.d_int32;

  *nreturn_vals = 1;
  *return_vals = values;

  values[0].type = PARAM_STATUS;
  values[0].data.d_status = STATUS_CALLING_ERROR;

  if (strcmp (name, "file_url_load") == 0)
    {
      image_ID = load_image (param[2].data.d_string);
      if (image_ID != -1)
	{
	  *nreturn_vals = 2;
	  values[0].data.d_status = STATUS_SUCCESS;
	  values[1].type = PARAM_IMAGE;
	  values[1].data.d_image = image_ID;
	}
      else
	{
	  values[0].data.d_status = STATUS_EXECUTION_ERROR;
	}
    }
  else
    g_assert (FALSE);
}

static gint32
load_image (char *filename)
{
  GParam* params;
  gint retvals;
  char* ext = strrchr (filename, '.');
  char* tmpname;
  int pid;
  int status;

  if (!ext || ext[1] == 0 || strchr(ext, '/'))
    {
Manish Singh's avatar
Manish Singh committed
133
      g_message ("url: can't open URL without an extension\n");
Elliot Lee's avatar
Elliot Lee committed
134
135
136
137
138
139
140
141
142
143
      return -1;
    }

  params = gimp_run_procedure ("gimp_temp_name",
			       &retvals,
			       PARAM_STRING, ext + 1,
			       PARAM_END);

  tmpname = params[1].data.d_string;

Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
144
#ifndef __EMX__
Elliot Lee's avatar
Elliot Lee committed
145
146
  if ((pid = fork()) < 0)
    {
Manish Singh's avatar
Manish Singh committed
147
      g_message ("url: fork failed: %s\n", g_strerror(errno));
Elliot Lee's avatar
Elliot Lee committed
148
149
150
151
152
      return -1;
    }
  else if (pid == 0)
    {
      execlp ("wget", "wget", filename, "-O", tmpname, NULL);
Manish Singh's avatar
Manish Singh committed
153
      g_message ("url: exec failed: wget: %s\n", g_strerror(errno));
Elliot Lee's avatar
Elliot Lee committed
154
155
156
      _exit(127);
    }
  else
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
157
158
159
160
161
162
163
164
#else /* __EMX__ */
  pid = spawnlp (P_NOWAIT, "wget", "wget", filename, "-O", tmpname, NULL);
  if (pid == -1)
    {
      g_message ("url: spawn failed: %s\n", g_strerror(errno));
      return -1;
    }
#endif
Elliot Lee's avatar
Elliot Lee committed
165
166
167
168
169
170
    {
      waitpid (pid, &status, 0);

      if (!WIFEXITED(status) ||
	  WEXITSTATUS(status) != 0)
	{
Manish Singh's avatar
Manish Singh committed
171
	  g_message ("url: wget exited abnormally on URL %s\n", filename);
Elliot Lee's avatar
Elliot Lee committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
	  return -1;
	}
    }

  params = gimp_run_procedure ("gimp_file_load",
			       &retvals,
			       PARAM_INT32, 0,
			       PARAM_STRING, tmpname,
			       PARAM_STRING, tmpname,
			       PARAM_END);

  unlink (tmpname);

  if (params[0].data.d_status == FALSE)
    return -1;
  else
    {
189
      gimp_image_set_filename (params[1].data.d_int32, filename);
Elliot Lee's avatar
Elliot Lee committed
190
191
192
      return params[1].data.d_int32;
    }
}