00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #pragma ident "@(#) libf/fio/opn.c 92.3 06/23/99 16:08:16"
00043
00044 #include <errno.h>
00045 #include <fortran.h>
00046 #include <liberrno.h>
00047 #include <string.h>
00048 #include <stdarg.h>
00049 #include <cray/nassert.h>
00050 #ifdef KEY
00051 # include <cray/assign.h>
00052 #endif
00053 #include "fmt.h"
00054 #include "fio.h"
00055 #include "f90io.h"
00056
00057 #define OPNERR(n) { \
00058 errn = n; \
00059 goto opn_done; \
00060 }
00061
00062 #define SETSPEC(_specifier, _default_value, _error_code, _nval, _spec_list) \
00063 \
00064 if (_fcdtocp(_specifier) == NULL) \
00065 a.o##_specifier = _default_value; \
00066 else if (! findmatch(_specifier, (int*) &a.o##_specifier, _nval \
00067 _spec_list)) \
00068 OPNERR(_error_code)
00069
00070 #undef S
00071 #define S(_spec) , #_spec, OS_##_spec
00072
00073
00074
00075
00076
00077 #define ARGS_11 (4 + 7*sizeof(_fcd)/sizeof(long))
00078 #define ARGS_12 (4 + 8*sizeof(_fcd)/sizeof(long))
00079 #define ARGS_13 (4 + 9*sizeof(_fcd)/sizeof(long))
00080 #define ARGS_16 (7 + 9*sizeof(_fcd)/sizeof(long))
00081 #if _UNICOS
00082 #define PASSED_ARG(x) (_numargs() >= x)
00083 #else
00084 #define PASSED_ARG(x) (1)
00085 #endif
00086
00087
00088 static int findmatch(_fcd fortstring, int *result, int nval, ...);
00089
00090
00091
00092
00093
00094 #ifdef _UNICOS
00095 #pragma _CRI duplicate __OPN as $OPN
00096 #endif
00097
00098 #ifdef _CRAYMPP
00099 __OPN(
00100 _f_int *unitn,
00101 _f_int *iostat,
00102 int *errf,
00103 ...
00104 )
00105 #else
00106 int __OPN(
00107 _f_int *unitn,
00108 _f_int *iostat,
00109 int *errf,
00110 _fcd file,
00111 _fcd status,
00112 _fcd access,
00113 _fcd form,
00114 _f_int *recl,
00115 _fcd blank,
00116 _fcd position,
00117 _fcd action_arg,
00118 _fcd delim_arg,
00119 _fcd pad_arg,
00120 int unused1,
00121 int unused2,
00122 int isf90_arg)
00123 #endif
00124 {
00125 olist a;
00126 long fstrlen;
00127 int errn;
00128 int error;
00129 unum_t unum;
00130 _fcd action;
00131 _fcd delim;
00132 _fcd pad;
00133 int isf90;
00134 unit *cup;
00135 enum form_spec formdef;
00136 struct fiostate cfs;
00137
00138 #ifdef _CRAYMPP
00139 va_list args;
00140 _fcd file;
00141 _fcd status;
00142 _fcd access;
00143 _fcd form;
00144 _f_int *recl;
00145 _fcd blank;
00146 _fcd position;
00147 int unused1;
00148 int unused2;
00149 int isf90_arg;
00150 #endif
00151
00152
00153
00154
00155
00156
00157 action = _cptofcd(NULL, 0);
00158 delim = _cptofcd(NULL, 0);
00159 pad = _cptofcd(NULL, 0);
00160 #ifdef _CRAYMPP
00161 va_start(args,errf);
00162 file = va_arg(args, _fcd);
00163 status = va_arg(args, _fcd);
00164 access = va_arg(args, _fcd);
00165 form = va_arg(args, _fcd);
00166 recl = va_arg(args, _f_int *);
00167 blank = va_arg(args, _fcd);
00168 position = va_arg(args, _fcd);
00169
00170 #endif
00171 if (PASSED_ARG(ARGS_11)) {
00172 #ifdef _CRAYMPP
00173 action = va_arg(args, _fcd);
00174 #else
00175 action = action_arg;
00176 #endif
00177 }
00178 if (PASSED_ARG(ARGS_12)) {
00179 #ifdef _CRAYMPP
00180 delim = va_arg(args, _fcd);
00181 #else
00182 delim = delim_arg;
00183 #endif
00184 }
00185 if (PASSED_ARG(ARGS_13)) {
00186 #ifdef _CRAYMPP
00187 pad = va_arg(args, _fcd);
00188 #else
00189 pad = pad_arg;
00190 #endif
00191 }
00192
00193
00194
00195 isf90 = 0;
00196
00197 if (PASSED_ARG(ARGS_16)) {
00198 #ifdef _CRAYMPP
00199 unused1 = va_arg(args, int);
00200 unused2 = va_arg(args, int);
00201 isf90 = va_arg(args, int);
00202 #else
00203 isf90 = isf90_arg;
00204 #endif
00205 }
00206 #ifdef _CRAYMPP
00207 va_end(args);
00208 #endif
00209 errn = 0;
00210
00211 OPENLOCK();
00212
00213 #ifdef KEY
00214
00215
00216
00217 __io_byteswap();
00218 #endif
00219
00220 unum = *unitn;
00221 a.ounit = unum;
00222
00223 STMT_BEGIN(unum, 0, T_OPEN, NULL, &cfs, cup);
00224
00225 if (!GOOD_UNUM(unum) || RSVD_UNUM(unum))
00226 OPNERR(FEIVUNTO);
00227
00228 a.oerr = (errf || iostat) ? 1 : 0;
00229
00230
00231
00232
00233 if (_fcdtocp(file) != NULL) {
00234 a.ofile = _fcdtocp(file);
00235 a.ofilelen = _fcdlen (file);
00236 }
00237 else {
00238 a.ofile = NULL;
00239 a.ofilelen = 0;
00240 }
00241
00242 if (recl != NULL)
00243 a.orecl = *recl;
00244 else
00245 a.orecl = 0;
00246
00247
00248
00249
00250
00251
00252
00253
00254 SETSPEC(status, OS_UNKNOWN, FEOPSTAT, 5,
00255 S(OLD) S(NEW) S(SCRATCH) S(UNKNOWN) S(REPLACE));
00256
00257 #if !defined(__mips) && !defined(_LITTLE_ENDIAN)
00258 SETSPEC(access, OS_SEQUENTIAL, FEOPACCS, 2,
00259 S(DIRECT) S(SEQUENTIAL));
00260 SETSPEC(position, OS_ASIS, FEOPPOSN, 3,
00261 S(APPEND) S(ASIS) S(REWIND));
00262 #else
00263 SETSPEC(access, OS_SEQUENTIAL, FEOPACCS, 4,
00264 S(DIRECT) S(SEQUENTIAL) S(KEYED) S(APPEND));
00265 if ((_fcdtocp(access) != NULL) && (a.oaccess == OS_OAPPEND)) {
00266 if (_fcdtocp(position) != NULL) {
00267 OPNERR(FEOPACCS);
00268 }
00269 #ifdef KEY
00270
00271
00272
00273
00274 #else
00275 else if (isf90) {
00276 OPNERR(FEOPACCS);
00277 }
00278 #endif
00279 else {
00280 a.oposition = OS_APPEND;
00281 a.oaccess = OS_SEQUENTIAL;
00282 }
00283 }
00284 else {
00285
00286 SETSPEC(position, OS_ASIS, FEOPPOSN, 3,
00287 S(APPEND) S(ASIS) S(REWIND));
00288 }
00289 #endif
00290
00291 formdef = (a.oaccess == OS_SEQUENTIAL) ? OS_FORMATTED : OS_UNFORMATTED;
00292
00293 SETSPEC(form, formdef, FEOPFORM, 4,
00294 S(UNFORMATTED) S(FORMATTED) S(BINARY) S(SYSTEM));
00295
00296 SETSPEC(blank, OS_NULL, FEOPBLNK, 2,
00297 S(ZERO) S(NULL));
00298
00299 SETSPEC(action, OS_ACTION_UNSPECIFIED, FEOPACTB, 3,
00300 S(READ) S(WRITE) S(READWRITE));
00301
00302 SETSPEC(delim, OS_NONE, FEOPDLMB, 3,
00303 S(APOSTROPHE) S(QUOTE) S(NONE));
00304
00305 SETSPEC(pad, OS_YES, FEOPPADB, 2,
00306 S(YES) S(NO));
00307
00308
00309
00310
00311
00312 if (recl != NULL && a.orecl <= 0)
00313 OPNERR(FEOPRECL);
00314
00315 if (recl == NULL && a.oaccess == OS_DIRECT)
00316 OPNERR(FEOPRCRQ);
00317
00318 if (_fcdtocp(blank) != NULL && (a.oform == OS_UNFORMATTED ||
00319 a.oform == OS_BINARY || a.oform == OS_SYSTEM))
00320 OPNERR(FEOPBKIV);
00321
00322 if (_fcdtocp(delim) != NULL && (a.oform == OS_UNFORMATTED ||
00323 a.oform == OS_BINARY || a.oform == OS_SYSTEM))
00324 OPNERR(FEOPDLMI);
00325
00326 if (_fcdtocp(pad) != NULL && (a.oform == OS_UNFORMATTED ||
00327 a.oform == OS_BINARY || a.oform == OS_SYSTEM))
00328 OPNERR(FEOPPDIV);
00329
00330 if (_fcdtocp(position) != NULL && a.oaccess == OS_DIRECT)
00331 OPNERR(FEOPPSIV);
00332
00333
00334
00335
00336 if (OPEN_UPTR(cup) && cup->ufs == FS_AUX)
00337 OPNERR(FEOPAUXT);
00338
00339 if (OPEN_UPTR(cup) &&
00340 (_fcdtocp(file) == NULL || (cup->ufnm != NULL &&
00341 strncmp(cup->ufnm, a.ofile, a.ofilelen) == 0))) {
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 if (_fcdtocp(status) != NULL && a.ostatus != cup->uostatus) {
00358 if (a.ostatus == OS_NEW && cup->uostatus == OS_OLD) {
00359 OPNERR(FEOPNNEW);
00360 }
00361 else
00362 OPNERR(FEOPCBNK);
00363 }
00364
00365 if (_fcdtocp(access) != NULL &&
00366 ((a.oaccess == OS_SEQUENTIAL && cup->useq == 0 ) ||
00367 (a.oaccess == OS_DIRECT && cup->useq == 1) ))
00368 OPNERR(FEOPCBNK);
00369
00370 if (_fcdtocp(form) != NULL &&
00371 ((a.oform == OS_FORMATTED && cup->ufmt == 0) ||
00372 (a.oform == OS_UNFORMATTED && cup->ufmt == 1) ))
00373 OPNERR(FEOPCBNK);
00374
00375 if (recl != NULL && a.orecl != cup->urecl)
00376 OPNERR(FEOPCBNK);
00377
00378 if (_fcdtocp(position) != NULL && a.oposition != cup->uposition)
00379 OPNERR(FEOPCBNK);
00380
00381 if (_fcdtocp(action) != NULL && a.oaction != cup->uaction)
00382 OPNERR(FEOPCBNK);
00383
00384
00385
00386
00387
00388
00389 if (_fcdtocp(blank) != NULL)
00390 cup->ublnk = (a.oblank == OS_ZERO);
00391
00392 if (_fcdtocp(delim) != NULL)
00393 cup->udelim = a.odelim;
00394
00395 if (_fcdtocp(pad) != NULL)
00396 cup->upad = a.opad;
00397 }
00398 else {
00399
00400
00401
00402
00403
00404
00405
00406 #if (!defined(__mips) && !defined(_LITTLE_ENDIAN)) || defined(KEY)
00407
00408
00409
00410
00411
00412 if (a.ostatus == OS_REPLACE && a.ofile == NULL)
00413 OPNERR(FEOPFNRQ);
00414
00415 if (a.ostatus == OS_OLD && a.ofile == NULL)
00416 OPNERR(FEOPFNRQ);
00417
00418 if (a.ostatus == OS_NEW && a.ofile == NULL)
00419 OPNERR(FEOPFNRQ);
00420 #endif
00421 #ifdef _CRAYMPP
00422
00423
00424
00425 if (a.ostatus == OS_SCRATCH && a.ofile != NULL)
00426 OPNERR(FEOPFNIV);
00427 #endif
00428
00429
00430
00431
00432
00433
00434 errn = _f_open(&cfs, &cup, &a, isf90);
00435 }
00436
00437
00438
00439
00440 opn_done:
00441 error = (errn != 0) ? IO_ERR : IO_OKAY;
00442
00443 if (iostat != NULL)
00444 *iostat = errn;
00445 else
00446 if (error != IO_OKAY && errf == 0)
00447 if (errn == FEIVUNTO)
00448 _ferr(&cfs, errn, unum);
00449 else
00450 _ferr(&cfs, errn);
00451
00452 STMT_END(cup, T_OPEN, NULL, NULL);
00453
00454 OPENUNLOCK();
00455
00456 return(CFT77_RETVAL(error));
00457 }
00458
00459
00460
00461
00462 int
00463 _OPEN(struct open_spec_list *o)
00464 {
00465
00466
00467
00468
00469 assert ( o->version == 0 );
00470
00471 return( __OPN(o->unit, o->iostat, (int*)o->err, o->file, o->status,
00472 o->access, o->form, o->recl, o->blank, o->position,
00473 o->action, o->delim, o->pad, (int)NULL, (int)NULL, 1) );
00474 }
00475
00476
00477 #if 0
00478
00479
00480
00481
00482
00483 int
00484 _OPN(
00485 _f_int *unitn,
00486 _f_int *iostat,
00487 int *errf,
00488 _fcd file,
00489 _fcd status,
00490 _fcd access,
00491 _fcd form,
00492 _f_int *recl,
00493 _fcd blank,
00494 _fcd position,
00495 _fcd action_arg,
00496 _fcd delim_arg,
00497 _fcd pad_arg)
00498 {
00499
00500
00501
00502
00503 return( __OPN(unitn, iostat, errf, file, status, access, form, recl, \
00504 blank, position, action_arg, delim_arg, pad_arg,
00505 NULL, NULL, 1) );
00506 }
00507 #endif
00508
00509
00510
00511
00512
00513
00514
00515
00516 static int
00517 findmatch(_fcd fortstring, int *result, int nval, ...)
00518 {
00519 va_list ap;
00520 char *fstring;
00521 long fstrlen;
00522 int _string_cmp();
00523 char *next_string;
00524 int next_value;
00525 int ret, i;
00526
00527 va_start(ap, nval);
00528
00529 fstring = _fcdtocp(fortstring);
00530 fstrlen = _fcdlen (fortstring);
00531
00532 ret = 0;
00533
00534 for (i = 0; i < nval; i++) {
00535 next_string = va_arg(ap, char *);
00536 next_value = va_arg(ap, int);
00537 if (_string_cmp(next_string, fstring, fstrlen)) {
00538 *result = next_value;
00539 ret = 1;
00540 break;
00541 }
00542 }
00543
00544 va_end(ap);
00545
00546 return(ret);
00547 }
00548
00549 #if defined(__mips) || defined(_LITTLE_ENDIAN)
00550
00551
00552
00553
00554 int
00555 _OPENF77(struct open_spec_list *o)
00556 {
00557
00558
00559
00560
00561 assert ( o->version == 0 );
00562
00563 return( __OPN(o->unit, o->iostat, (int*)o->err, o->file, o->status,
00564 o->access, o->form, o->recl, o->blank, o->position,
00565 o->action, o->delim, o->pad, (int)NULL, (int)NULL, (int)NULL) );
00566 }
00567 #endif