]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/mime.c
2 * "$Id: mime.c,v 1.14 1999/07/12 16:09:38 mike Exp $"
4 * MIME database file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-1999 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * mimeDelete() - Delete (free) a MIME database.
27 * mimeMerge() - Merge a MIME database from disk with the current one.
28 * mimeNew() - Create a new, empty MIME database.
29 * load_types() - Load a xyz.types file...
30 * delete_rules() - Free all memory for the given rule tree.
31 * load_convs() - Load a xyz.convs file...
36 * Revision 1.14 1999/07/12 16:09:38 mike
37 * Fixed all constant arrays to use "const" modifier.
39 * Revision 1.13 1999/06/18 18:36:10 mike
40 * Fixed address to 44141 Airport View Drive...
42 * Revision 1.12 1999/04/21 21:19:33 mike
45 * Revision 1.11 1999/04/21 19:31:29 mike
46 * Changed the directory header stuff to use the autoconf-recommended
47 * sequence of #ifdef's.
49 * Changed the language routines to look for the LOCALEDIR environment
50 * variable, and if it is not defined to use the LOCALEDIR string defined
53 * Revision 1.10 1999/03/01 20:51:53 mike
54 * Code cleanup - removed extraneous semi-colons...
56 * Revision 1.9 1999/02/26 22:00:51 mike
57 * Added more debug statements.
59 * Fixed bugs in cupsPrintFile() - wasn't setting the IPP_TAG_MIMETYPE
60 * value tag for the file type.
62 * Updated conversion filter code to handle wildcards for super-type.
64 * Revision 1.8 1999/02/20 16:04:38 mike
65 * Updated mime.c to scan directories under WIN32.
67 * Fixed some compiler warnings under WIN32.
69 * Updated VC++ project files.
71 * Updated mime.types and mime.convs files for actual registered
74 * Revision 1.7 1999/02/05 17:40:53 mike
75 * Added IPP client read/write code.
77 * Added string functions missing from some UNIXs.
79 * Added option parsing functions.
81 * Added IPP convenience functions (not implemented yet).
83 * Updated source files to use local string.h as needed (for
84 * missing string functions)
86 * Revision 1.6 1999/02/01 22:08:39 mike
87 * Restored original directory-scanning functionality of mimeLoad().
89 * Revision 1.4 1999/01/27 18:31:56 mike
90 * Updated PPD routines to handle emulations and patch files.
92 * Added DSC comments to emit output as appropriate.
94 * Revision 1.3 1999/01/24 14:18:43 mike
95 * Check-in prior to CVS use.
97 * Revision 1.2 1998/08/06 14:38:38 mike
98 * Finished coding and testing for CUPS 1.0.
100 * Revision 1.1 1998/06/11 20:50:53 mike
105 * Include necessary headers...
115 #if defined(WIN32) || defined(__EMX__)
116 # include <windows.h>
119 typedef struct dirent DIRENT
;
120 # define NAMLEN(dirent) strlen((dirent)->d_name)
123 # include <sys/ndir.h>
126 # include <sys/dir.h>
131 typedef struct direct DIRENT
;
132 # define NAMLEN(dirent) (dirent)->d_namlen
140 static void load_types(mime_t
*mime
, char *filename
);
141 static void load_convs(mime_t
*mime
, char *filename
);
142 static void delete_rules(mime_magic_t
*rules
);
146 * 'mimeDelete()' - Delete (free) a MIME database.
150 mimeDelete(mime_t
*mime
) /* I - MIME database */
152 int i
; /* Looping var */
159 * Loop through the file types and delete any rules...
162 for (i
= 0; i
< mime
->num_types
; i
++)
164 delete_rules(mime
->types
[i
]->rules
);
165 free(mime
->types
[i
]);
169 * Free the types and filters arrays, and then the MIME database structure.
179 * 'mimeMerge()' - Merge a MIME database from disk with the current one.
182 mime_t
* /* O - Updated MIME database */
183 mimeMerge(mime_t
*mime
, /* I - MIME database to add to */
184 const char *pathname
) /* I - Directory to load */
186 #if defined(WIN32) || defined(__EMX__)
187 HANDLE dir
; /* Directory handle */
188 WIN32_FIND_DATA dent
; /* Directory entry */
189 char filename
[1024], /* Full filename of types/converts file */
190 *pathsep
; /* Last character in path */
194 * First open the directory specified by pathname... Return NULL if nothing
195 * was read or if the pathname is NULL...
198 if (pathname
== NULL
)
201 strcpy(filename
, pathname
);
202 pathsep
= filename
+ strlen(filename
);
203 if (pathsep
== filename
||
204 (pathsep
[-1] != '/' && pathsep
[-1] != '\\'))
206 strcpy(pathsep
, "/");
210 strcpy(pathsep
, "*.types");
212 if ((dir
= FindFirstFile(filename
, &dent
)) == 0)
216 * If "mime" is NULL, make a new, blank database...
220 if ((mime
= mimeNew()) == NULL
)
224 * Read all the .types files...
230 * Load a mime.types file...
233 strcpy(pathsep
, dent
.cFileName
);
234 load_types(mime
, filename
);
236 while (FindNextFile(dir
, &dent
));
241 * Read all the .convs files...
244 strcpy(pathsep
, "*.convs");
246 if ((dir
= FindFirstFile(filename
, &dent
)) == 0)
252 * Load a mime.convs file...
255 strcpy(pathsep
, dent
.cFileName
);
256 load_convs(mime
, filename
);
258 while (FindNextFile(dir
, &dent
));
264 DIR *dir
; /* Directory */
265 DIRENT
*dent
; /* Directory entry */
266 char filename
[1024]; /* Full filename of types/converts file */
270 * First open the directory specified by pathname... Return NULL if nothing
271 * was read or if the pathname is NULL...
274 if (pathname
== NULL
)
277 if ((dir
= opendir(pathname
)) == NULL
)
281 * If "mime" is NULL, make a new, blank database...
285 if ((mime
= mimeNew()) == NULL
)
289 * Read all the .types files...
292 while ((dent
= readdir(dir
)) != NULL
)
294 if (NAMLEN(dent
) > 6 &&
295 strcmp(dent
->d_name
+ NAMLEN(dent
) - 6, ".types") == 0)
298 * Load a mime.types file...
301 sprintf(filename
, "%s/%s", pathname
, dent
->d_name
);
302 load_types(mime
, filename
);
309 * Read all the .convs files...
312 while ((dent
= readdir(dir
)) != NULL
)
314 if (NAMLEN(dent
) > 6 &&
315 strcmp(dent
->d_name
+ NAMLEN(dent
) - 6, ".convs") == 0)
318 * Load a mime.convs file...
321 sprintf(filename
, "%s/%s", pathname
, dent
->d_name
);
322 load_convs(mime
, filename
);
329 #endif /* WIN32 || __EMX__ */
334 * 'mimeNew()' - Create a new, empty MIME database.
337 mime_t
* /* O - MIME database */
340 return ((mime_t
*)calloc(1, sizeof(mime_t
)));
345 * 'load_types()' - Load a xyz.types file...
349 load_types(mime_t
*mime
, /* I - MIME database */
350 char *filename
) /* I - Types file to load */
352 FILE *fp
; /* Types file */
353 int linelen
; /* Length of line */
354 char line
[65536], /* Input line from file */
355 *lineptr
, /* Current position in line */
356 super
[MIME_MAX_SUPER
], /* Super-type name */
357 type
[MIME_MAX_TYPE
], /* Type name */
358 *temp
; /* Temporary pointer */
359 mime_type_t
*typeptr
; /* New MIME type */
363 * First try to open the file...
366 if ((fp
= fopen(filename
, "r")) == NULL
)
370 * Then read each line from the file, skipping any comments in the file...
373 while (fgets(line
, sizeof(line
), fp
) != NULL
)
375 linelen
= strlen(line
);
378 * While the last character in the line is a backslash, continue on to the
379 * next line (and the next, etc.)
382 if (line
[linelen
- 1] == '\n')
384 line
[linelen
- 1] = '\0';
388 while (line
[linelen
- 1] == '\\')
392 if (fgets(line
+ linelen
, sizeof(line
) - linelen
, fp
) == NULL
)
393 line
[linelen
] = '\0';
396 linelen
+= strlen(line
+ linelen
);
397 if (line
[linelen
- 1] == '\n')
399 line
[linelen
- 1] = '\0';
406 * Skip blank lines and lines starting with a #...
409 if (line
[0] == '\n' || line
[0] == '#')
413 * Extract the super-type and type names from the beginning of the line.
419 while (*lineptr
!= '/' && *lineptr
!= '\n' && *lineptr
!= '\0' &&
420 (temp
- super
+ 1) < MIME_MAX_SUPER
)
421 *temp
++ = tolower(*lineptr
++);
431 while (*lineptr
!= ' ' && *lineptr
!= '\t' && *lineptr
!= '\n' &&
432 *lineptr
!= '\0' && (temp
- type
+ 1) < MIME_MAX_TYPE
)
433 *temp
++ = tolower(*lineptr
++);
438 * Add the type and rules to the MIME database...
441 typeptr
= mimeAddType(mime
, super
, type
);
442 mimeAddTypeRule(typeptr
, lineptr
);
448 * 'load_convs()' - Load a xyz.convs file...
452 load_convs(mime_t
*mime
, /* I - MIME database */
453 char *filename
) /* I - Convs file to load */
455 int i
; /* Looping var */
456 FILE *fp
; /* Convs file */
457 char line
[1024], /* Input line from file */
458 *lineptr
, /* Current position in line */
459 super
[MIME_MAX_SUPER
], /* Super-type name */
460 type
[MIME_MAX_TYPE
], /* Type name */
461 *temp
, /* Temporary pointer */
462 *filter
; /* Filter program */
463 mime_type_t
**temptype
, /* MIME type looping var */
464 *dsttype
; /* Destination MIME type */
465 int cost
; /* Cost of filter */
469 * First try to open the file...
472 if ((fp
= fopen(filename
, "r")) == NULL
)
476 * Then read each line from the file, skipping any comments in the file...
479 while (fgets(line
, sizeof(line
), fp
) != NULL
)
482 * Skip blank lines and lines starting with a #...
485 if (line
[0] == '\n' || line
[0] == '#')
489 * Extract the destination super-type and type names from the middle of
494 while (*lineptr
!= ' ' && *lineptr
!= '\t' && *lineptr
!= '\0')
497 while (*lineptr
== ' ' || *lineptr
== '\t')
502 while (*lineptr
!= '/' && *lineptr
!= '\n' && *lineptr
!= '\0' &&
503 (temp
- super
+ 1) < MIME_MAX_SUPER
)
504 *temp
++ = tolower(*lineptr
++);
514 while (*lineptr
!= ' ' && *lineptr
!= '\t' && *lineptr
!= '\n' &&
515 *lineptr
!= '\0' && (temp
- type
+ 1) < MIME_MAX_TYPE
)
516 *temp
++ = tolower(*lineptr
++);
520 if (*lineptr
== '\0' || *lineptr
== '\n')
523 if ((dsttype
= mimeType(mime
, super
, type
)) == NULL
)
527 * Then get the cost and filter program...
530 while (*lineptr
== ' ' || *lineptr
== '\t')
533 if (*lineptr
< '0' || *lineptr
> '9')
536 cost
= atoi(lineptr
);
538 while (*lineptr
!= ' ' && *lineptr
!= '\t' && *lineptr
!= '\0')
540 while (*lineptr
== ' ' || *lineptr
== '\t')
543 if (*lineptr
== '\0' || *lineptr
== '\n')
547 if (filter
[strlen(filter
) - 1] == '\n')
548 filter
[strlen(filter
) - 1] = '\0';
551 * Finally, get the source super-type and type names from the beginning of
552 * the line. We do it here so we can support wildcards...
558 while (*lineptr
!= '/' && *lineptr
!= '\n' && *lineptr
!= '\0' &&
559 (temp
- super
+ 1) < MIME_MAX_SUPER
)
560 *temp
++ = tolower(*lineptr
++);
570 while (*lineptr
!= ' ' && *lineptr
!= '\t' && *lineptr
!= '\n' &&
571 *lineptr
!= '\0' && (temp
- type
+ 1) < MIME_MAX_TYPE
)
572 *temp
++ = tolower(*lineptr
++);
577 * Add the filter to the MIME database, supporting wildcards as needed...
580 for (temptype
= mime
->types
, i
= 0; i
< mime
->num_types
; i
++, temptype
++)
581 if ((super
[0] == '*' || strcmp((*temptype
)->super
, super
) == 0) &&
582 (type
[0] == '*' || strcmp((*temptype
)->type
, type
) == 0))
583 mimeAddFilter(mime
, *temptype
, dsttype
, cost
, filter
);
589 * 'delete_rules()' - Free all memory for the given rule tree.
593 delete_rules(mime_magic_t
*rules
) /* I - Rules to free */
595 mime_magic_t
*next
; /* Next rule to free */
599 * Free the rules list, descending recursively to free any child rules.
602 while (rules
!= NULL
)
606 if (rules
->child
!= NULL
)
607 delete_rules(rules
->child
);
616 * End of "$Id: mime.c,v 1.14 1999/07/12 16:09:38 mike Exp $".