1 /* cyr_sequence.c -- manipulate sequences
2  *
3  * Copyright (c) 1994-2008 Carnegie Mellon University.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. The name "Carnegie Mellon University" must not be used to
18  *    endorse or promote products derived from this software without
19  *    prior written permission. For permission or any legal
20  *    details, please contact
21  *      Carnegie Mellon University
22  *      Center for Technology Transfer and Enterprise Creation
23  *      4615 Forbes Avenue
24  *      Suite 302
25  *      Pittsburgh, PA  15213
26  *      (412) 268-7393, fax: (412) 268-7395
27  *      innovation@andrew.cmu.edu
28  *
29  * 4. Redistributions of any form whatsoever must retain the following
30  *    acknowledgment:
31  *    "This product includes software developed by Computing Services
32  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33  *
34  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41  */
42 
43 #include <config.h>
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <sys/uio.h>
54 #include <fcntl.h>
55 
56 #include <sys/ipc.h>
57 #include <sys/msg.h>
58 
59 #include "sequence.h"
60 #include "global.h"
61 #include "util.h"
62 
63 /* generated headers are not necessarily in current directory */
64 #include "imap/imap_err.h"
65 
usage(const char * name)66 static void usage(const char *name)
67 {
68     fprintf(stderr, "Usage: %s [-C altconfig] [-m maxval] command sequence [args]\n", name);
69     fprintf(stderr, "\n");
70     fprintf(stderr, " - parsed               => dump a parsed view of the list structure\n");
71     fprintf(stderr, " - compress             => dump a compressed list\n");
72     fprintf(stderr, " - ismember [num...]    => is num in the list for each num\n");
73     fprintf(stderr, " - members              => all list members in order\n");
74     fprintf(stderr, " - create [-s] [items]  => generate a new list from the items\n");
75     fprintf(stderr, "                           - prefix numbers with '~' for remove\n");
76     exit(-1);
77 }
78 
main(int argc,char * argv[])79 int main(int argc, char *argv[])
80 {
81     const char *alt_config = NULL;
82     unsigned maxval = 0;
83     int flags = SEQ_MERGE;
84     struct seqset *seq = NULL;
85     int opt;
86     unsigned num;
87     char *res;
88     const char *origlist = NULL;
89 
90     while ((opt = getopt(argc, argv, "C:m:o:s")) != EOF) {
91         switch (opt) {
92         case 'C': /* alt config file */
93             alt_config = optarg;
94             break;
95         case 'm': /* maxval */
96             parseuint32(optarg, NULL, &maxval);
97             break;
98         case 'o':
99             origlist = optarg;
100             break;
101         case 's':
102             flags = SEQ_SPARSE;
103         }
104     }
105 
106     if ((argc - optind) < 1) usage(argv[0]);
107 
108 
109     cyrus_init(alt_config, "cyr_sequence", 0, 0);
110 
111     /* special case */
112     if (!strcmp(argv[optind], "create")) {
113         int i;
114         seq = seqset_init(maxval, flags);
115         for (i = optind + 1; i < argc; i++) {
116             char *ptr = argv[i];
117             int isadd = 1;
118             if (*ptr == '~') {
119                 isadd = 0;
120                 ptr++;
121             }
122             if (parseuint32(ptr, NULL, &num))
123                 printf("%s NAN\n", argv[i]);
124             else
125                 seqset_add(seq, num, isadd);
126         }
127         if (origlist) {
128             unsigned oldmax = seq_lastnum(origlist, NULL);
129             if (oldmax > maxval) {
130                 struct seqset *origseq = seqset_parse(origlist, NULL, oldmax);
131                 unsigned val;
132                 for (val = maxval + 1; val <= oldmax; val++)
133                     seqset_add(seq, val, seqset_ismember(origseq, val));
134                 seqset_free(origseq);
135             }
136         }
137         res = seqset_cstring(seq);
138         printf("%s\n", res);
139         free(res);
140     }
141     else if (!strcmp(argv[optind], "parsed")) {
142         unsigned i;
143         seq = seqset_parse(argv[optind+1], NULL, maxval);
144         printf("Sections: " SIZE_T_FMT "\n", seq->len);
145         for (i = 0; i < seq->len; i++) {
146             if (seq->set[i].high == UINT_MAX)
147                 printf(" [%u, *]\n", seq->set[i].low);
148             else
149                 printf(" [%u, %u]\n", seq->set[i].low, seq->set[i].high);
150         }
151     }
152     else if (!strcmp(argv[optind], "compress")) {
153         seq = seqset_parse(argv[optind+1], NULL, maxval);
154         res = seqset_cstring(seq);
155         printf("%s\n", res);
156         free(res);
157     }
158     else if (!strcmp(argv[optind], "members")) {
159         seq = seqset_parse(argv[optind+1], NULL, maxval);
160         while ((num = seqset_getnext(seq))) {
161             printf("%u\n", num);
162         }
163     }
164     else if (!strcmp(argv[optind], "join")) {
165         struct seqset *seq2;
166         seq = seqset_parse(argv[optind+1], NULL, maxval);
167         seq2 = seqset_parse(argv[optind+2], NULL, maxval);
168         seqset_join(seq, seq2);
169         res = seqset_cstring(seq);
170         printf("%s\n", res);
171         free(res);
172     }
173     else if (!strcmp(argv[optind], "ismember")) {
174         int i;
175         seq = seqset_parse(argv[optind+1], NULL, maxval);
176         for (i = optind + 2; i < argc; i++) {
177             if (parseuint32(argv[i], NULL, &num))
178                 printf("%s NAN\n", argv[i]);
179             else
180                 printf("%d %s\n", num, seqset_ismember(seq, num) ? "Yes" : "No");
181         }
182     }
183     else {
184         printf("Unknown command %s", argv[optind]);
185     }
186 
187     seqset_free(seq);
188 
189     cyrus_done();
190 
191     return 0;
192 }
193