2 * Copyright (C) 1989-95 GROUPE BULL
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * 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.
21 * Except as contained in this notice, the name of GROUPE BULL 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 GROUPE BULL.
26 /*****************************************************************************\
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
35 /* October 2004, source code review by Thomas Biege <thomas@suse.de> */
39 /* Official version number */
40 static char *RCS_Version = "$XpmVersion: 3.4k $";
42 /* Internal version number */
43 static char *RCS_Id = "Id: xpm.shar,v 3.71 1998/03/19 19:47:14 lehors Exp $";
53 #define Getc(data, file) getc(file)
54 #define Ungetc(data, c, file) ungetc(c, file)
58 ParseComment(xpmData *data)
60 if (data->type == XPMBUFFER) {
62 register unsigned int n = 0;
70 /* skip the string beginning comment */
77 } while (c == *s2 && *s2 != '\0' && c);
80 /* this wasn't the beginning of a comment */
85 data->Comment[0] = *s;
91 while (*s != *s2 && c) {
93 if (n == XPMMAXCMTLEN - 1) { /* forget it */
100 data->CommentLength = n;
103 if (n == XPMMAXCMTLEN - 1) { /* forget it */
110 } while (c == *s2 && *s2 != '\0' && c);
112 /* this is the end of the comment */
119 FILE *file = data->stream.file;
121 register unsigned int n = 0, a;
129 /* skip the string beginning comment */
132 c = Getc(data, file);
136 } while (c == *s2 && *s2 != '\0' && c != EOF);
139 /* this wasn't the beginning of a comment */
140 /* put characters back in the order that we got them */
141 for (a = n; a > 0; a--, s--)
142 Ungetc(data, *s, file);
146 data->Comment[0] = *s;
152 while (*s != *s2 && c != EOF) {
153 c = Getc(data, file);
154 if (n == XPMMAXCMTLEN - 1) { /* forget it */
161 data->CommentLength = n;
163 c = Getc(data, file);
164 if (n == XPMMAXCMTLEN - 1) { /* forget it */
171 } while (c == *s2 && *s2 != '\0' && c != EOF);
173 /* this is the end of the comment */
175 Ungetc(data, *s, file);
183 * skip to the end of the current string and the beginning of the next one
186 xpmNextString(xpmData *data)
189 data->cptr = (data->stream.data)[++data->line];
190 else if (data->type == XPMBUFFER) {
193 /* get to the end of the current string */
195 while ((c = *data->cptr++) && c != data->Eos);
198 * then get to the beginning of the next string looking for possible
202 while ((c = *data->cptr++) && c != data->Bos)
203 if (data->Bcmt && c == data->Bcmt[0])
205 } else if (data->Bcmt) { /* XPM2 natural */
206 while ((c = *data->cptr++) == data->Bcmt[0])
212 FILE *file = data->stream.file;
214 /* get to the end of the current string */
216 while ((c = Getc(data, file)) != data->Eos && c != EOF);
219 * then get to the beginning of the next string looking for possible
223 while ((c = Getc(data, file)) != data->Bos && c != EOF)
224 if (data->Bcmt && c == data->Bcmt[0])
227 } else if (data->Bcmt) { /* XPM2 natural */
228 while ((c = Getc(data, file)) == data->Bcmt[0])
230 Ungetc(data, c, file);
238 * skip whitespace and return the following word
246 register unsigned int n = 0;
249 if (!data->type || data->type == XPMBUFFER) {
250 while (isspace(c = *data->cptr) && c != data->Eos)
256 } while (!isspace(c) && c != data->Eos && n < buflen);
260 FILE *file = data->stream.file;
262 while ((c = Getc(data, file)) != EOF && isspace(c) && c != data->Eos);
263 while (!isspace(c) && c != data->Eos && c != EOF && n < buflen) {
266 c = Getc(data, file);
268 Ungetc(data, c, file);
270 return (n); /* this returns bytes read + 1 */
274 * skip whitespace and compute the following unsigned int,
275 * returns 1 if one is found and 0 if not
280 unsigned int *ui_return)
285 l = xpmNextWord(data, buf, BUFSIZ);
286 return xpmatoui(buf, l, ui_return);
290 * return end of string - WARNING: malloc!
298 unsigned int i, n = 0;
300 char *p = NULL, *q, buf[BUFSIZ];
302 if (!data->type || data->type == XPMBUFFER) {
304 char *start = data->cptr;
305 while ((c = *data->cptr) && c != data->Eos)
307 n = data->cptr - start + 1;
308 p = (char *) XpmMalloc(n);
310 return (XpmNoMemory);
311 strncpy(p, start, n);
312 if (data->type) /* XPMBUFFER */
316 FILE *file = data->stream.file;
318 if ((c = Getc(data, file)) == EOF)
319 return (XpmFileInvalid);
323 p = (char *) XpmMalloc(1);
324 while (c != data->Eos && c != EOF) {
326 /* get to the end of the buffer */
327 /* malloc needed memory */
328 q = (char *) XpmRealloc(p, n + i);
331 return (XpmNoMemory);
335 /* and copy what we already have */
343 c = Getc(data, file);
347 return (XpmFileInvalid);
350 /* malloc needed memory */
351 q = (char *) XpmRealloc(p, n + i + 1);
354 return (XpmNoMemory);
358 /* and copy the buffer */
366 Ungetc(data, c, file);
374 * get the current comment line
383 else if (data->CommentLength != 0 && data->CommentLength < UINT_MAX - 1) {
384 if( (*cmt = (char *) XpmMalloc(data->CommentLength + 1)) == NULL)
386 strncpy(*cmt, data->Comment, data->CommentLength);
387 (*cmt)[data->CommentLength] = '\0';
388 data->CommentLength = 0;
394 xpmDataType xpmDataTypes[] =
396 {"", "!", "\n", '\0', '\n', "", "", "", ""}, /* Natural type */
397 {"C", "/*", "*/", '"', '"', ",\n", "static char *", "[] = {\n", "};\n"},
398 {"Lisp", ";", "\n", '"', '"', "\n", "(setq ", " '(\n", "))\n"},
399 {NULL, NULL, NULL, 0, 0, NULL, NULL, NULL, NULL}
406 xpmParseHeader(xpmData *data)
408 char buf[BUFSIZ+1] = {0};
414 data->Bcmt = data->Ecmt = NULL;
415 l = xpmNextWord(data, buf, BUFSIZ);
416 if (l == 7 && !strncmp("#define", buf, 7)) {
417 /* this maybe an XPM 1 file */
420 l = xpmNextWord(data, buf, BUFSIZ);
422 return (XpmFileInvalid);
424 ptr = strrchr(buf, '_');
425 if (!ptr || strncmp("_format", ptr, l - (ptr - buf)))
426 return XpmFileInvalid;
427 /* this is definitely an XPM 1 file */
429 n = 1; /* handle XPM1 as mainly XPM2 C */
433 * skip the first word, get the second one, and see if this is
436 l = xpmNextWord(data, buf, BUFSIZ);
437 if ((l == 3 && !strncmp("XPM", buf, 3)) ||
438 (l == 4 && !strncmp("XPM2", buf, 4))) {
440 n = 1; /* handle XPM as XPM2 C */
442 /* get the type key word */
443 l = xpmNextWord(data, buf, BUFSIZ);
446 * get infos about this type
448 while (xpmDataTypes[n].type
449 && strncmp(xpmDataTypes[n].type, buf, l))
454 /* nope this is not an XPM file */
455 return XpmFileInvalid;
457 if (xpmDataTypes[n].type) {
458 if (n == 0) { /* natural type */
459 data->Bcmt = xpmDataTypes[n].Bcmt;
460 data->Ecmt = xpmDataTypes[n].Ecmt;
461 xpmNextString(data); /* skip the end of the headerline */
462 data->Bos = xpmDataTypes[n].Bos;
463 data->Eos = xpmDataTypes[n].Eos;
465 data->Bcmt = xpmDataTypes[n].Bcmt;
466 data->Ecmt = xpmDataTypes[n].Ecmt;
467 if (!data->format) { /* XPM 2 or 3 */
468 data->Bos = xpmDataTypes[n].Bos;
470 /* get to the beginning of the first string */
472 data->Eos = xpmDataTypes[n].Eos;
473 } else /* XPM 1 skip end of line */
477 /* we don't know about that type of XPM file... */
478 return XpmFileInvalid;