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