File: snprimedb.c

package info (click to toggle)
sn 0.3.8-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 976 kB
  • sloc: ansic: 9,255; sh: 467; makefile: 210
file content (168 lines) | stat: -rw-r--r-- 3,774 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * This file is part of the sn package.
 * Distribution of sn is covered by the GNU GPL. See file COPYING.
 * Copyright � 1998-2000 Harold Tay.
 * Copyright � 2000- Patrik R�dman.
 */

/*
 * Feed the database with values.  This could be useful if the
 * database has suffered corruption.  Then delete all the database
 * files so they will be recreated.  You can feed this program from
 * snscan.
 *
 * The database converts article-id to newsgroup:serial.
 *
 * Input is expected in the form one to a line,
 * newsgroup id serial
 * with spaces between the words.
 */

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "config.h"
#include "dhash.h"
#include "parameters.h"
#include <out.h>
#include <readln.h>
#include <tokensep.h>
#include <opt.h>
#include <format.h>

static const char ver_ctrl_id[] = "$Id: snprimedb.c 29 2004-04-24 23:02:38Z patrik $";

int debug = 0;

struct readln input;

static void handler (int signum)
{
   dh_close();
   LOG("Caught signal %d, exiting", signum);
   _exit(3);
}

int nr = 0;

int insert (char *newsgroup, char *id, int serial)
{
   struct data d = { 0, };
   char *cp;

   if (0 == strcmp(newsgroup, JUNK_GROUP))
      return 0;
   if ('<' == *id)
   {
      d.messageid = id + 1;
      if ((cp = strchr(d.messageid, '>')))
         *cp = '\0';
   }
   else
      d.messageid = id;
   d.newsgroup = newsgroup;
   d.serial = serial;
   if (-1 == dh_insert(&d))
   {
      if (EEXIST == errno)
      {
         LOG("insert:\"%s\" already exists in %s:%d", d.messageid, newsgroup, serial);
         return 1;
      }
      else
         LOG("insert:Can't insert record \"%s %s %d\":%m\n", newsgroup, d.messageid, serial);
      return -1;
   }
   nr++;
   return 0;
}

void usage (void)
{
   LOG("Usage: %s [-i]\n", progname);
   LOG("(No arguments).  Input is read from stdin in the form\n"
       "\"newsgroup id serial\"\n" "until end-of-file.\n");
   _exit(1);
}

int main (int argc, char **argv)
{
   char *line;
   char *newsgroup;
   char *id;
   char *serial;
   bool onlyinitialize = FALSE;
   char *cp;
   int len;
   int i;

   progname = ((cp = strrchr(argv[0], '/')) ? cp + 1 : argv[0]);

   parameters(TRUE);

   while ((i = opt_get(argc, argv, "")) > -1)
      switch (i)
      {
         case 'P': log_with_pid(); break;
         case 'i': onlyinitialize = TRUE; break;
         case 'd': debug++; break;
         case 'V': version(); _exit(0);
         default: usage();
      }
   if (opt_ind != argc)
      usage();

   if (-1 == chdir(snroot))
      FAIL(2, "chdir(%s):%m", snroot);

   if (-1 == dh_open(NULL, FALSE))
      FAIL(2, "Can't open database:%m?");

   if (onlyinitialize)
   {
      dh_close();
      _exit(0);
   }

   signal(SIGPIPE, handler);

   if (-1 == readln_ready(0, 0, &input))
      fail(2, "readln_ready:%m");

   while ((len = readln(&input, &line, '\n')) > 0)
   {
      char *p;
      int s;

      line[len - 1] = '\0';
      if (!(newsgroup = tokensep(&line, " ")))
         continue;
      if (!(serial = tokensep(&line, " ")))
         continue;
      if (!(id = tokensep(&line, " ")))
         continue;
      for (p = newsgroup; *p; p++)
         if (*p >= 'A' && *p <= 'Z')
            *p -= 'a' - 'A';
      if (p - newsgroup >= GROUPNAMELEN)
      {
         LOG("newsgroup name too long, skipping:%s", newsgroup);
         continue;
      }
      s = strtoul(serial, &cp, 10);
      if (s <= 0 || *cp)
         fail(3, "Bad value \"%s\" for serial number", serial);
      switch (insert(newsgroup, id, s))
      {
         default: LOG("Eh?"); /* Fall Through */
         case -1: dh_close(); _exit(2);
         case 1: ;
         case 0: ;
      }
   }
   dh_close();
   LOG("%d insertions", nr);
   _exit(0);
}