root/tags/5.4.pre05/mxcp.c

Revision 1, 16.8 kB (checked in by heitor.barbieri, 4 years ago)

Criação do svn para Cisis.

Line 
1/* ------------------------------ mxcp.c ----------------------------- */
2
3#include <stdio.h>
4#include <string.h>
5#include <ctype.h>
6
7#if 1
8#include "cisis.h"   /* CISIS Interface header file */
9#include "cirun.h"   /* CISIS Interface runtime declarations */
10#else
11#include "\cisis\include\cisis.h"   /* CISIS Interface header file */
12#include "\cisis\include\cirun.h"   /* CISIS Interface runtime declarations */
13#endif
14
15#define MAXTAG  9999
16
17#define PERIOD  '.'
18#define REPEAT  '%'
19
20/* Global */
21UBYTE line[CIMPL+1],*buffup;
22char *dbnamep,*dbnoutp,*batchp;
23LONGX needup=0L;
24LONGX totalup=0L;
25UWORD mintag = 1;
26UWORD maxtag = MAXTAG;
27int parmtrace = 0;
28
29LONGX count1=0;
30LONGX count2=0;
31
32FILE *lfp = NULL; /* stdout */
33FILE *xfp = NULL;
34
35UBYTE *vperiod = NULL;
36UBYTE *vrepeat = NULL;
37int tagperiod = 0;                                              /* 17/03/92 */
38int tagrepeat = 0;                                              /* 17/03/92 */
39
40UBYTE spacechr = ' ';                                           /* 17/03/92 */
41
42#define MAXARGC 128
43char *inargv[MAXARGC+1];                                        /* 25/05/98 */
44char *inargvp=NULL;
45int inargc=0;
46
47/* Prototypes */
48#if ANSI
49LONGX clean(LONGX irec);
50LONGX store(LONGX mfn, UWORD tag, int xocc, UWORD len, char *fldp);
51#else
52LONGX clean();
53LONGX store();
54#endif
55
56
57void main(argc,argv)
58int argc;
59char *argv[];
60{
61    RECSTRU *recp;      /* mandatory for defines REC RDB MF0 MFR DIR, FIELDP */
62
63    LONGX irec;          /* current master file record */
64    LONGX upcrec;        /* output control record */
65#if RECGIZM
66    LONGX xrec;          /* gizmread() work record */
67#endif
68
69    int argnext,parmclean,parmundel;
70    LONGX parmfrom,parmto,parmloop,parmcount;
71    LONGX parmoffset,parmtell;
72
73    LONGX mfn;
74    UBYTE *p,*q,period,repeat;
75    UWORD mftype,tag,tag1,tag2,create=0,n;
76    LONGX xtag,xtag1,xtag2;                                      /* 17/03/92 */
77#if RECGIZM
78    VGIZPSTRU *gizmap,*vgizmap = NULL;
79#endif
80
81
82    /* processa in= como primeiro parm */
83   
84    if (argc > 1 && strncmp(p=argv[1],"in=",3) == 0) {
85        inargc=1; inargv[inargc-1]=argv[0];
86        inargvp=loadfile(NULL,'\0',p+3,NULL,0L,'\n');
87        if (!inargvp) fatal(p);
88        for (inargv[inargc]=p=inargvp; *p; p++) {
89            if (*p != '\n') continue;
90            *p='\0';
91            inargc++; inargv[inargc]=p+1;
92        }
93        inargv[inargc]=NULL;
94    }
95    else {
96        for (inargc=0; inargc < argc; inargc++)
97            inargv[inargc]=argv[inargc];
98        inargv[inargc]=NULL;
99    }
100
101
102    if (inargc < 3) {
103        printf("%s",cicopyr("Utility MXCP"));
104        printf("\n");
105        printf("mxcp {in=<file>|<dbin>} [create=]<dbout> [<option> [...]] \n");
106        printf("\n");
107        printf("options: {from|to|loop|count|tell|offset}=<n> \n");
108#if RECGIZM
109        printf("         gizmo=<dbgiz>[,tag_list>] \n");
110#endif
111        printf("         undelete \n");
112        printf("         clean [mintag=%ld] ",(LONGX)mintag);
113        printf("[maxtag=%ld] \n",(LONGX)maxtag);
114        printf("         period=%c[,<tag_list>] \n",PERIOD);
115        printf("         repeat=%c[,<tag_list>] \n",REPEAT);
116        printf("         log=<filename> \n",(LONGX)maxtag);
117        printf("\n");
118        printf("\n");
119        printf(" Ex: mxcp in create=out clean period=.,3 repeat=;,7 \n");
120        printf(" \n");
121        printf("      in = 3 %c   Field 3 occ 1. %c\n",OPENFCHR,CLOSFCHR);
122        printf("           3 %cField 3 occ 2 . %c\n",OPENFCHR,CLOSFCHR);
123        printf("           7 %c%s%c\n",OPENFCHR,
124                                " Field 7/1;Field 7/2 ;Field 7/3.",
125                                CLOSFCHR);
126        printf(" \n");
127        printf("     out = 3 %cField 3 occ 1%c\n",OPENFCHR,CLOSFCHR);
128        printf("           3 %cField 3 occ 2%c\n",OPENFCHR,CLOSFCHR);
129        printf("           7 %cField 7/1%c\n",OPENFCHR,CLOSFCHR);
130        printf("           7 %cField 7/2%c\n",OPENFCHR,CLOSFCHR);
131        printf("           7 %cField 7/3.%c\n",OPENFCHR,CLOSFCHR);
132        printf("\n");
133        exit(1);
134    }
135
136    argnext=1;
137   
138    dbnamep=inargv[argnext++];
139    dbnoutp=inargv[argnext++];
140
141    parmclean=0; parmundel=0;
142    parmfrom=1; parmto=0; parmloop=1; parmcount=LONGX_MAX;
143    parmoffset=0; parmtell=0;
144
145#if RECGIZM
146    xrec=nrecs++;
147#endif
148
149    /*
150        get optional parameters
151    */
152    for ( ; argnext < inargc; argnext++) {
153        p=inargv[argnext];
154
155        if (strncmp(p,"from=",5) == 0) {
156            if (sscanf(p+5,"%ld",&parmfrom) != 1)
157                fatal(p);
158            continue;
159        }
160        if (strncmp(p,"to=",3) == 0) {
161            if (sscanf(p+3,"%ld",&parmto) != 1)
162                fatal(p);
163            continue;
164        }
165        if (strncmp(p,"loop=",5) == 0) {
166            if (sscanf(p+5,"%ld",&parmloop) != 1)
167                fatal(p);
168            continue;
169        }
170        if (strncmp(p,"count=",6) == 0) {
171            if (sscanf(p+6,"%ld",&parmcount) != 1)
172                fatal(p);
173            continue;
174        }
175        if (strncmp(p,"tell=",5) == 0) {
176            if (sscanf(p+5,"%ld",&parmtell) != 1)
177                fatal(p);
178            continue;
179        }
180        if (strncmp(p,"offset=",7) == 0) {
181            if (sscanf(p+7,"%ld",&parmoffset) != 1)
182                fatal(p);
183            continue;
184        }
185
186        if (strncmp(p,"mintag=",7) == 0) {
187            if (sscanf(p+7,"%ld",&tag) != 1) fatal(p);
188            if (tag < 1 || tag > maxtag) fatal(p);
189            mintag=tag;
190            continue;
191        }
192        if (strncmp(p,"maxtag=",7) == 0) {
193            if (sscanf(p+7,"%ld",&tag) != 1) fatal(p);
194            if (tag < mintag || tag > /* ALLOMAXV*/ USHRT_MAX) fatal(p);  /* AOT, 05/04/2001 */
195            if (vperiod || vrepeat) fatal(p);
196            maxtag=tag;
197            continue;
198        }
199
200        if (strncmp(p,"log=",4) == 0) {
201            if ((lfp=fopen(p+4,"w")) == NULL)
202                fatal(p);
203            continue;
204        }
205        if (strncmp(p,"xct=",4) == 0) {
206            if ((xfp=fopen(p+4,"w")) == NULL)
207                fatal(p);
208            continue;
209        }
210
211        if (strncmp(p,"period=",7) == 0) {
212            q=p+7; if (!*q) fatal(p);
213            period = *q++;
214            if (*q == '=' || *q == ',') q++;
215            if (!vperiod) {
216                vperiod=(UBYTE *)ALLOC((ALLOPARM)(maxtag+1));
217                if (vperiod == ALLONULL)
218                    fatal("Not enough memory for period");
219                memset(vperiod,0x00,(unsigned)(maxtag+1));
220            }
221            else fatal(p);
222            while (*q) {
223                if (tagperiod) fatal(p);
224                tagperiod=1;
225                if (sscanf(q,"%ld/%ld",&xtag1,&xtag2) == 2) {
226                    tag1=(UWORD)xtag1; tag2=(UWORD)xtag2;
227                    if (tag1 < 1 || tag2 > maxtag) fatal(p);
228                    if (tag1 > tag2) fatal(p);
229                    while (*q != '/') q++; q++;
230                    while (isdigit(*q)) q++;
231                    for (tag=tag1; tag <= tag2; tag++) {
232                        if (vperiod[tag]) fatal(p);
233                        vperiod[tag]=period;
234                    }
235                    if (*q == ',') if (*(q+1)) q++;
236                    continue;
237                }
238                if (sscanf(q,"%ld",&xtag) == 1) {
239                    tag=(UWORD)xtag;
240                    if (tag > maxtag) fatal(p);
241                    while (isdigit(*q)) q++;
242                    if (vperiod[tag]) fatal(p);
243                    vperiod[tag]=period;
244                    if (*q == ',') if (*(q+1)) q++;
245                    continue;
246                }
247                fatal(p);
248            }
249            if (!tagperiod)
250                memset(vperiod,period,(unsigned)(maxtag+1));
251            if (parmtrace)
252                for (tag=0; tag <= maxtag; tag++)
253                    if (vperiod)
254                        if (vperiod[tag])
255                            printf("+++ period[%d]=%c\n",tag,vperiod[tag]);
256            continue;
257        }
258
259        if (strncmp(p,"repeat=",6) == 0) {
260            q=p+7; if (!*q) fatal(p);
261            repeat = *q++;
262            if (*q == '=' || *q == ',') q++;
263            if (!vrepeat) {
264                vrepeat=(UBYTE *)ALLOC((ALLOPARM)(maxtag+1));
265                if (vrepeat == ALLONULL)
266                    fatal("Not enough memory for repeat");
267                memset(vrepeat,0x00,(unsigned)(maxtag+1));
268            }
269            else fatal(p);
270            while (*q) {
271                if (tagrepeat) fatal(p);
272                tagrepeat=1;
273                if (sscanf(q,"%ld/%ld",&xtag1,&xtag2) == 2) {
274                    tag1=(UWORD)xtag1; tag2=(UWORD)xtag2;
275                    if (tag1 < 1 || tag2 > maxtag) fatal(p);
276                    if (tag1 > tag2) fatal(p);
277                    while (*q != '/') q++; q++;
278                    while (isdigit(*q)) q++;
279                    for (tag=tag1; tag <= tag2; tag++) {
280                        if (vrepeat[tag]) fatal(p);
281                        vrepeat[tag]=repeat;
282                    }
283                    if (*q == ',') if (*(q+1)) q++;
284                    continue;
285                }
286                if (sscanf(q,"%ld",&xtag) == 1) {
287                    tag=(UWORD)xtag;
288                    if (tag > maxtag) fatal(p);
289                    while (isdigit(*q)) q++;
290                    if (vrepeat[tag]) fatal(p);
291                    vrepeat[tag]=repeat;
292                    if (*q == ',') if (*(q+1)) q++;
293                    continue;
294                }
295                fatal(p);
296            }
297            if (!tagrepeat)
298                memset(vrepeat,repeat,(unsigned)(maxtag+1));
299            if (parmtrace)
300                for (tag=0; tag <= maxtag; tag++)
301                    if (vrepeat)
302                        if (vrepeat[tag])
303                            printf("+++ repeat[%d]=%c\n",tag,vrepeat[tag]);
304            continue;
305        }
306
307#if RECGIZM
308        if (strncmp(p,"gizmo=",6) == 0) {
309            q=p+6;
310
311            if (!*q) fatal(p);
312
313            if (vgizmap) {      /* gizmo already defined */
314                for (gizmap=vgizmap; gizmap->nextp; )
315                    gizmap=gizmap->nextp;
316                gizmread(q,&gizmap->nextp,xrec);
317                gizmap=gizmap->nextp;
318            }
319            else {              /* first gizmo definition */
320                gizmread(q,&vgizmap,xrec);
321            }
322
323            if (parmtrace) {
324                for (gizmap=vgizmap; gizmap; gizmap=gizmap->nextp)
325                    printf("+++ gizmo=%s \n",gizmap->gdbnp);
326            }
327            continue;
328        }
329#endif
330
331        if (strcmp(p,"clean") == 0) {
332            parmclean=1;
333            continue;
334        }
335
336        if (strcmp(p,"undelete") == 0) {
337            parmundel=1;
338            continue;
339        }
340
341        if (strcmp(p,"trace") == 0) {
342            parmtrace=1;
343            printf("+++ input data base:  %s\n",dbnamep);
344            printf("+++ output data base: %s\n",dbnoutp);
345            continue;
346        }
347
348        fatal(p);
349    }
350
351    if (!lfp) lfp=stdout;
352
353    /*
354        setup input data base
355    */
356    RECORD((irec=nrecs),dbnamep,0L);
357    mftype=MF0mftype;
358#if RECGIZM
359    RDBvgzrp=vgizmap;                                           /* 17/03/92 */
360#endif
361    if (parmtrace && !parmtell) {
362        parmtell=(MF0nxtmfn-1)/10;
363        if (parmtell < 1) parmtell=1;
364    }
365
366
367    /*
368        setup output master file
369    */
370    if (strncmp(dbnoutp,"create=",7) == 0) {
371        if (strcmp(RDBname,dbnoutp+7) == 0) fatal(dbnoutp);
372        recisis0((dbnoutp+=7));
373        create=1;
374    }
375
376    /*
377        open output and load its output control record
378    */
379    RECORD((upcrec=nrecs),dbnoutp,1L);
380    RECORD(upcrec,dbnoutp,0L);
381    if (create) MF0mftype=mftype;
382
383
384    /*
385        set spacechr to 0x00 [clean() is to be called for period/repeat]
386    */
387    if (!parmclean) if (vperiod || vrepeat) parmclean=1;
388
389    /*
390        allocate buffup
391    */
392    if (parmclean) {
393        buffup=(UBYTE *)ALLOC((ALLOPARM)(BUFFUPL));
394        if (buffup == ALLONULL)
395            fatal("Not enough memory for buffup");
396    }
397
398
399    /*
400        main loop
401    */
402    for (mfn=parmfrom; ; mfn+=parmloop) {
403
404        if (parmtell)
405            if (!(count1 % parmtell))
406                if (count1)
407                    fprintf(stderr,"+++ %ld/%ld recs\n",count1,count2);
408
409        if (parmto)
410            if (mfn > parmto)
411                break;
412
413        if (parmcount)
414            if (count2 >= parmcount)
415                break;
416
417        RECORD(irec,dbnamep,mfn);
418
419        if (RECrc == RCEOF)
420            break;
421        count1++;
422
423        if (RECrc != RCNORMAL)
424            if (parmundel)
425                if (RECrc == RCLDEL)
426                    MFRstatus=ACTIVE;
427
428        if (MFRstatus == DELETED) continue;
429
430        /*
431            cleaning procedure
432        */
433        if (parmclean) totalup+=clean(irec);
434
435        /*
436            output the current master file record
437        */
438        n=1;
439        if (needup == 0) if (strcmp(RDBname,dbnoutp) == 0) n=0;
440        if (create) n=1;
441        if (!n) if (parmundel) if (RECrc == RCLDEL) n=1;
442        if (n)
443            if (MFRnvf > 0) {
444                MFRmfn=mfn+parmoffset;
445                MFRstatus=ACTIVE; /* good */
446                if (recupdat(upcrec,irec))
447                    fatal("recupdat");
448
449                count2++;
450            }
451            else
452                fprintf(lfp,"*** mfn %ld -> rejected record\n",mfn);
453
454    } /* end of main loop */
455
456    if (parmtrace)
457        if (!(count1 % parmtell))
458            ;
459        else
460            fprintf(stderr,"+++ %ld/%ld recs\n",count1,count2);
461
462    if (xfp) fprintf(xfp,"%ld|%ld|%ld\n",count1,count2,totalup);
463   
464    exit(0);
465}
466
467
468LONGX clean(irec)
469LONGX irec;
470{
471    RECSTRU *recp;
472    UBYTE *p,*q,repeat;
473    int dirindex,dirsleft;
474    LONGX chrsleft;
475    UWORD tag,pos,len;
476    int xocc,xdir;
477
478    needup=0;
479    recp=vrecp[(unsigned)irec];
480
481    /*
482        set up the update buffer
483    */
484    batchp=buffup;
485    for (dirindex=0, dirsleft=MFRnvf; dirsleft--; dirindex++) {
486
487        for (xocc=0, xdir=0; xdir <= dirindex; xdir++)
488            if (DIRtag(xdir) == DIRtag(dirindex)) xocc++;
489
490        if ((tag=DIRtag(dirindex)) < 1 || tag > maxtag || tag < mintag) {
491            fprintf(lfp,"*** mfn %ld tag=%u/%d -> rejected tag\n",
492                MFRmfn,tag,xocc);
493            needup++;
494            continue;
495        }
496        if ((len=DIRlen(dirindex)) < 1) {
497            fprintf(lfp,"*** mfn %ld tag=%u/%d len=%u -> rejected length\n",
498                MFRmfn,tag,xocc,len);
499            needup++;
500            continue;
501        }
502        if ((pos=DIRpos(dirindex)) < 0 || pos+len+MFRbase > MFRmfrl) {
503            fprintf(lfp,"*** mfn %ld tag=%u/%d pos=%u -> rejected position\n",
504                MFRmfn,tag,xocc,pos);
505            needup++;
506            continue;
507        }
508
509        p=FIELDP(dirindex);
510        chrsleft=len;
511
512        repeat='\0';
513        if (vrepeat)
514            if (!tagrepeat)
515                repeat=vrepeat[0];
516            else
517                if (tag <= maxtag)
518                    repeat=vrepeat[tag];
519
520        if (repeat)
521            for (q=p, len=0; chrsleft--; ) {
522                if (*q++ == repeat) {
523                    store(MFRmfn,tag,xocc,len,p);
524                    p=q; len=0;
525                    needup++;
526                }
527                else
528                    len++;
529            }
530
531        if (len > 0)
532            store(MFRmfn,tag,xocc,len,p);
533
534    } /* end of directory entries */
535
536    *batchp='\0';
537
538    /*
539        update the current master file record
540    */
541    if (needup) {
542        if (parmtrace) printf("buffup='%s'\n",buffup);
543        if (fldupdat(irec,"D*")) fatal(fldupdat(irec,"D*"));
544        if (fldupdat(irec,buffup)) fatal(fldupdat(irec,buffup));
545    }
546
547    return(needup);
548}
549
550
551LONGX store(mfn, tag, xocc, len, fldp)
552LONGX mfn;
553UWORD tag;
554int xocc;
555UWORD len;
556char *fldp;
557{
558    UBYTE *p,*q,period,chrhex[4];
559    LONGX chrsleft;
560
561    p=fldp;
562    q=p+len-1;
563
564    chrsleft=len;
565
566    while (chrsleft && *p <= spacechr) {
567        sprintf(chrhex, isprint(*p) ? "%c  " : "%03d", *p);
568        fprintf(lfp,"*** mfn %ld tag=%u/%d %s -> rejected char\n",
569            mfn,tag,xocc,chrhex);
570        p++; chrsleft--;
571        needup++;
572    }
573
574    period='\0';
575    if (vperiod)
576        if (!tagperiod)
577            period=vperiod[0];
578        else
579            if (tag <= maxtag)
580                period=vperiod[tag];
581
582    while (chrsleft > 0 && (*q <= spacechr || *q == period)) {
583        sprintf(chrhex, isprint(*q) ? "%c  " : "%03d", *q);
584        fprintf(lfp,"*** mfn %ld tag=%u/%d %s -> rejected char\n",
585            mfn,tag,xocc,chrhex);
586        q--; chrsleft--;
587        needup++;
588    }
589
590    if (chrsleft < 1) {
591        fprintf(lfp,"*** mfn %ld tag=%u/%d len=%u/%ld -> rejected length\n",
592            mfn,tag,xocc,len,chrsleft);
593        needup++;
594        return(0);
595    }
596
597    sprintf(batchp,"H%u %ld ",tag,chrsleft);
598    batchp+=strlen(batchp);
599
600    for (; chrsleft--; )
601        if (*p < spacechr) {
602            *batchp++ = spacechr;
603            fprintf(lfp,"*** mfn %ld tag=%u/%d %03d -> blanked\n",
604                mfn,tag,xocc,*p);
605            p++;
606            needup++;
607        }
608        else
609            *batchp++ = *p++;
610
611    *batchp = '\0';
612
613    return(chrsleft);
614}
Note: See TracBrowser for help on using the browser.