1a966c04fSmrg/*
2a966c04fSmrg * Copyright (C) 1998 Arnaud LE HORS
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 * Arnaud LE HORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18a966c04fSmrg * IN 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 Arnaud LE HORS 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 Arnaud LE HORS.
24a966c04fSmrg */
25a966c04fSmrg
26a966c04fSmrg/*****************************************************************************\
27a966c04fSmrg* cxpm.c:                                                                     *
28a966c04fSmrg*                                                                             *
29a966c04fSmrg*  Check XPM File program                                                     *
30a966c04fSmrg*                                                                             *
31a966c04fSmrg*  Developed by Arnaud Le Hors                                                *
32a966c04fSmrg\*****************************************************************************/
33a966c04fSmrg
34a966c04fSmrg#define CXPMPROG
35a966c04fSmrg
36a966c04fSmrg#ifdef HAVE_CONFIG_H
37a966c04fSmrg#include <config.h>
38a966c04fSmrg#endif
39a966c04fSmrg#include "XpmI.h"
40a966c04fSmrg#ifdef USE_GETTEXT
41a966c04fSmrg#include <locale.h>
42a966c04fSmrg#include <libintl.h>
43a966c04fSmrg#else
44a966c04fSmrg#define gettext(a) (a)
45a966c04fSmrg#endif
46a966c04fSmrg
47a966c04fSmrg#undef xpmGetC
48a966c04fSmrg#define xpmGetC(data) sGetc(data, data->stream.file)
49a966c04fSmrg#define Getc sGetc
50a966c04fSmrg#define Ungetc sUngetc
51a966c04fSmrg
52a966c04fSmrg
53a966c04fSmrg/*
54a966c04fSmrg * special getc and ungetc counting read lines and characters
55a966c04fSmrg * note that 's' could stand both for "special" and "slow" ;-)
56a966c04fSmrg */
57a966c04fSmrgstatic int
582e2dd055SmrgsGetc(xpmData *data, FILE *file)
59a966c04fSmrg{
60a966c04fSmrg    int c = getc(data->stream.file);
61a966c04fSmrg    if (c == '\n') {
62a966c04fSmrg        data->lineNum++;
63a966c04fSmrg        data->charNum = 0;
641c235774Smrg    } else if (c != EOF) {
65a966c04fSmrg        data->charNum++;
66a966c04fSmrg    }
67a966c04fSmrg    return c;
68a966c04fSmrg}
69a966c04fSmrg
70a966c04fSmrgstatic void
712e2dd055SmrgsUngetc(xpmData *data, int c, FILE *file)
72a966c04fSmrg{
73a966c04fSmrg    ungetc(c, data->stream.file);
74a966c04fSmrg    if (c == '\n') {
75a966c04fSmrg        data->lineNum--;
76a966c04fSmrg        data->charNum = 0;
771c235774Smrg    } else if (c != EOF) {
78a966c04fSmrg        data->charNum--;
79a966c04fSmrg    }
80a966c04fSmrg}
81a966c04fSmrg
82a966c04fSmrg/* include all the code we need (yeah, I know, quite ugly...) */
83a966c04fSmrg#include "data.c"
84a966c04fSmrg#include "parse.c"
85a966c04fSmrg#include "RdFToI.c"	/* only for OpenReadFile and xpmDataClose */
86a966c04fSmrg#include "hashtab.c"
87a966c04fSmrg#include "misc.c"
88a966c04fSmrg#include "Attrib.c"
89a966c04fSmrg#include "Image.c"
90a966c04fSmrg
912e2dd055Smrgstatic void
92a966c04fSmrgErrorMessage(
93a966c04fSmrg    int ErrorStatus,
94a966c04fSmrg    xpmData *data)
95a966c04fSmrg
96a966c04fSmrg{
97a966c04fSmrg    char *error = NULL;
98a966c04fSmrg
99a966c04fSmrg    switch (ErrorStatus) {
100a966c04fSmrg    case XpmSuccess:
101a966c04fSmrg	return;
102a966c04fSmrg    case XpmOpenFailed:
10397cf2ee2Smrg	/* L10N_Comments : Error produced when filename does not exist
104a966c04fSmrg	   or insufficient permissions to open (i.e. cxpm /no/such/file ) */
105a966c04fSmrg	error = gettext("Cannot open file");
106a966c04fSmrg	break;
107a966c04fSmrg    case XpmFileInvalid:
108a966c04fSmrg	/* L10N_Comments : Error produced when filename can be read, but
109a966c04fSmrg	   is not an XPM file (i.e. cxpm /dev/null ) */
110a966c04fSmrg	error = gettext("Invalid XPM file");
111a966c04fSmrg	break;
112a966c04fSmrg    case XpmNoMemory:
113a966c04fSmrg	/* L10N_Comments : Error produced when filename can be read, but
11497cf2ee2Smrg	   is too big for memory
115a966c04fSmrg	   (i.e. limit datasize 32 ; cxpm /usr/dt/backdrops/Crochet.pm ) */
116a966c04fSmrg	error = gettext("Not enough memory");
117a966c04fSmrg	break;
118a966c04fSmrg    case XpmColorFailed:
119a966c04fSmrg	/* L10N_Comments : Error produced when filename can be read, but
120a966c04fSmrg	   contains an invalid color specification (need to create test case)*/
121a966c04fSmrg	error = gettext("Failed to parse color");
122a966c04fSmrg	break;
123a966c04fSmrg    }
124a966c04fSmrg
125a966c04fSmrg    if (error) {
126a966c04fSmrg	/* L10N_Comments : Wrapper around above Xpm errors - %s is
127a966c04fSmrg	   replaced with the contents of the error message retrieved above */
128a966c04fSmrg	fprintf(stderr, gettext("Xpm Error: %s.\n"), error);
129a966c04fSmrg	if (ErrorStatus == XpmFileInvalid && data)
130a966c04fSmrg	/* L10N_Comments : Error produced when filename can be read, but
131a966c04fSmrg	   is not an XPM file (i.e. cxpm /dev/null ) */
132a966c04fSmrg	  fprintf(stderr, gettext("Error found line %d near character %d\n"),
133a966c04fSmrg		  data->lineNum + 1,
134a966c04fSmrg		  data->charNum + 1);
135a966c04fSmrg	exit(1);
136a966c04fSmrg    }
137a966c04fSmrg}
138a966c04fSmrg
139a966c04fSmrgint
1402e2dd055Smrgmain(int argc, char **argv)
141a966c04fSmrg{
142a966c04fSmrg    XpmImage image;
143a966c04fSmrg    char *filename;
144a966c04fSmrg    int ErrorStatus;
145a966c04fSmrg    xpmData data;
146a966c04fSmrg
147a966c04fSmrg#ifdef USE_GETTEXT
148a966c04fSmrg    setlocale(LC_ALL,"");
149a966c04fSmrg    bindtextdomain("cxpm",LOCALEDIR);
150a966c04fSmrg    textdomain("cxpm");
151a966c04fSmrg#endif
152a966c04fSmrg
153a966c04fSmrg    if (argc > 1) {
154a966c04fSmrg        if (!strcmp(argv[1], "-?") || !strncmp(argv[1], "-h", 2)) {
155a966c04fSmrg	    /* L10N_Comments : Usage message produced by running cxpm -h
156a966c04fSmrg		%s will be replaced by argv[0] (program name) */
157a966c04fSmrg	    fprintf(stderr, gettext("Usage: %s [filename]\n"), argv[0]);
158a966c04fSmrg	    exit(1);
159a966c04fSmrg	}
160a966c04fSmrg        filename = argv[1];
161a966c04fSmrg    } else {
162a966c04fSmrg        filename = NULL;
163a966c04fSmrg    }
164a966c04fSmrg
165a966c04fSmrg    xpmInitXpmImage(&image);
166a966c04fSmrg
167a966c04fSmrg    if ((ErrorStatus = OpenReadFile(filename, &data)) != XpmSuccess)
168a966c04fSmrg	ErrorMessage(ErrorStatus, NULL);
169a966c04fSmrg
170a966c04fSmrg    ErrorStatus = xpmParseData(&data, &image, NULL);
171a966c04fSmrg    ErrorMessage(ErrorStatus, &data);
172a966c04fSmrg
173a966c04fSmrg    xpmDataClose(&data);
174a966c04fSmrg    XpmFreeXpmImage(&image);
175a966c04fSmrg
176a966c04fSmrg    exit(0);
177a966c04fSmrg}
178