home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / WAIS / ir / wutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-02  |  46.8 KB  |  1,878 lines

  1. /*--------------------------------------------------------------------------
  2.  * ABSTRACT:    WAIS_TOOLS
  3.  *
  4.  * AUTHOR
  5.  *   M. Tracy Shen
  6.  *
  7.  *
  8.  *------------------------------------------------------------------------*/
  9.  
  10. /* Change log:
  11.  * ported to Unix and SaberC -brewster 7/7/90
  12.  * added in bug fixes -Tracy 11/14/90
  13.  */
  14.  
  15. /*  
  16.  * INCLUDE EXTERNAL CONSTANTS                                         
  17.  */
  18.  
  19. #include "futil.h"
  20. #include <ctype.h>
  21. #include "zprot.h"
  22. #include "zutil.h"
  23. #include "wprot.h"
  24. #include <string.h>
  25. #include "wutil.h"
  26.  
  27.  
  28. /*  
  29.  * DEFINE LOCAL CONSTANTS
  30.  */
  31.  
  32.  
  33. #define CARRIAGE_RETURN 13
  34. #define LINE_FEED 10
  35. #define  ESC 27
  36. #define  DC1 17
  37. #define  DC3 19
  38. #define  ETS  3
  39.  
  40. #define FF  12
  41.  
  42.  
  43. #define INIT_CMD   1
  44. #define SEARCH_CMD 2
  45. #define EXTRACT_CMD 3
  46. #define STOP_CMD    4
  47.  
  48. #define MAX_CMD 4  /* init, query search, extract story, stop */
  49. #define MAX_BUFFER_LENGTH 10000 /* kludge -brewster */
  50.  
  51. /*
  52.  * DEFINE MACROS
  53.  */
  54.  
  55. #ifndef MIN
  56. #define MIN(a,b)  (a > b ? b : a)
  57. #endif
  58.  
  59. #define BIT(n)  (1L << n)
  60.  
  61. /*
  62.  * define global variables
  63.  */
  64.  
  65.  /* WAIS element names */
  66.     char *elm_name_tbl[] = {  ES_DocumentHeader
  67.                     ,ES_DocumentShortHeader
  68.                     ,ES_DocumentLongHeader
  69.                     ,ES_DocumentText
  70.                     ,ES_DocumentHeadline
  71.                     ,ES_DocumentCodes
  72.                     };
  73.  
  74. #ifdef TEST
  75.  
  76. twais_tmplt_typ1_srch_apdu _AP((char* buff,long* buff_len));
  77. twais_tmplt_typ3_srch_apdu _AP((char* buff,long* buff_len));
  78. dsply_doc_hdr_record _AP((WAISDocumentHeader* record));
  79. dsply_short_hdr_record _AP((WAISDocumentHeader* record));
  80.  
  81. main()
  82. {
  83.   char cmd[4096];
  84.   long cmd_len;
  85.  
  86.   printf("\n\n test template request apdu\n\n");
  87.  
  88.   do 
  89.     twais_format_req_apdu( TRUE, cmd, &cmd_len);
  90.   while ( cmd_len > 0 );
  91.  
  92.     
  93.  
  94.   printf("\n\n test formating request apdu\n\n");
  95.   do 
  96.     twais_format_req_apdu( FALSE, cmd, &cmd_len);
  97.   while ( cmd_len > 0 );
  98.  
  99.  
  100.   printf("\n\n test display init response apdu\n\n");
  101.  
  102.   twais_tmplt_init_rsp_apdu(  cmd, &cmd_len);
  103.   twais_dsply_rsp_apdu( cmd, cmd_len);
  104.  
  105.   printf("\n\n test display type3 search response apdu\n\n");
  106.   twais_tmplt_typ3_srch_rsp_apdu(  cmd, &cmd_len);
  107.   twais_dsply_rsp_apdu( cmd, cmd_len);
  108.  
  109.   printf("\n\n test display type1 search response apdu\n\n");
  110.   twais_tmplt_typ1_stry_rsp_apdu( cmd, &cmd_len);
  111.   twais_dsply_rsp_apdu( cmd, cmd_len);
  112.  
  113. }                /* main */
  114.  
  115. #endif
  116.  
  117.  
  118. static void get_int_item _AP((char* itm_name, long *itm_val));
  119.  
  120. static void
  121. get_int_item(itm_name, itm_val)
  122. char *itm_name;
  123. long  *itm_val;
  124. {
  125.   printf("    %s (integer): ", itm_name);
  126.   scanf("%ld", itm_val);
  127. }
  128.  
  129. static void get_str_item _AP((char* itm_name,char*  itm_val));
  130.  
  131. static void
  132. get_str_item(itm_name, itm_val)
  133. char *itm_name;
  134. char *itm_val;
  135. {
  136.   printf("    %s (string): ", itm_name);
  137.   scanf("%s", itm_val);
  138. }
  139.  
  140. static void get_line_item _AP((char* itm_name,char*  itm_val));
  141.  
  142. static void
  143. get_line_item(itm_name, itm_val)
  144. char *itm_name;
  145. char *itm_val;
  146. {
  147.    
  148.   printf("    %s (one line string):\n", itm_name);
  149.   gets( itm_val);
  150.   if ( itm_val[0] == 0 )
  151.     gets(itm_val);
  152. }
  153.  
  154. static void get_req_type _AP((long* req_type));
  155.  
  156. static void
  157. get_req_type( req_type)
  158. long *req_type;
  159. {
  160.  
  161.   printf("\n\n");
  162.   printf(" request type 1)init  2)query search 3)extract story 4)stop: ");
  163.   scanf("%ld", req_type);
  164.  
  165. }                /* get_req_type */
  166.  
  167. /*
  168.  * "twais_format_req_apdu"  This function gets request message.  It reads the
  169.  *   request message into the the specified buffer.
  170.  *   It returns the the length of the data 
  171.  *   written into the buffer as an output parameter.  
  172.  *   A zero length value indicates "end session".
  173.  *
  174.  * return values:
  175.  *  none
  176.  */
  177. void 
  178. twais_format_req_apdu(use_template,apdu_buff,len)
  179. boolean use_template;
  180. char* apdu_buff;
  181. long* len;
  182. /* use_template :  (I) flag indicating whether use template apdu */
  183. /* apdu_buff    :  (O) buffer to put apdu */
  184. /* len          :  (O) number of bytes written to the buffer */
  185. {
  186.   long req_type;
  187.  
  188.   do { 
  189.  
  190.     get_req_type( &req_type);
  191.  
  192.     switch (req_type) {
  193.     case INIT_CMD: 
  194.       *len = twais_format_init_apdu( use_template, apdu_buff);
  195.       break;
  196.     case SEARCH_CMD: 
  197.       *len = twais_format_typ3_srch_apdu( use_template, apdu_buff);
  198.       break;
  199.     case EXTRACT_CMD: 
  200.       *len = twais_format_typ1_srch_apdu( use_template,  apdu_buff);
  201.       break;
  202.     case STOP_CMD: 
  203.       *len = 0;
  204.       break;
  205.     default: 
  206.       break;
  207.     
  208.     }                /* end switch */
  209.   }  
  210.   while ( req_type < 1 || req_type > MAX_CMD );
  211.  
  212. }                /* twais_format_req_apdu */
  213.  
  214.  
  215.  
  216. /*
  217.  * "twais_format_init_apdu"
  218.  *   prompt user information to format an init apdu and 
  219.  *   write the apdu to the specified buffer
  220.  *
  221.  * function return:
  222.  *   number of bytes written to the buffer 
  223.  */
  224. long 
  225. twais_format_init_apdu(use_template,apdu_buff)
  226. boolean use_template;
  227. char* apdu_buff;
  228.  
  229. /* use_template :  (I) flag indicating whether use template apdu */
  230. /* apdu_buff    :  (O) buffer to put apdu */
  231. {
  232.   InitAPDU *init1;
  233.   char  *end_ptr;
  234.   long len;
  235.   long pref_msg_size;
  236.   long max_rec_size;
  237.   any refID;
  238.   char ref_id[128];
  239.  
  240.   if ( use_template ) {
  241.     twais_tmplt_init_apdu(apdu_buff, &len);
  242.     return( len);
  243.   }      
  244.  
  245.   get_int_item( "prefered message size", &pref_msg_size);
  246.   get_int_item( "maximum record size", &max_rec_size);
  247.  
  248.   get_str_item("reference id", ref_id);
  249.   refID.size = strlen(ref_id);
  250.   refID.bytes = &ref_id[0];
  251.  
  252.   init1 = makeInitAPDU(WILL_USE,WILL_NOT_USE,WILL_NOT_USE,WILL_NOT_SUPPORT,
  253.                        WILL_NOT_SUPPORT,
  254.                        pref_msg_size,
  255.                        max_rec_size, NULL,
  256.                        defaultImplementationID(),defaultImplementationName(),
  257.                        defaultImplementationVersion(), &refID,NULL);
  258.  
  259.   { long buffer_len = MAX_BUFFER_LENGTH;
  260.     end_ptr = writeInitAPDU(init1, apdu_buff, &buffer_len);
  261.   }
  262.   freeInitAPDU(init1);
  263.   len = (long)end_ptr - (long)apdu_buff;
  264.   return(len);  
  265. }                /* twais_format_init_apdu */
  266.  
  267. /* 
  268.  * "twais_format_typ3_srch_apdu"  
  269.  *  prompt user information to form a relevance feedback search apdu and
  270.  *  write the apdu to the specified buffer
  271.  *
  272.  * function return:
  273.  *   number of bytes written to the buffer 
  274.  */
  275. long 
  276. twais_format_typ3_srch_apdu(use_template,apdu_buff)
  277. boolean use_template;
  278. char* apdu_buff;
  279.  
  280. /* use_template :  (I) flag indicating whether use template apdu */
  281. /* apdu_buff    :  (O) buffer to put apdu */
  282. {
  283.   SearchAPDU *search;
  284.   char  *end_ptr;
  285.   long len;
  286.   long small, large, medium;
  287.  
  288.   char temp_string[80];
  289.  
  290.   long num_databases, num_elmsets, i;
  291.   char **database_names_ptr;
  292.   char *database_names[11];
  293.   char database_name[10][129];
  294.   long  elm_type;
  295.   char *elm_names[21];
  296.   char elm_name[20][128];
  297.   char **elm_names_ptr = &elm_names[0];
  298.  
  299.   any refID;
  300.   char ref_id[256];
  301.   char seed_words[257];
  302.  
  303.   DocObj *DocObjs[5];
  304.  
  305.   DocObj **DocObjsPtr = 0;
  306.   long DateFactor;
  307.   char BeginDateRange[9], EndDateRange[9];
  308.   long maxDocsRetrieved;
  309.   long num, sw;
  310.   char doc_id[4][200];
  311.   any docID[4];
  312.   long ChunkCode;
  313.   any Start[4], End[4];   
  314.   char start_pos[4][10], end_pos[4][10];
  315.   long start_ipos, end_ipos;
  316.   WAISSearch *query;
  317.   long count;
  318.   for(count = 0; count < 5; count++){
  319.     DocObjs[count] = 0;        /* added by brewster */
  320.   }
  321.  
  322.   if ( use_template ) {
  323.     twais_tmplt_typ3_srch_apdu(apdu_buff, &len);
  324.     return( len);
  325.   }      
  326.  
  327.  
  328.   get_int_item("small set upper bound", &small);
  329.   get_int_item("large set lower bound", &large);
  330.   get_int_item("medium set present number", &medium);
  331.  
  332.   get_int_item("number of databases(max 10, 0 search entire databases)", &num_databases);
  333.  
  334.   if ( num_databases == 0 ) {
  335.     database_names_ptr = 0;
  336.   }
  337.  
  338.   else {
  339.     database_names_ptr = &database_names[0];
  340.     database_names[num_databases] = 0;  
  341.     for ( i=0; i < num_databases; i++ ) {
  342.       database_names[i] = &database_name[i][0];
  343.       sprintf( temp_string,"database name %ld", (i+1) );
  344.       get_str_item(temp_string, database_name[i]);
  345.     }
  346.   }
  347.  
  348.   get_int_item("number of database-element_set pairs(max 10)", &num_elmsets);
  349.  
  350.   if ( num_elmsets == 0 ) 
  351.     elm_names_ptr = 0;
  352.   else {
  353.     elm_names[num_elmsets * 2 ] = 0;
  354.     for ( i=0; i < num_elmsets; i++ ) {
  355.       elm_names[i*2] = &elm_name[i*2][0];
  356.       sprintf( temp_string,"database name %ld", (i+1) );
  357.       get_str_item(temp_string, &elm_name[i*2][0]);
  358.  
  359.       get_int_item("  element type 0)default 1)DocHeaders 2)ShortHeaders\n        3)LongHeaders 4)Text 5)HeadLines 6)Codes", 
  360.            &elm_type);
  361.       if ( (elm_type < 1) || (elm_type > 6) )
  362.     elm_type = 1;
  363.       elm_names[i*2+1] = &elm_name[i*2+1][0];
  364.       strcpy( &elm_name[i*2+1][0], elm_name_tbl[elm_type-1]);
  365.     }
  366.   }
  367.  
  368.  
  369.   get_str_item("reference id", ref_id);
  370.   refID.size = strlen(ref_id);
  371.   refID.bytes = &ref_id[0];
  372.  
  373.   /*
  374.    * format wais search query
  375.    */
  376.   /* fill in the user information */
  377.  
  378.   get_line_item("seed_words", seed_words);
  379.   get_int_item("number of relevance feedback documents(max 4)", &num);
  380.   if ( num > 0 ) {
  381.  
  382.     if ( num > 4 )
  383.       num = 4;
  384.     DocObjsPtr = &DocObjs[0];
  385.     for ( i=0; i< num; i++) {
  386.       docID[i].bytes = &doc_id[i][0];
  387.       sprintf(temp_string, "document id %ld", (i+1));
  388.       get_str_item(temp_string, &doc_id[i][0]);
  389.       docID[i].size = strlen( &doc_id[i][0]);
  390.       get_int_item("  (1)whole story (2)part story", &sw);
  391.       if ( sw == 1) {
  392.     DocObjs[i] = makeDocObjUsingWholeDocument( &docID[i],NULL);
  393.       }
  394.       else {
  395.     get_int_item("  ChunkCode (1)byte (2)line (3)parag", &ChunkCode);
  396.     if ( ChunkCode == 1 ) {
  397.       get_int_item("  start position", &start_ipos);
  398.       get_int_item("  end position", &end_ipos);
  399.       DocObjs[i] = makeDocObjUsingBytes( &docID[i], NULL,
  400.                         start_ipos, end_ipos);
  401.     }
  402.     else if ( ChunkCode == 2 ) {
  403.       get_int_item("  start position", &start_ipos);
  404.       get_int_item("  end position", &end_ipos);
  405.       DocObjs[i] = makeDocObjUsingLines( &docID[i], NULL,
  406.                         start_ipos, end_ipos);
  407.     }
  408.     else {
  409.       get_str_item("  start position", &start_pos[i][0]);
  410.       get_str_item("  end position", &end_pos[i][0]);
  411.       Start[i].size = strlen( &start_pos[i][0]);
  412.       Start[i].bytes = &start_pos[i][0];
  413.       End[i].size = strlen( &end_pos[i][0]);
  414.       End[i].bytes = &end_pos[i][0];
  415.       DocObjs[i] = makeDocObjUsingParagraphs( &docID[i], NULL,
  416.                          &Start[i], &End[i]);
  417.     }
  418.       }                /* end if-else */
  419.     }                /* end for */
  420.   }                /* end if */
  421.    
  422.   get_int_item("DateFactor (1-independent,2-later,3-earlier,4-range)", &DateFactor);
  423.   if ( DateFactor == 2 || DateFactor == 4) 
  424.     get_str_item("begin date  (yyyymmdd)", BeginDateRange);
  425.   else
  426.     BeginDateRange[0] = '\0';
  427.   if ( DateFactor == 3 || DateFactor == 4) 
  428.     get_str_item("end date (yyyymmdd)", EndDateRange);
  429.   else
  430.     EndDateRange[0] = '\0';
  431.  
  432.   get_int_item("max. documents retrieved", &maxDocsRetrieved);
  433.  
  434.   query = makeWAISSearch( seed_words, DocObjsPtr, 0L,
  435.              DateFactor, BeginDateRange, EndDateRange,
  436.              maxDocsRetrieved);
  437.  
  438.   search = makeSearchAPDU( small, large, medium, 
  439.               1L,    /* replace indicator */
  440.               "FOO", /* result set name */
  441.               database_names_ptr, /* database name */   
  442.               QT_RelevanceFeedbackQuery, /* query_type */
  443.               elm_names_ptr, /* element name */
  444.               &refID, query);
  445.  
  446.   {
  447.     long buffer_len = MAX_BUFFER_LENGTH;
  448.     end_ptr = writeSearchAPDU(search, apdu_buff, &buffer_len);
  449.   }
  450.   len = (long)end_ptr - (long)apdu_buff;
  451.  
  452.   i =0;
  453.   while ( DocObjs[i] != 0 ) {
  454.     CSTFreeDocObj( DocObjs[i]);
  455.     i++;
  456.   }
  457.  
  458.   CSTFreeWAISSearch(query);
  459.  
  460.   freeSearchAPDU(search);
  461.  
  462.   return(len);
  463.  
  464. }                /* twais_format_typ3_srch_apdu */
  465.  
  466. /* 
  467.  * "twais_format_typ1_srch_apdu"  
  468.  *  prompt user information to form a text retrieval search apdu and
  469.  *  write the apdu to the specified buffer
  470.  *
  471.  * function return:
  472.  *   number of bytes written to the buffer 
  473.  */
  474. long
  475. twais_format_typ1_srch_apdu(use_template,apdu_buff)
  476. boolean use_template;
  477. char* apdu_buff;
  478.  
  479. /* use_template :  (I) flag indicating whether use template apdu */
  480. /* apdu_buff    :  (O) buffer to put apdu */
  481. {
  482.   SearchAPDU *search;
  483.   char  *end_ptr;
  484.   long len;
  485.   long small, large, medium;
  486.  
  487.   char temp_string[80];
  488.  
  489.   long num_databases, num_elmsets, i;
  490.   char **database_names_ptr;
  491.   char *database_names[11];
  492.   char database_name[10][129];
  493.   long  elm_type;
  494.   char *elm_names[21];
  495.   char elm_name[20][128];
  496.   char **elm_names_ptr = &elm_names[0];
  497.  
  498.   any refID;
  499.   char ref_id[256];
  500.  
  501.   any *query;            /* changed by brewster from char * */
  502.   DocObj *DocObjs[11];
  503.   long num_doc;
  504.   long sw;
  505.   char doc_id[10][200];
  506.   any docID[10];
  507.   long ChunkCode;
  508.   any Start[10], End[10];   
  509.   long start_ipos, end_ipos;
  510.   char start_pos[10][10], end_pos[10][10];
  511.  
  512.   long use_text = 0;
  513.  
  514.  
  515.   if ( use_template ) {
  516.     twais_tmplt_typ1_srch_apdu(apdu_buff, &len);
  517.     return( len);
  518.   }      
  519.  
  520.  
  521.   get_int_item("small set upper bound", &small);
  522.   get_int_item("large set lower bound", &large);
  523.   get_int_item("medium set present number", &medium);
  524.  
  525.   get_int_item("number of databases(max 10, 0 search entire databases)", &num_databases);
  526.  
  527.   if ( num_databases == 0 ) {
  528.     database_names_ptr = 0;
  529.   }
  530.   else {
  531.     database_names_ptr = &database_names[0];
  532.     database_names[num_databases] = 0;  
  533.     for ( i=0; i < num_databases; i++ ) {
  534.       database_names[i] = &database_name[i][0];
  535.       sprintf(temp_string, "database name %ld", (i+1));
  536.       get_str_item( temp_string, database_name[i]);
  537.     }
  538.   }
  539.  
  540.   get_int_item("number of database-element_set pairs(max 10)", &num_elmsets);
  541.  
  542.   if ( num_elmsets == 0 ) 
  543.     elm_names_ptr = 0;
  544.   else {
  545.     elm_names[num_elmsets * 2 ] = 0;
  546.     for ( i=0; i < num_elmsets; i++ ) {
  547.       elm_names[i*2] = &elm_name[i*2][0];
  548.       sprintf( temp_string,"database name %ld", (i+1) );
  549.       get_str_item(temp_string, &elm_name[i*2][0]);
  550.  
  551.       get_int_item("  element type 0)default 1)DocHeaders 2)ShortHeaders\n        3)LongHeaders 4)Text 5)HeadLines 6)Codes", 
  552.            &elm_type);
  553.       if ( (elm_type < 1) || (elm_type > 6) )
  554.     elm_type = 1;
  555.       if ( elm_type == 4)  use_text++;
  556.       elm_names[i*2+1] = &elm_name[i*2+1][0];
  557.       strcpy( &elm_name[i*2+1][0], elm_name_tbl[elm_type-1]);
  558.     }
  559.   }
  560.  
  561.   get_str_item("reference id", ref_id);
  562.   refID.size = strlen(ref_id);
  563.   refID.bytes = &ref_id[0];
  564.  
  565.   /*
  566.    * format type 1 query
  567.    */
  568.   get_int_item("num of documents to retrieve (max 10)", &num_doc);
  569.   if ( num_doc > 10 ) num_doc = 10;
  570.   if ( num_doc < 1 ) num_doc = 1;
  571.  
  572.   DocObjs[num_doc] = 0;
  573.  
  574.   for ( i=0; i < num_doc; i++ ) {
  575.     docID[i].bytes = &doc_id[i][0];
  576.  
  577.     sprintf( temp_string, "document id %ld", (i+1));
  578.     get_str_item(temp_string, &doc_id[i][0]);
  579.     docID[i].size = strlen( &doc_id[i][0]);
  580.         
  581.     if ( use_text == 0 )
  582.       DocObjs[i] = makeDocObjUsingWholeDocument( &docID[i],NULL);
  583.     else {         
  584.       get_int_item("  (1)whole story (2)part story", &sw);
  585.       if ( sw == 1) {
  586.     DocObjs[i] = makeDocObjUsingWholeDocument( &docID[i],NULL);
  587.       }
  588.       else {
  589.     get_int_item("  ChunkCode (1)byte (2)line (3)parag", &ChunkCode);
  590.     if ( ChunkCode == 1 ) {
  591.       get_int_item("  start position", &start_ipos);
  592.       get_int_item("  end position", &end_ipos);
  593.       DocObjs[i] = makeDocObjUsingBytes( &docID[i], NULL,
  594.                         start_ipos, end_ipos);
  595.     }
  596.     else if ( ChunkCode == 2 ) {
  597.       get_int_item("  start position", &start_ipos);
  598.       get_int_item("  end position", &end_ipos);
  599.       DocObjs[i] = makeDocObjUsingLines( &docID[i], NULL,
  600.                         start_ipos, end_ipos);
  601.     }
  602.     else {
  603.       get_str_item("  start position", &start_pos[i][0]);
  604.       get_str_item("  end position", &end_pos[i][0]);
  605.       Start[i].size = strlen( &start_pos[i][0]);
  606.       Start[i].bytes = &start_pos[i][0];
  607.       End[i].size = strlen( &end_pos[i][0]);
  608.       End[i].bytes = &end_pos[i][0];
  609.       DocObjs[i] = makeDocObjUsingParagraphs( &docID[i], NULL,
  610.                          &Start[i], &End[i]);
  611.     }
  612.       }                /* end if-else */
  613.     }
  614.   }
  615.  
  616.   query = makeWAISTextQuery(DocObjs);   
  617.  
  618.  
  619.   search = makeSearchAPDU( small, large, medium, 
  620.               1L,    /* replace indicator */
  621.               "FOO", /* result set name */
  622.               database_names_ptr, /* database name */   
  623.               QT_TextRetrievalQuery, /* query_type */
  624.               elm_names_ptr, /* element name */
  625.               &refID, query);
  626.  
  627.   {
  628.     long buffer_len = MAX_BUFFER_LENGTH;
  629.     end_ptr = writeSearchAPDU(search, apdu_buff, &buffer_len);
  630.   }
  631.   len = (long)end_ptr - (long)apdu_buff;
  632.  
  633.   i =0;
  634.   while ( DocObjs[i] != 0) {
  635.     CSTFreeDocObj( DocObjs[i]);
  636.     i++;
  637.   }
  638.  
  639.   CSTFreeWAISTextQuery( query);
  640.  
  641.   freeSearchAPDU(search);
  642.  
  643.   return(len);
  644.  
  645. }                /* twais_format_typ1_srch_apdu */
  646.  
  647. static void print_hdline _AP((char* hdline));
  648.  
  649. static void print_hdline(hdline)
  650. char *hdline;
  651. {
  652.   char buf[301];
  653.   long len, i;
  654.  
  655.   len = strlen(hdline);
  656.   if (len > 300 )
  657.     len =300;
  658.   for ( i=0; i< len; i++ )
  659.     if ( hdline[i] > 31 && hdline[i] < 127 )
  660.       buf[i] = hdline[i];
  661.     else
  662.       buf[i] = '@';         
  663.   buf[i] = 0;
  664.   printf("     Headline:  %s\n", buf);
  665. }
  666.  
  667.  
  668. static void print_any _AP((char* title,any*  any_ptr));
  669.  
  670. static void print_any( title, any_ptr)
  671. char *title;
  672. any *any_ptr;
  673. {
  674.   long i;
  675.   printf("%s", title);
  676.   if ( any_ptr ) {
  677.     for ( i=0; i < any_ptr->size; i++)
  678.      { if (isprint(any_ptr->bytes[i]))
  679.      printf("%c", any_ptr->bytes[i]);
  680.        else
  681.      printf("%ld", (long)any_ptr->bytes[i]);
  682.      }
  683.     printf("\n");
  684.   } 
  685. }
  686.  
  687.  
  688.   
  689. static void dsply_long_hdr_record _AP((WAISDocumentLongHeader* record));
  690.  
  691. static void dsply_long_hdr_record(record)
  692. WAISDocumentLongHeader *record;
  693. /*
  694.  * 'dsply_long_hdr_record'
  695.  *   display one WAIS long header  record 
  696.  */
  697. {
  698.   printf(" LongHeaders\n");
  699.  
  700.   print_any("     DocumentID:  ", record->DocumentID);
  701.  
  702.   printf("     VersionNumber:  %ld\n", record->VersionNumber);
  703.  
  704.   printf("     Score:  %ld,  BestMatch:  %ld\n", 
  705.      record->Score,
  706.      record->BestMatch);
  707.  
  708.   printf("     DocumentLength:  %ld,  Lines:  %ld\n", 
  709.      record->DocumentLength,
  710.      record->Lines);
  711.  
  712.   if ( record->Source )
  713.     printf("     Source:  %s\n", record->Source);
  714.  
  715.   if ( record->Date )
  716.     printf("     Date:  %s\n", record->Date);
  717.  
  718.   if ( record->OriginCity)
  719.     printf("     OriginCity:  %s\n", record->OriginCity);
  720.  
  721.   if ( record->Headline)
  722. #ifdef FOR_DUM_TERM
  723.     print_hdline(record->Headline);
  724. #else
  725.   printf("     Headline:  %s\n", record->Headline);
  726. #endif
  727.  
  728.   if ( record->StockCodes)
  729.     printf("     StockCodes:  %s", record->StockCodes);
  730.  
  731.   if ( record->CompanyCodes )
  732.     printf("     CompanyCodes:  %s", record->CompanyCodes);
  733.  
  734.   if ( record->IndustryCodes)
  735.     printf("     IndustryCodes:  %s", record->IndustryCodes);
  736.  
  737.   printf("\n");
  738. }
  739. /*
  740.  * dsply_text
  741.  *  display story text
  742.  */
  743. #define TEXT_LINES_PER_PAGE 18
  744. #define  DQ_PAR_LEN   3
  745. #define  DQ_EOL_LEN   1
  746. #define  DQ_MAX_LINE_LEN  256
  747.  
  748. #define  PRINT_LINE(line, ndx, line_cnt,text_ptr,last_ptr,continue_viewing)\
  749.          line[ndx] = NULL;\
  750.          line_cnt++; \
  751.          printf("%s\n", line);\
  752.          ndx = 0;\
  753.          if ( (line_cnt == TEXT_LINES_PER_PAGE) && (text_ptr <= last_ptr)) {\
  754.             line_cnt = 0;\
  755.             printf("\n  ... more to come, enter 1 to continue or 0 to stop: ");\
  756.             scanf("%ld", &continue_viewing);\
  757.             }
  758.  
  759. #if 0
  760. /*
  761.  * try to format text
  762.  */
  763. dsply_text( size, text)
  764. long size;
  765. char *text;
  766. {
  767.   char  line[DQ_MAX_LINE_LEN +1];
  768.   char *last_ptr;
  769.   char *text_ptr;
  770.   long  ndx;
  771.   long line_cnt = 0;
  772.   long continue_viewing = 1;
  773.     
  774.   text_ptr = text;
  775.  
  776.   last_ptr = text_ptr + size;
  777.  
  778.   while ( (*text_ptr != ETS) && (text_ptr < last_ptr) ) {
  779.     if (  *text_ptr++ == ESC ) {
  780.       if (*text_ptr++ == 'k') 
  781.     break;
  782.     }
  783.   }
  784.  
  785.   if ( text_ptr >= last_ptr ) {
  786.     printf("**** ERROR in display text -- could not find text\n");
  787.     return;
  788.   }
  789.  
  790.   ndx = 0;
  791.  
  792.   while ( (continue_viewing == 1) && ( *text_ptr != ETS) &&
  793.      (text_ptr < last_ptr) ) {
  794.  
  795.     /* paragraph id -- skip */
  796.     if ( *text_ptr == ESC) {
  797.       text_ptr++;
  798.       if ( *text_ptr == 'l' )
  799.     text_ptr++;
  800.       if ( ndx > 0 ) {
  801.     PRINT_LINE(line, ndx,line_cnt,text_ptr,last_ptr,continue_viewing);  
  802.       }
  803.       ndx = DQ_PAR_LEN;
  804.       strncpy( line, text_ptr, ndx);
  805.       text_ptr += DQ_PAR_LEN;
  806.       PRINT_LINE(line, ndx,line_cnt,text_ptr,last_ptr,continue_viewing);  
  807.     }
  808.  
  809.     /* highlight & dehighlight markers -- skip */
  810.     else if ( (*text_ptr == DC1) || (*text_ptr == DC3) ) 
  811.       text_ptr++;
  812.  
  813.     /* CR -- skip and print */
  814.     else if ( *text_ptr == CARRIAGE_RETURN ) {
  815.       text_ptr += DQ_EOL_LEN;    /* CR, LF */
  816.       PRINT_LINE(line, ndx, line_cnt,text_ptr,last_ptr,continue_viewing);
  817.     }
  818.  
  819.     else {
  820.       line[ndx++] = *text_ptr++;
  821.       if ( ndx > DQ_MAX_LINE_LEN ) {
  822.     printf("**** ERROR in display text -- line too long %ld\n", ndx);
  823.     return;
  824.       }
  825.     }
  826.   }                /* end while */
  827.  
  828.   if ( text_ptr >= last_ptr ) {
  829.     printf("**** ERROR in display text -- could not find End Of Text\n");
  830.     return;
  831.   }
  832.  
  833.   if ( ndx > 0 ) {
  834.     line[ndx] = NULL;
  835.     line_cnt++; 
  836.     printf("%s\n", line);
  837.   }
  838.  
  839.  
  840. }                /* dsply_text */
  841. #endif
  842.  
  843. static void dsply_text _AP((long size,char* text));
  844.  
  845. static void dsply_text( size, text)
  846. long size;
  847. char *text;
  848. {
  849.   char *text_ptr, *last_ptr;
  850.   long line_cnt = 0;
  851.   long continue_viewing = 1;
  852.   char buff[3200];
  853.   long len;
  854.   char *buff_start, *buff_ptr;
  855.     
  856.   text_ptr = text;
  857.  
  858.   buff_start = &buff[0];    
  859.   buff_ptr = buff_start;
  860.   
  861.   last_ptr = text_ptr + size;
  862.  
  863.   while ( (text_ptr <= last_ptr) && (continue_viewing == 1) ) {
  864.     /* control markers */
  865.     if (  *text_ptr == ESC ) {
  866.       text_ptr++;
  867.       text_ptr++;
  868.     }
  869.     /* highlight & dehighlight markers -- skip */
  870.     else if (*text_ptr == DC1 || *text_ptr == DC3)
  871.       text_ptr++;
  872.     else if (*text_ptr == ETS)
  873.       text_ptr++;
  874.     else if ( *text_ptr == CARRIAGE_RETURN ) {
  875.       text_ptr++;        /* CR */
  876.       *buff_ptr++= CARRIAGE_RETURN;
  877.       *buff_ptr++ = LINE_FEED;
  878.       line_cnt++;
  879.       if ( (line_cnt == TEXT_LINES_PER_PAGE) && 
  880.       (text_ptr <= last_ptr)) {
  881.     len = (long)buff_ptr - (long) buff_start;
  882.     fwrite( buff_start, len, 1, stdout);
  883.     line_cnt = 0;
  884.     buff_ptr = buff_start;
  885.     printf("\n  ... more to come, enter 1 to continue or 0 to stop: ");
  886.     scanf("%ld", &continue_viewing);
  887.       }
  888.     }
  889.     else 
  890.       *buff_ptr++ = *text_ptr++;
  891.   }                /* end while */
  892.  
  893.   if ( buff_ptr > buff_start) {
  894.     len = (long)buff_ptr - (long)buff_start;
  895.     fwrite( buff_start, len,  1, stdout);
  896.   }
  897.  
  898. }                /* dsply_text */
  899.  
  900. static void dsply_text_record _AP((WAISDocumentText*  record));
  901.  
  902. static void dsply_text_record( record)
  903. WAISDocumentText *record;
  904. /*
  905.  * 'dsply_text_record'
  906.  *   display one WAIS text record 
  907.  */
  908. {
  909.   printf(" Text\n");
  910.   print_any("     DocumentID:  ", record->DocumentID);
  911.  
  912.   printf("     VersionNumber:  %ld\n", record->VersionNumber);
  913.   dsply_text( record->DocumentText->size,
  914.          record->DocumentText->bytes);
  915. }
  916.  
  917. static void dsply_headline_record _AP((WAISDocumentHeadlines*  record));
  918.  
  919. static void dsply_headline_record( record)
  920. WAISDocumentHeadlines *record;
  921. /*
  922.  * 'dsply_headline_record'
  923.  *   display one WAIS headline record 
  924.  */
  925. {
  926.   printf(" Headlines\n");
  927.   print_any("     DocumentID:  ", record->DocumentID);
  928.  
  929.   printf("     VersionNumber:  %ld\n", record->VersionNumber);
  930.  
  931.   if ( record->Source )
  932.     printf("     Source:  %s\n", record->Source);
  933.  
  934.   if ( record->Date )
  935.     printf("     Date:  %s\n", record->Date);
  936.  
  937.   if ( record->OriginCity)
  938.     printf("     OriginCity:  %s\n", record->OriginCity);
  939.   print_hdline(record->Headline);
  940.  
  941.   if ( record->Headline)
  942. #ifdef FOR_DUM_TERM
  943.     print_hdline(record->Headline);
  944. #else
  945.   printf("     Headline:  %s\n", record->Headline);
  946. #endif
  947. }
  948.  
  949. static void dsply_code_record _AP((WAISDocumentCodes*  record));
  950.  
  951. static void dsply_code_record( record)
  952. WAISDocumentCodes  *record;
  953. /*
  954.  * 'dsply_code_record'
  955.  *   display one WAIS code record 
  956.  */
  957. {
  958.   printf(" Codes\n");
  959.   print_any("     DocumentID:  ", record->DocumentID);
  960.  
  961.   printf("     VersionNumber:  %ld\n", record->VersionNumber);
  962.  
  963.   if ( record->StockCodes)
  964.     printf("     StockCodes:  %s", record->StockCodes);
  965.  
  966.   if ( record->CompanyCodes )
  967.     printf("     CompanyCodes:  %s", record->CompanyCodes);
  968.  
  969.   if ( record->IndustryCodes)
  970.     printf("     IndustryCodes:  %s", record->IndustryCodes);
  971.  
  972.   printf("\n");
  973. }
  974.  
  975.  
  976. /*
  977.  * "twais_dsply_rsp_apdu"
  978.  *   display response apdu on stdout
  979.  *   the response apdu is in the specified buffer
  980.  */
  981. void
  982. twais_dsply_rsp_apdu(rsp_buff, rsp_len)
  983. char *rsp_buff;         /* (I) buffer contain response apdu */
  984. long  rsp_len;           /* (I) number of bytes in the buffer */
  985. {
  986.  
  987.   pdu_type pdu;
  988.   pdu = peekPDUType(rsp_buff);
  989.   switch ( pdu) {
  990.   case initResponseAPDU:
  991.     twais_dsply_init_rsp_apdu( rsp_buff);
  992.     break;
  993.   case searchResponseAPDU:
  994.     twais_dsply_srch_rsp_apdu( rsp_buff);
  995.     break;
  996.  
  997.   case initAPDU:
  998.     twais_dsply_init_apdu( rsp_buff);
  999.     break;
  1000.   case searchAPDU:
  1001.     twais_dsply_srch_apdu( rsp_buff);
  1002.     break;
  1003.   default:
  1004.     /* others not supported yet */
  1005.     break;
  1006.   }
  1007.  
  1008. }                /* twais_dsply_rsp_apdu */
  1009.  
  1010.  
  1011. /*
  1012.  * "twais_dsply_init_rsp_apdu"
  1013.  *   display init response apdu on stdout
  1014.  *   the response apdu is encoded in the specified buffer
  1015.  */
  1016. void
  1017. twais_dsply_init_rsp_apdu( buffer)
  1018. char *buffer;           /* (I) buffer contain the init response apdu */
  1019. {
  1020.  
  1021.   InitResponseAPDU *response;
  1022.   WAISInitResponse *info;
  1023.   long i, len;
  1024.  
  1025.   printf("\n\n Init Response:\n");
  1026.  
  1027.   readInitResponseAPDU(&response,buffer);
  1028.  
  1029.   printf("    willSearch: %ld,  willPresent: %ld,  willDelete: %ld\n", 
  1030.      response->willSearch, 
  1031.      response->willPresent,
  1032.      response->willDelete);
  1033.   printf("    supportAccessControl: %ld,  supportResourceControl: %ld\n",
  1034.      response->supportAccessControl,
  1035.      response->supportResourceControl);
  1036.   printf("    PreferredMessageSize: %ld,  MaximumRecordSize: %ld\n",
  1037.      response->PreferredMessageSize, 
  1038.      response->MaximumRecordSize);
  1039.   if ( response->ImplementationID != 0) {
  1040.     printf("    ImplementationID: %s\n", 
  1041.        response->ImplementationID);   
  1042.   }
  1043.   if ( response->ImplementationName != 0) {
  1044.     printf("    ImplementationName: %s\n",
  1045.        response->ImplementationName);   
  1046.   }
  1047.   if ( response->ImplementationVersion != 0) {
  1048.     printf("    ImplementationVersion: %s\n",
  1049.        response->ImplementationVersion);   
  1050.   }
  1051.   if ( response->ReferenceID != 0) {
  1052.     print_any("    ReferenceID: ", response->ReferenceID);
  1053.   }
  1054.  
  1055.   if ( response->UserInformationField != 0) {
  1056.     info = (WAISInitResponse *)response->UserInformationField;
  1057.     printf("    ChunkCode: %ld,  ChunkIDLength: %ld\n",
  1058.        info->ChunkCode,
  1059.        info->ChunkIDLength);
  1060.  
  1061.     printf("    ChunkMarker: ");
  1062.     len = strlen( info->ChunkMarker);
  1063.     for ( i=0; i< len; i++)
  1064.       printf("%ld, ", info->ChunkMarker[i]);
  1065.     printf("\n");
  1066.  
  1067.     printf("    HighlightMarker: ");
  1068.     len = strlen( info->HighlightMarker);
  1069.     for ( i=0; i< len; i++)
  1070.       printf("%ld, ", info->HighlightMarker[i]);
  1071.     printf("\n");
  1072.  
  1073.     printf("    DeHighlightMarker: ");
  1074.     len = strlen( info->DeHighlightMarker);
  1075.     for ( i=0; i< len; i++)
  1076.       printf("%ld, ", info->DeHighlightMarker[i]);
  1077.     printf("\n");
  1078.  
  1079.     printf("    NewlineCharacters: ");
  1080.     len = strlen( info->NewlineCharacters);
  1081.     for ( i=0; i< len; i++)
  1082.       printf("%ld, ", info->NewlineCharacters[i]);
  1083.     printf("\n");
  1084.  
  1085.   }
  1086.   freeInitResponseAPDU( response);
  1087.  
  1088. }                /* twais_dsply_init_rsp_apdu */
  1089.  
  1090. /*
  1091.  * "twais_dsply_init_apdu"
  1092.  *   display init apdu on stdout
  1093.  *   the apdu is in the specified buffer
  1094.  */
  1095. void
  1096. twais_dsply_init_apdu( buffer)
  1097. char *buffer;           /* (I) buffer contain the init response apdu */
  1098. {
  1099.  
  1100.   InitAPDU *init;
  1101.  
  1102.   printf("\n\n Init Request:\n");
  1103.  
  1104.   readInitAPDU(&init,buffer);
  1105.  
  1106.   printf("    willSearch: %ld,  willPresent: %ld,  willDelete: %ld\n", 
  1107.      init->willSearch, 
  1108.      init->willPresent,
  1109.      init->willDelete);
  1110.   printf("    supportAccessControl: %ld,  supportResourceControl: %ld\n",
  1111.      init->supportAccessControl,
  1112.      init->supportResourceControl);
  1113.   printf("    PreferredMessageSize: %ld,  MaximumRecordSize: %ld\n",
  1114.      init->PreferredMessageSize, 
  1115.      init->MaximumRecordSize);
  1116.  
  1117.   if ( init->IDAuthentication != 0) {
  1118.     printf("    IDAuthentication: %s\n", 
  1119.        init->IDAuthentication);   
  1120.   }
  1121.  
  1122.   if ( init->ImplementationID != 0) {
  1123.     printf("    ImplementationID: %s\n", 
  1124.        init->ImplementationID);   
  1125.   }
  1126.  
  1127.   if ( init->ImplementationName != 0) {
  1128.     printf("    ImplementationName: %s\n",
  1129.        init->ImplementationName);   
  1130.   }
  1131.  
  1132.   if ( init->ImplementationVersion != 0) {
  1133.     printf("    ImplementationVersion: %s\n",
  1134.        init->ImplementationVersion);   
  1135.   }
  1136.  
  1137.   if ( init->ReferenceID != 0) {
  1138.     print_any("    ReferenceID: ", init->ReferenceID);
  1139.   }
  1140.  
  1141.   freeInitAPDU( init);
  1142.  
  1143. }                /* twais_dsply_init_apdu */
  1144.  
  1145. /*
  1146.  * "twais_dsply_srch_rsp_apdu"
  1147.  *   display search response apdu on stdout
  1148.  *   the response apdu is encoded in the specified buffer
  1149.  */
  1150. void
  1151. twais_dsply_srch_rsp_apdu( buffer)
  1152. char *buffer;           /* (I) buffer contain the search response apdu */
  1153. {
  1154.   SearchResponseAPDU  *response;
  1155.   WAISSearchResponse  *info;
  1156.   long continue_viewing;
  1157.   long i, k;
  1158.  
  1159.   printf("\n\n Search Response:\n");
  1160.  
  1161.   readSearchResponseAPDU(&response,buffer);
  1162.   printf("    SearchStatus:            %ld\n", response->SearchStatus); 
  1163.   printf("    ResultCount:             %ld\n", response->ResultCount); 
  1164.   printf("    NumberOfRecordsReturned: %ld\n", response->NumberOfRecordsReturned); 
  1165.   printf("    PresentStatus:           %ld\n", response->PresentStatus); 
  1166.   if ( response->ReferenceID != 0 )
  1167.     print_any("    ReferenceID:             ", response->ReferenceID);
  1168.  
  1169.   if ( response->DatabaseDiagnosticRecords != 0 ) {
  1170.     info = (WAISSearchResponse *)response->DatabaseDiagnosticRecords;
  1171.     if ( info->SeedWordsUsed != 0 )
  1172.       printf("    SeedWordsUsed:           %s\n", info->SeedWordsUsed); 
  1173.  
  1174.     i =0; 
  1175.     continue_viewing = 1; 
  1176.  
  1177.     if ( info->DocHeaders != 0 ) {
  1178.       k =0;
  1179.       while ( (continue_viewing == 1) && info->DocHeaders[k] != 0 ) {
  1180.     i++;
  1181.     printf("\n    record %2d, ", i);
  1182.     dsply_doc_hdr_record( info->DocHeaders[k++]);
  1183. #ifdef FOR_DUM_TERM
  1184.     if ( i < response->NumberOfRecordsReturned ) {
  1185.       printf("\n\n  ... more to come,  enter 1 to continue or 0 to stop: ");
  1186.       scanf("%ld", &continue_viewing);
  1187.     }
  1188. #endif
  1189.       }
  1190.     }
  1191.  
  1192.     if ( info->ShortHeaders != 0 ) {
  1193.       k =0;
  1194.       while ( (continue_viewing == 1) && info->ShortHeaders[k] != 0 ) {
  1195.     i++;
  1196.     printf("\n    record %2d, ", i);
  1197.     dsply_short_hdr_record( info->ShortHeaders[k++]);
  1198. #ifdef FOR_DUM_TERM
  1199.     if ( i < response->NumberOfRecordsReturned ) {
  1200.       printf("\n\n  ... more to come,  enter 1 to continue or 0 to stop: ");
  1201.       scanf("%ld", &continue_viewing);
  1202.     }
  1203. #endif
  1204.       }
  1205.     }
  1206.  
  1207.     if ( info->LongHeaders != 0 ) {
  1208.       k =0;
  1209.       while ( (continue_viewing == 1) && (info->LongHeaders[k] != 0) ) {
  1210.     i++;
  1211.     printf("\n    record %2d, ", i);
  1212.     dsply_long_hdr_record( info->LongHeaders[k++]);
  1213. #ifdef FOR_DUM_TERM
  1214.     if ( i < response->NumberOfRecordsReturned ) {
  1215.       printf("\n\n  ... more to come,  enter 1 to continue or 0 to stop: ");
  1216.       scanf("%ld", &continue_viewing);
  1217.     }
  1218. #endif
  1219.       }
  1220.     }
  1221.  
  1222.     if ( info->Text != 0 ) {
  1223.       k =0;
  1224.       while ( (continue_viewing == 1) && (info->Text[k] != 0) ) {
  1225.     i++;
  1226.     printf("\n    record %2d, ", i);
  1227.     dsply_text_record( info->Text[k++]);
  1228. #ifdef FOR_DUM_TERM
  1229.     if ( i < response->NumberOfRecordsReturned ) {
  1230.       printf("\n\n  ... more to come,  enter 1 to continue or 0 to stop: ");
  1231.       scanf("%ld", &continue_viewing);
  1232.     }
  1233. #endif
  1234.       }
  1235.     }
  1236.  
  1237.     if ( info->Headlines != 0 ) {
  1238.       k =0;
  1239.       while ( (continue_viewing ==1) && (info->Headlines[k] != 0) ) {
  1240.     i++;
  1241.     printf("\n    record %2d, ", i);
  1242.     dsply_headline_record( info->Headlines[k++]);
  1243. #ifdef DUM_TERM
  1244.     if ( i < response->NumberOfRecordsReturned ) {
  1245.       printf("\n\n  ... more to come,  enter 1 to continue or 0 to stop: ");
  1246.       scanf("%ld", &continue_viewing);
  1247.     }
  1248. #endif
  1249.       }
  1250.     }
  1251.          
  1252.     if ( info->Codes != 0 ) {
  1253.       k =0;
  1254.       while ( (continue_viewing ==1) && (info->Codes[k] != 0) ) {
  1255.     i++;
  1256.     printf("\n    record %2d, ", i);
  1257.     dsply_code_record( info->Codes[k++]);
  1258. #ifdef FOR_DUM_TERM
  1259.     if ( i < response->NumberOfRecordsReturned ) {
  1260.       printf("\n\n  ... more to come,  enter 1 to continue or 0 to stop: ");
  1261.       scanf("%ld", &continue_viewing);
  1262.     }
  1263. #endif
  1264.       }
  1265.     }
  1266.  
  1267.     freeWAISSearchResponse(info);         
  1268.  
  1269.   }                /* display user info */
  1270.  
  1271.  
  1272.   freeSearchResponseAPDU( response);
  1273.  
  1274. }                /* twais_dsply_srch_rsp_apdu */
  1275.  
  1276.  
  1277. /*
  1278.  * 'dsply_doc_hdr_record'
  1279.  *   display one WAIS document header record 
  1280.  */
  1281. dsply_doc_hdr_record( record)
  1282. WAISDocumentHeader  *record;
  1283. {
  1284.   printf(" DocHeaders\n");
  1285.   print_any("     DocumentID:  ", record->DocumentID);
  1286.  
  1287.   printf("     VersionNumber:  %ld\n", record->VersionNumber);
  1288.  
  1289.   printf("     Score:  %ld,  BestMatch:  %ld\n", 
  1290.      record->Score, record->BestMatch);
  1291.  
  1292.   printf("     DocumentLength:  %ld,  Lines:  %ld\n", 
  1293.      record->DocumentLength,
  1294.      record->Lines);
  1295.  
  1296.   if ( record->Source )
  1297.     printf("     Source:  %s\n", record->Source);
  1298.   if ( record->Date  )
  1299.     printf("     Date:  %s\n", record->Date);
  1300.  
  1301.   if ( record->OriginCity )
  1302.     printf("     OriginCity:  %s\n", record->OriginCity);
  1303.  
  1304.   if ( record->Headline )
  1305. #ifdef FOR_DUM_TERM
  1306.     print_hdline(record->Headline);
  1307. #else
  1308.   printf("     Headline:  %s\n", record->Headline);
  1309. #endif
  1310. }
  1311.  
  1312. /*
  1313.  * 'dsply_short_hdr_record'
  1314.  *   display one WAIS short header record 
  1315.  */
  1316. dsply_short_hdr_record( record)
  1317. WAISDocumentShortHeader  *record;
  1318. {
  1319.   printf(" ShortHeaders\n");
  1320.   print_any("     DocumentID:  ", record->DocumentID);
  1321.  
  1322.   printf("     VersionNumber:  %ld\n", record->VersionNumber);
  1323.   printf("     Score:  %ld,  BestMatch:  %ld\n", 
  1324.      record->Score, record->BestMatch);
  1325.   printf("     DocumentLength:  %ld,  Lines:  %ld\n", 
  1326.      record->DocumentLength,  record->Lines);
  1327. }
  1328.  
  1329. static void print_docs _AP((DocObj** docs));
  1330.  
  1331. static void print_docs( docs)
  1332. DocObj **docs;
  1333. {
  1334.   long i;
  1335.  
  1336.   for ( i=0; docs[i] !=0; i++ ) {
  1337.     printf("    Document %ld:\n", i+1 );
  1338.     print_any("     DocID: ", docs[i]->DocumentID);
  1339.     if (docs[i]->Type != NULL)
  1340.       printf("     Type: %s\n",docs[i]->Type);
  1341.     printf("     ChunkCode: %ld\n", docs[i]->ChunkCode);
  1342.     switch ( docs[i]->ChunkCode ) {
  1343.     case CT_byte:
  1344.     case CT_line:
  1345.       printf("     Range: (%ld, %ld)\n", docs[i]->ChunkStart.Pos,
  1346.          docs[i]->ChunkEnd.Pos);
  1347.       break;
  1348.     case CT_paragraph:
  1349.       print_any("     Chunk Start: ", docs[i]->ChunkStart.ID);
  1350.       print_any("     Chunk End:   ", docs[i]->ChunkEnd.ID);
  1351.       break;
  1352.     case CT_document:
  1353.     default:
  1354.       break;
  1355.     }
  1356.   }
  1357.  
  1358. }                /* print_docs */
  1359.  
  1360.  
  1361. /*
  1362.  * "twais_dsply_srch_apdu"
  1363.  *   display search apdu on stdout
  1364.  *   the  apdu is in the specified buffer
  1365.  */
  1366. void
  1367. twais_dsply_srch_apdu( buffer)
  1368. char *buffer;           /* (I) buffer contain the search apdu */
  1369. {
  1370.   SearchAPDU  *search;
  1371.   WAISSearch  *info;
  1372.   DocObj **docs;
  1373.   any *text_search;
  1374.   long i;
  1375.  
  1376.   printf("\n\n Search Request:\n");
  1377.  
  1378.   readSearchAPDU(&search,buffer);
  1379.  
  1380.   printf("    SmallSetUpperBound:      %ld\n", search->SmallSetUpperBound); 
  1381.   printf("    LargeSetLowerBound:      %ld\n", search->LargeSetLowerBound); 
  1382.   printf("    MediumSetPresentNumber:  %ld\n", search->MediumSetPresentNumber); 
  1383.   printf("    ReplaceIndicator:        %ld\n", search->ReplaceIndicator); 
  1384.   if ( search->ResultSetName != 0 )
  1385.     printf("    ResultSetName:           %s\n", search->ResultSetName); 
  1386.   if ( search->QueryType != 0 )
  1387.     printf("    QueryType:               %s\n", search->QueryType); 
  1388.   if ( search->DatabaseNames != 0 )
  1389.     for ( i=0; search->DatabaseNames[i] != 0 ; i++ )
  1390.       printf("    Databasenames[%ld]:          %s\n", i,
  1391.          search->DatabaseNames[i]);
  1392.  
  1393.   if ( search->ElementSetNames != 0 )
  1394.     for ( i=0; search->ElementSetNames[i] != 0 ; i++ )
  1395.       printf("    ElementSetNames[%ld]:        %s\n", i,
  1396.          search->ElementSetNames[i]);
  1397.   
  1398.  
  1399.   if ( search->ReferenceID != 0 )
  1400.     print_any("    ReferenceID:             ", search->ReferenceID);
  1401.  
  1402.   if ( search->Query != 0 ) {
  1403.  
  1404.     /* type 1 */
  1405.     if ( ! strcmp( search->QueryType, QT_TextRetrievalQuery) ) {
  1406.       text_search = (any *) search->Query;
  1407.       docs = readWAISTextQuery(text_search);
  1408.       if ( docs != 0 )
  1409.     print_docs( docs);
  1410.       freeAny(text_search);
  1411.       doList((void**)docs,freeDocObj);
  1412.       s_free(docs);
  1413.     }
  1414.  
  1415.     else if ( ! strcmp( search->QueryType, QT_RelevanceFeedbackQuery) ) {
  1416.  
  1417.       info = (WAISSearch *)search->Query;
  1418.       if ( info->SeedWords != 0 )
  1419.     printf("    SeedWords:               %s\n", info->SeedWords); 
  1420.       if ( info->Docs != 0 )
  1421.     print_docs( info->Docs);
  1422.       printf("    DateFactor: %ld\n", info->DateFactor);
  1423.       if ( info->BeginDateRange )
  1424.     printf("    BeginDateRange: %s\n", info->BeginDateRange);
  1425.       if ( info->EndDateRange )
  1426.     printf("    EndDateRange:   %s\n", info->EndDateRange);
  1427.       printf("    MaxDocumentsRetrieved: %ld\n", info->MaxDocumentsRetrieved);
  1428.  
  1429.       freeWAISSearch(info);         
  1430.     }
  1431.     else {
  1432.       printf(" Unrecognized query type\n");
  1433.     }
  1434.   }
  1435.  
  1436.   freeSearchAPDU( search);
  1437.  
  1438. }                /* twais_dsply_srch_apdu */
  1439.  
  1440.  
  1441. void twais_free_apdu(apdu_buff)
  1442. char *apdu_buff;        /* (I) buffer contain the apdu */
  1443. {
  1444.   pdu_type pdu;
  1445.  
  1446.   pdu = peekPDUType(apdu_buff);
  1447.   switch ( pdu) {
  1448.   case (initAPDU):
  1449.     freeInitAPDU((struct InitAPDU *)apdu_buff);
  1450.     break;
  1451.   case (initResponseAPDU):
  1452.     freeInitResponseAPDU((struct InitResponseAPDU *)apdu_buff);
  1453.     break;
  1454.   case (searchAPDU):
  1455.     freeSearchAPDU((struct SearchAPDU *)apdu_buff);
  1456.     break;
  1457.   case (searchResponseAPDU):
  1458.     freeSearchResponseAPDU((struct SearchResponseAPDU *)apdu_buff);
  1459.     break;
  1460.   default:
  1461.     break;
  1462.   }
  1463.   
  1464. }                /* twais_free_apdu */
  1465.  
  1466.  
  1467. /*----------------------------------------------------------------------------*
  1468.  *  template apdus for testing purpose                                        *
  1469.  *----------------------------------------------------------------------------*/
  1470. void
  1471. twais_tmplt_init_apdu(buff, buff_len)
  1472. char *buff;             /* (O) buffer to hold the apdu */
  1473. long *buff_len;          /* (O) number of bytes written to the buffer */
  1474. {
  1475.   InitAPDU *init;
  1476.   char  *end_ptr;
  1477.   long len;
  1478.   any refID;
  1479.   refID.size = 2;
  1480.   refID.bytes = "10";
  1481.  
  1482.  
  1483.   init = makeInitAPDU(WILL_USE,WILL_NOT_USE,WILL_NOT_USE,WILL_NOT_SUPPORT,
  1484.               WILL_NOT_SUPPORT,
  1485.               2048L,
  1486.               2048L, NULL,
  1487.               defaultImplementationID(),defaultImplementationName(),
  1488.               defaultImplementationVersion(), &refID,NULL);
  1489.   { long buffer_len = MAX_BUFFER_LENGTH;
  1490.     end_ptr = writeInitAPDU(init,buff, &buffer_len);
  1491.   }
  1492.   len = (long)end_ptr - (long)&buff[0];
  1493.   *buff_len = len;
  1494.   freeInitAPDU(init);
  1495. }                /* twais_tmplt_init_apdu */
  1496.  
  1497.  
  1498. void
  1499. twais_tmplt_init_rsp_apdu(buff, buff_len)
  1500. char *buff;             /* (O) buffer to hold the apdu */
  1501. long *buff_len;          /* (O) number of bytes written to the buffer */
  1502. {
  1503.   WAISInitResponse *info;
  1504.   InitResponseAPDU *response;
  1505.   char  *end_ptr;
  1506.   long len;
  1507.   any refID;
  1508.   char chunk_marker[3];
  1509.   char highl_marker[2];
  1510.   char dhighl_marker[2];
  1511.   char new_line_chars[2];
  1512.   refID.size = 1;
  1513.   refID.bytes = "0";
  1514.   chunk_marker[0] = ESC;
  1515.   chunk_marker[1] = '1';
  1516.   chunk_marker[2]= 0;
  1517.   highl_marker[0] = DC1;
  1518.   highl_marker[1] = 0;
  1519.   dhighl_marker[0] = DC3;
  1520.   dhighl_marker[1] = 0;
  1521.   new_line_chars[0] = CARRIAGE_RETURN;
  1522.   new_line_chars[1] = 0;
  1523.  
  1524.   info =  makeWAISInitResponse(  CT_paragraph /* chunkCode */
  1525.                    ,3 /* chunkIDLen */
  1526.                    ,chunk_marker /* chunkMarker */
  1527.                    ,highl_marker /* highlightMarker */
  1528.                    ,dhighl_marker /* deHighlightMarker */
  1529.                    ,new_line_chars /* newLineChars */
  1530.                    );
  1531.  
  1532.   response = makeInitResponseAPDU( ACCEPT, /* result */
  1533.                   WILL_USE,WILL_NOT_USE,WILL_NOT_USE, /* search, rsp, del */
  1534.                   WILL_NOT_SUPPORT,WILL_NOT_SUPPORT, /* acc ctl, rsc ctl */
  1535.                   1024L, 2048L, /* preferred msg size, max msg size */
  1536.                   NULL,    /* authentication */
  1537.                   defaultImplementationID(),
  1538.                   defaultImplementationName(),
  1539.                   defaultImplementationVersion(),
  1540.                   &refID, /* reference id */
  1541.                   info);
  1542.   { long buffer_len = MAX_BUFFER_LENGTH;
  1543.     end_ptr = writeInitResponseAPDU(response, buff, &buffer_len);
  1544.   }
  1545.   len = (long)end_ptr - (long)&buff[0];
  1546.   *buff_len = len;
  1547.  
  1548.   CSTFreeWAISInitResponse( info);
  1549.   freeInitResponseAPDU(response);
  1550. }                /* twais_tmplt_init_rsp_apdu */
  1551.  
  1552.  
  1553.  
  1554. void twais_tmplt_typ1_srch_apdu( buff, buff_len)
  1555. char *buff;             /* (O) buffer to hold the apdu */
  1556. long *buff_len;          /* (O) number of bytes written to the buffer */
  1557. {
  1558.  
  1559.   SearchAPDU *search1;
  1560.   char  *end_ptr;
  1561.   long len;
  1562.  
  1563.   static char *database_names[2];
  1564.   any docID;
  1565.   any refID;
  1566.  
  1567.   DocObj *DocObjs[2];
  1568.   any *query;            /* changed from char* by brewster */
  1569.   database_names[0] = "Quest";
  1570.   database_names[1] = NULL;
  1571.   docID.size = 12;
  1572.   docID.bytes = "0000106776WJ";
  1573.   refID.size = 1;
  1574.   refID.bytes = "3";
  1575.    
  1576.   DocObjs[0] = makeDocObjUsingWholeDocument( &docID,NULL);
  1577.   DocObjs[1] = NULL;
  1578.  
  1579.   query = makeWAISTextQuery(DocObjs);   
  1580.  
  1581.   search1 = makeSearchAPDU( 10L, 16L, 15L, 
  1582.                1L,    /* replace indicator */
  1583.                "FOO", /* result set name */
  1584.                database_names, /* database name */   
  1585.                QT_TextRetrievalQuery, /* query_type */
  1586.                0L,    /* element name */
  1587.                &refID, /* reference ID */
  1588.                query);
  1589.  
  1590.   {
  1591.     long buffer_len = MAX_BUFFER_LENGTH;
  1592.     end_ptr = writeSearchAPDU(  search1, buff, &buffer_len);
  1593.   }
  1594.   len = (long)end_ptr - (long)&buff[0];
  1595.   *buff_len = len;
  1596.  
  1597.   CSTFreeWAISTextQuery( query);
  1598.   freeSearchAPDU(search1);
  1599.  
  1600. }                /* twais_tmplt_typ1_srch_apdu */
  1601.  
  1602. #if 0
  1603.  
  1604. twais_tmplt_typ3_srch_apdu( buff, buff_len)
  1605. char *buff;             /* (O) buffer to hold the apdu */
  1606. long *buff_len;          /* (O) number of bytes written to the buffer */
  1607. {
  1608.  
  1609.   SearchAPDU *search3;
  1610.   char  *end_ptr;
  1611.   static char *database_names[2];
  1612.   long len;
  1613.   any refID;
  1614.   WAISSearch *query;
  1615.   database_names[0] = "Quest"
  1616.     database_names[1] = NULL;
  1617.   refID.size = 1;
  1618.   refID.bytes = "3";
  1619.  
  1620.   query = makeWAISSearch( "Supercomputers in Taiwan", /* seed_words*/
  1621.              0L,    /* DocObjsPtr */
  1622.              0L,
  1623.              1L,    /* DateFactor */
  1624.              0L,    /* BeginDateRange */
  1625.              0L,    /* EndDateRange */
  1626.              10L    /* maxDocsRetrieved */
  1627.              );
  1628.  
  1629.   search3 = makeSearchAPDU( 10L, 16L, 15L, 
  1630.                1L,    /* replace indicator */
  1631.                "FOO", /* result set name */
  1632.                database_names, /* database name */   
  1633.                QT_RelevanceFeedbackQuery, /* query_type */
  1634.                0L,    /* element name */
  1635.                &refID, /* reference ID */
  1636.                query);
  1637.   {
  1638.     long buffer_len = MAX_BUFFER_LENGTH;
  1639.     end_ptr = writeSearchAPDU(  search3, buff, &buffer_len);
  1640.   }
  1641.   len = (long)end_ptr - (long)&buff[0];
  1642.   *buff_len = len;
  1643.  
  1644.   CSTFreeWAISSearch( query);
  1645.   freeSearchAPDU(search3);
  1646.  
  1647. }                /* twais_tmplt_typ3_srch_apdu */
  1648. #endif
  1649.  
  1650.  
  1651. void twais_tmplt_typ3_srch_rsp_apdu( buff, buff_len)
  1652. char *buff;
  1653. long *buff_len;
  1654. {
  1655.  
  1656.   char  *end_ptr;
  1657.   long len;
  1658.   any refID;
  1659.  
  1660.   WAISDocumentHeader  *doc_headers[3];
  1661.   WAISSearchResponse *records;
  1662.   SearchResponseAPDU *response;
  1663.   any doc_id1;
  1664.   any doc_id2;
  1665.   refID.size = 1;
  1666.   refID.bytes = "1";
  1667.   doc_id1.size = 12;   
  1668.   doc_id1.bytes = "0000106776WJ";
  1669.   doc_id2.size = 12;
  1670.   doc_id2.bytes = "0000026870WP";
  1671.  
  1672.   doc_headers[0] = makeWAISDocumentHeader(
  1673.                       &doc_id1, /* docID */
  1674.                       1L, /* versionNumber */
  1675.                       80L, /* score */
  1676.                       1L, /* bestMatch */
  1677.                       850L, /* docLen */
  1678.                       200L, /* lines */
  1679.                       NULL,    /* types */
  1680.                       "Source1", /* source */
  1681.                       "19900115", /* date */
  1682.                       "CRAY sells supercomputer to Taiwan",    /* headline */
  1683.                       "New York"); /* originCity */
  1684.  
  1685.  
  1686.   doc_headers[1] = makeWAISDocumentHeader(
  1687.                       &doc_id2, /* docID */
  1688.                       1L, /* versionNumber */
  1689.                       60L, /* score */
  1690.                       1L, /* bestMatch */
  1691.                       550L, /* docLen */
  1692.                       100L, /* lines */
  1693.                       NULL,
  1694.                       "Test Source", /* source */
  1695.                       "19900110", /* date */
  1696.                       "Test Headline", /* headline */
  1697.                       "Test City");    /* originCity */
  1698.  
  1699.   doc_headers[2] = 0;
  1700.  
  1701.   records = makeWAISSearchResponse( "Supercomputer Taiwan" /* seedWordsUsed*/
  1702.                    ,doc_headers    /* docHeaders */
  1703.                    ,0 ,0 ,0 ,0 /* shortHeaders, longHeaders, text, headlines */
  1704.                    ,0 /* codes */
  1705.                    ,NULL /* diagnostics.  KLUDGE */
  1706.                    );
  1707.  
  1708.  
  1709.   response = makeSearchResponseAPDU( SUCCESS /* result */
  1710.                     ,2 /* count */
  1711.                     ,2 /* recordsReturned */
  1712.                     ,0 /* nextPos */
  1713.                     ,0 /* ignore resultStatus since result SUCCESS */
  1714.                     ,SUCCESS /* presentStatus */
  1715.                     ,&refID /* refID */
  1716.                     ,records);
  1717.  
  1718.  
  1719.   {
  1720.     long buffer_len = MAX_BUFFER_LENGTH;
  1721.     end_ptr = writeSearchResponseAPDU(  response, buff, &buffer_len);
  1722.   }
  1723.   len = (long)end_ptr - (long)&buff[0];
  1724.   *buff_len = len;
  1725.  
  1726.   CSTFreeWAISDocumentHeader( doc_headers[0]);
  1727.  
  1728.   CSTFreeWAISDocumentHeader( doc_headers[1]);
  1729.  
  1730.   CSTFreeWAISSearchResponse( records);
  1731.   freeSearchResponseAPDU(response);
  1732.  
  1733.  
  1734. }                /* twais_tmplt_typ3_srch_rsp_apdu */
  1735.  
  1736.  
  1737. twais_tmplt_typ1_stry_rsp_apdu( buff, buff_len)
  1738. char *buff;
  1739. long *buff_len;
  1740. {
  1741.  
  1742.   char  *end_ptr;
  1743.   long len;
  1744.   any refID;
  1745.  
  1746.   WAISDocumentText *doc_text[2];
  1747.   WAISSearchResponse *records;
  1748.   SearchResponseAPDU *response;
  1749.   any docID;
  1750.   any story;
  1751.   char *story_buff;
  1752.   
  1753.   FILE *fptr;
  1754.   refID.size = 1;
  1755.   refID.bytes = "1";
  1756.   docID.size = 12;
  1757.   docID.bytes = "0000106776WJ";
  1758.   doc_text[0] = 0;
  1759.   doc_text[1] = 0;
  1760.  
  1761.   fptr = s_fopen("twais_template.txt", "r");
  1762.   if (fptr == NULL ) {
  1763.     printf(" unable to open story text file \n");
  1764.     return;
  1765.   }
  1766.    
  1767.   /* read story length */
  1768.   fread((char*)&story.size, sizeof(long), 1, fptr);
  1769.   story_buff = s_malloc( story.size +1);
  1770.   if ( story_buff == NULL) {
  1771.     printf(" insufficient memory\n");
  1772.     s_fclose( fptr);
  1773.     return;
  1774.   }
  1775.  
  1776.   /* read story text */
  1777.   fread( story_buff, 1, story.size, fptr);   
  1778.   story.bytes = story_buff;
  1779.    
  1780.   doc_text[0] = makeWAISDocumentText( &docID, 1L, &story);
  1781.  
  1782.   records = makeWAISSearchResponse( 0 /* seedWordsUsed*/
  1783.                    ,0 ,0 ,0 /* docHeaders, shortHeaders, longHeaders */
  1784.                    ,doc_text ,0    /* text, headlines */
  1785.                    ,0 /* codes */
  1786.                    ,NULL /* diagnostics.  KLUDGE */
  1787.                    );
  1788.  
  1789.  
  1790.   response = makeSearchResponseAPDU( SUCCESS /* result */
  1791.                     ,1 /* count */
  1792.                     ,1 /* recordsReturned */
  1793.                     ,0 /* nextPos */
  1794.                     ,0 /* ignore resultStatus since result SUCCESS */
  1795.                     ,SUCCESS /* presentStatus */
  1796.                     ,&refID /* refID */
  1797.                     ,records);
  1798.  
  1799.  
  1800.   {
  1801.     long buffer_len = MAX_BUFFER_LENGTH;
  1802.     end_ptr = writeSearchResponseAPDU(  response, buff, &buffer_len);
  1803.   }
  1804.   len = (long)end_ptr - (long)&buff[0];
  1805.   *buff_len = len;
  1806.  
  1807.   CSTFreeWAISDocumentText( doc_text[0]);
  1808.  
  1809.   CSTFreeWAISSearchResponse( records);
  1810.  
  1811.   freeSearchResponseAPDU(response);
  1812.  
  1813.   s_free(story_buff);
  1814.   s_fclose(fptr);
  1815.  
  1816. }                /* twais_tmplt_typ1_stry_rsp_apdu */
  1817.  
  1818.  
  1819. twais_tmplt_typ3_srch_apdu( buff, buff_len)
  1820. char *buff;             /* (O) buffer to hold the apdu */
  1821. long *buff_len;          /* (O) number of bytes written to the buffer */
  1822. {
  1823.  
  1824.   SearchAPDU *search3;
  1825.   char  *end_ptr;
  1826.   static char *database_names[7] = { 
  1827.     "11111111111111111111111111111111111111111111111111",
  1828.     "22222222222222222222222222222222222222222222222222",
  1829.     "33333333333333333333333333333333333333333333333333",
  1830.     "44444444444444444444444444444444444444444444444444",
  1831.     "55555555555555555555555555555555555555555555555555",
  1832.     "66666666666666666666666666666666666666666666666666",
  1833.     0};
  1834.   static char *elem_names[7] = { ES_DocumentHeader
  1835.                    ,ES_DocumentShortHeader
  1836.                      ,ES_DocumentLongHeader
  1837.                        ,ES_DocumentText
  1838.                      ,ES_DocumentHeadline
  1839.                        ,ES_DocumentCodes
  1840.                          ,0};
  1841.  
  1842.   long len;          
  1843.   any refID;
  1844.   WAISSearch *query;
  1845.   refID.size = 1;
  1846.   refID.bytes = "3";
  1847.  
  1848.   query = makeWAISSearch( 
  1849.              "What is the penalities for driving without insurance",
  1850.              0L,    /* DocObjsPtr */
  1851.              0L,
  1852.              1L,    /* DateFactor */
  1853.              0L,    /* BeginDateRange */
  1854.              0L,    /* EndDateRange */
  1855.              10L    /* maxDocsRetrieved */
  1856.              );
  1857.  
  1858.   search3 = makeSearchAPDU( 10L, 16L, 15L, 
  1859.                1L,    /* replace indicator */
  1860.                "FOO", /* result set name */
  1861.                database_names, /* database name */   
  1862.                QT_RelevanceFeedbackQuery, /* query_type */
  1863.                elem_names, /* element name */
  1864.                &refID, /* reference ID */
  1865.                query);
  1866.   {
  1867.     long buffer_len = MAX_BUFFER_LENGTH;
  1868.     end_ptr = writeSearchAPDU(  search3, buff, &buffer_len);
  1869.   }
  1870.   len = (long)end_ptr - (long)&buff[0];
  1871.   *buff_len = len;
  1872.  
  1873.   CSTFreeWAISSearch( query);
  1874.   freeSearchAPDU(search3);
  1875.  
  1876. }                /* twais_tmplt_typ3_srch_apdu */
  1877.  
  1878.