| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1988-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Correspondence concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 29 |
.TYPE Module |
| 30 |
.NAME osfparse.c |
| 31 |
.LANGUAGE C |
| 32 |
.AUTHOR IPG-ESO Garching |
| 33 |
.ENVIRONMENT Unix Version |
| 34 |
.CATEGORY Host operating system interfaces. File management. |
| 35 |
.COMMENTS |
| 36 |
File name Parsing. |
| 37 |
A file name can be decomposed into path, filename, |
| 38 |
extension and version. VMS, Unix or MSDOS file naming |
| 39 |
conventions are recognized. |
| 40 |
|
| 41 |
|
| 42 |
\begin{TeX} |
| 43 |
In particular, the VMS convention of the colon ({\bf:}) |
| 44 |
representing a path is accepted, even with multiple pathnames. |
| 45 |
The following example shows how the translation works with osftr: |
| 46 |
Let's assume that the environment variable {\em myPath} is defined as |
| 47 |
\fbox{{\tt .,\$home,/tmp}}. The file named {\tt myPath:test.log} |
| 48 |
will be looked for, first in the current directory ({\tt .}), |
| 49 |
if not found in the {\tt \$home} directory, and if still not found |
| 50 |
in the {\tt /tmp} directory. |
| 51 |
\end{TeX} |
| 52 |
|
| 53 |
|
| 54 |
.VERSION 1.0 05-Feb-1988 F. Ochsenbein. |
| 55 |
.VERSION 1.1 05-Apr-1989 Don't convert to uppercase in Unix... |
| 56 |
.VERSION 1.2 19-Jun-1989 Convention of Logical Names followed by : |
| 57 |
(to convert names from VMS) |
| 58 |
.VERSION 1.3 05-Dec-1989 Names followed by : may have several paths, |
| 59 |
separated by commas. |
| 60 |
.VERSION 1.4 31-Jan-1990 Removed bug in osfsupply - Cosmetics |
| 61 |
.VERSION 1.5 23-Nov-1990 Don't crash if NULL filename |
| 62 |
|
| 63 |
090324 last modif |
| 64 |
-----------------------------------------------*/ |
| 65 |
|
| 66 |
#include <filedef.h> |
| 67 |
#include <atype.h> |
| 68 |
#include <macrogen.h> |
| 69 |
#include <sys/types.h> |
| 70 |
#include <sys/stat.h> |
| 71 |
#include <proto_os.h> /* ANSI-C prototyping */ |
| 72 |
|
| 73 |
#ifndef F_OK |
| 74 |
#define F_OK 0 |
| 75 |
#endif |
| 76 |
|
| 77 |
#define COMPONENTS (__PATH_+1) |
| 78 |
static short int comp1[COMPONENTS][2] = { {0,0}}; |
| 79 |
static short int comp2[COMPONENTS][2] = { {0,0}}; |
| 80 |
|
| 81 |
static char *buffer = NULL_PTR(char); |
| 82 |
static int lbuffer = 0; |
| 83 |
/* Index + length of components */ |
| 84 |
|
| 85 |
/*===========================================================================*/ |
| 86 |
#ifdef __STDC__ |
| 87 |
static void strlower (char * str ) |
| 88 |
#else |
| 89 |
static void strlower ( str ) |
| 90 |
/*+++++++++ |
| 91 |
.PURPOSE Converts (in place) a string to lower case |
| 92 |
.RETURNS Nothing |
| 93 |
--------*/ |
| 94 |
char *str; /* MOD: starting address */ |
| 95 |
#endif |
| 96 |
{ |
| 97 |
register char *p; |
| 98 |
|
| 99 |
for (p = str; *p; p++) |
| 100 |
*p = tolower(*p); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
/*==========================================================================*/ |
| 105 |
#ifdef __STDC__ |
| 106 |
static int split(char * name , short comp[6][2]) |
| 107 |
#else |
| 108 |
static int split(name, comp) |
| 109 |
/*+++ |
| 110 |
.PURPOSE Split a file name into components, returned in `comp'. |
| 111 |
.RETURNS 0 (Unix) / 1 (VMS or MSDOS) |
| 112 |
.REMARKS |
| 113 |
---*/ |
| 114 |
char *name; /* IN: EOS-terminated File Name */ |
| 115 |
short int comp[COMPONENTS][2]; /* OUT: Index + length of components*/ |
| 116 |
#endif |
| 117 |
{ |
| 118 |
register int i, len, status; |
| 119 |
|
| 120 |
oscfill((char *)comp, sizeof(comp1), 0); /* initialize length and indexes */ |
| 121 |
|
| 122 |
len = strlen(name); |
| 123 |
|
| 124 |
/* The structure of VMS file names is: |
| 125 |
NODE::DEVICE:[DIRECTORY]FILE.EXTENSION;VERSION */ |
| 126 |
|
| 127 |
/* Look for path name (terminated by / ) */ |
| 128 |
|
| 129 |
i = oscbloc(name, len, '/'); /* Unix--like */ |
| 130 |
if (i < 0) i = oscbloc(name, len, '\\'); /* Look MSDOS */ |
| 131 |
if (i < 0) i = oscbloc(name, len, ']') ; /* Look VMS */ |
| 132 |
if (i < 0) i = oscbloc(name, len, ':') ; /* Look VMS */ |
| 133 |
|
| 134 |
status = 0; |
| 135 |
|
| 136 |
if (i >= 0) /* A Path was found, reset name to remaining chars */ |
| 137 |
{ status = *(name+i); |
| 138 |
if ( (status == ']') || (status == '\\') ) status = 1; |
| 139 |
else status = 0; |
| 140 |
|
| 141 |
comp[__PATH_][1] = ++i; |
| 142 |
name += i, len -= i; |
| 143 |
for (i = __PATH_; --i >= 0; ) /* Set Indexes */ |
| 144 |
comp[i][0] = comp[__PATH_][1]; |
| 145 |
} |
| 146 |
|
| 147 |
/* Look for version */ |
| 148 |
|
| 149 |
i = oscbloc(name, len, ';'); |
| 150 |
if (i >= 0) /* Version Found */ |
| 151 |
{ comp[__VERSION_][0] += i; /* Offset */ |
| 152 |
comp[__VERSION_][1] = len - i; |
| 153 |
len = i; |
| 154 |
} |
| 155 |
|
| 156 |
/* Look for extension */ |
| 157 |
|
| 158 |
i = oscbloc(name, len, '.'); |
| 159 |
if (i >= 0) /* Extension Found */ |
| 160 |
{ comp[__EXTENSION_][0] += i; |
| 161 |
comp[__EXTENSION_][1] = len - i; |
| 162 |
len = i; |
| 163 |
} |
| 164 |
|
| 165 |
/* Look for file name (remaining bytes) */ |
| 166 |
|
| 167 |
comp[__FILE_][1] = len; |
| 168 |
|
| 169 |
return(status); |
| 170 |
} |
| 171 |
|
| 172 |
/*==========================================================================*/ |
| 173 |
#ifdef __STDC__ |
| 174 |
static char * path(char * name , int len) |
| 175 |
#else |
| 176 |
static char *path(name, len) |
| 177 |
/*+++ |
| 178 |
.PURPOSE Convert a non-Unix (i.e. VMS or MSDOS) pathname |
| 179 |
.RETURNS Pointer to EOS-terminated pathname if transformation needed |
| 180 |
.REMARKS Unix-like $... are converted. |
| 181 |
---*/ |
| 182 |
char *name; /* IN: Path Name */ |
| 183 |
int len; /* IN: Length of Path Name */ |
| 184 |
#endif |
| 185 |
{ |
| 186 |
char *p, *pe, *q, savec, dir_sep; |
| 187 |
char *oshenv(); |
| 188 |
int i; |
| 189 |
char the_path[256]; |
| 190 |
static char zero = EOS; |
| 191 |
static char *pathname = NULL_PTR(char); |
| 192 |
static int lpathname = 0; |
| 193 |
|
| 194 |
if (len <= 0) return(&zero); |
| 195 |
|
| 196 |
pe = name + len - 1; /* Last character */ |
| 197 |
dir_sep = *pe; |
| 198 |
savec = '\0'; /* right default value? */ |
| 199 |
|
| 200 |
if ((dir_sep == '/') && (*name != '$')) |
| 201 |
return(name); /* No translation required */ |
| 202 |
|
| 203 |
i = 0; /* Index in the_path */ |
| 204 |
if (*name == '$') |
| 205 |
{ q = name + oscloc(name, len, dir_sep); |
| 206 |
savec = *q, *q = EOS; |
| 207 |
p = oshenv(name+1, NULL_PTR(char)); |
| 208 |
if (p) |
| 209 |
{ while ( (*p) && (i < sizeof(the_path)-1)) |
| 210 |
the_path[i++] = *(p++); |
| 211 |
name = q; |
| 212 |
} |
| 213 |
*q = savec; |
| 214 |
} |
| 215 |
for (p = name; (p <= pe) && (i < sizeof(the_path)-1); ) |
| 216 |
the_path[i++] = *(p++); |
| 217 |
the_path[i] = EOS; |
| 218 |
|
| 219 |
/* The local variable the_path now contains the path name. |
| 220 |
Compute length, and |
| 221 |
Change it if source is MSDOS or VMS |
| 222 |
*/ |
| 223 |
|
| 224 |
if (dir_sep == ']') |
| 225 |
for (p = the_path; *p; p++) |
| 226 |
if (*p == '-') i += 2; /* VMS - must translate to ../ */ |
| 227 |
|
| 228 |
if (i > lpathname) /* Expand Buffer */ |
| 229 |
lpathname = (i+63)& ~63, pathname = osmmexp(pathname, lpathname); |
| 230 |
|
| 231 |
/* Copy with modifications */ |
| 232 |
|
| 233 |
p = the_path, q = pathname; |
| 234 |
|
| 235 |
if (dir_sep == ']') for( ; *p ; p++) switch(*p) |
| 236 |
{ case '[': |
| 237 |
savec = 1; /* The next dash has a special meaning */ |
| 238 |
if (*(++p) == '.') continue; |
| 239 |
if (*(p--) != '-') *(q++) = '/'; |
| 240 |
continue; |
| 241 |
case ']': |
| 242 |
savec = 1; /* The next dash has a special meaning */ |
| 243 |
*(q++) = '/'; |
| 244 |
continue; |
| 245 |
case '-': |
| 246 |
if (savec) |
| 247 |
{ savec = 0; /* The next dash is standard */ |
| 248 |
*(q++) = '.', *(q++) = '.'; |
| 249 |
continue; |
| 250 |
} |
| 251 |
case '.': |
| 252 |
savec = 1; /* The next dash has a special meaning */ |
| 253 |
*(q++) = '/'; continue; |
| 254 |
default: |
| 255 |
savec = 0; /* The next dash is standard */ |
| 256 |
*(q++) = *p; continue; |
| 257 |
} |
| 258 |
else for( ; *p ; p++) |
| 259 |
{ *(q++) = (*p == '\\' ? '/' : *p); |
| 260 |
} |
| 261 |
*q = EOS; |
| 262 |
|
| 263 |
return(pathname); |
| 264 |
} |
| 265 |
|
| 266 |
/*======================================================================*/ |
| 267 |
char *osfparse(name, option) |
| 268 |
/*+++ |
| 269 |
.PURPOSE Extract from a file name the specified items, copy them |
| 270 |
to an internal buffer. |
| 271 |
.RETURNS Address of the extracted item(s). |
| 272 |
.REMARKS Options are _PATH_, _FILE_, _EXTENSION_, _VERSION_. |
| 273 |
Filenames are NOT converted (use osftr or osfsupply) |
| 274 |
---*/ |
| 275 |
char *name; /* IN: File name */ |
| 276 |
unsigned int option; /* IN: what to extract, as a \ |
| 277 |
combination of options */ |
| 278 |
{ |
| 279 |
char *p; |
| 280 |
int i, l; |
| 281 |
char local_copy[256]; |
| 282 |
|
| 283 |
if (!name) name = ""; /* Take default name... */ |
| 284 |
|
| 285 |
if (name == buffer) { /* Copy first... */ |
| 286 |
l = strlen(name) + 1; |
| 287 |
l = MIN(l, sizeof(local_copy)); |
| 288 |
oscopy(local_copy, name, l); |
| 289 |
name = local_copy; |
| 290 |
} |
| 291 |
split(name, comp1); /* Split the filename into its components */ |
| 292 |
|
| 293 |
|
| 294 |
/* Compute the required length */ |
| 295 |
|
| 296 |
l = 1; /* Take into account the EOS! */ |
| 297 |
for (i = COMPONENTS; --i>=0; ) |
| 298 |
if (option & (1<<i)) l += comp1[i][1]; |
| 299 |
|
| 300 |
if (l > lbuffer) |
| 301 |
lbuffer = (l+63)& ~63, buffer = osmmexp(buffer, lbuffer); |
| 302 |
|
| 303 |
|
| 304 |
/* Now, copy the required items */ |
| 305 |
|
| 306 |
p = buffer; |
| 307 |
for (i=COMPONENTS; --i>=0; ) |
| 308 |
if (option & (1<<i)) |
| 309 |
p += oscopy(p, name + comp1[i][0], comp1[i][1]); |
| 310 |
*p = EOS; /* Terminate with EOS */ |
| 311 |
|
| 312 |
return(buffer); |
| 313 |
} |
| 314 |
|
| 315 |
/*======================================================================*/ |
| 316 |
char *osfsupply(name, template) |
| 317 |
/*+++ |
| 318 |
.PURPOSE Convert a file name according to a specified template: |
| 319 |
missing parts in name are replaced by the corresponding parts |
| 320 |
provided in template. |
| 321 |
The resulting name is copied (in uppercase on VMS) |
| 322 |
to an internal buffer. |
| 323 |
.RETURNS The address of the internal buffer |
| 324 |
---*/ |
| 325 |
char *name; /* IN: File name */ |
| 326 |
char *template; /* IN: Template name */ |
| 327 |
{ |
| 328 |
char *oshenv(); |
| 329 |
char *p, *pname; |
| 330 |
int i, l, lpath, lp, in_path; |
| 331 |
char local_copy[256], isvms, ismult, next_path; |
| 332 |
|
| 333 |
/* First, split the filenames into its components */ |
| 334 |
|
| 335 |
if (!name) name = ""; /* Take default name... */ |
| 336 |
|
| 337 |
next_path = '\0'; |
| 338 |
|
| 339 |
if (name == buffer) { /* Copy first... */ |
| 340 |
l = strlen(name) + 1; |
| 341 |
l = MIN(l, sizeof(local_copy)); |
| 342 |
oscopy(local_copy, name, l); |
| 343 |
name = local_copy; |
| 344 |
} |
| 345 |
|
| 346 |
isvms = split(name, comp1); |
| 347 |
isvms |= split(template, comp2); |
| 348 |
|
| 349 |
if (comp1[__PATH_][1]) pname = name + comp1[__PATH_][0], |
| 350 |
lpath = comp1[__PATH_][1]; |
| 351 |
else if (comp2[__PATH_][1]) pname = template + comp2[__PATH_][0], |
| 352 |
lpath = comp2[__PATH_][1]; |
| 353 |
else pname = NULL_PTR(char), lpath = 0; |
| 354 |
|
| 355 |
ismult = 0; |
| 356 |
in_path = 0; |
| 357 |
if (pname) |
| 358 |
{ p = pname + lpath - 1; |
| 359 |
if (*p == ':') ismult = *p, *p = EOS; |
| 360 |
} |
| 361 |
else ismult = 0; |
| 362 |
|
| 363 |
/* The loop is executed more than once only when the pathname |
| 364 |
is terminated with ':' and there are several pathnames |
| 365 |
separated by commas |
| 366 |
*/ |
| 367 |
|
| 368 |
while(1) |
| 369 |
{ if (ismult) /* Several Names. */ |
| 370 |
{ if_not(p = oshenv(pname, NULL_PTR(char))) p = pname; |
| 371 |
p += in_path; |
| 372 |
lp = oscloc(p, strlen(p), ','); |
| 373 |
next_path = *(p+lp); |
| 374 |
in_path += (lp+1); |
| 375 |
} |
| 376 |
else /* Translate Name */ |
| 377 |
{ p = pname, lp = lpath; |
| 378 |
if (p) p = path(p, lp); |
| 379 |
if (p != pname) lp = strlen(p); |
| 380 |
} |
| 381 |
|
| 382 |
/* Compute the required length */ |
| 383 |
|
| 384 |
l = lp + 2 ; /* Take into account EOS and final / */ |
| 385 |
for (i = __PATH_; --i>=0; ) |
| 386 |
{ if (comp1[i][1]) l += comp1[i][1]; |
| 387 |
else l += comp2[i][1]; |
| 388 |
} |
| 389 |
if (l > lbuffer) |
| 390 |
lbuffer = (l+63)& ~63, buffer = osmmexp(buffer, lbuffer); |
| 391 |
|
| 392 |
/* Now, copy the required items */ |
| 393 |
|
| 394 |
if (p) |
| 395 |
{ p = buffer + oscopy(buffer, p, lp); /* Copy PathName */ |
| 396 |
if(ismult) *(p++) = '/'; |
| 397 |
} |
| 398 |
else p = buffer; |
| 399 |
|
| 400 |
for (i = __PATH_; --i>=0; ) |
| 401 |
{ if (comp1[i][1]) p += oscopy(p, name + comp1[i][0], comp1[i][1]); |
| 402 |
else p += oscopy(p, template + comp2[i][0], comp2[i][1]); |
| 403 |
} |
| 404 |
*p = EOS; /* Terminate with EOS */ |
| 405 |
|
| 406 |
if (ismult && (next_path == ',')) /* Check File Exists */ |
| 407 |
{ |
| 408 |
if (access(buffer, F_OK) != 0) |
| 409 |
continue; |
| 410 |
} |
| 411 |
break; |
| 412 |
} |
| 413 |
|
| 414 |
if (ismult) |
| 415 |
*(pname + lpath - 1) = ismult; |
| 416 |
|
| 417 |
if(isvms) strlower(buffer); |
| 418 |
|
| 419 |
return(buffer); |
| 420 |
} |
| 421 |
|
| 422 |
/*======================================================================*/ |
| 423 |
char *osftr(name) |
| 424 |
/*+++ |
| 425 |
.PURPOSE Convert filename. |
| 426 |
.RETURNS The address of the converted filename. |
| 427 |
---*/ |
| 428 |
char *name; /* IN: File name */ |
| 429 |
{ |
| 430 |
|
| 431 |
if (name != buffer) osfsupply(name, ""); |
| 432 |
|
| 433 |
return(buffer); |
| 434 |
} |
| 435 |
|