root/trunk/a1.c

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

essage first commit

Line 
1/* Program A1 - Search and MF display according an in-stream format
2                specification, with fatal error trapping
3*/
4#include <stdio.h>
5#include <string.h>
6
7#include "cisis.h"   /* CISIS Interface header file */
8#include "cirun.h"   /* CISIS Interface runtime declarations */
9
10
11#define TRAP_FATAL 1
12
13/* program parameters */
14char *dbnp;
15char *qryp;
16
17/* b4_run() options */
18char *savedbnp=NULL;            /* b4_xxx() MF to save queries/hits */
19LONGX trec=0L;                  /* 1-rec MF:    a101#^p*^yIF_DEFAULT#
20                                                a101#^pPFX1 ^yIF1#
21                                                a101#^pPFX2 ^yIF1#
22                                                ...
23                                                .....
24                                                a101#^pPFXlast ^yIFlast# */
25LONGX nbytes2=4096L;            /* b4_run() nbytes2 parameter */
26LONGX msglevel=1L;              /* b4_run() display operations & main Hits= */
27LONGX bhsize=MAXMFRL;            /* b4_xxx() record size for expr/pcode/ctrl */
28
29/* recfmt() */
30char *fmtp=NULL;               
31char fmtarea[MAXMFRL];          /* format spec&results */
32LONGX parmlw=79L;               /* line width */
33
34void main(argc,argv)
35int argc;
36char *argv[];
37{
38    RECSTRU *recp;              /* current vrecp pointer */
39    LONGX ridx;                 /* current vrecp index */
40    LONGX mfn;                  /* current mfn */
41
42    LONGX nbytes1;              /* b4_run() nbytes1 parameter */
43    LONGX boolidx;              /* b4_xxx() record */
44    char *boolp;                /* b4_xxx() return string */
45    int boolerr;                /* b4_xxx() return code */
46    LONGX nord;                 /* b4_hit() hit number */
47#if TRAP_FATAL
48    int rc;                     /* setjmp rc */
49#endif
50
51    char *prompt=NULL;          /* printf+getchar after each record */
52    LONGX parmfrom=1,parmto=LONGX_MAX,parmcount=LONGX_MAX;
53    LONGX count;
54    int argnext;
55    char *p,*q;
56
57
58    if (argc < (argnext=3)) {
59        printf("%s",cicopyr("Utility A1"));
60        printf("\n");
61        printf("a1 <dbn> {<query>|@<file>} [<option> [...]] \n");
62        printf("\n");
63        printf("options: {pfx|save}=<dbn> \n");
64        printf("         bhsize=<n>          default=%"_LD_"\n",bhsize);
65        printf("         nomsg|allmsg \n");
66        printf("         {from|to|count}=<n> \n");
67        printf("         pft={<fmt>|@[<file>]} \n");
68        printf("         lw=<line width> \n");
69        printf("         prompt={<text>|<file>} \n");
70        printf("\n");
71        exit(1);
72    }
73
74
75    /* get default values */
76    dbnp=argv[1]; 
77
78    qryp=loadfile(dbnp,'@',argv[2],NULL,0L,'\n');
79    if (!qryp) fatal(argv[2]);
80
81
82    /*
83        get optional parameters
84    */
85    for ( ; argnext < argc; argnext++) {
86
87        p=argv[argnext];
88
89        if (strncmp(p,"fsiz=",5) == 0) {
90            if (sscanf(p+5,"%"_LD_,&fmt_fsiz) != 1)
91                fatal(p);
92            continue;
93        }
94        if (strncmp(p,"from=",5) == 0) {
95            if (sscanf(p+5,"%"_LD_,&parmfrom) != 1)
96                fatal(p);
97            continue;
98        }
99        if (strncmp(p,"to=",3) == 0) {
100            if (sscanf(p+3,"%"_LD_,&parmto) != 1)
101                fatal(p);
102            continue;
103        }
104        if (strncmp(p,"count=",6) == 0) {
105            if (sscanf(p+6,"%"_LD_,&parmcount) != 1)
106                fatal(p);
107            continue;
108        }
109
110        if (strncmp(p,"prompt=",7) == 0) {
111            q=p+7;
112            prompt=loadfile(NULL,'@',q,prompt,0L,'\n');
113            if (!prompt) fatal(q);
114            continue;
115        }
116
117        if (strncmp(p,"pft=",4) == 0) {
118            q=p+4;
119            if (strcmp(q,"@") == 0) sprintf(q=fmtarea,"@%s.pft",dbnp);
120            fmtp=loadfile(dbnp,'@',q,fmtp,0L,'\0');
121            if (!fmtp) fatal(q);
122            if (!*fmtp) fatal(p);
123            continue;
124        }
125
126        if (strncmp(p,"lw=",3) == 0) {
127            if (sscanf(p+3,"%"_LD_,&parmlw) != 1)
128                fatal(p);
129            continue;
130        }
131
132        if (strncmp(p,"bhsize=",7) == 0) {
133            if (sscanf(p+7,"%"_LD_,&bhsize) != 1)
134                fatal(p);
135            continue;
136        }
137
138        if (strncmp(p,"pfx=",4) == 0) {
139            record(trec=maxnrec/2,p+4,1L); /* any, but 0L */
140            continue;
141        }
142        if (strncmp(p,"save=",5) == 0) {
143            savedbnp=p+5;
144            continue;
145        }
146        if (strcmp(p,"nomsg") == 0) {
147            msglevel=0L;
148            continue;
149        }
150        if (strcmp(p,"allmsg") == 0) {
151            msglevel=2L;
152            continue;
153        }
154
155        fatal(p);
156    }
157
158    /* trapping fatal errors */
159#if TRAP_FATAL
160    strcpy(fatal_iomsg,"Not Null for trapping fatal errors");
161    rc = setjmp(fatal_jumper);
162    if (rc != 0) {
163        printf("\n*** An error has occurred.  End of Execution.\n");
164        printf("\n*** Please report the following error has occurred:\n");
165        printf("\n    %s\n\n",fatal_iomsg);
166        exit(1);
167    }
168#endif
169
170    /* initialize ridx and b4_xxx() parameters */
171
172    /* get maxmfn */
173    RECORD(ridx=nrecs,dbnp,0L);
174    /* allocate b4_xxx() working record */
175    nbytes1=MF0nxtmfn/8+1;      /* one bit per mfn for b4_run bit strings */
176    recalloc(boolidx=nrecs,bhsize+nbytes1);
177    /* buffer size for b4_run() tmp file */
178    nbytes2=4096L;
179
180
181    /* compile/execute boolean expression */
182
183    boolp=b4_exp(boolidx,dbnp,qryp,savedbnp,&boolerr);
184    if (boolp) {
185        printf("Error #%d (%s)\n",boolerr,boolp);
186        fatal("Expression syntax error");
187    }
188    boolp=b4_run(boolidx,dbnp,nbytes1,nbytes2,msglevel,isisuctab,trec,&boolerr);
189    if (boolp) {
190        printf("Error #%d (%s)\n",boolerr,boolp);
191        fatal("Expression execution error");
192    }
193
194
195    /* get retrieved records, format and display */
196
197    if (msglevel) printf("Hits=%"_LD_" \n",b4_hit(boolidx,0L));
198   
199    for (count=0, nord=1L; ; nord++) {
200        mfn=b4_hit(boolidx,nord);
201        if (mfn < 0L) break;
202        if (mfn < parmfrom) continue;
203        if (mfn > parmto) break;
204        if (count++ >= parmcount) break;
205        RECORD(ridx,dbnp,mfn);
206        if (fmtp) {
207            if (recfmt(ridx,parmlw,fmtp,fmtarea,sizeof(fmtarea)-1L) < 0)
208                fatal(fmtp+fmt_errof);
209            printf("%s",fmtarea);
210        }
211        else prtfields(recp,MFRmfn);
212        if (prompt) { printf("%s",prompt); if (getchar() == 'x') break; }
213    }
214
215    exit(0);
216}
Note: See TracBrowser for help on using the browser.