Initialize vecp in send anno()
[mmh] / uip / comp.c
1 /*
2 ** comp.c -- compose 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 EDITRSW  0
15         { "editor editor", 0 },
16 #define FORMSW  1
17         { "form formfile", 0 },
18 #define USESW  2
19         { "use", 0 },
20 #define NUSESW  3
21         { "nouse", 2 },
22 #define WHATSW  4
23         { "whatnowproc program", 0 },
24 #define VERSIONSW  5
25         { "Version", 0 },
26 #define HELPSW  6
27         { "help", 0 },
28         { NULL, 0 }
29 };
30
31
32 int
33 main(int argc, char **argv)
34 {
35         int use = NOUSE;
36         int in, out;
37         char *cp, *cwd, *maildir;
38         char *ed = NULL, *form = NULL;
39         char *folder = NULL, *msg = NULL, buf[BUFSIZ];
40         char drft[BUFSIZ], **argp, **arguments;
41         struct msgs *mp = NULL;
42         char *fmtstr;
43
44         setlocale(LC_ALL, "");
45         invo_name = mhbasename(argv[0]);
46
47         /* read user profile/context */
48         context_read();
49
50         arguments = getarguments(invo_name, argc, argv, 1);
51         argp = arguments;
52
53         while ((cp = *argp++)) {
54                 if (*cp == '-') {
55                         switch (smatch(++cp, switches)) {
56                         case AMBIGSW:
57                                 ambigsw(cp, switches);
58                                 /* sysexits.h EX_USAGE */
59                                 exit(1);
60                         case UNKWNSW:
61                                 adios(NULL, "-%s unknown", cp);
62
63                         case HELPSW:
64                                 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
65                                 print_help(buf, switches, 1);
66                                 exit(0);
67                         case VERSIONSW:
68                                 print_version(invo_name);
69                                 exit(0);
70
71                         case EDITRSW:
72                                 if (!(ed = *argp++) || *ed == '-')
73                                         adios(NULL, "missing argument to %s", argp[-2]);
74                                 continue;
75
76                         case WHATSW:
77                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
78                                         adios(NULL, "missing argument to %s", argp[-2]);
79                                 continue;
80
81                         case FORMSW:
82                                 if (!(form = *argp++) || *form == '-')
83                                         adios(NULL, "missing argument to %s", argp[-2]);
84                                 continue;
85
86                         case USESW:
87                                 use++;
88                                 continue;
89                         case NUSESW:
90                                 use = NOUSE;
91                                 continue;
92                         }
93                 }
94                 if (*cp == '+' || *cp == '@') {
95                         if (folder)
96                                 adios(NULL, "only one folder at a time!");
97                         else
98                                 folder = getcpy(expandfol(cp));
99                 } else {
100                         if (msg)
101                                 adios(NULL, "only one message at a time!");
102                         else
103                                 msg = cp;
104                 }
105         }
106
107         cwd = getcpy(pwd());
108
109         if (form && (folder || msg))
110                 adios(NULL, "can't mix forms and folders/msgs");
111
112         if (use && folder)
113                 adios(NULL, "can't mix -use and +folder");
114
115         if (use) {
116                 /* Don't copy; the draft shall get removed in the end. */
117                 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
118
119         } else if (folder || msg) {
120                 /* Take a message as the "form" for the new message. */
121                 if (!msg)
122                         msg = seq_cur;
123                 if (!folder)
124                         folder = getcurfol();
125                 maildir = toabsdir(folder);
126                 if (chdir(maildir) == NOTOK)
127                         adios(maildir, "unable to change directory to");
128                 /* read folder and create message structure */
129                 if (!(mp = folder_read(folder)))
130                         adios(NULL, "unable to read folder %s", folder);
131                 /* check for empty folder */
132                 if (mp->nummsg == 0)
133                         adios(NULL, "no messages in %s", folder);
134                 /* parse the message range/sequence/name and set SELECTED */
135                 if (!m_convert(mp, msg))
136                         /* sysexits.h EX_USAGE */
137                         exit(1);
138                 seq_setprev(mp);  /* set the previous-sequence */
139                 if (mp->numsel > 1)
140                         adios(NULL, "only one message at a time!");
141                 if ((in = open(form = getcpy(m_name(mp->lowsel)),
142                                 O_RDONLY)) == NOTOK)
143                         adios(form, "unable to open message");
144
145                 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
146                 if ((out = creat(drft, m_gmprot())) == NOTOK) {
147                         adios(drft, "unable to create");
148                 }
149                 cpydata(in, out, form, drft);
150                 close(in);
151                 close(out);
152
153         } else {
154                 fmtstr = new_fs(form, components);
155                 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
156                 if ((out = creat(drft, m_gmprot())) == NOTOK) {
157                         adios(drft, "unable to create");
158                 }
159                 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
160                         adios(drft, "error writing");
161                 }
162                 close(out);
163         }
164
165         context_save();
166         what_now(ed, use, drft, NULL, 0, NULLMP, NULL, cwd);
167         /* sysexits.h EX_SOFTWARE */
168         return 1;
169 }