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