Relayouted all switch statements: case aligns with switch.
[mmh] / uip / dist.c
1 /*
2 ** dist.c -- re-distribute a message
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include <fcntl.h>
12
13 static struct swit switches[] = {
14 #define ANNOSW  0
15         { "annotate", 0 },
16 #define NANNOSW  1
17         { "noannotate", 0 },
18 #define EDITRSW  2
19         { "editor editor", 0 },
20 #define NEDITSW  3
21         { "noedit", 0 },
22 #define FORMSW  4
23         { "form formfile", 0 },
24 #define INPLSW  5
25         { "inplace", 0 },
26 #define NINPLSW  6
27         { "noinplace", 0 },
28 #define WHATSW  7
29         { "whatnowproc program", 0 },
30 #define NWHATSW  8
31         { "nowhatnowproc", 0 },
32 #define VERSIONSW  9
33         { "version", 0 },
34 #define HELPSW  10
35         { "help", 0 },
36 #define FILESW  11
37         { "file file", -4 },  /* interface from msh */
38         { NULL, 0 }
39 };
40
41
42 int
43 main(int argc, char **argv)
44 {
45         int anot = 0, inplace = 1, nedit = 0;
46         int nwhat = 0, in, out;
47         char *cp, *cwd, *maildir, *msgnam;
48         char *ed = NULL, *file = NULL, *folder = NULL;
49         char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
50         char **argp, **arguments;
51         struct msgs *mp = NULL;
52
53 #ifdef LOCALE
54         setlocale(LC_ALL, "");
55 #endif
56         invo_name = mhbasename(argv[0]);
57
58         /* read user profile/context */
59         context_read();
60
61         arguments = getarguments(invo_name, argc, argv, 1);
62         argp = arguments;
63
64         while ((cp = *argp++)) {
65                 if (*cp == '-') {
66                         switch (smatch(++cp, switches)) {
67                         case AMBIGSW:
68                                 ambigsw(cp, switches);
69                                 done(1);
70                         case UNKWNSW:
71                                 adios(NULL, "-%s unknown", cp);
72
73                         case HELPSW:
74                                 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
75                                 print_help(buf, switches, 1);
76                                 done(1);
77                         case VERSIONSW:
78                                 print_version(invo_name);
79                                 done(1);
80
81                         case ANNOSW:
82                                 anot++;
83                                 continue;
84                         case NANNOSW:
85                                 anot = 0;
86                                 continue;
87
88                         case EDITRSW:
89                                 if (!(ed = *argp++) || *ed == '-')
90                                         adios(NULL, "missing argument to %s",
91                                                         argp[-2]);
92                                 nedit = 0;
93                                 continue;
94                         case NEDITSW:
95                                 nedit++;
96                                 continue;
97
98                         case WHATSW:
99                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
100                                         adios(NULL, "missing argument to %s",
101                                                         argp[-2]);
102                                 nwhat = 0;
103                                 continue;
104                         case NWHATSW:
105                                 nwhat++;
106                                 continue;
107
108                         case FILESW:
109                                 if (file)
110                                         adios(NULL, "only one file at a time!");
111                                 if (!(cp = *argp++) || *cp == '-')
112                                         adios(NULL, "missing argument to %s",
113                                                         argp[-2]);
114                                 file = getcpy(expanddir(cp));
115                                 continue;
116                         case FORMSW:
117                                 if (!(form = *argp++) || *form == '-')
118                                         adios(NULL, "missing argument to %s",
119                                                         argp[-2]);
120                                 continue;
121
122                         case INPLSW:
123                                 inplace++;
124                                 continue;
125                         case NINPLSW:
126                                 inplace = 0;
127                                 continue;
128                         }
129                 }
130                 if (*cp == '+' || *cp == '@') {
131                         if (folder)
132                                 adios(NULL, "only one folder at a time!");
133                         else
134                                 folder = getcpy(expandfol(cp));
135                 } else {
136                         if (msg)
137                                 adios(NULL, "only one message at a time!");
138                         else
139                                 msg = cp;
140                 }
141         }
142
143         cwd = getcpy(pwd());
144
145         if (file && (msg || folder))
146                 adios(NULL, "can't mix files and folders/msgs");
147
148         in = open_form(&form, distcomps);
149
150         strncpy(drft, m_draft("new"), sizeof(drft));
151
152         if ((out = creat(drft, m_gmprot())) == NOTOK)
153                 adios(drft, "unable to create");
154
155         cpydata(in, out, form, drft);
156         close(in);
157         close(out);
158
159         if (file) {
160                 /*
161                 ** Dist a file
162                 */
163                 anot = 0;  /* don't want to annotate a file */
164         } else {
165                 /*
166                 ** Dist a message
167                 */
168                 if (!msg)
169                         msg = "cur";
170                 if (!folder)
171                         folder = getcurfol();
172                 maildir = toabsdir(folder);
173
174                 if (chdir(maildir) == NOTOK)
175                         adios(maildir, "unable to change directory to");
176
177                 /* read folder and create message structure */
178                 if (!(mp = folder_read(folder)))
179                         adios(NULL, "unable to read folder %s", folder);
180
181                 /* check for empty folder */
182                 if (mp->nummsg == 0)
183                         adios(NULL, "no messages in %s", folder);
184
185                 /* parse the message range/sequence/name and set SELECTED */
186                 if (!m_convert(mp, msg))
187                         done(1);
188                 seq_setprev(mp);  /* set the previous-sequence */
189
190                 if (mp->numsel > 1)
191                         adios(NULL, "only one message at a time!");
192         }
193
194         msgnam = file ? file : getcpy(m_name(mp->lowsel));
195         if ((in = open(msgnam, O_RDONLY)) == NOTOK)
196                 adios(msgnam, "unable to open message");
197
198         if (!file) {
199                 context_replace(curfolder, folder); /* update current folder */
200                 seq_setcur(mp, mp->lowsel);  /* update current message */
201                 seq_save(mp);  /* synchronize sequences  */
202                 context_save();  /* save the context file  */
203         }
204
205         if (nwhat)
206                 done(0);
207         what_now(ed, nedit, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL,
208                         inplace, cwd);
209         done(1);
210         return 1;
211 }