home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / dmail / range.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  3.2 KB  |  171 lines

  1. /*
  2.  * RANGE.C
  3.  *
  4.  *  (C) Copyright 1985-1990 by Matthew Dillon,    All Rights Reserved.
  5.  *
  6.  *  Global Routines:    REWIND_RANGE()
  7.  *            GET_RANGE()
  8.  *            SINGLE_POSITION()
  9.  *
  10.  *  Static Routines:    None.
  11.  *
  12.  *
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "dmail.h"
  18.  
  19. static int range_ac;
  20. static int in, start, end;
  21.  
  22. Prototype void rewind_range (int beg);
  23. Prototype int  get_range (void);
  24. Prototype int  single_position (void);
  25.  
  26.  
  27. struct RANOP {
  28.     char *name;
  29.     int status, kstatus;
  30. };
  31.  
  32. static struct RANOP Ranop[] = {
  33.     "all",  0,              0,
  34.     "tag",  ST_TAG,         ST_DELETED,
  35.     "wri",  ST_STORED,      ST_DELETED,
  36.     "del",  ST_DELETED,     0,
  37.     "mar",  ST_READ,        ST_DELETED,
  38.     "unt",  0,              ST_DELETED | ST_TAG,
  39.     "unw",  0,              ST_DELETED | ST_STORED,
  40.     "und",  0,              ST_DELETED,
  41.     "unm",  0,              ST_DELETED | ST_READ,
  42.     NULL ,  0,            0 };
  43.  
  44. void
  45. rewind_range (int beg)
  46. {
  47.     Silence = 0;
  48.     range_ac = beg;
  49.  
  50.     if (range_ac >= ac) {
  51.     start = (Current >= 0) ? Entry[Current].no : 0;
  52.     end   = start;
  53.     in    = 1;
  54.     } else {
  55.     in    = 0;
  56.     }
  57. }
  58.  
  59. int
  60. get_range (void)
  61. {
  62.     register char *ptr;
  63.     register int i;
  64.     static int status;        /* Status items required            */
  65.     static int kstatus;     /* Status items which cannot be present */
  66.  
  67. again:
  68.     if (in  &&    start <= end) {
  69.     i = indexof(start++);
  70.     if (i < 0  || (Entry[i].status & status) != status ||
  71.         (Entry[i].status & kstatus))
  72.         goto again;
  73.     return (start - 1);
  74.     }
  75.     in = status = kstatus = 0;
  76.     if (range_ac >= ac)
  77.     return (0);
  78.     ptr = av[range_ac++];
  79.     if (*ptr == '-') {
  80.     if (xstrncmp (ptr, "-s", 2) == 0) {
  81.         Silence = 1;
  82.         goto again;
  83.     }
  84.     start = 1;
  85.     ++ptr;
  86.     goto dash;
  87.     }
  88.     if (*ptr < '0'  ||  *ptr > '9') {
  89.     start = 1;
  90.     end = 0;
  91.     for (i = 0; Ranop[i].name; ++i) {
  92.         if (xstrncmp (ptr, Ranop[i].name, 3) == 0) {
  93.         status = Ranop[i].status;
  94.         kstatus = Ranop[i].kstatus;
  95.         goto imprange;
  96.         }
  97.     }
  98.     goto again;
  99.     }
  100.     start = atoi(ptr);
  101.     while (*(++ptr)) {
  102.     if (*ptr == '-') {
  103.         ++ptr;
  104.         goto dash;
  105.     }
  106.     }
  107.     if (range_ac >= ac)
  108.     return (start);
  109.     if (*av[range_ac] == '-') {
  110.     ptr = av[range_ac++] + 1;
  111.     goto dash;
  112.     }
  113.     return (start);
  114. dash:
  115.     if (*ptr) {
  116.     end = atoi(ptr);
  117.     goto imprange;
  118.     }
  119.     if (range_ac >= ac) {
  120.     end = 0;
  121.     goto imprange;
  122.     }
  123.     end = atoi(av[range_ac++]);
  124. imprange:
  125.     if (end == 0) {
  126.     end = indexof (0);
  127.     if (end < 0)
  128.         return (0);
  129.     end = Entry[end].no;
  130.     }
  131.     if (start > end) {
  132.     printf ("Bad Range: %s\n", av[range_ac - 1]);
  133.     return (0);
  134.     }
  135.     in = 1;
  136.     goto again;
  137. }
  138.  
  139. int
  140. single_position (void)
  141. {
  142.     int old = Current;
  143.  
  144.     switch (ac) {
  145.     case 1:
  146.     break;
  147.     case 2:
  148.     if (*av[1] == '\0' || (*av[1] == ' ' && strlen(av[1]) == 1))
  149.         break;
  150.     Current = indexof (atoi(av[1]));
  151.     if (Current < 0) {
  152.         Current = old;
  153.         puts ("Out of Range, 0 will take you to the last entry");
  154.         return (-1);
  155.     }
  156.     break;
  157.     default:
  158.     puts ("Range not implemented (yet?)");
  159.     return (-1);
  160.     }
  161.     while (Current < Entries  &&  Entry[Current].no == 0)
  162.     ++Current;
  163.     if (Current >= Entries) {
  164.     Current = old;
  165.     puts ("No More Messages");
  166.     return (-1);
  167.     }
  168.     position_current();
  169.     return (1);
  170. }
  171.