home *** CD-ROM | disk | FTP | other *** search
- /* ==( help/rwi.c )== */
- /* ----------------------------------------------- */
- /* Pro-C Copyright (C) 1988 - 1990 Vestronix Inc. */
- /* Modification to this source is not supported */
- /* by Vestronix Inc. */
- /* All Rights Reserved */
- /* ----------------------------------------------- */
- /* Written JPK 26-Sep-88 */
- /* Modified Geo 12-Dec-89 See comments below */
- /* ----------------------------------------------- */
- /* %W% (%H% %T%) */
-
- /*
- * Modifications
- *
- * 12-Dec-89 Geo - V2 version with variable lines
- * 25-Oct-89 Geo - 1.32 Merge
- *
- *
- */
-
- /* contains the routines that handle the index files */
-
- /*
- * Routines in this file :
- *
- * int read_total(void);
- * calculates the number of records in the index file
- *
- * int read_index(int);
- * reads the index file and fills the index structure with the
- * appropriate record
- *
- * void write_index(int);
- * writes the index structure to the index file
- *
- * void build_ndxfile(void);
- * builds the index file from the help text file. writes a record
- * to the file on every occurence of \f
- *
- * int read_help_index(int);
- * reads the temporary index file and fills the structure with the
- * position of the requested text block
- *
- * int build_help_ndxfile(void);
- * builds the temporary index file of the individual help records
- *
- */
-
- # include <stdio.h>
- # include <bench.h>
- # include <fileio.h>
- # include "help.h"
-
- # ifdef UNIX
- # include <setjmp.h>
- # include <signal.h>
- # endif
-
- # ifdef ANSI
- long fsize(int);
- # else
- long fsize();
- # endif
-
- static int tfd; /* file descriptor for help text index file */
-
- int read_total()
- {
- long size;
-
- if ((size = fsize(nfd)) == -1)
- return(-1);
-
- /* 0 .. n -1 prob */
- return( (int)((size / sizeof(struct help_ndx)) - 1) );
- }
-
- int check_index(num)
- int num;
- {
- if ((long)(num * sizeof(struct help_ndx)) >= fsize(nfd))
- return(FALSE);
-
- return(TRUE);
- }
-
- #ifdef UNIX
- static jmp_buf alarmjmp;
- void (*oldalarmfunc)();
-
- static void alarmsigstub()
- {
- longjmp(alarmjmp, -1);
- }
- #endif
-
- int lock_index_file(locktyp)
- {
- #ifdef UNIX
- struct flock ldesc;
- int i;
-
- switch(locktyp)
- {
- case ULOK:
- ldesc.l_type = F_UNLCK;
- break;
- case RLOK:
- ldesc.l_type = F_RDLCK;
- break;
- case WLOK:
- ldesc.l_type = F_WRLCK;
- break;
- default:
- errmsg("lock_index_file(): Bad lock type.");
- break;
- }
- ldesc.l_whence = 0L;
- ldesc.l_start = 0L;
- ldesc.l_len = 0L;
-
- if ((i = setjmp(alarmjmp)) == 0)
- {
- oldalarmfunc = signal(SIGALRM, alarmsigstub);
- alarm(5);
- i = fcntl(nfd, F_SETLKW, &ldesc);
- }
- alarm(0);
- signal(SIGALRM, oldalarmfunc);
- if (i != 0)
- return(FALSE);
- else
- #endif
- return(TRUE);
- }
-
- int lock_index(num, locktyp)
- int num;
- {
- /* Check it exists first */
- if (!check_index(num))
- {
- # ifdef HDEBUG
- errmsg("lock_index() : index does not exist");
- # endif
- return(FALSE);
- }
- /* read in the index records */
- if (lok_rec(nfd, num, locktyp) == -1)
- return(FALSE);
-
- return(TRUE);
- }
-
- int read_index(num)
- int num;
- {
- /* Check it exists first */
- if (!check_index(num))
- {
- # ifdef HDEBUG
- errmsg("read_index() : index does not exist");
- # endif
- return(FALSE);
- }
- /* read in the index records */
- get_rec(nfd, num, (char *)&h_ndx);
- return(TRUE);
- }
-
- void write_index(num)
- int num;
- {
- /* write out index record */
- put_rec(nfd, num, (char *)&h_ndx); /* Errors done by relio.c */
- }
-
-
-
- void build_ndxfile()
- {
- int ch;
- struct help_ndx ndx;
-
- statmsg("Building help index...");
-
- ndx.seekpos = 0L;
- ndx.size = 0;
- put_garbage_rec(&ndx);
-
- fseek(hfptr, 0L, SEEK_SET);
- while(ndx.seekpos != fsize(fileno(hfptr)))
- {
- if (read_hdr(&ndx) == FALSE)
- return;
- if (h_header.helpno == 0xffff)
- while((ch = getc(hfptr)) != EOF && ch != '\f')
- ;
- else
- {
- while((ch = getc(hfptr)) != EOF)
- {
- ndx.size++;
- if (ch == '\f')
- {
- put_rec(nfd, h_header.helpno, (char *)&ndx);
- ndx.size = 0;
- break;
- }
- }
- }
- ndx.seekpos = ftell(hfptr);
- }
- }
-
-
- int read_help_index(num)
- int num;
- {
- /* Check it exists first */
- if ((long)(num * sizeof(struct help_ndx)) >= fsize(tfd))
- {
- # ifdef HDEBUG
- errmsg("read_help_index(): invalid index number (%d)", num);
- # endif
- return(FALSE);
- }
-
- /* read in the index records */
- get_rec(tfd, num, (char *)&h_tdx);
- return(TRUE);
- }
-
-
- int build_help_ndxfile(help_num)
- int help_num;
- {
- int buf;
- struct help_ndx ndx;
-
- h_part.len = 0;
- if (read_index(help_num) == FALSE)
- {
- errmsg("build_help_ndxfile(): read_index failed");
- return(FALSE);
- }
- if (fseek(hfptr, h_ndx.seekpos + (long)HSTRSIZE, SEEK_SET))
- {
- errmsg("build_help_ndxfile(): fseek failed");
- return(FALSE);
- }
-
- lseek(tfd, 0L, SEEK_SET); /* go to beginning of file */
- ndx.seekpos = h_ndx.seekpos;
- ndx.size = 0;
- put_rec(tfd, 0, (char *)&ndx); /* put zeroth record */
-
- /* build the temporary index */
- while (((buf = getc(hfptr)) != EOF) && (ndx.size < h_ndx.size))
- {
- ndx.size++;
- if (buf == '\n')
- {
- write(tfd, (char *)&ndx, sizeof(struct help_ndx));
- ndx.seekpos = ftell(hfptr) - HSTRSIZE;
- ndx.size = 0;
- h_part.len++; /* count number of lines */
- }
- if (buf == '\f')
- break;
- }
- /* write out the last index for last line */
- if (buf != '\f')
- write(tfd, (char *)&ndx, sizeof(struct help_ndx));
-
- h_part.len++;
- return(TRUE);
- }
-
-
- open_help_ndxfile()
- {
- char tdxfile[128];
- char *envptr;
- char pidstr[10];
-
- envptr = getenv("TMP");
- if (envptr == NULL)
- getcwd(tdxfile, 127);
- else
- strcpy(tdxfile, envptr);
- addslash(tdxfile);
- sprintf(pidstr, "%04X.tdx", getpid());
- strcat(tdxfile, pidstr);
-
- tfd = -1;
-
- openf(tdxfile, SH_OPENRWC, sizeof(struct help_ndx), &tfd);
- if (tfd == -1)
- {
- errmsg("Unable to create help index file");
- return(FALSE);
- }
- return(TRUE);
- }
-
- void close_help_ndxfile()
- {
- char tmp_ndxfile[128];
- char *envptr;
- char pidstr[10];
-
- closef(tfd);
- tfd = -1;
-
- envptr = getenv("TMP");
- if (envptr == NULL)
- getcwd(tmp_ndxfile, 127);
- else
- strcpy(tmp_ndxfile, envptr);
- addslash(tmp_ndxfile);
- sprintf(pidstr, "%04X.tdx", getpid());
- strcat(tmp_ndxfile, pidstr);
- unlink(tmp_ndxfile);
- }
-
-