home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / PROCWRKB.ZIP / BENCH1.ZIP / HELP / RWI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-07  |  6.6 KB  |  348 lines

  1. /* ==( help/rwi.c )== */
  2. /* ----------------------------------------------- */
  3. /* Pro-C  Copyright (C) 1988 - 1990 Vestronix Inc. */
  4. /* Modification to this source is not supported    */
  5. /* by Vestronix Inc.                               */
  6. /*            All Rights Reserved                  */
  7. /* ----------------------------------------------- */
  8. /* Written   JPK  26-Sep-88                        */
  9. /* Modified  Geo  12-Dec-89  See comments below    */
  10. /* ----------------------------------------------- */
  11. /* %W%  (%H% %T%) */
  12.  
  13. /*
  14.  *  Modifications
  15.  *
  16.  *  12-Dec-89  Geo - V2 version with variable lines
  17.  *  25-Oct-89  Geo - 1.32 Merge
  18.  *
  19.  *
  20. */
  21.  
  22. /* contains the routines that handle the index files */
  23.  
  24. /* 
  25.  * Routines in this file :
  26.  *
  27.  * int read_total(void);
  28.  *    calculates the number of records in the index file
  29.  *
  30.  * int read_index(int);
  31.  *    reads the index file and fills the index structure with the 
  32.  *    appropriate record
  33.  *
  34.  * void write_index(int);
  35.  *    writes the index structure to the index file  
  36.  *
  37.  * void build_ndxfile(void);
  38.  *    builds the index file from the help text file.  writes a record
  39.  *     to the file on every occurence of \f
  40.  *
  41.  * int read_help_index(int);
  42.  *    reads the temporary index file and fills the structure with the
  43.  *     position of the requested text block
  44.  *
  45.  * int build_help_ndxfile(void);
  46.  *    builds the temporary index file of the individual help records
  47.  *
  48.  */
  49.  
  50. # include <stdio.h> 
  51. # include <bench.h>
  52. # include <fileio.h>
  53. # include "help.h"
  54.  
  55. # ifdef UNIX
  56. #  include <setjmp.h>
  57. #  include <signal.h>
  58. # endif
  59.  
  60. extern char CantCreatIdx[];
  61.  
  62. char BldHlpIdx[]    = "Building help index ...";
  63. char BadLockTyp[]   = "lock_index_file(): Bad lock type";
  64. char ReadIdxFail[]  = "build_help_ndxfile(): read_index failed";
  65. char FseekFail[]    = "build_help_ndxfile(): fseek failed";
  66. # ifdef HDEBUG
  67. char InvIdxNum_d[]  = "read_help_index(): invalid index number (%d)";
  68. char NoLockIndex[]  = "lock_index() : index does not exist";
  69. char NoReadIndex[]  = "read_index() : index does not exist";
  70. # endif
  71.  
  72. PROTO (long fsize, (int));
  73.  
  74. static int tfd;   /* file descriptor for help text index file */
  75.  
  76. int read_total()
  77. {
  78.     long size;
  79.  
  80.     if ((size = fsize(nfd)) == -1)
  81.         return(-1);
  82.  
  83.     /* 0 .. n -1 prob */
  84.     return( (int)((size / sizeof(struct help_ndx)) - 1) );
  85. }
  86.  
  87. int check_index(num)
  88.     int num;
  89. {
  90.     if ((long)(num * sizeof(struct help_ndx)) >= fsize(nfd))
  91.         return(FALSE);
  92.  
  93.     return(TRUE);
  94. }
  95.  
  96. #ifdef UNIX
  97. static jmp_buf alarmjmp;
  98. void (*oldalarmfunc)();
  99.  
  100. static void alarmsigstub()
  101. {
  102.     longjmp(alarmjmp, -1);
  103. }
  104. #endif
  105.  
  106. int lock_index_file(locktyp)
  107. int locktyp;
  108. {
  109. #ifdef UNIX
  110.     struct flock ldesc;
  111.     int i;
  112.  
  113.     switch(locktyp)
  114.     {
  115.         case ULOK:
  116.             ldesc.l_type = F_UNLCK;
  117.             break;
  118.         case RLOK:
  119.             ldesc.l_type = F_RDLCK;
  120.             break;
  121.         case WLOK:
  122.             ldesc.l_type = F_WRLCK;
  123.             break;
  124.         default:
  125.             errmsg(BadLockTyp);
  126.             break;
  127.     }
  128.     ldesc.l_whence = 0L;
  129.     ldesc.l_start = 0L;
  130.     ldesc.l_len = 0L;
  131.  
  132.     if ((i = setjmp(alarmjmp)) == 0)
  133.     {
  134.         oldalarmfunc = signal(SIGALRM, alarmsigstub);
  135.         alarm(5);
  136.         i = fcntl(nfd, F_SETLKW, &ldesc);
  137.     }
  138.     alarm(0);
  139.     signal(SIGALRM, oldalarmfunc);
  140.     if (i != 0)
  141.         return(FALSE);
  142.     else
  143. #endif
  144.         return(TRUE);
  145. }
  146.  
  147. int lock_index(num, locktyp)
  148.     int num;
  149.     int locktyp;
  150. {
  151.     /* Check it exists first */
  152.     if (!check_index(num))
  153.     {
  154. # ifdef HDEBUG
  155.         errmsg(NoLockIndex);
  156. # endif
  157.         return(FALSE);
  158.     }
  159.     /* read in the index records */
  160.     if (lok_rec(nfd, num, locktyp) == -1)
  161.         return(FALSE);
  162.  
  163.     return(TRUE);
  164. }
  165.  
  166. int read_index(num)
  167.     int num;
  168. {
  169.     /* Check it exists first */
  170.     if (!check_index(num))
  171.     {
  172. # ifdef HDEBUG
  173.         errmsg(NoReadIndex);
  174. # endif
  175.         return(FALSE);
  176.     }
  177.     /* read in the index records */
  178.     get_rec(nfd, num, (char *)&h_ndx);
  179.     return(TRUE);
  180. }
  181.  
  182. void write_index(num)
  183.     int num;
  184. {
  185.     /* write out index record */
  186.     put_rec(nfd, num, (char *)&h_ndx); /* Errors done by relio.c */
  187. }
  188.  
  189.  
  190.  
  191. void build_ndxfile()
  192. {
  193.     int    ch;
  194.     struct    help_ndx ndx;
  195.  
  196.     statmsg(BldHlpIdx);
  197.  
  198.     ndx.seekpos = 0L;
  199.     ndx.size = 0;
  200.     put_garbage_rec(&ndx);
  201.  
  202.     fseek(hfptr, 0L, SEEK_SET);
  203.     while(ndx.seekpos != fsize(fileno(hfptr)))
  204.     {
  205.         if (read_hdr(&ndx) == FALSE)
  206.             return;
  207.         if (h_header.helpno == 0xffff)
  208.             while((ch = getc(hfptr)) != EOF && ch != '\f')
  209.                 ;
  210.         else
  211.         {
  212.             while((ch = getc(hfptr)) != EOF)
  213.             {
  214.                 ndx.size++;
  215.                 if (ch == '\f')
  216.                 {
  217.                     put_rec(nfd, h_header.helpno, (char *)&ndx);
  218.                     ndx.size = 0;
  219.                     break;
  220.                 }
  221.             }
  222.         }
  223.         ndx.seekpos = ftell(hfptr); 
  224.     }
  225. }
  226.  
  227.  
  228. int read_help_index(num)
  229.     int num;
  230. {
  231.     /* Check it exists first */
  232.     if ((long)(num * sizeof(struct help_ndx)) >= fsize(tfd))
  233.     {
  234. # ifdef HDEBUG
  235.         errmsg(InvIdxNum_d, num);
  236. # endif
  237.         return(FALSE);
  238.     }
  239.  
  240.     /* read in the index records */
  241.     get_rec(tfd, num, (char *)&h_tdx);
  242.     return(TRUE);
  243. }
  244.  
  245.  
  246. int build_help_ndxfile(help_num)
  247. int help_num;
  248. {
  249.     int    buf;
  250.     struct    help_ndx ndx;
  251.  
  252.     h_part.len = 0;
  253.     if (read_index(help_num) == FALSE)
  254.     {
  255.         errmsg(ReadIdxFail);
  256.         return(FALSE);
  257.     }
  258.     if (fseek(hfptr, h_ndx.seekpos + (long)HSTRSIZE, SEEK_SET))
  259.     {
  260.         errmsg(FseekFail);
  261.         return(FALSE);
  262.     }
  263.  
  264.     lseek(tfd, 0L, SEEK_SET);  /* go to beginning of file */
  265.     ndx.seekpos = h_ndx.seekpos;
  266.     ndx.size = 0;
  267.     put_rec(tfd, 0, (char *)&ndx);  /* put zeroth record */
  268.  
  269.     /* build the temporary index */
  270.     while (((buf = getc(hfptr)) != EOF) && (ndx.size < h_ndx.size))
  271.     {
  272.         ndx.size++;
  273.         if (buf == '\n')
  274.         {
  275.             write(tfd, (char *)&ndx, sizeof(struct help_ndx));
  276.             ndx.seekpos = ftell(hfptr) - HSTRSIZE;
  277.             ndx.size = 0;
  278.             h_part.len++; /* count number of lines */
  279.         }
  280.         if (buf == '\f')
  281.             break;
  282.     }
  283.     /* write out the last index for last line */
  284.     if (buf != '\f')
  285.         write(tfd, (char *)&ndx, sizeof(struct help_ndx));
  286.  
  287.     h_part.len++;
  288.     return(TRUE);
  289. }
  290.  
  291.  
  292. open_help_ndxfile()
  293. {
  294.     char    tdxfile[128];
  295.     char *envptr;
  296.     char pidstr[10];
  297.  
  298.     envptr = getenv("TMP");
  299.     if (envptr == NULL)
  300.         getcwd(tdxfile, 127);
  301.     else
  302.         strcpy(tdxfile, envptr);
  303.     addslash(tdxfile);
  304.     sprintf(pidstr, "%04X.tdx",
  305. #ifdef __WATCOMC__
  306.         0x1234);
  307. #else
  308.         getpid());
  309. #endif
  310.     strcat(tdxfile, pidstr);
  311.  
  312.     tfd = -1;
  313.  
  314.     openf(tdxfile, SH_OPENRWC, sizeof(struct help_ndx), &tfd);
  315.     if (tfd == -1)
  316.     {
  317.         errmsg(CantCreatIdx);
  318.         return(FALSE);
  319.     }
  320.     return(TRUE);
  321. }
  322.  
  323. void close_help_ndxfile()
  324. {
  325.     char tmp_ndxfile[128];
  326.     char *envptr;
  327.     char pidstr[10];
  328.  
  329.     closef(tfd);
  330.     tfd = -1;
  331.  
  332.     envptr = getenv("TMP");
  333.     if (envptr == NULL)
  334.         getcwd(tmp_ndxfile, 127);
  335.     else
  336.         strcpy(tmp_ndxfile, envptr);
  337.     addslash(tmp_ndxfile);
  338.     sprintf(pidstr, "%04X.tdx",
  339. #ifdef __WATCOMC__
  340.         0x1234);
  341. #else
  342.         getpid());
  343. #endif
  344.     strcat(tmp_ndxfile, pidstr);
  345.     unlink(tmp_ndxfile);
  346. }
  347.  
  348.