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 #pragma ident "@(#) libf/fio/wnly.c 92.1 06/21/99 10:37:55"
00039
00040
00041
00042
00043
00044 #include <stdio.h>
00045 #include <errno.h>
00046 #include <fortran.h>
00047 #include <memory.h>
00048 #include <malloc.h>
00049 #include <liberrno.h>
00050 #include <stdlib.h>
00051 #include <cray/fmtconv.h>
00052 #include "fio.h"
00053 #include "fmt.h"
00054 #include "lio.h"
00055 #include "rnl.h"
00056
00057 extern void _memwcpy (long *_S1, long *_S2, int _N);
00058
00059
00060
00061
00062
00063
00064
00065 #define YMP80 (_dreal8 == DREAL8_YMP80)
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 struct BUFFERS {
00078 long *outbuff;
00079 long *outptr;
00080 int outcnt;
00081 long *f_lbuf;
00082 long *f_lbufptr;
00083 int f_lbufcnt;
00084 int lcomma;
00085 };
00086
00087 static char *char_rep(char *_P, int _Cn, unsigned int _Ln, int *_Lc,
00088 struct BUFFERS *_Bp);
00089
00090 static long *find_rep(long *_P, int _Cn, int _In, int _Ty, int *_Lc,
00091 struct BUFFERS *_Bp);
00092
00093 static int l_write(FIOSPTR css, unit *cup, void *dptr, unsigned elsize,
00094 int count, int inc, int type, long recsize, int errf,
00095 struct BUFFERS *bptr);
00096
00097 static int lw_A(FIOSPTR css, char *_P, int _Cl, long _Rc, unit *_Cu,
00098 int _Er, struct BUFFERS *_Bp);
00099
00100 static void writ_rep(long repcnt, struct BUFFERS *buffers);
00101
00102
00103
00104
00105
00106 #define NLPUT(x) { \
00107 *(bptr->outptr)++ = (long) x; \
00108 bptr->outcnt--; \
00109 }
00110
00111 #define NLPUTS(string) { \
00112 s = string; \
00113 while (c = *s++) { \
00114 NLPUT(c); \
00115 } \
00116 }
00117
00118
00119
00120
00121
00122 #define LPUT(x) { \
00123 (*(bptr->f_lbufptr)++ = (long) x); \
00124 bptr->f_lbufcnt++; \
00125 }
00126
00127 #define LPUTS(string) { \
00128 s = string; \
00129 while (c = *s++) { \
00130 LPUT(c); \
00131 } \
00132 }
00133
00134
00135
00136
00137
00138 #define NLINE() { \
00139 bptr->lcomma = 0; \
00140 if (OUT_LINE) { \
00141 REPFLUSH(); \
00142 } \
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 #define REPFLUSH() { \
00152 if (_fwch(cup, bptr->outbuff, recsize - bptr->outcnt, 1) < 0)\
00153 RERR(css, errno); \
00154 bptr->outptr = bptr->outbuff;\
00155 *bptr->outptr++ = (long) ' '; \
00156 *bptr->outptr++ = (long) ' '; \
00157 bptr->outcnt = recsize - 2; \
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 int
00175 @WNL(
00176 _f_int *unump,
00177 Namelist *nl,
00178 int errf
00179 )
00180 {
00181 unum_t unum;
00182 int errn;
00183 int n, ss;
00184 void *vaddr;
00185 unsigned elsize;
00186 long recsize;
00187
00188 char c;
00189 char *s;
00190 unit *cup;
00191 Nlentry *nlent;
00192 FIOSPTR css;
00193 struct BUFFERS wnlbuffers;
00194 struct BUFFERS *bptr;
00195 bptr = &wnlbuffers;
00196 bptr->f_lbuf = NULL;
00197
00198 unum = *unump;
00199
00200 GET_FIOS_PTR(css);
00201 STMT_BEGIN(unum, 0, T_WNL, NULL, css, cup);
00202
00203 if (cup == NULL) {
00204 cup = _imp_open77(css, SEQ, FMT, unum, errf, &errn);
00205
00206
00207
00208
00209 if (cup == NULL)
00210 RERR(css, errn);
00211 }
00212
00213
00214
00215 cup->uflag = (errf != 0 ? _UERRF : 0);
00216 cup->ulineptr = cup->ulinebuf;
00217 cup->uwrt = 1;
00218
00219
00220
00221 css->u.fmt.nonl = 0;
00222
00223
00224 if (cup->useq == 0)
00225 RERR(css, FESEQTIV);
00226
00227 if (!cup->ufmt)
00228 RERR(css, FEFMTTIV);
00229
00230 if ((cup->uaction & OS_WRITE) == 0)
00231 RERR(css, FENOWRIT);
00232
00233 bptr = &wnlbuffers;
00234 bptr->lcomma = 0;
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 recsize = cup->uldwsize;
00249
00250 if (cup->urecl == 0 && _wnlrecsiz > 0)
00251 recsize = MIN(cup->urecsize, _wnlrecsiz);
00252
00253 bptr->outcnt = recsize - 1;
00254 bptr->outbuff = cup->ulinebuf;
00255 bptr->outptr = bptr->outbuff;
00256 *bptr->outptr++ = OUT_ECHO;
00257 bptr->f_lbuf = (long *) malloc((recsize + 1) * sizeof(long));
00258
00259 if (bptr->f_lbuf == NULL)
00260 RERR(css, FENOMEMY);
00261
00262
00263
00264 NLPUT(OUT_CHAR);
00265 NLPUTS(nl->nlname);
00266 NLPUT(' ');
00267 NLPUT(' ');
00268 NLINE();
00269
00270 nlent = nl->nlvnames;
00271
00272 do {
00273 int ntype;
00274
00275 ntype = _old_namelist_to_f77_type_cnvt[nlent->na.type];
00276
00277
00278
00279
00280
00281
00282
00283
00284 bptr->f_lbufptr = bptr->f_lbuf;
00285 bptr->f_lbufcnt = 0;
00286
00287 LPUTS(nlent->varname);
00288 LPUT(' ');
00289 LPUT(OUT_EQ);
00290
00291
00292 n = (nlent->na.offdim) ? nlent->na.nels : 1;
00293
00294 if (ntype == DT_CHAR) {
00295 _fcd f;
00296 f = *(_fcd *)(((unsigned long) nlent->va.varaddr +
00297 (long *)nl));
00298 vaddr = _fcdtocp(f);
00299 elsize = _fcdlen(f);
00300 }
00301 else {
00302 vaddr = (void *)nlent->va.varaddr;
00303 elsize = 0;
00304 }
00305
00306 LPUT(' ');
00307
00308
00309
00310 ss = l_write(css, cup, vaddr, elsize, n, 1, ntype, recsize,
00311 errf, bptr);
00312
00313 if (ss != 0) {
00314 RERR(css, ss);
00315 }
00316
00317 NLINE();
00318
00319 nlent++;
00320
00321 } while (nlent->varname[0]);
00322
00323 if (bptr->outcnt < 6) {
00324 REPFLUSH();
00325 bptr->outptr--;
00326 bptr->outcnt++;
00327 }
00328
00329 NLPUT(OUT_CHAR);
00330 NLPUTS("END");
00331 REPFLUSH();
00332 ret:
00333
00334 STMT_END(cup, T_WNL, NULL, css);
00335
00336 if (bptr->f_lbuf != NULL)
00337 free(bptr->f_lbuf);
00338
00339 return(CFT77_RETVAL(ss));
00340 }
00341
00342
00343
00344
00345
00346 static int
00347 l_write(
00348 FIOSPTR css,
00349 unit *cup,
00350 void *dptr,
00351 unsigned elsize,
00352 int count,
00353 int inc,
00354 int type,
00355 long recsize,
00356 int errf,
00357 struct BUFFERS *bptr
00358 )
00359 {
00360 unsigned int len77;
00361 char *cp;
00362 long *ptr;
00363 long ugly[ITEMBUFSIZ];
00364 long dig;
00365 long exp;
00366 long mod;
00367 long scl;
00368 long ss;
00369 long wid;
00370 long *ib_ptr;
00371 long *newp;
00372 int lcount;
00373 oc_func *gcf;
00374 ftype_t f90type;
00375
00376 if (type == DT_CHAR) {
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 cp = dptr;
00387 len77 = elsize;
00388
00389 for (; count > 0; count-- ) {
00390
00391 bptr->lcomma = 0;
00392
00393 if (count > 1) {
00394
00395
00396
00397
00398 cp = char_rep(cp, count, len77, &lcount,
00399 bptr);
00400 count = count - (lcount - 1);
00401 }
00402
00403
00404
00405 ss = lw_A(css, cp, len77, recsize, cup, errf, bptr);
00406
00407 if (ss != 0) {
00408 RERR(css, ss);
00409 }
00410
00411 cp = cp + len77;
00412 }
00413
00414 return(0);
00415
00416 }
00417
00418
00419
00420 ptr = (long *)dptr;
00421 f90type = _f77_to_f90_type_cnvt[type];
00422
00423 if ((type == DT_DBLE) || (type == DT_CMPLX))
00424 inc = inc + inc;
00425
00426 for (; count > 0; count--, ptr += inc) {
00427
00428 if (count > 1) {
00429
00430 ptr = find_rep(ptr, count, inc, type, &lcount,
00431 bptr);
00432
00433 count = count - (lcount - 1);
00434 }
00435
00436 ib_ptr = bptr->f_lbufptr;
00437
00438 switch (type) {
00439
00440 case DT_NONE:
00441 gcf = _s2uo; mod = MODEUN; wid = WOCTWRD;
00442 dig = WOCTWRD; exp = 0; scl = 0;
00443 break;
00444
00445 case DT_SINT:
00446 case DT_INT:
00447 gcf = _s2ui; mod = 0; wid = WINT;
00448 dig = 1; exp = 0; scl = 0;
00449 break;
00450
00451 case DT_REAL:
00452 case DT_CMPLX:
00453 gcf = _sd2uge; mod = 0; wid = WREAL8;
00454 dig = _dreal8; exp = DEXP8; scl = 1;
00455 if (YMP80) dig = 9;
00456 break;
00457
00458 case DT_DBLE:
00459
00460
00461
00462
00463
00464
00465 gcf = _sd2udee; mod = MODEDP; wid = WREAL16;
00466 dig = _dreal16-1; exp = DEXP16; scl = 1;
00467 if (YMP80) dig = 25;
00468 break;
00469 }
00470
00471
00472
00473
00474
00475 switch (type) {
00476
00477 default:
00478
00479 #if _F_REAL16 == 1
00480 if (YMP80 && !cup->uft90 && type == DT_DBLE &&
00481 *(_f_dble *)ptr == 0.0) {
00482
00483 static const char *zero_dp = "0.0E+00";
00484 ib_ptr += _unpack(zero_dp, ib_ptr,
00485 strlen(zero_dp), -1);
00486 break;
00487 }
00488 #endif
00489
00490 newp = gcf(ptr, ugly, &mod, &wid, &dig, &exp, &scl);
00491
00492 if (type == DT_NONE)
00493 *newp++ = 'B';
00494
00495 ib_ptr = ib_ptr + _wnl_beautify(f90type, ugly, newp,
00496 ib_ptr, cup->uft90);
00497 break;
00498
00499 case DT_CMPLX:
00500
00501 *ib_ptr++ = '(';
00502
00503 newp = gcf(ptr, ugly, &mod, &wid, &dig, &exp, &scl);
00504
00505 ib_ptr = ib_ptr + _wnl_beautify(f90type, ugly, newp,
00506 ib_ptr, cup->uft90);
00507
00508 *ib_ptr++ = COMMA;
00509
00510 newp = gcf((_f_real *)ptr + 1, ugly,
00511 &mod, &wid, &dig, &exp, &scl);
00512
00513 ib_ptr = ib_ptr + _wnl_beautify(f90type, ugly, newp,
00514 ib_ptr, cup->uft90);
00515
00516 *ib_ptr++ = ')';
00517
00518 break;
00519
00520 case DT_LOG:
00521 *ib_ptr++ = _lvtob(*(_f_log8 *)ptr)? 'T':'F';
00522 break;
00523 }
00524
00525
00526
00527
00528 bptr->f_lbufcnt += ib_ptr - bptr->f_lbufptr;
00529 bptr->f_lbufptr = ib_ptr;
00530
00531 LPUT(OUT_SEP);
00532 LPUT(' ');
00533 LPUT(' ');
00534
00535 if (bptr->outcnt <= bptr->f_lbufcnt) {
00536
00537
00538
00539
00540
00541
00542 REPFLUSH();
00543 }
00544
00545 bptr->f_lbufptr = bptr->f_lbuf;
00546
00547 _memwcpy(bptr->outptr, bptr->f_lbufptr, bptr->f_lbufcnt);
00548
00549 bptr->outptr += bptr->f_lbufcnt;
00550 bptr->outcnt -= bptr->f_lbufcnt;
00551 bptr->f_lbufptr = bptr->f_lbuf;
00552 bptr->f_lbufcnt = 0;
00553 }
00554
00555 return(0);
00556
00557 ret:
00558 return(ss);
00559 }
00560
00561
00562
00563
00564
00565
00566
00567 static long *
00568 find_rep(
00569 long *ptr,
00570 int count,
00571 int inc,
00572 int type,
00573 int *lcount,
00574 struct BUFFERS *bptr
00575 )
00576 {
00577 int i;
00578 long *p1, *p2, *q1, *q2;
00579
00580 p1 = ptr;
00581 q1 = ptr + inc;
00582
00583 if (type == DT_CMPLX || type == DT_DBLE) {
00584
00585 p2 = p1 + 1;
00586 q2 = q1 + 1;
00587
00588 for (i = 1; i < count; i++) {
00589
00590 if ((*p1 != *q1) || (*p2 != *q2)) {
00591 break;
00592 }
00593 else {
00594 p1 = q1;
00595 p2 = p1 + 1;
00596 q1 = q1 + inc;
00597 q2 = q1 + 1;
00598 }
00599 }
00600 }
00601 else {
00602 for (i = 1; i < count; i++) {
00603
00604 if (*p1 != *q1) {
00605 break;
00606 }
00607 else {
00608 p1 = q1;
00609 q1 = q1 + inc;
00610 }
00611 }
00612 }
00613
00614 *lcount = (long) i;
00615
00616 if (i > 1)
00617 writ_rep(i, bptr);
00618
00619 return(p1);
00620 }
00621
00622 static void
00623 writ_rep(
00624 long repcnt,
00625 struct BUFFERS *bptr
00626 )
00627 {
00628 long mode;
00629 long wid;
00630 long dig;
00631 long zero = 0;
00632 long *newp;
00633 long *q;
00634 long buf[WINT];
00635
00636 mode = 0;
00637 wid = WINT;
00638 dig = 0;
00639
00640 newp = _s2ui((long*)&repcnt, buf, &mode, &wid, &dig, &zero, &zero);
00641
00642 for (q = buf; q < newp; q++)
00643 if ((char)*q != ' ')
00644 break;
00645
00646 while (q < newp) {
00647 *bptr->f_lbufptr++ = *q++;
00648 bptr->f_lbufcnt++;
00649 }
00650
00651 *bptr->f_lbufptr++ = (long) '*';
00652 bptr->f_lbufcnt++;
00653 }
00654
00655
00656
00657
00658
00659
00660
00661 static char *
00662 char_rep(
00663 char *ptr,
00664 int count,
00665 unsigned int len77,
00666 int *lcount,
00667 struct BUFFERS *bptr
00668 )
00669 {
00670 int i;
00671 char *qptr;
00672
00673 qptr = ptr + len77;
00674
00675 for (i = 1; i < count; i++) {
00676
00677 if (memcmp(ptr, qptr, len77))
00678 break;
00679
00680 qptr = qptr + len77;
00681 }
00682
00683 *lcount = (long)i;
00684
00685 if (i > 1)
00686 writ_rep(i, bptr);
00687
00688 return(ptr + (*lcount - 1) * len77);
00689 }
00690
00691
00692
00693
00694
00695 static int
00696 lw_A(
00697 FIOSPTR css,
00698 char *ptr,
00699 int charlen,
00700 long recsize,
00701 unit *cup,
00702 int errf,
00703 struct BUFFERS *bptr
00704 )
00705 {
00706 int m;
00707 char *aposptr;
00708 int ss;
00709 int fflag;
00710 int recmax;
00711
00712
00713
00714
00715
00716
00717
00718 fflag = 0;
00719 *bptr->f_lbufptr++ = (long) '\'';
00720 bptr->f_lbufcnt++;
00721
00722 for (; charlen > 0; ) {
00723
00724 if (fflag == 0) {
00725 recmax = recsize - 2;
00726 m = MIN(charlen, recmax - bptr->f_lbufcnt);
00727 }
00728 else {
00729 recmax = recsize - 1;
00730 m = MIN(charlen, recmax - bptr->f_lbufcnt);
00731 }
00732
00733
00734
00735 aposptr = memchr(ptr, '\'', m);
00736
00737 if (aposptr != 0) {
00738
00739 m = aposptr + 1 - ptr;
00740
00741
00742 (void) _unpack(ptr, bptr->f_lbufptr, m, -1);
00743
00744 *(bptr->f_lbufptr + m) = '\'';
00745 ptr = ptr + m;
00746 charlen = charlen - m;
00747 m++;
00748 }
00749 else {
00750
00751
00752 (void) _unpack(ptr, bptr->f_lbufptr, m, -1);
00753
00754 ptr = ptr + m;
00755 charlen = charlen - m;
00756 }
00757
00758 bptr->f_lbufptr += m;
00759 bptr->f_lbufcnt += m;
00760
00761
00762
00763
00764
00765 if (bptr->f_lbufcnt >= recmax) {
00766 if (bptr->outcnt <= bptr->f_lbufcnt) {
00767 REPFLUSH();
00768
00769
00770
00771 if (fflag == 1) {
00772 bptr->outptr--;
00773 bptr->outcnt++;
00774 }
00775 fflag = 1;
00776 }
00777 bptr->f_lbufptr = bptr->f_lbuf;
00778
00779 _memwcpy(bptr->outptr, bptr->f_lbufptr,
00780 bptr->f_lbufcnt);
00781
00782 bptr->outptr += bptr->f_lbufcnt;
00783 bptr->outcnt -= bptr->f_lbufcnt;
00784 bptr->f_lbufptr = bptr->f_lbuf;
00785 bptr->f_lbufcnt = 0;
00786 }
00787 }
00788
00789 *bptr->f_lbufptr++ = (long) '\'';
00790 bptr->f_lbufcnt++;
00791
00792 LPUT(OUT_SEP);
00793 LPUT(' ');
00794 LPUT(' ');
00795
00796 bptr->lcomma = 1;
00797
00798 if (bptr->outcnt <= bptr->f_lbufcnt) {
00799
00800
00801
00802
00803 REPFLUSH();
00804
00805
00806 if (fflag == 1) {
00807 bptr->outptr--;
00808 bptr->outcnt++;
00809 }
00810 }
00811
00812 bptr->f_lbufptr = bptr->f_lbuf;
00813
00814 _memwcpy(bptr->outptr, bptr->f_lbufptr, bptr->f_lbufcnt);
00815
00816 bptr->outptr += bptr->f_lbufcnt;
00817 bptr->outcnt -= bptr->f_lbufcnt;
00818 bptr->f_lbufptr = bptr->f_lbuf;
00819 bptr->f_lbufcnt = 0;
00820
00821 return(0);
00822
00823 ret:
00824 return(ss);
00825 }
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843 int
00844 _wnl_beautify(
00845 ftype_t typ90,
00846 long *ugly,
00847 long *p_limit,
00848 long *pretty,
00849 unsigned isf90)
00850
00851 {
00852 int ret;
00853
00854 ret = _beautify(typ90, ugly, p_limit, pretty, isf90);
00855
00856
00857
00858
00859
00860 if (YMP80 && !isf90 && typ90 == DVTYPE_REAL || typ90 == DVTYPE_COMPLEX){
00861 if (pretty[ret - 1] == '.')
00862 pretty[ret++] = '0';
00863 }
00864
00865 return (ret);
00866 }