home *** CD-ROM | disk | FTP | other *** search
- /* ==( help/rwt.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 help text files */
-
- /*
- * Routines in this file :
- *
- * long fsize(int);
- * returns the size of the file
- *
- * static void xtoa(int, char *, int);
- * convert hex to ascii
- *
- * static int atox(char *, int);
- * convert ascii to hex
- *
- * int read_help(char *, int, struct help_ndx);
- * reads a line from the help text file into the buffer
- *
- * int write_help(struct help_ndx, char *);
- * writes a line to end of the help text file
- *
- * int read_hdr(struct help_ndx);
- * reads the header from the help text file
- *
- * int write_hdr(struct help_ndx);
- * writes a header to the help text file
- *
- * int openhelp(char *, int);
- * opens the help text, and index files
- *
- * int create_node(int);
- * initializes a newly created help file
- *
- * int helptofile(char *);
- * transfers help text from help text file to edit file for editting
- *
- * int filetohelp(char *);
- * transfers help text from edit file to actual help text file
- *
- */
-
- # include <stdio.h>
- # include <bench.h>
- # include <fileio.h>
- # include "help.h"
-
- # include <errno.h>
-
-
- # ifdef ANSI
- static void xtoa(int, char *, int);
- static int atox(char *);
- long fsize(int);
- # else
- static void xtoa();
- static int atox();
- long fsize();
- # endif
-
-
- int read_help(line, lmax, hptr)
- char * line;
- int lmax;
- struct help_ndx *hptr;
- {
- /* Seek to position and fill yer boots */
- if (fseek(hfptr, hptr->seekpos + (long)HSTRSIZE, SEEK_SET))
- {
- # ifdef HDEBUGI
- errmsg("read_help() : Seek failed (%d)", errno);
- # endif
- return(FALSE);
- }
- if (!(fgets(line, lmax, hfptr)))
- {
- # ifdef HDEBUGI
- errmsg("read_help() : Read failed (%d)", errno);
- # endif
- return(FALSE);
- }
- return(TRUE);
- }
-
- int write_help(hptr, text_buf)
- struct help_ndx *hptr;
- char * text_buf;
- {
- /* Seek to position and dump yer boots */
- if (fseek(hfptr, hptr->seekpos + (long)HSTRSIZE, SEEK_SET))
- {
- errmsg("write_help() : Seek failed (%d)", errno);
- return(FALSE);
- }
- /* Text part */
- if (fputs(text_buf,hfptr) == EOF)
- {
- errmsg("write_help() : Write failed (%d)", errno);
- return(FALSE);
- }
- (void) fflush(hfptr);
- return(TRUE);
- }
-
- int read_hdr(hptr)
- struct help_ndx *hptr;
- {
- char text_buf[HSTRSIZE+1];
- long csp;
- int ch;
-
- /* Header will be zeroed if an error occurs */
- zerorec(&h_header, sizeof(struct help_hdr));
-
- /* Check it exists first */
- if (hptr->seekpos >= fsize(fileno(hfptr)))
- {
- errmsg("read_hdr() : help does not exist");
- return(FALSE);
- }
- /* Seek to position and dump yer boots */
- if (fseek(hfptr, hptr->seekpos, SEEK_SET))
- {
- errmsg("read_hdr() : Seek failed (%d)", errno);
- return(FALSE);
- }
- csp = hptr->seekpos;
-
- while(!isdigit(ch = getc(hfptr)))
- {
- if (ch == EOF)
- {
- csp = hptr->seekpos;
- break;
- }
- csp++;
- }
- hptr->seekpos = csp;
-
- if (fseek(hfptr, hptr->seekpos, SEEK_SET))
- {
- errmsg("read_hdr() : Seek correction failed (%d)", errno);
- return(FALSE);
- }
-
- /* Text part */
- if (!(fgets(text_buf, HSTRSIZE+1, hfptr)))
- {
- errmsg("read_hdr() : Read failed (%d)", errno);
- return(FALSE);
- }
- /* set all fields of the header using the ascii chars read in */
- h_header.row = (atox(text_buf) << 4) + atox(text_buf+1);
- h_header.col = (atox(text_buf+2) << 4) + atox(text_buf+3);
- h_header.width = (atox(text_buf+4) << 4) + atox(text_buf+5);
- h_header.height = (atox(text_buf+6) << 4) + atox(text_buf+7);
-
- h_header.box_style = atox(text_buf+8);
- h_header.box_attr = atox(text_buf+9);
- h_header.disp_attr = atox(text_buf+10);
- h_header.wrap = atox(text_buf+11);
- h_header.tabs = atox(text_buf+12);
-
- h_header.helpno = (atox(text_buf+36) << 12) +
- (atox(text_buf+37) << 8) +
- (atox(text_buf+38) << 4) +
- atox(text_buf+39);
- return(TRUE);
- }
-
- int mark_hdr(hptr)
- struct help_ndx *hptr;
- {
- char header_buf[HSTRSIZE+1];
-
- byteset(header_buf, '0', HSTRSIZE);
- header_buf[HSTRSIZE] = '\0';
-
- /* convert the entire header into ascii format before writing out */
- xtoa(h_header.row , header_buf , 2);
- xtoa(h_header.col , header_buf+2 , 2);
- xtoa(h_header.width , header_buf+4 , 2);
- xtoa(h_header.height , header_buf+6 , 2);
- xtoa(h_header.box_style , header_buf+8 , 1);
- xtoa(h_header.box_attr , header_buf+9 , 1);
- xtoa(h_header.disp_attr , header_buf+10, 1);
- xtoa(h_header.wrap , header_buf+11, 1);
- xtoa(h_header.tabs , header_buf+12, 1);
- xtoa(0xffff , header_buf+36, 4);
-
- /* Seek to position and dump yer boots */
- if (fseek(hfptr, hptr->seekpos, SEEK_SET))
- {
- errmsg("mark_hdr() : Seek failed (%d)", errno);
- return(FALSE);
- }
- /* Text hdr */
- if (fputs(header_buf, hfptr) == EOF)
- {
- errmsg("mark_hdr() : Write failed (%d)", errno);
- return(FALSE);
- }
-
- (void) fflush(hfptr);
- return(TRUE);
- }
-
- int write_hdr(hptr)
- struct help_ndx *hptr;
- {
- char header_buf[HSTRSIZE+1];
-
- byteset(header_buf, '0', HSTRSIZE);
- header_buf[HSTRSIZE] = '\0';
-
- /* convert the entire header into ascii format before writing out */
- xtoa(h_header.row , header_buf , 2);
- xtoa(h_header.col , header_buf+2 , 2);
- xtoa(h_header.width , header_buf+4 , 2);
- xtoa(h_header.height , header_buf+6 , 2);
- xtoa(h_header.box_style , header_buf+8 , 1);
- xtoa(h_header.box_attr , header_buf+9 , 1);
- xtoa(h_header.disp_attr , header_buf+10, 1);
- xtoa(h_header.wrap , header_buf+11, 1);
- xtoa(h_header.tabs , header_buf+12, 1);
- xtoa(h_header.helpno , header_buf+36, 4);
-
- /* Seek to position and dump yer boots */
- if (fseek(hfptr, hptr->seekpos, SEEK_SET))
- {
- errmsg("write_hdr() : Seek failed (%d)", errno);
- return(FALSE);
- }
- /* Text hdr */
- if (fputs(header_buf, hfptr) == EOF)
- {
- errmsg("write_hdr() : Write failed (%d)", errno);
- return(FALSE);
- }
-
- (void) fflush(hfptr);
- return(TRUE);
- }
-
- int openhelp(file, num)
- char * file;
- int num;
- {
- char helpfile[128];
- char ndxfile[128];
- struct help_ndx garbage_ndx;
- int crtflag = 0;
-
- /* open or create the help and help index files */
- (void) strcpy(helpfile, file);
- (void) strcat(helpfile, ".hlp");
-
- (void) strcpy(ndxfile, file);
- (void) strcat(ndxfile, ".ndx");
-
- if ((hfptr = fopen(helpfile, "r+")) == NULL)
- {
- if (!warning(-1, "Help message not found, Create it ?"))
- return(FALSE);
-
- /* index file doesn't exist so create it and the help file */
- if ((hfptr = fopen(helpfile, "w+")) == NULL)
- {
- /* couldn't create the help file so abort help */
- errmsg("Unable to create help file");
- return(FALSE);
- }
- unlink(ndxfile);
- openf(ndxfile, SH_OPENRWC, sizeof(struct help_ndx), &nfd);
- if (nfd < 0)
- {
- (void) fclose(hfptr);
- if (nfd == -1)
- errmsg("Unable to create help index file");
- return(FALSE);
- }
- garbage_ndx.seekpos = 0L;
- garbage_ndx.size = 0;
- put_garbage_rec(&garbage_ndx); /* force a null record */
-
- /* files are created so initialize them */
- (void) create_node(num);
- closef(nfd);
- openf(ndxfile, SH_OPENRWNC, sizeof(struct help_ndx), &nfd);
- }
- else
- {
- openf(ndxfile, SH_OPENRWNC, sizeof(struct help_ndx), &nfd);
- if (nfd < 0 )
- {
- openf(ndxfile, SH_OPENRWC, sizeof(struct help_ndx), &nfd);
- if (nfd < 0 )
- {
- (void) fclose(hfptr);
- if (nfd == -1)
- errmsg("Unable to create help index file");
- return(FALSE);
- }
-
- build_ndxfile();
- closef(nfd);
- openf(ndxfile, SH_OPENRWNC, sizeof(struct help_ndx), &nfd);
- if (nfd < 0)
- {
- fclose(hfptr);
- return(FALSE);
- }
- }
- /* if index is older than help text file - recreate it*/
- else
- {
- if (file_time(ndxfile) < file_time(helpfile))
- {
- closef(nfd);
- unlink(ndxfile);
- openf(ndxfile, SH_OPENRWC, sizeof(struct help_ndx), &nfd);
- if (nfd < 0)
- {
- fclose(hfptr);
- return(FALSE);
- }
- build_ndxfile();
- closef(nfd);
- openf(ndxfile, SH_OPENRWNC, sizeof(struct help_ndx), &nfd);
- if (nfd < 0)
- {
- fclose(hfptr);
- return(FALSE);
- }
- }
- }
- }
-
- # ifdef UNIX
- chmod(ndxfile, 00666);
- # endif
-
- return(TRUE);
- }
-
- /* default help header, used to initialize newly created nodes */
- static struct help_hdr default_header = {
- 12,
- 1,
- 80,
- 12,
- 0,
- NORMAL,
- NORMAL,
- 0,
- 4,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0
- };
-
- int create_node(num)
- int num;
- {
- int total;
- static char first_line[] = "\n\f";
-
- if ((total = read_total()) == -1)
- {
- # ifdef HDEBUG
- errmsg("create_node(): read_total failed");
- # endif
- return(FALSE);
- }
-
- h_header = default_header;
-
- h_header.row = w_nrows / 2;
- h_header.width = w_ncols;
- h_header.height = w_nrows / 2;
-
- while (total < num)
- {
- /* Add to the end */
- h_ndx.seekpos = fsize(fileno(hfptr));
- h_ndx.size = strlen(first_line);
-
- /* save this index to file */
- write_index(++total);
-
- h_header.helpno = total;
-
- /* write the header out to file */
- if (write_hdr(&h_ndx) == FALSE)
- return(FALSE);
-
- if (write_help(&h_ndx, first_line) == FALSE)
- return(FALSE);
- }
- return(TRUE);
- }
-
-
- int helptofile(editfile)
- char * editfile;
- {
- FILE * fptr;
- int ch;
-
- if ((fptr = fopen(editfile, "w")) == NULL)
- {
- errmsg("helptofile(): can not open text file");
- return(FALSE);
- }
-
- if (read_index(h_num) == FALSE || h_ndx.size == 0)
- {
- # ifdef HDEBUG
- errmsg("helptofile(): read_index failed");
- # endif
- (void) fclose(fptr);
- return(FALSE);
- }
- if (fseek(hfptr, h_ndx.seekpos + (long)HSTRSIZE, SEEK_SET))
- {
- # ifdef HDEBUG
- errmsg("helptofile(): lseek failed (%d)", errno);
- # endif
- (void) fclose(fptr);
- return(FALSE);
- }
-
- /* copy the help text into the edit file */
- while (((ch = getc(hfptr)) != EOF) && (ch != '\f'))
- putc(ch, fptr);
-
- (void)fclose(fptr);
- return(TRUE);
- }
-
- int filetohelp(editfile)
- char * editfile;
- {
- FILE * fptr;
- int ch;
- extern char h_file[]; /* callhelp.c */
- struct help_ndx garbage_ndx;
-
- if ((fptr = fopen(editfile, "r")) == NULL)
- {
- errmsg("filetohelp(): can not open text file");
- return(FALSE);
- }
-
- get_garbage_rec(&garbage_ndx);
- garbage_ndx.size += h_ndx.size;
- put_garbage_rec(&garbage_ndx);
-
- mark_hdr(&h_ndx);
-
- h_ndx.seekpos = fsize(fileno(hfptr));
- h_ndx.size = (int)fsize(fileno(fptr));
- h_ndx.size++; /* include the \f that we will write to the file */
-
- write_index(h_num);
-
- h_header.helpno = h_num;
-
- if (write_hdr(&h_ndx) == FALSE)
- {
- errmsg("filetohelp(): can't write to index file");
- return(FALSE);
- }
-
- /* copy the help text from the edit file to the help file */
- if (h_ndx.size - 1)
- while((ch = getc(fptr)) != EOF)
- putc(ch, hfptr);
-
- /* make sure line feed separator is in help file */
- putc('\f', hfptr);
-
- fflush(hfptr);
- (void) fclose(fptr);
-
- lock_index(h_num, ULOK);
- if (lock_index_file(WLOK))
- {
- /* clean fragmentation out of file */
- if (eat_garbage(h_file) == FALSE)
- {
- errmsg("filetohelp(): got FALSE from eat_garbage()");
- return(FALSE);
- }
- lock_index_file(ULOK);
- }
- lock_index(h_num, WLOK);
-
- close_help_ndxfile();
- open_help_ndxfile();
- if (build_help_ndxfile(h_num) == FALSE)
- {
- errmsg("filetohelp(): build_help_ndxfile failed");
- return(FALSE);
- }
-
- return(TRUE);
- }
-
- static void xtoa(val, buf, num)
- int val;
- char * buf;
- int num;
- {
- char temp[18];
- int i;
- int j;
-
- (void) sprintf(temp, "%x", val);
- byteset(buf, 0x30, num);
- for (i=strlen(temp)-1, j=num-1; i >= 0; )
- buf[j--] = temp[i--];
- }
-
-
- static int atox(buf)
- char * buf;
- {
- int temp = 0;
-
- if (buf[0] >= 'a' && buf[0] <= 'f')
- temp = (int)(buf[0] - 'a' + 0x0a);
- else
- temp = (int)(buf[0] - '0');
-
- return(temp);
- }
-