Rearranged whitespace (and comments) in all the code!
[mmh] / uip / rmm.c
1 /*
2  * rmm.c -- remove a message(s)
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
12 static struct swit switches[] = {
13 #define UNLINKSW  0
14         { "unlink", 0 },
15 #define NUNLINKSW  1
16         { "nounlink", 0 },
17 #define VERSIONSW  2
18         { "version", 0 },
19 #define HELPSW  3
20         { "help", 0 },
21         { NULL, 0 }
22 };
23
24
25 int
26 main (int argc, char **argv)
27 {
28         int msgnum, unlink_msgs = 0;
29         char *cp, *maildir, *folder = NULL;
30         char buf[BUFSIZ], **argp;
31         char **arguments;
32         struct msgs_array msgs = { 0, 0, NULL };
33         struct msgs *mp;
34
35 #ifdef LOCALE
36         setlocale(LC_ALL, "");
37 #endif
38         invo_name = r1bindex (argv[0], '/');
39
40         /* read user profile/context */
41         context_read();
42
43         arguments = getarguments (invo_name, argc, argv, 1);
44         argp = arguments;
45
46         /* parse arguments */
47         while ((cp = *argp++)) {
48                 if (*cp == '-') {
49                         switch (smatch (++cp, switches)) {
50                         case AMBIGSW:
51                                 ambigsw (cp, switches);
52                                 done (1);
53                         case UNKWNSW:
54                                 adios (NULL, "-%s unknown\n", cp);
55
56                         case HELPSW:
57                                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
58                                                   invo_name);
59                                 print_help (buf, switches, 1);
60                                 done (1);
61                         case VERSIONSW:
62                                 print_version(invo_name);
63                                 done (1);
64
65                         case UNLINKSW:
66                                 unlink_msgs++;
67                                 continue;
68                         case NUNLINKSW:
69                                 unlink_msgs = 0;
70                                 continue;
71                         }
72                 }
73                 if (*cp == '+' || *cp == '@') {
74                         if (folder)
75                                 adios (NULL, "only one folder at a time!");
76                         else
77                                 folder = pluspath (cp);
78                 } else
79                                 app_msgarg(&msgs, cp);
80         }
81
82         if (!context_find ("path"))
83                 free (path ("./", TFOLDER));
84         if (!msgs.size)
85                 app_msgarg(&msgs, "cur");
86         if (!folder)
87                 folder = getfolder (1);
88         maildir = m_maildir (folder);
89
90         if (chdir (maildir) == NOTOK)
91                 adios (maildir, "unable to change directory to");
92
93         /* read folder and create message structure */
94         if (!(mp = folder_read (folder)))
95                 adios (NULL, "unable to read folder %s", folder);
96
97         /* check for empty folder */
98         if (mp->nummsg == 0)
99                 adios (NULL, "no messages in %s", folder);
100
101         /* parse all the message ranges/sequences and set SELECTED */
102         for (msgnum = 0; msgnum < msgs.size; msgnum++)
103                 if (!m_convert (mp, msgs.msgs[msgnum]))
104                         done (1);
105         seq_setprev (mp);  /* set the previous-sequence */
106
107         /*
108          * This is hackish.  If we are using a external rmmproc,
109          * then we need to update the current folder in the
110          * context so the external rmmproc will remove files
111          * from the correct directory.  This should be moved to
112          * folder_delmsgs().
113          */
114         if (rmmproc) {
115                 context_replace (pfolder, folder);
116                 context_save ();
117                 fflush (stdout);
118         }
119
120         /* "remove" the SELECTED messages */
121         folder_delmsgs (mp, unlink_msgs, 0);
122
123         seq_save (mp);  /* synchronize message sequences  */
124         context_replace (pfolder, folder);  /* update current folder   */
125         context_save ();  /* save the context file   */
126         folder_free (mp);  /* free folder structure   */
127         done (0);
128         return 1;
129 }