]> diplodocus.org Git - nmh/blob - docs/historical/SRI-NOSC/open.c
Added start_test/finish_test to a bunch of tests.
[nmh] / docs / historical / SRI-NOSC / open.c
1 /* formerly folder */
2 #include "mh.h"
3 #include "stat.h"
4 #define NFOLDERS 100
5
6 char lsproc[],
7 listname[];
8
9 int fout, all, hdrflag, foldp;
10 struct msgs *mp;
11 char folder[128], *folds[NFOLDERS];
12 int msgtot, foldtot, totonly, shrt;
13 struct swit switches[] {
14 "all", 0, /* 0 */
15 "down", 0, /* 1 */
16 "fast", 0, /* 2 */
17 "nofast", 0, /* 3 */
18 "header", 0, /* 4 */
19 "noheader", 0, /* 5 */
20 "short", 0, /* 6 */
21 "total", 0, /* 7 */
22 "nototal", 0, /* 8 */
23 "up", 0, /* 9 */
24 "help", 4, /*10 */
25 0, 0
26 };
27
28 main(argc, argv)
29 char *argv[];
30 {
31 register char *cp, *curm;
32 register int i;
33 char *argfolder;
34 int up, down, j, def_short;
35 char *ap, *inp;
36 char *arguments[50], **argp;
37 struct inode stbf, *np;
38 struct { int inum;
39 char name[14];
40 int pad;
41 } ent;
42
43 fout = dup(1);
44 #ifdef NEWS
45 m_news();
46 #endif
47 up = down = argfolder = 0;
48 curm = 0;
49 if(argv[0][length(argv[0])-1] == 's') /* Plural name?? */
50 all++;
51 ap = cp = argv[0];
52 while(*cp)
53 if(*cp++ == '/')
54 ap = cp;
55 inp = ap;
56 if(!uleq("folder", ap) && (cp = m_find(ap)) != -1) {
57 ap = brkstring(cp = getcpy(cp), " ", "\n");
58 ap = copyip(ap, arguments);
59 } else
60 ap = arguments;
61 copyip(argv+1, ap);
62 argp = arguments;
63 while(cp = *argp++) {
64 if(*cp == '-')
65 switch(smatch(++cp, switches)) {
66 case -2:ambigsw(cp, switches); /* ambiguous */
67 goto leave;
68 /* unknown */
69 case -1:printf("-%s unknown\n", cp);
70 goto leave;
71 case 0: all++; continue; /* -all */
72 case 1: down++; continue; /* -down */
73 case 2: /* -fast */
74 case 6: shrt = 1; continue; /* -short */
75 case 3: shrt = 0; continue; /* -nofast */
76 case 4: hdrflag = -1; continue; /* -header */
77 case 5: hdrflag = 0; continue; /* -noheader */
78 case 7: all++; totonly = 1; /* -total */
79 continue;
80 case 8: if(totonly) all--; /* -nototal */
81 totonly =0; continue;
82 case 9: up++; continue; /* -up */
83 /* -help */
84 case 10:help(concat( inp, " [+folder] [msg] [switches]",0),
85 switches);
86 goto leave;
87 }
88 if(*cp == '+') {
89 if(argfolder) {
90 printf("Only one folder at a time.\n");
91 goto leave;
92 } else
93 argfolder = cp + 1;
94 } else if(curm) {
95 printf("Only one current may be given.\n");
96 goto leave;
97 } else
98 curm = cp;
99 }
100 if(all) {
101 hdrflag = 0;
102 cp = m_maildir("");
103 m_getdefs();
104 for(np = m_defs; np; np = np->n_next) {
105 if(!ssequal("cur-", np->n_name))
106 continue;
107 if(shrt) {
108 def_short++;
109 printf("%s\n", np->n_name+4);
110 } else
111 addfold(np->n_name+4);
112 }
113 if(def_short)
114 putchar('\n');
115 if(shrt) {
116 m_update();
117 flush();
118 execl(lsproc, "mh-ls", "-x", cp, 0);
119 printf("Can't exec: "); flush();
120 perror(lsproc);
121 goto leave;
122 }
123 if(chdir(cp) < 0) {
124 printf("Can't chdir to: "); flush();
125 perror(cp);
126 goto leave;
127 }
128 if((cp = m_find("folder")) == -1)
129 *folder = 0;
130 else
131 copy(cp, folder);
132 i = open(".", 0);
133 ent.pad = 0;
134 while(read(i, &ent.inum, sizeof ent.name + sizeof ent.inum))
135 if(ent.inum && ent.name[0] != '.' &&
136 stat(ent.name, &stbf) >= 0 &&
137 (stbf.i_mode&IFMT) == IFDIR)
138 addfold(ent.name);
139 close(i);
140 for(i = 0; i < foldp; i++) {
141 pfold(folds[i], 0); flush();
142 }
143 if(!totonly)
144 printf("\n\t\t ");
145 printf("TOTAL= %3d message%c in %d Folder%s.\n",
146 msgtot, msgtot!=1? 's':' ',
147 foldtot, foldtot!=1? "s":"");
148 } else {
149 hdrflag++;
150 if(argfolder)
151 cp = copy(argfolder, folder);
152 else
153 cp = copy(m_getfolder(), folder);
154 if(up) {
155 while(cp > folder && *cp != '/') --cp;
156 if(cp > folder)
157 *cp = 0;
158 argfolder = folder;
159 } else if(down) {
160 copy(listname, copy("/", cp));
161 argfolder = folder;
162 }
163 if(pfold(folder, curm) && argfolder)
164 m_replace("folder",argfolder);
165 }
166
167 leave:
168 m_update();
169 flush();
170 }
171
172
173 addfold(fold)
174 char *fold;
175 {
176 register int i,j;
177 register char *cp;
178
179 if(foldp >= NFOLDERS) {
180 printf("More than %d folders!!\n", NFOLDERS);
181 return(1);
182 }
183 cp = getcpy(fold);
184 for(i = 0; i < foldp; i++)
185 if(compare(cp, folds[i]) < 0) {
186 for(j = foldp - 1; j >= i; j--)
187 folds[j+1] = folds[j];
188 foldp++;
189 folds[i] = cp;
190 return(0);
191 }
192 folds[foldp++] = cp;
193 return(0);
194 }
195
196
197 pfold(fold, curm)
198 char *fold;
199 {
200 register char *mailfile;
201
202 mailfile = m_maildir(fold);
203 if(chdir(mailfile) < 0) {
204 printf("Can't chdir to: "); flush();
205 perror(mailfile);
206 return(0);
207 }
208 if(shrt) {
209 printf("%s\n", fold);
210 return(0);
211 }
212 mp = m_gmsg(fold);
213 foldtot++;
214 msgtot =+ mp->nummsg;
215 if(totonly)
216 goto out;
217 if(curm) {
218 if(!m_convert(curm))
219 return(0);
220 if(mp->numsel > 1) {
221 printf("Can't set current msg to %s\n", curm);
222 return(0);
223 }
224 m_setcur(mp->curmsg = mp->hghsel);
225 }
226 if(!hdrflag++)
227 printf("\t\tFolder # of messages ( range ); cur msg (other files)\n");
228 printf("%22s", fold);
229 if(equal(folder, fold))
230 printf("+ ");
231 else
232 printf(" ");
233 if(mp->hghmsg == 0)
234 printf("has no messages");
235 else {
236 printf("has %3d message%s (%3d-%3d)",
237 mp->nummsg, (mp->nummsg==1)?" ":"s",
238 mp->lowmsg, mp->hghmsg);
239 if(mp->curmsg >= mp->lowmsg && mp->curmsg <= mp->hghmsg)
240 printf("; cur=%3s", m_name(mp->curmsg));
241 }
242 if(mp->selist || mp->others) {
243 printf("; (");
244 if(mp->selist) {
245 printf("%s", listname);
246 if(mp->others)
247 printf(", ");
248 }
249 if(mp->others)
250 printf("others");
251 putchar(')');
252 }
253 putchar('.');
254 putchar('\n');
255 out:
256 free(mp);
257 mp = 0;
258 return(1);
259 }
260
261
262 compare(s1, s2)
263 char *s1, *s2;
264 {
265 register char *c1, *c2;
266 register int i;
267
268 c1 = s1; c2 = s2;
269 while(*c1 || *c2)
270 if(i = *c1++ - *c2++)
271 return(i);
272 return(0);
273 }