]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testmime.c
2 * "$Id: testmime.c 181 2006-06-22 20:01:18Z jlovell $"
4 * MIME test program for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
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.
32 * Include necessary headers...
37 #include <cups/string.h>
46 static void print_rules(mime_magic_t
*rules
);
47 static void type_dir(mime_t
*mime
, const char *dirname
);
51 * 'main()' - Main entry for the test program.
54 int /* O - Exit status */
55 main(int argc
, /* I - Number of command-line args */
56 char *argv
[]) /* I - Command-line arguments */
58 int i
; /* Looping vars */
59 const char *filter_path
; /* Filter path */
60 char super
[MIME_MAX_SUPER
], /* Super-type name */
61 type
[MIME_MAX_TYPE
]; /* Type name */
62 int compression
; /* Compression of file */
63 int cost
; /* Cost of filters */
64 mime_t
*mime
; /* MIME database */
65 mime_type_t
*src
, /* Source type */
66 *dst
; /* Destination type */
67 cups_array_t
*filters
; /* Filters for the file */
68 mime_filter_t
*filter
; /* Current filter */
74 filter_path
= "../filter:../pdftops:" CUPS_SERVERBIN
"/filter";
76 for (i
= 1; i
< argc
; i
++)
77 if (!strcmp(argv
[i
], "-d"))
82 mime
= mimeLoad(argv
[i
], filter_path
);
84 else if (!strcmp(argv
[i
], "-f"))
89 filter_path
= argv
[i
];
94 mime
= mimeLoad("../conf", filter_path
);
96 src
= mimeFileType(mime
, argv
[i
], NULL
, &compression
);
99 printf("%s: %s/%s%s\n", argv
[i
], src
->super
, src
->type
,
100 compression
? " (gzipped)" : "");
101 else if ((src
= mimeType(mime
, "application", "octet-stream")) != NULL
)
102 printf("%s: application/octet-stream\n", argv
[i
]);
105 printf("%s: unknown\n", argv
[i
]);
113 sscanf(argv
[i
], "%15[^/]/%31s", super
, type
);
114 dst
= mimeType(mime
, super
, type
);
116 filters
= mimeFilter(mime
, src
, dst
, &cost
);
120 printf("No filters to convert from %s/%s to %s.\n", src
->super
,
125 printf("Filter cost = %d\n", cost
);
127 filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
128 fputs(filter
->filter
, stdout
);
130 for (filter
= (mime_filter_t
*)cupsArrayNext(filters
);
132 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
133 printf(" | %s", filter
->filter
);
137 cupsArrayDelete(filters
);
142 mime
= mimeLoad("../conf", filter_path
);
146 puts("MIME database types:");
147 for (src
= mimeFirstType(mime
); src
; src
= mimeNextType(mime
))
149 printf("\t%s/%s:\n", src
->super
, src
->type
);
150 print_rules(src
->rules
);
156 puts("MIME database filters:");
157 for (filter
= mimeFirstFilter(mime
); filter
; filter
= mimeNextFilter(mime
))
158 printf("\t%s/%s to %s/%s: %s (%d)\n",
159 filter
->src
->super
, filter
->src
->type
,
160 filter
->dst
->super
, filter
->dst
->type
,
161 filter
->filter
, filter
->cost
);
163 type_dir(mime
, "../doc");
164 type_dir(mime
, "../man");
172 * 'print_rules()' - Print the rules for a file type...
176 print_rules(mime_magic_t
*rules
) /* I - Rules to print */
178 int i
; /* Looping var */
179 static char indent
[255] = "\t"; /* Indentation for rules */
185 while (rules
!= NULL
)
187 printf("%s[%p] ", indent
, rules
);
194 case MIME_MAGIC_MATCH
:
195 printf("match(%s)", rules
->value
.matchv
);
197 case MIME_MAGIC_LOCALE
:
198 printf("locale(%s)", rules
->value
.localev
);
200 case MIME_MAGIC_ASCII
:
201 printf("ascii(%d,%d)", rules
->offset
, rules
->length
);
203 case MIME_MAGIC_PRINTABLE
:
204 printf("printable(%d,%d)", rules
->offset
, rules
->length
);
206 case MIME_MAGIC_STRING
:
207 printf("string(%d,", rules
->offset
);
208 for (i
= 0; i
< rules
->length
; i
++)
209 if (rules
->value
.stringv
[i
] < ' ' ||
210 rules
->value
.stringv
[i
] > 126)
211 printf("<%02X>", rules
->value
.stringv
[i
]);
213 putchar(rules
->value
.stringv
[i
]);
216 case MIME_MAGIC_CHAR
:
217 printf("char(%d,%d)", rules
->offset
, rules
->value
.charv
);
219 case MIME_MAGIC_SHORT
:
220 printf("short(%d,%d)", rules
->offset
, rules
->value
.shortv
);
222 case MIME_MAGIC_INT
:
223 printf("int(%d,%d)", rules
->offset
, rules
->value
.intv
);
225 case MIME_MAGIC_CONTAINS
:
226 printf("contains(%d,%d,", rules
->offset
, rules
->region
);
227 for (i
= 0; i
< rules
->length
; i
++)
228 if (rules
->value
.stringv
[i
] < ' ' ||
229 rules
->value
.stringv
[i
] > 126)
230 printf("<%02X>", rules
->value
.stringv
[i
]);
232 putchar(rules
->value
.stringv
[i
]);
239 if (rules
->child
!= NULL
)
241 if (rules
->op
== MIME_MAGIC_OR
)
246 strcat(indent
, "\t");
247 print_rules(rules
->child
);
248 indent
[strlen(indent
) - 1] = '\0';
249 printf("%s)\n", indent
);
260 * 'type_dir()' - Show the MIME types for a given directory.
264 type_dir(mime_t
*mime
, /* I - MIME database */
265 const char *dirname
) /* I - Directory */
267 cups_dir_t
*dir
; /* Directory */
268 cups_dentry_t
*dent
; /* Directory entry */
269 char filename
[1024]; /* File to type */
270 mime_type_t
*filetype
; /* File type */
271 int compression
; /* Compressed file? */
272 mime_type_t
*pstype
; /* application/vnd.cups-postscript */
273 cups_array_t
*filters
; /* Filters to pstype */
274 mime_filter_t
*filter
; /* Current filter */
275 int cost
; /* Filter cost */
278 dir
= cupsDirOpen(dirname
);
282 pstype
= mimeType(mime
, "application", "vnd.cups-postscript");
284 while ((dent
= cupsDirRead(dir
)) != NULL
)
286 if (dent
->filename
[0] == '.')
289 snprintf(filename
, sizeof(filename
), "%s/%s", dirname
, dent
->filename
);
291 if (S_ISDIR(dent
->fileinfo
.st_mode
))
292 type_dir(mime
, filename
);
294 if (!S_ISREG(dent
->fileinfo
.st_mode
))
297 filetype
= mimeFileType(mime
, filename
, NULL
, &compression
);
301 printf("%s: %s/%s%s\n", filename
, filetype
->super
, filetype
->type
,
302 compression
? " (compressed)" : "");
304 filters
= mimeFilter(mime
, filetype
, pstype
, &cost
);
307 puts(" No filters to convert application/vnd.cups-postscript.");
310 printf(" Filter cost = %d\n", cost
);
312 filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
313 printf(" %s", filter
->filter
);
315 for (filter
= (mime_filter_t
*)cupsArrayNext(filters
);
317 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
318 printf(" | %s", filter
->filter
);
322 cupsArrayDelete(filters
);
326 printf("%s: unknown%s\n", filename
, compression
? " (compressed)" : "");
334 * End of "$Id: testmime.c 181 2006-06-22 20:01:18Z jlovell $".