RdFToI.c revision ac92798b
1a966c04fSmrg/*
2a966c04fSmrg * Copyright (C) 1989-95 GROUPE BULL
3a966c04fSmrg *
4a966c04fSmrg * Permission is hereby granted, free of charge, to any person obtaining a copy
5a966c04fSmrg * of this software and associated documentation files (the "Software"), to
6a966c04fSmrg * deal in the Software without restriction, including without limitation the
7a966c04fSmrg * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8a966c04fSmrg * sell copies of the Software, and to permit persons to whom the Software is
9a966c04fSmrg * furnished to do so, subject to the following conditions:
10a966c04fSmrg *
11a966c04fSmrg * The above copyright notice and this permission notice shall be included in
12a966c04fSmrg * all copies or substantial portions of the Software.
13a966c04fSmrg *
14a966c04fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15a966c04fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16a966c04fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17a966c04fSmrg * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18a966c04fSmrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19a966c04fSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20a966c04fSmrg *
21a966c04fSmrg * Except as contained in this notice, the name of GROUPE BULL shall not be
22a966c04fSmrg * used in advertising or otherwise to promote the sale, use or other dealings
23a966c04fSmrg * in this Software without prior written authorization from GROUPE BULL.
24a966c04fSmrg */
25a966c04fSmrg
26a966c04fSmrg/*****************************************************************************\
27a966c04fSmrg*  RdFToI.c:                                                                  *
28a966c04fSmrg*                                                                             *
29a966c04fSmrg*  XPM library                                                                *
30a966c04fSmrg*  Parse an XPM file and create the image and possibly its mask               *
31a966c04fSmrg*                                                                             *
32a966c04fSmrg*  Developed by Arnaud Le Hors                                                *
33a966c04fSmrg\*****************************************************************************/
34a966c04fSmrg
35a966c04fSmrg/* October 2004, source code review by Thomas Biege <thomas@suse.de> */
36a966c04fSmrg
37a966c04fSmrg#ifdef HAVE_CONFIG_H
38a966c04fSmrg#include <config.h>
39a966c04fSmrg#endif
40a966c04fSmrg#include "XpmI.h"
41a966c04fSmrg#ifndef NO_ZPIPE
42a966c04fSmrg#include <fcntl.h>
43a966c04fSmrg#include <errno.h>
44a966c04fSmrg#include <sys/types.h>
45a966c04fSmrg#include <sys/wait.h>
46a966c04fSmrg#else
47a966c04fSmrg#ifdef FOR_MSW
48a966c04fSmrg#include <fcntl.h>
49a966c04fSmrg#endif
50a966c04fSmrg#endif
51a966c04fSmrg
52ac92798bSmrgLFUNC(OpenReadFile, int, (const char *filename, xpmData *mdata));
53a966c04fSmrgLFUNC(xpmDataClose, void, (xpmData *mdata));
54a966c04fSmrg
552e2dd055SmrgFUNC(xpmPipeThrough, FILE*, (int fd,
562e2dd055Smrg			     const char *cmd,
572e2dd055Smrg			     const char *arg1,
582e2dd055Smrg			     const char *mode));
592e2dd055Smrg
60a966c04fSmrg#ifndef CXPMPROG
61a966c04fSmrgint
622e2dd055SmrgXpmReadFileToImage(
632e2dd055Smrg    Display		 *display,
64ac92798bSmrg    const char		 *filename,
652e2dd055Smrg    XImage		**image_return,
662e2dd055Smrg    XImage		**shapeimage_return,
672e2dd055Smrg    XpmAttributes	 *attributes)
68a966c04fSmrg{
69a966c04fSmrg    XpmImage image;
70a966c04fSmrg    XpmInfo info;
71a966c04fSmrg    int ErrorStatus;
72a966c04fSmrg    xpmData mdata;
73a966c04fSmrg
74a966c04fSmrg    xpmInitXpmImage(&image);
75a966c04fSmrg    xpmInitXpmInfo(&info);
76a966c04fSmrg
77a966c04fSmrg    /* open file to read */
78a966c04fSmrg    if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
79a966c04fSmrg	return (ErrorStatus);
80a966c04fSmrg
81a966c04fSmrg    /* create the XImage from the XpmData */
82a966c04fSmrg    if (attributes) {
83a966c04fSmrg	xpmInitAttributes(attributes);
84a966c04fSmrg	xpmSetInfoMask(&info, attributes);
85a966c04fSmrg	ErrorStatus = xpmParseDataAndCreate(display, &mdata,
86a966c04fSmrg					    image_return, shapeimage_return,
87a966c04fSmrg					    &image, &info, attributes);
88a966c04fSmrg    } else
89a966c04fSmrg	ErrorStatus = xpmParseDataAndCreate(display, &mdata,
90a966c04fSmrg					    image_return, shapeimage_return,
91a966c04fSmrg					    &image, NULL, attributes);
92a966c04fSmrg    if (attributes) {
93a966c04fSmrg	if (ErrorStatus >= 0)		/* no fatal error */
94a966c04fSmrg	    xpmSetAttributes(attributes, &image, &info);
95a966c04fSmrg	XpmFreeXpmInfo(&info);
96a966c04fSmrg    }
97a966c04fSmrg
98a966c04fSmrg    xpmDataClose(&mdata);
99a966c04fSmrg    /* free the XpmImage */
100a966c04fSmrg    XpmFreeXpmImage(&image);
101a966c04fSmrg
102a966c04fSmrg    return (ErrorStatus);
103a966c04fSmrg}
104a966c04fSmrg
105a966c04fSmrgint
1062e2dd055SmrgXpmReadFileToXpmImage(
107ac92798bSmrg    const char	*filename,
1082e2dd055Smrg    XpmImage	*image,
1092e2dd055Smrg    XpmInfo	*info)
110a966c04fSmrg{
111a966c04fSmrg    xpmData mdata;
112a966c04fSmrg    int ErrorStatus;
113a966c04fSmrg
114a966c04fSmrg    /* init returned values */
115a966c04fSmrg    xpmInitXpmImage(image);
116a966c04fSmrg    xpmInitXpmInfo(info);
117a966c04fSmrg
118a966c04fSmrg    /* open file to read */
119a966c04fSmrg    if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
120a966c04fSmrg	return (ErrorStatus);
121a966c04fSmrg
122a966c04fSmrg    /* create the XpmImage from the XpmData */
123a966c04fSmrg    ErrorStatus = xpmParseData(&mdata, image, info);
124a966c04fSmrg
125a966c04fSmrg    xpmDataClose(&mdata);
126a966c04fSmrg
127a966c04fSmrg    return (ErrorStatus);
128a966c04fSmrg}
129a966c04fSmrg#endif /* CXPMPROG */
130a966c04fSmrg
131a966c04fSmrg#ifndef NO_ZPIPE
132a966c04fSmrg/* Do not depend on errno after read_through */
133a966c04fSmrgFILE*
1342e2dd055SmrgxpmPipeThrough(
1352e2dd055Smrg    int		 fd,
1362e2dd055Smrg    const char	*cmd,
1372e2dd055Smrg    const char	*arg1,
1382e2dd055Smrg    const char	*mode)
139a966c04fSmrg{
140a966c04fSmrg    FILE* fp;
141a966c04fSmrg    int status, fds[2], in = 0, out = 1;
142a966c04fSmrg    pid_t pid;
143a966c04fSmrg    if ( 'w' == *mode )
144a966c04fSmrg	out = 0, in = 1;
145a966c04fSmrg    if ( pipe(fds) < 0 )
146a966c04fSmrg	return NULL;
147a966c04fSmrg    pid = fork();
148a966c04fSmrg    if ( pid < 0 )
149a966c04fSmrg	goto fail1;
150a966c04fSmrg    if ( 0 == pid )
151a966c04fSmrg    {
152a966c04fSmrg	close(fds[in]);
153a966c04fSmrg	if ( dup2(fds[out], out) < 0 )
154a966c04fSmrg	    goto err;
155a966c04fSmrg	close(fds[out]);
156a966c04fSmrg	if ( dup2(fd, in) < 0 )
157a966c04fSmrg	    goto err;
158a966c04fSmrg	close(fd);
159a966c04fSmrg	pid = fork();
160a966c04fSmrg	if ( pid < 0 )
161a966c04fSmrg	    goto err;
162a966c04fSmrg	if ( 0 == pid )
163a966c04fSmrg	{
164a966c04fSmrg	    execlp(cmd, cmd, arg1, (char *)NULL);
165a966c04fSmrg	    perror(cmd);
166a966c04fSmrg	    goto err;
167a966c04fSmrg	}
168a966c04fSmrg	_exit(0);
169a966c04fSmrg    err:
170a966c04fSmrg	_exit(1);
171a966c04fSmrg    }
172a966c04fSmrg    close(fds[out]);
173a966c04fSmrg    /* calling process: wait for first child */
174a966c04fSmrg    while ( waitpid(pid, &status, 0) < 0 && EINTR == errno )
175a966c04fSmrg	;
176a966c04fSmrg    if ( WIFSIGNALED(status) ||
177a966c04fSmrg	 (WIFEXITED(status) && WEXITSTATUS(status) != 0) )
178a966c04fSmrg	goto fail2;
179a966c04fSmrg    fp = fdopen(fds[in], mode);
180a966c04fSmrg    if ( !fp )
181a966c04fSmrg	goto fail2;
182a966c04fSmrg    close(fd); /* still open in 2nd child */
183a966c04fSmrg    return fp;
184a966c04fSmrgfail1:
185a966c04fSmrg    close(fds[out]);
186a966c04fSmrgfail2:
187a966c04fSmrg    close(fds[in]);
188a966c04fSmrg    return NULL;
189a966c04fSmrg}
190a966c04fSmrg#endif
191a966c04fSmrg
192a966c04fSmrg/*
193a966c04fSmrg * open the given file to be read as an xpmData which is returned.
194a966c04fSmrg */
195a966c04fSmrgstatic int
1962e2dd055SmrgOpenReadFile(
197ac92798bSmrg    const char	*filename,
1982e2dd055Smrg    xpmData	*mdata)
199a966c04fSmrg{
200a966c04fSmrg    if (!filename) {
201a966c04fSmrg	mdata->stream.file = (stdin);
202a966c04fSmrg	mdata->type = XPMFILE;
203a966c04fSmrg    } else {
204a966c04fSmrg	int fd = open(filename, O_RDONLY);
205a966c04fSmrg#if defined(NO_ZPIPE)
206a966c04fSmrg	if ( fd < 0 )
207a966c04fSmrg	    return XpmOpenFailed;
208a966c04fSmrg#else
209a966c04fSmrg	const char* ext = NULL;
210a966c04fSmrg	if ( fd >= 0 )
211a966c04fSmrg	    ext = strrchr(filename, '.');
212a966c04fSmrg#ifdef STAT_ZFILE /* searching for z-files if the given name not found */
213a966c04fSmrg	else
214a966c04fSmrg	{
215a966c04fSmrg	    size_t len = strlen(filename);
216a966c04fSmrg	    char *compressfile = (char *) XpmMalloc(len + 4);
217a966c04fSmrg	    if ( !compressfile )
218a966c04fSmrg		return (XpmNoMemory);
219a966c04fSmrg	    strcpy(compressfile, filename);
220a966c04fSmrg	    strcpy(compressfile + len, ext = ".Z");
221a966c04fSmrg	    fd = open(compressfile, O_RDONLY);
222a966c04fSmrg	    if ( fd < 0 )
223a966c04fSmrg	    {
224a966c04fSmrg		strcpy(compressfile + len, ext = ".gz");
225a966c04fSmrg		fd = open(compressfile, O_RDONLY);
226a966c04fSmrg		if ( fd < 0 )
227a966c04fSmrg		{
228a966c04fSmrg		    XpmFree(compressfile);
229a966c04fSmrg		    return XpmOpenFailed;
230a966c04fSmrg		}
231a966c04fSmrg	    }
232a966c04fSmrg	    XpmFree(compressfile);
233a966c04fSmrg	}
234a966c04fSmrg#endif
235a966c04fSmrg	if ( ext && !strcmp(ext, ".Z") )
236a966c04fSmrg	{
237a966c04fSmrg	    mdata->type = XPMPIPE;
238a966c04fSmrg	    mdata->stream.file = xpmPipeThrough(fd, "uncompress", "-c", "r");
239a966c04fSmrg	}
240a966c04fSmrg	else if ( ext && !strcmp(ext, ".gz") )
241a966c04fSmrg	{
242a966c04fSmrg	    mdata->type = XPMPIPE;
243a966c04fSmrg	    mdata->stream.file = xpmPipeThrough(fd, "gunzip", "-qc", "r");
244a966c04fSmrg	}
245a966c04fSmrg	else
246a966c04fSmrg#endif /* z-files */
247a966c04fSmrg	{
248a966c04fSmrg	    mdata->type = XPMFILE;
249a966c04fSmrg	    mdata->stream.file = fdopen(fd, "r");
250a966c04fSmrg	}
251a966c04fSmrg	if (!mdata->stream.file)
252a966c04fSmrg	{
253a966c04fSmrg	    close(fd);
254a966c04fSmrg	    return (XpmOpenFailed);
255a966c04fSmrg	}
256a966c04fSmrg    }
257a966c04fSmrg    mdata->CommentLength = 0;
258a966c04fSmrg#ifdef CXPMPROG
259a966c04fSmrg    mdata->lineNum = 0;
260a966c04fSmrg    mdata->charNum = 0;
261a966c04fSmrg#endif
262a966c04fSmrg    return (XpmSuccess);
263a966c04fSmrg}
264a966c04fSmrg
265a966c04fSmrg/*
266a966c04fSmrg * close the file related to the xpmData if any
267a966c04fSmrg */
268a966c04fSmrgstatic void
2692e2dd055SmrgxpmDataClose(xpmData *mdata)
270a966c04fSmrg{
271a966c04fSmrg    if (mdata->stream.file != (stdin))
272a966c04fSmrg	fclose(mdata->stream.file);
273a966c04fSmrg}
274