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 #include <isam.h>
00041 #include <mutex.h>
00042 #include <cmplrs/fio.h>
00043 #include <cmplrs/f_errno.h>
00044 #include "fmt.h"
00045 #include "idxio.h"
00046 #include "err.h"
00047 #include "wsfe.h"
00048 #include "lio.h"
00049 #include "sue.h"
00050 #include "open.h"
00051 #include "uio.h"
00052 #include "bcompat.h"
00053 #include "util.h"
00054
00055
00056 extern int isread(int, char *, int);
00057 extern int isstart(int, struct keydesc *, int, char *, int);
00058 extern int isrewcurr(int, char *);
00059 extern int isrelease(int);
00060 extern int isbuild(char *, int, struct keydesc *, int);
00061 extern int isaddindex(int, struct keydesc *);
00062 extern int isclose(int);
00063 extern int isopen(char *, int);
00064 extern int isindexinfo(int, void *, int);
00065 extern int iserase(char *);
00066 extern int isdelcurr(int);
00067 extern int iswrite(int, char *);
00068 extern int iscleanup(void);
00069 extern void stlong(ftnint, char *);
00070 extern int iserrno;
00071
00072
00073
00074 #define KEYOFF(x) keys[x].e1
00075 #define KEYLEN(x) keys[x].e2 - keys[x].e1 + 1
00076 #define KEYTYPE(x) keys[x].dt
00077 #define KEYEND(x) keys[x].e2
00078
00079 #define SUCCESS 0
00080
00081 #define ONEKEY 1
00082 #define UNITKEY 2
00083 #define NOMATCH 3
00084
00085 Keyspec *keys;
00086 struct keydesc onekey ={ISNODUPS, 1};
00087
00088 int
00089 idxopen (unit *ftnunit, char *name, int create, flag idxerr)
00090 {
00091 struct dictinfo info;
00092 int mode =(ftnunit->ureadonly ? ISINPUT : ISINOUT) +
00093 (ftnunit->ushared ? ISAUTOLOCK : ISEXCLLOCK);
00094 register int i;
00095
00096 keys = ftnunit->ukeys;
00097 if (create) {
00098 if (ftnunit->url == 0)
00099 err (idxerr, 153, "indexed open");
00100 if (ftnunit->unkeys == 0 || keys == NULL)
00101 err (idxerr, 161, "indexed open");
00102 onekey.k_flags = ISNODUPS;
00103 KEYOFF (0)--;
00104 KEYEND (0)--;
00105 dokey (0, ONEKEY);
00106 if (ftnunit->unkeys == 1) {
00107
00108 if ((ftnunit->isfd = isbuild (name, ftnunit->url, &onekey, mode)) < SUCCESS)
00109 ierr (idxerr, iserrno, "indexed open");
00110 } else {
00111 if ((ftnunit->isfd =
00112 isbuild (name, ftnunit->url, &onekey, ISINOUT + ISEXCLLOCK)) < SUCCESS)
00113 ierr (idxerr, iserrno, "indexed open");
00114 onekey.k_flags = ISDUPS;
00115 for (i = 1; i < ftnunit->unkeys; i++) {
00116 KEYOFF (i)--;
00117 KEYEND (i)--;
00118 dokey (i, ONEKEY);
00119 if (isaddindex (ftnunit->isfd, &onekey) < SUCCESS)
00120 ierr (idxerr, iserrno, "indexed open");
00121 }
00122 if (ftnunit->ushared) {
00123 if (isclose (ftnunit->isfd) < SUCCESS)
00124 ierr (idxerr, iserrno, "indexed open");
00125 if ((ftnunit->isfd = isopen (name, mode)) < SUCCESS)
00126 ierr (idxerr, iserrno, "indexed open");
00127 }
00128 }
00129 } else {
00130 if ((ftnunit->isfd = isopen (name, mode)) < SUCCESS)
00131 ierr (idxerr, iserrno, "indexed open");
00132 if (isindexinfo (ftnunit->isfd, &info, 0) < SUCCESS)
00133 ierr (idxerr, iserrno, "indexed open");
00134 if (ftnunit->unkeys != info.di_nkeys) {
00135 if (ftnunit->unkeys) {
00136 err (idxerr, 148, "indexed open");
00137 } else
00138 ftnunit->unkeys = info.di_nkeys;
00139 }
00140 if (!keys)
00141 keys = (Keyspec *) malloc (sizeof (Keyspec) * info.di_nkeys);
00142 for (i = 0; i < info.di_nkeys; i++) {
00143 if (isindexinfo (ftnunit->isfd, &onekey, i + 1) < SUCCESS)
00144 ierr (idxerr, iserrno, "indexed open");
00145
00146
00147
00148
00149
00150 KEYOFF (i)--;
00151 KEYEND (i)--;
00152
00153 if (ftnunit->ukeys && dokey (i, NOMATCH)) {
00154 err (idxerr, 148, "indexed open");
00155 } else
00156 dokey (i, UNITKEY);
00157 }
00158 ftnunit->url = info.di_recsize;
00159 ftnunit->ukeys = keys;
00160 }
00161 return SUCCESS;
00162 }
00163
00164 int
00165 idxclose (unit *ftnunit, flag idxerr)
00166 {
00167 if (isclose (ftnunit->isfd) < SUCCESS)
00168 ierr (idxerr, iserrno, "indexed close");
00169 if (ftnunit->ufnm) {
00170 if (ftnunit->udisp & DELETE)
00171 if (iserase (ftnunit->ufnm) < SUCCESS)
00172 ierr (idxerr, iserrno, "indexed close");
00173 free (ftnunit->ufnm);
00174 ftnunit->ufnm = NULL;
00175 }
00176
00177
00178
00179
00180
00181
00182 if (ftnunit->f77syl) {
00183 free(ftnunit->f77syl);
00184 ftnunit->f77syl = NULL;
00185 }
00186 if (ftnunit->f77fio_buf) {
00187 free(ftnunit->f77fio_buf);
00188 ftnunit->f77fio_buf = NULL;
00189 ftnunit->f77fio_size = 0;
00190 }
00191 if (ftnunit->ukeys) {
00192 free(ftnunit->ukeys);
00193 ftnunit->ukeys = NULL;
00194 }
00195 ftnunit->ufd = NULL;
00196 ftnunit->uconn = 0;
00197 ftnunit->luno = 0;
00198 ftnunit->ukeyid = -1;
00199 return SUCCESS;
00200 }
00201
00202 int
00203 idxread (unit *ftnunit)
00204 {
00205 register int i;
00206 register char *keyval, *field;
00207 int newkeyid = 0, mode = ftnunit->f77idxlist.cimatch;
00208
00209 keys = ftnunit->ukeys;
00210 ftnunit->f77recend = i = ftnunit->url;
00211 if (ftnunit->url > ftnunit->f77fio_size)
00212 check_buflen (ftnunit, ftnunit->url);
00213 while (i-- > 0)
00214 ftnunit->f77fio_buf[i] = '\0';
00215 if (ftnunit->f77idxlist.cikeyid >= 0 && ftnunit->ukeyid != ftnunit->f77idxlist.cikeyid) {
00216 ftnunit->ukeyid = ftnunit->f77idxlist.cikeyid;
00217 newkeyid = 1;
00218 }
00219
00220 else if (ftnunit->ukeyid < 0) {
00221 ftnunit->ukeyid = ftnunit->f77idxlist.cikeyid >= 0 ? ftnunit->f77idxlist.cikeyid : 0;
00222 newkeyid = 1;
00223 }
00224 if (mode) {
00225 field = ftnunit->f77fio_buf + KEYOFF (ftnunit->ukeyid);
00226 if (KEYTYPE (ftnunit->ukeyid) != ftnunit->f77idxlist.cikeytype)
00227 err (ftnunit->f77errlist.cierr, 154, "indexed read");
00228 if (KEYTYPE (ftnunit->ukeyid) == CHARTYPE) {
00229 keyval = ftnunit->f77idxlist.cikeyval.cicharval;
00230
00231 if ((i = ftnunit->f77idxlist.cikeyvallen) > KEYLEN (ftnunit->ukeyid))
00232 err (ftnunit->f77errlist.cierr, 155, "indexed read");
00233
00234
00235
00236
00237
00238
00239
00240 while (i-- > 0)
00241 *field++ = *keyval++;
00242 } else
00243 stlong (ftnunit->f77idxlist.cikeyval.ciintval, field);
00244 } else
00245
00246
00247
00248
00249 mode = (newkeyid ? ISFIRST : ftnunit->uerror == F_ERLOCKED ? ISCURR : ISNEXT);
00250 ftnunit->uerror = 0;
00251 if (newkeyid) {
00252 dokey (ftnunit->ukeyid, ONEKEY);
00253 if (isstart (ftnunit->isfd, &onekey, ftnunit->url, ftnunit->f77fio_buf, mode)
00254 < SUCCESS)
00255 ierr (ftnunit->f77errlist.cierr, iserrno, "indexed read");
00256 }
00257 if (isread (ftnunit->isfd, ftnunit->f77fio_buf, mode) < SUCCESS)
00258 if (iserrno == EENDFILE) {
00259 ftnunit->uend = 1;
00260 err (ftnunit->f77errlist.ciend, EOF, "indexed read");
00261 } else
00262 ierr (ftnunit->f77errlist.cierr, iserrno, "indexed read");
00263
00264 ftnunit->uend = 0;
00265 return SUCCESS;
00266 }
00267
00268 void
00269 s_idxwrite (unit *ftnunit)
00270 {
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 check_buflen (ftnunit, ftnunit->url);
00281 if (ftnunit->ufmt)
00282 while (ftnunit->f77recpos++ < ftnunit->url)
00283 ftnunit->f77fio_buf[ftnunit->f77recpos] = ' ';
00284 else
00285 while (ftnunit->f77reclen++ < ftnunit->url)
00286 ftnunit->f77fio_buf[ftnunit->f77reclen] = '\0';
00287 ftnunit->f77fio_buf[ftnunit->url] = '\0';
00288 }
00289
00290 int
00291 idxwrite (unit *ftnunit)
00292 {
00293 s_idxwrite (ftnunit);
00294 if (iswrite (ftnunit->isfd, ftnunit->f77fio_buf) < SUCCESS)
00295 ierr (ftnunit->f77errlist.cierr, iserrno, "indexed write");
00296 return SUCCESS;
00297 }
00298
00299 int
00300 idxrewrite (unit *ftnunit)
00301 {
00302 s_idxwrite (ftnunit);
00303 if (isrewcurr (ftnunit->isfd, ftnunit->f77fio_buf) < SUCCESS)
00304 ierr (ftnunit->f77errlist.cierr, iserrno, "indexed rewrite");
00305 return 0;
00306 }
00307
00308 int
00309 dokey(int keyid, int mode)
00310 {
00311 switch (mode) {
00312 case NOMATCH:
00313 return KEYOFF (keyid) != onekey.k_start ||
00314 KEYLEN (keyid) != onekey.k_leng ||
00315 KEYTYPE (keyid) != onekey.k_type;
00316
00317 case ONEKEY:
00318 onekey.k_start = KEYOFF (keyid);
00319 onekey.k_leng = (short) (KEYLEN (keyid));
00320 onekey.k_type = KEYTYPE (keyid);
00321 break;
00322
00323 case UNITKEY:
00324 KEYOFF (keyid) = onekey.k_start;
00325 KEYEND (keyid) = (short) (onekey.k_start + onekey.k_leng - 1);
00326 KEYTYPE (keyid) = onekey.k_type;
00327 break;
00328 }
00329 return 0;
00330 }
00331
00332 #if 00
00333 static int f_del_com (alist64 *a, int lock);
00334 static int f_unl_com (alist64 *a, int lock);
00335 #else
00336 static int f_del_com (alist *a, int lock);
00337 static int f_unl_com (alist *a, int lock);
00338 #endif
00339
00340 int
00341 f_del (alist *a)
00342 {
00343 #if 00
00344 alist64 a64;
00345 a64.aerr = a->aerr;
00346 a64.aunit = a->aunit;
00347 return( f_del_com( &a64, 0 ) );
00348 #else
00349 return( f_del_com( a, 0 ) );
00350 #endif
00351 }
00352
00353 int
00354 f_del_mp (alist *a)
00355 {
00356 #if 00
00357 alist64 a64;
00358 a64.aerr = a->aerr;
00359 a64.aunit = a->aunit;
00360 return( f_del_com( &a64, 1 ) );
00361 #else
00362 return( f_del_com( a, 1 ) );
00363 #endif
00364 }
00365
00366 #if 00
00367 int
00368 f_del64 (alist64 *a)
00369 {
00370 return( f_del_com( a, 0 ) );
00371 }
00372
00373 int
00374 f_del64_mp (alist64 *a)
00375 {
00376 return( f_del_com( a, 1 ) );
00377 }
00378
00379 static int
00380 f_del_com (alist64 *a, int lock)
00381 #else
00382 static int
00383 f_del_com (alist *a, int lock)
00384 #endif
00385 {
00386 unit *ftnunit = find_luno(a->aunit);
00387
00388 if (ftnunit == NULL)
00389 err (a->aerr, 101, "delete");
00390 while (lock && test_and_set( &ftnunit->lock_unit, 1L ))
00391 ;
00392 if (ftnunit->uconn > 0) {
00393 if (ftnunit->uacc != KEYED)
00394 errret(a->aerr, 163, "delete");
00395 if (isdelcurr (ftnunit->isfd) < SUCCESS)
00396 ierrret(a->aerr, iserrno, "delete");
00397 }
00398 else
00399 err (a->aerr, 101, "delete");
00400 if (lock) ftnunit->lock_unit = 0;
00401 return 0;
00402 }
00403
00404 int
00405 f_unl (alist *a)
00406 {
00407 #if 00
00408 alist64 a64;
00409 a64.aerr = a->aerr;
00410 a64.aunit = a->aunit;
00411 return( f_unl_com( &a64, 0 ) );
00412 #else
00413 return( f_unl_com( a, 0 ) );
00414 #endif
00415 }
00416
00417 int
00418 f_unl_mp (alist *a)
00419 {
00420 #if 00
00421 alist64 a64;
00422 a64.aerr = a->aerr;
00423 a64.aunit = a->aunit;
00424 return( f_unl_com( &a64, 1 ) );
00425 #else
00426 return( f_unl_com( a, 1 ) );
00427 #endif
00428 }
00429
00430 #if 00
00431 int
00432 f_unl64 (alist64 *a)
00433 {
00434 return( f_unl_com( a, 0 ) );
00435 }
00436
00437 int
00438 f_unl64_mp (alist64 *a)
00439 {
00440 return( f_unl_com( a, 1 ) );
00441 }
00442
00443 static int
00444 f_unl_com (alist64 *a, int lock)
00445 #else
00446 static int
00447 f_unl_com (alist *a, int lock)
00448 #endif
00449 {
00450 unit *ftnunit = find_luno(a->aunit);
00451
00452 if (ftnunit == NULL)
00453 err (a->aerr, 101, "unlock");
00454 while (lock && test_and_set( &ftnunit->lock_unit, 1L ))
00455 ;
00456 if (ftnunit->uconn > 0) {
00457 if (ftnunit->uacc != KEYED)
00458 errret(a->aerr, 163, "unlock");
00459 if (isrelease (ftnunit->isfd) < SUCCESS)
00460 ierrret(a->aerr, iserrno, "unlock");
00461 }
00462 else
00463 err (a->aerr, 101, "delete");
00464 if (lock) ftnunit->lock_unit = 0;
00465 return 0;
00466 }
00467
00468 int
00469 s_xsue (cilist *a)
00470 {
00471 return( s_xsue_mp( a, &f77curunit ) );
00472 }
00473
00474
00475 #if 11
00476 int
00477 s_xsue64 (cilist64 *a)
00478 {
00479 return( s_xsue64_mp( a, &f77curunit ) );
00480 }
00481
00482 int
00483 s_xsue_mp (cilist *a, unit **fu)
00484 {
00485 cilist64 a64;
00486 get_cilist64( &a64, a );
00487 return( s_xsue64_mp( &a64, fu ) );
00488 }
00489
00490 int
00491 s_xsue64_mp (cilist64 *a, unit **fu)
00492 #else
00493 int
00494 s_xsue_mp (cilist *a, unit **fu)
00495 #endif
00496 {
00497 unit *ftnunit;
00498 (void) wsue(a, fu);
00499 ftnunit = *fu;
00500
00501 ftnunit->f77idxlist.cimatch = a->cimatch;
00502 ftnunit->f77idxlist.cikeytype = a->cikeytype;
00503 ftnunit->f77idxlist.cikeyval.cicharval = a->cikeyval.cicharval;
00504 ftnunit->f77idxlist.cikeyid = a->cikeyid;
00505 ftnunit->f77idxlist.cinml = a->cinml;
00506 ftnunit->f77idxlist.cikeyvallen = a->cikeyvallen;
00507 if (ftnunit->uacc != KEYED)
00508 err (a->cierr, 162, "rewrite");
00509 return 0;
00510 }
00511
00512 #pragma weak e_xsue64 = e_xsue
00513
00514 int
00515 e_xsue (void)
00516 {
00517 return( e_xsue_mp( &f77curunit) );
00518 }
00519
00520 #pragma weak e_xsue64_mp = e_xsue_mp
00521
00522 int
00523 e_xsue_mp (unit **fu)
00524 {
00525
00526 int n = idxrewrite(*fu);
00527
00528 (*fu)->lock_unit = 0;
00529 return (n == 1 ? 0 : n);
00530 }
00531
00532 int
00533 s_xsle (cilist *a)
00534 {
00535 return( s_xsle_mp( a, &f77curunit ) );
00536 }
00537
00538 #if 11
00539 int
00540 s_xsle64 (cilist64 *a)
00541 {
00542 return( s_xsle64_mp( a, &f77curunit ) );
00543 }
00544
00545 int
00546 s_xsle_mp (cilist *a, unit **fu)
00547 {
00548 cilist64 a64;
00549 get_cilist64( &a64, a );
00550 return( s_xsle64_mp( &a64, fu ) );
00551 }
00552
00553 int
00554 s_xsle64_mp (cilist64 *a, unit **fu)
00555 {
00556 int n = s_wsle64_mp(a, fu);
00557 #else
00558 int
00559 s_xsle_mp (cilist *a, unit **fu)
00560 {
00561 int n = s_wsle_mp(a, fu);
00562 #endif
00563 unit *ftnunit = *fu;
00564
00565 if (n) {
00566 return n;
00567 }
00568 ftnunit->f77idxlist.cimatch = a->cimatch;
00569 ftnunit->f77idxlist.cikeytype = a->cikeytype;
00570 ftnunit->f77idxlist.cikeyval.cicharval = a->cikeyval.cicharval;
00571 ftnunit->f77idxlist.cikeyid = a->cikeyid;
00572 ftnunit->f77idxlist.cinml = a->cinml;
00573 ftnunit->f77idxlist.cikeyvallen = a->cikeyvallen;
00574 if (ftnunit->uacc != KEYED)
00575 errret(a->cierr, 162, "rewrite");
00576 ftnunit->dowrite = idxrewrite;
00577 return (0);
00578 }
00579
00580 int
00581 s_xsfe (cilist *a)
00582 {
00583 return( s_xsfe_mp( a, &f77curunit ) );
00584 }
00585
00586
00587 #if 11
00588 int
00589 s_xsfe64 (cilist64 *a)
00590 {
00591 return( s_xsfe64_mp( a, &f77curunit ) );
00592 }
00593
00594 int
00595 s_xsfe_mp (cilist *a, unit **fu)
00596 {
00597 cilist64 a64;
00598 get_cilist64( &a64, a );
00599 return( s_xsfe64_mp( &a64, fu ) );
00600 }
00601
00602 int
00603 s_xsfe64_mp (cilist64 *a, unit **fu)
00604 #else
00605 int
00606 s_xsfe_mp (cilist *a, unit **fu)
00607 #endif
00608 {
00609 int n = wsfe(a, fu, 0);
00610 unit *ftnunit;
00611
00612 ftnunit = *fu;
00613 ftnunit->f77idxlist.cimatch = a->cimatch;
00614 ftnunit->f77idxlist.cikeytype = a->cikeytype;
00615 ftnunit->f77idxlist.cikeyval.cicharval = a->cikeyval.cicharval;
00616 ftnunit->f77idxlist.cikeyid = a->cikeyid;
00617 ftnunit->f77idxlist.cinml = a->cinml;
00618 ftnunit->f77idxlist.cikeyvallen = a->cikeyvallen;
00619 if (n) {
00620 return n;
00621 }
00622 if (ftnunit->uacc != KEYED)
00623 errret(a->cierr, 162, "rewrite");
00624 ftnunit->dowrite = idxrewrite;
00625 return (0);
00626 }
00627
00628 #if 11
00629 #pragma weak f_del64 = f_del
00630 #pragma weak f_del64_mp = f_del_mp
00631 #pragma weak f_unl64 = f_unl
00632 #pragma weak f_unl64_mp = f_unl_mp
00633 #endif