cxpm.c revision 97cf2ee2
1/*
2 * Copyright (C) 1998 Arnaud LE HORS
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * Arnaud LE HORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Except as contained in this notice, the name of Arnaud LE HORS shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from Arnaud LE HORS.
24 */
25
26/*****************************************************************************\
27* cxpm.c:                                                                     *
28*                                                                             *
29*  Check XPM File program                                                     *
30*                                                                             *
31*  Developed by Arnaud Le Hors                                                *
32\*****************************************************************************/
33
34#define CXPMPROG
35
36#ifdef HAVE_CONFIG_H
37#include <config.h>
38#endif
39#include "XpmI.h"
40#ifdef USE_GETTEXT
41#include <locale.h>
42#include <libintl.h>
43#else
44#define gettext(a) (a)
45#endif
46
47#undef xpmGetC
48#define xpmGetC(data) sGetc(data, data->stream.file)
49#define Getc sGetc
50#define Ungetc sUngetc
51
52
53/*
54 * special getc and ungetc counting read lines and characters
55 * note that 's' could stand both for "special" and "slow" ;-)
56 */
57static int
58sGetc(xpmData *data, FILE *file)
59{
60    int c = getc(data->stream.file);
61    if (c == '\n') {
62        data->lineNum++;
63        data->charNum = 0;
64    } else {
65        data->charNum++;
66    }
67    return c;
68}
69
70static void
71sUngetc(xpmData *data, int c, FILE *file)
72{
73    ungetc(c, data->stream.file);
74    if (c == '\n') {
75        data->lineNum--;
76        data->charNum = 0;
77    } else {
78        data->charNum--;
79    }
80}
81
82/* include all the code we need (yeah, I know, quite ugly...) */
83#include "data.c"
84#include "parse.c"
85#include "RdFToI.c"	/* only for OpenReadFile and xpmDataClose */
86#include "hashtab.c"
87#include "misc.c"
88#include "Attrib.c"
89#include "Image.c"
90
91static void
92ErrorMessage(
93    int ErrorStatus,
94    xpmData *data)
95
96{
97    char *error = NULL;
98
99    switch (ErrorStatus) {
100    case XpmSuccess:
101	return;
102    case XpmOpenFailed:
103	/* L10N_Comments : Error produced when filename does not exist
104	   or insufficient permissions to open (i.e. cxpm /no/such/file ) */
105	error = gettext("Cannot open file");
106	break;
107    case XpmFileInvalid:
108	/* L10N_Comments : Error produced when filename can be read, but
109	   is not an XPM file (i.e. cxpm /dev/null ) */
110	error = gettext("Invalid XPM file");
111	break;
112    case XpmNoMemory:
113	/* L10N_Comments : Error produced when filename can be read, but
114	   is too big for memory
115	   (i.e. limit datasize 32 ; cxpm /usr/dt/backdrops/Crochet.pm ) */
116	error = gettext("Not enough memory");
117	break;
118    case XpmColorFailed:
119	/* L10N_Comments : Error produced when filename can be read, but
120	   contains an invalid color specification (need to create test case)*/
121	error = gettext("Failed to parse color");
122	break;
123    }
124
125    if (error) {
126	/* L10N_Comments : Wrapper around above Xpm errors - %s is
127	   replaced with the contents of the error message retrieved above */
128	fprintf(stderr, gettext("Xpm Error: %s.\n"), error);
129	if (ErrorStatus == XpmFileInvalid && data)
130	/* L10N_Comments : Error produced when filename can be read, but
131	   is not an XPM file (i.e. cxpm /dev/null ) */
132	  fprintf(stderr, gettext("Error found line %d near character %d\n"),
133		  data->lineNum + 1,
134		  data->charNum + 1);
135	exit(1);
136    }
137}
138
139int
140main(int argc, char **argv)
141{
142    XpmImage image;
143    char *filename;
144    int ErrorStatus;
145    xpmData data;
146
147#ifdef USE_GETTEXT
148    setlocale(LC_ALL,"");
149    bindtextdomain("cxpm",LOCALEDIR);
150    textdomain("cxpm");
151#endif
152
153    if (argc > 1) {
154        if (!strcmp(argv[1], "-?") || !strncmp(argv[1], "-h", 2)) {
155	    /* L10N_Comments : Usage message produced by running cxpm -h
156		%s will be replaced by argv[0] (program name) */
157	    fprintf(stderr, gettext("Usage: %s [filename]\n"), argv[0]);
158	    exit(1);
159	}
160        filename = argv[1];
161    } else {
162        filename = NULL;
163    }
164
165    xpmInitXpmImage(&image);
166
167    if ((ErrorStatus = OpenReadFile(filename, &data)) != XpmSuccess)
168	ErrorMessage(ErrorStatus, NULL);
169
170    ErrorStatus = xpmParseData(&data, &image, NULL);
171    ErrorMessage(ErrorStatus, &data);
172
173    xpmDataClose(&data);
174    XpmFreeXpmImage(&image);
175
176    exit(0);
177}
178