dtsrcreate.c 25 KB

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