dtsrapi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * COMPONENT_NAME: austext
  25. *
  26. * FUNCTIONS: DtSearchGetKeytypes
  27. * DtSearchHighlight
  28. * DtSearchInit
  29. * DtSearchQuery
  30. * DtSearchRetrieve
  31. * aa_categories
  32. * aa_is_semantic_db
  33. * aa_netrc
  34. * aa_reinit
  35. * ausapi_get_hirec
  36. * ausapi_getexpire
  37. * ausapi_getnews
  38. * both_valid_dates
  39. * build_dbnames_array
  40. * pack_navstr
  41. * signal_abort
  42. * valid_dbname
  43. *
  44. * ORIGINS: 27
  45. *
  46. *
  47. * (C) COPYRIGHT International Business Machines Corp. 1993,1995
  48. * All Rights Reserved
  49. * Licensed Materials - Property of IBM
  50. * US Government Users Restricted Rights - Use, duplication or
  51. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  52. */
  53. /**************************** DTSRAPI.C *************************
  54. * $XConsortium: dtsrapi.c /main/9 1996/11/25 18:54:18 drk $
  55. * Nov 1993. Originally called "calloe".
  56. * Implements a simple DtSearch/AusText API at a higher level than OE calls.
  57. * Substantially isolates caller from knowledge of usrblks,
  58. * hiding details of searches, possible return codes, etc.
  59. * Does its own mallocs and frees and controls the usrblk.
  60. *
  61. * Function descriptions (ie documentation) is in dtsearch.doc
  62. * and Search.h.
  63. *
  64. * Revision 2.8 1996/03/14 22:54:41 miker
  65. * Pass backdoor usrblk.debug into DtSearch.
  66. *
  67. * Revision 2.7 1996/02/01 18:09:24 miker
  68. * Deleted ausapi_kwic, ausapi_ping, ausapi_shutdown, signal(SIGUSR1).
  69. * DtSearchInit: usrblk.debug = high order 16 bits of init_switches arg.
  70. *
  71. * Revision 2.6 1995/12/27 16:08:22 miker
  72. * Added DtSearchReinit().
  73. *
  74. * Revision 2.5 1995/10/26 17:34:52 miker
  75. * Fixed duplicate messages catalog open.
  76. *
  77. * Revision 2.4 1995/10/25 21:39:45 miker
  78. * Renamed from ausapi.c. Added prolog.
  79. *
  80. * Log: ausapi.c,v
  81. * Revision 2.3 1995/10/19 20:19:34 miker
  82. * Databases opened read-only unless DtSrInRDWR is specified.
  83. *
  84. * Revision 2.2 1995/10/02 20:51:32 miker
  85. * Bug: Tried to free unmalloced version string after Init (Takuki Kamiya).
  86. * Added symbolic msg catalog set numbers.
  87. * Fixed erroneous dbnames assignment.
  88. *
  89. * Revision 2.1 1995/09/22 18:48:45 miker
  90. * Freeze DtSearch 0.1, AusText 2.1.8
  91. *
  92. * Revision 1.20 1995/09/19 21:43:46 miker
  93. * Fixed minor bug in debugging statements only.
  94. *
  95. * Revision 1.19 1995/08/31 21:36:39 miker
  96. * Added #define to block out code not to be delivered to DtSearch.
  97. * Changed all msgslists to one global one at ausapi_msglist.
  98. * Moved function documentation to dtsearch.doc
  99. * Public functions renamed to match DtSearch conventions.
  100. * Rearranged some function args to eliminate public global variables.
  101. * Broke messages catalog in two so DtSearch can have its own.
  102. * Minor changes to accommodate portability.
  103. * Reenabled user to change name of site config file.
  104. *
  105. * Revision 1.18 1995/07/18 22:05:10 miker
  106. * Set OE_sitecnfg_fname to arg passed to ausapi_init().
  107. */
  108. #include "SearchE.h"
  109. #include <ctype.h>
  110. #include <signal.h>
  111. #include <locale.h>
  112. #include <string.h>
  113. #include <stdlib.h>
  114. #include <fcntl.h>
  115. #define PROGNAME "DTSRAPI"
  116. #define SPRINTBUFSZ 1024
  117. #define MS_misc 1
  118. #define MS_ausapi 2
  119. /*------------------- PRIVATE GLOBALS ---------------------*/
  120. /* Usrblk should not be visible to user interface code,
  121. * but must be visible to real engine... */
  122. USRBLK usrblk = { { 0 } };
  123. static int save_search_type = '$';
  124. static char *sprintbuf = NULL;
  125. /*------------------- EXTERNS (aajoint.c) ---------------------*/
  126. extern int aa_is_initialized;
  127. extern void aa_check_initialization (void);
  128. extern long save_init_switches;
  129. #ifndef db_oflag
  130. extern int db_oflag;
  131. #endif
  132. /************************************************/
  133. /* */
  134. /* signal_abort */
  135. /* */
  136. /************************************************/
  137. /* Interrupt handler for all common 'abort' signals.
  138. * Shuts down gracefully by ensuring database properly closed.
  139. * The database close and write to the audit log occur in OE.
  140. */
  141. static void signal_abort (int sig)
  142. {
  143. fputs (DtSearchGetMessages (), aa_stderr);
  144. fprintf (aa_stderr, CATGETS(dtsearch_catd, MS_ausapi, 216,
  145. "\n%s %s Caught signal %d.\n"),
  146. PROGNAME"216",
  147. (aa_argv0) ? aa_argv0 : OE_prodname,
  148. sig);
  149. fflush (aa_stderr);
  150. DtSearchExit (100 + sig);
  151. } /* signal_abort() */
  152. /************************************************/
  153. /* */
  154. /* valid_dbname */
  155. /* */
  156. /************************************************/
  157. /* Sets usrblk.dblk to passed dbname.
  158. * Name must match dblk.label if it exists,
  159. * otherwise it must match dblk.name (same
  160. * algorithm that generates dbnamesv[]).
  161. * If dbname == NULL, sets dblk to first dblist node.
  162. * Returns TRUE on success.
  163. * Returns FALSE and err msg if invalid dbname.
  164. */
  165. static int valid_dbname (char *dbname)
  166. {
  167. DBLK *db;
  168. aa_check_initialization ();
  169. if (dbname == NULL) {
  170. usrblk.dblk = usrblk.dblist;
  171. return TRUE;
  172. }
  173. if (dbname[0] == 0) {
  174. usrblk.dblk = usrblk.dblist;
  175. return TRUE;
  176. }
  177. for (db = usrblk.dblist; db != NULL; db = db->link) {
  178. if (db->label) {
  179. if (strcmp (dbname, db->label) == 0)
  180. break;
  181. }
  182. if (strcmp (dbname, db->name) == 0)
  183. break;
  184. }
  185. if (db == NULL) {
  186. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_ausapi, 1,
  187. "%1$sInvalid or unavailable database '%2$s'."),
  188. PROGNAME "48 ", dbname);
  189. DtSearchAddMessage (sprintbuf);
  190. if (DtSrInANY_DEBUG & save_init_switches) {
  191. fputs (PROGNAME "49 Available Databases:\n", aa_stderr);
  192. for (db = usrblk.dblist; db != NULL; db = db->link)
  193. fprintf (aa_stderr, " name='%s'\tlabel='%s'\n",
  194. db->name, db->label);
  195. }
  196. return FALSE;
  197. }
  198. else
  199. usrblk.dblk = db;
  200. return TRUE;
  201. } /* valid_dbname() */
  202. /************************************************/
  203. /* */
  204. /* build_dbnames_array */
  205. /* */
  206. /************************************************/
  207. /* Builds array of database name string ptrs with same
  208. * architecture as command line arguments argv and argc
  209. * from the shell. Uses dblk.label if it exists,
  210. * otherwise uses dblk.name.
  211. * Called at initialization and reinitialization.
  212. */
  213. static void build_dbnames_array (void)
  214. {
  215. char *targ, *src;
  216. DBLK *db;
  217. int i;
  218. size_t mallocsz = 0L;
  219. if (ausapi_dbnamesv)
  220. free (ausapi_dbnamesv);
  221. ausapi_dbnamesc = 0;
  222. for (db = usrblk.dblist; db != NULL; db = db->link) {
  223. ausapi_dbnamesc++;
  224. mallocsz += ((db->label) ? strlen (db->label) : sizeof (db->name)) + 2L;
  225. }
  226. ausapi_dbnamesv = austext_malloc (
  227. mallocsz + (sizeof (char *) * (2 + ausapi_dbnamesc)) + 4,
  228. PROGNAME "106", NULL);
  229. /* The first part of the malloc is the array of pointers.
  230. * The strings start just after the array. Set 'targ' to
  231. * the beginning of the strings, while 'i' indexes the ptrs.
  232. */
  233. targ = (char *) ausapi_dbnamesv + (sizeof (char *) * (1 + ausapi_dbnamesc));
  234. for (i = 0, db = usrblk.dblist; db != NULL; i++, db = db->link) {
  235. ausapi_dbnamesv[i] = targ;
  236. src = (db->label) ? db->label : db->name;
  237. while (*src != 0)
  238. *targ++ = *src++;
  239. *targ++ = 0;
  240. if (save_init_switches & DtSrInIDEBUG) {
  241. fprintf (aa_stderr, PROGNAME "490 "
  242. "dbnames[%d] = '%s': dname='%s', dlabel='%s'\n",
  243. i, ausapi_dbnamesv[i], db->name, db->label);
  244. fflush (aa_stderr);
  245. }
  246. }
  247. ausapi_dbnamesv[i] = NULL; /* terminate the list this way too */
  248. return;
  249. } /* build_dbnames_array() */
  250. /************************************************/
  251. /* */
  252. /* aa_reinit */
  253. /* */
  254. /************************************************/
  255. /* Performs all reinitialization functions required by OE_REINIT.
  256. * Always returns DtSrREINIT.
  257. */
  258. static int aa_reinit (void)
  259. {
  260. clear_usrblk_record ();
  261. clear_hitwords ();
  262. usrblk.dba = 0;
  263. DtSearchFreeResults (&usrblk.dittolist);
  264. usrblk.dittocount = 0;
  265. build_dbnames_array ();
  266. return DtSrREINIT;
  267. } /* aa_reinit() */
  268. /************************************************/
  269. /* */
  270. /* DtSearchInit */
  271. /* */
  272. /************************************************/
  273. /* Initializes ausapi and the AusText engine (performs OE_INIT).
  274. * Must be first ausapi call. Must be called only once (reinit?).
  275. * See dtsearch.doc for specs.
  276. */
  277. int DtSearchInit (
  278. char *argv0,
  279. char *userid,
  280. long switches,
  281. char *config_file,
  282. FILE *err_file,
  283. char ***dbnames,
  284. int *dbcount)
  285. {
  286. if (aa_is_initialized) {
  287. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_ausapi, 621,
  288. "%1$s %2%s has already been initialized."),
  289. PROGNAME"621", PRODNAME);
  290. DtSearchAddMessage (sprintbuf);
  291. return DtSrFAIL;
  292. }
  293. aa_is_initialized = TRUE;
  294. save_init_switches = switches;
  295. if (argv0)
  296. aa_argv0 = argv0;
  297. if (err_file)
  298. aa_stderr = err_file;
  299. else
  300. aa_stderr = stderr;
  301. sprintbuf = austext_malloc (SPRINTBUFSZ, PROGNAME "135", NULL);
  302. /* Open msgs and help text catalogs. */
  303. if (switches & (DtSrInNOLOCALE == 0)) {
  304. setlocale (LC_ALL, "");
  305. dtsearch_catd = CATOPEN(FNAME_DTSRCAT, 0);
  306. }
  307. /* Register AusText abort signal handlers.
  308. * This ensures that if caller is killed,
  309. * engine will shutdown gracefully.
  310. */
  311. if (switches & DtSrInSIGNAL) {
  312. if (!(switches & DtSrInENAB_NOHUP))
  313. signal (SIGHUP, signal_abort); /* trap hangups */
  314. signal (SIGINT, signal_abort); /* interrupt, ctrl-c */
  315. signal (SIGQUIT, signal_abort); /* quit, ctrl-d */
  316. signal (SIGKILL, signal_abort); /* (kill -9, cannot be trapped) */
  317. signal (SIGTERM, signal_abort); /* kill [-15], sfwr terminate */
  318. #ifdef SIGPWR
  319. signal (SIGPWR, signal_abort); /* power failure imminent */
  320. #endif
  321. #ifdef _AIX
  322. signal (SIGXCPU, signal_abort); /* cpu time limit exceeded */
  323. signal (SIGDANGER, signal_abort); /* imminent paging space crash */
  324. #endif
  325. }
  326. /* If user name was not passed, get it from LOGNAME environment var */
  327. if (userid == NULL || *userid == 0)
  328. if ((userid = (char *) getenv ("LOGNAME")) == NULL) {
  329. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_ausapi, 187,
  330. "%1$s Missing both userid and LOGNAME environment variable."),
  331. PROGNAME "187 ");
  332. DtSearchAddMessage (sprintbuf);
  333. return DtSrFAIL;
  334. }
  335. /* initialize usrblk fields */
  336. memset (&usrblk, 0, sizeof (USRBLK));
  337. strncpy (usrblk.userid, userid, 8);
  338. usrblk.userid[8] = 0;
  339. usrblk.flags |= USR_NO_INFOMSGS; /* standard for ausapi */
  340. usrblk.flags |= USR_SORT_WHITL; /* standard for ausapi */
  341. usrblk.flags |= USR_NO_ITERATE; /* must ALWAYS be on in
  342. * this api */
  343. if (switches & DtSrInIDEBUG)
  344. usrblk.debug |= USRDBG_RARE;
  345. if (switches & DtSrInSDEBUG)
  346. usrblk.debug |= USRDBG_SRCHCMPL;
  347. if (switches & DtSrInRDEBUG)
  348. usrblk.debug |= USRDBG_RETRVL;
  349. /* Secret unadvertised feature. High order 2 bytes
  350. * are direct settings of lower 2 bytes of usrblk.debug.
  351. */
  352. usrblk.debug |= switches >> 16;
  353. /* Set vista's db_oflag from DtSrInRDWR switch.
  354. * Note the vista flag and the switch are inverses
  355. * of each other (RDWR vs. RDONLY).
  356. */
  357. if ((switches & DtSrInRDWR) == 0)
  358. db_oflag = O_RDONLY;
  359. /* Prespecify site config file to engine */
  360. if (config_file)
  361. OE_sitecnfg_fname = (char *) strdup (config_file);
  362. /* initialize AusText Engine */
  363. usrblk.request = OE_INITIALIZE;
  364. usrblk.query = AUSAPI_VERSION;
  365. Opera_Engine ();
  366. usrblk.query = NULL; /* so we don't try to free it at query time */
  367. if (usrblk.retncode != OE_OK)
  368. return DtSrFAIL;
  369. build_dbnames_array ();
  370. if (dbnames) {
  371. *dbnames = ausapi_dbnamesv;
  372. if (!dbcount) {
  373. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_ausapi, 7,
  374. "%s dbnames specified but not dbcount."),
  375. PROGNAME"304");
  376. DtSearchAddMessage (sprintbuf);
  377. return DtSrFAIL;
  378. }
  379. *dbcount = ausapi_dbnamesc;
  380. }
  381. return DtSrOK;
  382. } /* DtSearchInit() */
  383. /************************************************/
  384. /* */
  385. /* DtSearchReinit */
  386. /* */
  387. /************************************************/
  388. /* Returns pointer to dbnames array.
  389. * Used after database or config file changes to trigger
  390. * engine reinitialization and to reaccess the database names.
  391. */
  392. int DtSearchReinit (char ***dbnames, int *dbcount)
  393. {
  394. aa_check_initialization();
  395. usrblk.request = OE_PING;
  396. Opera_Engine();
  397. switch (usrblk.retncode) {
  398. case OE_REINIT:
  399. if (save_init_switches & DtSrInANY_DEBUG) {
  400. fputs (PROGNAME"755 "
  401. "DtSearchReinit: Engine did return REINIT.\n",
  402. aa_stderr);
  403. fflush (aa_stderr);
  404. }
  405. aa_reinit();
  406. /* fall thru to OK... */
  407. case OE_OK:
  408. *dbnames = ausapi_dbnamesv;
  409. *dbcount = ausapi_dbnamesc;
  410. return DtSrOK;
  411. default:
  412. return DtSrERROR;
  413. }
  414. } /* DtSearchReinit() */
  415. /************************************************/
  416. /* */
  417. /* DtSearchGetKeytypes */
  418. /* */
  419. /************************************************/
  420. /* Returns pointer to keytypes array of specified database.
  421. * If dbname == NULL, returns keytypes of first dblist node.
  422. * Caller may modify is_selected field but should
  423. * not alter other keytypes fields or pointers.
  424. */
  425. int DtSearchGetKeytypes (
  426. char *dbname, /* 1 - 8 char database name */
  427. int *ktcount, /* number entries in array */
  428. DtSrKeytype **keytypes) /* array of database types */
  429. {
  430. if (!valid_dbname (dbname))
  431. return DtSrREINIT;
  432. *ktcount = usrblk.dblk->ktcount;
  433. *keytypes = usrblk.dblk->keytypes;
  434. return DtSrOK;
  435. } /* DtSearchGetKeytypes() */
  436. /************************************************/
  437. /* */
  438. /* both_valid_dates */
  439. /* */
  440. /************************************************/
  441. /* Subroutine of DtSearchQuery().
  442. * Sets usrblk.objdate1 and objdate2 from passed date strings.
  443. * Returns TRUE on successful parse and conversion.
  444. * Returns FALSE and err msg if either date string is invalid.
  445. */
  446. static int both_valid_dates (char *date1, char *date2)
  447. {
  448. int convert_error = FALSE;
  449. if ((usrblk.objdate1 = DtSearchValidDateString (date1)) == -1L)
  450. convert_error = TRUE;
  451. if ((usrblk.objdate2 = DtSearchValidDateString (date2)) == -1L)
  452. convert_error = TRUE;
  453. if (!convert_error &&
  454. usrblk.objdate1 != 0L &&
  455. usrblk.objdate2 != 0L &&
  456. usrblk.objdate1 >= usrblk.objdate2) {
  457. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_ausapi, 198,
  458. "%s 'Before' date is equal to or after 'After' date.\n"
  459. " No records would be returned."),
  460. PROGNAME "198");
  461. DtSearchAddMessage (sprintbuf);
  462. convert_error = TRUE;
  463. }
  464. return !convert_error;
  465. } /* both_valid_dates() */
  466. /************************************************/
  467. /* */
  468. /* DtSearchQuery */
  469. /* */
  470. /************************************************/
  471. /* Returns hitlist (dittolist) if search successful.
  472. * If dbname is NULL, assumes name from first dblk on dblist.
  473. * Will work correctly even if arg dittolist == &usrblk.dittolist.
  474. * Only return hits 'after' date1 and 'before' date2.
  475. * NULL in one date field means search not limited in that direction.
  476. * NULL in both date fields means search not restricted by dates.
  477. * NULL is also permitted in stems, but if stems is
  478. * not NULL, the passed stems array MUST be defined:
  479. * char stems [DtSrMAX_STEMCOUNT] [DtSrMAXWIDTH_HWORD].
  480. * If query is fzkeyi, it must point to
  481. * array of FZKEYSZ integers.
  482. * DtSearchQuery() was formerly named ausapi_search().
  483. */
  484. int DtSearchQuery (
  485. void *qry, /* query, fzkeyi, nav string */
  486. char *dbname, /* database name from dbnamesv */
  487. int search_type, /* 'P', 'W', 'S', 'T', 'Z', or 'N' */
  488. char *date1, /* "yyyy mm dd", 3 numeric tokens */
  489. char *date2, /* date1 earlier than date2 */
  490. DtSrResult
  491. **dittolist, /* put hitlist here */
  492. long *dittocount, /* put num items on returned hitlist */
  493. char *stems, /* put stems array here */
  494. int *stemcount) /* put size of stems array */
  495. {
  496. int final_request;
  497. int i;
  498. char *ptr;
  499. DBLK *db;
  500. LLIST *llp;
  501. enum { TEXT, NAVSTRING, FZKEYI }
  502. qryarg;
  503. # define _DATE1STR_LEN (24)
  504. char date1str[_DATE1STR_LEN];
  505. if (!valid_dbname (dbname))
  506. return DtSrREINIT;
  507. /* Verify valid search_type and set flag
  508. * to tell us how to interpret 'qry' arg.
  509. */
  510. switch (search_type) {
  511. case 'P': /* statistical search */
  512. qryarg = TEXT;
  513. final_request = OE_SRCH_STATISTICAL;
  514. break;
  515. case 'W': /* exact words search */
  516. qryarg = TEXT;
  517. final_request = OE_SRCH_WORDS;
  518. break;
  519. case 'S': /* exact stems search */
  520. qryarg = TEXT;
  521. final_request = OE_SRCH_STEMS;
  522. break;
  523. default:
  524. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_ausapi, 20,
  525. "%1$s Invalid search_type '%2$c'."),
  526. PROGNAME "172 ", search_type);
  527. DtSearchAddMessage (sprintbuf);
  528. return DtSrERROR;
  529. } /* end switch on search_type */
  530. /* Validate the 'qry' argument */
  531. if (qry == NULL) {
  532. QUERY_ERROR:
  533. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_ausapi, 30,
  534. "%s Null query. No search performed."),
  535. PROGNAME"81");
  536. DtSearchAddMessage (sprintbuf);
  537. return DtSrFAIL;
  538. }
  539. /* If qry is a char string, it must be nonempty. */
  540. if (qryarg != FZKEYI && ((char *) qry)[0] == 0)
  541. goto QUERY_ERROR;
  542. /* If qry is text to be sent as is to engine,
  543. * copy it over now to userblk.
  544. */
  545. if (qryarg == TEXT && qry != usrblk.query) {
  546. if (usrblk.query != NULL)
  547. free (usrblk.query);
  548. usrblk.query = austext_malloc (strlen (qry) + 16, PROGNAME "102", NULL);
  549. strcpy (usrblk.query, qry);
  550. }
  551. /* Validate the 'date' args and place them into usrblk. */
  552. if (!both_valid_dates (date1, date2))
  553. return DtSrFAIL;
  554. /* Finish setup usrblk for final search */
  555. usrblk.flags |= USR_NO_ITERATE; /* must ALWAYS be ON in this api */
  556. usrblk.request = final_request;
  557. db = usrblk.dblk;
  558. db->maxhits = aa_maxhits;
  559. if (qryarg == TEXT)
  560. save_search_type = usrblk.search_type = search_type;
  561. if (usrblk.debug & USRDBG_SRCHCMPL) {
  562. ptr = sprintbuf;
  563. for (i = 0; i < db->ktcount; i++) {
  564. if (db->keytypes[i].is_selected)
  565. *ptr++ = '*';
  566. *ptr++ = db->keytypes[i].ktchar;
  567. *ptr++ = ' ';
  568. }
  569. *(--ptr) = 0;
  570. strncpy(date1str, objdate2fzkstr (usrblk.objdate1), _DATE1STR_LEN);
  571. date1str[_DATE1STR_LEN - 1] = 0;
  572. fprintf (aa_stderr,
  573. PROGNAME "353 DtSearchQuery(): dbname='%s' srchtype='%c'\n"
  574. " maxhits=%d keytypes='%s'\n"
  575. " date1='%s' -> %s. date2='%s' -> %s.\n"
  576. " query='%.60s'\n",
  577. db->name, search_type, db->maxhits, sprintbuf,
  578. NULLORSTR (date1), date1str,
  579. NULLORSTR (date2), objdate2fzkstr (usrblk.objdate2),
  580. NULLORSTR (usrblk.query));
  581. fflush (aa_stderr);
  582. }
  583. /* Final engine call, the search itself... */
  584. Opera_Engine ();
  585. END_OF_SEARCH:
  586. if (usrblk.debug & USRDBG_SRCHCMPL) {
  587. /* count msgs */
  588. i = 0;
  589. for (llp = ausapi_msglist; llp != NULL; llp = llp->link)
  590. i++;
  591. fprintf (aa_stderr, PROGNAME "380 Return from Search: "
  592. "retncode=%d hitcount=%ld srchmsgs=%d.\n",
  593. usrblk.retncode, usrblk.dittocount, i);
  594. fflush (aa_stderr);
  595. }
  596. /* Set this func's retn value based on engine's retncode, and return. */
  597. switch (usrblk.retncode) {
  598. case OE_OK:
  599. /*
  600. * Transfer usrblk.dittolist to user's own dittolist
  601. * pointer. Also he MUST use DtSearchFreeResults()
  602. * between calls. Otherwise the code below will cause a
  603. * memory leak.
  604. */
  605. *dittolist = usrblk.dittolist;
  606. *dittocount = usrblk.dittocount;
  607. usrblk.dittolist = NULL;
  608. usrblk.dittocount = 0;
  609. if (stems) {
  610. *stemcount = usrblk.stemcount;
  611. for (i = 0; i < *stemcount; i++) {
  612. strcpy (stems, usrblk.stems[i]);
  613. stems += DtSrMAXWIDTH_HWORD;
  614. }
  615. }
  616. return DtSrOK;
  617. case OE_NOTAVAIL:
  618. return DtSrNOTAVAIL;
  619. case OE_ABORT:
  620. return DtSrABORT;
  621. case OE_REINIT:
  622. return aa_reinit ();
  623. case OE_BAD_QUERY:
  624. /* Query was invalid. Tell the user why. */
  625. if (ausapi_msglist == NULL) {
  626. sprintf (sprintbuf,
  627. CATGETS(dtsearch_catd, MS_ausapi, 806,
  628. "%s Query insufficient or search options "
  629. "incompatible with database '%s' to commence search."),
  630. PROGNAME "806", usrblk.dblk->name);
  631. DtSearchAddMessage (sprintbuf);
  632. }
  633. return DtSrFAIL;
  634. case OE_NOOP:
  635. /* Search was unsuccessful. Msgs should say why. */
  636. return DtSrFAIL;
  637. default:
  638. /*
  639. * Includes OE_SEARCHING, OE_USER_STOP, etc. Probable
  640. * program error. Msgs may or may not say why.
  641. */
  642. return DtSrERROR;
  643. } /* end switch */
  644. } /* DtSearchQuery() */
  645. /************************************************/
  646. /* */
  647. /* DtSearchRetrieve */
  648. /* */
  649. /************************************************/
  650. /* Mallocs and returns cleartext of a record given database address (dba).
  651. * fzkey integers start at bkt #2, just like categories.
  652. * WARNING! USER SHOULD NEITHER MALLOC NOR FREE HIS CLEARTEXT POINTER!
  653. */
  654. int DtSearchRetrieve (
  655. char *dbname, /* 1 - 8 char database name */
  656. DB_ADDR dba, /* database address from dittolist */
  657. char **cleartext, /* cleartext put here (freed first) */
  658. long *clearlen, /* length of returned cleartext */
  659. int *fzkeyi) /* ptr to array of FZKEYSZ integers */
  660. {
  661. if (!valid_dbname (dbname))
  662. return DtSrREINIT;
  663. usrblk.dba = dba;
  664. usrblk.request = OE_GETREC;
  665. Opera_Engine ();
  666. /* Set this func's retn value based on engine's retncode, and return. */
  667. switch (usrblk.retncode) {
  668. case OE_OK:
  669. /*
  670. * WARNING! USER SHOULD NEITHER MALLOC NOR FREE HIS
  671. * CLEARTEXT POINTER.
  672. */
  673. *cleartext = usrblk.cleartext; /* user MUST leave
  674. * cleartext alone */
  675. *clearlen = usrblk.clearlen;
  676. return DtSrOK;
  677. case OE_NOTAVAIL:
  678. return DtSrNOTAVAIL;
  679. case OE_ABORT:
  680. return DtSrABORT;
  681. case OE_REINIT:
  682. return aa_reinit ();
  683. case OE_NOOP:
  684. return DtSrFAIL;
  685. default:
  686. /*
  687. * Includes all unexpected return codes. Probable
  688. * program error. Msgs may or may not say why.
  689. */
  690. return DtSrERROR;
  691. } /* end switch */
  692. } /* DtSearchRetrieve() */
  693. /************************************************/
  694. /* */
  695. /* DtSearchHighlight */
  696. /* */
  697. /************************************************/
  698. /* Mallocs and returns hitwords array for passed text string
  699. * using database and stems array from last search.
  700. * The stems ptr may be NULL, but if specified
  701. * the passed stems array MUST be defined:
  702. * char stems [DtSrMAX_STEMCOUNT] [DtSrMAXWIDTH_HWORD].
  703. */
  704. int DtSearchHighlight (
  705. char *dbname, /* database name */
  706. char *cleartext, /* text to be hilited */
  707. DtSrHitword ** hitwords,
  708. /* where to put hitwords array */
  709. long *hitwcount, /* num items in hitwords array */
  710. int search_type, /* [opt] override save_search_type */
  711. char *stems, /* [opt] override last search stems */
  712. int stemcount /* num stems in stems array */
  713. )
  714. {
  715. int i;
  716. char *cptr;
  717. if (!valid_dbname (dbname))
  718. return DtSrREINIT;
  719. /* copy cleartext to usrblk if necessary */
  720. if (cleartext == NULL || cleartext[0] == 0) {
  721. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_ausapi, 40,
  722. "%1$s Null cleartext. No highlighting performed."),
  723. PROGNAME "349 ", dbname);
  724. DtSearchAddMessage (sprintbuf);
  725. return DtSrERROR;
  726. }
  727. if (cleartext != usrblk.cleartext) {
  728. if (usrblk.cleartext != NULL)
  729. free (usrblk.cleartext);
  730. usrblk.clearlen = strlen (cleartext);
  731. usrblk.cleartext = austext_malloc (usrblk.clearlen + 16,
  732. PROGNAME "267", NULL);
  733. strcpy (usrblk.cleartext, cleartext);
  734. }
  735. if (search_type)
  736. usrblk.search_type = search_type;
  737. else
  738. usrblk.search_type = save_search_type;
  739. if (stems) {
  740. if (stemcount > DtSrMAX_STEMCOUNT) {
  741. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_ausapi, 1072,
  742. "%s Program Error: Stem count (%d) greater than maximum (%d)."),
  743. PROGNAME"1072", stemcount, DtSrMAX_STEMCOUNT);
  744. DtSearchAddMessage (sprintbuf);
  745. return DtSrERROR;
  746. }
  747. usrblk.stemcount = stemcount;
  748. cptr = stems;
  749. for (i = 0; i < stemcount; i++) {
  750. snprintf(usrblk.stems[i], sizeof(usrblk.stems[i]), "%s", cptr);
  751. cptr += DtSrMAXWIDTH_HWORD;
  752. }
  753. }
  754. usrblk.request = OE_HILITE_STEMS;
  755. Opera_Engine ();
  756. /* Set this func's retn value based on engine's retncode, and return. */
  757. switch (usrblk.retncode) {
  758. case OE_OK:
  759. /*
  760. * Note that the following assignment works even if the
  761. * user's hitwords arg is in fact &usrblk.hitwords and
  762. * hitwcount is in fact &usrblk.hitwcount.
  763. */
  764. *hitwords = usrblk.hitwords;
  765. *hitwcount = usrblk.hitwcount;
  766. return DtSrOK;
  767. case OE_ABORT:
  768. return DtSrABORT;
  769. case OE_REINIT:
  770. return aa_reinit ();
  771. case OE_NOOP:
  772. case OE_NOTAVAIL:
  773. return DtSrFAIL;
  774. default:
  775. /*
  776. * Includes OE_BAD_QUERY and all unexpected return
  777. * codes. Probable program error. Msgs may or may not
  778. * say why. In case they don't, put OE's retncode on
  779. * msglist.
  780. */
  781. if (!ausapi_msglist) {
  782. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_ausapi, 1342,
  783. "%s Search Engine Error %d for highlight request for "
  784. "database '%s', hit word count=%ld, search type='%c', text='%.30s'"),
  785. PROGNAME "1342",
  786. usrblk.retncode, dbname, *hitwcount, search_type,
  787. NULLORSTR (cleartext));
  788. DtSearchAddMessage (sprintbuf);
  789. }
  790. return DtSrERROR;
  791. } /* end switch */
  792. } /* DtSearchHighlight() */
  793. /**************************** DTSRAPI.C *************************/