]> diplodocus.org Git - nmh/blob - docs/historical/mh-nov-1983/cmds/comp.c
Removed --depth 1 from git clone invocation.
[nmh] / docs / historical / mh-nov-1983 / cmds / comp.c
1 #include "mh.h"
2 #include <stdio.h>
3 #include <strings.h>
4 #include <signal.h>
5
6 /* #define NEWS 1 */
7 #define NONE 0
8
9 /* #define TEST 1 */
10
11 char *anyul[] = {
12 "no", 0,
13 "yes", 0,
14 "use", 0,
15 "list", 0,
16 0
17 };
18
19 char *aleqs[] = {
20 "list", 0, /* 0 */
21 "edit [<editor>]", 0, /* 1 */
22 "quit [delete]", 0, /* 2 */
23 "send [verbose]", 0, /* 3 */
24 0
25 };
26
27
28 struct swit switches[] = {
29 "editor editor", 0, /* 0 */
30 "form formfile", 0, /* 1 */
31 "use", 0, /* 2 */
32 "nouse", 0, /* 3 */
33 "help", 4, /* 4 */
34 0, 0
35 };
36
37 main(argc, argv)
38 char *argv[];
39 {
40 register char *cp;
41 register int in, out;
42 int use, cnt, status, intr;
43 char buf[BUFSIZ], *ed, *file, *form;
44 static char path[128];
45 char **ap;
46 char *arguments[50], **argp;
47
48 /*** setbuf(stdout, _sobuf); ***/
49 #ifdef NEWS
50 m_news();
51 #endif
52 form = 0; use = 0; file = 0; ed = 0;
53 cp = r1bindex(argv[0], '/');
54 if((cp = m_find(cp)) != NULL) {
55 ap = brkstring(cp = getcpy(cp), " ", "\n");
56 ap = copyip(ap, arguments);
57 } else
58 ap = arguments;
59 copyip(argv+1, ap);
60 argp = arguments;
61 while(cp = *argp++) {
62 if(*cp == '-') {
63 switch(smatch(++cp, switches)) {
64 case -2:ambigsw(cp, switches); /* ambiguous */
65 goto leave;
66 /* unknown */
67 case -1:fprintf(stderr, "comp: -%s unknown\n", cp);
68 goto leave;
69 case 0: if(!(ed = *argp++)) { /* -editor */
70 missing: fprintf(stderr, "comp: Missing argument for %s switch\n", argp[-2]);
71 goto leave;
72 }
73 continue;
74 case 1: if(!(form = *argp++)) /* -form */
75 goto missing;
76 continue;
77 case 2: use = 1; continue; /* -use */
78 case 3: use = 0; continue; /* -nouse */
79 case 4: help("comp [file] [switches]",
80 switches);
81 goto leave;
82 }
83 }
84 file = cp;
85 }
86 if(form) {
87 if((in = open(m_maildir(form), 0)) < 0) {
88 fprintf(stderr, "comp: Can't open form file: %s\n", form);
89 goto leave;
90 }
91 } else if((in = open(m_maildir(components), 0)) < 0 &&
92 (in = open(stdcomps, 0)) < 0) {
93 fprintf(stderr, "comp: Can't open default components file!!\n");
94 goto leave;
95 }
96 if(!file)
97 file = draft;
98 copy(m_maildir(file), path);
99 if((out = open(path, 0)) >= 0) {
100 cp = concat("\n\"", path, "\" exists; delete? ", 0);
101 if(use || fdcompare(in, out))
102 goto editit;
103 while((status = gans(cp, anyul)) == 3)
104 showfile(path);
105 if(status == 2) {
106 use++;
107 goto editit;
108 }
109 if(status == 0)
110 goto leave;
111 close(out);
112 } else if(use) {
113 fprintf(stderr, "comp: \"%s\" doesn't exist!\n", path);
114 goto leave;
115 }
116 if((out = creat(path, m_gmprot())) < 0) {
117 fprintf(stderr, "comp: Can't create \"%s\"\n", path);
118 goto leave;
119 }
120 do
121 if(cnt = read(in, buf, sizeof buf))
122 write(out, buf, cnt);
123 while(cnt == sizeof buf);
124 close(in);
125 editit:
126 close(out);
127 if(m_edit(&ed, path, use, NONE) < 0)
128 goto leave;
129 #ifdef TEST
130 fprintf(stderr, "!! Test Version of SEND Being Run !!\n");
131 fprintf(stderr, " Send verbose !\n\n");
132 #endif
133
134 for(;;) {
135 if(!(argp = getans("\nWhat now? ", aleqs)))
136 goto leave;
137 switch(smatch(*argp, aleqs)) {
138 case 0: showfile(path); /* list */
139 break;
140 case 1: if(*++argp) /* edit */
141 ed = *argp;
142 if(m_edit(&ed, path, use, NONE) == -1)
143 goto leave;
144 break;
145 case 2: if(*++argp && *argp[0] == 'd') /* quit */
146 if(unlink(path) == -1) {
147 fprintf(stderr, "Can't unlink %s ", path);
148 perror("");
149 }
150 goto leave;
151 case 3: if(*++argp) cp = *argp; else cp = ""; /* send */
152 if(! m_send(cp, path))
153 goto leave;
154 default:fprintf(stderr, "comp: illegal option\n"); /*##*/
155 break;
156 }
157 }
158
159 leave:
160 m_update();
161 done(0);
162 }
163