dtsrcreate.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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: change_max_wordsize
  27. * change_min_wordsize
  28. * confirm_ok_to_overwrite
  29. * create_new_dbd
  30. * main
  31. * print_usage
  32. * remove_d9x_file
  33. * user_args_processor
  34. *
  35. * ORIGINS: 27
  36. *
  37. *
  38. * (C) COPYRIGHT International Business Machines Corp. 1993,1996
  39. * All Rights Reserved
  40. * Licensed Materials - Property of IBM
  41. * US Government Users Restricted Rights - Use, duplication or
  42. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  43. */
  44. /************************* DTSRCREATE.C **************************
  45. * $XConsortium: dtsrcreate.c /main/9 1996/09/23 21:02:04 cde-ibm $
  46. * October 1993.
  47. * Program formerly named initausd, in module initausd.c.
  48. * Essentially performs the same function as vista's initdb,
  49. * but uses only the dtsearch.dbd and renames the files during creation.
  50. * Also creates and initializes the first slot, the 'dbrec'.
  51. *
  52. * $Log$
  53. * Revision 2.8 1996/03/25 18:53:33 miker
  54. * Changed FILENAME_MAX to _POSIX_PATH_MAX.
  55. *
  56. * Revision 2.7 1996/02/01 18:16:16 miker
  57. * Changed some arg defaults depending on DTSEARCH definition.
  58. * Deleted BETA definition.
  59. *
  60. * Revision 2.6 1995/12/27 16:10:03 miker
  61. * Permit -wx before -wn on command line.
  62. *
  63. * Revision 2.5 1995/12/07 23:27:51 miker
  64. * Fixed bug: minwordsz was > max when max inited to -1.
  65. * Changed 'Engine Type' to 'Flavor' for AusBuild.
  66. *
  67. * Revision 2.4 1995/10/25 21:09:42 miker
  68. * Added prolog.
  69. *
  70. * Revision 2.3 1995/10/20 21:28:25 miker
  71. * Intelligently look for dtsearch.dbd in 3 places.
  72. *
  73. * Revision 2.2 1995/10/19 21:16:17 miker
  74. * Internally rename database files so it doesn't have to be
  75. * done at open time. Ask permission to overwrite preexisting
  76. * files. Always create databases from current model dtsearch.dbd
  77. * (elminate SECOND CASE). Coincides with libDtvis enhancements.
  78. *
  79. * Revision 2.1 1995/09/22 19:32:18 miker
  80. * Freeze DtSearch 0.1, AusText 2.1.8
  81. *
  82. * Revision 1.2 1995/09/19 21:56:53 miker
  83. * Enabled Japanese language DtSrJPN.
  84. * If DtSearch, use DtSrVERSION instead of AUSAPI_VERSION in banner.
  85. *
  86. * Revision 1.1 1995/08/31 20:50:28 miker
  87. * Initial revision
  88. */
  89. #include "SearchP.h"
  90. #include <limits.h>
  91. #include <errno.h>
  92. #include <ctype.h>
  93. #include <sys/types.h>
  94. #include <sys/stat.h>
  95. #include <locale.h>
  96. #include "vista.h"
  97. #define PROGNAME "DTSRCREATE"
  98. #define DEFAULT_MINWORD (MINWIDTH_TOKEN + 1)
  99. #define STANDARD_MAXWORD (DtSrMAXWIDTH_HWORD - 1)
  100. #define MS_misc 1
  101. #define MS_initausd 12
  102. #define FNAME_MODEL "dtsearch.dbd"
  103. /* The following MUST MATCH vista's dbtype.h! */
  104. #define SIZEOF_FILE_ENTRY 252 /* sizeof(FILE_ENTRY) */
  105. #define DBD_COMPAT_LEN 6
  106. #define START_OF_FT (DBD_COMPAT_LEN + (8 * sizeof(INT)))
  107. /* Values for 'flavor' global variable */
  108. #define AUSTEXT_FLAVOR 'a'
  109. #define DTSEARCH_FLAVOR 'd'
  110. /*------------------ GLOBALS -------------------*/
  111. static int abstrsz = -1;
  112. static char dbname [12] = "<dbname>";
  113. struct or_dbrec dbrec;
  114. static int debug_mode = FALSE;
  115. static char default_cant_open_msg[] =
  116. "%s: %s: %s.\n";
  117. static int fzkeysz = 0;
  118. static int flavor = DTSEARCH_FLAVOR;
  119. static int language = DtSrLaENG;
  120. static int minwordsz = DEFAULT_MINWORD;
  121. static int maxwordsz = INT_MAX;
  122. static int max_ormisc_size;
  123. static int maxwidth_lword;
  124. static int maxwidth_sword;
  125. static char modelpath [_POSIX_PATH_MAX];
  126. /* path/name of model dbd file */
  127. static char newpath [_POSIX_PATH_MAX];
  128. /* path/name for each renamed file */
  129. static char *newextp; /* loc where extension suffixes placed */
  130. static int ok_to_overwrite = FALSE;
  131. static long path_offset = 0;
  132. static int quiet_mode = FALSE;
  133. static char *exttab[] = {
  134. /* Must be in same order as model .dbd file tables */
  135. ".d00", ".d01", ".d21", ".d22", ".d23",
  136. ".k00", ".k01", ".k21", ".k22", ".k23",
  137. NULL };
  138. /* Same as MS_initausd, 213... */
  139. static char default_unable_to_open_msg[] =
  140. "%1$s Unable to open '%2$s':\n %3$s.\a\n";
  141. /************************************************/
  142. /* */
  143. /* confirm_ok_to_overwrite */
  144. /* */
  145. /************************************************/
  146. /* Called whenever we are about to write a new file.
  147. * Checks to see if file preexists. If it does,
  148. * and user has never said it's ok to overwrite,
  149. * prompts for permission to overlay all preexisting files.
  150. * If 'yes', never asks again. If 'no', exits.
  151. * Returns if ok to overwrite, else exits.
  152. */
  153. static void confirm_ok_to_overwrite (char *fname)
  154. {
  155. FILE *fptr;
  156. int i;
  157. if (ok_to_overwrite)
  158. return;
  159. if ((fptr = fopen (newpath, "r")) == NULL)
  160. return;
  161. fclose (fptr);
  162. printf ( catgets(dtsearch_catd, MS_initausd, 12,
  163. "\nFile '%s' already exists.\n"
  164. "Is it ok to overwrite it and other database files? [y,n] ") ,
  165. newpath);
  166. i = tolower (getchar());
  167. if (i == 'y')
  168. ok_to_overwrite = TRUE;
  169. else
  170. DtSearchExit (2);
  171. return;
  172. } /* confirm_ok_to_overwrite() */
  173. /************************************************/
  174. /* */
  175. /* change_max_wordsize */
  176. /* */
  177. /************************************************/
  178. /* Subroutine of user_args_processor().
  179. * Adjusts maxwordsz per user request and allowed sizes of schema.
  180. */
  181. static int change_max_wordsize (char *new_size)
  182. {
  183. int users_newsize;
  184. maxwordsz = users_newsize = atoi (new_size);
  185. /* error if min and max specifications incompatible */
  186. if (minwordsz > maxwordsz) {
  187. printf (catgets (dtsearch_catd, MS_initausd, 5,
  188. PROGNAME" Minimum word size %d greater "
  189. "than maximum word size %d.\n"),
  190. minwordsz, maxwordsz);
  191. return FALSE;
  192. }
  193. /* If necessary, adjust to nearest logical maxwordsz */
  194. if (maxwordsz != maxwidth_sword &&
  195. maxwordsz != maxwidth_lword &&
  196. maxwordsz != DtSrMAXWIDTH_HWORD - 1) {
  197. if (maxwordsz < maxwidth_sword)
  198. maxwordsz = maxwidth_sword;
  199. else if (maxwordsz < maxwidth_lword)
  200. maxwordsz = maxwidth_lword;
  201. else
  202. maxwordsz = DtSrMAXWIDTH_HWORD - 1;
  203. }
  204. if (maxwordsz != users_newsize)
  205. printf (catgets (dtsearch_catd, MS_initausd, 8,
  206. PROGNAME " Adjusted maximum word size to %d.\n"),
  207. maxwordsz);
  208. /* Give user a final warning about large word sizes */
  209. if (maxwordsz > STANDARD_MAXWORD && language != DtSrLaDEU && !quiet_mode)
  210. printf ("%s", catgets (dtsearch_catd, MS_initausd, 10,
  211. PROGNAME" Specifying large maximum word sizes may "
  212. "significantly\n increase storage requirements.\n"));
  213. return TRUE;
  214. } /* change_max_wordsize() */
  215. /************************************************/
  216. /* */
  217. /* change_min_wordsize */
  218. /* */
  219. /************************************************/
  220. /* Subroutine of user_args_processor().
  221. * Adjusts minwordsz per user request.
  222. */
  223. static int change_min_wordsize (char *new_size)
  224. {
  225. int old_minwordsz = minwordsz;
  226. if ((minwordsz = atoi (new_size)) < 0)
  227. return FALSE;
  228. /* error if min and max specifications incompatible */
  229. if (minwordsz > maxwordsz) {
  230. printf (catgets (dtsearch_catd, MS_initausd, 5,
  231. PROGNAME " Minimum word size %d greater than "
  232. "maximum word size %d.\n"),
  233. minwordsz, maxwordsz);
  234. return FALSE;
  235. }
  236. if (!quiet_mode) {
  237. if (minwordsz != old_minwordsz)
  238. printf (catgets (dtsearch_catd, MS_initausd, 6,
  239. PROGNAME " Adjusted minimum word size to %d.\n"),
  240. minwordsz);
  241. /* give user a warning about short word sizes */
  242. if (minwordsz < DEFAULT_MINWORD)
  243. printf ("%s", catgets (dtsearch_catd, MS_initausd, 9,
  244. PROGNAME " Specifying small minimum word sizes"
  245. " may require extensive\n"
  246. " editing of stopword file to prevent significantly\n"
  247. " increased index storage requirements.\n"));
  248. }
  249. return TRUE;
  250. } /* change_min_wordsize() */
  251. /************************************************/
  252. /* */
  253. /* print_usage */
  254. /* */
  255. /************************************************/
  256. static void print_usage (void)
  257. {
  258. int i;
  259. printf (catgets (dtsearch_catd, MS_initausd,
  260. 3,
  261. "\nUSAGE: %s [-options] dbname\n"
  262. " Creates and initializes DtSearch/AusText database files.\n"
  263. " -q Do not print information messages.\n"
  264. " -o Ok to overwrite preexisting database.\n"
  265. " -a<n> Set maximum abstract size to <N> (default per flavor).\n"
  266. " -d<dir> Dir containing "FNAME_MODEL" file if not in dbname dir.\n"
  267. " -wn<n> Change minimum word size to <N>. Default is %d.\n"
  268. " -wx<n> Change maximum word size to <N>. Default per language.\n"
  269. " ---------- Database Flavor ----------\n"
  270. " -fd DtSearch flavor. No documents, only document references\n"
  271. " in abstracts (default).\n"
  272. " -fa AusText flavor. Documents stored in central server repository.\n"
  273. " ------------ Supported Languages ------------\n"
  274. " -l<n> Set language number to <N>. Default is 0. Supported values:\n"
  275. " 0 English-ASCII\n"
  276. " 1 English-Latin1\n"
  277. " 2 Spanish\n"
  278. " 3 French\n"
  279. " 4 Italian\n"
  280. " 5 German\n"
  281. " 6 Japanese-autoknj\n"
  282. " 7 Japanese-knjlist\n"
  283. " <dbname> Optional path prefix, then 1 - 8 character\n"
  284. " database name. Do not specify 'austext' or 'dtsearch'.\n"),
  285. aa_argv0, DEFAULT_MINWORD);
  286. return;
  287. } /* print_usage() */
  288. /************************************************/
  289. /* */
  290. /* user_args_processor */
  291. /* */
  292. /************************************************/
  293. /* Handles command line arguments for main().
  294. * Initializes global variables.
  295. */
  296. static void user_args_processor (int argc, char **argv)
  297. {
  298. int i;
  299. int remaining_slot_space;
  300. char *ptr;
  301. /* Initialize variables prior to parsing command line */
  302. newpath[0] = 0;
  303. modelpath[0] = 0;
  304. if (argc < 2) {
  305. print_usage();
  306. DtSearchExit (2);
  307. }
  308. /* Each pass grabs new parm of "-xxx" format */
  309. for (;;) {
  310. argc--;
  311. argv++;
  312. if (argc <= 0)
  313. break;
  314. ptr = argv[0];
  315. if (ptr[0] != '-')
  316. break;
  317. switch (ptr[1]) {
  318. case 'r': /* unadvertised debug mode */
  319. if (strcmp (ptr, "-russell") == 0) {
  320. debug_mode = TRUE;
  321. puts ("001*** debug mode.");
  322. }
  323. else {
  324. BAD_ARG:
  325. print_usage();
  326. printf (catgets (dtsearch_catd, MS_misc, 9,
  327. "%sInvalid command line argument '%s'.\a\n"),
  328. "\n"PROGNAME" ", ptr);
  329. DtSearchExit (2);
  330. }
  331. break;
  332. case 'a':
  333. /* zero length abstract may be explicity specified */
  334. abstrsz = atoi (ptr + 2);
  335. if (abstrsz < 0 || (abstrsz == 0 && ptr[2] != '0'))
  336. goto BAD_ARG;
  337. break;
  338. case 'q':
  339. quiet_mode = TRUE;
  340. break;
  341. case 'o':
  342. ok_to_overwrite = TRUE;
  343. break;
  344. case 'f':
  345. switch (ptr[2]) {
  346. case AUSTEXT_FLAVOR:
  347. case DTSEARCH_FLAVOR:
  348. flavor = ptr[2];
  349. break;
  350. default:
  351. goto BAD_ARG;
  352. }
  353. break;
  354. case 'w': /* change min (-wn..) or max (-wx..) word size */
  355. switch (ptr[2]) {
  356. case 'x':
  357. if (!change_max_wordsize (ptr + 3))
  358. goto BAD_ARG;
  359. break;
  360. case 'n':
  361. if (!change_min_wordsize (ptr + 3))
  362. goto BAD_ARG;
  363. break;
  364. default:
  365. goto BAD_ARG;
  366. }
  367. break;
  368. case 'd': /* special path name for model .dbd */
  369. strncpy (modelpath, ptr + 2, sizeof(modelpath));
  370. modelpath [sizeof(modelpath) - sizeof(FNAME_MODEL) - 4] = 0;
  371. ensure_end_slash (modelpath);
  372. strcat (modelpath, FNAME_MODEL);
  373. break;
  374. case 'l':
  375. /* Note that custom, unsupported languages
  376. * greater than DtSrLaLAST are permitted.
  377. */
  378. language = atoi (ptr + 2);
  379. if (language < 0)
  380. goto BAD_ARG;
  381. if (!quiet_mode && language > DtSrLaLAST)
  382. printf ( catgets(dtsearch_catd, MS_initausd, 13,
  383. "%s Warning! you have specified "
  384. "an unsupported, custom language.\n"
  385. " You will have to provide your own "
  386. "language loaders at run time\n"
  387. " in user function 'load_custom_language' "
  388. "to access this database.\a\n"),
  389. PROGNAME"444");
  390. break;
  391. default:
  392. printf (catgets (dtsearch_catd, MS_misc, 10,
  393. "%sIgnored unknown command line argument '%s'.\n"),
  394. PROGNAME " ", ptr);
  395. break;
  396. } /* end switch */
  397. } /* end parse of cmd line options beginning with '-' */
  398. /* Only required arg is new database name,
  399. * including optional path prefix.
  400. * Load newpath and newextp, leaving room
  401. * for long dbnames and .xxx extensions.
  402. */
  403. if (argc <= 0) {
  404. print_usage();
  405. printf (catgets (dtsearch_catd, MS_misc, 18,
  406. "%sDatabase name not specified.\n\a"), "\n"PROGNAME" ");
  407. DtSearchExit(2);
  408. }
  409. strncpy (newpath, argv[0], sizeof (newpath));
  410. newpath [sizeof(newpath) - 12] = 0;
  411. newextp = newpath + strlen (newpath);
  412. /* Get just the 1 - 8 char database name by moving ptr
  413. * backwards until first non-alphanumeric character
  414. * (such as a ":" in the dos drive id or a slash between directories),
  415. * or to the beginning of string.
  416. * Then test database name for validity.
  417. */
  418. for (ptr = newpath + strlen(newpath) - 1; ptr >= newpath; ptr--)
  419. if (!isalnum (*ptr)) {
  420. ptr++;
  421. break;
  422. }
  423. if (ptr < newpath)
  424. ptr = newpath;
  425. i = strlen (ptr);
  426. if (i < 1 || i > 8) {
  427. BAD_DBNAME:
  428. print_usage();
  429. printf (catgets (dtsearch_catd, MS_misc, 11,
  430. "%sInvalid database name '%s'.\a\n"),
  431. "\n"PROGNAME"346 ", ptr);
  432. DtSearchExit(2);
  433. }
  434. path_offset = ptr - newpath;
  435. strcpy (dbname, ptr); /* save it */
  436. if (strcmp (dbname, "austext") == 0 || strcmp (dbname, "dtsearch") == 0) {
  437. goto BAD_DBNAME;
  438. }
  439. /* Ensure semantic processing specified only for english language */
  440. if (fzkeysz != 0 && language != DtSrLaENG && language != DtSrLaENG2) {
  441. print_usage();
  442. printf ( catgets(dtsearch_catd, MS_initausd, 14,
  443. "\n%s semantic processing is only available "
  444. "for English language databases.\n\a") ,
  445. PROGNAME"340");
  446. DtSearchExit(2);
  447. }
  448. /* Unless overridden by user args,
  449. * initialize abstract based on flavor.
  450. * The abstract size defaults to the remaining
  451. * space in the final misc slot after the fzkey.
  452. * However if the user specified a specific
  453. * abstract size, it may be adjusted later
  454. * to fill up the last slot.
  455. */
  456. if (abstrsz == -1)
  457. abstrsz = max_ormisc_size - (fzkeysz % max_ormisc_size);
  458. /* Default maxword size is 'short', except for German */
  459. if (maxwordsz == INT_MAX)
  460. maxwordsz = STANDARD_MAXWORD;
  461. if (debug_mode)
  462. printf ("002*** userargs: modelpath='%s' newpath='%s'\n"
  463. " fzkeysz=%d abstrsz=%d\n",
  464. modelpath, newpath, fzkeysz, abstrsz);
  465. return;
  466. } /* user_args_processor() */
  467. /************************************************/
  468. /* */
  469. /* remove_d9x_file */
  470. /* */
  471. /************************************************/
  472. static void remove_d9x_file (char *extension)
  473. {
  474. strcpy (newextp, extension);
  475. if (debug_mode)
  476. printf ("094*** delete '%s'.\n", newpath);
  477. if (remove (newpath) != 0) {
  478. /* 'file not found' is not an error */
  479. if (errno != ENOENT) {
  480. printf (catgets (dtsearch_catd, MS_initausd, 244,
  481. PROGNAME "244 Unable to remove '%s': %s\n"),
  482. newpath, strerror (errno));
  483. DtSearchExit (5);
  484. }
  485. }
  486. return;
  487. } /* remove_d9x_file() */
  488. /************************************************/
  489. /* */
  490. /* create_new_dbd */
  491. /* */
  492. /************************************************/
  493. /* Copies and moves binary contents in passed, preopened
  494. * model .dbd file (f) to new dbd file in target directory.
  495. * Rename the internal .d00, etc filenames to match dbname.
  496. */
  497. static void create_new_dbd (FILE *f)
  498. {
  499. FILE *g; /* target dbd file */
  500. int i;
  501. static char *nocopy_msg =
  502. "%s Unable to copy '%s' to '%s':\n %s\a\n";
  503. /* (Same as dtsearch.msg: MS_initausd, 214) */
  504. static char zeros[] =
  505. "\0\0\0\0\0\0\0\0\0\0\0\0";
  506. strcpy (newextp, ".dbd");
  507. if (debug_mode)
  508. printf (PROGNAME"507 create_new_dbd '%s'\n", newpath);
  509. /* If new .dbd file preexists, make sure it is writable */
  510. confirm_ok_to_overwrite (newpath);
  511. if (chmod (newpath, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) {
  512. if (errno != ENOENT) {
  513. printf (catgets (dtsearch_catd, MS_initausd, 214, nocopy_msg),
  514. PROGNAME"515", modelpath, newpath, strerror(errno));
  515. DtSearchExit (15);
  516. }
  517. }
  518. if ((g = fopen (newpath, "w+b")) == NULL) {
  519. printf (catgets (dtsearch_catd, MS_initausd, 214, nocopy_msg),
  520. PROGNAME"509", modelpath, newpath, strerror(errno));
  521. DtSearchExit (4);
  522. }
  523. errno = 0;
  524. while ((i = fgetc (f)) != EOF)
  525. fputc (i, g);
  526. if (errno) {
  527. printf (catgets (dtsearch_catd, MS_initausd, 214, nocopy_msg),
  528. PROGNAME"531", modelpath, newpath, strerror(errno));
  529. DtSearchExit (13);
  530. }
  531. /* Now reposition the write head in the new dbd file
  532. * to rename the filenames. Rename each internal file
  533. * name to '<newdbname>.xxx'.
  534. */
  535. for (i = 0; exttab[i] != NULL; i++) {
  536. fseek (g, START_OF_FT + (i * SIZEOF_FILE_ENTRY), SEEK_SET);
  537. fprintf (g, "%s%s", dbname, exttab[i]);
  538. fwrite (zeros, sizeof(char), sizeof(zeros), g);
  539. }
  540. /* The new dbd file only has to be readable */
  541. fclose (g);
  542. chmod (newpath, S_IRUSR | S_IRGRP | S_IROTH);
  543. return;
  544. } /* create_new_dbd() */
  545. /************************************************/
  546. /* */
  547. /* main */
  548. /* */
  549. /************************************************/
  550. /* 1. CREATE or find database dictionary (.dbd file).
  551. * 2. CREATE empty 'dtsearch' database files.
  552. * 3. OPEN 'dtsearch' database.
  553. * 4. INITIALIZE the database.
  554. * 5. WRITE dbrec after initializing it.
  555. * 6. RENAME each database file.
  556. * 7. UNLINK (delete) d9x files.
  557. */
  558. int main (int argc, char *argv[])
  559. {
  560. int i;
  561. char *ptr;
  562. FILE *f;
  563. struct or_miscrec miscrec;
  564. struct or_swordrec swordrec;
  565. struct or_lwordrec lwordrec;
  566. setlocale (LC_ALL, "");
  567. dtsearch_catd = catopen (FNAME_DTSRCAT, 0);
  568. aa_argv0 = argv[0];
  569. max_ormisc_size = sizeof (miscrec.or_misc);
  570. maxwidth_sword = sizeof (swordrec.or_swordkey) - 1;
  571. maxwidth_lword = sizeof (lwordrec.or_lwordkey) - 1;
  572. printf (catgets (dtsearch_catd, MS_misc, 4,
  573. "%s Version %s.\n"),
  574. aa_argv0,
  575. DtSrVERSION
  576. );
  577. /* Handle cmd line args. Init global variables. */
  578. user_args_processor (argc, argv);
  579. /* ------- copy model .dbd to new .dbd ------- */
  580. /* CASE 1: If user specified -d special alternative
  581. * directory for model .dbd, it should be there.
  582. */
  583. if (modelpath[0] != 0) {
  584. if (debug_mode)
  585. printf (PROGNAME"628 Try opening '%s' (-d dir).\n", modelpath);
  586. if ((f = fopen (modelpath, "rb")) != NULL) {
  587. if (debug_mode)
  588. puts (PROGNAME"638 Found it!");
  589. create_new_dbd (f);
  590. fclose (f);
  591. goto DBD_OKAY;
  592. }
  593. else {
  594. print_usage();
  595. printf (catgets (dtsearch_catd, MS_initausd, 213,
  596. default_unable_to_open_msg),
  597. "\n"PROGNAME"302", modelpath, strerror(errno));
  598. DtSearchExit (4);
  599. }
  600. } /* end CASE 1 */
  601. /* CASE 2: If model .dbd is in current directory, use it.
  602. * If error is anything other than 'cant find file', quit now.
  603. */
  604. if (debug_mode)
  605. printf (PROGNAME"649 Try opening '%s' (curr dir).\n", FNAME_MODEL);
  606. if ((f = fopen (FNAME_MODEL, "rb")) != NULL) {
  607. if (debug_mode)
  608. puts (PROGNAME"660 Found it!");
  609. create_new_dbd (f);
  610. fclose (f);
  611. goto DBD_OKAY;
  612. }
  613. else if (errno != ENOENT) {
  614. print_usage();
  615. printf (catgets (dtsearch_catd, MS_initausd, 213,
  616. default_unable_to_open_msg),
  617. "\n"PROGNAME"655", FNAME_MODEL, strerror(errno));
  618. DtSearchExit (4);
  619. } /* end else CASE 2 */
  620. /* CASE 3: Last chance. Look for model .dbd in target directory.
  621. * At this point have to quit on any error.
  622. */
  623. strcpy (modelpath, newpath);
  624. strcpy (modelpath + path_offset, FNAME_MODEL);
  625. if (debug_mode)
  626. printf (PROGNAME"672 Try opening '%s' (new dir).\n", modelpath);
  627. if ((f = fopen (modelpath, "rb")) != NULL) {
  628. if (debug_mode)
  629. puts (PROGNAME"675 Found it!");
  630. create_new_dbd (f);
  631. fclose (f);
  632. goto DBD_OKAY;
  633. }
  634. if (debug_mode)
  635. puts (PROGNAME"682 Never found it!");
  636. print_usage();
  637. printf (catgets (dtsearch_catd, MS_initausd, 213,
  638. default_unable_to_open_msg),
  639. "\n"PROGNAME"686", FNAME_MODEL,
  640. "Not found in either current or target directories. Use -d option\a");
  641. DtSearchExit (4);
  642. DBD_OKAY:
  643. /* Open a new database */
  644. *newextp = 0; /* use no extension when opening database */
  645. if (debug_mode)
  646. printf ("040*** d_open newpath = '%s'.\n", newpath);
  647. d_open (newpath, "o");
  648. if (db_status != S_OKAY) {
  649. printf (catgets (dtsearch_catd, MS_initausd, 230,
  650. PROGNAME "230 Could not open database '%s'.\n"), newpath);
  651. puts (vista_msg (PROGNAME "231"));
  652. DtSearchExit (3);
  653. }
  654. austext_exit_dbms = (void (*) (int)) d_close; /* emerg exit func */
  655. /* initialize the 'dtsearch' database */
  656. if (debug_mode)
  657. printf ("042*** d_initialize.\n");
  658. d_initialize (0);
  659. if (db_status != S_OKAY) {
  660. printf (catgets (dtsearch_catd, MS_initausd, 239,
  661. PROGNAME "239 Could not initialize database '%s'.\n"), newpath);
  662. puts (vista_msg (PROGNAME "240"));
  663. DtSearchExit (3);
  664. }
  665. /* Create and initialize dbrec database header record in first slot.
  666. * First fill entire record with binary zeros.
  667. * Then set specific values as specified by flavor on command line.
  668. * For now most values are hard-coded.
  669. */
  670. if (debug_mode)
  671. printf ("050*** create dbrec.\n");
  672. memset (&dbrec, 0, sizeof (dbrec));
  673. /* Init fields that are completely independent */
  674. dbrec.or_language = (DtSrINT16) language;
  675. dbrec.or_maxwordsz = (DtSrINT16) maxwordsz;
  676. dbrec.or_minwordsz = (DtSrINT16) minwordsz;
  677. dbrec.or_fzkeysz = (DtSrINT16) fzkeysz;
  678. dbrec.or_abstrsz = (DtSrINT16) abstrsz;
  679. dbrec.or_dbflags = ORD_NONOTES | ORD_NOMARKDEL | ORD_XWORDS;
  680. strncpy (dbrec.or_version, SCHEMA_VERSION, sizeof(dbrec.or_version));
  681. dbrec.or_version [sizeof(dbrec.or_version) - 1] = 0;
  682. /* Load dbrec's recslots fields based on correct number
  683. * of misc recs required to hold user's abstract.
  684. * Round abstrsz upward if there is any space left on last misc rec.
  685. */
  686. dbrec.or_recslots = 1; /* start with obj rec itself */
  687. for (i = dbrec.or_fzkeysz + dbrec.or_abstrsz; i > 0; i -= max_ormisc_size)
  688. dbrec.or_recslots++;
  689. if (i < 0) {
  690. /* Add in difference to INCREASE abstrsz */
  691. dbrec.or_abstrsz -= i;
  692. printf (catgets (dtsearch_catd, MS_misc, 433,
  693. "%1$sAdjusted maximum abstract size upward to %2$hd.\n"),
  694. PROGNAME "433 ", dbrec.or_abstrsz);
  695. }
  696. /* Init fields that are dependent on language */
  697. switch (language) {
  698. case DtSrLaENG:
  699. case DtSrLaENG2:
  700. dbrec.or_dbflags |= ORD_XSTEMS;
  701. break;
  702. default:
  703. break;
  704. }
  705. /* Init fields that are dependent on flavor */
  706. if (flavor == AUSTEXT_FLAVOR) {
  707. dbrec.or_dbaccess = ORA_BLOB;
  708. dbrec.or_compflags = ORC_COMPBLOB;
  709. dbrec.or_hufid = -1L; /* -1 = use huffman compression, but
  710. * hufid not yet known. */
  711. dbrec.or_dbotype = DtSrObjTEXT;
  712. }
  713. else { /* default flavor == DTSEARCH_FLAVOR */
  714. dbrec.or_dbaccess = ORA_NOTAVAIL;
  715. }
  716. if (!quiet_mode) {
  717. /******putchar ('\n');******/
  718. print_dbrec (newpath, &dbrec);
  719. fflush (stdout);
  720. }
  721. swab_dbrec (&dbrec, HTON);
  722. if (debug_mode)
  723. printf ("060*** fillnew dbrec.\n");
  724. d_fillnew (OR_DBREC, &dbrec, 0);
  725. if (db_status != S_OKAY) {
  726. printf ("%s", catgets (dtsearch_catd, MS_initausd, 509,
  727. PROGNAME "509 Could not initialize database header record.\n"));
  728. puts (vista_msg (PROGNAME "510"));
  729. DtSearchExit (3);
  730. }
  731. /* Close the database */
  732. d_close ();
  733. austext_exit_dbms = NULL; /* emerg exit no longer required */
  734. /* Delete all nonvista (inverted index) database files (.d9x) */
  735. remove_d9x_file (".d97");
  736. remove_d9x_file (".d98");
  737. remove_d9x_file (".d99");
  738. *newextp = 0; /* no extension suffixes for next msgs */
  739. printf (catgets (dtsearch_catd, MS_initausd, 24,
  740. PROGNAME " Successfully initialized database '%s'.\n"), newpath);
  741. return 0;
  742. } /* main() */
  743. /************************* DTSRCREATE.C **************************/