Debian Codegraph @

WWW/Library/Implementation/HTVMS_WaisProt.c

raw
1 /*
2 * $LynxId: HTVMS_WaisProt.c,v 1.10 2020/01/21 22:10:03 tom Exp $
3 *
4 * HTVMS_WAISProt.c
5 *
6 * Adaptation for Lynx by F.Macrides ([email protected])
7 *
8 * 31-May-1994 FM Initial version.
9 *
10 *----------------------------------------------------------------------*/
11
12 /*
13 * Routines originally from WProt.c -- FM
14 *
15 *----------------------------------------------------------------------*/
16 /* WIDE AREA INFORMATION SERVER SOFTWARE:
17 * No guarantees or restrictions. See the readme file for the full standard
18 * disclaimer.
19
20 * 3.26.90 Harry Morris, [email protected]
21 * 3.30.90 Harry Morris
22 * - removed chunk code from WAISSearchAPDU,
23 * - added makeWAISQueryType1Query() and readWAISType1Query() which replace
24 * makeWAISQueryTerms() and makeWAISQueryDocs().
25 * 4.11.90 HWM - generalized conditional includes (see c-dialect.h)
26 * - renamed makeWAISType1Query() to makeWAISTextQuery()
27 * renamed readWAISType1Query() to readWAISTextQuery()
28 * 5.29.90 TS - fixed bug in makeWAISQueryDocs
29 * added CSTFreeWAISFoo functions
30 */
31
32 #define _C_WAIS_protocol_
33
34 /* This file implements the Z39.50 extensions required for WAIS
35 */
36
37 #include <HTUtils.h>
38 #include <HTVMS_WaisUI.h>
39 #include <HTVMS_WaisProt.h>
40
41 #include <LYLeaks.h>
42
43 /* very rough estimates of the size of an object */
44 #define DefWAISInitResponseSize (size_t)200
45 #define DefWAISSearchSize (size_t)3000
46 #define DefWAISSearchResponseSize (size_t)6000
47 #define DefWAISPresentSize (size_t)1000
48 #define DefWAISPresentResponseSize (size_t)6000
49 #define DefWAISDocHeaderSize (size_t)500
50 #define DefWAISShortHeaderSize (size_t)200
51 #define DefWAISLongHeaderSize (size_t)800
52 #define DefWAISDocTextSize (size_t)6000
53 #define DefWAISDocHeadlineSize (size_t)500
54 #define DefWAISDocCodeSize (size_t)500
55
56 #define RESERVE_SPACE_FOR_WAIS_HEADER(len) \
57 if (*len > 0) \
58 *len -= header_len;
59
60 #define S_MALLOC(type) (type*)s_malloc(sizeof(type))
61 #define S_MALLOC2(type) (type*)s_malloc(sizeof(type) * 2)
62
63 #define S_REALLOC2(type, ptr, num) (type*)s_realloc((char*)ptr, (sizeof(type) * (num + 2)))
64
65 /*----------------------------------------------------------------------*/
66
67 static unsigned long userInfoTagSize(data_tag tag,
68 unsigned long length)
69 /* return the number of bytes required to write the user info tag and
70 length
71 */
72 {
73 unsigned long size;
74
75 /* calculate bytes required to represent tag. max tag is 16K */
76 size = writtenCompressedIntSize(tag);
77 size += writtenCompressedIntSize(length);
78
79 return (size);
80 }
81
82 /*----------------------------------------------------------------------*/
83
84 static char *writeUserInfoHeader(data_tag tag,
85 long infoSize,
86 long estHeaderSize,
87 char *buffer,
88 long *len)
89 /* write the tag and size, making sure the info fits. return the true end
90 of the info (after adjustment) note that the argument infoSize includes
91 estHeaderSize. Note that the argument len is the number of bytes remaining
92 in the buffer. Since we write the tag and size at the beginning of the
93 buffer (in space that we reserved) we don't want to pass len the calls which
94 do that writing.
95 */
96 {
97 long dummyLen = 100; /* plenty of space for a tag and size */
98 char *buf = buffer;
99 long realSize = infoSize - estHeaderSize;
100 long realHeaderSize = userInfoTagSize(tag, realSize);
101
102 if (buffer == NULL || *len == 0)
103 return (NULL);
104
105 /* write the tag */
106 buf = writeTag(tag, buf, &dummyLen);
107
108 /* see if the if the header size was correct. if not,
109 we have to shift the info to fit the real header size */
110 if (estHeaderSize != realHeaderSize) { /* make sure there is enough space */
111 CHECK_FOR_SPACE_LEFT(realHeaderSize - estHeaderSize, len);
112 memmove(buffer + realHeaderSize, buffer + estHeaderSize, (size_t) (realSize));
113 }
114
115 /* write the size */
116 writeCompressedInteger(realSize, buf, &dummyLen);
117
118 /* return the true end of buffer */
119 return (buffer + realHeaderSize + realSize);
120 }
121
122 /*----------------------------------------------------------------------*/
123
124 static char *readUserInfoHeader(data_tag *tag,
125 unsigned long *num,
126 char *buffer)
127 /* read the tag and size */
128 {
129 char *buf = buffer;
130
131 buf = readTag(tag, buf);
132 buf = readCompressedInteger(num, buf);
133 return (buf);
134 }
135
136 /*----------------------------------------------------------------------*/
137
138 WAISInitResponse *makeWAISInitResponse(long chunkCode,
139 long chunkIDLen,
140 char *chunkMarker,
141 char *highlightMarker,
142 char *deHighlightMarker,
143 char *newLineChars)
144 /* create a WAIS init response object */
145 {
146 WAISInitResponse *init = S_MALLOC(WAISInitResponse);
147
148 init->ChunkCode = chunkCode; /* note: none are copied! */
149 init->ChunkIDLength = chunkIDLen;
150 init->ChunkMarker = chunkMarker;
151 init->HighlightMarker = highlightMarker;
152 init->DeHighlightMarker = deHighlightMarker;
153 init->NewlineCharacters = newLineChars;
154
155 return (init);
156 }
157
158 /*----------------------------------------------------------------------*/
159
160 void freeWAISInitResponse(WAISInitResponse *init)
161 /* free an object made with makeWAISInitResponse */
162 {
163 s_free(init->ChunkMarker);
164 s_free(init->HighlightMarker);
165 s_free(init->DeHighlightMarker);
166 s_free(init->NewlineCharacters);
167 s_free(init);
168 }
169
170 /*----------------------------------------------------------------------*/
171
172 char *writeInitResponseInfo(InitResponseAPDU *init,
173 char *buffer,
174 long *len)
175 /* write an init response object */
176 {
177 unsigned long header_len = userInfoTagSize(DT_UserInformationLength,
178 DefWAISInitResponseSize);
179 char *buf = buffer + header_len;
180 WAISInitResponse *info = (WAISInitResponse *) init->UserInformationField;
181 unsigned long size;
182
183 RESERVE_SPACE_FOR_WAIS_HEADER(len);
184
185 buf = writeNum(info->ChunkCode, DT_ChunkCode, buf, len);
186 buf = writeNum(info->ChunkIDLength, DT_ChunkIDLength, buf, len);
187 buf = writeString(info->ChunkMarker, DT_ChunkMarker, buf, len);
188 buf = writeString(info->HighlightMarker, DT_HighlightMarker, buf, len);
189 buf = writeString(info->DeHighlightMarker, DT_DeHighlightMarker, buf, len);
190 buf = writeString(info->NewlineCharacters, DT_NewlineCharacters, buf, len);
191
192 /* now write the header and size */
193 size = buf - buffer;
194 buf = writeUserInfoHeader(DT_UserInformationLength,
195 size,
196 header_len,
197 buffer,
198 len);
199
200 return (buf);
201 }
202
203 /*----------------------------------------------------------------------*/
204
205 char *readInitResponseInfo(void **info,
206 char *buffer)
207 /* read an init response object */
208 {
209 char *buf = buffer;
210 unsigned long size;
211 unsigned long headerSize;
212 long chunkCode, chunkIDLen;
213 data_tag tag1;
214 char *chunkMarker = NULL;
215 char *highlightMarker = NULL;
216 char *deHighlightMarker = NULL;
217 char *newLineChars = NULL;
218
219 chunkCode = chunkIDLen = UNUSED;
220
221 buf = readUserInfoHeader(&tag1, &size, buf);
222 headerSize = buf - buffer;
223
224 while (buf < (buffer + size + headerSize)) {
225 data_tag tag = peekTag(buf);
226
227 switch (tag) {
228 case DT_ChunkCode:
229 buf = readNum(&chunkCode, buf);
230 break;
231 case DT_ChunkIDLength:
232 buf = readNum(&chunkIDLen, buf);
233 break;
234 case DT_ChunkMarker:
235 buf = readString(&chunkMarker, buf);
236 break;
237 case DT_HighlightMarker:
238 buf = readString(&highlightMarker, buf);
239 break;
240 case DT_DeHighlightMarker:
241 buf = readString(&deHighlightMarker, buf);
242 break;
243 case DT_NewlineCharacters:
244 buf = readString(&newLineChars, buf);
245 break;
246 default:
247 s_free(highlightMarker);
248 s_free(deHighlightMarker);
249 s_free(newLineChars);
250 REPORT_READ_ERROR(buf);
251 break;
252 }
253 }
254
255 *info = (void *) makeWAISInitResponse(chunkCode, chunkIDLen, chunkMarker,
256 highlightMarker, deHighlightMarker,
257 newLineChars);
258 return (buf);
259 }
260
261 /*----------------------------------------------------------------------*/
262
263 WAISSearch *makeWAISSearch(char *seedWords,
264 DocObj **docs,
265 char **textList,
266 long dateFactor,
267 char *beginDateRange,
268 char *endDateRange,
269 long maxDocsRetrieved)
270
271 /* create a type 3 query object */
272 {
273 WAISSearch *query = S_MALLOC(WAISSearch);
274
275 query->SeedWords = seedWords; /* not copied! */
276 query->Docs = docs; /* not copied! */
277 query->TextList = textList; /* not copied! */
278 query->DateFactor = dateFactor;
279 query->BeginDateRange = beginDateRange;
280 query->EndDateRange = endDateRange;
281 query->MaxDocumentsRetrieved = maxDocsRetrieved;
282
283 return (query);
284 }
285
286 /*----------------------------------------------------------------------*/
287
288 void freeWAISSearch(WAISSearch *query)
289
290 /* destroy an object made with makeWAISSearch() */
291 {
292 void *ptr = NULL;
293 long i;
294
295 s_free(query->SeedWords);
296
297 if (query->Docs != NULL)
298 for (i = 0, ptr = (void *) query->Docs[i];
299 ptr != NULL;
300 ptr = (void *) query->Docs[++i])
301 freeDocObj((DocObj *) ptr);
302 s_free(query->Docs);
303
304 if (query->TextList != NULL) /* XXX revisit when textlist is fully defined */
305 for (i = 0, ptr = (void *) query->TextList[i];
306 ptr != NULL;
307 ptr = (void *) query->TextList[++i])
308 s_free(ptr);
309 s_free(query->TextList);
310
311 s_free(query->BeginDateRange);
312 s_free(query->EndDateRange);
313 s_free(query);
314 }
315
316 /*----------------------------------------------------------------------*/
317
318 DocObj *makeDocObjUsingWholeDocument(any *docID,
319 char *type)
320
321 /* construct a document object using byte chunks - only for use by
322 servers */
323 {
324 DocObj *doc = S_MALLOC(DocObj);
325
326 doc->DocumentID = docID; /* not copied! */
327 doc->Type = type; /* not copied! */
328 doc->ChunkCode = CT_document;
329 return (doc);
330 }
331
332 /*----------------------------------------------------------------------*/
333
334 DocObj *makeDocObjUsingLines(any *docID,
335 char *type,
336 long start,
337 long end)
338
339 /* construct a document object using line chunks - only for use by
340 servers */
341 {
342 DocObj *doc = S_MALLOC(DocObj);
343
344 doc->ChunkCode = CT_line;
345 doc->DocumentID = docID; /* not copied */
346 doc->Type = type; /* not copied! */
347 doc->ChunkStart.Pos = start;
348 doc->ChunkEnd.Pos = end;
349 return (doc);
350 }
351
352 /*----------------------------------------------------------------------*/
353
354 DocObj *makeDocObjUsingBytes(any *docID,
355 char *type,
356 long start,
357 long end)
358
359 /* construct a document object using byte chunks - only for use by
360 servers */
361 {
362 DocObj *doc = S_MALLOC(DocObj);
363
364 doc->ChunkCode = CT_byte;
365 doc->DocumentID = docID; /* not copied */
366 doc->Type = type; /* not copied! */
367 doc->ChunkStart.Pos = start;
368 doc->ChunkEnd.Pos = end;
369 return (doc);
370 }
371
372 /*----------------------------------------------------------------------*/
373
374 DocObj *makeDocObjUsingParagraphs(any *docID,
375 char *type,
376 any *start,
377 any *end)
378
379 /* construct a document object using byte chunks - only for use by
380 servers */
381 {
382 DocObj *doc = S_MALLOC(DocObj);
383
384 doc->ChunkCode = CT_paragraph;
385 doc->DocumentID = docID; /* not copied */
386 doc->Type = type;
387 doc->ChunkStart.ID = start;
388 doc->ChunkEnd.ID = end;
389 return (doc);
390 }
391
392 /*----------------------------------------------------------------------*/
393
394 void freeDocObj(DocObj *doc)
395
396 /* free a docObj */
397 {
398 freeAny(doc->DocumentID);
399 s_free(doc->Type);
400 if (doc->ChunkCode == CT_paragraph) {
401 freeAny(doc->ChunkStart.ID);
402 freeAny(doc->ChunkEnd.ID);
403 }
404 s_free(doc);
405 }
406
407 /*----------------------------------------------------------------------*/
408
409 static char *writeDocObj(DocObj *doc,
410 char *buffer,
411 long *len)
412
413 /* write as little as we can about the doc obj */
414 {
415 char *buf = buffer;
416
417 /* we always have to write the id, but its tag depends on if it's a chunk */
418 if (doc->ChunkCode == CT_document)
419 buf = writeAny(doc->DocumentID, DT_DocumentID, buf, len);
420 else
421 buf = writeAny(doc->DocumentID, DT_DocumentIDChunk, buf, len);
422
423 if (doc->Type != NULL)
424 buf = writeString(doc->Type, DT_TYPE, buf, len);
425
426 switch (doc->ChunkCode) {
427 case CT_document:
428 /* do nothing - there is no chunk data */
429 break;
430 case CT_byte:
431 case CT_line:
432 buf = writeNum(doc->ChunkCode, DT_ChunkCode, buf, len);
433 buf = writeNum(doc->ChunkStart.Pos, DT_ChunkStartID, buf, len);
434 buf = writeNum(doc->ChunkEnd.Pos, DT_ChunkEndID, buf, len);
435 break;
436 case CT_paragraph:
437 buf = writeNum(doc->ChunkCode, DT_ChunkCode, buf, len);
438 buf = writeAny(doc->ChunkStart.ID, DT_ChunkStartID, buf, len);
439 buf = writeAny(doc->ChunkEnd.ID, DT_ChunkEndID, buf, len);
440 break;
441 default:
442 panic("Implementation error: unknown chuck type %ld",
443 doc->ChunkCode);
444 break;
445 }
446
447 return (buf);
448 }
449
450 /*----------------------------------------------------------------------*/
451
452 static char *readDocObj(DocObj **doc,
453 char *buffer)
454
455 /* read whatever we have about the new document */
456 {
457 char *buf = buffer;
458 data_tag tag;
459
460 *doc = S_MALLOC(DocObj);
461
462 tag = peekTag(buf);
463 buf = readAny(&((*doc)->DocumentID), buf);
464
465 if (tag == DT_DocumentID) {
466 (*doc)->ChunkCode = CT_document;
467 tag = peekTag(buf);
468 if (tag == DT_TYPE) /* XXX depends on DT_TYPE != what comes next */
469 buf = readString(&((*doc)->Type), buf);
470 /* ChunkStart and ChunkEnd are undefined */
471 } else if (tag == DT_DocumentIDChunk) {
472 boolean readParagraphs = false; /* for cleanup */
473
474 tag = peekTag(buf);
475 if (tag == DT_TYPE) /* XXX depends on DT_TYPE != CT_FOO */
476 buf = readString(&((*doc)->Type), buf);
477 buf = readNum(&((*doc)->ChunkCode), buf);
478 switch ((*doc)->ChunkCode) {
479 case CT_byte:
480 case CT_line:
481 buf = readNum(&((*doc)->ChunkStart.Pos), buf);
482 buf = readNum(&((*doc)->ChunkEnd.Pos), buf);
483 break;
484 case CT_paragraph:
485 buf = readAny(&((*doc)->ChunkStart.ID), buf);
486 buf = readAny(&((*doc)->ChunkEnd.ID), buf);
487 break;
488 default:
489 freeAny((*doc)->DocumentID);
490 if (readParagraphs) {
491 freeAny((*doc)->ChunkStart.ID);
492 freeAny((*doc)->ChunkEnd.ID);
493 }
494 s_free(doc);
495 REPORT_READ_ERROR(buf);
496 break;
497 }
498 } else {
499 freeAny((*doc)->DocumentID);
500 s_free(*doc);
501 REPORT_READ_ERROR(buf);
502 }
503 return (buf);
504 }
505
506 /*----------------------------------------------------------------------*/
507
508 char *writeSearchInfo(SearchAPDU *query,
509 char *buffer,
510 long *len)
511
512 /* write out a WAIS query (type 1 or 3) */
513 {
514 if (strcmp(query->QueryType, QT_TextRetrievalQuery) == 0) {
515 return (writeAny((any *) query->Query, DT_Query, buffer, len));
516 } else {
517 unsigned long header_len = userInfoTagSize(DT_UserInformationLength,
518 DefWAISSearchSize);
519 char *buf = buffer + header_len;
520 WAISSearch *info = (WAISSearch *) query->Query;
521 unsigned long size;
522 long i;
523
524 RESERVE_SPACE_FOR_WAIS_HEADER(len);
525
526 buf = writeString(info->SeedWords, DT_SeedWords, buf, len);
527
528 if (info->Docs != NULL) {
529 for (i = 0; info->Docs[i] != NULL; i++) {
530 buf = writeDocObj(info->Docs[i], buf, len);
531 }
532 }
533
534 /* XXX text list */
535
536 buf = writeNum(info->DateFactor,
537 DT_DateFactor,
538 buf,
539 len);
540 buf = writeString(info->BeginDateRange,
541 DT_BeginDateRange,
542 buf,
543 len);
544 buf = writeString(info->EndDateRange,
545 DT_EndDateRange,
546 buf,
547 len);
548 buf = writeNum(info->MaxDocumentsRetrieved,
549 DT_MaxDocumentsRetrieved,
550 buf,
551 len);
552
553 /* now write the header and size */
554 size = buf - buffer;
555 buf = writeUserInfoHeader(DT_UserInformationLength,
556 size,
557 header_len,
558 buffer,
559 len);
560
561 return (buf);
562 }
563 }
564
565 /*----------------------------------------------------------------------*/
566
567 char *readSearchInfo(void **info,
568 char *buffer)
569
570 /* read a WAIS query (type 1 or 3) */
571 {
572 data_tag type = peekTag(buffer);
573
574 if (type == DT_Query) /* this is a type 1 query */
575 {
576 char *buf = buffer;
577 any *query = NULL;
578
579 buf = readAny(&query, buf);
580 *info = (void *) query;
581 return (buf);
582 } else { /* a type 3 query */
583 char *buf = buffer;
584 unsigned long size;
585 unsigned long headerSize;
586 data_tag tag1;
587 char *seedWords = NULL;
588 char *beginDateRange = NULL;
589 char *endDateRange = NULL;
590 long dateFactor, maxDocsRetrieved;
591 char **textList = NULL;
592 DocObj **docIDs = NULL;
593 DocObj *doc = NULL;
594 long docs = 0;
595 long i;
596 void *ptr = NULL;
597
598 dateFactor = maxDocsRetrieved = UNUSED;
599
600 buf = readUserInfoHeader(&tag1, &size, buf);
601 headerSize = buf - buffer;
602
603 while (buf < (buffer + size + headerSize)) {
604 data_tag tag = peekTag(buf);
605
606 switch (tag) {
607 case DT_SeedWords:
608 buf = readString(&seedWords, buf);
609 break;
610 case DT_DocumentID:
611 case DT_DocumentIDChunk:
612 if (docIDs == NULL) /* create a new doc list */
613 {
614 docIDs = S_MALLOC2(DocObj *);
615 } else { /* grow the doc list */
616 docIDs = S_REALLOC2(DocObj *, docIDs, docs);
617 }
618 buf = readDocObj(&doc, buf);
619 if (buf == NULL) {
620 s_free(seedWords);
621 s_free(beginDateRange);
622 s_free(endDateRange);
623 if (docIDs != NULL)
624 for (i = 0, ptr = (void *) docIDs[i];
625 ptr != NULL;
626 ptr = (void *) docIDs[++i])
627 freeDocObj((DocObj *) ptr);
628 s_free(docIDs);
629 /* XXX should also free textlist when it is fully defined */
630 }
631 RETURN_ON_NULL(buf);
632 docIDs[docs++] = doc; /* put it in the list */
633 docIDs[docs] = NULL;
634 break;
635 case DT_TextList:
636 /* XXX */
637 break;
638 case DT_DateFactor:
639 buf = readNum(&dateFactor, buf);
640 break;
641 case DT_BeginDateRange:
642 buf = readString(&beginDateRange, buf);
643 break;
644 case DT_EndDateRange:
645 buf = readString(&endDateRange, buf);
646 break;
647 case DT_MaxDocumentsRetrieved:
648 buf = readNum(&maxDocsRetrieved, buf);
649 break;
650 default:
651 s_free(seedWords);
652 s_free(beginDateRange);
653 s_free(endDateRange);
654 if (docIDs != NULL)
655 for (i = 0, ptr = (void *) docIDs[i];
656 ptr != NULL;
657 ptr = (void *) docIDs[++i])
658 freeDocObj((DocObj *) ptr);
659 s_free(docIDs);
660 /* XXX should also free textlist when it is fully defined */
661 REPORT_READ_ERROR(buf);
662 break;
663 }
664 }
665
666 *info = (void *) makeWAISSearch(seedWords, docIDs, textList,
667 dateFactor, beginDateRange, endDateRange,
668 maxDocsRetrieved);
669 return (buf);
670 }
671 }
672
673 /*----------------------------------------------------------------------*/
674
675 WAISDocumentHeader *makeWAISDocumentHeader(any *docID,
676 long versionNumber,
677 long score,
678 long bestMatch,
679 long docLen,
680 long lines,
681 char **types,
682 char *source,
683 char *date,
684 char *headline,
685 char *originCity)
686
687 /* construct a standard document header, note that no fields are copied!
688 if the application needs to save these fields, it should copy them,
689 or set the field in this object to NULL before freeing it.
690 */
691 {
692 WAISDocumentHeader *header = S_MALLOC(WAISDocumentHeader);
693
694 header->DocumentID = docID;
695 header->VersionNumber = versionNumber;
696 header->Score = score;
697 header->BestMatch = bestMatch;
698 header->DocumentLength = docLen;
699 header->Lines = lines;
700 header->Types = types;
701 header->Source = source;
702 header->Date = date;
703 header->Headline = headline;
704 header->OriginCity = originCity;
705
706 return (header);
707 }
708
709 /*----------------------------------------------------------------------*/
710
711 void freeWAISDocumentHeader(WAISDocumentHeader *header)
712 {
713 freeAny(header->DocumentID);
714 doList((void **) header->Types, fs_free); /* can't use the macro here ! */
715 s_free(header->Types);
716 s_free(header->Source);
717 s_free(header->Date);
718 s_free(header->Headline);
719 s_free(header->OriginCity);
720 s_free(header);
721 }
722
723 /*----------------------------------------------------------------------*/
724
725 char *writeWAISDocumentHeader(WAISDocumentHeader *header, char *buffer,
726 long *len)
727 {
728 unsigned long header_len = userInfoTagSize(DT_DocumentHeaderGroup,
729 DefWAISDocHeaderSize);
730 char *buf = buffer + header_len;
731 unsigned long size1;
732
733 RESERVE_SPACE_FOR_WAIS_HEADER(len);
734
735 buf = writeAny(header->DocumentID, DT_DocumentID, buf, len);
736 buf = writeNum(header->VersionNumber, DT_VersionNumber, buf, len);
737 buf = writeNum(header->Score, DT_Score, buf, len);
738 buf = writeNum(header->BestMatch, DT_BestMatch, buf, len);
739 buf = writeNum(header->DocumentLength, DT_DocumentLength, buf, len);
740 buf = writeNum(header->Lines, DT_Lines, buf, len);
741 if (header->Types != NULL) {
742 long size;
743 char *ptr = NULL;
744 long i;
745
746 buf = writeTag(DT_TYPE_BLOCK, buf, len);
747 for (i = 0, size = 0, ptr = header->Types[i];
748 ptr != NULL;
749 ptr = header->Types[++i]) {
750 long typeSize = strlen(ptr);
751
752 size += writtenTagSize(DT_TYPE);
753 size += writtenCompressedIntSize(typeSize);
754 size += typeSize;
755 }
756 buf = writeCompressedInteger((unsigned long) size, buf, len);
757 for (i = 0, ptr = header->Types[i]; ptr != NULL; ptr = header->Types[++i])
758 buf = writeString(ptr, DT_TYPE, buf, len);
759 }
760 buf = writeString(header->Source, DT_Source, buf, len);
761 buf = writeString(header->Date, DT_Date, buf, len);
762 buf = writeString(header->Headline, DT_Headline, buf, len);
763 buf = writeString(header->OriginCity, DT_OriginCity, buf, len);
764
765 /* now write the header and size */
766 size1 = buf - buffer;
767 buf = writeUserInfoHeader(DT_DocumentHeaderGroup,
768 size1,
769 header_len,
770 buffer,
771 len);
772
773 return (buf);
774 }
775
776 /*----------------------------------------------------------------------*/
777
778 char *readWAISDocumentHeader(WAISDocumentHeader **header, char *buffer)
779 {
780 char *buf = buffer;
781 unsigned long size1;
782 unsigned long headerSize;
783 data_tag tag1;
784 any *docID = NULL;
785 long versionNumber, score, bestMatch, docLength, lines;
786 char **types = NULL;
787 char *source = NULL;
788 char *date = NULL;
789 char *headline = NULL;
790 char *originCity = NULL;
791
792 versionNumber = score = bestMatch = docLength = lines = UNUSED;
793
794 buf = readUserInfoHeader(&tag1, &size1, buf);
795 headerSize = buf - buffer;
796
797 while (buf < (buffer + size1 + headerSize)) {
798 data_tag tag = peekTag(buf);
799
800 switch (tag) {
801 case DT_DocumentID:
802 buf = readAny(&docID, buf);
803 break;
804 case DT_VersionNumber:
805 buf = readNum(&versionNumber, buf);
806 break;
807 case DT_Score:
808 buf = readNum(&score, buf);
809 break;
810 case DT_BestMatch:
811 buf = readNum(&bestMatch, buf);
812 break;
813 case DT_DocumentLength:
814 buf = readNum(&docLength, buf);
815 break;
816 case DT_Lines:
817 buf = readNum(&lines, buf);
818 break;
819 case DT_TYPE_BLOCK:
820 {
821 unsigned long size = -1;
822 long numTypes = 0;
823
824 buf = readTag(&tag, buf);
825 buf = readCompressedInteger(&size, buf);
826 while (size > 0) {
827 char *type = NULL;
828 char *originalBuf = buf;
829
830 buf = readString(&type, buf);
831 types = S_REALLOC2(char *, types, numTypes);
832
833 types[numTypes++] = type;
834 types[numTypes] = NULL;
835 size -= (buf - originalBuf);
836 }
837 }
838 /* FALLTHRU */
839 case DT_Source:
840 buf = readString(&source, buf);
841 break;
842 case DT_Date:
843 buf = readString(&date, buf);
844 break;
845 case DT_Headline:
846 buf = readString(&headline, buf);
847 break;
848 case DT_OriginCity:
849 buf = readString(&originCity, buf);
850 break;
851 default:
852 freeAny(docID);
853 s_free(source);
854 s_free(date);
855 s_free(headline);
856 s_free(originCity);
857 REPORT_READ_ERROR(buf);
858 break;
859 }
860 }
861
862 *header = makeWAISDocumentHeader(docID, versionNumber, score, bestMatch,
863 docLength, lines, types, source, date, headline,
864 originCity);
865 return (buf);
866 }
867
868 /*----------------------------------------------------------------------*/
869
870 WAISDocumentShortHeader *makeWAISDocumentShortHeader(any *docID,
871 long versionNumber,
872 long score,
873 long bestMatch,
874 long docLen,
875 long lines)
876 /* construct a short document header, note that no fields are copied!
877 if the application needs to save these fields, it should copy them,
878 or set the field in this object to NULL before freeing it.
879 */
880 {
881 WAISDocumentShortHeader *header = S_MALLOC(WAISDocumentShortHeader);
882
883 header->DocumentID = docID;
884 header->VersionNumber = versionNumber;
885 header->Score = score;
886 header->BestMatch = bestMatch;
887 header->DocumentLength = docLen;
888 header->Lines = lines;
889
890 return (header);
891 }
892
893 /*----------------------------------------------------------------------*/
894
895 void freeWAISDocumentShortHeader(WAISDocumentShortHeader *header)
896 {
897 freeAny(header->DocumentID);
898 s_free(header);
899 }
900
901 /*----------------------------------------------------------------------*/
902
903 char *writeWAISDocumentShortHeader(WAISDocumentShortHeader *header, char *buffer,
904 long *len)
905 {
906 unsigned long header_len = userInfoTagSize(DT_DocumentShortHeaderGroup,
907 DefWAISShortHeaderSize);
908 char *buf = buffer + header_len;
909 unsigned long size;
910
911 RESERVE_SPACE_FOR_WAIS_HEADER(len);
912
913 buf = writeAny(header->DocumentID, DT_DocumentID, buf, len);
914 buf = writeNum(header->VersionNumber, DT_VersionNumber, buf, len);
915 buf = writeNum(header->Score, DT_Score, buf, len);
916 buf = writeNum(header->BestMatch, DT_BestMatch, buf, len);
917 buf = writeNum(header->DocumentLength, DT_DocumentLength, buf, len);
918 buf = writeNum(header->Lines, DT_Lines, buf, len);
919
920 /* now write the header and size */
921 size = buf - buffer;
922 buf = writeUserInfoHeader(DT_DocumentShortHeaderGroup,
923 size,
924 header_len,
925 buffer,
926 len);
927
928 return (buf);
929 }
930
931 /*----------------------------------------------------------------------*/
932
933 char *readWAISDocumentShortHeader(WAISDocumentShortHeader **header, char *buffer)
934 {
935 char *buf = buffer;
936 unsigned long size;
937 unsigned long headerSize;
938 data_tag tag1;
939 any *docID = NULL;
940 long versionNumber, score, bestMatch, docLength, lines;
941
942 versionNumber = score = bestMatch = docLength = lines = UNUSED;
943
944 buf = readUserInfoHeader(&tag1, &size, buf);
945 headerSize = buf - buffer;
946
947 while (buf < (buffer + size + headerSize)) {
948 data_tag tag = peekTag(buf);
949
950 switch (tag) {
951 case DT_DocumentID:
952 buf = readAny(&docID, buf);
953 break;
954 case DT_VersionNumber:
955 buf = readNum(&versionNumber, buf);
956 break;
957 case DT_Score:
958 buf = readNum(&score, buf);
959 break;
960 case DT_BestMatch:
961 buf = readNum(&bestMatch, buf);
962 break;
963 case DT_DocumentLength:
964 buf = readNum(&docLength, buf);
965 break;
966 case DT_Lines:
967 buf = readNum(&lines, buf);
968 break;
969 default:
970 freeAny(docID);
971 REPORT_READ_ERROR(buf);
972 break;
973 }
974 }
975
976 *header = makeWAISDocumentShortHeader(docID, versionNumber, score, bestMatch,
977 docLength, lines);
978 return (buf);
979 }
980
981 /*----------------------------------------------------------------------*/
982
983 WAISDocumentLongHeader *makeWAISDocumentLongHeader(any *docID,
984 long versionNumber,
985 long score,
986 long bestMatch,
987 long docLen,
988 long lines,
989 char **types,
990 char *source,
991 char *date,
992 char *headline,
993 char *originCity,
994 char *stockCodes,
995 char *companyCodes,
996 char *industryCodes)
997 /* construct a long document header, note that no fields are copied!
998 if the application needs to save these fields, it should copy them,
999 or set the field in this object to NULL before freeing it.
1000 */
1001 {
1002 WAISDocumentLongHeader *header = S_MALLOC(WAISDocumentLongHeader);
1003
1004 header->DocumentID = docID;
1005 header->VersionNumber = versionNumber;
1006 header->Score = score;
1007 header->BestMatch = bestMatch;
1008 header->DocumentLength = docLen;
1009 header->Lines = lines;
1010 header->Types = types;
1011 header->Source = source;
1012 header->Date = date;
1013 header->Headline = headline;
1014 header->OriginCity = originCity;
1015 header->StockCodes = stockCodes;
1016 header->CompanyCodes = companyCodes;
1017 header->IndustryCodes = industryCodes;
1018
1019 return (header);
1020 }
1021
1022 /*----------------------------------------------------------------------*/
1023
1024 void freeWAISDocumentLongHeader(WAISDocumentLongHeader *header)
1025 {
1026 freeAny(header->DocumentID);
1027 doList((void **) header->Types, fs_free); /* can't use the macro here! */
1028 s_free(header->Source);
1029 s_free(header->Date);
1030 s_free(header->Headline);
1031 s_free(header->OriginCity);
1032 s_free(header->StockCodes);
1033 s_free(header->CompanyCodes);
1034 s_free(header->IndustryCodes);
1035 s_free(header);
1036 }
1037
1038 /*----------------------------------------------------------------------*/
1039
1040 char *writeWAISDocumentLongHeader(WAISDocumentLongHeader *header, char *buffer,
1041 long *len)
1042 {
1043 unsigned long header_len = userInfoTagSize(DT_DocumentLongHeaderGroup,
1044 DefWAISLongHeaderSize);
1045 char *buf = buffer + header_len;
1046 unsigned long size1;
1047
1048 RESERVE_SPACE_FOR_WAIS_HEADER(len);
1049
1050 buf = writeAny(header->DocumentID, DT_DocumentID, buf, len);
1051 buf = writeNum(header->VersionNumber, DT_VersionNumber, buf, len);
1052 buf = writeNum(header->Score, DT_Score, buf, len);
1053 buf = writeNum(header->BestMatch, DT_BestMatch, buf, len);
1054 buf = writeNum(header->DocumentLength, DT_DocumentLength, buf, len);
1055 buf = writeNum(header->Lines, DT_Lines, buf, len);
1056 if (header->Types != NULL) {
1057 long size;
1058 char *ptr = NULL;
1059 long i;
1060
1061 buf = writeTag(DT_TYPE_BLOCK, buf, len);
1062 for (i = 0, size = 0, ptr = header->Types[i];
1063 ptr != NULL;
1064 ptr = header->Types[++i]) {
1065 long typeSize = strlen(ptr);
1066
1067 size += writtenTagSize(DT_TYPE);
1068 size += writtenCompressedIntSize(typeSize);
1069 size += typeSize;
1070 }
1071 buf = writeCompressedInteger((unsigned long) size, buf, len);
1072 for (i = 0, ptr = header->Types[i]; ptr != NULL; ptr = header->Types[++i])
1073 buf = writeString(ptr, DT_TYPE, buf, len);
1074 }
1075 buf = writeString(header->Source, DT_Source, buf, len);
1076 buf = writeString(header->Date, DT_Date, buf, len);
1077 buf = writeString(header->Headline, DT_Headline, buf, len);
1078 buf = writeString(header->OriginCity, DT_OriginCity, buf, len);
1079 buf = writeString(header->StockCodes, DT_StockCodes, buf, len);
1080 buf = writeString(header->CompanyCodes, DT_CompanyCodes, buf, len);
1081 buf = writeString(header->IndustryCodes, DT_IndustryCodes, buf, len);
1082
1083 /* now write the header and size */
1084 size1 = buf - buffer;
1085 buf = writeUserInfoHeader(DT_DocumentLongHeaderGroup,
1086 size1,
1087 header_len,
1088 buffer,
1089 len);
1090
1091 return (buf);
1092 }
1093
1094 /*----------------------------------------------------------------------*/
1095
1096 char *readWAISDocumentLongHeader(WAISDocumentLongHeader **header, char *buffer)
1097 {
1098 char *buf = buffer;
1099 unsigned long size1;
1100 unsigned long headerSize;
1101 data_tag tag1;
1102 any *docID;
1103 long versionNumber, score, bestMatch, docLength, lines;
1104 char **types;
1105 char *source, *date, *headline, *originCity, *stockCodes, *companyCodes, *industryCodes;
1106
1107 docID = NULL;
1108 versionNumber =
1109 score =
1110 bestMatch =
1111 docLength =
1112 lines = UNUSED;
1113 types = NULL;
1114 source =
1115 date =
1116 headline =
1117 originCity =
1118 stockCodes =
1119 companyCodes =
1120 industryCodes = NULL;
1121
1122 buf = readUserInfoHeader(&tag1, &size1, buf);
1123 headerSize = buf - buffer;
1124
1125 while (buf < (buffer + size1 + headerSize)) {
1126 data_tag tag = peekTag(buf);
1127
1128 switch (tag) {
1129 case DT_DocumentID:
1130 buf = readAny(&docID, buf);
1131 break;
1132 case DT_VersionNumber:
1133 buf = readNum(&versionNumber, buf);
1134 break;
1135 case DT_Score:
1136 buf = readNum(&score, buf);
1137 break;
1138 case DT_BestMatch:
1139 buf = readNum(&bestMatch, buf);
1140 break;
1141 case DT_DocumentLength:
1142 buf = readNum(&docLength, buf);
1143 break;
1144 case DT_Lines:
1145 buf = readNum(&lines, buf);
1146 break;
1147 case DT_TYPE_BLOCK:
1148 {
1149 unsigned long size = -1;
1150 long numTypes = 0;
1151
1152 buf = readTag(&tag, buf);
1153 readCompressedInteger(&size, buf);
1154 while (size > 0) {
1155 char *type = NULL;
1156 char *originalBuf = buf;
1157
1158 buf = readString(&type, buf);
1159 types = S_REALLOC2(char *, types, numTypes);
1160
1161 types[numTypes++] = type;
1162 types[numTypes] = NULL;
1163 size -= (buf - originalBuf);
1164 }
1165 }
1166 /* FALLTHRU */
1167 case DT_Source:
1168 buf = readString(&source, buf);
1169 break;
1170 case DT_Date:
1171 buf = readString(&date, buf);
1172 break;
1173 case DT_Headline:
1174 buf = readString(&headline, buf);
1175 break;
1176 case DT_OriginCity:
1177 buf = readString(&originCity, buf);
1178 break;
1179 case DT_StockCodes:
1180 buf = readString(&stockCodes, buf);
1181 break;
1182 case DT_CompanyCodes:
1183 buf = readString(&companyCodes, buf);
1184 break;
1185 case DT_IndustryCodes:
1186 buf = readString(&industryCodes, buf);
1187 break;
1188 default:
1189 freeAny(docID);
1190 s_free(source);
1191 s_free(date);
1192 s_free(headline);
1193 s_free(originCity);
1194 s_free(stockCodes);
1195 s_free(companyCodes);
1196 s_free(industryCodes);
1197 REPORT_READ_ERROR(buf);
1198 break;
1199 }
1200 }
1201
1202 *header = makeWAISDocumentLongHeader(docID,
1203 versionNumber,
1204 score,
1205 bestMatch,
1206 docLength,
1207 lines,
1208 types,
1209 source,
1210 date,
1211 headline,
1212 originCity,
1213 stockCodes,
1214 companyCodes,
1215 industryCodes);
1216 return (buf);
1217 }
1218
1219 /*----------------------------------------------------------------------*/
1220
1221 WAISSearchResponse *
1222 makeWAISSearchResponse(
1223 char *seedWordsUsed,
1224 WAISDocumentHeader **docHeaders,
1225 WAISDocumentShortHeader **shortHeaders,
1226 WAISDocumentLongHeader **longHeaders,
1227 WAISDocumentText **text,
1228 WAISDocumentHeadlines **headlines,
1229 WAISDocumentCodes **codes,
1230 diagnosticRecord ** diagnostics)
1231 {
1232 WAISSearchResponse *response = S_MALLOC(WAISSearchResponse);
1233
1234 response->SeedWordsUsed = seedWordsUsed;
1235 response->DocHeaders = docHeaders;
1236 response->ShortHeaders = shortHeaders;
1237 response->LongHeaders = longHeaders;
1238 response->Text = text;
1239 response->Headlines = headlines;
1240 response->Codes = codes;
1241 response->Diagnostics = diagnostics;
1242
1243 return (response);
1244 }
1245
1246 /*----------------------------------------------------------------------*/
1247
1248 void freeWAISSearchResponse(WAISSearchResponse * response)
1249 {
1250 void *ptr = NULL;
1251 long i;
1252
1253 s_free(response->SeedWordsUsed);
1254
1255 if (response->DocHeaders != NULL)
1256 for (i = 0, ptr = (void *) response->DocHeaders[i];
1257 ptr != NULL;
1258 ptr = (void *) response->DocHeaders[++i])
1259 freeWAISDocumentHeader((WAISDocumentHeader *) ptr);
1260 s_free(response->DocHeaders);
1261
1262 if (response->ShortHeaders != NULL)
1263 for (i = 0, ptr = (void *) response->ShortHeaders[i];
1264 ptr != NULL;
1265 ptr = (void *) response->ShortHeaders[++i])
1266 freeWAISDocumentShortHeader((WAISDocumentShortHeader *) ptr);
1267 s_free(response->ShortHeaders);
1268
1269 if (response->LongHeaders != NULL)
1270 for (i = 0, ptr = (void *) response->LongHeaders[i];
1271 ptr != NULL;
1272 ptr = (void *) response->LongHeaders[++i])
1273 freeWAISDocumentLongHeader((WAISDocumentLongHeader *) ptr);
1274 s_free(response->LongHeaders);
1275
1276 if (response->Text != NULL)
1277 for (i = 0, ptr = (void *) response->Text[i];
1278 ptr != NULL;
1279 ptr = (void *) response->Text[++i])
1280 freeWAISDocumentText((WAISDocumentText *) ptr);
1281 s_free(response->Text);
1282
1283 if (response->Headlines != NULL)
1284 for (i = 0, ptr = (void *) response->Headlines[i];
1285 ptr != NULL;
1286 ptr = (void *) response->Headlines[++i])
1287 freeWAISDocumentHeadlines((WAISDocumentHeadlines *) ptr);
1288 s_free(response->Headlines);
1289
1290 if (response->Codes != NULL)
1291 for (i = 0, ptr = (void *) response->Codes[i];
1292 ptr != NULL;
1293 ptr = (void *) response->Codes[++i])
1294 freeWAISDocumentCodes((WAISDocumentCodes *) ptr);
1295 s_free(response->Codes);
1296
1297 if (response->Diagnostics != NULL)
1298 for (i = 0, ptr = (void *) response->Diagnostics[i];
1299 ptr != NULL;
1300 ptr = (void *) response->Diagnostics[++i])
1301 freeDiag((diagnosticRecord *) ptr);
1302 s_free(response->Diagnostics);
1303
1304 s_free(response);
1305 }
1306
1307 /*----------------------------------------------------------------------*/
1308
1309 char *writeSearchResponseInfo(SearchResponseAPDU *query,
1310 char *buffer,
1311 long *len)
1312 {
1313 unsigned long header_len = userInfoTagSize(DT_UserInformationLength,
1314 DefWAISSearchResponseSize);
1315 char *buf = buffer + header_len;
1316 WAISSearchResponse *info = (WAISSearchResponse *) query->DatabaseDiagnosticRecords;
1317 unsigned long size;
1318 void *header = NULL;
1319 long i;
1320
1321 RESERVE_SPACE_FOR_WAIS_HEADER(len);
1322
1323 buf = writeString(info->SeedWordsUsed, DT_SeedWordsUsed, buf, len);
1324
1325 /* write out all the headers */
1326 if (info->DocHeaders != NULL) {
1327 for (i = 0, header = (void *) info->DocHeaders[i];
1328 header != NULL;
1329 header = (void *) info->DocHeaders[++i])
1330 buf = writeWAISDocumentHeader((WAISDocumentHeader *) header, buf, len);
1331 }
1332
1333 if (info->ShortHeaders != NULL) {
1334 for (i = 0, header = (void *) info->ShortHeaders[i];
1335 header != NULL;
1336 header = (void *) info->ShortHeaders[++i])
1337 buf = writeWAISDocumentShortHeader((WAISDocumentShortHeader *) header,
1338 buf,
1339 len);
1340 }
1341
1342 if (info->LongHeaders != NULL) {
1343 for (i = 0, header = (void *) info->LongHeaders[i];
1344 header != NULL;
1345 header = (void *) info->LongHeaders[++i])
1346 buf = writeWAISDocumentLongHeader((WAISDocumentLongHeader *) header,
1347 buf,
1348 len);
1349 }
1350
1351 if (info->Text != NULL) {
1352 for (i = 0, header = (void *) info->Text[i];
1353 header != NULL;
1354 header = (void *) info->Text[++i])
1355 buf = writeWAISDocumentText((WAISDocumentText *) header, buf, len);
1356 }
1357
1358 if (info->Headlines != NULL) {
1359 for (i = 0, header = (void *) info->Headlines[i];
1360 header != NULL;
1361 header = (void *) info->Headlines[++i])
1362 buf = writeWAISDocumentHeadlines((WAISDocumentHeadlines *) header,
1363 buf,
1364 len);
1365 }
1366
1367 if (info->Codes != NULL) {
1368 for (i = 0, header = (void *) info->Codes[i];
1369 header != NULL;
1370 header = (void *) info->Codes[++i])
1371 buf = writeWAISDocumentCodes((WAISDocumentCodes *) header, buf, len);
1372 }
1373
1374 if (info->Diagnostics != NULL) {
1375 for (i = 0, header = (void *) info->Diagnostics[i];
1376 header != NULL;
1377 header = (void *) info->Diagnostics[++i])
1378 buf = writeDiag((diagnosticRecord *) header, buf, len);
1379 }
1380
1381 /* now write the header and size */
1382 size = buf - buffer;
1383 buf = writeUserInfoHeader(DT_UserInformationLength,
1384 size,
1385 header_len,
1386 buffer,
1387 len);
1388
1389 return (buf);
1390 }
1391
1392 /*----------------------------------------------------------------------*/
1393
1394 static void cleanUpWaisSearchResponse(char *buf,
1395 char *seedWordsUsed,
1396 WAISDocumentHeader **docHeaders,
1397 WAISDocumentShortHeader **shortHeaders,
1398 WAISDocumentLongHeader **longHeaders,
1399 WAISDocumentText **text,
1400 WAISDocumentHeadlines **headlines,
1401 WAISDocumentCodes **codes,
1402 diagnosticRecord ** diags)
1403 /* if buf is NULL, we have just gotten a read error, and need to clean up
1404 any state we have built. If not, then everything is going fine, and
1405 we should just hang loose
1406 */
1407 {
1408 void *ptr = NULL;
1409 long i;
1410
1411 if (buf == NULL) {
1412 s_free(seedWordsUsed);
1413 if (docHeaders != NULL)
1414 for (i = 0, ptr = (void *) docHeaders[i]; ptr != NULL;
1415 ptr = (void *) docHeaders[++i])
1416 freeWAISDocumentHeader((WAISDocumentHeader *) ptr);
1417 s_free(docHeaders);
1418 if (shortHeaders != NULL)
1419 for (i = 0, ptr = (void *) shortHeaders[i]; ptr != NULL;
1420 ptr = (void *) shortHeaders[++i])
1421 freeWAISDocumentShortHeader((WAISDocumentShortHeader *) ptr);
1422 s_free(shortHeaders);
1423 if (longHeaders != NULL)
1424 for (i = 0, ptr = (void *) longHeaders[i]; ptr != NULL;
1425 ptr = (void *) longHeaders[++i])
1426 freeWAISDocumentLongHeader((WAISDocumentLongHeader *) ptr);
1427 s_free(longHeaders);
1428 if (text != NULL)
1429 for (i = 0, ptr = (void *) text[i];
1430 ptr != NULL;
1431 ptr = (void *) text[++i])
1432 freeWAISDocumentText((WAISDocumentText *) ptr);
1433 s_free(text);
1434 if (headlines != NULL)
1435 for (i = 0, ptr = (void *) headlines[i]; ptr != NULL;
1436 ptr = (void *) headlines[++i])
1437 freeWAISDocumentHeadlines((WAISDocumentHeadlines *) ptr);
1438 s_free(headlines);
1439 if (codes != NULL)
1440 for (i = 0, ptr = (void *) codes[i]; ptr != NULL;
1441 ptr = (void *) codes[++i])
1442 freeWAISDocumentCodes((WAISDocumentCodes *) ptr);
1443 s_free(codes);
1444 if (diags != NULL)
1445 for (i = 0, ptr = (void *) diags[i]; ptr != NULL;
1446 ptr = (void *) diags[++i])
1447 freeDiag((diagnosticRecord *) ptr);
1448 s_free(diags);
1449 }
1450 }
1451
1452 /*----------------------------------------------------------------------*/
1453
1454 char *readSearchResponseInfo(void **info,
1455 char *buffer)
1456 {
1457 char *buf = buffer;
1458 unsigned long size;
1459 unsigned long headerSize;
1460 data_tag tag1;
1461 void *header = NULL;
1462 WAISDocumentHeader **docHeaders = NULL;
1463 WAISDocumentShortHeader **shortHeaders = NULL;
1464 WAISDocumentLongHeader **longHeaders = NULL;
1465 WAISDocumentText **text = NULL;
1466 WAISDocumentHeadlines **headlines = NULL;
1467 WAISDocumentCodes **codes = NULL;
1468 long numDocHeaders, numLongHeaders, numShortHeaders, numText, numHeadlines;
1469 long numCodes;
1470 char *seedWordsUsed = NULL;
1471 diagnosticRecord **diags = NULL;
1472 diagnosticRecord *diag = NULL;
1473 long numDiags = 0;
1474
1475 numDocHeaders =
1476 numLongHeaders =
1477 numShortHeaders =
1478 numText =
1479 numHeadlines =
1480 numCodes = 0;
1481
1482 buf = readUserInfoHeader(&tag1, &size, buf);
1483 headerSize = buf - buffer;
1484
1485 while (buf < (buffer + size + headerSize)) {
1486 data_tag tag = peekTag(buf);
1487
1488 switch (tag) {
1489 case DT_SeedWordsUsed:
1490 buf = readString(&seedWordsUsed, buf);
1491 break;
1492 case DT_DatabaseDiagnosticRecords:
1493 if (diags == NULL) /* create a new diag list */
1494 {
1495 diags = S_MALLOC2(diagnosticRecord *);
1496 } else { /* grow the diag list */
1497 diags = S_REALLOC2(diagnosticRecord *, diags, numDiags);
1498 }
1499 buf = readDiag(&diag, buf);
1500 diags[numDiags++] = diag; /* put it in the list */
1501 diags[numDiags] = NULL;
1502 break;
1503 case DT_DocumentHeaderGroup:
1504 if (docHeaders == NULL) /* create a new header list */
1505 {
1506 docHeaders = S_MALLOC2(WAISDocumentHeader *);
1507 } else { /* grow the doc list */
1508 docHeaders = S_REALLOC2(WAISDocumentHeader *, docHeaders, numDocHeaders);
1509 }
1510 buf = readWAISDocumentHeader((WAISDocumentHeader **) &header, buf);
1511 cleanUpWaisSearchResponse(buf,
1512 seedWordsUsed,
1513 docHeaders,
1514 shortHeaders,
1515 longHeaders,
1516 text,
1517 headlines,
1518 codes,
1519 diags);
1520 RETURN_ON_NULL(buf);
1521 docHeaders[numDocHeaders++] =
1522 (WAISDocumentHeader *) header; /* put it in the list */
1523 docHeaders[numDocHeaders] = NULL;
1524 break;
1525 case DT_DocumentShortHeaderGroup:
1526 if (shortHeaders == NULL) /* create a new header list */
1527 {
1528 shortHeaders = S_MALLOC2(WAISDocumentShortHeader *);
1529 } else { /* grow the doc list */
1530 shortHeaders = S_REALLOC2(WAISDocumentShortHeader *,
1531 shortHeaders,
1532 numShortHeaders);
1533 }
1534 buf = readWAISDocumentShortHeader((WAISDocumentShortHeader **) &header,
1535 buf);
1536 cleanUpWaisSearchResponse(buf,
1537 seedWordsUsed,
1538 docHeaders,
1539 shortHeaders,
1540 longHeaders,
1541 text,
1542 headlines,
1543 codes,
1544 diags);
1545 RETURN_ON_NULL(buf);
1546 shortHeaders[numShortHeaders++] =
1547 (WAISDocumentShortHeader *) header; /* put it in the list */
1548 shortHeaders[numShortHeaders] = NULL;
1549 break;
1550 case DT_DocumentLongHeaderGroup:
1551 if (longHeaders == NULL) /* create a new header list */
1552 {
1553 longHeaders = S_MALLOC2(WAISDocumentLongHeader *);
1554 } else { /* grow the doc list */
1555 longHeaders = S_REALLOC2(WAISDocumentLongHeader *,
1556 longHeaders,
1557 numLongHeaders);
1558 }
1559 buf = readWAISDocumentLongHeader((WAISDocumentLongHeader **) &header,
1560 buf);
1561 cleanUpWaisSearchResponse(buf,
1562 seedWordsUsed,
1563 docHeaders,
1564 shortHeaders,
1565 longHeaders,
1566 text,
1567 headlines,
1568 codes,
1569 diags);
1570 RETURN_ON_NULL(buf);
1571 longHeaders[numLongHeaders++] =
1572 (WAISDocumentLongHeader *) header; /* put it in the list */
1573 longHeaders[numLongHeaders] = NULL;
1574 break;
1575 case DT_DocumentTextGroup:
1576 if (text == NULL) /* create a new list */
1577 {
1578 text = S_MALLOC2(WAISDocumentText *);
1579 } else { /* grow the list */
1580 text = S_REALLOC2(WAISDocumentText *, text, numText);
1581 }
1582 buf = readWAISDocumentText((WAISDocumentText **) &header, buf);
1583 cleanUpWaisSearchResponse(buf,
1584 seedWordsUsed,
1585 docHeaders,
1586 shortHeaders,
1587 longHeaders,
1588 text,
1589 headlines,
1590 codes,
1591 diags);
1592 RETURN_ON_NULL(buf);
1593 text[numText++] =
1594 (WAISDocumentText *) header; /* put it in the list */
1595 text[numText] = NULL;
1596 break;
1597 case DT_DocumentHeadlineGroup:
1598 if (headlines == NULL) /* create a new list */
1599 {
1600 headlines = S_MALLOC2(WAISDocumentHeadlines *);
1601 } else { /* grow the list */
1602 headlines = S_REALLOC2(WAISDocumentHeadlines *, headlines, numHeadlines);
1603 }
1604 buf = readWAISDocumentHeadlines((WAISDocumentHeadlines **) &header,
1605 buf);
1606 cleanUpWaisSearchResponse(buf,
1607 seedWordsUsed,
1608 docHeaders,
1609 shortHeaders,
1610 longHeaders,
1611 text,
1612 headlines,
1613 codes,
1614 diags);
1615 RETURN_ON_NULL(buf);
1616 headlines[numHeadlines++] =
1617 (WAISDocumentHeadlines *) header; /* put it in the list */
1618 headlines[numHeadlines] = NULL;
1619 break;
1620 case DT_DocumentCodeGroup:
1621 if (codes == NULL) /* create a new list */
1622 {
1623 codes = S_MALLOC2(WAISDocumentCodes *);
1624 } else { /* grow the list */
1625 codes = S_REALLOC2(WAISDocumentCodes *, codes, numCodes);
1626 }
1627 buf = readWAISDocumentCodes((WAISDocumentCodes **) &header, buf);
1628 cleanUpWaisSearchResponse(buf,
1629 seedWordsUsed,
1630 docHeaders,
1631 shortHeaders,
1632 longHeaders,
1633 text,
1634 headlines,
1635 codes,
1636 diags);
1637 RETURN_ON_NULL(buf);
1638 codes[numCodes++] =
1639 (WAISDocumentCodes *) header; /* put it in the list */
1640 codes[numCodes] = NULL;
1641 break;
1642 default:
1643 cleanUpWaisSearchResponse(buf,
1644 seedWordsUsed,
1645 docHeaders,
1646 shortHeaders,
1647 longHeaders,
1648 text,
1649 headlines,
1650 codes,
1651 diags);
1652 REPORT_READ_ERROR(buf);
1653 break;
1654 }
1655 }
1656
1657 *info = (void *) makeWAISSearchResponse(seedWordsUsed,
1658 docHeaders,
1659 shortHeaders,
1660 longHeaders,
1661 text,
1662 headlines,
1663 codes,
1664 diags);
1665
1666 return (buf);
1667 }
1668
1669 /*----------------------------------------------------------------------*/
1670
1671 WAISDocumentText *makeWAISDocumentText(any *docID,
1672 long versionNumber,
1673 any *documentText)
1674 {
1675 WAISDocumentText *docText = S_MALLOC(WAISDocumentText);
1676
1677 docText->DocumentID = docID;
1678 docText->VersionNumber = versionNumber;
1679 docText->DocumentText = documentText;
1680
1681 return (docText);
1682 }
1683
1684 /*----------------------------------------------------------------------*/
1685
1686 void freeWAISDocumentText(WAISDocumentText *docText)
1687 {
1688 freeAny(docText->DocumentID);
1689 freeAny(docText->DocumentText);
1690 s_free(docText);
1691 }
1692
1693 /*----------------------------------------------------------------------*/
1694
1695 char *writeWAISDocumentText(WAISDocumentText *docText, char *buffer,
1696 long *len)
1697 {
1698 unsigned long header_len = userInfoTagSize(DT_DocumentTextGroup,
1699 DefWAISDocTextSize);
1700 char *buf = buffer + header_len;
1701 unsigned long size;
1702
1703 RESERVE_SPACE_FOR_WAIS_HEADER(len);
1704
1705 buf = writeAny(docText->DocumentID, DT_DocumentID, buf, len);
1706 buf = writeNum(docText->VersionNumber, DT_VersionNumber, buf, len);
1707 buf = writeAny(docText->DocumentText, DT_DocumentText, buf, len);
1708
1709 /* now write the header and size */
1710 size = buf - buffer;
1711 buf = writeUserInfoHeader(DT_DocumentTextGroup, size, header_len, buffer, len);
1712
1713 return (buf);
1714 }
1715
1716 /*----------------------------------------------------------------------*/
1717
1718 char *readWAISDocumentText(WAISDocumentText **docText, char *buffer)
1719 {
1720 char *buf = buffer;
1721 unsigned long size;
1722 unsigned long headerSize;
1723 data_tag tag1;
1724 any *docID, *documentText;
1725 long versionNumber;
1726
1727 docID = documentText = NULL;
1728 versionNumber = UNUSED;
1729
1730 buf = readUserInfoHeader(&tag1, &size, buf);
1731 headerSize = buf - buffer;
1732
1733 while (buf < (buffer + size + headerSize)) {
1734 data_tag tag = peekTag(buf);
1735
1736 switch (tag) {
1737 case DT_DocumentID:
1738 buf = readAny(&docID, buf);
1739 break;
1740 case DT_VersionNumber:
1741 buf = readNum(&versionNumber, buf);
1742 break;
1743 case DT_DocumentText:
1744 buf = readAny(&documentText, buf);
1745 break;
1746 default:
1747 freeAny(docID);
1748 freeAny(documentText);
1749 REPORT_READ_ERROR(buf);
1750 break;
1751 }
1752 }
1753
1754 *docText = makeWAISDocumentText(docID, versionNumber, documentText);
1755 return (buf);
1756 }
1757
1758 /*----------------------------------------------------------------------*/
1759
1760 WAISDocumentHeadlines *makeWAISDocumentHeadlines(any *docID,
1761 long versionNumber,
1762 char *source,
1763 char *date,
1764 char *headline,
1765 char *originCity)
1766 {
1767 WAISDocumentHeadlines *docHeadline = S_MALLOC(WAISDocumentHeadlines);
1768
1769 docHeadline->DocumentID = docID;
1770 docHeadline->VersionNumber = versionNumber;
1771 docHeadline->Source = source;
1772 docHeadline->Date = date;
1773 docHeadline->Headline = headline;
1774 docHeadline->OriginCity = originCity;
1775
1776 return (docHeadline);
1777 }
1778
1779 /*----------------------------------------------------------------------*/
1780
1781 void freeWAISDocumentHeadlines(WAISDocumentHeadlines *docHeadline)
1782 {
1783 freeAny(docHeadline->DocumentID);
1784 s_free(docHeadline->Source);
1785 s_free(docHeadline->Date);
1786 s_free(docHeadline->Headline);
1787 s_free(docHeadline->OriginCity);
1788 s_free(docHeadline);
1789 }
1790
1791 /*----------------------------------------------------------------------*/
1792
1793 char *writeWAISDocumentHeadlines(WAISDocumentHeadlines *docHeadline, char *buffer,
1794 long *len)
1795 {
1796 unsigned long header_len = userInfoTagSize(DT_DocumentHeadlineGroup,
1797 DefWAISDocHeadlineSize);
1798 char *buf = buffer + header_len;
1799 unsigned long size;
1800
1801 RESERVE_SPACE_FOR_WAIS_HEADER(len);
1802
1803 buf = writeAny(docHeadline->DocumentID, DT_DocumentID, buf, len);
1804 buf = writeNum(docHeadline->VersionNumber, DT_VersionNumber, buf, len);
1805 buf = writeString(docHeadline->Source, DT_Source, buf, len);
1806 buf = writeString(docHeadline->Date, DT_Date, buf, len);
1807 buf = writeString(docHeadline->Headline, DT_Headline, buf, len);
1808 buf = writeString(docHeadline->OriginCity, DT_OriginCity, buf, len);
1809
1810 /* now write the header and size */
1811 size = buf - buffer;
1812 buf = writeUserInfoHeader(DT_DocumentHeadlineGroup,
1813 size,
1814 header_len,
1815 buffer,
1816 len);
1817
1818 return (buf);
1819 }
1820
1821 /*----------------------------------------------------------------------*/
1822
1823 char *readWAISDocumentHeadlines(WAISDocumentHeadlines **docHeadline, char *buffer)
1824 {
1825 char *buf = buffer;
1826 unsigned long size;
1827 unsigned long headerSize;
1828 data_tag tag1;
1829 any *docID;
1830 long versionNumber;
1831 char *source, *date, *headline, *originCity;
1832
1833 docID = NULL;
1834 versionNumber = UNUSED;
1835 source = date = headline = originCity = NULL;
1836
1837 buf = readUserInfoHeader(&tag1, &size, buf);
1838 headerSize = buf - buffer;
1839
1840 while (buf < (buffer + size + headerSize)) {
1841 data_tag tag = peekTag(buf);
1842
1843 switch (tag) {
1844 case DT_DocumentID:
1845 buf = readAny(&docID, buf);
1846 break;
1847 case DT_VersionNumber:
1848 buf = readNum(&versionNumber, buf);
1849 break;
1850 case DT_Source:
1851 buf = readString(&source, buf);
1852 break;
1853 case DT_Date:
1854 buf = readString(&date, buf);
1855 break;
1856 case DT_Headline:
1857 buf = readString(&headline, buf);
1858 break;
1859 case DT_OriginCity:
1860 buf = readString(&originCity, buf);
1861 break;
1862 default:
1863 freeAny(docID);
1864 s_free(source);
1865 s_free(date);
1866 s_free(headline);
1867 s_free(originCity);
1868 REPORT_READ_ERROR(buf);
1869 break;
1870 }
1871 }
1872
1873 *docHeadline = makeWAISDocumentHeadlines(docID, versionNumber, source, date,
1874 headline, originCity);
1875 return (buf);
1876 }
1877
1878 /*----------------------------------------------------------------------*/
1879
1880 WAISDocumentCodes *makeWAISDocumentCodes(any *docID,
1881 long versionNumber,
1882 char *stockCodes,
1883 char *companyCodes,
1884 char *industryCodes)
1885 {
1886 WAISDocumentCodes *docCodes = S_MALLOC(WAISDocumentCodes);
1887
1888 docCodes->DocumentID = docID;
1889 docCodes->VersionNumber = versionNumber;
1890 docCodes->StockCodes = stockCodes;
1891 docCodes->CompanyCodes = companyCodes;
1892 docCodes->IndustryCodes = industryCodes;
1893
1894 return (docCodes);
1895 }
1896
1897 /*----------------------------------------------------------------------*/
1898
1899 void freeWAISDocumentCodes(WAISDocumentCodes *docCodes)
1900 {
1901 freeAny(docCodes->DocumentID);
1902 s_free(docCodes->StockCodes);
1903 s_free(docCodes->CompanyCodes);
1904 s_free(docCodes->IndustryCodes);
1905 s_free(docCodes);
1906 }
1907
1908 /*----------------------------------------------------------------------*/
1909
1910 char *writeWAISDocumentCodes(WAISDocumentCodes *docCodes,
1911 char *buffer,
1912 long *len)
1913 {
1914 unsigned long header_len = userInfoTagSize(DT_DocumentCodeGroup,
1915 DefWAISDocCodeSize);
1916 char *buf = buffer + header_len;
1917 unsigned long size;
1918
1919 RESERVE_SPACE_FOR_WAIS_HEADER(len);
1920
1921 buf = writeAny(docCodes->DocumentID, DT_DocumentID, buf, len);
1922 buf = writeNum(docCodes->VersionNumber, DT_VersionNumber, buf, len);
1923 buf = writeString(docCodes->StockCodes, DT_StockCodes, buf, len);
1924 buf = writeString(docCodes->CompanyCodes, DT_CompanyCodes, buf, len);
1925 buf = writeString(docCodes->IndustryCodes, DT_IndustryCodes, buf, len);
1926
1927 /* now write the header and size */
1928 size = buf - buffer;
1929 buf = writeUserInfoHeader(DT_DocumentCodeGroup, size, header_len, buffer, len);
1930
1931 return (buf);
1932 }
1933
1934 /*----------------------------------------------------------------------*/
1935
1936 char *readWAISDocumentCodes(WAISDocumentCodes **docCodes,
1937 char *buffer)
1938 {
1939 char *buf = buffer;
1940 unsigned long size;
1941 unsigned long headerSize;
1942 data_tag tag1;
1943 any *docID;
1944 long versionNumber;
1945 char *stockCodes, *companyCodes, *industryCodes;
1946
1947 docID = NULL;
1948 versionNumber = UNUSED;
1949 stockCodes = companyCodes = industryCodes = NULL;
1950
1951 buf = readUserInfoHeader(&tag1, &size, buf);
1952 headerSize = buf - buffer;
1953
1954 while (buf < (buffer + size + headerSize)) {
1955 data_tag tag = peekTag(buf);
1956
1957 switch (tag) {
1958 case DT_DocumentID:
1959 buf = readAny(&docID, buf);
1960 break;
1961 case DT_VersionNumber:
1962 buf = readNum(&versionNumber, buf);
1963 break;
1964 case DT_StockCodes:
1965 buf = readString(&stockCodes, buf);
1966 break;
1967 case DT_CompanyCodes:
1968 buf = readString(&companyCodes, buf);
1969 break;
1970 case DT_IndustryCodes:
1971 buf = readString(&industryCodes, buf);
1972 break;
1973 default:
1974 freeAny(docID);
1975 s_free(stockCodes);
1976 s_free(companyCodes);
1977 s_free(industryCodes);
1978 REPORT_READ_ERROR(buf);
1979 break;
1980 }
1981 }
1982
1983 *docCodes = makeWAISDocumentCodes(docID, versionNumber, stockCodes,
1984 companyCodes, industryCodes);
1985 return (buf);
1986 }
1987
1988 /*----------------------------------------------------------------------*/
1989
1990 char *writePresentInfo(PresentAPDU * present GCC_UNUSED, char *buffer,
1991 long *len GCC_UNUSED)
1992 {
1993 /* The WAIS protocol doesn't use present info */
1994 return (buffer);
1995 }
1996
1997 /*----------------------------------------------------------------------*/
1998
1999 char *readPresentInfo(void **info,
2000 char *buffer)
2001 {
2002 /* The WAIS protocol doesn't use present info */
2003 *info = NULL;
2004 return (buffer);
2005 }
2006
2007 /*----------------------------------------------------------------------*/
2008
2009 char *writePresentResponseInfo(PresentResponseAPDU * response GCC_UNUSED, char *buffer,
2010 long *len GCC_UNUSED)
2011 {
2012 /* The WAIS protocol doesn't use presentResponse info */
2013 return (buffer);
2014 }
2015
2016 /*----------------------------------------------------------------------*/
2017
2018 char *readPresentResponseInfo(void **info,
2019 char *buffer)
2020 {
2021 /* The WAIS protocol doesn't use presentResponse info */
2022 *info = NULL;
2023 return (buffer);
2024 }
2025
2026 /*----------------------------------------------------------------------*/
2027
2028 /* support for type 1 queries */
2029
2030 /* new use values (for the chunk types) */
2031 #define BYTE "wb"
2032 #define LINE "wl"
2033 #define PARAGRAPH "wp"
2034 #define DATA_TYPE "wt"
2035
2036 /* WAIS supports the following semantics for type 1 queries:
2037
2038 1. retrieve the header/codes from a document:
2039
2040 System_Control_Number = docID
2041 Data Type = type (optional)
2042 And
2043
2044 2. retrieve a fragment of the text of a document:
2045
2046 System_Control_Number = docID
2047 Data Type = type (optional)
2048 And
2049 Chunk >= start
2050 And
2051 Chunk < end
2052 And
2053
2054 Information from multiple documents may be requested by using
2055 groups of the above joined by:
2056
2057 OR
2058
2059 ( XXX does an OR come after every group but the first, or do they
2060 all come at the end? )
2061
2062 ( XXX return type could be in the element set)
2063 */
2064
2065 static query_term **makeWAISQueryTerms(DocObj **docs)
2066 /* given a null terminated list of docObjs, construct the appropriate
2067 query of the form given above
2068 */
2069 {
2070 query_term **terms = NULL;
2071 long numTerms = 0;
2072 DocObj *doc = NULL;
2073 long i;
2074
2075 if (docs == NULL)
2076 return ((query_term **) NULL);
2077
2078 terms = (query_term **) s_malloc((size_t) (sizeof(query_term *) * 1));
2079
2080 terms[numTerms] = NULL;
2081
2082 /* loop through the docs making terms for them all */
2083 for (i = 0, doc = docs[i]; doc != NULL; doc = docs[++i]) {
2084 any *type = NULL;
2085
2086 if (doc->Type != NULL)
2087 type = stringToAny(doc->Type);
2088
2089 if (doc->ChunkCode == CT_document) /* a whole document */
2090 {
2091 terms = S_REALLOC2(query_term *, terms, numTerms + 2);
2092
2093 terms[numTerms++] = makeAttributeTerm(SYSTEM_CONTROL_NUMBER,
2094 EQUAL, IGNORE, IGNORE,
2095 IGNORE, IGNORE, doc->DocumentID);
2096 if (type != NULL) {
2097 terms[numTerms++] = makeAttributeTerm(DATA_TYPE, EQUAL,
2098 IGNORE, IGNORE, IGNORE,
2099 IGNORE, type);
2100 terms[numTerms++] = makeOperatorTerm(AND);
2101 }
2102 terms[numTerms] = NULL;
2103 } else { /* a document fragment */
2104 char chunk_att[ATTRIBUTE_SIZE];
2105 any *startChunk = NULL;
2106 any *endChunk = NULL;
2107
2108 terms = S_REALLOC2(query_term *, terms, numTerms + 6);
2109
2110 switch (doc->ChunkCode) {
2111 case CT_byte:
2112 case CT_line:
2113 {
2114 char start[20], end[20];
2115
2116 (doc->ChunkCode == CT_byte) ?
2117 StrNCpy(chunk_att, BYTE, ATTRIBUTE_SIZE) :
2118 StrNCpy(chunk_att, LINE, ATTRIBUTE_SIZE);
2119 sprintf(start, "%ld", doc->ChunkStart.Pos);
2120 startChunk = stringToAny(start);
2121 sprintf(end, "%ld", doc->ChunkEnd.Pos);
2122 endChunk = stringToAny(end);
2123 }
2124 break;
2125 case CT_paragraph:
2126 StrNCpy(chunk_att, PARAGRAPH, ATTRIBUTE_SIZE);
2127 startChunk = doc->ChunkStart.ID;
2128 endChunk = doc->ChunkEnd.ID;
2129 break;
2130 default:
2131 /* error */
2132 break;
2133 }
2134
2135 terms[numTerms++] = makeAttributeTerm(SYSTEM_CONTROL_NUMBER,
2136 EQUAL, IGNORE, IGNORE,
2137 IGNORE,
2138 IGNORE, doc->DocumentID);
2139 if (type != NULL) {
2140 terms[numTerms++] = makeAttributeTerm(DATA_TYPE, EQUAL, IGNORE,
2141 IGNORE, IGNORE, IGNORE,
2142 type);
2143 terms[numTerms++] = makeOperatorTerm(AND);
2144 }
2145 terms[numTerms++] = makeAttributeTerm(chunk_att,
2146 GREATER_THAN_OR_EQUAL,
2147 IGNORE, IGNORE, IGNORE,
2148 IGNORE,
2149 startChunk);
2150 terms[numTerms++] = makeOperatorTerm(AND);
2151 terms[numTerms++] = makeAttributeTerm(chunk_att, LESS_THAN,
2152 IGNORE, IGNORE, IGNORE,
2153 IGNORE,
2154 endChunk);
2155 terms[numTerms++] = makeOperatorTerm(AND);
2156 terms[numTerms] = NULL;
2157
2158 if (doc->ChunkCode == CT_byte || doc->ChunkCode == CT_line) {
2159 freeAny(startChunk);
2160 freeAny(endChunk);
2161 }
2162 }
2163
2164 freeAny(type);
2165
2166 if (i != 0) /* multiple independent queries, need a disjunction */
2167 {
2168 terms = S_REALLOC2(query_term *, terms, numTerms);
2169
2170 terms[numTerms++] = makeOperatorTerm(OR);
2171 terms[numTerms] = NULL;
2172 }
2173 }
2174
2175 return (terms);
2176 }
2177
2178 /*----------------------------------------------------------------------*/
2179
2180 static DocObj **makeWAISQueryDocs(query_term **terms)
2181 /* given a list of terms in the form given above, convert them to
2182 DocObjs.
2183 */
2184 {
2185 query_term *docTerm = NULL;
2186 query_term *fragmentTerm = NULL;
2187 DocObj **docs = NULL;
2188 DocObj *doc = NULL;
2189 long docNum, termNum;
2190
2191 docNum = termNum = 0;
2192
2193 docs = S_MALLOC(DocObj *);
2194
2195 docs[docNum] = NULL;
2196
2197 /* translate the terms into DocObjs */
2198 while (true) {
2199 query_term *typeTerm = NULL;
2200 char *type = NULL;
2201 long startTermOffset;
2202
2203 docTerm = terms[termNum];
2204
2205 if (docTerm == NULL)
2206 break; /* we're done converting */
2207
2208 typeTerm = terms[termNum + 1]; /* get the lead Term if it exists */
2209
2210 if (strcmp(typeTerm->Use, DATA_TYPE) == 0) /* we do have a type */
2211 {
2212 startTermOffset = 3;
2213 type = anyToString(typeTerm->Term);
2214 } else { /* no type */
2215 startTermOffset = 1;
2216 typeTerm = NULL;
2217 type = NULL;
2218 }
2219
2220 /* grow the doc list */
2221 docs = S_REALLOC2(DocObj *, docs, docNum);
2222
2223 /* figure out what kind of docObj to build - and build it */
2224 fragmentTerm = terms[termNum + startTermOffset];
2225 if (fragmentTerm != NULL && fragmentTerm->TermType == TT_Attribute) { /* build a document fragment */
2226 query_term *startTerm = fragmentTerm;
2227 query_term *endTerm = terms[termNum + startTermOffset + 2];
2228
2229 if (strcmp(startTerm->Use, BYTE) == 0) { /* a byte chunk */
2230 doc = makeDocObjUsingBytes(duplicateAny(docTerm->Term),
2231 type,
2232 anyToLong(startTerm->Term),
2233 anyToLong(endTerm->Term));
2234 log_write("byte");
2235 } else if (strcmp(startTerm->Use, LINE) == 0) { /* a line chunk */
2236 doc = makeDocObjUsingLines(duplicateAny(docTerm->Term),
2237 type,
2238 anyToLong(startTerm->Term),
2239 anyToLong(endTerm->Term));
2240 log_write("line");
2241 } else {
2242 log_write("chunk"); /* a paragraph chunk */
2243 doc = makeDocObjUsingParagraphs(duplicateAny(docTerm->Term),
2244 type,
2245 duplicateAny(startTerm->Term),
2246 duplicateAny(endTerm->Term));
2247 }
2248 termNum += (startTermOffset + 4); /* point to next term */
2249 } else { /* build a full document */
2250 doc = makeDocObjUsingWholeDocument(duplicateAny(docTerm->Term),
2251 type);
2252 log_write("whole doc");
2253 termNum += startTermOffset; /* point to next term */
2254 }
2255
2256 docs[docNum++] = doc; /* insert the new document */
2257
2258 docs[docNum] = NULL; /* keep the doc list terminated */
2259
2260 if (terms[termNum] != NULL)
2261 termNum++; /* skip the OR operator it necessary */
2262 else
2263 break; /* we are done */
2264 }
2265
2266 return (docs);
2267 }
2268
2269 /*----------------------------------------------------------------------*/
2270
2271 any *makeWAISTextQuery(DocObj **docs)
2272 /* given a list of DocObjs, return an any whose contents is the corresponding
2273 type 1 query
2274 */
2275 {
2276 any *buf = NULL;
2277 query_term **terms = NULL;
2278
2279 terms = makeWAISQueryTerms(docs);
2280 buf = writeQuery(terms);
2281
2282 doList((void **) terms, freeTerm);
2283 s_free(terms);
2284
2285 return (buf);
2286 }
2287
2288 /*----------------------------------------------------------------------*/
2289
2290 DocObj **readWAISTextQuery(any *buf)
2291 /* given an any whose contents are type 1 queries of the WAIS sort,
2292 construct a list of the corresponding DocObjs
2293 */
2294 {
2295 query_term **terms = NULL;
2296 DocObj **docs = NULL;
2297
2298 terms = readQuery(buf);
2299 docs = makeWAISQueryDocs(terms);
2300
2301 doList((void **) terms, freeTerm);
2302 s_free(terms);
2303
2304 return (docs);
2305 }
2306
2307 /*----------------------------------------------------------------------*/
2308 /* Customized free WAIS object routines: */
2309 /* */
2310 /* This set of procedures is for applications to free a WAIS object */
2311 /* which was made with makeWAISFOO. */
2312 /* Each procedure frees only the memory that was allocated in its */
2313 /* associated makeWAISFOO routine, thus it's not necessary for the */
2314 /* caller to assign nulls to the pointer fields of the WAIS object. */
2315 /*----------------------------------------------------------------------*/
2316
2317 void CSTFreeWAISInitResponse(WAISInitResponse *init)
2318 /* free an object made with makeWAISInitResponse */
2319 {
2320 s_free(init);
2321 }
2322
2323 /*----------------------------------------------------------------------*/
2324
2325 void CSTFreeWAISSearch(WAISSearch *query)
2326 /* destroy an object made with makeWAISSearch() */
2327 {
2328 s_free(query);
2329 }
2330
2331 /*----------------------------------------------------------------------*/
2332
2333 void CSTFreeDocObj(DocObj *doc)
2334 /* free a docObj */
2335 {
2336 s_free(doc);
2337 }
2338
2339 /*----------------------------------------------------------------------*/
2340
2341 void CSTFreeWAISDocumentHeader(WAISDocumentHeader *header)
2342 {
2343 s_free(header);
2344 }
2345
2346 /*----------------------------------------------------------------------*/
2347
2348 void CSTFreeWAISDocumentShortHeader(WAISDocumentShortHeader *header)
2349 {
2350 s_free(header);
2351 }
2352
2353 /*----------------------------------------------------------------------*/
2354
2355 void CSTFreeWAISDocumentLongHeader(WAISDocumentLongHeader *header)
2356 {
2357 s_free(header);
2358 }
2359
2360 /*----------------------------------------------------------------------*/
2361
2362 void CSTFreeWAISSearchResponse(WAISSearchResponse * response)
2363 {
2364 s_free(response);
2365 }
2366
2367 /*----------------------------------------------------------------------*/
2368
2369 void CSTFreeWAISDocumentText(WAISDocumentText *docText)
2370 {
2371 s_free(docText);
2372 }
2373
2374 /*----------------------------------------------------------------------*/
2375
2376 void CSTFreeWAISDocHeadlines(WAISDocumentHeadlines *docHeadline)
2377 {
2378 s_free(docHeadline);
2379 }
2380
2381 /*----------------------------------------------------------------------*/
2382
2383 void CSTFreeWAISDocumentCodes(WAISDocumentCodes *docCodes)
2384 {
2385 s_free(docCodes);
2386 }
2387
2388 /*----------------------------------------------------------------------*/
2389
2390 void CSTFreeWAISTextQuery(any *query)
2391 {
2392 freeAny(query);
2393 }
2394
2395 /*----------------------------------------------------------------------*/
2396
2397 /*
2398 * Routines originally from WMessage.c -- FM
2399 *
2400 *----------------------------------------------------------------------*/
2401 /* WIDE AREA INFORMATION SERVER SOFTWARE
2402 * No guarantees or restrictions. See the readme file for the full standard
2403 * disclaimer.
2404 * 3.26.90
2405 */
2406
2407 /* This file is for reading and writing the wais packet header.
2408 * [email protected]
2409 */
2410
2411 /* to do:
2412 * add check sum
2413 * what do you do when checksum is wrong?
2414 */
2415
2416 /*---------------------------------------------------------------------*/
2417
2418 void readWAISPacketHeader(char *msgBuffer,
2419 WAISMessage * header_struct)
2420 {
2421 /* msgBuffer is a string containing at least HEADER_LENGTH bytes. */
2422
2423 memmove(header_struct->msg_len, msgBuffer, (size_t) 10);
2424 header_struct->msg_type = char_downcase((unsigned long) msgBuffer[10]);
2425 header_struct->hdr_vers = char_downcase((unsigned long) msgBuffer[11]);
2426 memmove(header_struct->server, (void *) (msgBuffer + 12), (size_t) 10);
2427 header_struct->compression = char_downcase((unsigned long) msgBuffer[22]);
2428 header_struct->encoding = char_downcase((unsigned long) msgBuffer[23]);
2429 header_struct->msg_checksum = char_downcase((unsigned long) msgBuffer[24]);
2430 }
2431
2432 /*---------------------------------------------------------------------*/
2433
2434 /* this modifies the header argument. See wais-message.h for the different
2435 * options for the arguments.
2436 */
2437
2438 void writeWAISPacketHeader(char *header,
2439 long dataLen,
2440 long type,
2441 char *server,
2442 long compression,
2443 long encoding,
2444 long version)
2445 /* Puts together the new wais before-the-z39-packet header. */
2446 {
2447 char lengthBuf[11];
2448 char serverBuf[11];
2449
2450 long serverLen = strlen(server);
2451
2452 if (serverLen > 10)
2453 serverLen = 10;
2454
2455 sprintf(lengthBuf, "%010ld", dataLen);
2456 StrNCpy(header, lengthBuf, 10);
2457
2458 header[10] = type & 0xFF;
2459 header[11] = version & 0xFF;
2460
2461 StrNCpy(serverBuf, server, serverLen);
2462 StrNCpy((char *) (header + 12), serverBuf, serverLen);
2463
2464 header[22] = compression & 0xFF;
2465 header[23] = encoding & 0xFF;
2466 header[24] = '0'; /* checkSum(header + HEADER_LENGTH,dataLen); XXX the result must be ascii */
2467 }
2468
2469 /*---------------------------------------------------------------------*/