| 1 |
/* Copyright (c) 1994-1999 All rights reserved. */ |
| 2 |
/* Written from scratch July 30, 1994, by A. P. Smith. */ |
| 3 |
|
| 4 |
/* Copyright (c) 2003-2004 the xdvik development team */ |
| 5 |
|
| 6 |
/* |
| 7 |
Read mime.types and mailcap entries to check what |
| 8 |
viewers to invoke on a new file. |
| 9 |
|
| 10 |
Patch by Allin Cottrell ([email protected]) to |
| 11 |
invokeviewer applied 30/11/98. |
| 12 |
|
| 13 |
Patched further in january 1999 by Nicolai Langfeldt |
| 14 |
([email protected]) to allow saner mime typing. |
| 15 |
|
| 16 |
Rewritten by Stefan Ulrich <[email protected]> |
| 17 |
on 2003/03/25 for better RFC 1343 conformance. |
| 18 |
|
| 19 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 20 |
of this software and associated documentation files (the "Software"), to |
| 21 |
deal in the Software without restriction, including without limitation the |
| 22 |
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 23 |
sell copies of the Software, and to permit persons to whom the Software is |
| 24 |
furnished to do so, subject to the following conditions: |
| 25 |
|
| 26 |
The above copyright notice and this permission notice shall be included in |
| 27 |
all copies or substantial portions of the Software. |
| 28 |
|
| 29 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 32 |
IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE |
| 33 |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 |
|
| 37 |
*/ |
| 38 |
|
| 39 |
#include "xdvi-config.h" |
| 40 |
#include "xdvi.h" |
| 41 |
#include "hypertex.h" |
| 42 |
#include "xdvi-debug.h" |
| 43 |
#include "util.h" |
| 44 |
#include "string-utils.h" |
| 45 |
#include "mime.h" |
| 46 |
#include "browser.h" |
| 47 |
#include "message-window.h" |
| 48 |
|
| 49 |
#include <ctype.h> |
| 50 |
#include "kpathsea/c-fopen.h" |
| 51 |
#include "kpathsea/variable.h" |
| 52 |
#include "kpathsea/c-pathmx.h" |
| 53 |
|
| 54 |
/* default settings if $EXTENSIONMAPS and $MAILCAPS is not set */ |
| 55 |
|
| 56 |
static const char *const DEFAULT_EXTENSIONMAPS = |
| 57 |
"$HOME/.mime.types:/etc/mime.types:/usr/etc/mime.types:/usr/local/etc/mime.types"; |
| 58 |
|
| 59 |
static const char *const DEFAULT_MAILCAPS = |
| 60 |
"$HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap"; |
| 61 |
|
| 62 |
struct mime_map { |
| 63 |
char *content_type; |
| 64 |
char *extensions; |
| 65 |
}; |
| 66 |
|
| 67 |
static struct mime_map default_mimemap[] = { |
| 68 |
{"application/postscript", "ai eps epsf ps",}, |
| 69 |
{"application/octet-stream", "bin",}, |
| 70 |
{"application/oda", "oda",}, |
| 71 |
{"application/pdf", "pdf",}, |
| 72 |
{"application/rtf", "rtf",}, |
| 73 |
{"application/x-mif", "mif",}, |
| 74 |
{"application/x-csh", "csh",}, |
| 75 |
{"application/x-dvi", "dvi Dvi DVI",}, |
| 76 |
{"application/x-hdf", "hdf",}, |
| 77 |
{"application/x-latex", "latex",}, |
| 78 |
{"application/x-netcdf", "nc cdf",}, |
| 79 |
{"application/x-sh", "sh",}, |
| 80 |
{"application/x-tcl", "tcl",}, |
| 81 |
{"application/x-tex", "tex",}, |
| 82 |
{"application/x-texinfo", "texinfo texi",}, |
| 83 |
{"application/x-troff", "t tr roff",}, |
| 84 |
{"application/x-troff-man", "man",}, |
| 85 |
{"application/x-troff-me", "me",}, |
| 86 |
{"application/x-troff-ms", "ms",}, |
| 87 |
{"application/x-wais-source", "src",}, |
| 88 |
{"application/zip", "zip",}, |
| 89 |
{"application/x-bcpio", "bcpio",}, |
| 90 |
{"application/x-cpio", "cpio",}, |
| 91 |
{"application/x-gtar", "gtar",}, |
| 92 |
{"application/x-shar", "shar",}, |
| 93 |
{"application/x-sv4cpio", "sv4cpio",}, |
| 94 |
{"application/x-sv4crc", "sv4crc",}, |
| 95 |
{"application/x-tar", "tar",}, |
| 96 |
{"application/x-ustar", "ustar",}, |
| 97 |
{"audio/basic", "au snd",}, |
| 98 |
{"audio/x-aiff", "aif aiff aifc",}, |
| 99 |
{"audio/x-wav", "wav",}, |
| 100 |
{"image/gif", "gif",}, |
| 101 |
{"image/ief", "ief",}, |
| 102 |
{"image/jpeg", "jpeg jpg jpe",}, |
| 103 |
{"image/tiff", "tiff tif",}, |
| 104 |
{"image/x-cmu-raster", "ras",}, |
| 105 |
{"image/x-portable-anymap", "pnm",}, |
| 106 |
{"image/x-portable-bitmap", "pbm",}, |
| 107 |
{"image/x-portable-graymap", "pgm",}, |
| 108 |
{"image/x-portable-pixmap", "ppm",}, |
| 109 |
{"image/x-rgb", "rgb",}, |
| 110 |
{"image/x-xbitmap", "xbm",}, |
| 111 |
{"image/x-xpixmap", "xpm",}, |
| 112 |
{"image/x-xwindowdump", "xwd",}, |
| 113 |
{"text/html", "html htm sht shtml",}, |
| 114 |
{"text/plain", "txt",}, |
| 115 |
{"text/richtext", "rtx",}, |
| 116 |
{"text/tab-separated-values", "tsv",}, |
| 117 |
{"text/x-setext", "etx",}, |
| 118 |
{"video/mpeg", "mpeg mpg mpe",}, |
| 119 |
{"video/quicktime", "qt mov",}, |
| 120 |
{"video/x-msvideo", "avi",}, |
| 121 |
{"video/x-sgi-movie", "movie",}, |
| 122 |
{"application/gzip", "gz",}, |
| 123 |
{"application/compress", "Z",}, |
| 124 |
{"application/bzip", "bz",}, |
| 125 |
{"application/bzip2", "bz2",}, |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
struct mailcap_map { |
| 130 |
char *content_type; |
| 131 |
const char *command; /* command string */ |
| 132 |
const char *testcmd; /* value of the `test=' field, or NULL if no test field present */ |
| 133 |
Boolean needsterminal; /* whether the `needsterminal' flag has been specified for this entry */ |
| 134 |
const char *format_string; /* format specifier ("%s" or "%u" or "") */ |
| 135 |
}; |
| 136 |
|
| 137 |
static struct mailcap_map default_mailcap[] = { |
| 138 |
{"audio/*", "showaudio %s", NULL, False, "%s"}, |
| 139 |
{"image/*", "xv %s", NULL, False, "%s"}, |
| 140 |
{"video/mpeg", "mpeg_play %s", NULL, False, "%s"}, |
| 141 |
{"application/pdf", "acroread %s", NULL, False, "%s"}, |
| 142 |
{"text/html", "netscape-raise -remote 'openURL(%s,new-window)'", NULL, False, "%s"}, |
| 143 |
{"application/postscript", "ghostview %s", NULL, False, "%s"}, |
| 144 |
{"application/x-dvi", "xdvi %s", NULL, False, "%s"}, |
| 145 |
}; |
| 146 |
|
| 147 |
static struct mime_map *m_mimemap = NULL; |
| 148 |
static int m_mimemap_currlen = 0; |
| 149 |
static int m_mimemap_size = 0; |
| 150 |
|
| 151 |
static struct mailcap_map *m_mailcap = NULL; |
| 152 |
static int m_mailcap_currlen, m_mailcap_size = 0; |
| 153 |
|
| 154 |
static const size_t MIME_ALLOC_STEP = 64; |
| 155 |
|
| 156 |
/* Utility function: |
| 157 |
Read a line from a config file, dealing with continued lines |
| 158 |
(ending with '\[ \t]*'). |
| 159 |
The *linebuf argument is re-alloced as needed from its initial size alloc_len. |
| 160 |
The length of the read line is returned as ret_len. |
| 161 |
The lineno int is by the number of lines read. |
| 162 |
Trailing whitespace and newline is removed, and either the line, |
| 163 |
or NULL for EOF is returned. |
| 164 |
*/ |
| 165 |
static char * |
| 166 |
read_config_line(const char *filename, FILE *fp, char **linebuf, |
| 167 |
size_t alloc_len, size_t *ret_len, |
| 168 |
int *lineno) |
| 169 |
{ |
| 170 |
Boolean read_complete_line = False; |
| 171 |
*ret_len = 0; |
| 172 |
while (!read_complete_line) { |
| 173 |
if ((fgets((*linebuf) + *ret_len, alloc_len - *ret_len, fp)) == NULL) { |
| 174 |
if (*ret_len == 0) /* at EOF */ |
| 175 |
return NULL; |
| 176 |
else /* nothing read, but we have a result from previous loop iteration */ |
| 177 |
return *linebuf; |
| 178 |
} |
| 179 |
(*lineno)++; |
| 180 |
*ret_len = strlen(*linebuf); |
| 181 |
if (*ret_len > 0 && (*linebuf)[*ret_len - 1] != '\n') { |
| 182 |
/* catch the special case of missing NL at EOF */ |
| 183 |
if (*ret_len < alloc_len - 1) { |
| 184 |
XDVI_WARNING((stderr, "%s, line %d: missing newline at end of file.", filename, *lineno)); |
| 185 |
return *linebuf; |
| 186 |
} |
| 187 |
/* buffer too short, need to re-allocate */ |
| 188 |
alloc_len *= 2; |
| 189 |
*linebuf = xrealloc(*linebuf, alloc_len); |
| 190 |
} |
| 191 |
else if (*ret_len > 2 |
| 192 |
&& (*linebuf)[*ret_len - 1] == '\n' |
| 193 |
&& ((*linebuf)[*ret_len - 2] == '\\' |
| 194 |
|| isspace((int)(*linebuf)[*ret_len - 2]))) { |
| 195 |
/* we may have a continued line */ |
| 196 |
/* chop off trailing whitespace */ |
| 197 |
while (*ret_len > 1 && isspace((int)(*linebuf)[*ret_len - 2])) |
| 198 |
(*ret_len)--; |
| 199 |
if (*ret_len > 1 && (*linebuf)[*ret_len - 2] != '\\') { /* no continued line */ |
| 200 |
read_complete_line = True; |
| 201 |
} |
| 202 |
else /* overwrite backslash and newline at next read */ |
| 203 |
*ret_len -= 2; |
| 204 |
} |
| 205 |
else |
| 206 |
read_complete_line = True; |
| 207 |
} |
| 208 |
/* chop off trailing NL */ |
| 209 |
(*linebuf)[--(*ret_len)] = '\0'; |
| 210 |
return *linebuf; |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
read_mime_file(FILE *fp, const char *filename, int *mime_line) |
| 215 |
{ |
| 216 |
int i; |
| 217 |
const size_t line_len = 1024; |
| 218 |
size_t ret_len = 0; |
| 219 |
char *linebuf = xmalloc(line_len); |
| 220 |
char *cp, *cp2; |
| 221 |
|
| 222 |
int dummy = 0; |
| 223 |
TRACE_HTEX((stderr, "reading mime file \"%s\"", filename)); |
| 224 |
while ((read_config_line(filename, fp, &linebuf, line_len, &ret_len, &dummy)) != NULL) { |
| 225 |
if (linebuf[ret_len] == '\n') |
| 226 |
linebuf[ret_len] = '\0'; |
| 227 |
|
| 228 |
cp = linebuf; |
| 229 |
while (isspace((int)*cp)) |
| 230 |
cp++; |
| 231 |
if (*cp == '#') |
| 232 |
continue; |
| 233 |
|
| 234 |
/* split either on tab or on whitespace */ |
| 235 |
if ((cp2 = strchr(cp, '\t')) == NULL && (cp2 = strchr(cp, ' ')) == NULL) |
| 236 |
continue; |
| 237 |
*cp2 = '\0'; /* Terminate cp string */ |
| 238 |
cp2++; |
| 239 |
while (isspace((int)*cp2)) |
| 240 |
cp2++; |
| 241 |
if (*cp2 == '\0') |
| 242 |
continue; /* No extensions list */ |
| 243 |
if (*mime_line >= m_mimemap_size) { |
| 244 |
m_mimemap_size += MIME_ALLOC_STEP; |
| 245 |
m_mimemap = xrealloc(m_mimemap, m_mimemap_size * sizeof *m_mimemap); |
| 246 |
for (i = *mime_line; i < m_mimemap_size; i++) { |
| 247 |
m_mimemap[i].content_type = m_mimemap[i].extensions = NULL; |
| 248 |
} |
| 249 |
} |
| 250 |
#if 0 |
| 251 |
fprintf(stderr, "========== type %d: |%s| extension: |%s|\n", *mime_line, cp, cp2); |
| 252 |
#endif |
| 253 |
free(m_mimemap[*mime_line].content_type); |
| 254 |
m_mimemap[*mime_line].content_type = xstrdup(cp); |
| 255 |
free(m_mimemap[*mime_line].extensions); |
| 256 |
m_mimemap[*mime_line].extensions = xstrdup(cp2); |
| 257 |
|
| 258 |
(*mime_line)++; |
| 259 |
} |
| 260 |
free(linebuf); |
| 261 |
} |
| 262 |
|
| 263 |
static char * |
| 264 |
maybe_expand_homedir(const char *path) |
| 265 |
{ |
| 266 |
char *newpath = NULL; |
| 267 |
TRACE_HTEX((stderr, "maybe_expand_homedir: |%s|", path)); |
| 268 |
if (memcmp(path, "$HOME", strlen("$HOME")) == 0) { |
| 269 |
newpath = xstrdup(getenv("HOME")); |
| 270 |
newpath = xstrcat(newpath, path + strlen("$HOME")); |
| 271 |
} |
| 272 |
else if (path[0] == '~') { |
| 273 |
newpath = expand_homedir(path); |
| 274 |
if (newpath == NULL) { |
| 275 |
XDVI_WARNING((stderr, "Couldn't expand path `%s'", path)); |
| 276 |
} |
| 277 |
} |
| 278 |
TRACE_HTEX((stderr, "maybe_expand_homedir: after expansion: |%s|", newpath ? newpath : "<NULL>")); |
| 279 |
return newpath; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
static void |
| 284 |
read_mailcap(FILE *fp, const char *filename, int *mailcap_line) |
| 285 |
{ |
| 286 |
size_t alloc_len = 1024; |
| 287 |
size_t ret_len = 0; |
| 288 |
char *linebuf = xmalloc(alloc_len); |
| 289 |
int lineno = 0; |
| 290 |
|
| 291 |
TRACE_HTEX((stderr, "reading mailcap file \"%s\"", filename)); |
| 292 |
|
| 293 |
while ((read_config_line(filename, fp, &linebuf, alloc_len, &ret_len, &lineno)) != NULL) { |
| 294 |
Boolean error = False; |
| 295 |
int i; |
| 296 |
size_t n, num_items; |
| 297 |
char **items; |
| 298 |
char *ptr = linebuf; |
| 299 |
|
| 300 |
while (isspace((int)*ptr)) { |
| 301 |
ptr++; |
| 302 |
ret_len--; |
| 303 |
} |
| 304 |
if (*ptr == '#' || *ptr == '\0') /* comments or empty lines */ |
| 305 |
continue; |
| 306 |
|
| 307 |
/* split line into fields */ |
| 308 |
items = split_line(ptr, ';', 0, ret_len, &num_items); |
| 309 |
|
| 310 |
if (num_items == 0) { |
| 311 |
XDVI_WARNING((stderr, "%s, line %d: skipping malformed line \"%s\" (no command specified)", |
| 312 |
filename, lineno, linebuf)); |
| 313 |
error = True; |
| 314 |
} |
| 315 |
else if (num_items == 1 || (num_items >= 2 && strlen(items[1]) == 0)) { |
| 316 |
XDVI_WARNING((stderr, "%s, line %d: skipping malformed line \"%s\" (empty command)", |
| 317 |
filename, lineno, linebuf)); |
| 318 |
error = True; |
| 319 |
} |
| 320 |
|
| 321 |
if (error) { |
| 322 |
for (n = 0; n < num_items; n++) |
| 323 |
free(items[n]); |
| 324 |
free(items); |
| 325 |
continue; |
| 326 |
} |
| 327 |
|
| 328 |
/* resize m_mailcap */ |
| 329 |
while (*mailcap_line >= m_mailcap_size) { |
| 330 |
m_mailcap_size += MIME_ALLOC_STEP; |
| 331 |
m_mailcap = xrealloc(m_mailcap, m_mailcap_size * sizeof *m_mailcap); |
| 332 |
for (i = *mailcap_line; i < m_mailcap_size; i++) { |
| 333 |
m_mailcap[i].content_type = NULL; |
| 334 |
m_mailcap[i].command = NULL; |
| 335 |
m_mailcap[i].needsterminal = False; |
| 336 |
m_mailcap[i].testcmd = NULL; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
for (n = 0; n < num_items; n++) { |
| 341 |
if (n == 0) { /* first field: content-type */ |
| 342 |
m_mailcap[*mailcap_line].content_type = items[n]; |
| 343 |
/* |
| 344 |
* add \/\* to content-type if it only consists of one field |
| 345 |
* (it's not clear to me from RFC 1343 how this should be handled) |
| 346 |
*/ |
| 347 |
if (strchr(m_mailcap[*mailcap_line].content_type, '/') == NULL) { |
| 348 |
m_mailcap[*mailcap_line].content_type = xstrcat(m_mailcap[*mailcap_line].content_type, "/*"); |
| 349 |
} |
| 350 |
} |
| 351 |
else if (memcmp(items[n], "test=", strlen("test=")) == 0) { |
| 352 |
m_mailcap[*mailcap_line].testcmd = xstrdup(items[n] + strlen("test=")); |
| 353 |
free(items[n]); |
| 354 |
} |
| 355 |
else if (memcmp(items[n], "needsterminal", strlen("needsterminal")) == 0) { /* set flag */ |
| 356 |
m_mailcap[*mailcap_line].needsterminal = True; |
| 357 |
free(items[n]); |
| 358 |
} |
| 359 |
else if (memcmp(items[n], "copiousoutput", strlen("copiousoutput")) == 0 |
| 360 |
|| memcmp(items[n], "compose=", strlen("compose=")) == 0 |
| 361 |
|| memcmp(items[n], "composetyped=", strlen("composetyped=")) == 0 |
| 362 |
|| memcmp(items[n], "print=", strlen("print=")) == 0 |
| 363 |
|| memcmp(items[n], "edit=", strlen("edit=")) == 0 |
| 364 |
|| memcmp(items[n], "x11-bitmap=", strlen("x11-bitmap=")) == 0 |
| 365 |
|| memcmp(items[n], "description=", strlen("description=")) == 0) { |
| 366 |
free(items[n]); |
| 367 |
continue; |
| 368 |
} |
| 369 |
else { /* command field */ |
| 370 |
m_mailcap[*mailcap_line].command = items[n]; |
| 371 |
|
| 372 |
if (find_format_str(m_mailcap[*mailcap_line].command, "%s") != NULL) |
| 373 |
m_mailcap[*mailcap_line].format_string = "%s"; |
| 374 |
else if (find_format_str(m_mailcap[*mailcap_line].command, "%u") != NULL) |
| 375 |
m_mailcap[*mailcap_line].format_string = "%u"; |
| 376 |
else |
| 377 |
m_mailcap[*mailcap_line].format_string = ""; |
| 378 |
} |
| 379 |
} |
| 380 |
free(items); |
| 381 |
|
| 382 |
(*mailcap_line)++; |
| 383 |
} |
| 384 |
free(linebuf); |
| 385 |
} |
| 386 |
|
| 387 |
/* parse mime or mailcap file, return true if successful. */ |
| 388 |
static Boolean |
| 389 |
parse_mime_mailcap(const char *env_var, |
| 390 |
const char *default_var_value, |
| 391 |
int *lines, |
| 392 |
void(*parse_func)(FILE *fp, const char *filename, int *mime_line)) |
| 393 |
{ |
| 394 |
const char *path = NULL; |
| 395 |
char **path_elems; |
| 396 |
size_t elem_cnt, i; |
| 397 |
FILE *fp; |
| 398 |
Boolean success = False; |
| 399 |
|
| 400 |
if ((path = getenv(env_var)) == NULL) { |
| 401 |
path = default_var_value; |
| 402 |
} |
| 403 |
|
| 404 |
path_elems = split_line(path, ':', 0, strlen(path), &elem_cnt); |
| 405 |
for (i = 0; i < elem_cnt; i++) { |
| 406 |
char *newpath; |
| 407 |
/* expand paths */ |
| 408 |
if ((newpath = maybe_expand_homedir(path_elems[i])) != NULL) { |
| 409 |
free(path_elems[i]); |
| 410 |
path_elems[i] = newpath; |
| 411 |
} |
| 412 |
|
| 413 |
if ((fp = XFOPEN(path_elems[i], FOPEN_R_MODE)) != NULL) { |
| 414 |
parse_func(fp, path_elems[i], lines); |
| 415 |
success = True; |
| 416 |
} |
| 417 |
free(path_elems[i]); |
| 418 |
} |
| 419 |
free(path_elems); |
| 420 |
return success; |
| 421 |
} |
| 422 |
|
| 423 |
static void |
| 424 |
parsemimes(void) |
| 425 |
{ |
| 426 |
static Boolean already_called = False; |
| 427 |
|
| 428 |
if (already_called) |
| 429 |
return; |
| 430 |
already_called = True; |
| 431 |
|
| 432 |
m_mimemap_currlen = 0; |
| 433 |
|
| 434 |
if (!parse_mime_mailcap("EXTENSIONMAPS", DEFAULT_EXTENSIONMAPS, &m_mimemap_currlen, read_mime_file)) { |
| 435 |
m_mimemap_currlen = XtNumber(default_mimemap); |
| 436 |
m_mimemap = default_mimemap; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
/* |
| 442 |
Partial parsing of mailcap file. |
| 443 |
|
| 444 |
Currently not implemented: |
| 445 |
- specifiers %t, %{xyz}, %n, %F |
| 446 |
- realplayer's %u is treated as %s (couldn't find any documentation on %u) |
| 447 |
*/ |
| 448 |
static void |
| 449 |
parsemailcap(void) |
| 450 |
{ |
| 451 |
static Boolean already_called = False; |
| 452 |
|
| 453 |
if (already_called) |
| 454 |
return; |
| 455 |
|
| 456 |
already_called = True; |
| 457 |
|
| 458 |
m_mailcap_currlen = 0; |
| 459 |
|
| 460 |
if (!parse_mime_mailcap("MAILCAPS", DEFAULT_MAILCAPS, &m_mailcap_currlen, read_mailcap)) { |
| 461 |
m_mailcap_currlen = XtNumber(default_mailcap); |
| 462 |
m_mailcap = default_mailcap; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
/* |
| 468 |
Try to match test against a content type definition pattern. |
| 469 |
The strings have one of the following formats (ignore spaces around `/' |
| 470 |
- these are just to avoid breaking the C comment syntax): |
| 471 |
* / * |
| 472 |
text / * |
| 473 |
* / plain |
| 474 |
text / plain |
| 475 |
where * matches everything. |
| 476 |
*/ |
| 477 |
static Boolean |
| 478 |
match_content_type(const char *pattern, const char *test) |
| 479 |
{ |
| 480 |
/* chop both strings into parts at the '/' */ |
| 481 |
char *subtype_pattern, *subtype_test; |
| 482 |
if ((subtype_pattern = strchr(pattern, '/')) == NULL) { /* malformed string? */ |
| 483 |
XDVI_WARNING((stderr, "Malformed content-type \"%s\" (should be \"type/subtype\")", pattern)); |
| 484 |
return False; |
| 485 |
} |
| 486 |
subtype_pattern++; |
| 487 |
if ((subtype_test = strchr(test, '/')) == NULL) { /* malformed string? */ |
| 488 |
XDVI_WARNING((stderr, "Malformed content-type \"%s\" (should be \"type/subtype\")", test)); |
| 489 |
return False; |
| 490 |
} |
| 491 |
subtype_test++; |
| 492 |
/* check whether it either matches the strings or a wildcard */ |
| 493 |
if ((*pattern == '*' && *subtype_pattern == '*') || |
| 494 |
(*pattern == '*' && strcmp(subtype_pattern, subtype_test) == 0) || |
| 495 |
(*subtype_pattern == '*' && memcmp(pattern, test, subtype_test - test) == 0) || |
| 496 |
(strcmp(subtype_pattern, subtype_test) == 0 && memcmp(pattern, test, subtype_test - test) == 0)) { |
| 497 |
return True; |
| 498 |
} |
| 499 |
return False; |
| 500 |
} |
| 501 |
|
| 502 |
static Boolean |
| 503 |
run_test_command(const char *cmd, const char *arg) |
| 504 |
{ |
| 505 |
int retval; |
| 506 |
const char *ptr; |
| 507 |
size_t len; |
| 508 |
char *syscmd = NULL; |
| 509 |
|
| 510 |
UNUSED(arg); /* FIXME */ |
| 511 |
if (cmd == NULL) |
| 512 |
return True; |
| 513 |
|
| 514 |
syscmd = xmalloc(strlen(cmd) + strlen(arg) + 1); |
| 515 |
if ((ptr = find_format_str(cmd, "%s")) != NULL) { |
| 516 |
len = 2; |
| 517 |
} |
| 518 |
else if ((ptr = find_format_str(cmd, "%u")) != NULL) { |
| 519 |
len = 2; |
| 520 |
} |
| 521 |
else { |
| 522 |
len = 0; |
| 523 |
ptr = strchr(cmd, '\0'); |
| 524 |
} |
| 525 |
/* build system command */ |
| 526 |
memcpy(syscmd, cmd, ptr - cmd); |
| 527 |
if (len > 0) { /* append argument */ |
| 528 |
strcpy(syscmd + (ptr - cmd), arg); |
| 529 |
strcpy(syscmd + (ptr - cmd) + strlen(arg), ptr + len); |
| 530 |
} |
| 531 |
else { |
| 532 |
syscmd[ptr - cmd] = '\0'; |
| 533 |
} |
| 534 |
|
| 535 |
retval = system(syscmd); |
| 536 |
TRACE_HTEX((stderr, "Execution of test command `%s' returned value %d", syscmd, retval)); |
| 537 |
free(syscmd); |
| 538 |
return retval == 0; |
| 539 |
} |
| 540 |
|
| 541 |
char * |
| 542 |
figure_viewer(const char *content_type, const char **format_string, Boolean *needs_terminal, const char *arg) |
| 543 |
{ |
| 544 |
int i; |
| 545 |
|
| 546 |
/* Hardwire xdvi for this MIME type so that xdvizilla isn't used, |
| 547 |
which would be too dangerous since xdvizilla tries to unlink |
| 548 |
the DVI file by default. There might be other DVI viewers, but |
| 549 |
since the user invoking this from xdvi anyway ... |
| 550 |
|
| 551 |
FIXME: A better way would be to always copy the original file, like |
| 552 |
e.g. Acroread does it; see comment in launch_program(), hypertex.c. |
| 553 |
|
| 554 |
Furthermore, xdvizilla has been retired in the meantime. |
| 555 |
*/ |
| 556 |
/* FIXME: why do we actually need format_string - why not sprintf() |
| 557 |
directly into the target? |
| 558 |
*/ |
| 559 |
if (strcmp(content_type, "application/x-dvi") == 0) { |
| 560 |
*format_string = "%s"; |
| 561 |
*needs_terminal = False; |
| 562 |
return xstrdup("xdvi %s"); |
| 563 |
} |
| 564 |
/* try each command from our mailcap list */ |
| 565 |
for (i = 0; i < m_mailcap_currlen; i++) { |
| 566 |
if (globals.debug & DBG_HTEX) { |
| 567 |
fprintf(stderr, "type |%s| viewer |%s| needsterminal=%s testcmd |%s|\n", |
| 568 |
m_mailcap[i].content_type, m_mailcap[i].command, |
| 569 |
m_mailcap[i].needsterminal ? "yes" : "no", |
| 570 |
m_mailcap[i].testcmd ? m_mailcap[i].testcmd : "None"); |
| 571 |
} |
| 572 |
if (match_content_type(m_mailcap[i].content_type, content_type) |
| 573 |
&& run_test_command(m_mailcap[i].testcmd, arg)) { |
| 574 |
*format_string = m_mailcap[i].format_string; |
| 575 |
*needs_terminal = m_mailcap[i].needsterminal; |
| 576 |
return xstrdup(m_mailcap[i].command); |
| 577 |
} |
| 578 |
} |
| 579 |
/* failure */ |
| 580 |
return NULL; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
/* return the mime type for filename, or default types |
| 585 |
for non-recognized extensions/no extensions */ |
| 586 |
char * |
| 587 |
figure_mime_type(const char *filename) |
| 588 |
{ |
| 589 |
int i; |
| 590 |
char *extension, *cp; |
| 591 |
char *content_type = NULL; |
| 592 |
|
| 593 |
/* First check for the mailcap and mime files */ |
| 594 |
parsemimes(); |
| 595 |
parsemailcap(); |
| 596 |
|
| 597 |
if (globals.debug & DBG_HTEX) |
| 598 |
fprintf(stderr, "figure_mime_type: Called to find type of %s\n", |
| 599 |
filename); |
| 600 |
|
| 601 |
/* See if this is a directory */ |
| 602 |
if (filename[strlen(filename) - 1] == '/') { |
| 603 |
if (globals.debug & DBG_HTEX) |
| 604 |
fprintf(stderr, "It's a directory, returning unknownExtensionMimeType: %s\n", |
| 605 |
resource.unknown_mime_suffix); |
| 606 |
return resource.unknown_mime_suffix; |
| 607 |
} |
| 608 |
|
| 609 |
/* See if filename extension is on the mime list: */ |
| 610 |
extension = strrchr(filename, '.'); |
| 611 |
|
| 612 |
if (extension == NULL) { |
| 613 |
TRACE_HTEX((stderr, |
| 614 |
"No extension, defaulting to noExtensionMimeType: %s\n", |
| 615 |
resource.no_mime_suffix)); |
| 616 |
return resource.no_mime_suffix; |
| 617 |
} |
| 618 |
extension++; |
| 619 |
/* |
| 620 |
* corrupt URLs might have empty extensions; we need to catch this, |
| 621 |
* since the while loop below would not terminate in that case: |
| 622 |
*/ |
| 623 |
if (strcmp(extension, "") == 0) { |
| 624 |
XDVI_WARNING((stderr, "Empty extension for file name or URL `%s'\n", filename)); |
| 625 |
return resource.no_mime_suffix; |
| 626 |
} |
| 627 |
|
| 628 |
for (i = 0; i < m_mimemap_currlen; i++) { |
| 629 |
/* |
| 630 |
* find extension in m_mimemap[i].extensions, a space-separated list |
| 631 |
* of extension strings. |
| 632 |
*/ |
| 633 |
cp = m_mimemap[i].extensions; |
| 634 |
while ((cp = strstr(cp, extension)) != NULL) { |
| 635 |
if ((cp - m_mimemap[i].extensions > 0) && (cp[-1] != ' ')) { |
| 636 |
cp++; |
| 637 |
continue; |
| 638 |
} |
| 639 |
cp += strlen(extension); |
| 640 |
if ((*cp != ' ') && (*cp != '\0')) |
| 641 |
continue; |
| 642 |
content_type = m_mimemap[i].content_type; |
| 643 |
break; |
| 644 |
} |
| 645 |
if (content_type != NULL) |
| 646 |
break; |
| 647 |
} |
| 648 |
|
| 649 |
if (content_type == NULL) { |
| 650 |
content_type = xstrdup(resource.unknown_mime_suffix); |
| 651 |
TRACE_HTEX((stderr, |
| 652 |
"Unknown extension, defaulting to unknownExtensionMimeType: %s", |
| 653 |
content_type)); |
| 654 |
} |
| 655 |
else { |
| 656 |
TRACE_HTEX((stderr, "Found mime type: %s", content_type)); |
| 657 |
} |
| 658 |
|
| 659 |
return content_type; |
| 660 |
} |
| 661 |
|