| 1 |
/* @(#)osfparse.c 19.1 (ES0-DMD) 02/25/03 13:56:12 */ |
| 2 |
/*=========================================================================== |
| 3 |
Copyright (C) 1995 European Southern Observatory (ESO) |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of |
| 8 |
the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public |
| 16 |
License along with this program; if not, write to the Free |
| 17 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 18 |
MA 02139, USA. |
| 19 |
|
| 20 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 21 |
Internet e-mail: [email protected] |
| 22 |
Postal address: European Southern Observatory |
| 23 |
Data Management Division |
| 24 |
Karl-Schwarzschild-Strasse 2 |
| 25 |
D 85748 Garching bei Muenchen |
| 26 |
GERMANY |
| 27 |
===========================================================================*/ |
| 28 |
|
| 29 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 30 |
.TYPE Module |
| 31 |
.NAME osfparse.c |
| 32 |
.LANGUAGE C |
| 33 |
.AUTHOR IPG-ESO Garching |
| 34 |
.ENVIRONMENT VAX / VMS Version |
| 35 |
.CATEGORY Host operating system interfaces. File management. |
| 36 |
.COMMENTS |
| 37 |
File name Parsing. |
| 38 |
A file name can be decomposed into path, filename, |
| 39 |
extension and version. |
| 40 |
|
| 41 |
.VERSION 1.0 05-Feb-1988 F. Ochsenbein. |
| 42 |
.VERSION 1.1 19-Jun-1989 Convention of Logical Names followed by : |
| 43 |
(to convert names from Unix) |
| 44 |
.VERSION 1.2 05-Dec-1989 Take care of .. = up subdirectory in Unix. |
| 45 |
Several paths are available with logicals, e,g, |
| 46 |
LOG:file.vers with ASSIGN p1,p2 LOG |
| 47 |
.VERSION 1.3 31-Jan-1990 Cosmetic |
| 48 |
.VERSION 1.4 23-Nov-1990 Don't crash if NULL filename |
| 49 |
-----------------------------------------------*/ |
| 50 |
|
| 51 |
#include <osfile.h> |
| 52 |
#include <atype.h> |
| 53 |
#include <macrogen.h> |
| 54 |
|
| 55 |
#define COMPONENTS (__PATH_+1) |
| 56 |
static short int comp1[COMPONENTS][2] = { {0,0}}; |
| 57 |
static short int comp2[COMPONENTS][2] = { {0,0}}; |
| 58 |
/* Index + length of components */ |
| 59 |
|
| 60 |
static char *buffer = NULL_PTR(char); |
| 61 |
static int lbuffer = 0; |
| 62 |
char *osmmget(), *osmmexp(); |
| 63 |
|
| 64 |
/*===========================================================================*/ |
| 65 |
static void strupper ( str ) |
| 66 |
/*+++++++++ |
| 67 |
.PURPOSE Converts (in place) a string to upper case |
| 68 |
.RETURNS Nothing |
| 69 |
--------*/ |
| 70 |
char *str; /* MOD: starting address */ |
| 71 |
{ |
| 72 |
register char *p; |
| 73 |
|
| 74 |
for (p = str; *p; p++) |
| 75 |
*p = toupper(*p); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
/*==========================================================================*/ |
| 80 |
static int split(name, comp) |
| 81 |
/*+++ |
| 82 |
.PURPOSE Split a file name into components, returned in `comp'. |
| 83 |
.RETURNS The character that ends up the path (] or : or / or EOS) |
| 84 |
.REMARKS Only written for VMS... |
| 85 |
---*/ |
| 86 |
char *name; /* IN: EOS-terminated File Name */ |
| 87 |
short int comp[COMPONENTS][2]; /* OUT: Index + length of components*/ |
| 88 |
{ |
| 89 |
register int i, len, status; |
| 90 |
|
| 91 |
oscfill((char *)comp, sizeof(comp1), 0); /* initialize length and indexes */ |
| 92 |
|
| 93 |
len = strlen(name); |
| 94 |
|
| 95 |
/* The structure of VMS file names is: |
| 96 |
NODE::DEVICE:[DIRECTORY]FILE.EXTENSION;VERSION */ |
| 97 |
|
| 98 |
/* Look for path name (terminated by / ) */ |
| 99 |
|
| 100 |
i = oscbloc(name, len, ']'); /* VMS--like */ |
| 101 |
if (i < 0) i = oscbloc(name, len, '/') ; /* Look Unix */ |
| 102 |
if (i < 0) i = oscbloc(name, len, '\\'); /* Look MSDOS */ |
| 103 |
if (i < 0) i = oscbloc(name, len, ':') ; /* Look VMS */ |
| 104 |
|
| 105 |
status = 0; |
| 106 |
|
| 107 |
if (i >= 0) /* A Path was found, reset name to remaining chars */ |
| 108 |
{ status = *(name + i); |
| 109 |
comp[__PATH_][1] = ++i; |
| 110 |
name += i, len -= i; |
| 111 |
for (i = __PATH_; --i >= 0; ) /* Set Indexes */ |
| 112 |
comp[i][0] = comp[__PATH_][1]; |
| 113 |
} |
| 114 |
|
| 115 |
/* Look for version */ |
| 116 |
|
| 117 |
i = oscbloc(name, len, ';'); |
| 118 |
if (i >= 0) /* Version Found */ |
| 119 |
{ comp[__VERSION_][0] += i; /* Offset */ |
| 120 |
comp[__VERSION_][1] = len - i; |
| 121 |
len = i; |
| 122 |
} |
| 123 |
|
| 124 |
/* Look for extension */ |
| 125 |
|
| 126 |
i = oscbloc(name, len, '.'); |
| 127 |
if (i >= 0) /* Extension Found */ |
| 128 |
{ comp[__EXTENSION_][0] += i; |
| 129 |
comp[__EXTENSION_][1] = len - i; |
| 130 |
len = i; |
| 131 |
} |
| 132 |
|
| 133 |
/* Look for file name (remaining bytes) */ |
| 134 |
|
| 135 |
comp[__FILE_][1] = len; |
| 136 |
|
| 137 |
return(status); |
| 138 |
} |
| 139 |
|
| 140 |
/*==========================================================================*/ |
| 141 |
static char *path(name, len) |
| 142 |
/*+++ |
| 143 |
.PURPOSE Convert a non-VMS (i.e. Unix or MSDOS) pathname |
| 144 |
.RETURNS Pointer to EOS-terminated pathname if transformation needed |
| 145 |
.REMARKS |
| 146 |
---*/ |
| 147 |
char *name; /* IN: Path Name */ |
| 148 |
int len; /* IN: Length of Path Name */ |
| 149 |
{ |
| 150 |
char *p, *pe, *q, *start, savec, dir_sep; |
| 151 |
char *oshenv(); |
| 152 |
int leq, l; |
| 153 |
static char zero = EOS; |
| 154 |
static char *pathname = NULL_PTR(char); /* Returned path */ |
| 155 |
static int lpathname = 0; |
| 156 |
|
| 157 |
if (len <= 0) return(&zero); |
| 158 |
|
| 159 |
pe = name + len - 1; /* Last character */ |
| 160 |
dir_sep = *pe; /* Is / for Unix, \ for MSDOS */ |
| 161 |
|
| 162 |
if ((dir_sep == ']') || (dir_sep == ':')) return(name); /* VMS Notation */ |
| 163 |
|
| 164 |
/* The path uses, either Unix, or MSDOS notation. |
| 165 |
dir_sep is '/' (Unix notation) or '\' (MSDOS notation) */ |
| 166 |
|
| 167 |
l = len + 4; /* Minimal Size for Translated Path Name */ |
| 168 |
|
| 169 |
if (*name == '$') q = name + oscloc(name, len, dir_sep); |
| 170 |
else q = name + len; |
| 171 |
|
| 172 |
if (q <= pe) /* Requires Logical Translation */ |
| 173 |
{ savec = *q; |
| 174 |
*q = EOS; |
| 175 |
p = oshenv(name+1, NULL_PTR(char)); |
| 176 |
*q = savec; |
| 177 |
leq = (p ? strlen(p) : 0); |
| 178 |
l = leq + 4 + (pe-q); |
| 179 |
} |
| 180 |
else leq = 0, l = len+1; |
| 181 |
l += 3; |
| 182 |
|
| 183 |
if (l > lpathname) /* Expand Buffer */ |
| 184 |
lpathname = (l+63)& ~63, pathname = osmmexp(pathname, lpathname); |
| 185 |
|
| 186 |
/* Copy with modificataions */ |
| 187 |
|
| 188 |
if(leq) /* Name comes from a $path (logical translation) */ |
| 189 |
{ |
| 190 |
oscopy(pathname, p, leq); |
| 191 |
p = q; |
| 192 |
start = pathname + leq - 1; /* Last byte of translation */ |
| 193 |
if (*start != ']') *++start = '[', start++; |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
p = name; |
| 198 |
start = pathname, *(start++) = '['; |
| 199 |
if ( *p == dir_sep ) p++; /* It's Root directory */ |
| 200 |
else *(start++) = '.'; /* It's a subdirectory */ |
| 201 |
} |
| 202 |
|
| 203 |
for (q = start; p <= pe; p++, q++) |
| 204 |
{ if (*p == '.') /* Special Files */ |
| 205 |
{ if (*++p == dir_sep) /* ./ = current direct. */ |
| 206 |
{ --q; continue;} |
| 207 |
if (*p == '.') /* ../ = up directory */ |
| 208 |
{ *q = '-'; continue; } |
| 209 |
--p; |
| 210 |
} |
| 211 |
*q = ( *p == dir_sep ? '.' : *p) ; |
| 212 |
} |
| 213 |
|
| 214 |
if (*(q-1) == '[') *(q++) = ']'; |
| 215 |
if (*(q-1) == '.') *(q-1) = ']'; |
| 216 |
*q = EOS; |
| 217 |
|
| 218 |
return(pathname); |
| 219 |
} |
| 220 |
|
| 221 |
/*======================================================================*/ |
| 222 |
char *osfparse(name, option) |
| 223 |
/*+++ |
| 224 |
.PURPOSE Extract from a file name the specified items, copy them |
| 225 |
to an internal buffer. |
| 226 |
.RETURNS Address of the item(s) to extract |
| 227 |
.REMARKS Options are _PATH_, _FILE_, _EXTENSION_, _VERSION_. |
| 228 |
VMS filenames are converted. |
| 229 |
---*/ |
| 230 |
char *name; /* IN: File name */ |
| 231 |
unsigned int option; /* IN: what to extract, as a \ |
| 232 |
combination of options */ |
| 233 |
{ |
| 234 |
char *p, *pname; /* Pathname */ |
| 235 |
int i, l, lpath; |
| 236 |
char local_copy[256]; |
| 237 |
|
| 238 |
if (!name) name = ""; /* Take default name... */ |
| 239 |
|
| 240 |
if (name == buffer) { /* Copy first... */ |
| 241 |
l = strlen(name) + 1; |
| 242 |
l = MIN(l, sizeof(local_copy)); |
| 243 |
oscopy(local_copy, name, l); |
| 244 |
name = local_copy; |
| 245 |
} |
| 246 |
|
| 247 |
split(name, comp1); /* Split the filename into its components */ |
| 248 |
|
| 249 |
if (option & _PATH_) |
| 250 |
{ |
| 251 |
p = name + comp1[__PATH_][0]; |
| 252 |
pname = path(p, comp1[__PATH_][1]); |
| 253 |
if (p == pname) pname = NULL_PTR(char); /* Was not modified */ |
| 254 |
else comp1[__PATH_][1] = 0; |
| 255 |
} |
| 256 |
else pname = NULL_PTR(char); |
| 257 |
|
| 258 |
/* Compute the required length */ |
| 259 |
|
| 260 |
lpath = 0; |
| 261 |
l = 1; /* Take into account the EOS! */ |
| 262 |
if (pname) lpath = strlen(pname), l += lpath; |
| 263 |
|
| 264 |
for (i = __PATH_; --i>=0; ) |
| 265 |
if (option & (1<<i)) l += comp1[i][1]; |
| 266 |
|
| 267 |
if (l > lbuffer) |
| 268 |
lbuffer = (l+63)& ~63, buffer = osmmexp(buffer, lbuffer); |
| 269 |
|
| 270 |
|
| 271 |
/* Now, copy the required items */ |
| 272 |
|
| 273 |
p = buffer; |
| 274 |
if (pname) p += oscopy(p, pname, lpath); |
| 275 |
|
| 276 |
for (i=COMPONENTS; --i>=0; ) |
| 277 |
if (option & (1<<i)) |
| 278 |
p += oscopy(p, name + comp1[i][0], comp1[i][1]); |
| 279 |
*p = EOS; /* Terminate with EOS */ |
| 280 |
|
| 281 |
|
| 282 |
strupper(buffer); /* Convert to UpperCase */ |
| 283 |
|
| 284 |
return(buffer); |
| 285 |
} |
| 286 |
|
| 287 |
/*======================================================================*/ |
| 288 |
char *osfsupply(name, template) |
| 289 |
/*+++ |
| 290 |
.PURPOSE Convert a file name according to a specified template: |
| 291 |
missing parts in name are replaced by the corresponding parts |
| 292 |
provided in template. |
| 293 |
The resulting name is copied (in uppercase on VMS) |
| 294 |
to an internal buffer. |
| 295 |
.RETURNS The address of the internal buffer |
| 296 |
---*/ |
| 297 |
char *name; /* IN: File name */ |
| 298 |
char *template; /* IN: Template name */ |
| 299 |
{ |
| 300 |
char *p, *pname; |
| 301 |
int i, l, lpath; |
| 302 |
char local_copy[256]; |
| 303 |
|
| 304 |
/* First, split the filenames into its components */ |
| 305 |
|
| 306 |
if (!name) name = ""; /* Take default name... */ |
| 307 |
|
| 308 |
if (name == buffer) { /* Copy first... */ |
| 309 |
l = strlen(name) + 1; |
| 310 |
l = MIN(l, sizeof(local_copy)); |
| 311 |
oscopy(local_copy, name, l); |
| 312 |
name = local_copy; |
| 313 |
} |
| 314 |
|
| 315 |
split(name, comp1); |
| 316 |
split(template, comp2); |
| 317 |
|
| 318 |
if (comp1[__PATH_][1]) pname = name + comp1[__PATH_][0], |
| 319 |
lpath = comp1[__PATH_][1]; |
| 320 |
else if (comp2[__PATH_][1]) pname = template + comp2[__PATH_][0], |
| 321 |
lpath = comp2[__PATH_][1]; |
| 322 |
else pname = NULL_PTR(char), lpath = 0; |
| 323 |
|
| 324 |
if (pname) { |
| 325 |
p = pname, pname = path(p, lpath); |
| 326 |
if (pname != p) lpath = strlen(pname); |
| 327 |
} |
| 328 |
|
| 329 |
/* Compute the required length */ |
| 330 |
|
| 331 |
l = 1 + lpath; /* Take into account the EOS! */ |
| 332 |
for (i = __PATH_; --i>=0; ) { |
| 333 |
if (comp1[i][1]) l += comp1[i][1]; |
| 334 |
else l += comp2[i][1]; |
| 335 |
} |
| 336 |
|
| 337 |
if (l > lbuffer) |
| 338 |
lbuffer = (l+63)& ~63, buffer = osmmexp(buffer, lbuffer); |
| 339 |
|
| 340 |
/* Now, copy the required items */ |
| 341 |
|
| 342 |
p = buffer; |
| 343 |
if (pname) p += oscopy(p, pname, lpath); |
| 344 |
|
| 345 |
for (i = __PATH_; --i>=0; ) { |
| 346 |
if (comp1[i][1]) p += oscopy(p, name + comp1[i][0], comp1[i][1]); |
| 347 |
else p += oscopy(p, template + comp2[i][0], comp2[i][1]); |
| 348 |
} |
| 349 |
*p = EOS; /* Terminate with EOS */ |
| 350 |
|
| 351 |
strupper(buffer); /* Convert to UpperCase */ |
| 352 |
|
| 353 |
return(buffer); |
| 354 |
} |
| 355 |
|
| 356 |
/*======================================================================*/ |
| 357 |
char *osftr(name) |
| 358 |
/*+++ |
| 359 |
.PURPOSE Convert filename. |
| 360 |
.RETURNS The address of the converted filename. |
| 361 |
---*/ |
| 362 |
char *name; /* IN: File name */ |
| 363 |
{ |
| 364 |
|
| 365 |
if (name != buffer) osfsupply(name, ""); |
| 366 |
|
| 367 |
return(buffer); |
| 368 |
} |
| 369 |
|