root/tags/5.4.pre05/retag.c

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

Criação do svn para Cisis.

Line 
1/* Program RETAG: retag field tags, in place
2                  or mfunlock
3*/
4#include <stdio.h>
5#include <string.h>
6#include "cisis.h"   /* CISIS Interface header file */
7#include "cirun.h"   /* CISIS Interface runtime declarations */
8#define TRACEX  0
9#define TRACEY  0
10#define TRACEW  01
11#define MAXNVF (MAXMFRL/sizeof(DIRSTRU))
12int tag1[MAXNVF];
13int tag2[MAXNVF];
14int ntags=0;
15void main(argc,argv)
16int argc;
17char *argv[];
18{
19    RECSTRU *recp;      /* mandatory for defines REC,MFR,DIR,FIELDP */
20    LONGX irec,mfn;
21    int dirloop,loop;
22    DIRSTRU *dirp;
23    char *dbnp;
24    int rc;
25    FILE *fp;
26    int changed,tag;
27    int iarg;
28    char *p;
29    LONGX parmtell=0;
30    int unlock=0,wlen=2;                                /* leader + dir */
31    LONGX count=0,count2=0;
32    LONGX parmfrom=1,parmto=999999999L; 
33    int parmshift=0;
34
35    if (argc < 3) {
36        printf("%s",cicopyr("Utility RETAG"));
37        printf(" \n");
38        printf("retag <dbname> {<retag.tab>|unlock} [<option> [...]] \n");
39        printf(" \n");
40        printf(" <dbname>    master file to be retagged/unlocked \n");
41        printf(" <retag.tab> retag table \n");
42        printf(" \n");
43        printf("options:     {from|to|tell|shift}=<n> \n");
44        printf(" \n");
45        printf("The retag table is a sequential file in the format: \n");
46        printf(" \n");
47        printf("   <tag> <new tag>   (max %d entries)\n",MAXNVF);
48        printf(" \n");
49        printf("The table: \n");
50        printf(" \n");
51        printf(" 24 240 \n");
52        printf(" 70 700 \n");
53        printf(" \n");
54        printf("changes tag 24 to 240 and 70 to 700 \n");
55        printf(" \n");
56        exit(1);
57    }
58    dbnp=argv[1];
59    if (strcmp(argv[2],"unlock") != 0) {
60        if ((fp=fopen(dbxcipar(NULL,argv[2],'='),"r")) == NULL) fatal(argv[2]);
61        for ( ; ; ) {
62        if ((rc=fscanf(fp,"%d %d\n",&tag1[ntags],&tag2[ntags])) != 2)
63            if (rc == EOF) break;
64            else fatal(argv[2]);
65#if TRACEX
66printf("+++ tag1=%3d  tag2=%3d\n",tag1[ntags],tag2[ntags]);
67#endif
68            ntags++;
69        }
70        fclose(fp);
71    }
72    else unlock=1;
73   
74    for (iarg=3; iarg < argc; iarg++) {
75        p=argv[iarg];
76        if (strncmp(p,"from?",5) == 0 || strncmp(p,"from=",5) == 0) {
77            if (sscanf(p+5,"%ld",&parmfrom) != 1)
78                fatal(p);
79            continue;
80        }
81        if (strncmp(p,"to?",3) == 0 || strncmp(p,"to=",3) == 0) {
82            if (sscanf(p+3,"%ld",&parmto) != 1)
83                fatal(p);
84            continue;
85        }
86        if (strncmp(p,"tell?",5) == 0 || strncmp(p,"tell=",5) == 0) {
87            if (sscanf(p+5,"%ld",&parmtell) != 1)
88                fatal(p);
89            continue;
90        }
91        if (strncmp(p,"shift?",6) == 0 || strncmp(p,"shift=",6) == 0) {
92            if (sscanf(p+6,"%d",&parmshift) != 1)
93                fatal(p);
94            continue;
95        }
96        if (strcmp(p,"trace") == 0) {
97            dbxtrace=rectrace=1;
98            continue;
99        }
100        fatal(p);
101    }
102
103    if (parmshift)
104        for (ntags=0; ntags < MAXNVF; ntags++) {
105            tag1[ntags] = ntags+1;
106            tag2[ntags] = ntags+1 + parmshift;
107            if (tag2[ntags] < 0) tag2[ntags]=0;
108        }
109   
110    RECORD(irec=nrecs,dbnp,0L); /* RECdbxp and mstsetup() */
111
112    dbxopenw("",RDBname,mx1extp,&RDBmsopn,&RDBmsopw,"msopn/w"); /* recwmast() */
113
114    if (unlock) {
115        if (MF0mfcxx2+MF0mfcxx3) {
116            printf("unlock: mfcxx2=%ld mfcxx3=%ld\n",MF0mfcxx2,MF0mfcxx3);
117            MF0mfcxx2=MF0mfcxx3=0;
118            recwmast(recp,NULL,0L,0,0,sizeof(M0STRU));  /* ctl */
119            count++;
120        }
121        wlen=1;                                         /* leader */
122    }
123   
124    for (loop=0; loop < ntags; loop++)
125        if (tag2[loop] == 0) { wlen=0; break; }
126
127    for (mfn=parmfrom; mfn <= parmto; mfn++) {
128        recreadl=wlen; recread(recp,mfn);               /* leader/+dir */
129        if (RECrc == RCEOF) break;
130        if (RECrc == RCPDEL) continue;
131        for (changed=0, loop=0; loop < ntags; loop++) {
132            tag=tag1[loop];
133            for (dirp=MFRdir, dirloop=MFRnvf; dirloop--; dirp++) {
134                if (dirp->tag == tag) {
135                  if (tag2[loop]) {
136                    dirp->tag = tag2[loop];
137#if TRACEY
138printf("+++ now %3d -> %3d\n",tag,dirp->tag);
139#endif
140                  } else {
141                    char lineup[10];
142                    sprintf(lineup,"D%ld",(LONGX)dirp->tag);
143                    if (fldupdat(irec,lineup)) fatal(lineup);
144                    if (dirloop) { dirloop--; dirp--; }
145#if TRACEY
146printf("+++ del %3d -> %3d\n",tag,0);
147#endif
148                  }
149                  changed++;
150                }
151            }
152        }
153        if (unlock) {
154            if (RECgdbw) {
155                printf("unlock: mfn=%ld mfrl=%ld\n",MFRmfn,(LONGX)MFRmfrl);
156#if !MULTI
157                MFRmfrl=(UWORD)0-MFRmfrl;
158#endif
159                changed=1;
160            }
161        }
162        if (changed) {
163            recwmast(NULL,recp,recxrefb,recxrefp,0,wlen); /* leader/+dir */
164            count2++;
165        }
166        if (parmtell)
167            if (mfn % parmtell == 0)
168                fprintf(stderr,"+++ %ld/%ld [%ld+%ld] \n",
169                    mfn,RDBmsmfn-1,count,count2);
170    }
171    if (parmtell)
172        if ((mfn-1) % parmtell != 0)
173            fprintf(stderr,"+++ %ld/%ld [%ld+%ld] \n",
174                mfn-1,RDBmsmfn-1,count,count2);
175    exit(0);
176
177} /* end of main */
Note: See TracBrowser for help on using the browser.