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