]> 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 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * MIME test program for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * Contents:
16 *
17 * main() - Main entry for the test program.
18 * print_rules() - Print the rules for a file type...
19 * type_dir() - Show the MIME types for a given directory.
20 */
21
22 /*
23 * Include necessary headers...
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <cups/string.h>
29 #include "mime.h"
30 #include <cups/dir.h>
31
32
33 /*
34 * Local functions...
35 */
36
37 static void print_rules(mime_magic_t *rules);
38 static void type_dir(mime_t *mime, const char *dirname);
39
40
41 /*
42 * 'main()' - Main entry for the test program.
43 */
44
45 int /* O - Exit status */
46 main(int argc, /* I - Number of command-line args */
47 char *argv[]) /* I - Command-line arguments */
48 {
49 int i; /* Looping vars */
50 const char *filter_path; /* Filter path */
51 char super[MIME_MAX_SUPER], /* Super-type name */
52 type[MIME_MAX_TYPE]; /* Type name */
53 int compression; /* Compression of file */
54 int cost; /* Cost of filters */
55 mime_t *mime; /* MIME database */
56 mime_type_t *src, /* Source type */
57 *dst; /* Destination type */
58 cups_array_t *filters; /* Filters for the file */
59 mime_filter_t *filter; /* Current filter */
60
61
62 mime = NULL;
63 src = NULL;
64 dst = NULL;
65 filter_path = "../filter:../pdftops:" CUPS_SERVERBIN "/filter";
66
67 for (i = 1; i < argc; i ++)
68 if (!strcmp(argv[i], "-d"))
69 {
70 i ++;
71
72 if (i < argc)
73 mime = mimeLoad(argv[i], filter_path);
74 }
75 else if (!strcmp(argv[i], "-f"))
76 {
77 i ++;
78
79 if (i < argc)
80 filter_path = argv[i];
81 }
82 else if (!src)
83 {
84 if (!mime)
85 mime = mimeLoad("../conf", filter_path);
86
87 src = mimeFileType(mime, argv[i], NULL, &compression);
88
89 if (src)
90 printf("%s: %s/%s%s\n", argv[i], src->super, src->type,
91 compression ? " (gzipped)" : "");
92 else if ((src = mimeType(mime, "application", "octet-stream")) != NULL)
93 printf("%s: application/octet-stream\n", argv[i]);
94 else
95 {
96 printf("%s: unknown\n", argv[i]);
97 if (mime)
98 mimeDelete(mime);
99 return (1);
100 }
101 }
102 else
103 {
104 sscanf(argv[i], "%15[^/]/%31s", super, type);
105 dst = mimeType(mime, super, type);
106
107 filters = mimeFilter(mime, src, dst, &cost);
108
109 if (!filters)
110 {
111 printf("No filters to convert from %s/%s to %s.\n", src->super,
112 src->type, argv[i]);
113 }
114 else
115 {
116 printf("Filter cost = %d\n", cost);
117
118 filter = (mime_filter_t *)cupsArrayFirst(filters);
119 fputs(filter->filter, stdout);
120
121 for (filter = (mime_filter_t *)cupsArrayNext(filters);
122 filter;
123 filter = (mime_filter_t *)cupsArrayNext(filters))
124 printf(" | %s", filter->filter);
125
126 putchar('\n');
127
128 cupsArrayDelete(filters);
129 }
130 }
131
132 if (!mime)
133 mime = mimeLoad("../conf", filter_path);
134
135 if (!src)
136 {
137 puts("MIME database types:");
138 for (src = mimeFirstType(mime); src; src = mimeNextType(mime))
139 {
140 printf("\t%s/%s:\n", src->super, src->type);
141 print_rules(src->rules);
142 puts("");
143 }
144
145 puts("");
146
147 puts("MIME database filters:");
148 for (filter = mimeFirstFilter(mime); filter; filter = mimeNextFilter(mime))
149 printf("\t%s/%s to %s/%s: %s (%d)\n",
150 filter->src->super, filter->src->type,
151 filter->dst->super, filter->dst->type,
152 filter->filter, filter->cost);
153
154 type_dir(mime, "../doc");
155 type_dir(mime, "../man");
156 }
157
158 return (0);
159 }
160
161
162 /*
163 * 'print_rules()' - Print the rules for a file type...
164 */
165
166 static void
167 print_rules(mime_magic_t *rules) /* I - Rules to print */
168 {
169 int i; /* Looping var */
170 static char indent[255] = "\t"; /* Indentation for rules */
171
172
173 if (rules == NULL)
174 return;
175
176 while (rules != NULL)
177 {
178 printf("%s[%p] ", indent, rules);
179
180 if (rules->invert)
181 printf("NOT ");
182
183 switch (rules->op)
184 {
185 case MIME_MAGIC_MATCH :
186 printf("match(%s)", rules->value.matchv);
187 break;
188 case MIME_MAGIC_LOCALE :
189 printf("locale(%s)", rules->value.localev);
190 break;
191 case MIME_MAGIC_ASCII :
192 printf("ascii(%d,%d)", rules->offset, rules->length);
193 break;
194 case MIME_MAGIC_PRINTABLE :
195 printf("printable(%d,%d)", rules->offset, rules->length);
196 break;
197 case MIME_MAGIC_STRING :
198 printf("string(%d,", rules->offset);
199 for (i = 0; i < rules->length; i ++)
200 if (rules->value.stringv[i] < ' ' ||
201 rules->value.stringv[i] > 126)
202 printf("<%02X>", rules->value.stringv[i]);
203 else
204 putchar(rules->value.stringv[i]);
205 putchar(')');
206 break;
207 case MIME_MAGIC_CHAR :
208 printf("char(%d,%d)", rules->offset, rules->value.charv);
209 break;
210 case MIME_MAGIC_SHORT :
211 printf("short(%d,%d)", rules->offset, rules->value.shortv);
212 break;
213 case MIME_MAGIC_INT :
214 printf("int(%d,%d)", rules->offset, rules->value.intv);
215 break;
216 case MIME_MAGIC_CONTAINS :
217 printf("contains(%d,%d,", rules->offset, rules->region);
218 for (i = 0; i < rules->length; i ++)
219 if (rules->value.stringv[i] < ' ' ||
220 rules->value.stringv[i] > 126)
221 printf("<%02X>", rules->value.stringv[i]);
222 else
223 putchar(rules->value.stringv[i]);
224 putchar(')');
225 break;
226 default :
227 break;
228 }
229
230 if (rules->child != NULL)
231 {
232 if (rules->op == MIME_MAGIC_OR)
233 puts("OR (");
234 else
235 puts("AND (");
236
237 strcat(indent, "\t");
238 print_rules(rules->child);
239 indent[strlen(indent) - 1] = '\0';
240 printf("%s)\n", indent);
241 }
242 else
243 putchar('\n');
244
245 rules = rules->next;
246 }
247 }
248
249
250 /*
251 * 'type_dir()' - Show the MIME types for a given directory.
252 */
253
254 static void
255 type_dir(mime_t *mime, /* I - MIME database */
256 const char *dirname) /* I - Directory */
257 {
258 cups_dir_t *dir; /* Directory */
259 cups_dentry_t *dent; /* Directory entry */
260 char filename[1024]; /* File to type */
261 mime_type_t *filetype; /* File type */
262 int compression; /* Compressed file? */
263 mime_type_t *pstype; /* application/vnd.cups-postscript */
264 cups_array_t *filters; /* Filters to pstype */
265 mime_filter_t *filter; /* Current filter */
266 int cost; /* Filter cost */
267
268
269 dir = cupsDirOpen(dirname);
270 if (!dir)
271 return;
272
273 pstype = mimeType(mime, "application", "vnd.cups-postscript");
274
275 while ((dent = cupsDirRead(dir)) != NULL)
276 {
277 if (dent->filename[0] == '.')
278 continue;
279
280 snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->filename);
281
282 if (S_ISDIR(dent->fileinfo.st_mode))
283 type_dir(mime, filename);
284
285 if (!S_ISREG(dent->fileinfo.st_mode))
286 continue;
287
288 filetype = mimeFileType(mime, filename, NULL, &compression);
289
290 if (filetype)
291 {
292 printf("%s: %s/%s%s\n", filename, filetype->super, filetype->type,
293 compression ? " (compressed)" : "");
294
295 filters = mimeFilter(mime, filetype, pstype, &cost);
296
297 if (!filters)
298 puts(" No filters to convert application/vnd.cups-postscript.");
299 else
300 {
301 printf(" Filter cost = %d\n", cost);
302
303 filter = (mime_filter_t *)cupsArrayFirst(filters);
304 printf(" %s", filter->filter);
305
306 for (filter = (mime_filter_t *)cupsArrayNext(filters);
307 filter;
308 filter = (mime_filter_t *)cupsArrayNext(filters))
309 printf(" | %s", filter->filter);
310
311 putchar('\n');
312
313 cupsArrayDelete(filters);
314 }
315 }
316 else
317 printf("%s: unknown%s\n", filename, compression ? " (compressed)" : "");
318 }
319
320 cupsDirClose(dir);
321 }
322
323
324 /*
325 * End of "$Id: testmime.c 6649 2007-07-11 21:46:42Z mike $".
326 */