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