]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testmime.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / testmime.c
1 /*
2 * "$Id: testmime.c 4719 2005-09-28 21:12:44Z mike $"
3 *
4 * MIME test program for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Main entry for the test program.
27 */
28
29 /*
30 * Include necessary headers...
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <cups/string.h>
36 #include "mime.h"
37
38
39 /*
40 * Local functions...
41 */
42
43 static void print_rules(mime_magic_t *rules);
44
45
46 /*
47 * 'main()' - Main entry for the test program.
48 */
49
50 int /* O - Exit status */
51 main(int argc, /* I - Number of command-line args */
52 char *argv[]) /* I - Command-line arguments */
53 {
54 int i, j; /* Looping vars */
55 const char *filter_path; /* Filter path */
56 char super[MIME_MAX_SUPER], /* Super-type name */
57 type[MIME_MAX_TYPE]; /* Type name */
58 int compression; /* Compression of file */
59 mime_t *mime; /* MIME database */
60 mime_type_t *src, /* Source type */
61 *dst, /* Destination type */
62 **types; /* File type array pointer */
63 mime_filter_t *filters; /* Filters for the file */
64 int num_filters; /* Number of filters for the file */
65
66
67 mime = NULL;
68 src = NULL;
69 dst = NULL;
70 filter_path = "../filter";
71
72 for (i = 1; i < argc; i ++)
73 if (strcmp(argv[i], "-d") == 0)
74 {
75 i ++;
76
77 if (i < argc)
78 mime = mimeLoad(argv[i], filter_path);
79 }
80 else if (strcmp(argv[i], "-f") == 0)
81 {
82 i ++;
83
84 if (i < argc)
85 filter_path = argv[i];
86 }
87 else if (src == NULL)
88 {
89 if (!mime)
90 mime = mimeLoad("../conf", filter_path);
91
92 src = mimeFileType(mime, argv[i], &compression);
93
94 if (src != NULL)
95 printf("%s: %s/%s%s\n", argv[i], src->super, src->type,
96 compression ? " (gzipped)" : "");
97 else
98 {
99 printf("%s: unknown\n", argv[i]);
100 if (mime)
101 mimeDelete(mime);
102 return (1);
103 }
104 }
105 else
106 {
107 sscanf(argv[i], "%15[^/]/%31s", super, type);
108 dst = mimeType(mime, super, type);
109
110 filters = mimeFilter(mime, src, dst, &num_filters, 10);
111
112 if (filters == NULL)
113 {
114 printf("No filters to convert from %s/%s to %s.\n", src->super,
115 src->type, argv[i]);
116 }
117 else
118 {
119 for (j = 0; j < num_filters; j ++)
120 if (j < (num_filters - 1))
121 printf("%s | ", filters[j].filter);
122 else
123 puts(filters[j].filter);
124
125 free(filters);
126 }
127 }
128
129 if (!mime)
130 mime = mimeLoad("../conf", filter_path);
131
132 if (src == NULL)
133 {
134 puts("MIME database types:");
135 for (i = 0, types = mime->types; i < mime->num_types; i ++, types ++)
136 {
137 printf("\t%s/%s:\n", (*types)->super, (*types)->type);
138 print_rules((*types)->rules);
139 puts("");
140 }
141
142 puts("");
143
144 puts("MIME database filters:");
145 for (i = 0, filters = mime->filters; i < mime->num_filters; i ++, filters ++)
146 printf("\t%s/%s to %s/%s: %s (%d)\n",
147 filters->src->super, filters->src->type,
148 filters->dst->super, filters->dst->type,
149 filters->filter, filters->cost);
150 }
151
152 return (0);
153 }
154
155
156 /*
157 * 'print_rules()' - Print the rules for a file type...
158 */
159
160 static void
161 print_rules(mime_magic_t *rules) /* I - Rules to print */
162 {
163 int i; /* Looping var */
164 static char indent[255] = "\t"; /* Indentation for rules */
165
166
167 if (rules == NULL)
168 return;
169
170 while (rules != NULL)
171 {
172 printf("%s[%p] ", indent, rules);
173
174 if (rules->invert)
175 printf("NOT ");
176
177 switch (rules->op)
178 {
179 case MIME_MAGIC_MATCH :
180 printf("match(%s)", rules->value.matchv);
181 break;
182 case MIME_MAGIC_LOCALE :
183 printf("locale(%s)", rules->value.localev);
184 break;
185 case MIME_MAGIC_ASCII :
186 printf("ascii(%d,%d)", rules->offset, rules->length);
187 break;
188 case MIME_MAGIC_PRINTABLE :
189 printf("printable(%d,%d)", rules->offset, rules->length);
190 break;
191 case MIME_MAGIC_STRING :
192 printf("string(%d,", rules->offset);
193 for (i = 0; i < rules->length; i ++)
194 if (rules->value.stringv[i] < ' ' ||
195 rules->value.stringv[i] > 126)
196 printf("<%02X>", rules->value.stringv[i]);
197 else
198 putchar(rules->value.stringv[i]);
199 putchar(')');
200 break;
201 case MIME_MAGIC_CHAR :
202 printf("char(%d,%d)", rules->offset, rules->value.charv);
203 break;
204 case MIME_MAGIC_SHORT :
205 printf("short(%d,%d)", rules->offset, rules->value.shortv);
206 break;
207 case MIME_MAGIC_INT :
208 printf("int(%d,%d)", rules->offset, rules->value.intv);
209 break;
210 case MIME_MAGIC_CONTAINS :
211 printf("contains(%d,%d,", rules->offset, rules->region);
212 for (i = 0; i < rules->length; i ++)
213 if (rules->value.stringv[i] < ' ' ||
214 rules->value.stringv[i] > 126)
215 printf("<%02X>", rules->value.stringv[i]);
216 else
217 putchar(rules->value.stringv[i]);
218 putchar(')');
219 break;
220 default :
221 break;
222 }
223
224 if (rules->child != NULL)
225 {
226 if (rules->op == MIME_MAGIC_OR)
227 puts("OR (");
228 else
229 puts("AND (");
230
231 strcat(indent, "\t");
232 print_rules(rules->child);
233 indent[strlen(indent) - 1] = '\0';
234 printf("%s)\n", indent);
235 }
236 else
237 putchar('\n');
238
239 rules = rules->next;
240 }
241 }
242
243
244 /*
245 * End of "$Id: testmime.c 4719 2005-09-28 21:12:44Z mike $".
246 */