]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/testmime.c
Add SSL + LDAP support (STR #1967)
[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;
5adebf48 74 filter_path = "../filter:../pdftops:" CUPS_SERVERBIN "/filter";
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)" : "");
0d0fc2d7 101 else if ((src = mimeType(mime, "application", "octet-stream")) != NULL)
102 printf("%s: application/octet-stream\n", argv[i]);
7b3a0e77 103 else
104 {
105 printf("%s: unknown\n", argv[i]);
106 if (mime)
107 mimeDelete(mime);
108 return (1);
109 }
110 }
111 else
112 {
113 sscanf(argv[i], "%15[^/]/%31s", super, type);
114 dst = mimeType(mime, super, type);
0b74af7d 115
238ad980 116 filters = mimeFilter(mime, src, dst, &cost);
0b74af7d 117
0557217b 118 if (!filters)
7b3a0e77 119 {
120 printf("No filters to convert from %s/%s to %s.\n", src->super,
121 src->type, argv[i]);
122 }
123 else
124 {
0557217b 125 printf("Filter cost = %d\n", cost);
7b3a0e77 126
0557217b 127 filter = (mime_filter_t *)cupsArrayFirst(filters);
128 fputs(filter->filter, stdout);
129
130 for (filter = (mime_filter_t *)cupsArrayNext(filters);
131 filter;
132 filter = (mime_filter_t *)cupsArrayNext(filters))
133 printf(" | %s", filter->filter);
134
135 putchar('\n');
136
137 cupsArrayDelete(filters);
7b3a0e77 138 }
139 }
0b74af7d 140
7b3a0e77 141 if (!mime)
eb40f117 142 mime = mimeLoad("../conf", filter_path);
0b74af7d 143
0557217b 144 if (!src)
0b74af7d 145 {
7b3a0e77 146 puts("MIME database types:");
0557217b 147 for (src = mimeFirstType(mime); src; src = mimeNextType(mime))
7b3a0e77 148 {
0557217b 149 printf("\t%s/%s:\n", src->super, src->type);
150 print_rules(src->rules);
7b3a0e77 151 puts("");
152 }
0b74af7d 153
7b3a0e77 154 puts("");
0b74af7d 155
7b3a0e77 156 puts("MIME database filters:");
0557217b 157 for (filter = mimeFirstFilter(mime); filter; filter = mimeNextFilter(mime))
7b3a0e77 158 printf("\t%s/%s to %s/%s: %s (%d)\n",
0557217b 159 filter->src->super, filter->src->type,
160 filter->dst->super, filter->dst->type,
161 filter->filter, filter->cost);
870aafdf 162
0d0fc2d7 163 type_dir(mime, "../doc");
164 type_dir(mime, "../man");
e8fda7b9 165 }
7b3a0e77 166
167 return (0);
0b74af7d 168}
169
170
171/*
172 * 'print_rules()' - Print the rules for a file type...
173 */
174
175static void
176print_rules(mime_magic_t *rules) /* I - Rules to print */
177{
8cf6bc6e 178 int i; /* Looping var */
179 static char indent[255] = "\t"; /* Indentation for rules */
0b74af7d 180
181
182 if (rules == NULL)
183 return;
184
0b74af7d 185 while (rules != NULL)
186 {
8cf6bc6e 187 printf("%s[%p] ", indent, rules);
188
189 if (rules->invert)
190 printf("NOT ");
0b74af7d 191
192 switch (rules->op)
193 {
194 case MIME_MAGIC_MATCH :
195 printf("match(%s)", rules->value.matchv);
196 break;
197 case MIME_MAGIC_LOCALE :
198 printf("locale(%s)", rules->value.localev);
199 break;
200 case MIME_MAGIC_ASCII :
201 printf("ascii(%d,%d)", rules->offset, rules->length);
202 break;
203 case MIME_MAGIC_PRINTABLE :
204 printf("printable(%d,%d)", rules->offset, rules->length);
205 break;
206 case MIME_MAGIC_STRING :
8cf6bc6e 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]);
212 else
213 putchar(rules->value.stringv[i]);
214 putchar(')');
0b74af7d 215 break;
216 case MIME_MAGIC_CHAR :
217 printf("char(%d,%d)", rules->offset, rules->value.charv);
218 break;
219 case MIME_MAGIC_SHORT :
220 printf("short(%d,%d)", rules->offset, rules->value.shortv);
221 break;
222 case MIME_MAGIC_INT :
223 printf("int(%d,%d)", rules->offset, rules->value.intv);
224 break;
8cf6bc6e 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]);
231 else
232 putchar(rules->value.stringv[i]);
233 putchar(')');
234 break;
0b74af7d 235 default :
0b74af7d 236 break;
e8fda7b9 237 }
0b74af7d 238
8cf6bc6e 239 if (rules->child != NULL)
240 {
241 if (rules->op == MIME_MAGIC_OR)
242 puts("OR (");
243 else
244 puts("AND (");
245
246 strcat(indent, "\t");
247 print_rules(rules->child);
248 indent[strlen(indent) - 1] = '\0';
249 printf("%s)\n", indent);
250 }
251 else
252 putchar('\n');
253
0b74af7d 254 rules = rules->next;
e8fda7b9 255 }
0b74af7d 256}
257
57555697 258
870aafdf 259/*
260 * 'type_dir()' - Show the MIME types for a given directory.
261 */
262
263static void
264type_dir(mime_t *mime, /* I - MIME database */
265 const char *dirname) /* I - Directory */
266{
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 */
276
277
278 dir = cupsDirOpen(dirname);
279 if (!dir)
280 return;
281
282 pstype = mimeType(mime, "application", "vnd.cups-postscript");
283
284 while ((dent = cupsDirRead(dir)) != NULL)
285 {
0d0fc2d7 286 if (dent->filename[0] == '.')
287 continue;
288
870aafdf 289 snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->filename);
290
291 if (S_ISDIR(dent->fileinfo.st_mode))
292 type_dir(mime, filename);
293
294 if (!S_ISREG(dent->fileinfo.st_mode))
295 continue;
296
ab88a636 297 filetype = mimeFileType(mime, filename, NULL, &compression);
870aafdf 298
299 if (filetype)
300 {
301 printf("%s: %s/%s%s\n", filename, filetype->super, filetype->type,
302 compression ? " (compressed)" : "");
303
238ad980 304 filters = mimeFilter(mime, filetype, pstype, &cost);
870aafdf 305
306 if (!filters)
307 puts(" No filters to convert application/vnd.cups-postscript.");
308 else
309 {
310 printf(" Filter cost = %d\n", cost);
311
312 filter = (mime_filter_t *)cupsArrayFirst(filters);
313 printf(" %s", filter->filter);
314
315 for (filter = (mime_filter_t *)cupsArrayNext(filters);
316 filter;
317 filter = (mime_filter_t *)cupsArrayNext(filters))
318 printf(" | %s", filter->filter);
319
320 putchar('\n');
321
322 cupsArrayDelete(filters);
323 }
324 }
325 else
326 printf("%s: unknown%s\n", filename, compression ? " (compressed)" : "");
327 }
328
329 cupsDirClose(dir);
330}
331
332
57555697 333/*
c9d3f842 334 * End of "$Id$".
57555697 335 */