]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/testmime.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / testmime.c
CommitLineData
ef416fc2 1/*
fa73b229 2 * "$Id: testmime.c 4970 2006-01-24 14:05:45Z mike $"
ef416fc2 3 *
4 * MIME test program for the Common UNIX Printing System (CUPS).
5 *
fa73b229 6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
ef416fc2 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
43static void print_rules(mime_magic_t *rules);
44
45
46/*
47 * 'main()' - Main entry for the test program.
48 */
49
50int /* O - Exit status */
51main(int argc, /* I - Number of command-line args */
52 char *argv[]) /* I - Command-line arguments */
53{
fa73b229 54 int i; /* Looping vars */
ef416fc2 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 */
fa73b229 59 int cost; /* Cost of filters */
ef416fc2 60 mime_t *mime; /* MIME database */
61 mime_type_t *src, /* Source type */
fa73b229 62 *dst; /* Destination type */
63 cups_array_t *filters; /* Filters for the file */
64 mime_filter_t *filter; /* Current filter */
ef416fc2 65
66
67 mime = NULL;
68 src = NULL;
69 dst = NULL;
fa73b229 70 filter_path = "../filter:../pdftops";
ef416fc2 71
72 for (i = 1; i < argc; i ++)
fa73b229 73 if (!strcmp(argv[i], "-d"))
ef416fc2 74 {
75 i ++;
76
77 if (i < argc)
78 mime = mimeLoad(argv[i], filter_path);
79 }
fa73b229 80 else if (!strcmp(argv[i], "-f"))
ef416fc2 81 {
82 i ++;
83
84 if (i < argc)
85 filter_path = argv[i];
86 }
fa73b229 87 else if (!src)
ef416fc2 88 {
89 if (!mime)
90 mime = mimeLoad("../conf", filter_path);
91
92 src = mimeFileType(mime, argv[i], &compression);
93
fa73b229 94 if (src)
ef416fc2 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
fa73b229 110 filters = mimeFilter(mime, src, dst, &cost, 10);
ef416fc2 111
fa73b229 112 if (!filters)
ef416fc2 113 {
114 printf("No filters to convert from %s/%s to %s.\n", src->super,
115 src->type, argv[i]);
116 }
117 else
118 {
fa73b229 119 printf("Filter cost = %d\n", cost);
ef416fc2 120
fa73b229 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);
ef416fc2 132 }
133 }
134
135 if (!mime)
136 mime = mimeLoad("../conf", filter_path);
137
fa73b229 138 if (!src)
ef416fc2 139 {
140 puts("MIME database types:");
fa73b229 141 for (src = mimeFirstType(mime); src; src = mimeNextType(mime))
ef416fc2 142 {
fa73b229 143 printf("\t%s/%s:\n", src->super, src->type);
144 print_rules(src->rules);
ef416fc2 145 puts("");
146 }
147
148 puts("");
149
150 puts("MIME database filters:");
fa73b229 151 for (filter = mimeFirstFilter(mime); filter; filter = mimeNextFilter(mime))
ef416fc2 152 printf("\t%s/%s to %s/%s: %s (%d)\n",
fa73b229 153 filter->src->super, filter->src->type,
154 filter->dst->super, filter->dst->type,
155 filter->filter, filter->cost);
ef416fc2 156 }
157
158 return (0);
159}
160
161
162/*
163 * 'print_rules()' - Print the rules for a file type...
164 */
165
166static void
167print_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/*
fa73b229 251 * End of "$Id: testmime.c 4970 2006-01-24 14:05:45Z mike $".
ef416fc2 252 */