chiark / gitweb /
WIP input file handling
[innduct.git] / frontends / sm.c
1 /*  $Id: sm.c 6682 2004-03-06 18:31:15Z rra $
2 **
3 **  Provide a command line interface to the storage manager
4 */
5
6 #include "config.h"
7 #include "clibrary.h"
8
9 #include "inn/innconf.h"
10 #include "inn/messages.h"
11 #include "inn/qio.h"
12 #include "storage.h"
13
14 static const char usage[] = "\
15 Usage: sm [-dHiqrRS] [token ...]\n\
16 \n\
17 Command-line interface to the INN storage manager.  The default action is\n\
18 to display the complete article associated with each token given.  If no\n\
19 tokens are specified on the command line, they're read from stdin, one per\n\
20 line.\n\
21 \n\
22     -d, -r      Delete the articles associated with the given tokens\n\
23     -H          Display the headers of articles only\n\
24     -i          Translate tokens into newsgroup names and article numbers\n\
25     -q          Suppress all error messages except usage\n\
26     -R          Display the raw article rather than undoing wire format\n\
27     -S          Output articles in rnews batch file format\n";
28
29 /* The options that can be set on the command line, used to determine what to
30    do with each token. */
31 struct options {
32     bool artinfo;               /* Show newsgroup and article number. */
33     bool delete;                /* Delete articles instead of showing them. */
34     bool header;                /* Display article headers only. */
35     bool raw;                   /* Show the raw wire-format articles. */
36     bool rnews;                 /* Output articles as rnews batch files. */
37 };
38
39
40 /*
41 **  Process a single token, performing the operations specified in the given
42 **  options struct.  Calls warn and die to display error messages; -q is
43 **  implemented by removing all the warn and die error handlers.
44 */
45 static bool
46 process_token(const char *id, const struct options *options)
47 {
48     TOKEN token;
49     struct artngnum artinfo;
50     ARTHANDLE *article;
51     size_t length;
52     char *text;
53
54     if (!IsToken(id)) {
55         warn("%s is not a storage token", id);
56         return false;
57     }
58     token = TextToToken(id);
59
60     if (options->artinfo) {
61         if (!SMprobe(SMARTNGNUM, &token, &artinfo)) {
62             warn("could not get article information for %s", id);
63             return false;
64         } else {
65             printf("%s: %lu\n", artinfo.groupname, artinfo.artnum);
66             free(artinfo.groupname);
67         }
68     } else if (options->delete) {
69         if (!SMcancel(token)) {
70             warn("could not remove %s: %s", id, SMerrorstr);
71             return false;
72         }
73     } else {
74         article = SMretrieve(token, options->header ? RETR_HEAD : RETR_ALL);
75         if (article == NULL) {
76             warn("could not retrieve %s", id);
77             return false;
78         }
79         if (options->raw) {
80             if (fwrite(article->data, article->len, 1, stdout) != 1)
81                 die("output failed");
82         } else {
83             text = FromWireFmt(article->data, article->len, &length);
84             if (options->rnews)
85                 printf("#! rnews %lu\n", (unsigned long) length);
86             if (fwrite(text, length, 1, stdout) != 1)
87                 die("output failed");
88             free(text);
89         }
90         SMfreearticle(article);
91     }
92     return true;
93 }
94
95
96 int
97 main(int argc, char *argv[])
98 {
99     int option;
100     bool okay, status;
101     struct options options = { false, false, false, false, false };
102
103     message_program_name = "sm";
104
105     if (!innconf_read(NULL))
106         exit(1);
107
108     while ((option = getopt(argc, argv, "iqrdRSH")) != EOF) {
109         switch (option) {
110         case 'd':
111         case 'r':
112             options.delete = true;
113             break;
114         case 'H':
115             options.header = true;
116             break;
117         case 'i':
118             options.artinfo = true;
119             break;
120         case 'q':
121             message_handlers_warn(0);
122             message_handlers_die(0);
123             break;
124         case 'R':
125             options.raw = true;
126             break;
127         case 'S':
128             options.rnews = true;
129             break;
130         default:
131             fprintf(stderr, usage);
132             exit(1);
133         }
134     }
135
136     /* Check options for consistency. */
137     if (options.artinfo && options.delete)
138         die("-i cannot be used with -r, -d");
139     if (options.artinfo && (options.header || options.raw || options.rnews))
140         die("-i cannot be used with -H, -R, or -S");
141     if (options.delete && (options.header || options.rnews))
142         die("-r or -d cannot be used with -H or -S");
143     if (options.raw && options.rnews)
144         die("-R cannot be used with -S");
145     if (options.header && options.rnews)
146         die("-H cannot be used with -S");
147
148     /* Initialize the storage manager.  If we're doing article deletions, we
149        need to open it read/write. */
150     if (options.delete) {
151         bool value = true;
152
153         if (!SMsetup(SM_RDWR, &value))
154             die("cannot set up storage manager");
155     }
156     if (!SMinit())
157         die("cannot initialize storage manager: %s", SMerrorstr);
158
159     /* Process tokens.  If no arguments were given on the command line,
160        process tokens from stdin.  Otherwise, walk through the remaining
161        command line arguments. */
162     okay = true;
163     if (optind == argc) {
164         QIOSTATE *qp;
165         char *line;
166
167         qp = QIOfdopen(fileno(stdin));
168         for (line = QIOread(qp); line != NULL; line = QIOread(qp)) {
169             status = process_token(line, &options);
170             okay = okay && status;
171         }
172         if (QIOerror(qp)) {
173             if (QIOtoolong(qp))
174                 die("input line too long");
175             sysdie("error reading stdin");
176         }
177         QIOclose(qp);
178     } else {
179         int i;
180
181         for (i = optind; i < argc; i++) {
182             status = process_token(argv[i], &options);
183             okay = okay && status;
184         }
185     }
186
187     SMshutdown();
188     exit(okay ? 0 : 1);
189 }