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 static char USMID[] = "@(#) libcif/cifopen.c 30.6 05/22/97 11:49:31";
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #define CIF_VERSION 3
00051
00052 #ifdef _ABSOFT
00053 #include "cif.h"
00054 #else
00055 #include <cif.h>
00056 #endif
00057
00058 #include <stdlib.h>
00059 #include <stdio.h>
00060 #include <string.h>
00061 #include <sys/types.h>
00062 #include <sys/stat.h>
00063
00064 #define DEFGLOBAL
00065 #include "cif_int.h"
00066
00067
00068 #if !(defined(__sgi) || defined(__sun) || defined(__alpha))
00069 extern int fileno(FILE *stream);
00070
00071 #endif
00072 extern char *strdup(const char *s);
00073
00074
00075 #define HDRBUF_SIZE 128
00076 #define HDRTOKENS 10
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 int _cif_version = 0;
00089
00090
00091 #undef Cif_Open
00092
00093
00094
00095
00096
00097
00098
00099
00100 int Cif_Open_V1
00101 #ifdef __STDC__
00102 (char *filename, char *optype, int *rtypes, int version)
00103 #else
00104 (filename, optype, rtypes, version)
00105 char *filename;
00106 char *optype;
00107 int *rtypes;
00108 int version;
00109 #endif
00110 {
00111
00112
00113
00114
00115
00116 if (version > 1)
00117 return (CIF_EXP_VERS);
00118
00119 _cif_version = 1;
00120
00121
00122
00123 return( Cif_Open(filename, optype, rtypes, version) );
00124
00125 }
00126
00127
00128
00129
00130
00131
00132 int Cif_Open_V2
00133 #ifdef __STDC__
00134 (char *filename, char *optype, int *rtypes, int version)
00135 #else
00136 (filename, optype, rtypes, version)
00137 char *filename;
00138 char *optype;
00139 int *rtypes;
00140 int version;
00141 #endif
00142 {
00143
00144
00145
00146
00147
00148 if (version < 2)
00149 return (CIF_EXP_VERS2);
00150
00151 _cif_version = 2;
00152
00153
00154
00155 return( Cif_Open(filename, optype, rtypes, version) );
00156
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166 int Cif_Open_V2_1
00167 #ifdef __STDC__
00168 (char *filename, char *optype, int *rtypes, int version, int sub_version)
00169 #else
00170 (filename, optype, rtypes, version, sub_version)
00171 char *filename;
00172 char *optype;
00173 int *rtypes;
00174 int version;
00175 int sub_version;
00176 #endif
00177 {
00178
00179
00180
00181
00182
00183 if (version < 2)
00184 return (CIF_EXP_VERS2);
00185
00186 if (sub_version != CIF_SUB_VERSION_2)
00187 return(CIF_SUBVER);
00188
00189 _cif_version = 2;
00190
00191
00192
00193 return( Cif_Open(filename, optype, rtypes, version) );
00194
00195 }
00196
00197
00198
00199
00200 int Cif_Open_V3_1
00201 #ifdef __STDC__
00202 (char *filename, char *optype, int *rtypes, int version, int sub_version)
00203 #else
00204 (filename, optype, rtypes, version, sub_version)
00205 char *filename;
00206 char *optype;
00207 int *rtypes;
00208 int version;
00209 int sub_version;
00210 #endif
00211 {
00212
00213
00214
00215
00216
00217
00218 if (version < 3)
00219 return (CIF_EXP_VERS2);
00220
00221 if (sub_version != CIF_SUB_VERSION_3)
00222 return(CIF_SUBVER);
00223
00224 _cif_version = 3;
00225
00226
00227
00228 return( Cif_Open(filename, optype, rtypes, version) );
00229
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 int Cif_Open
00242 #ifdef __STDC__
00243 (char *filename, char *optype, int *rtypes, int version)
00244 #else
00245 (filename, optype, rtypes, version)
00246 char *filename;
00247 char *optype;
00248 int *rtypes;
00249 int version;
00250 #endif
00251 {
00252 int i;
00253 int cx;
00254 int *rt;
00255 FILE *fd;
00256 char hdrbuf[HDRBUF_SIZE];
00257 struct stat statbuf;
00258
00259 static char ascii_hdrkey[6] = { '2',SEPARATOR,'c','i','f',SEPARATOR };
00260 static char binary_hdrkey[6] = { SEPARATOR,'c','i','f','b',SEPARATOR };
00261 static char binary_version = _CIF_INT_VERSION;
00262
00263 int rtype;
00264 struct Cif_generic *rptr;
00265
00266 if (_cif_version == 0) {
00267
00268
00269
00270
00271
00272
00273 if (version > 1)
00274 return (CIF_EXP_VERS);
00275
00276 _cif_version = 1;
00277
00278 }
00279
00280
00281
00282
00283
00284 if (version > _CIF_INT_VERSION)
00285 return (CIF_EXP_VERS);
00286 for (cx=0; cx < CIF_FT_SIZE; cx++)
00287 if (_Cif_filetbl[cx].form == NOT_A_CIF) break;
00288 if (cx >= CIF_FT_SIZE)
00289 return (CIF_MAXOPENS);
00290 _Cif_filetbl[cx].ifull = NO;
00291 _Cif_filetbl[cx].seek = NO;
00292 _Cif_filetbl[cx].version = 0;
00293 _Cif_filetbl[cx].return_version = 0;
00294 _Cif_filetbl[cx].ip = NULL;
00295 _Cif_filetbl[cx].mode = CIF_MEM_DEFAULT;
00296
00297
00298
00299 if (strcmp (optype, "r") == 0 || strcmp (optype, "w") == 0)
00300 _Cif_filetbl[cx].optype = *optype;
00301 else
00302 return (CIF_BADREQ);
00303 if (strcmp(filename, "-") == 0) {
00304 if (*optype == 'r')
00305 fd = stdin;
00306 else
00307 fd = stdout;
00308 }
00309 else if ((fd = fopen(filename, optype)) == NULL)
00310 return (CIF_SYSERR);
00311
00312
00313
00314
00315
00316 if (*optype == 'r') {
00317
00318 if (fread (hdrbuf, sizeof(char), 6, fd) != 6) {
00319 (void) fclose (fd);
00320 return (CIF_NOTCIF);
00321 }
00322
00323 else if (strncmp (hdrbuf, ascii_hdrkey, 6) == 0) {
00324
00325
00326
00327 _Cif_filetbl[cx].form = ASCII_CIF;
00328 if ((_Cif_filetbl[cx].ip = malloc (CIF_BUFSIZE)) == NULL)
00329 return (CIF_NOMEM);
00330 (void) strncpy (_Cif_filetbl[cx].ip, hdrbuf, 6);
00331 if (fgets (_Cif_filetbl[cx].ip+6, CIF_BUFSIZE-6, fd) == NULL)
00332 IO_ERROR;
00333 i = atoi(_Cif_filetbl[cx].ip+7);
00334 if (i > _CIF_INT_VERSION)
00335 return (CIF_FILE_VERS);
00336
00337 _Cif_filetbl[cx].version = i;
00338 _Cif_filetbl[cx].return_version = version;
00339
00340 _Cif_filetbl[cx].ifull = YES;
00341 }
00342
00343 else if (strncmp (hdrbuf, binary_hdrkey, 6) == 0) {
00344
00345
00346
00347 _Cif_filetbl[cx].form = BINARY_CIF;
00348 if (fread (hdrbuf, sizeof(char), 1, fd) != 1)
00349 IO_ERROR;
00350 if (hdrbuf[0] > _CIF_INT_VERSION)
00351 return (CIF_FILE_VERS);
00352
00353 _Cif_filetbl[cx].version = hdrbuf[0];
00354 _Cif_filetbl[cx].return_version = version;
00355
00356 }
00357
00358 else
00359 return (CIF_NOTCIF);
00360
00361 if (rtypes == NULL)
00362 for (i = 0; i < CIF_MAXRECORD; i++) _Cif_filetbl[cx].rmask[i] = '\1';
00363 else {
00364 for (i = 0; i < CIF_MAXRECORD; i++) _Cif_filetbl[cx].rmask[i] = '\0';
00365 for (rt = rtypes; *rt != 0; rt++)
00366 _Cif_filetbl[cx].rmask[*rt] = '\1';
00367 }
00368
00369
00370
00371 if (version == 1) {
00372 _Cif_filetbl[cx].rmask[CIF_C_LINT_DIRECTIVE] = '\0';
00373 _Cif_filetbl[cx].rmask[CIF_C_MACRO_DEF] = '\0';
00374 _Cif_filetbl[cx].rmask[CIF_C_MACRO_UNDEF] = '\0';
00375 _Cif_filetbl[cx].rmask[CIF_C_MACRO_USAGE] = '\0';
00376 _Cif_filetbl[cx].rmask[CIF_C_ENTRY_END] = '\0';
00377
00378 _Cif_filetbl[cx].rmask[CIF_CDIR] = '\0';
00379 _Cif_filetbl[cx].rmask[CIF_CDIR_DOSHARED] = '\0';
00380 _Cif_filetbl[cx].rmask[CIF_GEOMETRY] = '\0';
00381 _Cif_filetbl[cx].rmask[CIF_CONTINUATION] = '\0';
00382 _Cif_filetbl[cx].rmask[CIF_TRANSFORM] = '\0';
00383
00384 _Cif_filetbl[cx].rmask[CIF_F90_CALLSITE] = '\0';
00385 _Cif_filetbl[cx].rmask[CIF_F90_COMBLK] = '\0';
00386 _Cif_filetbl[cx].rmask[CIF_F90_CONST] = '\0';
00387 _Cif_filetbl[cx].rmask[CIF_F90_ENTRY] = '\0';
00388 _Cif_filetbl[cx].rmask[CIF_F90_LOOP] = '\0';
00389 _Cif_filetbl[cx].rmask[CIF_F90_DERIVED_TYPE] = '\0';
00390 _Cif_filetbl[cx].rmask[CIF_F90_LABEL] = '\0';
00391 _Cif_filetbl[cx].rmask[CIF_F90_NAMELIST] = '\0';
00392 _Cif_filetbl[cx].rmask[CIF_F90_OBJECT] = '\0';
00393 _Cif_filetbl[cx].rmask[CIF_F90_MISC_OPTS] = '\0';
00394 _Cif_filetbl[cx].rmask[CIF_F90_OPT_OPTS] = '\0';
00395 _Cif_filetbl[cx].rmask[CIF_F90_BEGIN_SCOPE] = '\0';
00396 _Cif_filetbl[cx].rmask[CIF_F90_END_SCOPE] = '\0';
00397 _Cif_filetbl[cx].rmask[CIF_F90_SCOPE_INFO] = '\0';
00398 _Cif_filetbl[cx].rmask[CIF_F90_USE_MODULE] = '\0';
00399 _Cif_filetbl[cx].rmask[CIF_F90_RENAME] = '\0';
00400 _Cif_filetbl[cx].rmask[CIF_F90_INT_BLOCK] = '\0';
00401 _Cif_filetbl[cx].rmask[CIF_F90_VECTORIZATION] = '\0';
00402
00403 _Cif_filetbl[cx].rmask[CIF_BE_NODE] = '\0';
00404 _Cif_filetbl[cx].rmask[CIF_BE_FID] = '\0';
00405
00406 }
00407
00408 if (version < 3) {
00409
00410 _Cif_filetbl[cx].rmask[CIF_CC_TYPE] = '\0';
00411 _Cif_filetbl[cx].rmask[CIF_CC_ENTRY] = '\0';
00412 _Cif_filetbl[cx].rmask[CIF_CC_OBJ] = '\0';
00413 _Cif_filetbl[cx].rmask[CIF_CC_SUBTYPE] = '\0';
00414 _Cif_filetbl[cx].rmask[CIF_CC_ENUM] = '\0';
00415 _Cif_filetbl[cx].rmask[CIF_CC_EXPR] = '\0';
00416
00417 _Cif_filetbl[cx].rmask[CIF_SRC_POS] = '\0';
00418 _Cif_filetbl[cx].rmask[CIF_ORIG_CMD] = '\0';
00419
00420 }
00421 }
00422
00423 else {
00424 _Cif_filetbl[cx].form = BINARY_CIF;
00425 if (fwrite (binary_hdrkey, sizeof(char), 6, fd) != 6)
00426 return (CIF_SYSERR);
00427 if (fwrite (&binary_version, sizeof(char), 1, fd) != 1)
00428 return (CIF_SYSERR);
00429
00430
00431
00432
00433
00434
00435
00436 _Cif_filetbl[cx].version = version;
00437 _Cif_filetbl[cx].return_version = version;
00438 }
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 (void) fstat ((int) (fileno(fd) & 0xffffffff), &statbuf);
00455 _Cif_filetbl[cx].seek = (S_ISREG(statbuf.st_mode) ? YES : NO);
00456
00457 _Cif_filetbl[cx].fd = fd;
00458
00459
00460 _Cif_filetbl[cx].filename = strdup(filename);
00461
00462
00463
00464
00465
00466
00467 _Cif_filetbl[cx].tmp_cif = 0;
00468
00469
00470 if (*optype == 'r' &&
00471 _Cif_filetbl[cx].seek == YES &&
00472 _Cif_filetbl[cx].return_version > 1) {
00473
00474
00475
00476
00477
00478
00479
00480
00481 (void) Cif_Setpos(cx, CIF_FIRST_RECORD);
00482
00483 do {
00484 rtype = Cif_Getrecord(cx, &rptr);
00485 }
00486 while (rtype > 0 && rtype != CIF_SRCFILE &&
00487 rtype != CIF_UNIT);
00488
00489 if (rtype == CIF_SRCFILE) {
00490 _Cif_filetbl[cx].srcfid = CIFSRC(rptr)->fid;
00491 }
00492
00493
00494 _Cif_filetbl[cx].mode = CIF_MEM_DEFAULT;
00495 }
00496
00497
00498 if (_Cif_filetbl[cx].seek == YES)
00499 (void) Cif_Setpos(cx, CIF_FIRST_RECORD);
00500
00501 return (cx);
00502
00503 }