remove unused defines in uip/pick.c
[mmh] / uip / whatnow.c
1 /*
2 ** whatnow.c -- the WhatNow shell
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 <fcntl.h>
11 #include <signal.h>
12 #include <h/mime.h>
13 #include <h/utils.h>
14 #include <unistd.h>
15 #include <sys/stat.h>
16 #include <locale.h>
17 #include <sysexits.h>
18
19 #ifdef HAVE_SYS_PARAM_H
20 # include <sys/param.h>
21 #endif
22
23 static struct swit switches[] = {
24 #define EDITRSW  0
25         { "editor editor", 0 },
26 #define PRMPTSW  1
27         { "prompt string", 0 },
28 #define VERSIONSW  2
29         { "Version", 0 },
30 #define HELPSW  3
31         { "help", 0 },
32         { NULL, 0 }
33 };
34
35 char *version=VERSION;
36
37 /*
38 ** Options at the "whatnow" prompt
39 */
40 static struct swit aleqs[] = {
41 #define EDITSW  0
42         { "edit [editor [switches]]", 0 },
43 #define LISTSW  1
44         { "list", 0 },
45 #define DISPSW  2
46         { "display", 0 },
47 #define WHOMSW  3
48         { "whom", 0 },
49 #define SENDSW  4
50         { "send", 0 },
51 #define REFILEOPT  5
52         { "refile +folder", 0 },
53 #define DELETESW  6
54         { "delete", 0 },
55 #define QUITSW  7
56         { "quit", 0 },
57 #define CDCMDSW  8
58         { "cd [directory]", 0 },
59 #define PWDCMDSW  9
60         { "pwd", 0 },
61 #define LSCMDSW  10
62         { "ls", 0 },
63 #define ALISTCMDSW  11
64         { "alist", 0 },
65 #define ATTACHCMDSW  12
66         { "attach files", 0 },
67 #define DETACHCMDSW  13
68         { "detach numbers", 0 },
69         { NULL, 0 }
70 };
71
72 static char *myprompt = "\nWhat now? ";
73
74 /*
75 ** static prototypes
76 */
77 static int editfile(char **, char **, char *);
78 static int sendfile(char **, char *);
79 static int refile(char **, char *);
80 static int removefile(char *);
81 static void writelscmd(char *, int, char **);
82 static void writesomecmd(char *, int, char *, char *, char **);
83 static FILE* popen_in_dir(const char *, const char *, const char *);
84 static int system_in_dir(const char *, const char *);
85
86
87 int
88 main(int argc, char **argv)
89 {
90         int use = NOUSE;
91         char *cp;
92         char *ed = NULL, *drft = NULL;
93         char buf[BUFSIZ], prompt[BUFSIZ];
94         char **argp, **arguments;
95         struct stat st;
96         char cwd[MAXPATHLEN + 1];  /* current working directory */
97         char file[MAXPATHLEN + 1];  /* file name buffer */
98         char shell[MAXPATHLEN + 1];  /* shell response buffer */
99         FILE *f;  /* read pointer for bgnd proc */
100
101         setlocale(LC_ALL, "");
102         invo_name = mhbasename(argv[0]);
103
104         /* read user profile/context */
105         context_read();
106
107         advise(NULL, "whatnow is deprecated. Consider switching to whatnow2.");
108
109         arguments = getarguments(invo_name, argc, argv, 1);
110         argp = arguments;
111
112         /*
113         ** Get the initial current working directory.
114         */
115
116         if (!getcwd(cwd, sizeof (cwd))) {
117                 adios(EX_USAGE, "getcwd", "could not get working directory");
118         }
119
120         while ((cp = *argp++)) {
121                 if (*cp == '-') {
122                         switch (smatch(++cp, switches)) {
123                         case AMBIGSW:
124                                 ambigsw(cp, switches);
125                                 exit(EX_USAGE);
126                         case UNKWNSW:
127                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
128
129                         case HELPSW:
130                                 snprintf(buf, sizeof(buf),
131                                                 "%s [switches] [file]",
132                                                 invo_name);
133                                 print_help(buf, switches, 1);
134                                 exit(argc == 2 ? EX_OK : EX_USAGE);
135                         case VERSIONSW:
136                                 print_version(invo_name);
137                                 exit(argc == 2 ? EX_OK : EX_USAGE);
138
139                         case EDITRSW:
140                                 if (!(ed = *argp++) || *ed == '-')
141                                         adios(EX_USAGE, NULL, "missing argument to %s",
142                                                         argp[-2]);
143                                 continue;
144
145                         case PRMPTSW:
146                                 if (!(myprompt = *argp++) || *myprompt == '-')
147                                         adios(EX_USAGE, NULL, "missing argument to %s",
148                                                         argp[-2]);
149                                 continue;
150
151                         }
152                 }
153                 if (drft)
154                         adios(EX_USAGE, NULL, "only one draft at a time!");
155                 else
156                         drft = cp;
157         }
158
159         if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
160                 drft = mh_xstrdup(m_draft(seq_cur));
161
162         if ((cp = getenv("mhuse")) && *cp)
163                 use = atoi(cp);
164
165         if (!ed && !(ed = getenv("mheditor"))) {
166                 ed = "";  /* Don't initially edit the draft */
167         }
168
169         /* start editing the draft, unless editor is the empty string */
170         if (*ed) {
171                 if (editfile(&ed, NULL, drft) <0) {
172                         if (!use) {
173                                 unlink(drft);
174                         }
175                         adios(EX_SOFTWARE, NULL, "Try again.");
176                 }
177         }
178
179         snprintf(prompt, sizeof(prompt), myprompt, invo_name);
180         while ((argp = getans(prompt, aleqs))) {
181                 switch (smatch(*argp, aleqs)) {
182                 case DISPSW:
183                         /* display the msg being replied to or distributed */
184                         if ((cp = getenv("mhaltmsg")) && *cp) {
185                                 execprogl(listproc, listproc, "-file", cp,
186                                                 (char *)NULL);
187                         } else {
188                                 advise(NULL, "no alternate message to display");
189                         }
190                         break;
191
192                 case EDITSW:
193                         /* Call an editor on the draft file */
194                         if (*++argp)
195                                 ed = *argp++;
196                         editfile(&ed, argp, drft);
197                         break;
198
199                 case LISTSW:
200                         /* display the draft file */
201                         execprogl(listproc, listproc, "-file", drft,
202                                         (char *)NULL);
203                         break;
204
205                 case QUITSW:
206                         /* quit */
207                         if (stat(drft, &st) != NOTOK) {
208                                 advise(NULL, "draft left on %s", drft);
209                         }
210                         exit(EX_OK);
211
212                 case DELETESW:
213                         /* Delete draft and exit */
214                         removefile(drft);
215                         exit(EX_OK);
216
217                 case SENDSW:
218                         /* Send draft */
219                         sendfile(++argp, drft);
220                         break;
221
222                 case REFILEOPT:
223                         /* Refile the draft */
224                         if (refile(++argp, drft) == 0) {
225                                 exit(EX_OK);
226                         }
227                         break;
228
229                 case CDCMDSW:
230                         /*
231                         ** Change the working directory for attachments
232                         **
233                         ** Run the directory through the user's shell
234                         ** so that we can take advantage of any syntax
235                         ** that the user is accustomed to.  Read back
236                         ** the absolute path.
237                         */
238
239                         if (*(argp+1) == NULL) {
240                                 sprintf(buf, "$SHELL -c \"cd;pwd\"");
241                         } else {
242                                 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
243                         }
244                         if ((f = popen_in_dir(cwd, buf, "r"))) {
245                                 fgets(cwd, sizeof (cwd), f);
246
247                                 if (strchr(cwd, '\n'))
248                                         *strchr(cwd, '\n') = '\0';
249
250                                 pclose(f);
251                         } else {
252                                 advise("popen", "could not get directory");
253                         }
254
255                         break;
256
257                 case PWDCMDSW:
258                         /* Print the working directory for attachments */
259                         printf("%s\n", cwd);
260                         break;
261
262                 case LSCMDSW:
263                         /*
264                         ** List files in the current attachment working
265                         ** directory
266                         **
267                         ** Use the user's shell so that we can take
268                         ** advantage of any syntax that the user is
269                         ** accustomed to.
270                         */
271                         writelscmd(buf, sizeof(buf), argp);
272                         system_in_dir(cwd, buf);
273                         break;
274
275                 case ALISTCMDSW:
276                         /*
277                         ** List attachments on current draft.
278                         */
279                         if (execprogl("anno", "anno", "-list", "-comp",
280                                         attach_hdr, "-number", drft,
281                                         (char *)NULL) != 0) {
282                                 advise(NULL, "Could not list attachment headers.");
283                         }
284                         break;
285
286                 case ATTACHCMDSW:
287                         /*
288                         ** Attach files to current draft.
289                         */
290
291                         if (*(argp+1) == NULL) {
292                                 advise(NULL, "attach command requires file argument(s).");
293                                 break;
294                         }
295
296                         /*
297                         ** Build a command line that causes the user's
298                         ** shell to list the file name arguments.
299                         ** This handles and wildcard expansion, tilde
300                         ** expansion, etc.
301                         */
302                         writelscmd(buf, sizeof(buf), argp);
303
304                         /*
305                         ** Read back the response from the shell,
306                         ** which contains a number of lines with one
307                         ** file name per line.  Remove off the newline.
308                         ** Determine whether we have an absolute or
309                         ** relative path name.  Prepend the current
310                         ** working directory to relative path names.
311                         ** Add the attachment annotation to the draft.
312                         */
313                         if (!(f = popen_in_dir(cwd, buf, "r"))) {
314                                 advise("popen", "could not get file from shell");
315                                 break;
316                         }
317
318                         while (fgets(shell, sizeof(shell), f)) {
319                                 *(strchr(shell, '\n')) = '\0';
320
321                                 if (*shell == '/')
322                                         sprintf(file, "%s", shell);
323                                 else {
324                                         sprintf(file, "%s/%s", cwd, shell);
325                                 }
326                                 if (execprogl("anno", "anno",
327                                                 "-nodate", "-append",
328                                                 "-comp", attach_hdr,
329                                                 "-text", file,
330                                                 drft, (char *)NULL) != 0) {
331                                         advise(NULL, "Could not add attachment header.");
332                                 }
333                         }
334                         pclose(f);
335                         break;
336
337                 case DETACHCMDSW:
338                         /*
339                         ** Detach files from current draft.
340                         **
341                         ** Interpret the arguments as
342                         ** attachment numbers.  Decrement any remaining
343                         ** argument number that is greater than the one
344                         ** just processed after processing each one so
345                         ** that the numbering stays correct.
346                         */
347                         for (arguments=argp+1; *arguments; arguments++) {
348                                 int n;
349
350                                 if (**arguments == '\0') {
351                                         continue;
352                                 }
353
354                                 if (execprogl("anno", "anno", "-delete",
355                                                 "-comp", attach_hdr,
356                                                 "-number", *arguments, drft,
357                                                 (char *)NULL) != 0) {
358                                         advise(NULL, "Could not delete attachment header.");
359                                 }
360
361                                 n = atoi(*arguments);
362                                 for (argp=arguments+1; *argp; argp++) {
363                                         if (atoi(*argp) > n) {
364                                                 if (atoi(*argp) == 1) {
365                                                         *argp = "";
366                                                 } else {
367                                                         sprintf(*argp, "%d", atoi(*argp) - 1);
368                                                 }
369                                         }
370                                 }
371                         }
372                         break;
373
374                 case WHOMSW:
375                         /* list recipients */
376                         execprogl("whom", "whom", drft, (char *)NULL);
377                         break;
378
379                 default:
380                         /* Unknown command */
381                         advise(NULL, "say what?");
382                         break;
383                 }
384         }
385
386         exit(EX_IOERR);
387 }
388
389
390
391 /*
392 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
393 ** trailcmd".
394 */
395 static void
396 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
397 {
398         char *cp;
399         /*
400         ** Note that we do not quote -- the argp from the user
401         ** is assumed to be quoted as they desire. (We can't treat
402         ** it as pure literal as that would prevent them using ~,
403         ** wildcards, etc.) The buffer produced by this function
404         ** should be given to popen_in_dir() or system_in_dir() so
405         ** that the current working directory is set correctly.
406         */
407         int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
408         /*
409         ** NB that some snprintf() return -1 on overflow rather than the
410         ** new C99 mandated 'number of chars that would have been written'
411         */
412         /*
413         ** length checks here and inside the loop allow for the
414         ** trailing ';', trailcmd, '"' and NUL
415         */
416         int trailln = strlen(trailcmd) + 3;
417         if (ln < 0 || ln + trailln > bufsz)
418                 adios(EX_USAGE, NULL, "arguments too long");
419
420         cp = buf + ln;
421
422         while (*++argp) {
423                 ln = strlen(*argp);
424                 /* +1 for leading space */
425                 if (ln + trailln + 1 > bufsz - (cp-buf))
426                         adios(EX_USAGE, NULL, "arguments too long");
427                 *cp++ = ' ';
428                 memcpy(cp, *argp, ln+1);
429                 cp += ln;
430         }
431         if (*trailcmd) {
432                 *cp++ = ';';
433                 strcpy(cp, trailcmd);
434                 cp += trailln - 3;
435         }
436         *cp++ = '"';
437         *cp = 0;
438 }
439
440 /*
441 ** Build a command line that causes the user's shell to list the file name
442 ** arguments.  This handles and wildcard expansion, tilde expansion, etc.
443 */
444 static void
445 writelscmd(char *buf, int bufsz, char **argp)
446 {
447         writesomecmd(buf, bufsz, "ls", "", argp);
448 }
449
450 /*
451 ** Like system(), but run the command in directory dir.
452 ** This assumes the program is single-threaded!
453 */
454 static int
455 system_in_dir(const char *dir, const char *cmd)
456 {
457         char olddir[BUFSIZ];
458         int r;
459         if (getcwd(olddir, sizeof(olddir)) == 0)
460                 adios(EX_OSERR, "getcwd", "could not get working directory");
461         if (chdir(dir) != 0)
462                 adios(EX_OSERR, "chdir", "could not change working directory");
463         r = system(cmd);
464         if (chdir(olddir) != 0)
465                 adios(EX_OSERR, "chdir", "could not change working directory");
466         return r;
467 }
468
469 /* ditto for popen() */
470 static FILE*
471 popen_in_dir(const char *dir, const char *cmd, const char *type)
472 {
473         char olddir[BUFSIZ];
474         FILE *f;
475         if (getcwd(olddir, sizeof(olddir)) == 0)
476                 adios(EX_OSERR, "getcwd", "could not get working directory");
477         if (chdir(dir) != 0)
478                 adios(EX_OSERR, "chdir", "could not change working directory");
479         f = popen(cmd, type);
480         if (chdir(olddir) != 0)
481                 adios(EX_OSERR, "chdir", "could not change working directory");
482         return f;
483 }
484
485
486 /*
487 ** EDIT
488 */
489
490 static char *edsave = NULL;  /* the editor we used previously */
491
492
493 static int
494 editfile(char **ed, char **arg, char *file)
495 {
496         int pid, status, vecp;
497         char *cp, *vec[MAXARGS];
498
499         if (!*ed || !**ed) {
500                 /* We have no explicit editor. */
501                 if (edsave) {
502                         /* Use the previous editor ... */
503                         *ed = edsave;
504                         if (!(cp = mhbasename(*ed)))
505                                 cp = *ed;
506
507                         /* but prefer one specified via "editor-next" */
508                         cp = concat(cp, "-next", NULL);
509                         if ((cp = context_find(cp)))
510                                 *ed = cp;
511                 } else {
512                         /* set initial editor */
513                         *ed = defaulteditor;
514                 }
515         }
516
517         context_save();
518         fflush(stdout);
519
520         switch (pid = fork()) {
521         case NOTOK:
522                 advise("fork", "unable to");
523                 status = EX_OSERR;
524                 break;
525
526         case OK:
527                 vecp = 0;
528                 vec[vecp++] = mhbasename(*ed);
529                 while (arg && *arg) {
530                         vec[vecp++] = *arg++;
531                 }
532                 vec[vecp++] = file;
533                 vec[vecp] = NULL;
534
535                 execvp(*ed, vec);
536                 fprintf(stderr, "%s: unable to exec ", invo_name);
537                 perror(*ed);
538                 _exit(EX_OSERR);
539
540         default:
541                 if ((status = pidwait(pid, NOTOK))) {
542                         if ((status & 0xff00) == 0xff00) {
543                                 /* cmd not found or pidwait() failed */
544                                 status = EX_SOFTWARE;
545                                 break;
546                         }
547                         if (status & 0x00ff) {
548                                 /* terminated by signal */
549                                 advise(NULL, "%s terminated by signal %d",
550                                                 *ed, status & 0x7f);
551                         } else {
552                                 /* failure exit */
553                                 advise(NULL, "%s exited with return code %d",
554                                                 *ed, (status & 0xff00) >> 8);
555                         }
556                         status = -1;
557                         break;
558                 }
559         }
560
561         /* remember which editor we used */
562         edsave = mh_xstrdup(*ed);
563
564         *ed = NULL;
565
566         return status;
567 }
568
569
570 /*
571 ** SEND
572 */
573
574 static int
575 sendfile(char **arg, char *file)
576 {
577         int vecp = 0;
578         char *vec[MAXARGS];
579
580         context_save();
581         fflush(stdout);
582
583         vec[vecp++] = "send";
584         while (arg && *arg) {
585                 vec[vecp++] = *arg++;
586         }
587         vec[vecp++] = file;
588         vec[vecp] = NULL;
589         execvp(*vec, vec);
590         fprintf(stderr, "%s: unable to exec ", invo_name);
591         perror("send");
592         _exit(EX_OSERR);
593 }
594
595
596 /*
597 ** refile msg into another folder
598 */
599 static int
600 refile(char **arg, char *file)
601 {
602         int vecp = 0;
603         char *vec[MAXARGS];
604
605         vec[vecp++] = "refile";
606         vec[vecp++] = "-nolink";  /* override bad .mh_profile defaults */
607         vec[vecp++] = "-file";
608         vec[vecp++] = file;
609
610         while (arg && *arg) {
611                 vec[vecp++] = *arg++;
612         }
613         vec[vecp] = NULL;
614
615         context_save();
616         fflush(stdout);
617
618         return execprog(*vec, vec);
619 }
620
621
622 /*
623 ** Remove the draft file
624 */
625
626 static int
627 removefile(char *drft)
628 {
629         if (unlink(drft) == NOTOK)
630                 adios(EX_IOERR, drft, "unable to unlink");
631
632         return OK;
633 }