]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/mime.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / mime.c
1 /*
2 * "$Id: mime.c 5605 2006-05-30 19:38:02Z mike $"
3 *
4 * MIME database file routines 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 * mimeDelete() - Delete (free) a MIME database.
27 * mimeDeleteFilter() - Delete a filter from the MIME database.
28 * mimeDeleteType() - Delete a type from the MIME database.
29 * mimeFirstFilter() - Get the first filter in the MIME database.
30 * mimeFirstType() - Get the first type in the MIME database.
31 * mimeLoad() - Create a new MIME database from disk.
32 * mimeMerge() - Merge a MIME database from disk with the current one.
33 * mimeNew() - Create a new, empty MIME database.
34 * mimeNextFilter() - Get the next filter in the MIME database.
35 * mimeNextType() - Get the next type in the MIME database.
36 * mimeNumFilters() - Get the number of filters in a MIME database.
37 * mimeNumTypes() - Get the number of types in a MIME database.
38 * add_fcache() - Add a filter to the filter cache.
39 * compare_fcache() - Compare two filter cache entries.
40 * delete_fcache() - Free all memory used by the filter cache.
41 * delete_rules() - Free all memory for the given rule tree.
42 * load_convs() - Load a xyz.convs file...
43 * load_types() - Load a xyz.types file...
44 */
45
46 /*
47 * Include necessary headers...
48 */
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <ctype.h>
53
54 #include <cups/debug.h>
55 #include <cups/dir.h>
56 #include <cups/string.h>
57 #include "mime.h"
58
59
60 /*
61 * Local types...
62 */
63
64 typedef struct _mime_fcache_s /**** Filter cache structure ****/
65 {
66 char *name, /* Filter name */
67 *path; /* Full path to filter if available */
68 } _mime_fcache_t;
69
70
71 /*
72 * Local functions...
73 */
74
75 static const char *add_fcache(cups_array_t *filtercache, const char *name,
76 const char *filterpath);
77 static int compare_fcache(_mime_fcache_t *a, _mime_fcache_t *b);
78 static void delete_fcache(cups_array_t *filtercache);
79 static void delete_rules(mime_magic_t *rules);
80 static void load_convs(mime_t *mime, const char *filename,
81 const char *filterpath,
82 cups_array_t *filtercache);
83 static void load_types(mime_t *mime, const char *filename);
84
85
86 /*
87 * 'mimeDelete()' - Delete (free) a MIME database.
88 */
89
90 void
91 mimeDelete(mime_t *mime) /* I - MIME database */
92 {
93 mime_type_t *type; /* Current type */
94 mime_filter_t *filter; /* Current filter */
95
96
97 if (!mime)
98 return;
99
100 /*
101 * Loop through filters and free them...
102 */
103
104 for (filter = (mime_filter_t *)cupsArrayFirst(mime->filters);
105 filter;
106 filter = (mime_filter_t *)cupsArrayNext(mime->filters))
107 mimeDeleteFilter(mime, filter);
108
109 /*
110 * Loop through the file types and delete any rules...
111 */
112
113 for (type = (mime_type_t *)cupsArrayFirst(mime->types);
114 type;
115 type = (mime_type_t *)cupsArrayNext(mime->types))
116 mimeDeleteType(mime, type);
117
118 /*
119 * Free the types and filters arrays, and then the MIME database structure.
120 */
121
122 cupsArrayDelete(mime->types);
123 cupsArrayDelete(mime->filters);
124 cupsArrayDelete(mime->srcs);
125 free(mime);
126 }
127
128
129 /*
130 * 'mimeDeleteFilter()' - Delete a filter from the MIME database.
131 */
132
133 void
134 mimeDeleteFilter(mime_t *mime, /* I - MIME database */
135 mime_filter_t *filter) /* I - Filter */
136 {
137 if (!mime || !filter)
138 return;
139
140 cupsArrayRemove(mime->filters, filter);
141 free(filter);
142
143 /*
144 * Deleting a filter invalidates the source lookup cache used by
145 * mimeFilter()...
146 */
147
148 if (mime->srcs)
149 {
150 cupsArrayDelete(mime->srcs);
151 mime->srcs = NULL;
152 }
153 }
154
155
156 /*
157 * 'mimeDeleteType()' - Delete a type from the MIME database.
158 */
159
160 void
161 mimeDeleteType(mime_t *mime, /* I - MIME database */
162 mime_type_t *mt) /* I - Type */
163 {
164 if (!mime || !mt)
165 return;
166
167 cupsArrayRemove(mime->types, mt);
168
169 delete_rules(mt->rules);
170 free(mt);
171 }
172
173
174 /*
175 * 'mimeFirstFilter()' - Get the first filter in the MIME database.
176 */
177
178 mime_filter_t * /* O - Filter or NULL */
179 mimeFirstFilter(mime_t *mime) /* I - MIME database */
180 {
181 if (!mime)
182 return (NULL);
183 else
184 return ((mime_filter_t *)cupsArrayFirst(mime->filters));
185 }
186
187
188 /*
189 * 'mimeFirstType()' - Get the first type in the MIME database.
190 */
191
192 mime_type_t * /* O - Type or NULL */
193 mimeFirstType(mime_t *mime) /* I - MIME database */
194 {
195 if (!mime)
196 return (NULL);
197 else
198 return ((mime_type_t *)cupsArrayFirst(mime->types));
199 }
200
201
202 /*
203 * 'mimeLoad()' - Create a new MIME database from disk.
204 */
205
206 mime_t * /* O - New MIME database */
207 mimeLoad(const char *pathname, /* I - Directory to load */
208 const char *filterpath) /* I - Directory to load */
209 {
210 return (mimeMerge(NULL, pathname, filterpath));
211 }
212
213
214 /*
215 * 'mimeMerge()' - Merge a MIME database from disk with the current one.
216 */
217
218 mime_t * /* O - Updated MIME database */
219 mimeMerge(mime_t *mime, /* I - MIME database to add to */
220 const char *pathname, /* I - Directory to load */
221 const char *filterpath) /* I - Directory to load */
222 {
223 cups_dir_t *dir; /* Directory */
224 cups_dentry_t *dent; /* Directory entry */
225 char filename[1024]; /* Full filename of types/converts file */
226 cups_array_t *filtercache; /* Filter cache */
227
228
229 /*
230 * First open the directory specified by pathname... Return NULL if nothing
231 * was read or if the pathname is NULL...
232 */
233
234 if (!pathname)
235 return (NULL);
236
237 if ((dir = cupsDirOpen(pathname)) == NULL)
238 return (NULL);
239
240 /*
241 * If "mime" is NULL, make a new, blank database...
242 */
243
244 if (!mime)
245 mime = mimeNew();
246 if (!mime)
247 return (NULL);
248
249 /*
250 * Read all the .types files...
251 */
252
253 while ((dent = cupsDirRead(dir)) != NULL)
254 {
255 if (strlen(dent->filename) > 6 &&
256 !strcmp(dent->filename + strlen(dent->filename) - 6, ".types"))
257 {
258 /*
259 * Load a mime.types file...
260 */
261
262 snprintf(filename, sizeof(filename), "%s/%s", pathname, dent->filename);
263 load_types(mime, filename);
264 }
265 }
266
267 cupsDirRewind(dir);
268
269 /*
270 * Read all the .convs files...
271 */
272
273 filtercache = cupsArrayNew((cups_array_func_t)compare_fcache, NULL);
274
275 while ((dent = cupsDirRead(dir)) != NULL)
276 {
277 if (strlen(dent->filename) > 6 &&
278 !strcmp(dent->filename + strlen(dent->filename) - 6, ".convs"))
279 {
280 /*
281 * Load a mime.convs file...
282 */
283
284 snprintf(filename, sizeof(filename), "%s/%s", pathname, dent->filename);
285 load_convs(mime, filename, filterpath, filtercache);
286 }
287 }
288
289 delete_fcache(filtercache);
290
291 cupsDirClose(dir);
292
293 return (mime);
294 }
295
296
297 /*
298 * 'mimeNew()' - Create a new, empty MIME database.
299 */
300
301 mime_t * /* O - MIME database */
302 mimeNew(void)
303 {
304 return ((mime_t *)calloc(1, sizeof(mime_t)));
305 }
306
307
308 /*
309 * 'mimeNextFilter()' - Get the next filter in the MIME database.
310 */
311
312 mime_filter_t * /* O - Filter or NULL */
313 mimeNextFilter(mime_t *mime) /* I - MIME database */
314 {
315 if (!mime)
316 return (NULL);
317 else
318 return ((mime_filter_t *)cupsArrayNext(mime->filters));
319 }
320
321
322 /*
323 * 'mimeNextType()' - Get the next type in the MIME database.
324 */
325
326 mime_type_t * /* O - Type or NULL */
327 mimeNextType(mime_t *mime) /* I - MIME database */
328 {
329 if (!mime)
330 return (NULL);
331 else
332 return ((mime_type_t *)cupsArrayNext(mime->types));
333 }
334
335
336 /*
337 * 'mimeNumFilters()' - Get the number of filters in a MIME database.
338 */
339
340 int
341 mimeNumFilters(mime_t *mime) /* I - MIME database */
342 {
343 if (!mime)
344 return (0);
345 else
346 return (cupsArrayCount(mime->filters));
347 }
348
349
350 /*
351 * 'mimeNumTypes()' - Get the number of types in a MIME database.
352 */
353
354 int
355 mimeNumTypes(mime_t *mime) /* I - MIME database */
356 {
357 if (!mime)
358 return (0);
359 else
360 return (cupsArrayCount(mime->types));
361 }
362
363
364 /*
365 * 'add_fcache()' - Add a filter to the filter cache.
366 */
367
368 static const char * /* O - Full path to filter or NULL */
369 add_fcache(cups_array_t *filtercache, /* I - Filter cache */
370 const char *name, /* I - Filter name */
371 const char *filterpath) /* I - Filter path */
372 {
373 _mime_fcache_t key, /* Search key */
374 *temp; /* New filter cache */
375 char path[1024]; /* Full path to filter */
376
377
378 key.name = (char *)name;
379 if ((temp = (_mime_fcache_t *)cupsArrayFind(filtercache, &key)) != NULL)
380 return (temp->path);
381
382 if ((temp = calloc(1, sizeof(_mime_fcache_t))) == NULL)
383 return (NULL);
384
385 temp->name = strdup(name);
386
387 if (cupsFileFind(name, filterpath, 1, path, sizeof(path)))
388 temp->path = strdup(path);
389
390 cupsArrayAdd(filtercache, temp);
391
392 return (temp->path);
393 }
394
395
396 /*
397 * 'compare_fcache()' - Compare two filter cache entries.
398 */
399
400 static int /* O - Result of comparison */
401 compare_fcache(_mime_fcache_t *a, /* I - First entry */
402 _mime_fcache_t *b) /* I - Second entry */
403 {
404 return (strcmp(a->name, b->name));
405 }
406
407
408 /*
409 * 'delete_fcache()' - Free all memory used by the filter cache.
410 */
411
412 static void
413 delete_fcache(cups_array_t *filtercache)/* I - Filter cache */
414 {
415 _mime_fcache_t *current; /* Current cache entry */
416
417
418 for (current = (_mime_fcache_t *)cupsArrayFirst(filtercache);
419 current;
420 current = (_mime_fcache_t *)cupsArrayNext(filtercache))
421 {
422 free(current->name);
423
424 if (current->path)
425 free(current->path);
426
427 free(current);
428 }
429
430 cupsArrayDelete(filtercache);
431 }
432
433
434 /*
435 * 'delete_rules()' - Free all memory for the given rule tree.
436 */
437
438 static void
439 delete_rules(mime_magic_t *rules) /* I - Rules to free */
440 {
441 mime_magic_t *next; /* Next rule to free */
442
443
444 /*
445 * Free the rules list, descending recursively to free any child rules.
446 */
447
448 while (rules != NULL)
449 {
450 next = rules->next;
451
452 if (rules->child != NULL)
453 delete_rules(rules->child);
454
455 free(rules);
456 rules = next;
457 }
458 }
459
460
461 /*
462 * 'load_convs()' - Load a xyz.convs file...
463 */
464
465 static void
466 load_convs(mime_t *mime, /* I - MIME database */
467 const char *filename, /* I - Convs file to load */
468 const char *filterpath, /* I - Path for filters */
469 cups_array_t *filtercache) /* I - Filter program cache */
470 {
471 cups_file_t *fp; /* Convs file */
472 char line[1024], /* Input line from file */
473 *lineptr, /* Current position in line */
474 super[MIME_MAX_SUPER], /* Super-type name */
475 type[MIME_MAX_TYPE], /* Type name */
476 *temp, /* Temporary pointer */
477 *filter; /* Filter program */
478 mime_type_t *temptype, /* MIME type looping var */
479 *dsttype; /* Destination MIME type */
480 int cost; /* Cost of filter */
481
482
483 /*
484 * First try to open the file...
485 */
486
487 if ((fp = cupsFileOpen(filename, "r")) == NULL)
488 return;
489
490 DEBUG_printf(("\"%s\":\n", filename));
491
492 /*
493 * Then read each line from the file, skipping any comments in the file...
494 */
495
496 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
497 {
498 /*
499 * Skip blank lines and lines starting with a #...
500 */
501
502 DEBUG_puts(line);
503
504 if (!line[0] || line[0] == '#')
505 continue;
506
507 /*
508 * Strip trailing whitespace...
509 */
510
511 for (lineptr = line + strlen(line) - 1;
512 lineptr >= line && isspace(*lineptr & 255);
513 lineptr --)
514 *lineptr = '\0';
515
516 /*
517 * Extract the destination super-type and type names from the middle of
518 * the line.
519 */
520
521 lineptr = line;
522 while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\0')
523 lineptr ++;
524
525 while (*lineptr == ' ' || *lineptr == '\t')
526 lineptr ++;
527
528 temp = super;
529
530 while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
531 (temp - super + 1) < MIME_MAX_SUPER)
532 *temp++ = tolower(*lineptr++ & 255);
533
534 *temp = '\0';
535
536 if (*lineptr != '/')
537 continue;
538
539 lineptr ++;
540 temp = type;
541
542 while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
543 *lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
544 *temp++ = tolower(*lineptr++ & 255);
545
546 *temp = '\0';
547
548 if (*lineptr == '\0' || *lineptr == '\n')
549 continue;
550
551 if ((dsttype = mimeType(mime, super, type)) == NULL)
552 {
553 DEBUG_printf((" Destination type %s/%s not found!\n", super, type));
554 continue;
555 }
556
557 /*
558 * Then get the cost and filter program...
559 */
560
561 while (*lineptr == ' ' || *lineptr == '\t')
562 lineptr ++;
563
564 if (*lineptr < '0' || *lineptr > '9')
565 continue;
566
567 cost = atoi(lineptr);
568
569 while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\0')
570 lineptr ++;
571 while (*lineptr == ' ' || *lineptr == '\t')
572 lineptr ++;
573
574 if (*lineptr == '\0' || *lineptr == '\n')
575 continue;
576
577 filter = lineptr;
578
579 if (strcmp(filter, "-"))
580 {
581 /*
582 * Verify that the filter exists and is executable...
583 */
584
585 if (!add_fcache(filtercache, filter, filterpath))
586 {
587 DEBUG_printf((" Filter %s not found in %s!\n", filter, filterpath));
588 continue;
589 }
590 }
591
592 /*
593 * Finally, get the source super-type and type names from the beginning of
594 * the line. We do it here so we can support wildcards...
595 */
596
597 lineptr = line;
598 temp = super;
599
600 while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
601 (temp - super + 1) < MIME_MAX_SUPER)
602 *temp++ = tolower(*lineptr++ & 255);
603
604 *temp = '\0';
605
606 if (*lineptr != '/')
607 continue;
608
609 lineptr ++;
610 temp = type;
611
612 while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
613 *lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
614 *temp++ = tolower(*lineptr++ & 255);
615
616 *temp = '\0';
617
618 if (!strcmp(super, "*") && !strcmp(type, "*"))
619 {
620 /*
621 * Force * / * to be "application/octet-stream"...
622 */
623
624 strcpy(super, "application");
625 strcpy(type, "octet-stream");
626 }
627
628 /*
629 * Add the filter to the MIME database, supporting wildcards as needed...
630 */
631
632 for (temptype = (mime_type_t *)cupsArrayFirst(mime->types);
633 temptype;
634 temptype = (mime_type_t *)cupsArrayNext(mime->types))
635 if ((super[0] == '*' || !strcmp(temptype->super, super)) &&
636 (type[0] == '*' || !strcmp(temptype->type, type)))
637 mimeAddFilter(mime, temptype, dsttype, cost, filter);
638 }
639
640 cupsFileClose(fp);
641 }
642
643
644 /*
645 * 'load_types()' - Load a xyz.types file...
646 */
647
648 static void
649 load_types(mime_t *mime, /* I - MIME database */
650 const char *filename) /* I - Types file to load */
651 {
652 cups_file_t *fp; /* Types file */
653 int linelen; /* Length of line */
654 char line[32768], /* Input line from file */
655 *lineptr, /* Current position in line */
656 super[MIME_MAX_SUPER], /* Super-type name */
657 type[MIME_MAX_TYPE], /* Type name */
658 *temp; /* Temporary pointer */
659 mime_type_t *typeptr; /* New MIME type */
660
661
662 /*
663 * First try to open the file...
664 */
665
666 if ((fp = cupsFileOpen(filename, "r")) == NULL)
667 return;
668
669 DEBUG_printf(("\"%s\":\n", filename));
670
671 /*
672 * Then read each line from the file, skipping any comments in the file...
673 */
674
675 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
676 {
677 /*
678 * Skip blank lines and lines starting with a #...
679 */
680
681 DEBUG_puts(line);
682
683 if (!line[0] || line[0] == '#')
684 continue;
685
686 /*
687 * While the last character in the line is a backslash, continue on to the
688 * next line (and the next, etc.)
689 */
690
691 linelen = strlen(line);
692
693 while (line[linelen - 1] == '\\')
694 {
695 linelen --;
696
697 if (cupsFileGets(fp, line + linelen, sizeof(line) - linelen) == NULL)
698 line[linelen] = '\0';
699 else
700 linelen += strlen(line + linelen);
701 }
702
703 /*
704 * Extract the super-type and type names from the beginning of the line.
705 */
706
707 lineptr = line;
708 temp = super;
709
710 while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
711 (temp - super + 1) < MIME_MAX_SUPER)
712 *temp++ = tolower(*lineptr++ & 255);
713
714 *temp = '\0';
715
716 if (*lineptr != '/')
717 continue;
718
719 lineptr ++;
720 temp = type;
721
722 while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
723 *lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
724 *temp++ = tolower(*lineptr++ & 255);
725
726 *temp = '\0';
727
728 /*
729 * Add the type and rules to the MIME database...
730 */
731
732 typeptr = mimeAddType(mime, super, type);
733 mimeAddTypeRule(typeptr, lineptr);
734 }
735
736 cupsFileClose(fp);
737 }
738
739
740 /*
741 * End of "$Id: mime.c 5605 2006-05-30 19:38:02Z mike $".
742 */