]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/form-tree.c
188a254b1359112e353e1980a7bca5294b972624
2 * "$Id: form-tree.c 4494 2005-02-18 02:18:11Z mike $"
4 * CUPS form document tree routines for the Common UNIX Printing
7 * Copyright 1997-2005 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
25 * This file is subject to the Apple OS-Developed Software exception.
32 * Include necessary headers...
42 static int compare_attr(attr_t
*a0
, attr_t
*a1
);
43 static int compare_elements(char **e0
, char **e1
);
44 static int parse_attr(tree_t
*t
, FILE *fp
);
45 static int parse_element(tree_t
*t
, FILE *fp
);
52 static char *elements
[] =
86 * 'formDelete()' - Delete a node and its children.
90 formDelete(tree_t
*t
) /* I - Tree node */
96 * 'formGetAttr()' - Get a node attribute value.
99 char * /* O - Value or NULL */
100 formGetAttr(tree_t
*t
, /* I - Tree node */
101 const char *name
) /* I - Name of attribute */
107 * 'formNew()' - Create a new form node.
110 tree_t
* /* O - New tree node */
111 formNew(tree_t
*p
) /* I - Parent node */
113 tree_t
*t
; /* New tree node */
117 * Allocate the new node...
120 if ((t
= (tree_t
*)calloc(sizeof(tree_t
), 1)) == NULL
)
124 * Set/copy attributes...
132 t
->halign
= HALIGN_LEFT
;
133 t
->valign
= VALIGN_MIDDLE
;
134 t
->typeface
= "Courier";
139 memcpy(t
, p
, sizeof(tree_t
));
144 t
->last_child
= NULL
;
152 * Return the new node...
160 * 'formRead()' - Read a form tree from a file.
163 tree_t
* /* O - New form tree */
164 formRead(FILE *fp
, /* I - File to read from */
165 tree_t
*p
) /* I - Parent node */
167 int ch
, /* Character from file */
168 closech
, /* Closing character */
169 have_whitespace
; /* Leading whitespace? */
170 static char s
[10240]; /* String from file */
171 uchar
*ptr
, /* Pointer in string */
172 glyph
[16], /* Glyph name (&#nnn;) */
173 *glyphptr
; /* Pointer in glyph string */
174 tree_t
*tree
, /* "top" of this tree */
175 *t
, /* New tree node */
176 *prev
, /* Previous tree node */
177 *temp
; /* Temporary looping var */
178 uchar
*face
, /* Typeface for FONT tag */
179 *color
, /* Color for FONT tag */
180 *size
; /* Size for FONT tag */
184 * Start off with no previous tree node...
191 * Parse data until we hit end-of-file...
194 while ((ch
= getc(fp
)) != EOF
)
197 * Ignore leading whitespace...
203 if (p
== NULL
|| !p
->preformatted
)
205 while (isspace(ch
& 255))
216 * Allocate a new tree node - use calloc() to get zeroed data...
222 * See what the character was...
228 * Markup char; grab the next char to see if this is a /...
235 * Illegal lone "<"! Ignore it...
245 if (parse_element(t
, fp
) < 0)
251 if ((closech
= getc(fp
)) == '/')
255 * If this is the matching close mark, or if we are starting the same
256 * element, or if we've completed a list, we're done!
262 * Close element; find matching element...
265 for (temp
= p
; temp
!= NULL
; temp
= temp
->p
)
266 if (temp
->element
== t
->element
)
277 else if (t
->preformatted
)
280 * Read a pre-formatted string into the current tree node...
284 while (ch
!= '<' && ch
!= EOF
&& ptr
< (s
+ sizeof(s
) - 1))
288 for (glyphptr
= glyph
;
289 (ch
= getc(fp
)) != EOF
&& (glyphptr
- glyph
) < 15;
291 if (!isalnum(ch
& 255))
299 else if (strcmp(glyph
, "lt") == 0)
301 else if (strcmp(glyph
, "gt") == 0)
303 else if (strcmp(glyph
, "quot") == 0)
305 else if (strcmp(glyph
, "nbsp") == 0)
325 t
->element
= ELEMENT_FRAGMENT
;
331 * Read the next string fragment...
338 while (!isspace(ch
& 255) && ch
!= '<' && ch
!= EOF
&& ptr
< (s
+ sizeof(s
) - 1))
342 for (glyphptr
= glyph
;
343 (ch
= getc(fp
)) != EOF
&& (glyphptr
- glyph
) < 15;
345 if (!isalnum(ch
& 255))
353 else if (strcmp(glyph
, "lt") == 0)
355 else if (strcmp(glyph
, "gt") == 0)
357 else if (strcmp(glyph
, "quot") == 0)
359 else if (strcmp(glyph
, "nbsp") == 0)
371 if (isspace(ch
& 255))
379 t
->element
= ELEMENT_FRAGMENT
;
384 * If the p tree pointer is not NULL and this is the first
385 * entry we've read, set the child pointer...
388 if (p
!= NULL
&& prev
== NULL
)
395 * Do the prev/next links...
408 * Do child stuff as needed...
412 t
->child
= formRead(t
, fp
);
420 * 'formSetAttr()' - Set a node attribute.
424 formSetAttr(tree_t
*t
, /* I - Tree node */
425 const char *name
, /* I - Attribute name */
426 const char *value
) /* I - Attribute value */
432 * 'compare_attr()' - Compare two attributes.
435 static int /* O - -1 if a0 < a1, etc. */
436 compare_attr(attr_t
*a0
, /* I - First attribute */
437 attr_t
*a1
) /* I - Second attribute */
439 return (strcasecmp(a0
->name
, a1
->name
));
444 * 'compare_elements()' - Compare two elements.
447 static int /* O - -1 if e0 < e1, etc. */
448 compare_elements(char **e0
, /* I - First element */
449 char **e1
) /* I - Second element */
451 return (strcasecmp(*e0
, *e1
));
456 * 'parse_attr()' - Parse an element attribute string.
459 static int /* O - -1 on error, 0 on success */
460 parse_attr(tree_t
*t
, /* I - Current tree node */
461 FILE *fp
) /* I - Input file */
463 char name
[1024], /* Name of attr */
464 value
[10240], /* Value of attr */
465 *ptr
; /* Temporary pointer */
466 int ch
; /* Character from file */
470 while ((ch
= getc(fp
)) != EOF
)
471 if (isalnum(ch
& 255))
473 if (ptr
< (name
+ sizeof(name
) - 1))
481 while (isspace(ch
& 255) || ch
== '\r')
488 return (formSetAttr(t
, name
, NULL
));
495 while (isspace(ch
& 255) || ch
== '\r')
503 while ((ch
= getc(fp
)) != EOF
)
506 else if (ptr
< (value
+ sizeof(value
) - 1))
513 while ((ch
= getc(fp
)) != EOF
)
516 else if (ptr
< (value
+ sizeof(value
) - 1))
524 while ((ch
= getc(fp
)) != EOF
)
525 if (isspace(ch
& 255) || ch
== '>' || ch
== '/' || ch
== '\r')
527 else if (ptr
< (value
+ sizeof(value
) - 1))
531 if (ch
== '>' || ch
== '/')
535 return (formSetAttr(t
, name
, value
));
541 * 'parse_element()' - Parse an element.
544 static int /* O - -1 on error or ELEMENT_nnnn */
545 parse_element(tree_t
*t
, /* I - Current tree node */
546 FILE *fp
) /* I - Input file */
548 int ch
; /* Character from file */
549 char element
[255], /* Element string... */
550 *eptr
, /* Current character... */
551 comment
[10240], /* Comment string */
552 *cptr
, /* Current char... */
553 **temp
; /* Element variable entry */
558 while ((ch
= getc(fp
)) != EOF
&& eptr
< (element
+ sizeof(element
) - 1))
559 if (ch
== '>' || ch
== '/' || isspace(ch
& 255))
567 return (ELEMENT_ERROR
);
570 temp
= bsearch(&mptr
, elements
, sizeof(elements
) / sizeof(elements
[0]),
572 (int (*)(const void *, const void *))compare_elements
);
577 * Unrecognized element stuff...
580 t
->element
= ELEMENT_COMMENT
;
581 strcpy(comment
, element
);
582 cptr
= comment
+ strlen(comment
);
586 t
->element
= (element_t
)((char **)temp
- elements
);
590 if (t
->element
== ELEMENT_COMMENT
)
592 while (ch
!= EOF
&& ch
!= '>' && cptr
< (comment
+ sizeof(comment
) - 1))
599 t
->data
= strdup(comment
);
603 while (ch
!= EOF
&& ch
!= '>' && ch
!= '/')
605 if (!isspace(ch
& 255))
608 parse_variable(t
, fp
);
623 * End of "$Id: form-tree.c 4494 2005-02-18 02:18:11Z mike $".