| 1 |
/* For use with emTeX set FONTPATH to "TEXTFM" |
| 2 |
*/ |
| 3 |
#ifndef FONTPATH |
| 4 |
#define FONTPATH "TEXFONTS" |
| 5 |
#endif |
| 6 |
|
| 7 |
/* |
| 8 |
* This code reads in and handles the defaults for the program from the |
| 9 |
* file config.sw. This entire file is a bit kludgy, sorry. |
| 10 |
*/ |
| 11 |
#include "dvips.h" /* The copyright notice in that file is included too! */ |
| 12 |
#include "paths.h" |
| 13 |
#ifdef KPATHSEA |
| 14 |
#include <kpathsea/c-ctype.h> |
| 15 |
#include <kpathsea/c-pathch.h> |
| 16 |
#include <kpathsea/c-stat.h> |
| 17 |
#include <kpathsea/pathsearch.h> |
| 18 |
#include <kpathsea/tex-file.h> |
| 19 |
#include <kpathsea/variable.h> |
| 20 |
#endif |
| 21 |
/* |
| 22 |
* This is the structure definition for resident fonts. We use |
| 23 |
* a small and simple hash table to handle these. We don't need |
| 24 |
* a big hash table. |
| 25 |
*/ |
| 26 |
struct resfont *reshash[RESHASHPRIME]; |
| 27 |
/* |
| 28 |
* The external declarations: |
| 29 |
*/ |
| 30 |
#include "protos.h" |
| 31 |
|
| 32 |
/* |
| 33 |
* To maintain a list of document fonts, we use the following |
| 34 |
* pointer. |
| 35 |
*/ |
| 36 |
struct header_list *ps_fonts_used; |
| 37 |
/* |
| 38 |
* Our hash routine. |
| 39 |
*/ |
| 40 |
unsigned int |
| 41 |
hash(char *s) |
| 42 |
{ |
| 43 |
unsigned int h = 12; |
| 44 |
|
| 45 |
while (*s != 0) |
| 46 |
h = (h + h + (unsigned char)(*s++)) % RESHASHPRIME; |
| 47 |
return(h); |
| 48 |
} |
| 49 |
/* |
| 50 |
* Reverse the hash chains. |
| 51 |
*/ |
| 52 |
void |
| 53 |
revpslists(void) { |
| 54 |
register int i; |
| 55 |
for (i=0; i<RESHASHPRIME; i++) |
| 56 |
reshash[i] = (struct resfont *)revlist(reshash[i]); |
| 57 |
} |
| 58 |
/* |
| 59 |
* cleanres() marks all resident fonts as not being yet sent, except |
| 60 |
* those marked with 2 meaning they were downloaded as part of the |
| 61 |
* main prolog (not section prolog). |
| 62 |
*/ |
| 63 |
void |
| 64 |
cleanres(void) { |
| 65 |
register int i; |
| 66 |
register struct resfont *p; |
| 67 |
for (i=0; i<RESHASHPRIME; i++) |
| 68 |
for (p=reshash[i]; p; p=p->next) |
| 69 |
if (p->sent == 1) |
| 70 |
p->sent = 0; |
| 71 |
} |
| 72 |
/* |
| 73 |
* The routine that looks up a font name. |
| 74 |
*/ |
| 75 |
struct resfont * |
| 76 |
lookup(char *name) |
| 77 |
{ |
| 78 |
struct resfont *p; |
| 79 |
|
| 80 |
for (p=reshash[hash(name)]; p!=NULL; p=p->next) |
| 81 |
if (strcmp(p->Keyname, name)==0) |
| 82 |
return(p); |
| 83 |
return(NULL); |
| 84 |
} |
| 85 |
|
| 86 |
struct resfont * |
| 87 |
findPSname(char *name) |
| 88 |
{ |
| 89 |
register int i; |
| 90 |
register struct resfont *p; |
| 91 |
for (i=0; i<RESHASHPRIME; i++) |
| 92 |
for (p=reshash[i]; p; p=p->next) { |
| 93 |
if (strcmp(p->PSname, name)==0) |
| 94 |
return p; |
| 95 |
} |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
/* |
| 100 |
* This routine adds an entry. |
| 101 |
*/ |
| 102 |
static void |
| 103 |
add_entry(char *TeXname, char *PSname, char *Fontfile, |
| 104 |
char *Vectfile, char *specinfo, char *downloadinfo) |
| 105 |
{ |
| 106 |
struct resfont *p; |
| 107 |
int h; |
| 108 |
if (PSname == NULL) |
| 109 |
PSname = TeXname; |
| 110 |
p = (struct resfont *)mymalloc((integer)sizeof(struct resfont)); |
| 111 |
p->Keyname = TeXname; |
| 112 |
p->PSname = PSname; |
| 113 |
p->Fontfile = Fontfile; |
| 114 |
p->Vectfile = Vectfile; |
| 115 |
p->TeXname = TeXname; |
| 116 |
p->specialinstructions = specinfo; |
| 117 |
if (downloadinfo && *downloadinfo) |
| 118 |
p->downloadheader = downloadinfo; |
| 119 |
else |
| 120 |
p->downloadheader = 0; |
| 121 |
h = hash(TeXname); |
| 122 |
p->next = reshash[h]; |
| 123 |
p->sent = 0; |
| 124 |
reshash[h] = p; |
| 125 |
} |
| 126 |
/* |
| 127 |
* Now our residentfont routine. Returns the number of characters in |
| 128 |
* this font, based on the TFM file. |
| 129 |
*/ |
| 130 |
int |
| 131 |
residentfont(register fontdesctype *curfnt) |
| 132 |
{ |
| 133 |
int i; |
| 134 |
struct resfont *p; |
| 135 |
|
| 136 |
/* |
| 137 |
* First we determine if we can find this font in the resident list. |
| 138 |
*/ |
| 139 |
if (*curfnt->area) |
| 140 |
return 0; /* resident fonts never have a nonstandard font area */ |
| 141 |
if ((p=lookup(curfnt->name))==NULL) |
| 142 |
return 0; |
| 143 |
/* |
| 144 |
* This is not yet the correct way to do things, but it is useful as it |
| 145 |
* is so we leave it in. The problem: if resident Times-Roman is |
| 146 |
* re-encoded, then it will be downloaded as bitmaps; this is not |
| 147 |
* right. The solution will be to introduce two types of `<' |
| 148 |
* directives, one that downloads fonts and one that downloads |
| 149 |
* short headers that are innocuous. |
| 150 |
*/ |
| 151 |
if (p->Fontfile && downloadpspk) { |
| 152 |
#ifdef DEBUG |
| 153 |
if (dd(D_FONTS)) |
| 154 |
fprintf_str(stderr,"Using PK font %s for <%s>.\n", |
| 155 |
curfnt->name, p->PSname); |
| 156 |
#endif /* DEBUG */ |
| 157 |
return 0; |
| 158 |
} |
| 159 |
/* |
| 160 |
* We clear out some pointers: |
| 161 |
*/ |
| 162 |
#ifdef DEBUG |
| 163 |
if (dd(D_FONTS)) |
| 164 |
fprintf_str(stderr,"Font %s <%s> is resident.\n", |
| 165 |
curfnt->name, p->PSname); |
| 166 |
#endif /* DEBUG */ |
| 167 |
curfnt->resfont = p; |
| 168 |
curfnt->name = p->TeXname; |
| 169 |
for (i=0; i<256; i++) { |
| 170 |
curfnt->chardesc[i].TFMwidth = 0; |
| 171 |
curfnt->chardesc[i].packptr = NULL; |
| 172 |
curfnt->chardesc[i].pixelwidth = 0; |
| 173 |
curfnt->chardesc[i].flags = 0; |
| 174 |
curfnt->chardesc[i].flags2 = 0; |
| 175 |
} |
| 176 |
add_name(p->PSname, &ps_fonts_used); |
| 177 |
/* |
| 178 |
* We include the font here. But we only should need to include the |
| 179 |
* font if we have a stupid spooler; smart spoolers should be able |
| 180 |
* to supply it automatically. |
| 181 |
*/ |
| 182 |
if (p->downloadheader) { |
| 183 |
char *cp = p->downloadheader; |
| 184 |
char *q; |
| 185 |
|
| 186 |
infont = p->PSname; |
| 187 |
while (1) { |
| 188 |
q = cp; |
| 189 |
while (*cp && *cp != ' ') |
| 190 |
cp++; |
| 191 |
if (*cp) { |
| 192 |
*cp = 0; |
| 193 |
add_header(q); |
| 194 |
*cp++ = ' '; |
| 195 |
} else { |
| 196 |
/* if (strstr(q,".pfa")||strstr(q,".pfb")|| |
| 197 |
strstr(q,".PFA")||strstr(q,".PFB")) |
| 198 |
break; |
| 199 |
else */ { |
| 200 |
add_header(q); |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
infont = 0; |
| 205 |
} |
| 206 |
infont = 0; |
| 207 |
} |
| 208 |
i = tfmload(curfnt); |
| 209 |
if (i < 0) |
| 210 |
i = 1; |
| 211 |
usesPSfonts = 1; |
| 212 |
return(i); |
| 213 |
} |
| 214 |
#define INLINE_SIZE (2000) |
| 215 |
static char was_inline[INLINE_SIZE]; |
| 216 |
static unsigned c_lineno; |
| 217 |
static void |
| 218 |
bad_config(const char *err) |
| 219 |
{ |
| 220 |
fprintf_str(stderr, "%s:%d:", realnameoffile, c_lineno); |
| 221 |
error (err); |
| 222 |
fprintf_str(stderr, " (%s)\n", was_inline); |
| 223 |
} |
| 224 |
|
| 225 |
#ifndef KPATHSEA |
| 226 |
/* |
| 227 |
* Get environment variables! These override entries in ./config.h. |
| 228 |
* We substitute everything of the form ::, ^: or :$ with default, |
| 229 |
* so a user can easily build on to the existing paths. |
| 230 |
*/ |
| 231 |
static char * |
| 232 |
getpath(char *who, char *what) |
| 233 |
{ |
| 234 |
if (who) { |
| 235 |
register char *pp, *qq; |
| 236 |
int lastsep = 1; |
| 237 |
|
| 238 |
for (pp=nextstring, qq=who; *qq;) { |
| 239 |
if (*qq == PATHSEP) { |
| 240 |
if (lastsep) { |
| 241 |
strcpy(pp, what); |
| 242 |
pp = pp + strlen(pp); |
| 243 |
} |
| 244 |
lastsep = 1; |
| 245 |
} else |
| 246 |
lastsep = 0; |
| 247 |
*pp++ = *qq++; |
| 248 |
} |
| 249 |
if (lastsep) { |
| 250 |
strcpy(pp, what); |
| 251 |
pp = pp + strlen(pp); |
| 252 |
} |
| 253 |
*pp = 0; |
| 254 |
qq = nextstring; |
| 255 |
nextstring = pp + 1; |
| 256 |
return qq; |
| 257 |
} else |
| 258 |
return what; |
| 259 |
} |
| 260 |
#endif |
| 261 |
/* |
| 262 |
* We use this function so we can support strings delimited by |
| 263 |
* double quotes with spaces in them. We also accept strings |
| 264 |
* with spaces in them, but kill off any spaces at the end. |
| 265 |
*/ |
| 266 |
static char * |
| 267 |
configstring(char *s, int nullok) |
| 268 |
{ |
| 269 |
char tstr[INLINE_SIZE]; |
| 270 |
char *p = tstr; |
| 271 |
|
| 272 |
while (*s && *s <= ' ') |
| 273 |
s++; |
| 274 |
if (*s == '"') { |
| 275 |
s++; |
| 276 |
while (*s != 10 && *s != 0 && *s != '"' && p < tstr+290) |
| 277 |
*p++ = *s++; |
| 278 |
} else { |
| 279 |
while (*s && p < tstr+290) |
| 280 |
*p++ = *s++; |
| 281 |
while (*(p-1) <= ' ' && p > tstr) |
| 282 |
p--; |
| 283 |
} |
| 284 |
*p = 0; |
| 285 |
if (p == tstr && ! nullok) |
| 286 |
bad_config("bad string"); |
| 287 |
return newstring(tstr); |
| 288 |
} |
| 289 |
#ifdef KPATHSEA |
| 290 |
/* We use this in `getdefaults' to modify the kpathsea structure for the |
| 291 |
paths we read. See kpathsea/tex-file.[ch]. */ |
| 292 |
#define SET_CLIENT_PATH(filefmt, val) \ |
| 293 |
kpse_format_info[filefmt].client_path = xstrdup (val) |
| 294 |
#endif |
| 295 |
/* |
| 296 |
* Now we have the getdefaults routine. |
| 297 |
*/ |
| 298 |
const char *psmapfile = PSMAPFILE; |
| 299 |
Boolean |
| 300 |
getdefaults(const char *s) |
| 301 |
{ |
| 302 |
FILE *deffile; |
| 303 |
char PSname[INLINE_SIZE]; |
| 304 |
register char *p; |
| 305 |
integer hsiz, vsiz; |
| 306 |
#ifndef KPATHSEA |
| 307 |
char *d = configpath; |
| 308 |
int i, j; |
| 309 |
#endif |
| 310 |
int canaddtopaper = 0; |
| 311 |
|
| 312 |
if (printer == NULL) { |
| 313 |
if (s) { |
| 314 |
strcpy(PSname, s); |
| 315 |
} else { |
| 316 |
#ifdef KPATHSEA |
| 317 |
char *dvipsrc = kpse_var_value ("DVIPSRC"); |
| 318 |
#ifdef WIN32 |
| 319 |
if (dvipsrc && *dvipsrc) { |
| 320 |
/* $DVIPSRC was set by user */ |
| 321 |
strcpy(PSname, dvipsrc); |
| 322 |
} |
| 323 |
else |
| 324 |
/* No env var, looking into some kind of standard path. */ |
| 325 |
if (SearchPath(".;%HOME%;c:\\", ".dvipsrc", NULL, |
| 326 |
INLINE_SIZE, PSname, |
| 327 |
&dvipsrc) == 0) { |
| 328 |
/* search failed, we must put something into PSname. */ |
| 329 |
dvipsrc = kpse_var_expand(DVIPSRC); |
| 330 |
if (dvipsrc) { |
| 331 |
strcpy(PSname, dvipsrc); |
| 332 |
free(dvipsrc); |
| 333 |
} |
| 334 |
/* Else SearchPath has filled PSname with something */ |
| 335 |
} |
| 336 |
/* remove any redundant path separators. Many configurations |
| 337 |
can show up: c:\/.dvipsrc and so on ... */ |
| 338 |
{ |
| 339 |
char *p, *q; |
| 340 |
for (p = q = PSname; *p && (p - PSname < INLINE_SIZE); |
| 341 |
p++, q++) { |
| 342 |
if (IS_DIR_SEP(*p)) { |
| 343 |
*q = DIR_SEP; p++; q++; |
| 344 |
while (*p && IS_DIR_SEP(*p)) p++; |
| 345 |
} |
| 346 |
else if (IS_KANJI(p)) { |
| 347 |
*q++ = *p++; |
| 348 |
} |
| 349 |
*q = *p; |
| 350 |
} |
| 351 |
*q = '\0'; |
| 352 |
} |
| 353 |
#else |
| 354 |
if(!dvipsrc) dvipsrc = kpse_var_expand(DVIPSRC); |
| 355 |
strcpy(PSname, dvipsrc ? dvipsrc : "~/.dvipsrc"); |
| 356 |
if(dvipsrc) free(dvipsrc); |
| 357 |
#endif /* WIN32 */ |
| 358 |
|
| 359 |
#else /* ! KPATHSEA */ |
| 360 |
#ifndef VMCMS /* IBM: VM/CMS - don't have home directory on VMCMS */ |
| 361 |
#ifndef MVSXA |
| 362 |
d = "~"; |
| 363 |
#endif |
| 364 |
#endif /* IBM: VM/CMS */ |
| 365 |
strcpy(PSname, DVIPSRC); |
| 366 |
#endif /* KPATHSEA */ |
| 367 |
} |
| 368 |
} else { |
| 369 |
#if defined(MSDOS) || defined(OS2) |
| 370 |
strcpy(PSname, printer); |
| 371 |
strcat(PSname, ".cfg"); |
| 372 |
#else |
| 373 |
strcpy(PSname, "config."); |
| 374 |
strcat(PSname, printer); |
| 375 |
#endif |
| 376 |
} |
| 377 |
#ifdef KPATHSEA |
| 378 |
if ((deffile=search(configpath,PSname,READ))!=NULL) { |
| 379 |
#else |
| 380 |
if ((deffile=search(d,PSname,READ))!=NULL) { |
| 381 |
#endif |
| 382 |
if (dvips_debug_flag && !quiet) { |
| 383 |
if (strlen(realnameoffile) + prettycolumn > STDOUTSIZE) { |
| 384 |
fprintf(stderr, "\n"); |
| 385 |
prettycolumn = 0; |
| 386 |
} |
| 387 |
fprintf_str(stderr, "{%s}", realnameoffile); |
| 388 |
prettycolumn += strlen(realnameoffile) + 2; |
| 389 |
} |
| 390 |
#ifdef DEBUG |
| 391 |
if (dd (D_CONFIG)) { |
| 392 |
fprintf_str(stderr, "Reading dvips config file `%s':\n", realnameoffile); |
| 393 |
} |
| 394 |
#endif |
| 395 |
c_lineno = 0; |
| 396 |
while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) { |
| 397 |
c_lineno++; |
| 398 |
#ifdef DEBUG |
| 399 |
if (dd (D_CONFIG)) { |
| 400 |
fprintf_str(stderr, "%s:%d:%s", realnameoffile, c_lineno, was_inline); |
| 401 |
} |
| 402 |
#endif |
| 403 |
/* |
| 404 |
* We need to get rid of the newline. |
| 405 |
*/ |
| 406 |
for (p=was_inline; *p; p++); |
| 407 |
while (p > was_inline && (*(p-1) == '\n' || *(p-1) == '\r')) { |
| 408 |
*--p = '\0'; |
| 409 |
} |
| 410 |
if (was_inline[0] != '@') |
| 411 |
canaddtopaper = 0; |
| 412 |
switch (was_inline[0]) { |
| 413 |
/* |
| 414 |
* Handling paper size information: |
| 415 |
* |
| 416 |
* If line is empty, then we clear out the paper size information |
| 417 |
* we have so far. |
| 418 |
* |
| 419 |
* If it is `@+', then we add to the current paper size info. |
| 420 |
* |
| 421 |
* If it is `name hsize vsize', then we start a new definition. |
| 422 |
*/ |
| 423 |
case '@' : |
| 424 |
p = was_inline + 1; |
| 425 |
while (*p && *p <= ' ') p++; |
| 426 |
if (*p == 0) { |
| 427 |
papsizes = 0; /* throw away memory */ |
| 428 |
} else if (*p == '+') { |
| 429 |
if (canaddtopaper == 0) |
| 430 |
error( |
| 431 |
" @+ in config files must immediately following a @ lines"); |
| 432 |
else { |
| 433 |
*(nextstring-1) = '\n';/* IBM: VM/CMS - changed 10 to "\n" */ |
| 434 |
p++; |
| 435 |
while (*p && *p == ' ') p++; |
| 436 |
strcpy(nextstring, p); |
| 437 |
nextstring += strlen(p) + 1; |
| 438 |
} |
| 439 |
} else { |
| 440 |
struct papsiz *ps; |
| 441 |
char *q; |
| 442 |
|
| 443 |
ps = (struct papsiz *)mymalloc((integer)sizeof(struct papsiz)); |
| 444 |
ps->next = papsizes; |
| 445 |
papsizes = ps; |
| 446 |
q = p; |
| 447 |
while (*p && *p > ' ') |
| 448 |
p++; |
| 449 |
*p++ = 0; |
| 450 |
ps->name = newstring(q); |
| 451 |
while (*p && *p <= ' ') p++; |
| 452 |
handlepapersize(p, &hsiz, &vsiz); |
| 453 |
ps->xsize = hsiz; |
| 454 |
ps->ysize = vsiz; |
| 455 |
ps->specdat = newstring(""); |
| 456 |
canaddtopaper = 1; |
| 457 |
} |
| 458 |
break; |
| 459 |
case 'a' : |
| 460 |
dopprescan = (was_inline[1] != '0'); |
| 461 |
break; |
| 462 |
case 'b': |
| 463 |
if (strncmp(was_inline, "bitmapfontenc", 13) == 0) { |
| 464 |
char *p = was_inline + 13 ; |
| 465 |
while (*p && *p <= ' ') |
| 466 |
p++ ; |
| 467 |
if (strncmp(p, "off", 3) == 0) { |
| 468 |
bitmapencopt(0) ; // disable bitmap font enc feature |
| 469 |
} else if (strncmp(p, "on", 2) == 0) { |
| 470 |
bitmapencopt(1) ; // try to include bitmap font encs |
| 471 |
} else if (strncmp(p, "strict", 6) == 0) { |
| 472 |
bitmapencopt(2) ; // issue warnings for missing encs |
| 473 |
} else { |
| 474 |
error( |
| 475 |
"! bitmapfontenc config file option only supports on, off, and strict") ; |
| 476 |
} |
| 477 |
break ; |
| 478 |
} |
| 479 |
#ifdef SHORTINT |
| 480 |
if (sscanf(was_inline+1, "%ld", &pagecopies) != 1) |
| 481 |
bad_config("missing pagecopies to b"); |
| 482 |
#else |
| 483 |
if (sscanf(was_inline+1, "%d", &pagecopies) != 1) |
| 484 |
bad_config("missing pagecopies to b"); |
| 485 |
#endif |
| 486 |
if (pagecopies < 1 || pagecopies > 1000) |
| 487 |
bad_config("pagecopies not between 1 and 1000"); |
| 488 |
break; |
| 489 |
case 'm' : |
| 490 |
#ifdef SHORTINT |
| 491 |
if (sscanf(was_inline+1, "%ld", &swmem) != 1) |
| 492 |
bad_config("missing swmem to m"); |
| 493 |
#else /* ~SHORTINT */ |
| 494 |
if (sscanf(was_inline+1, "%d", &swmem) != 1) |
| 495 |
bad_config("missing swmem to m"); |
| 496 |
#endif /* ~SHORTINT */ |
| 497 |
if (swmem > 0) { |
| 498 |
swmem += fontmem; /* grab headers we've seen already */ |
| 499 |
} else { |
| 500 |
swmem = INT_MAX; /* if value <=0, max it out */ |
| 501 |
} |
| 502 |
break; |
| 503 |
case 'M' : |
| 504 |
/* If the user specified a -mode, don't replace it. */ |
| 505 |
if (!mfmode_option) |
| 506 |
mfmode = configstring(was_inline+1, 0); |
| 507 |
mflandmode = 0; |
| 508 |
{ |
| 509 |
char *pp; |
| 510 |
for (pp=mfmode; pp && *pp>' '; pp++); |
| 511 |
if (pp && *pp == ' ') { |
| 512 |
*pp++ = 0; |
| 513 |
while (*pp && *pp <= ' ') |
| 514 |
pp++; |
| 515 |
if (*pp) |
| 516 |
mflandmode = pp; |
| 517 |
} |
| 518 |
} |
| 519 |
break; |
| 520 |
case 'o' : |
| 521 |
if (!oname_option) { |
| 522 |
struct stat st_buf; |
| 523 |
char *tmp_oname; |
| 524 |
tmp_oname = configstring(was_inline+1, 1); |
| 525 |
if ((*tmp_oname && tmp_oname[strlen(tmp_oname)-1] == ':') |
| 526 |
|| (stat(tmp_oname, &st_buf) == 0 && S_ISCHR(st_buf.st_mode))) { |
| 527 |
sendcontrolD = 1; /* if we send to a device, *we* are spooler */ |
| 528 |
#if defined(MSDOS) || defined(OS2) |
| 529 |
tmp_oname[strlen(tmp_oname)-1] = 0; |
| 530 |
#endif |
| 531 |
} |
| 532 |
oname = tmp_oname; |
| 533 |
} |
| 534 |
break; |
| 535 |
case 'F' : |
| 536 |
sendcontrolD = (was_inline[1] != '0'); |
| 537 |
break; |
| 538 |
case 'O' : |
| 539 |
p = was_inline + 1; |
| 540 |
handlepapersize(p, &hoff, &voff); |
| 541 |
break; |
| 542 |
case 'l': |
| 543 |
p = was_inline + 1 ; |
| 544 |
if (strncmp(p, "andscaperotate", 14) != 0) |
| 545 |
error("! in config file line starting with l was not landscaperotate") ; |
| 546 |
p += 14 ; |
| 547 |
landscaperotate = (*p != '0') ; |
| 548 |
break ; |
| 549 |
#ifdef FONTLIB |
| 550 |
case 'L' : |
| 551 |
{ |
| 552 |
char tempname[INLINE_SIZE]; |
| 553 |
if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config("missing arg to L"); |
| 554 |
else { |
| 555 |
flipath = getpath(fliparse(PSname,tempname), flipath); |
| 556 |
fliname = newstring(tempname); |
| 557 |
} |
| 558 |
} |
| 559 |
break; |
| 560 |
#endif |
| 561 |
case 'T' : |
| 562 |
if (sscanf(was_inline+1, "%s", PSname) != 1) |
| 563 |
bad_config("missing arg to T"); |
| 564 |
else |
| 565 |
#ifdef KPATHSEA |
| 566 |
SET_CLIENT_PATH (kpse_tfm_format, PSname); |
| 567 |
#else |
| 568 |
tfmpath = getpath(PSname, tfmpath); |
| 569 |
#endif |
| 570 |
break; |
| 571 |
case 'P' : |
| 572 |
if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config("missing arg to P"); |
| 573 |
else |
| 574 |
#ifdef KPATHSEA |
| 575 |
SET_CLIENT_PATH (kpse_pk_format, PSname); |
| 576 |
#else |
| 577 |
pkpath = getpath(PSname, pkpath); |
| 578 |
#endif |
| 579 |
break; |
| 580 |
case 'p' : |
| 581 |
p = was_inline + 1; |
| 582 |
while (*p && *p <= ' ') |
| 583 |
p++; |
| 584 |
if (*p == '+') { |
| 585 |
if (sscanf(p+1, "%s", PSname) != 1) |
| 586 |
bad_config("missing arg to p"); |
| 587 |
getpsinfo(PSname); |
| 588 |
} else { |
| 589 |
psmapfile = configstring(was_inline+1, 0); |
| 590 |
} |
| 591 |
break; |
| 592 |
case 'v' : case 'V' : |
| 593 |
if (sscanf(was_inline+1, "%s", PSname) != 1) |
| 594 |
bad_config("missing arg to V"); |
| 595 |
else |
| 596 |
#ifdef KPATHSEA |
| 597 |
SET_CLIENT_PATH (kpse_vf_format, PSname); |
| 598 |
#else |
| 599 |
vfpath = getpath(PSname, vfpath); |
| 600 |
#endif |
| 601 |
break; |
| 602 |
case 'S' : |
| 603 |
if (sscanf(was_inline+1, "%s", PSname) != 1) |
| 604 |
bad_config("missing arg to S"); |
| 605 |
else |
| 606 |
#ifdef KPATHSEA |
| 607 |
SET_CLIENT_PATH (kpse_pict_format, PSname); |
| 608 |
#else |
| 609 |
figpath = getpath(PSname, figpath); |
| 610 |
#endif |
| 611 |
break; |
| 612 |
case 's': |
| 613 |
safetyenclose = 1; |
| 614 |
break; |
| 615 |
case 'H' : |
| 616 |
if (sscanf(was_inline+1, "%s", PSname) != 1) |
| 617 |
bad_config("missing arg to H"); |
| 618 |
else |
| 619 |
#ifdef KPATHSEA |
| 620 |
SET_CLIENT_PATH (headerpath, PSname); |
| 621 |
#else |
| 622 |
headerpath = getpath(PSname, headerpath); |
| 623 |
#endif |
| 624 |
break; |
| 625 |
case '%': case ' ' : case '*' : case '#' : case ';' : |
| 626 |
case '=' : case 0 : case '\n' : |
| 627 |
break; |
| 628 |
case 'r' : |
| 629 |
reverse = (was_inline[1] != '0'); |
| 630 |
break; |
| 631 |
/* |
| 632 |
* This case is for last resort font scaling; I hate this, but enough |
| 633 |
* people have in no uncertain terms demanded it that I'll go ahead and |
| 634 |
* add it. |
| 635 |
* |
| 636 |
* This line must have numbers on it, resolutions, to search for the |
| 637 |
* font as a last resort, and then the font will be scaled. These |
| 638 |
* resolutions should be in increasing order. |
| 639 |
* |
| 640 |
* For most machines, just `300' is sufficient here; on the NeXT, |
| 641 |
* `300 400' may be more appropriate. |
| 642 |
*/ |
| 643 |
case 'R': |
| 644 |
#ifndef KPATHSEA |
| 645 |
i = 0; |
| 646 |
p = was_inline + 1; |
| 647 |
while (*p) { |
| 648 |
while (*p && *p <= ' ') |
| 649 |
p++; |
| 650 |
if ('0' <= *p && *p <= '9') { |
| 651 |
j = 0; |
| 652 |
while ('0' <= *p && *p <= '9') |
| 653 |
j = 10 * j + (*p++ - '0'); |
| 654 |
if (i > 0) |
| 655 |
if (lastresortsizes[i-1] > j) { |
| 656 |
bad_config("last resort sizes (R) must be sorted"); |
| 657 |
} |
| 658 |
lastresortsizes[i++] = j; |
| 659 |
} else { |
| 660 |
if (*p == 0) |
| 661 |
break; |
| 662 |
bad_config("only numbers expected on `R' line"); |
| 663 |
} |
| 664 |
} |
| 665 |
lastresortsizes[i] = 32000; |
| 666 |
#else /* KPATHSEA */ |
| 667 |
for (p = was_inline + 1; *p; p++) { |
| 668 |
if (isblank ((unsigned char)*p)) { |
| 669 |
*p = ':'; |
| 670 |
} |
| 671 |
} |
| 672 |
kpse_fallback_resolutions_string = xstrdup (was_inline + 1); |
| 673 |
#endif |
| 674 |
break; |
| 675 |
case 'D' : |
| 676 |
if (sscanf(was_inline+1, "%d", &actualdpi) != 1) |
| 677 |
bad_config("missing arg to D"); |
| 678 |
if (actualdpi < 10 || actualdpi > 10000) |
| 679 |
bad_config("dpi must be between 10 and 10000"); |
| 680 |
vactualdpi = actualdpi; |
| 681 |
break; |
| 682 |
/* |
| 683 |
* Execute a command. This can be dangerous, but can also be very useful. |
| 684 |
*/ |
| 685 |
case 'E' : |
| 686 |
#ifdef SECURE |
| 687 |
error("dvips was compiled with SECURE, which disables E in config"); |
| 688 |
#else |
| 689 |
if (secure) { |
| 690 |
error("E in config is disabled. To enable E, set z0 before E"); |
| 691 |
break; |
| 692 |
} |
| 693 |
system(was_inline+1); |
| 694 |
#endif |
| 695 |
break; |
| 696 |
case 'K': |
| 697 |
removecomments = (was_inline[1] != '0'); |
| 698 |
break; |
| 699 |
case 'L': |
| 700 |
lastpsizwins = (was_inline[1] != '0'); |
| 701 |
break; |
| 702 |
case 'U': |
| 703 |
nosmallchars = (was_inline[1] != '0'); |
| 704 |
break; |
| 705 |
case 'W': |
| 706 |
for (p=was_inline+1; *p && *p <= ' '; p++); |
| 707 |
if (*p) |
| 708 |
warningmsg = newstring(p); |
| 709 |
else |
| 710 |
warningmsg = 0; |
| 711 |
break; |
| 712 |
case 'X' : |
| 713 |
if (sscanf(was_inline+1, "%d", &actualdpi) != 1) |
| 714 |
bad_config("missing numeric arg to X"); |
| 715 |
if (actualdpi < 10 || actualdpi > 10000) |
| 716 |
bad_config("X arg must be between 10 and 10000"); |
| 717 |
break; |
| 718 |
case 'Y' : |
| 719 |
if (sscanf(was_inline+1, "%d", &vactualdpi) != 1) |
| 720 |
bad_config("missing numeric arg to Y"); |
| 721 |
if (vactualdpi < 10 || vactualdpi > 10000) |
| 722 |
bad_config("Y arg must be between 10 and 10000"); |
| 723 |
break; |
| 724 |
case 'x': case 'y': |
| 725 |
if (sscanf(was_inline+1, "%lg", &mag) != 1) |
| 726 |
bad_config("missing arg to x or y"); |
| 727 |
overridemag = (was_inline[0] == 'x') ? 1 : -1; |
| 728 |
break; |
| 729 |
case 'e' : |
| 730 |
if (sscanf(was_inline+1, "%d", &maxdrift) != 1) |
| 731 |
bad_config("missing arg to e"); |
| 732 |
if (maxdrift < 0) bad_config("bad argument to e"); |
| 733 |
vmaxdrift = maxdrift; |
| 734 |
break; |
| 735 |
case 'z' : |
| 736 |
if (secure_option && secure && was_inline[1] == '0') { |
| 737 |
fprintf_str(stderr, |
| 738 |
"warning: %s: z0 directive ignored since -R1 given\n", |
| 739 |
realnameoffile); /* Never happen */ |
| 740 |
} else { |
| 741 |
if (was_inline[1] == '0') { |
| 742 |
secure = 0; |
| 743 |
} else if (was_inline[1] == '2') { |
| 744 |
secure = 2; |
| 745 |
} else { |
| 746 |
secure = 1; |
| 747 |
} |
| 748 |
} |
| 749 |
break; |
| 750 |
case 'q' : case 'Q' : |
| 751 |
quiet = (was_inline[1] != '0'); |
| 752 |
break; |
| 753 |
case 'f' : |
| 754 |
filter = (was_inline[1] != '0'); |
| 755 |
if (filter) |
| 756 |
oname = ""; |
| 757 |
/* noenv has already been tested, so no point in setting. */ |
| 758 |
sendcontrolD = 0; |
| 759 |
break; |
| 760 |
#ifdef SHIFTLOWCHARS |
| 761 |
case 'G': |
| 762 |
shiftlowchars = (was_inline[1] != '0'); |
| 763 |
break; |
| 764 |
#endif |
| 765 |
case 'h' : |
| 766 |
if (sscanf(was_inline+1, "%s", PSname) != 1) |
| 767 |
bad_config("missing arg to h"); |
| 768 |
else add_header(PSname); |
| 769 |
break; |
| 770 |
case 'i' : |
| 771 |
if (sscanf(was_inline+1, "%d", &maxsecsize) != 1) |
| 772 |
maxsecsize = 0; |
| 773 |
sepfiles = 1; |
| 774 |
break; |
| 775 |
case 'I': |
| 776 |
noenv = (was_inline[1] != '0'); |
| 777 |
break; |
| 778 |
case 'N' : |
| 779 |
disablecomments = (was_inline[1] != '0'); |
| 780 |
break; |
| 781 |
case 'Z' : |
| 782 |
compressed = (was_inline[1] != '0'); |
| 783 |
break; |
| 784 |
case 'j': |
| 785 |
partialdownload = (was_inline[1] != '0'); |
| 786 |
break; |
| 787 |
case 't' : |
| 788 |
if (sscanf(was_inline+1, "%s", PSname) != 1) |
| 789 |
bad_config("missing arg to t"); |
| 790 |
else { |
| 791 |
if (strcmp(PSname, "landscape") == 0) { |
| 792 |
if (hpapersize || vpapersize) |
| 793 |
error( |
| 794 |
"both landscape and papersize specified; ignoring landscape"); |
| 795 |
else |
| 796 |
landscape = 1; |
| 797 |
} else |
| 798 |
paperfmt = newstring(PSname); |
| 799 |
} |
| 800 |
break; |
| 801 |
case 'c' : |
| 802 |
if (sscanf(was_inline+1, "%s", PSname) != 1) |
| 803 |
bad_config("missing arg to c"); |
| 804 |
else |
| 805 |
getdefaults(PSname); |
| 806 |
break; |
| 807 |
default: |
| 808 |
bad_config("strange line"); |
| 809 |
} |
| 810 |
} |
| 811 |
fclose(deffile); |
| 812 |
} else { |
| 813 |
if (printer) |
| 814 |
{ |
| 815 |
char msg[1000]; |
| 816 |
snprintf (msg, sizeof(msg), "warning: no config file for `%s'", |
| 817 |
printer); |
| 818 |
error(msg); |
| 819 |
return 0; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
return 1; |
| 824 |
} |
| 825 |
|
| 826 |
/* |
| 827 |
* If a character pointer is passed in, use that name; else, use the |
| 828 |
* default (possibly set) name, psfonts.map. |
| 829 |
*/ |
| 830 |
void |
| 831 |
getpsinfo(const char *name) |
| 832 |
{ |
| 833 |
FILE *deffile; |
| 834 |
register char *p; |
| 835 |
char *specinfo, *downloadinfo; |
| 836 |
char downbuf[500]; |
| 837 |
char specbuf[500]; |
| 838 |
int slen; |
| 839 |
|
| 840 |
if (name == 0) |
| 841 |
name = psmapfile; |
| 842 |
if ((deffile=search(mappath, name, READ))!=NULL) { |
| 843 |
if (dvips_debug_flag && !quiet) { |
| 844 |
if (strlen(realnameoffile) + prettycolumn > STDOUTSIZE) { |
| 845 |
fprintf(stderr, "\n"); |
| 846 |
prettycolumn = 0; |
| 847 |
} |
| 848 |
fprintf_str(stderr, "{%s}", realnameoffile); |
| 849 |
prettycolumn += strlen(realnameoffile) + 2; |
| 850 |
} |
| 851 |
while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) { |
| 852 |
p = was_inline; |
| 853 |
if (*p > ' ' && *p != '*' && *p != '#' && *p != ';' && *p != '%') { |
| 854 |
char *TeXname = NULL; |
| 855 |
char *PSname = NULL; |
| 856 |
char *Fontfile = NULL; |
| 857 |
char *Vectfile = NULL; |
| 858 |
char *hdr_name = NULL; |
| 859 |
boolean nopartial_p = false; |
| 860 |
boolean encoding_p = false; |
| 861 |
specinfo = NULL; |
| 862 |
downloadinfo = NULL; |
| 863 |
downbuf[0] = 0; |
| 864 |
specbuf[0] = 0; |
| 865 |
while (*p) { |
| 866 |
encoding_p = false; |
| 867 |
while (*p && *p <= ' ') |
| 868 |
p++; |
| 869 |
if (*p) { |
| 870 |
if (*p == '"') { /* PostScript instructions? */ |
| 871 |
if (specinfo) { |
| 872 |
strcat(specbuf, specinfo); |
| 873 |
strcat(specbuf, " "); |
| 874 |
} |
| 875 |
specinfo = p + 1; |
| 876 |
|
| 877 |
} else if (*p == '<') { /* Header to download? */ |
| 878 |
/* If had previous downloadinfo, save it. */ |
| 879 |
if (downloadinfo) { |
| 880 |
strcat(downbuf, downloadinfo); |
| 881 |
strcat(downbuf, " "); |
| 882 |
downloadinfo = NULL; |
| 883 |
} |
| 884 |
if (p[1] == '<') { /* << means always full download */ |
| 885 |
p++; |
| 886 |
nopartial_p = true; |
| 887 |
} else if (p[1] == '[') { /* <[ means an encoding */ |
| 888 |
p++; |
| 889 |
encoding_p = true; |
| 890 |
} |
| 891 |
p++; |
| 892 |
/* skip whitespace after < */ |
| 893 |
while (*p && *p <= ' ') |
| 894 |
p++; |
| 895 |
|
| 896 |
/* save start of header name */ |
| 897 |
hdr_name = p; |
| 898 |
|
| 899 |
} else if (TeXname) /* second regular word on line? */ |
| 900 |
PSname = p; |
| 901 |
|
| 902 |
else /* first regular word? */ |
| 903 |
TeXname = p; |
| 904 |
|
| 905 |
if (*p == '"') { |
| 906 |
p++; /* find end of "..." word */ |
| 907 |
while (*p != '"' && *p) |
| 908 |
p++; |
| 909 |
} else |
| 910 |
while (*p > ' ') /* find end of anything else */ |
| 911 |
p++; |
| 912 |
if (*p) |
| 913 |
*p++ = 0; |
| 914 |
|
| 915 |
/* If we had a header we were downloading, figure |
| 916 |
out what to do; couldn't do this above since we |
| 917 |
want to check the suffix. */ |
| 918 |
if (hdr_name) { |
| 919 |
const char *suffix = find_suffix (hdr_name); |
| 920 |
if (encoding_p || STREQ (suffix, "enc")) { |
| 921 |
/* (SPQR) if it is a reencoding, pass on to |
| 922 |
FontPart, and download as usual */ |
| 923 |
Vectfile = downloadinfo = hdr_name; |
| 924 |
} else if (nopartial_p) { |
| 925 |
downloadinfo = hdr_name; |
| 926 |
} else if (FILESTRCASEEQ (suffix, "pfa") |
| 927 |
|| FILESTRCASEEQ (suffix, "pfb") |
| 928 |
|| STREQ (suffix, "PFA") |
| 929 |
|| STREQ (suffix, "PFB")) { |
| 930 |
Fontfile = hdr_name; |
| 931 |
} else { |
| 932 |
downloadinfo = hdr_name; |
| 933 |
} |
| 934 |
} |
| 935 |
} |
| 936 |
} |
| 937 |
if (specinfo) |
| 938 |
strcat(specbuf, specinfo); |
| 939 |
if (downloadinfo) |
| 940 |
strcat(downbuf, downloadinfo); |
| 941 |
slen = strlen(downbuf) - 1; |
| 942 |
if (slen > 0 && downbuf[slen] == ' ') { |
| 943 |
downbuf[slen] = 0; |
| 944 |
} |
| 945 |
if (TeXname) { |
| 946 |
TeXname = newstring(TeXname); |
| 947 |
PSname = newstring(PSname); |
| 948 |
Fontfile = newstring(Fontfile); |
| 949 |
Vectfile = newstring(Vectfile); |
| 950 |
specinfo = newstring(specbuf); |
| 951 |
downloadinfo = newstring(downbuf); |
| 952 |
add_entry(TeXname, PSname, Fontfile, Vectfile, |
| 953 |
specinfo, downloadinfo); |
| 954 |
} |
| 955 |
} |
| 956 |
} |
| 957 |
fclose(deffile); |
| 958 |
} |
| 959 |
checkstrings(); |
| 960 |
} |
| 961 |
#ifndef KPATHSEA |
| 962 |
/* |
| 963 |
* Get environment variables! These override entries in ./config.h. |
| 964 |
* We substitute everything of the form ::, ^: or :$ with default, |
| 965 |
* so a user can easily build on to the existing paths. |
| 966 |
*/ |
| 967 |
static char * |
| 968 |
getenvup(char *who, char *what) |
| 969 |
{ |
| 970 |
return getpath(getenv(who), what); |
| 971 |
} |
| 972 |
#endif |
| 973 |
#if !defined(KPATHSEA) && defined(SEARCH_SUBDIRECTORIES) |
| 974 |
static char *concat3(); |
| 975 |
#endif |
| 976 |
void |
| 977 |
checkenv(int which) |
| 978 |
{ |
| 979 |
#ifndef KPATHSEA |
| 980 |
if (which) { |
| 981 |
tfmpath = getenvup(FONTPATH, tfmpath); |
| 982 |
vfpath = getenvup("VFFONTS", vfpath); |
| 983 |
pictpath = getenvup("TEXPICTS", pictpath); |
| 984 |
figpath = getenvup("TEXINPUTS", figpath); |
| 985 |
headerpath = getenvup("DVIPSHEADERS", headerpath); |
| 986 |
if (getenv("TEXPKS")) |
| 987 |
pkpath = getenvup("TEXPKS", pkpath); |
| 988 |
else if (getenv("TEXPACKED")) |
| 989 |
pkpath = getenvup("TEXPACKED", pkpath); |
| 990 |
else if (getenv("PKFONTS")) |
| 991 |
pkpath = getenvup("PKFONTS", pkpath); |
| 992 |
#ifdef SEARCH_SUBDIRECTORIES |
| 993 |
else if (getenv(FONTPATH)) |
| 994 |
pkpath = getenvup(FONTPATH, pkpath); |
| 995 |
if (getenv ("TEXFONTS_SUBDIR")) |
| 996 |
fontsubdirpath = getenvup ("TEXFONTS_SUBDIR", fontsubdirpath); |
| 997 |
{ |
| 998 |
char pathsep[2]; |
| 999 |
char *do_subdir_path(); |
| 1000 |
char *dirs = do_subdir_path (fontsubdirpath); |
| 1001 |
/* If the paths were in dynamic storage before, that memory is |
| 1002 |
wasted now. */ |
| 1003 |
pathsep[0] = PATHSEP; |
| 1004 |
pathsep[1] = '\0'; |
| 1005 |
tfmpath = concat3 (tfmpath, pathsep, dirs); |
| 1006 |
pkpath = concat3 (pkpath, pathsep, dirs); |
| 1007 |
} |
| 1008 |
#endif |
| 1009 |
} else |
| 1010 |
configpath = getenvup("TEXCONFIG", configpath); |
| 1011 |
#endif |
| 1012 |
} |
| 1013 |
|
| 1014 |
#if !defined(KPATHSEA) && defined(SEARCH_SUBDIRECTORIES) |
| 1015 |
|
| 1016 |
#include <sys/types.h> |
| 1017 |
#include <sys/stat.h> |
| 1018 |
#include <errno.h> |
| 1019 |
|
| 1020 |
#ifdef SYSV |
| 1021 |
#include <dirent.h> |
| 1022 |
typedef struct dirent *directory_entry_type; |
| 1023 |
#else |
| 1024 |
#include <sys/dir.h> |
| 1025 |
typedef struct direct *directory_entry_type; |
| 1026 |
#endif |
| 1027 |
|
| 1028 |
/* Declare the routine to get the current working directory. */ |
| 1029 |
|
| 1030 |
#ifndef HAVE_GETCWD |
| 1031 |
extern char *getwd (); |
| 1032 |
#define getcwd(b, len) ((b) ? getwd (b) : getwd (xmalloc (len))) |
| 1033 |
#else |
| 1034 |
#ifdef ANSI |
| 1035 |
extern char *getcwd (char *, int); |
| 1036 |
#else |
| 1037 |
extern char *getcwd (); |
| 1038 |
#endif /* not ANSI */ |
| 1039 |
#endif /* not HAVE_GETWD */ |
| 1040 |
|
| 1041 |
#if defined(SYSV) || defined(VMS) || defined(MSDOS) || defined(OS2) || defined(ATARIST) |
| 1042 |
#define MAXPATHLEN (256) |
| 1043 |
#else /* ~SYSV */ |
| 1044 |
#include <sys/param.h> /* for MAXPATHLEN */ |
| 1045 |
#endif |
| 1046 |
|
| 1047 |
extern void exit(); |
| 1048 |
extern int chdir(); |
| 1049 |
|
| 1050 |
/* Memory operations: variants of malloc(3) and realloc(3) that just |
| 1051 |
give up the ghost when they fail. */ |
| 1052 |
|
| 1053 |
char * |
| 1054 |
xmalloc(unsigned size) |
| 1055 |
{ |
| 1056 |
char *mem = malloc (size); |
| 1057 |
|
| 1058 |
if (mem == NULL) |
| 1059 |
{ |
| 1060 |
fprintf (stderr, "! Cannot allocate %u bytes.\n", size); |
| 1061 |
exit (10); |
| 1062 |
} |
| 1063 |
|
| 1064 |
return mem; |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
char * |
| 1069 |
xrealloc(char *ptr, unsigned size) |
| 1070 |
{ |
| 1071 |
char *mem = realloc (ptr, size); |
| 1072 |
|
| 1073 |
if (mem == NULL) |
| 1074 |
{ |
| 1075 |
fprintf (stderr, "! Cannot reallocate %u bytes at %x.\n", size, (int)ptr); |
| 1076 |
exit (10); |
| 1077 |
} |
| 1078 |
|
| 1079 |
return mem; |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
/* Return, in NAME, the next component of PATH, i.e., the characters up |
| 1084 |
to the next PATHSEP. */ |
| 1085 |
|
| 1086 |
static void |
| 1087 |
next_component(char *name, char **path) |
| 1088 |
{ |
| 1089 |
unsigned count = 0; |
| 1090 |
|
| 1091 |
while (**path != 0 && **path != PATHSEP) |
| 1092 |
{ |
| 1093 |
name[count++] = **path; |
| 1094 |
(*path)++; /* Move further along, even between calls. */ |
| 1095 |
} |
| 1096 |
|
| 1097 |
name[count] = 0; |
| 1098 |
if (**path == PATHSEP) |
| 1099 |
(*path)++; /* Move past the delimiter. */ |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
#ifndef _POSIX_SOURCE |
| 1104 |
#ifndef S_ISDIR |
| 1105 |
#define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR) |
| 1106 |
#endif |
| 1107 |
#endif |
| 1108 |
|
| 1109 |
/* Return true if FN is a directory or a symlink to a directory, |
| 1110 |
false if not. */ |
| 1111 |
|
| 1112 |
int |
| 1113 |
is_dir(char *fn) |
| 1114 |
{ |
| 1115 |
struct stat stats; |
| 1116 |
|
| 1117 |
return stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode); |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
static char * |
| 1122 |
concat3(char *s1, char *s2, char *s3) |
| 1123 |
{ |
| 1124 |
char *r = xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1); |
| 1125 |
strcpy (r, s1); |
| 1126 |
strcat (r, s2); |
| 1127 |
strcat (r, s3); |
| 1128 |
return r; |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
/* DIR_LIST is the default list of directories (colon-separated) to |
| 1133 |
search. We want to add all the subdirectories directly below each of |
| 1134 |
the directories in the path. |
| 1135 |
|
| 1136 |
We return the list of directories found. */ |
| 1137 |
|
| 1138 |
char * |
| 1139 |
do_subdir_path(char *dir_list) |
| 1140 |
{ |
| 1141 |
char *cwd; |
| 1142 |
unsigned len; |
| 1143 |
char *result = xmalloc ((unsigned)1); |
| 1144 |
char *temp = dir_list; |
| 1145 |
char dirsep[2]; |
| 1146 |
|
| 1147 |
dirsep[0] = DIRSEP; |
| 1148 |
dirsep[1] = '\0'; |
| 1149 |
|
| 1150 |
/* Make a copy in writable memory. */ |
| 1151 |
dir_list = xmalloc (strlen (temp) + 1); |
| 1152 |
strcpy (dir_list, temp); |
| 1153 |
|
| 1154 |
*result = 0; |
| 1155 |
|
| 1156 |
/* Unfortunately, we can't look in the environment for the current |
| 1157 |
directory, because if we are running under a program (let's say |
| 1158 |
Emacs), the PWD variable might have been set by Emacs' parent |
| 1159 |
to the current directory at the time Emacs was invoked. This |
| 1160 |
is not necessarily the same directory the user expects to be |
| 1161 |
in. So, we must always call getcwd(3) or getwd(3), even though |
| 1162 |
they are slow and prone to hang in networked installations. */ |
| 1163 |
cwd = getcwd (NULL, MAXPATHLEN + 2); |
| 1164 |
if (cwd == NULL) |
| 1165 |
{ |
| 1166 |
perror ("getcwd"); |
| 1167 |
exit (errno); |
| 1168 |
} |
| 1169 |
|
| 1170 |
do |
| 1171 |
{ |
| 1172 |
DIR *dir; |
| 1173 |
directory_entry_type e; |
| 1174 |
char dirname[MAXPATHLEN]; |
| 1175 |
|
| 1176 |
next_component (dirname, &dir_list); |
| 1177 |
|
| 1178 |
/* All the `::'s should be gone by now, but we may as well make |
| 1179 |
sure `chdir' doesn't crash. */ |
| 1180 |
if (*dirname == 0) continue; |
| 1181 |
|
| 1182 |
/* By changing directories, we save a bunch of string |
| 1183 |
concatenations (and make the pathnames the kernel looks up |
| 1184 |
shorter). */ |
| 1185 |
if (chdir (dirname) != 0) continue; |
| 1186 |
|
| 1187 |
dir = opendir ("."); |
| 1188 |
if (dir == NULL) continue; |
| 1189 |
|
| 1190 |
while ((e = readdir (dir)) != NULL) |
| 1191 |
{ |
| 1192 |
if (is_dir (e->d_name) && strcmp (e->d_name, ".") != 0 |
| 1193 |
&& strcmp (e->d_name, "..") != 0) |
| 1194 |
{ |
| 1195 |
char *found = concat3 (dirname, dirsep, e->d_name); |
| 1196 |
|
| 1197 |
result = xrealloc (result, strlen (result) + strlen (found) + 2); |
| 1198 |
|
| 1199 |
len = strlen (result); |
| 1200 |
if (len > 0) |
| 1201 |
{ |
| 1202 |
result[len] = PATHSEP; |
| 1203 |
result[len + 1] = 0; |
| 1204 |
} |
| 1205 |
strcat (result, found); |
| 1206 |
free (found); |
| 1207 |
} |
| 1208 |
} |
| 1209 |
closedir (dir); |
| 1210 |
|
| 1211 |
/* Change back to the current directory, in case the path |
| 1212 |
contains relative directory names. */ |
| 1213 |
if (chdir (cwd) != 0) |
| 1214 |
{ |
| 1215 |
perror (cwd); |
| 1216 |
exit (errno); |
| 1217 |
} |
| 1218 |
} |
| 1219 |
while (*dir_list != 0); |
| 1220 |
|
| 1221 |
return result; |
| 1222 |
} |
| 1223 |
#endif /* SEARCH_SUBDIRECTORIES */ |