dtoeinit.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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: UNMALLOC
  27. * dump_dblk
  28. * oe_initialize
  29. * oe_uninitialize
  30. *
  31. * ORIGINS: 27
  32. *
  33. *
  34. * (C) COPYRIGHT International Business Machines Corp. 1991,1995
  35. * All Rights Reserved
  36. * Licensed Materials - Property of IBM
  37. * US Government Users Restricted Rights - Use, duplication or
  38. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  39. */
  40. /******************************* DTOEINIT.C ********************************
  41. * $XConsortium: dtoeinit.c /main/6 1996/11/21 19:49:29 drk $
  42. * Sept 1991.
  43. * Contains oe_initialize() function from universal "Opera Engine" code.
  44. * Has been separated out because it is the largest function and
  45. * needs to be tested separately.
  46. *
  47. * $Log$
  48. * Revision 2.4 1996/03/13 22:52:04 miker
  49. * Enabled several language loading debug requests from API.
  50. *
  51. * Revision 2.3 1995/10/25 21:59:27 miker
  52. * Renamed from oeinit.c. Added prolog.
  53. *
  54. * Log: oeinit.c,v
  55. * Revision 2.2 1995/10/02 20:39:22 miker
  56. * Added zbflags arg to load_semantic().
  57. *
  58. * Revision 2.1 1995/09/22 21:38:53 miker
  59. * Freeze DtSearch 0.1, AusText 2.1.8
  60. *
  61. * Revision 1.14 1995/09/05 18:58:01 miker
  62. * Changed all socblk refs to universal, global usrblk (no more merges).
  63. * Deleted numerous globals. Added DTSEARCH define.
  64. * Conflated all msglists to one ausapi_msglist. All for DtSearch...
  65. */
  66. #include "SearchE.h"
  67. #include <string.h>
  68. #include <sys/stat.h>
  69. #include <stdlib.h>
  70. #define XOS_USE_NO_LOCKING
  71. #define X_INCLUDE_TIME_H
  72. #include <X11/Xos_r.h>
  73. #define PROGNAME "DTOEINIT"
  74. #define MS_misc 1
  75. #define MS_oeinit 9
  76. extern int debugging_loadlang;
  77. /************************************************/
  78. /* */
  79. /* UNMALLOC */
  80. /* */
  81. /************************************************/
  82. /* macro to test and free any malloc'ed pointer */
  83. #define UNMALLOC(ptr) if(ptr){free(ptr);ptr=NULL;}
  84. /********************************/
  85. /* */
  86. /* dump_dblk */
  87. /* */
  88. /********************************/
  89. /* Dumps values from passed dblk.
  90. * Used only for debugging initialization.
  91. */
  92. static void dump_dblk (char *msgprefix, DBLK *d)
  93. {
  94. int i;
  95. fprintf (aa_stderr, "%s: DBLK v#%d name='%s' label='%s', path='%s'\n",
  96. NULLORSTR (msgprefix), d->vistano, NULLORSTR (d->name),
  97. NULLORSTR (d->label), NULLORSTR (d->path));
  98. fprintf (aa_stderr, " mx=%d kt=", d->maxhits);
  99. for (i = 0; i < d->ktcount; i++) {
  100. fputc (' ', aa_stderr);
  101. if (d->keytypes[i].is_selected)
  102. fputc ('*', aa_stderr);
  103. fputc (d->keytypes[i].ktchar, aa_stderr);
  104. }
  105. fputc ('\n', aa_stderr);
  106. return;
  107. } /* dump_dblk() */
  108. /************************************************/
  109. /* */
  110. /* oe_initialize */
  111. /* */
  112. /************************************************/
  113. /* - verify version number compatibilities.
  114. * - initialize socblk and OE_... globals.
  115. * - load site defaults ocf file and create database list.
  116. * - load dictionaries.
  117. * - merge site defaults into usrblk.
  118. * - open database(s).
  119. */
  120. void oe_initialize (void)
  121. {
  122. char *ptr;
  123. int good_dblk_count = 0;
  124. DBLK *db, *bad_db, **lastlink;
  125. char sprintbuf[1024];
  126. FILE *stream;
  127. OEFTAB *oef;
  128. if (usrblk.debug & USRDBG_RARE)
  129. fprintf (aa_stderr, PROGNAME "555 Entered Engine Initialization.\n");
  130. debugging_loadlang = (usrblk.debug & USRDBG_RARE);
  131. /* Initialize most of the engine's globals.
  132. * OE_bit_vector_size is set in ve_initialize().
  133. * OE_bmhtab... are set by boyer-moore string searches only.
  134. * OE_prodname is initialized by compiler and maybe changed by main().
  135. * OE_expiration is initialized by compiler and never reset.
  136. * OE_sitecnfg_fname is initialized by ausapi or loadocf and never reset.
  137. * OE_sitecnfg_mtime is initialized by oe_initialize() and never reset.
  138. */
  139. global_memory_ptr = NULL;
  140. austext_exit_mem = (void (*) (int)) release_shm_mem;
  141. OE_flags = 0L;
  142. OE_objsize = 0L;
  143. OE_search_type = 'P'; /* default is statistical searches */
  144. OE_words_hitlimit = WORDS_HITLIMIT;
  145. OE_enable_markdel = FALSE; /* former lvl2 default: TRUE */
  146. OE_enable_usernotes = FALSE; /* former lvl2 default: TRUE */
  147. OE_fastdecode = FALSE; /* former lvl2 default: TRUE */
  148. OE_fileio = "-OFF"; /* former lvl2 default: "-ON" */
  149. OE_uppercase_keys = FALSE; /* former lvl2 default: TRUE */
  150. OEF_audit = FNAME_AUDIT;
  151. OEF_discard = FNAME_DISCARD_DATA;
  152. OEF_news = FNAME_SITENEWS;
  153. OEF_notesnot = FNAME_NOTES_BAC;
  154. OEF_notessem = FNAME_NOTES_SEM;
  155. OEF_readme = FNAME_README;
  156. for (oef = oef_table; oef->id != NULL; oef++)
  157. oef->previously_specified = FALSE;
  158. /* Verify version number compatibility between engine and UI.
  159. * Only checked if request is OE_INITIALIZE, not on reinits.
  160. */
  161. if (usrblk.request == OE_INITIALIZE) {
  162. if (!is_compatible_version (usrblk.query, SCHEMA_VERSION) ||
  163. !is_compatible_version (usrblk.query, PROTOCOL_VERSION)) {
  164. if (usrblk.query != NULL)
  165. ptr = usrblk.query;
  166. else
  167. ptr = CATGETS(dtsearch_catd, MS_misc, 6, "<unknown>");
  168. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_oeinit, 137,
  169. "%s User Interface version %s and Engine "
  170. "version %s are incompatible."),
  171. PROGNAME"137", ptr, AUSAPI_VERSION);
  172. DtSearchAddMessage (sprintbuf);
  173. usrblk.retncode = OE_NOTAVAIL;
  174. return;
  175. }
  176. }
  177. /* Load site configuration (ocf) file and create dblks list */
  178. if (!load_ocf()) {
  179. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_oeinit, 202,
  180. "%s Initialization failed due to errors in configuration file."),
  181. PROGNAME"202");
  182. DtSearchAddMessage (sprintbuf);
  183. usrblk.retncode = OE_NOTAVAIL;
  184. return;
  185. }
  186. /* Had to wait to write audit file till after site config set the flags */
  187. if (OE_flags & OE_AUDIT || usrblk.debug & USRDBG_RARE) {
  188. sprintf (sprintbuf,
  189. "ENGINEINIT='%s' UVER='%s' " AUDIT_WHOWHEN "\n",
  190. AUSAPI_VERSION, usrblk.query, usrblk.userid,
  191. nowstring (NULL));
  192. if (usrblk.debug & USRDBG_RARE)
  193. fprintf (aa_stderr, PROGNAME "362 %s", sprintbuf);
  194. if (OE_flags & OE_AUDIT) {
  195. if ((stream = fopen (OEF_audit, "a ")) != NULL)
  196. /* the blank in "a " works around old aix bug */
  197. {
  198. fputs (sprintbuf, stream);
  199. fclose (stream);
  200. }
  201. }
  202. } /* endif to write to audit file */
  203. /* ---- DATABASES PASSES #1 - #3 ------------------------------------
  204. * Call vista to open databases and load system (dbrec) records.
  205. * Where possible, this call will forgive errors by just
  206. * unlinking the offending dblk from the dblist.
  207. * However loss of all dblks equals a fatal error.
  208. */
  209. if (!ve_initialize()) {
  210. INIT_FAILED:
  211. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_oeinit, 266,
  212. "%s Initialization failed due to errors in database,\n"
  213. " language, or related files."),
  214. PROGNAME "266");
  215. DtSearchAddMessage (sprintbuf);
  216. usrblk.retncode = OE_NOTAVAIL;
  217. return;
  218. }
  219. /* ---- DATABASES PASS #4 ------------------------------------
  220. * Load each database's language and semantic files.
  221. * If an error is discovered in any one dblk,
  222. * it is unlinked from dblist.
  223. * However loss of all dblks on dblist is fatal.
  224. */
  225. good_dblk_count = 0;
  226. db = usrblk.dblist; /* could already = NULL after ve_initialize() */
  227. lastlink = &usrblk.dblist;
  228. while (db != NULL) {
  229. if (!load_language (db, usrblk.dblist))
  230. goto DELETE_DB;
  231. /*
  232. * This dblk successfully loaded its language and and semantic
  233. * files. If no gui label was provided, set it to the
  234. * database name. Then increment pointers and continue.
  235. */
  236. if (db->label == NULL)
  237. db->label = strdup (db->name);
  238. else if (db->label[0] == 0)
  239. db->label = strdup (db->name);
  240. good_dblk_count++;
  241. lastlink = &db->link;
  242. db = db->link;
  243. continue;
  244. DELETE_DB:
  245. /*
  246. * One or more language or semantic files could not be loaded for
  247. * this dblk. Unlink it and don't increment pointers.
  248. */
  249. bad_db = db; /* temp save */
  250. *lastlink = db->link;
  251. db = db->link;
  252. free (bad_db);
  253. } /* end loop that loads all database files */
  254. /* Abort if fatal dictionary load errors */
  255. if (good_dblk_count <= 0) {
  256. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_misc, 8,
  257. "%s No valid databases remain."),
  258. PROGNAME"265");
  259. DtSearchAddMessage (sprintbuf);
  260. goto INIT_FAILED;
  261. }
  262. memset (&saveusr, 0, sizeof (SAVEUSR));
  263. OE_flags |= OE_INITOK;
  264. if (usrblk.debug & USRDBG_RARE) {
  265. struct tm *time_ptr;
  266. _Xltimeparams localtime_buf;
  267. if (*OE_expiration != 0)
  268. {
  269. time_ptr = _XLocaltime(OE_expiration, localtime_buf);
  270. strftime (sprintbuf, 100, "%x", time_ptr);
  271. }
  272. else
  273. strcpy (sprintbuf, "0");
  274. if (OE_sitecnfg_mtime != 0)
  275. {
  276. time_ptr = _XLocaltime(&OE_sitecnfg_mtime, localtime_buf);
  277. strftime (sprintbuf + 100, 100, "%x,%X", time_ptr);
  278. }
  279. else
  280. strcpy (sprintbuf + 100, "0");
  281. fprintf (aa_stderr, PROGNAME "666 Engine Initialization Completed.\n"
  282. " usrblk.flags=%ld(x%04lx), usrblk.debug=%ld(x%04lx).\n"
  283. " OE_flags=%ld(x%04lx), expiration=%s, sitecnfg=%s.\n"
  284. ,usrblk.flags, usrblk.flags, usrblk.debug, usrblk.debug
  285. ,OE_flags, OE_flags, sprintbuf, sprintbuf + 100
  286. );
  287. }
  288. usrblk.retncode = OE_OK;
  289. return;
  290. } /* oe_initialize() */
  291. /************************************************/
  292. /* */
  293. /* oe_uninitialize */
  294. /* */
  295. /************************************************/
  296. /* Called by Opera_Engine() whenever the site config file
  297. * has been altered. Closes databases, frees all allocated storage,
  298. * and resets everything so oe_initialize() can be re-called.
  299. * Allows administrator to swap to newer, updated databases
  300. * without forcing users to shutdown by changing PATH value
  301. * in site config file.
  302. */
  303. void oe_uninitialize (void)
  304. {
  305. DBLK *db, *nextdb;
  306. OEFTAB *oef;
  307. ve_shutdown (); /* close all databases */
  308. UNMALLOC (usrblk.abstrbuf);
  309. /* release shared memory, suffixes array, etc, if any */
  310. release_shm_mem ();
  311. /* Free allocated storage in oef table and reset the default filenames. */
  312. for (oef = oef_table; oef->id != NULL; oef++) {
  313. if (oef->previously_specified) {
  314. oef->previously_specified = FALSE;
  315. free (*(oef->OEFptr));
  316. }
  317. }
  318. db = usrblk.dblist;
  319. while (db != NULL) {
  320. /* Save next dblk so we'll know where to go after freeing this one */
  321. nextdb = db->link;
  322. UNMALLOC (db->path);
  323. UNMALLOC (db->keytypes);
  324. if (db->iifile != NULL) {
  325. fclose (db->iifile);
  326. db->iifile = NULL;
  327. }
  328. if (db->syofile != NULL) {
  329. fclose (db->syofile);
  330. db->syofile = NULL;
  331. }
  332. if (db->syifile != NULL) {
  333. fclose (db->syifile);
  334. db->syifile = NULL;
  335. }
  336. unload_language (db);
  337. free (db);
  338. db = nextdb;
  339. }
  340. usrblk.dblist = NULL;
  341. usrblk.dblk = NULL;
  342. return;
  343. } /* oe_uninitialize() */
  344. /******************************* DTOEINIT.C ********************************/