home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / vn.jan.88 / part04 / pagefile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-30  |  4.0 KB  |  225 lines

  1. /*
  2. ** vn news reader.
  3. **
  4. ** pagefile.c - routines to deal with page display tempfile
  5. **
  6. ** see copyright disclaimer / history in vn.c source file
  7. */
  8.  
  9. #include <stdio.h>
  10.  
  11. #ifdef SYSV
  12. #include <sys/types.h>
  13. #include <fcntl.h>
  14. #endif
  15.  
  16. #include <sys/file.h>
  17.  
  18. #include "tune.h"
  19. #include "node.h"
  20. #include "page.h"
  21.  
  22. extern int Ncount,Lrec,L_allow,Cur_page,C_allow;
  23. extern NODE **Newsorder;
  24. extern PAGE Page;
  25. extern int Digest;
  26.  
  27. extern char *Aformat;
  28.  
  29. extern char *T_head, *F_head, *L_head;
  30.  
  31. static int Tdes;    /* temp file descriptor */
  32. static int Pgsize;    /* block size for seeking file */
  33.  
  34. static NODE *Curgp = NULL;    /* current newsgroup being written */
  35. static int Order = 0;        /* order counter */
  36.  
  37. /*
  38.     routines which deal with the temp file containing
  39.     display pages.  Note the "invisible" file feature -
  40.     tempfile is unlinked from /usr/tmp immediately.  when
  41.     Tdes is closed by UNIX the disk space will be given back.
  42. */
  43.  
  44. temp_open ()
  45. {
  46.     char tmpart [L_tmpnam];
  47.     Lrec = -1;
  48.     tmpnam (tmpart);
  49.     Pgsize = sizeof (HEAD) + L_allow * sizeof(BODY);
  50.     if ((Tdes = open(tmpart,O_RDWR|O_CREAT)) < 0)
  51.         printex ("can't open %s",tmpart);
  52.     unlink (tmpart);
  53. }
  54.  
  55. /*
  56. ** set newsgroup for tempfile write
  57. */
  58. fw_group(ng,new,sub,rd,look)
  59. char *ng;
  60. int new;
  61. int sub;
  62. int rd;
  63. int look;
  64. {
  65.     NODE *hashfind();
  66.  
  67.     if (Curgp != NULL && Page.h.artnum > 0)
  68.         fw_flush();
  69.     
  70.     if ((Curgp = hashfind(ng)) == NULL)
  71.         printex("fw_group - non-existent newsgroup, \"%s\"",ng);
  72.     if (Curgp->order >= 0)
  73.         printex("fw_group - repeat call on newsgroup, \"%s\"",ng);
  74.     Curgp->order = Order;
  75.     ++Order;
  76.     fw_chg(new,sub,rd,look);
  77.     Curgp->pages = 0;
  78.     Curgp->pnum = Lrec+1;
  79.     Page.h.name = Curgp->nd_name;
  80.     Page.h.group = Curgp;
  81.     Page.h.artnum = 0;
  82. }
  83.  
  84. fw_chg(new,sub,rd,look)
  85. int new;
  86. int sub;
  87. int rd;
  88. int look;
  89. {
  90.     Curgp->flags &= ~(FLG_NEW|FLG_SUB|FLG_SEARCH);
  91.     if (new)
  92.         Curgp->flags |= FLG_NEW;
  93.     if (sub)
  94.         Curgp->flags |= FLG_SUB;
  95.     if (look)
  96.         Curgp->flags |= FLG_SEARCH;
  97.     Curgp->rdnum = Curgp->orgrd = Curgp->pgrd = rd;
  98. }
  99.  
  100. /*
  101. ** write article to temp file.
  102. */
  103. fw_art(anum,subj,lines,author)
  104. int anum;
  105. char *subj;
  106. char *lines;
  107. char *author;
  108. {
  109.     char tbuf[RECLEN];
  110.     int idx;
  111.  
  112.     form_title(tbuf,subj,lines,author,anum);
  113.     idx = Page.h.artnum;
  114.     strcpy((Page.b)[idx].art_t, tbuf);
  115.     (Page.b)[idx].art_id = anum;
  116.     (Page.b)[idx].art_mark = ' ';
  117.  
  118.     ++(Page.h.artnum);
  119.     if (Page.h.artnum >= L_allow)
  120.         fw_flush();
  121. }
  122.  
  123. fw_done()
  124. {
  125.     if (Curgp != NULL && Page.h.artnum > 0)
  126.     {
  127.         /* correct if server was lying at fw_group() */
  128.         Curgp->flags |= FLG_SEARCH;
  129.         fw_flush();
  130.     }
  131. }
  132.  
  133. static
  134. fw_flush()
  135. {
  136.     ++(Curgp->pages);
  137.     ++Lrec;
  138.     Curgp->flags |= FLG_PAGE;
  139.     do_write();
  140.     Page.h.artnum = 0;
  141. }
  142.  
  143. find_page (n)
  144. int n;
  145. {
  146.     long off,lseek();
  147.     int i,last;
  148.     Cur_page = n;
  149.     off = Pgsize;
  150.     off *= (long) n;
  151.     lseek (Tdes, off, 0);
  152.     if (read(Tdes, (char *) &(Page.h), sizeof(HEAD)) < sizeof(HEAD))
  153.         printex("bad temp file read");
  154.     i = Pgsize - sizeof(HEAD);
  155.     if (read(Tdes, (char *) Page.b, i) < i)
  156.         printex("bad temp file read");
  157.     last = -1;
  158.     for (i=0; i < Ncount; ++i)
  159.     {
  160.         if ((Newsorder[i])->pages > 0)
  161.         {
  162.             if ((Newsorder[i])->pnum > n)
  163.                 break;
  164.             last = i;
  165.         }
  166.     }
  167.     if (last < 0)
  168.         printex ("can't find page %d",n);
  169.     Page.h.group = Newsorder[last];
  170.     Page.h.name = (Page.h.group)->nd_name;
  171.     vns_gset(Page.h.name);
  172. }
  173.  
  174. write_page ()
  175. {
  176.     long off,lseek();
  177.     if (!Digest)
  178.     {
  179.         off = Pgsize;
  180.         off *= (long) Cur_page;
  181.         lseek (Tdes, off, 0);
  182.         do_write();
  183.     }
  184. }
  185.  
  186. static do_write()
  187. {
  188.     int num;
  189.  
  190.     if (write(Tdes, (char *) &(Page.h), sizeof(HEAD)) < sizeof(HEAD))
  191.         printex ("Bad temp file write");
  192.     num = L_allow * sizeof(BODY);
  193.     if (write(Tdes, (char *) Page.b, num) < num)
  194.         printex ("Bad temp file write");
  195. }
  196.  
  197. form_title (t,fn,fl,ff,n)
  198. char *t,*fn,*fl,*ff;
  199. int n;
  200. {
  201.     char *ptr,*index();
  202.     int i;
  203.  
  204.     if ((ptr = index(ff,'(')) != NULL && strlen(ptr) > 3)
  205.         ff = ptr;
  206.     sprintf (t,TFORMAT,fn,fl,ff);
  207.     sprintf(ff,Aformat,' ',' ',n);
  208.     i = C_allow - strlen(ff) + 1;    /* remember newline in Aformat */
  209.     t[i] = '\0';
  210.     ctl_xlt(t);
  211.     return (0);
  212. }
  213.  
  214. /* replace control characters in titles */
  215. static ctl_xlt(s)
  216. char *s;
  217. {
  218.     while (*s != '\0')
  219.     {
  220.         if (*s < ' ')
  221.             *s += 'A' - 1;
  222.         ++s;
  223.     }
  224. }
  225.