]> diplodocus.org Git - nmh/blob - sbr/showfile.c
Improve fmttest output
[nmh] / sbr / showfile.c
1 /* showfile.c -- invoke the `lproc' command
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "showfile.h"
10 #include "r1bindex.h"
11 #include "context_save.h"
12 #include "arglist.h"
13 #include "error.h"
14
15
16 int
17 showfile (char **arg, char *file)
18 {
19 pid_t pid;
20 int vecp;
21 char **vec, *program;
22 int retval = 1;
23
24 context_save(); /* save the context file */
25 fflush(stdout);
26
27 /*
28 * If you have your lproc listed as "mhl",
29 * then really invoked the mhlproc instead
30 * (which is usually mhl anyway).
31 */
32 if (!strcmp (r1bindex (lproc, '/'), "mhl"))
33 lproc = mhlproc;
34
35 switch (pid = fork()) {
36 case -1:
37 /* fork error */
38 advise ("fork", "unable to");
39 break;
40
41 case 0:
42 /* child */
43 vec = argsplit(lproc, &program, &vecp);
44 bool isdraft = true;
45 if (arg) {
46 while (*arg) {
47 if (**arg != '-')
48 isdraft = false;
49 vec[vecp++] = *arg++;
50 }
51 }
52 if (isdraft) {
53 if (!strcmp (vec[0], "show"))
54 vec[vecp++] = "-file";
55 vec[vecp++] = file;
56 }
57 vec[vecp] = NULL;
58
59 execvp (program, vec);
60 fprintf (stderr, "unable to exec ");
61 perror (lproc);
62 _exit(1);
63
64 default:
65 /* parent */
66 retval = pidwait (pid, -1) & 0377 ? 1 : 0;
67 }
68
69 return retval;
70 }