Use sysexits.h for better exit-codes
[mmh] / uip / mhpath.c
1 /*
2 ** mhpath.c -- print full pathnames of nmh messages and folders
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 <unistd.h>
12 #include <locale.h>
13 #include <sysexits.h>
14
15 static struct swit switches[] = {
16 #define VERSIONSW 0
17         { "Version", 0 },
18 #define HELPSW  1
19         { "help", 0 },
20         { NULL, 0 }
21 };
22
23 int
24 main(int argc, char **argv)
25 {
26         int i;
27         char *cp, *maildir, *folder = NULL;
28         char **argp;
29         char **arguments, buf[BUFSIZ];
30         struct msgs_array msgs = { 0, 0, NULL };
31         struct msgs *mp;
32
33         setlocale(LC_ALL, "");
34         invo_name = mhbasename(argv[0]);
35
36         /* read user profile/context */
37         context_read();
38
39         arguments = getarguments(invo_name, argc, argv, 1);
40         argp = arguments;
41
42         /*
43         ** Parse arguments
44         */
45         while ((cp = *argp++)) {
46                 if (*cp == '-') {
47                         switch (smatch(++cp, switches)) {
48                         case AMBIGSW:
49                                 ambigsw(cp, switches);
50                                 exit(EX_USAGE);
51                         case UNKWNSW:
52                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
53
54                         case HELPSW:
55                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
56                                 print_help(buf, switches, 1);
57                                 exit(argc == 2 ? EX_OK : EX_USAGE);
58                         case VERSIONSW:
59                                 print_version(invo_name);
60                                 exit(argc == 2 ? EX_OK : EX_USAGE);
61                         }
62                 }
63                 if (*cp == '+' || *cp == '@') {
64                         if (folder) {
65                                 adios(EX_USAGE, NULL, "only one folder at a time!");
66                         } else {
67                                 folder = getcpy(expandfol(cp));
68                         }
69                 } else {
70                         app_msgarg(&msgs, cp);
71                 }
72         }
73
74         if (!folder)
75                 folder = getcurfol();
76         maildir = toabsdir(folder);
77
78         /* If no messages are given, print folder pathname */
79         if (!msgs.size) {
80                 printf("%s\n", maildir);
81                 exit(EX_OK);
82         }
83
84         if (chdir(maildir) == NOTOK)
85                 adios(EX_OSERR, maildir, "unable to change directory to");
86
87         /* read folder and create message structure */
88         if (!(mp = folder_read(folder)))
89                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
90
91         /*
92         ** We need to make sure there is message status space
93         ** for all the message numbers from 1 to one beyond last since
94         ** mhpath can select empty slots.  If we are adding
95         ** space at the end, we go ahead and add 10 slots.
96         */
97         if (mp->hghmsg >= mp->hghoff) {
98                 if (!(mp = folder_realloc(mp, 1, mp->hghmsg + 10))) {
99                         adios(EX_OSERR, NULL, "unable to allocate folder storage");
100                 }
101         } else if (mp->lowoff > 1) {
102                 if (!(mp = folder_realloc(mp, 1, mp->hghoff))) {
103                         adios(EX_OSERR, NULL, "unable to allocate folder storage");
104                 }
105         }
106         /*
107         ** TODO: As folder_realloc() checks itself if the realloc
108         ** really is necesary, why don't we then:
109         **    if (!(mp = folder_realloc (mp, 1, mp->hghmsg+1)))
110         **        adios (NULL, "unable to allocate folder storage");
111         ** ? This at least appears most clear to me. -- meillo
112         */
113
114
115         mp->msgflags |= ALLOW_BEYOND;  /* allow the beyond sequence */
116
117         /* parse all the message ranges/sequences and set SELECTED */
118         for (i = 0; i < msgs.size; i++) {
119                 if (!m_convert(mp, msgs.msgs[i])) {
120                         exit(EX_SOFTWARE);
121                 }
122         }
123
124         seq_setprev(mp);  /* set the previous-sequence */
125
126         /* print the path of all selected messages */
127         for (i = mp->lowsel; i <= mp->hghsel; i++)
128                 if (is_selected(mp, i))
129                         printf("%s/%s\n", mp->foldpath, m_name(i));
130
131         seq_save(mp);  /* synchronize message sequences */
132         context_save();  /* save the context file */
133         folder_free(mp);  /* free folder/message structure */
134         return EX_OK;
135 }