]> 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 4970 2006-01-24 14:05:45Z mike $"
3 *
4 * MIME test program for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 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; /* 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 int cost; /* Cost of filters */
60 mime_t *mime; /* MIME database */
61 mime_type_t *src, /* Source type */
62 *dst; /* Destination type */
63 cups_array_t *filters; /* Filters for the file */
64 mime_filter_t *filter; /* Current filter */
65
66
67 mime = NULL;
68 src = NULL;
69 dst = NULL;
70 filter_path = "../filter:../pdftops";
71
72 for (i = 1; i < argc; i ++)
73 if (!strcmp(argv[i], "-d"))
74 {
75 i ++;
76
77 if (i < argc)
78 mime = mimeLoad(argv[i], filter_path);
79 }
80 else if (!strcmp(argv[i], "-f"))
81 {
82 i ++;
83
84 if (i < argc)
85 filter_path = argv[i];
86 }
87 else if (!src)
88 {
89 if (!mime)
90 mime = mimeLoad("../conf", filter_path);
91
92 src = mimeFileType(mime, argv[i], &compression);
93
94 if (src)
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, &cost, 10);
111
112 if (!filters)
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 printf("Filter cost = %d\n", cost);
120
121 filter = (mime_filter_t *)cupsArrayFirst(filters);
122 fputs(filter->filter, stdout);
123
124 for (filter = (mime_filter_t *)cupsArrayNext(filters);
125 filter;
126 filter = (mime_filter_t *)cupsArrayNext(filters))
127 printf(" | %s", filter->filter);
128
129 putchar('\n');
130
131 cupsArrayDelete(filters);
132 }
133 }
134
135 if (!mime)
136 mime = mimeLoad("../conf", filter_path);
137
138 if (!src)
139 {
140 puts("MIME database types:");
141 for (src = mimeFirstType(mime); src; src = mimeNextType(mime))
142 {
143 printf("\t%s/%s:\n", src->super, src->type);
144 print_rules(src->rules);
145 puts("");
146 }
147
148 puts("");
149
150 puts("MIME database filters:");
151 for (filter = mimeFirstFilter(mime); filter; filter = mimeNextFilter(mime))
152 printf("\t%s/%s to %s/%s: %s (%d)\n",
153 filter->src->super, filter->src->type,
154 filter->dst->super, filter->dst->type,
155 filter->filter, filter->cost);
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 * End of "$Id: testmime.c 4970 2006-01-24 14:05:45Z mike $".
252 */