cxpm.c revision 2e2dd055
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(xpmData *data, FILE *file)
60{
61    int c = getc(data->stream.file);
62    if (c == '\n') {
63        data->lineNum++;
64        data->charNum = 0;
65    } else {
66        data->charNum++;
67    }
68    return c;
69}
70
71static void
72sUngetc(xpmData *data, int c, FILE *file)
73{
74    ungetc(c, data->stream.file);
75    if (c == '\n') {
76        data->lineNum--;
77        data->charNum = 0;
78    } else {
79        data->charNum--;
80    }
81}
82
83/* include all the code we need (yeah, I know, quite ugly...) */
84#include "data.c"
85#include "parse.c"
86#include "RdFToI.c"	/* only for OpenReadFile and xpmDataClose */
87#include "hashtab.c"
88#include "misc.c"
89#include "Attrib.c"
90#include "Image.c"
91
92static void
93ErrorMessage(
94    int ErrorStatus,
95    xpmData *data)
96
97{
98    char *error = NULL;
99
100    switch (ErrorStatus) {
101    case XpmSuccess:
102	return;
103    case XpmOpenFailed:
104	/* L10N_Comments : Error produced when filename does not exist
105	   or insufficient permissions to open (i.e. cxpm /no/such/file ) */
106	error = gettext("Cannot open file");
107	break;
108    case XpmFileInvalid:
109	/* L10N_Comments : Error produced when filename can be read, but
110	   is not an XPM file (i.e. cxpm /dev/null ) */
111	error = gettext("Invalid XPM file");
112	break;
113    case XpmNoMemory:
114	/* L10N_Comments : Error produced when filename can be read, but
115	   is too big for memory
116	   (i.e. limit datasize 32 ; cxpm /usr/dt/backdrops/Crochet.pm ) */
117	error = gettext("Not enough memory");
118	break;
119    case XpmColorFailed:
120	/* L10N_Comments : Error produced when filename can be read, but
121	   contains an invalid color specification (need to create test case)*/
122	error = gettext("Failed to parse color");
123	break;
124    }
125
126    if (error) {
127	/* L10N_Comments : Wrapper around above Xpm errors - %s is
128	   replaced with the contents of the error message retrieved above */
129	fprintf(stderr, gettext("Xpm Error: %s.\n"), error);
130	if (ErrorStatus == XpmFileInvalid && data)
131	/* L10N_Comments : Error produced when filename can be read, but
132	   is not an XPM file (i.e. cxpm /dev/null ) */
133	  fprintf(stderr, gettext("Error found line %d near character %d\n"),
134		  data->lineNum + 1,
135		  data->charNum + 1);
136	exit(1);
137    }
138}
139
140int
141main(int argc, char **argv)
142{
143    XpmImage image;
144    char *filename;
145    int ErrorStatus;
146    xpmData data;
147
148#ifdef USE_GETTEXT
149    setlocale(LC_ALL,"");
150    bindtextdomain("cxpm",LOCALEDIR);
151    textdomain("cxpm");
152#endif
153
154    if (argc > 1) {
155        if (!strcmp(argv[1], "-?") || !strncmp(argv[1], "-h", 2)) {
156	    /* L10N_Comments : Usage message produced by running cxpm -h
157		%s will be replaced by argv[0] (program name) */
158	    fprintf(stderr, gettext("Usage: %s [filename]\n"), argv[0]);
159	    exit(1);
160	}
161        filename = argv[1];
162    } else {
163        filename = NULL;
164    }
165
166    xpmInitXpmImage(&image);
167
168    if ((ErrorStatus = OpenReadFile(filename, &data)) != XpmSuccess)
169	ErrorMessage(ErrorStatus, NULL);
170
171    ErrorStatus = xpmParseData(&data, &image, NULL);
172    ErrorMessage(ErrorStatus, &data);
173
174    xpmDataClose(&data);
175    XpmFreeXpmImage(&image);
176
177    exit(0);
178}
179