home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / gnus / gnushdrs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-20  |  3.7 KB  |  161 lines

  1. /* gnushdrs.c - retrieve headers from news for GNUS - rick sladkey
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.    This program takes a directory name and a series of articles in
  22.    that directory and generates an Emacs-Lisp expression in the format
  23.    expected by GNUS from a retrieve-headers function.  It was inspired
  24.    by Scott Ballantyne's getgnushdrs.c but is otherwise unrelated.  It
  25.    doesn't require cnews libraries and the output format is different
  26.    and much faster for Emacs to parse.
  27.  
  28.    The output looks like:
  29.  
  30.    ([article subject from xref lines data message-id references] ... )
  31.  
  32.    i.e. a list of vectors of article header fields.  The article and
  33.    and lines fields are numbers.  The others are quoted strings unless
  34.    that field is missing and then they are nil.
  35. */
  36.  
  37. #include <stdio.h>
  38. #include <fcntl.h>
  39.  
  40. #define HDR_MAXCHARS    1024
  41. #define HDR_MAXLINES    128
  42.  
  43. char c[HDR_MAXCHARS + 3];
  44. int csize;
  45. int l[HDR_MAXLINES];
  46. int lsize;
  47.  
  48. int main(argc, argv)
  49. int argc;
  50. char **argv;
  51. {
  52.     int art;
  53.     char *file;
  54.     int fd;
  55.     int i;
  56.     int cc;
  57.     int inheader;
  58.  
  59.     argv++; argc--;
  60.     if (argc == 0)
  61.         exit(1);
  62.     if (chdir(argv[0]) < 0)
  63.         exit(1);
  64.     argv++; argc--;
  65.     printf("(\n");
  66.     for (art = 0; art < argc; art++) {
  67.         file = argv[art];
  68.         fd = open(file, O_RDONLY, 0);
  69.         if (fd < 0)
  70.             continue;
  71.         csize = read(fd, c, HDR_MAXCHARS);
  72.         close(fd);
  73.         if (csize <= 0)
  74.             continue;
  75.         c[csize] = c[csize + 1] = c[csize + 2] = '\n';
  76.         csize += 2;
  77.  
  78.         lsize = 0;
  79.         l[lsize++] = 0;
  80.         inheader = 1;
  81.         for (i = 0; i < csize; i++) {
  82.             if ((cc = c[i]) == '\n') {
  83.                 l[lsize++] = i + 1;
  84.                 if ((cc = c[i + 1]) == '\n')
  85.                     break;
  86.                 if (cc != ' ' && cc != '\t')
  87.                     inheader = 1;
  88.             }
  89.             else if (cc == ':')
  90.                 inheader = 0;
  91.             else if (inheader && cc >= 'A' && cc <= 'Z')
  92.                 c[i] += 'a' - 'A';
  93.         }
  94.         
  95.         if (!dump_field("message-id", (char *) 0, 0, 1))
  96.             continue;
  97.  
  98.         printf("[%s", file);
  99.         dump_field("subject", "\"(None)\"", 0, 0);
  100.         dump_field("from", "\"(Unknown User)\"", 0, 0);
  101.         dump_field("xref", "nil", 0, 0);
  102.         dump_field("lines", "0", 1, 0);
  103.         dump_field("date", "nil", 0, 0);
  104.         dump_field("message-id", "nil", 0, 0);
  105.         if (!dump_field("references", (char *) 0, 0, 0))
  106.             dump_field("in-reply-to", "nil", 0, 0);
  107.         printf("]\n");
  108.     }
  109.     printf(")\n");
  110. }
  111.  
  112. int dump_field(field, def, numeric, verify)
  113. char *field;
  114. char *def;
  115. int numeric;
  116. int verify;
  117. {
  118.     int i, j;
  119.     int n, m;
  120.     char *p;
  121.     char cc;
  122.  
  123.     n = strlen(field);
  124.     for (i = 0; i < lsize; i++) {
  125.         p = &c[l[i]];
  126.         if (*p == *field && strncmp(p, field, n) == 0 && p[n] == ':') {
  127.             if (verify)
  128.                 return 1;
  129.             p += n + 1;
  130.             if (numeric) {
  131.                 printf(" %d", atoi(p));
  132.                 return 1;
  133.             }
  134.             putchar(' ');
  135.             putchar('"');
  136.             for (j = 0; *p == ' ' || *p == '\t'; j++) {
  137.                 while (*p == ' ' || *p == '\t')
  138.                     p++;
  139.                 if (j)
  140.                     putchar(' ');
  141.                 for (cc = *p; cc != '\n'; cc = *++p) {
  142.                     if (cc == '\\' || cc == '"')
  143.                         putchar('\\');
  144.                     putchar(cc);
  145.                 }
  146.                 p++;
  147.             }
  148.             putchar('"');
  149.             return 1;
  150.         }
  151.     }
  152.     if (verify)
  153.         return 0;
  154.     if (def) {
  155.         printf(" %s", def);
  156.         return 1;
  157.     }
  158.     return 0;
  159. }
  160.  
  161.