RdFToI.c revision a966c04f
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/* $XFree86$ */ 35a966c04fSmrg 36a966c04fSmrg/* October 2004, source code review by Thomas Biege <thomas@suse.de> */ 37a966c04fSmrg 38a966c04fSmrg#ifdef HAVE_CONFIG_H 39a966c04fSmrg#include <config.h> 40a966c04fSmrg#endif 41a966c04fSmrg#include "XpmI.h" 42a966c04fSmrg#ifndef NO_ZPIPE 43a966c04fSmrg#include <fcntl.h> 44a966c04fSmrg#include <errno.h> 45a966c04fSmrg#include <sys/types.h> 46a966c04fSmrg#include <sys/wait.h> 47a966c04fSmrg#else 48a966c04fSmrg#ifdef FOR_MSW 49a966c04fSmrg#include <fcntl.h> 50a966c04fSmrg#endif 51a966c04fSmrg#endif 52a966c04fSmrg 53a966c04fSmrgLFUNC(OpenReadFile, int, (char *filename, xpmData *mdata)); 54a966c04fSmrgLFUNC(xpmDataClose, void, (xpmData *mdata)); 55a966c04fSmrg 56a966c04fSmrg#ifndef CXPMPROG 57a966c04fSmrgint 58a966c04fSmrgXpmReadFileToImage(display, filename, 59a966c04fSmrg image_return, shapeimage_return, attributes) 60a966c04fSmrg Display *display; 61a966c04fSmrg char *filename; 62a966c04fSmrg XImage **image_return; 63a966c04fSmrg XImage **shapeimage_return; 64a966c04fSmrg XpmAttributes *attributes; 65a966c04fSmrg{ 66a966c04fSmrg XpmImage image; 67a966c04fSmrg XpmInfo info; 68a966c04fSmrg int ErrorStatus; 69a966c04fSmrg xpmData mdata; 70a966c04fSmrg 71a966c04fSmrg xpmInitXpmImage(&image); 72a966c04fSmrg xpmInitXpmInfo(&info); 73a966c04fSmrg 74a966c04fSmrg /* open file to read */ 75a966c04fSmrg if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess) 76a966c04fSmrg return (ErrorStatus); 77a966c04fSmrg 78a966c04fSmrg /* create the XImage from the XpmData */ 79a966c04fSmrg if (attributes) { 80a966c04fSmrg xpmInitAttributes(attributes); 81a966c04fSmrg xpmSetInfoMask(&info, attributes); 82a966c04fSmrg ErrorStatus = xpmParseDataAndCreate(display, &mdata, 83a966c04fSmrg image_return, shapeimage_return, 84a966c04fSmrg &image, &info, attributes); 85a966c04fSmrg } else 86a966c04fSmrg ErrorStatus = xpmParseDataAndCreate(display, &mdata, 87a966c04fSmrg image_return, shapeimage_return, 88a966c04fSmrg &image, NULL, attributes); 89a966c04fSmrg if (attributes) { 90a966c04fSmrg if (ErrorStatus >= 0) /* no fatal error */ 91a966c04fSmrg xpmSetAttributes(attributes, &image, &info); 92a966c04fSmrg XpmFreeXpmInfo(&info); 93a966c04fSmrg } 94a966c04fSmrg 95a966c04fSmrg xpmDataClose(&mdata); 96a966c04fSmrg /* free the XpmImage */ 97a966c04fSmrg XpmFreeXpmImage(&image); 98a966c04fSmrg 99a966c04fSmrg return (ErrorStatus); 100a966c04fSmrg} 101a966c04fSmrg 102a966c04fSmrgint 103a966c04fSmrgXpmReadFileToXpmImage(filename, image, info) 104a966c04fSmrg char *filename; 105a966c04fSmrg XpmImage *image; 106a966c04fSmrg XpmInfo *info; 107a966c04fSmrg{ 108a966c04fSmrg xpmData mdata; 109a966c04fSmrg int ErrorStatus; 110a966c04fSmrg 111a966c04fSmrg /* init returned values */ 112a966c04fSmrg xpmInitXpmImage(image); 113a966c04fSmrg xpmInitXpmInfo(info); 114a966c04fSmrg 115a966c04fSmrg /* open file to read */ 116a966c04fSmrg if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess) 117a966c04fSmrg return (ErrorStatus); 118a966c04fSmrg 119a966c04fSmrg /* create the XpmImage from the XpmData */ 120a966c04fSmrg ErrorStatus = xpmParseData(&mdata, image, info); 121a966c04fSmrg 122a966c04fSmrg xpmDataClose(&mdata); 123a966c04fSmrg 124a966c04fSmrg return (ErrorStatus); 125a966c04fSmrg} 126a966c04fSmrg#endif /* CXPMPROG */ 127a966c04fSmrg 128a966c04fSmrg#ifndef NO_ZPIPE 129a966c04fSmrg/* Do not depend on errno after read_through */ 130a966c04fSmrgFILE* 131a966c04fSmrgxpmPipeThrough(fd, cmd, arg1, mode) 132a966c04fSmrg int fd; 133a966c04fSmrg const char* cmd; 134a966c04fSmrg const char* arg1; 135a966c04fSmrg const char* mode; 136a966c04fSmrg{ 137a966c04fSmrg FILE* fp; 138a966c04fSmrg int status, fds[2], in = 0, out = 1; 139a966c04fSmrg pid_t pid; 140a966c04fSmrg if ( 'w' == *mode ) 141a966c04fSmrg out = 0, in = 1; 142a966c04fSmrg if ( pipe(fds) < 0 ) 143a966c04fSmrg return NULL; 144a966c04fSmrg pid = fork(); 145a966c04fSmrg if ( pid < 0 ) 146a966c04fSmrg goto fail1; 147a966c04fSmrg if ( 0 == pid ) 148a966c04fSmrg { 149a966c04fSmrg close(fds[in]); 150a966c04fSmrg if ( dup2(fds[out], out) < 0 ) 151a966c04fSmrg goto err; 152a966c04fSmrg close(fds[out]); 153a966c04fSmrg if ( dup2(fd, in) < 0 ) 154a966c04fSmrg goto err; 155a966c04fSmrg close(fd); 156a966c04fSmrg pid = fork(); 157a966c04fSmrg if ( pid < 0 ) 158a966c04fSmrg goto err; 159a966c04fSmrg if ( 0 == pid ) 160a966c04fSmrg { 161a966c04fSmrg execlp(cmd, cmd, arg1, (char *)NULL); 162a966c04fSmrg perror(cmd); 163a966c04fSmrg goto err; 164a966c04fSmrg } 165a966c04fSmrg _exit(0); 166a966c04fSmrg err: 167a966c04fSmrg _exit(1); 168a966c04fSmrg } 169a966c04fSmrg close(fds[out]); 170a966c04fSmrg /* calling process: wait for first child */ 171a966c04fSmrg while ( waitpid(pid, &status, 0) < 0 && EINTR == errno ) 172a966c04fSmrg ; 173a966c04fSmrg if ( WIFSIGNALED(status) || 174a966c04fSmrg (WIFEXITED(status) && WEXITSTATUS(status) != 0) ) 175a966c04fSmrg goto fail2; 176a966c04fSmrg fp = fdopen(fds[in], mode); 177a966c04fSmrg if ( !fp ) 178a966c04fSmrg goto fail2; 179a966c04fSmrg close(fd); /* still open in 2nd child */ 180a966c04fSmrg return fp; 181a966c04fSmrgfail1: 182a966c04fSmrg close(fds[out]); 183a966c04fSmrgfail2: 184a966c04fSmrg close(fds[in]); 185a966c04fSmrg return NULL; 186a966c04fSmrg} 187a966c04fSmrg#endif 188a966c04fSmrg 189a966c04fSmrg/* 190a966c04fSmrg * open the given file to be read as an xpmData which is returned. 191a966c04fSmrg */ 192a966c04fSmrgstatic int 193a966c04fSmrgOpenReadFile(filename, mdata) 194a966c04fSmrg char *filename; 195a966c04fSmrg xpmData *mdata; 196a966c04fSmrg{ 197a966c04fSmrg if (!filename) { 198a966c04fSmrg mdata->stream.file = (stdin); 199a966c04fSmrg mdata->type = XPMFILE; 200a966c04fSmrg } else { 201a966c04fSmrg int fd = open(filename, O_RDONLY); 202a966c04fSmrg#if defined(NO_ZPIPE) 203a966c04fSmrg if ( fd < 0 ) 204a966c04fSmrg return XpmOpenFailed; 205a966c04fSmrg#else 206a966c04fSmrg const char* ext = NULL; 207a966c04fSmrg if ( fd >= 0 ) 208a966c04fSmrg ext = strrchr(filename, '.'); 209a966c04fSmrg#ifdef STAT_ZFILE /* searching for z-files if the given name not found */ 210a966c04fSmrg else 211a966c04fSmrg { 212a966c04fSmrg size_t len = strlen(filename); 213a966c04fSmrg char *compressfile = (char *) XpmMalloc(len + 4); 214a966c04fSmrg if ( !compressfile ) 215a966c04fSmrg return (XpmNoMemory); 216a966c04fSmrg strcpy(compressfile, filename); 217a966c04fSmrg strcpy(compressfile + len, ext = ".Z"); 218a966c04fSmrg fd = open(compressfile, O_RDONLY); 219a966c04fSmrg if ( fd < 0 ) 220a966c04fSmrg { 221a966c04fSmrg strcpy(compressfile + len, ext = ".gz"); 222a966c04fSmrg fd = open(compressfile, O_RDONLY); 223a966c04fSmrg if ( fd < 0 ) 224a966c04fSmrg { 225a966c04fSmrg XpmFree(compressfile); 226a966c04fSmrg return XpmOpenFailed; 227a966c04fSmrg } 228a966c04fSmrg } 229a966c04fSmrg XpmFree(compressfile); 230a966c04fSmrg } 231a966c04fSmrg#endif 232a966c04fSmrg if ( ext && !strcmp(ext, ".Z") ) 233a966c04fSmrg { 234a966c04fSmrg mdata->type = XPMPIPE; 235a966c04fSmrg mdata->stream.file = xpmPipeThrough(fd, "uncompress", "-c", "r"); 236a966c04fSmrg } 237a966c04fSmrg else if ( ext && !strcmp(ext, ".gz") ) 238a966c04fSmrg { 239a966c04fSmrg mdata->type = XPMPIPE; 240a966c04fSmrg mdata->stream.file = xpmPipeThrough(fd, "gunzip", "-qc", "r"); 241a966c04fSmrg } 242a966c04fSmrg else 243a966c04fSmrg#endif /* z-files */ 244a966c04fSmrg { 245a966c04fSmrg mdata->type = XPMFILE; 246a966c04fSmrg mdata->stream.file = fdopen(fd, "r"); 247a966c04fSmrg } 248a966c04fSmrg if (!mdata->stream.file) 249a966c04fSmrg { 250a966c04fSmrg close(fd); 251a966c04fSmrg return (XpmOpenFailed); 252a966c04fSmrg } 253a966c04fSmrg } 254a966c04fSmrg mdata->CommentLength = 0; 255a966c04fSmrg#ifdef CXPMPROG 256a966c04fSmrg mdata->lineNum = 0; 257a966c04fSmrg mdata->charNum = 0; 258a966c04fSmrg#endif 259a966c04fSmrg return (XpmSuccess); 260a966c04fSmrg} 261a966c04fSmrg 262a966c04fSmrg/* 263a966c04fSmrg * close the file related to the xpmData if any 264a966c04fSmrg */ 265a966c04fSmrgstatic void 266a966c04fSmrgxpmDataClose(mdata) 267a966c04fSmrg xpmData *mdata; 268a966c04fSmrg{ 269a966c04fSmrg if (mdata->stream.file != (stdin)) 270a966c04fSmrg fclose(mdata->stream.file); 271a966c04fSmrg} 272