root/trunk/mdlup.c

Revision 389, 9.5 kB (checked in by heitor.barbieri, 3 weeks ago)

essage first commit

Line 
1/* ----------------------------------------------------------------------
2   mdlup.c    -   NLM Medlars II - Master file update
3----------------------------------------------------------------------- */
4
5#include <stdio.h>
6#include <string.h>
7
8#include "cisis.h"       /* CISIS Interface */
9#include "cirun.h"       /* runtime area and defines by AOT */
10
11int argnext = 1;
12
13LONGX parmfrom=1,parmto=0,parmloop=1;
14LONGX parmcount=999999999L, parmtell=0;
15
16
17#define NLMTAGUI        350     /* A020                      */
18#define NLMTAGCU        102     /* A900                      */
19
20#define NLMTAG18         18     /* UI                        */
21#define NLMTAG56         56     /* ebcdic DD or ebcdic RR    */
22
23#define EBCDIC_D        0xc4    /* Upper D */
24#define EBCDIC_R        0xd9    /* Upper R */
25
26
27unsigned char dbname[CIMPL+1],dbnupd[CIMPL+1],dbnout[CIMPL+1],dbnlog[CIMPL+1];
28unsigned char *dbnamep,*dbnupdp,*dbnoutp,*dbnlogp;
29unsigned char buffupd[BUFSIZ],buffup[MAXMFRL],*batchp;
30LONGX irec,urec,outcrec,logcrec,logirec;
31LONGX utrm;
32
33LONGX count = 0;
34LONGX countok = 0;
35LONGX countdd = 0;
36LONGX countrr = 0;
37LONGX countcc = 0;
38
39void main(argc,argv)
40int argc;
41char *argv[];
42{
43    RECSTRU *recp;
44    TRMSTRU *trmp;
45    LONGX mfn,data1,data2,umfn,umfn0;
46    int iarg,xdir,rc;
47    unsigned char c,*p,*q;
48    unsigned char ui[8+1],uiasc[sizeof(ui)];
49    unsigned char cu[8+1],cuasc[sizeof(cu)];
50
51    argnext=1;
52    if (argc < 5) {
53        printf("%s",cicopyr("Utility MDL/MDLUP"));
54        printf("\n");
55        printf("mdlup <dbname> <dbnupd> [create=]<dbnout> <dbnlog> [options]' \n");
56        printf("\n");
57        printf("options: {from/to/loop/count/tell}=<n> \n");
58        printf("\n");
59        exit(1);
60    }
61
62    if (argc > argnext)
63        dbnamep=argv[argnext++];
64    else {
65        printf("input data base:  "); dbnamep=gets(dbname);
66    }
67    if (argc > argnext)
68        dbnupdp=argv[argnext++];
69    else {
70        printf("update data base: "); dbnupdp=gets(dbnupd);
71    }
72    if (argc > argnext)
73        dbnoutp=argv[argnext++];
74    else {
75        printf("output data base: "); dbnoutp=gets(dbnout);
76    }
77    if (argc > argnext)
78        dbnlogp=argv[argnext++];
79    else {
80        printf("log data base:    "); dbnlogp=gets(dbnlog);
81    }
82
83
84    for (iarg=argnext; iarg<argc; iarg++) {
85        p=argv[iarg];
86        if (strncmp(p,"from=",5) == 0) {
87            if (sscanf(p+5,"%"_LD_,&parmfrom) != 1)
88                fatal(p);
89            continue;
90        }
91        if (strncmp(p,"to=",3) == 0) {
92            if (sscanf(p+3,"%"_LD_,&parmto) != 1)
93                fatal(p);
94            continue;
95        }
96        if (strncmp(p,"loop=",5) == 0) {
97            if (sscanf(p+5,"%"_LD_,&parmloop) != 1)
98                fatal(p);
99            continue;
100        }
101
102        if (!strncmp(p,"count=",6)) {
103            if ((sscanf(p+6,"%"_LD_,&parmcount)) != 1)
104                fatal(p);
105            continue;
106        }
107        if (!strncmp(p,"tell=",5)) {
108            if ((sscanf(p+5,"%"_LD_,&parmtell)) != 1)
109                fatal(p);
110            continue;
111        }
112
113        fatal(p);
114    }
115
116
117    /*
118        setup bases de dados
119    */
120
121    /* entrada */
122    RECORD((irec=nrecs),dbnamep,0L);
123    prtcontrol(vrecp[irec],RDBname);
124    if (!parmto)
125        parmto=MF0nxtmfn-1;
126
127    /* entrada update */
128    RECORD((urec=nrecs),dbnupdp,0L);
129    prtcontrol(vrecp[urec],RDBname);
130    TERM((utrm=ntrms),dbnupdp,"!");
131
132    /* saida */
133    if (strncmp(dbnoutp,"create=",7) == 0) {
134        printf("*%s\n",dbnoutp);
135        recisis0((dbnoutp+=7));
136    }
137    RECORD((outcrec=nrecs),dbnoutp,1L);         /* recupdat()/xropn */
138    RECORD(outcrec,dbnoutp,0L);
139    prtcontrol(vrecp[outcrec],RDBname);
140
141    /* log */
142    RECORD((logcrec=nrecs),dbnlogp,0L);
143    prtcontrol(vrecp[logcrec],RDBname);
144    RECORD((logirec=nrecs),dbnlogp,1L);         /* recupdat()/xropn */
145
146    /*
147        loop principal
148    */
149    for (mfn=parmfrom; mfn<=parmto; mfn+=parmloop) {
150
151        RECORD(irec,dbnamep,mfn);
152
153        if (RECrc != RCNORMAL)
154            continue;
155
156        xdir=fieldx(irec,NLMTAGUI,1);
157        if (xdir < 0 || DIRlen(xdir) >= sizeof(ui)) {
158            printf("*** mfn=%"_LD_"  tag %d \n",mfn,NLMTAGUI);
159            fatal("mdlup/UI");
160        }
161        memcpy(ui,FIELDP(xdir),DIRlen(xdir)); ui[DIRlen(xdir)]='\0';
162        for (p=ui, q=uiasc; *p; p++, q++) {
163            c= *p; c&=0x0F; /* digit */
164            c+='0'; *q=c;
165        }
166        *q='\0';
167        TERM(utrm,dbnupdp,ui);
168        if (TRMrc != RCNORMAL) {
169/*
170            printf("*** mfn=%"_LD_"  UI='%s'\n",mfn,uiasc);
171            fatal("mdlup/TRMrc");
172*/
173                MFRmfn=VMF0nxtmfn(outcrec);     /* append */
174                rc=recupdat(outcrec,irec);
175                if (rc) {
176                    printf("*** recupdat=%d\n",rc);
177                    fatal("mdlup/recupdat/out");
178                }
179                countcc++;
180        }
181        if (TRMrc == RCNORMAL) {
182            if (TRMxytotp == 0) {
183                printf("*** mfn=%"_LD_"  UI='%s'\n",mfn,uiasc);
184                fatal("mdlup/posting");
185            }
186            batchp=buffup;
187            sprintf(batchp,"A1#%s# A2#%s^m%"_LD_"#",uiasc,dbnamep,mfn);
188            batchp+=strlen(batchp);
189            umfn=umfn0=posting(utrm,1L);
190            for (data1= -1L; ; ) {
191                RECORD(urec,dbnupdp,TRMpmfn);
192                if (RECrc != RCNORMAL) {
193                    printf("*** mfn=%"_LD_"/%"_LD_"='%s'\n",MFRmfn,mfn,uiasc);
194                    fatal("mdlup/record");
195                }
196                xdir=fieldx(urec,NLMTAGCU,1);
197                if (xdir >= 0) {
198                    if (DIRlen(xdir) >= sizeof(cu)) {
199                        printf("*** mfn=%"_LD_"  tag %d \n",mfn,NLMTAGCU);
200                         fatal("mdlup/CU");
201                    }
202                    memcpy(cu,FIELDP(xdir),DIRlen(xdir)); cu[DIRlen(xdir)]='\0';
203                    for (data2=0L, p=cu, q=cuasc; *p; p++, q++) {
204                        c= *p;
205                        if ((c & 0xF0) != 0xF0) {
206                            printf("*** mfn=%"_LD_"  UI='%s'\n",mfn,uiasc);
207                            fatal("mdlup/invalid CU");
208                        }
209                        c&=0x0F; /* digit */
210                        data2 = 10*data2 + c;
211                        c+='0'; *q=c;
212                    }
213                    *q='\0';
214                    sprintf(batchp,"A3#%"_LD_"^d%"_LD_"#",TRMpmfn,data2);
215                    batchp+=strlen(batchp);
216                    if (data2 >= data1) {
217                        if (TRMpost > 1L)
218                            printf("*** mfn=%"_LD_"  UI='%s'  %"_LD_"/%"_LD_" -> %"_LD_"/%s\n",
219                                mfn,uiasc,umfn,data1,MFRmfn,cuasc);
220                        data1=data2;
221                        umfn=TRMpmfn;
222                    }
223                }
224                else {
225                    if (TRMpost > 1L)
226                        printf("*** mfn=%"_LD_"  UI='%s'  %"_LD_"/%"_LD_" -> %"_LD_"\n",
227                                mfn,uiasc,umfn,data1,MFRmfn);
228                    sprintf(batchp,"A3#%"_LD_"#",TRMpmfn);
229                    batchp+=strlen(batchp);
230                    umfn=TRMpmfn;
231                }
232                if (posting(utrm,TRMpost+1) <= 0L)
233                    break;
234            }
235            if (umfn != umfn0)
236                RECORD(urec,dbnupdp,umfn);
237
238            xdir=fieldx(urec,NLMTAG56,1);
239            if (xdir < 0) {
240                printf("*** mfn=%"_LD_"/%"_LD_"='%s'\n",MFRmfn,mfn,uiasc);
241                fatal("mdlup/56 missing");
242            }
243            p=FIELDP(xdir);
244            if (DIRlen(xdir) != 2 ||
245                (*p != EBCDIC_D || *(p+1) != EBCDIC_D) &&
246                (*p != EBCDIC_R || *(p+1) != EBCDIC_R)   ) {
247                    printf("*** mfn=%"_LD_"/%"_LD_"='%s'\n",MFRmfn,mfn,uiasc);
248                    fatal("mdlup/56 invalid");
249            }
250
251            if (*p == EBCDIC_R) {               /* REPLACE */
252                sprintf(buffupd,"D%dD%d",NLMTAG18,NLMTAG56);
253                p=fldupdat(urec,buffupd);
254                if (p) {
255                    printf("*** mfn=%"_LD_"  fldupdat='%s'\n",mfn,p);
256                    fatal("mdlup/fldupdat/out");
257                }
258                MFRmfn=VMF0nxtmfn(outcrec);     /* append */
259                rc=recupdat(outcrec,urec);
260                if (rc) {
261                    printf("*** recupdat=%d\n",rc);
262                    fatal("mdlup/recupdat/out");
263                }
264                sprintf(batchp,"A4#%s^m%"_LD_"# A5#%s^m%"_LD_"#",
265                    dbnupdp,umfn,dbnoutp,VMFRmfn(urec));
266                batchp+=strlen(batchp);
267                countrr++;
268            }
269            else {
270                sprintf(batchp,"A4#%s^m%"_LD_"#",
271                    dbnupdp,umfn);
272                batchp+=strlen(batchp);
273                countdd++;
274            }
275            RECORD(logirec,dbnlogp,VMF0nxtmfn(logcrec));
276            MFRstatus=ACTIVE;
277            if (fldupdat(logirec,buffup)) fatal("mdlup/fldupdat/log");
278            rc=recupdat(logcrec,logirec);
279            if (rc) {
280                printf("*** recupdat=%d\n",rc);
281                fatal("mdlup/recupdat/log");
282            }
283        }
284        else countok++;
285
286        count++;
287        if (parmtell)
288            if (!(count % parmtell))
289              fprintf(stdout,
290                    "+++ %"_LD_" recs - ok=%"_LD_" dd=%"_LD_" rr=%"_LD_" cc=%"_LD_"\n",
291                        count,countok,countdd,countrr,countcc);
292
293        if (count >= parmcount)
294            break;
295    }
296
297    fprintf(stdout,
298        "+++ %"_LD_" recs - ok=%"_LD_" dd=%"_LD_" rr=%"_LD_" cc=%"_LD_"\n",
299            count,countok,countdd,countrr,countcc);
300
301    exit(0);
302}
Note: See TracBrowser for help on using the browser.