BSD 3 development
[unix-history] / usr / src / cmd / uucp / uux.c
CommitLineData
2defb729
BJ
1 /* uux 2.2 5/24/79 18:33:11 */
2#include "uucp.h"
3#include "uucpdefs.h"
4
5static char SiD[] = "@(#)uux 2.2";
6
7#define NOSYSPART 0
8#define HASSYSPART 1
9
10#define APPCMD(d) {\
11char *p;\
12for (p = d; *p != '\0';) *cmdp++ = *p++;\
13*cmdp++ = ' ';\
14*cmdp = '\0';}
15
16#define GENSEND(f, a, b, c, d) {\
17fprintf(f, "S %s %s %s - %s 0666\n", a, b, c, d);\
18}
19#define GENRCV(f, a, b, c) {\
20fprintf(f, "R %s %s %s - \n", a, b, c);\
21}
22/*
23 *
24 */
25
26main(argc, argv)
27char *argv[];
28{
29 char cfile[NAMESIZE]; /* send commands for files from here */
30 char dfile[NAMESIZE]; /* used for all data files from here */
31 char rxfile[NAMESIZE]; /* to be sent to xqt file (X. ...) */
32 char tfile[NAMESIZE]; /* temporary file name */
33 char tcfile[NAMESIZE]; /* temporary file name */
34 char t2file[NAMESIZE]; /* temporary file name */
35 int cflag = 0; /* commands in C. file flag */
36 int rflag = 0; /* C. files for receiving flag */
37 char buf[BUFSIZ];
38 char inargs[BUFSIZ];
39 int pipein = 0;
40 int startjob = 1;
41 char path[MAXFULLNAME];
42 char cmd[BUFSIZ];
43 char *ap, *cmdp;
44 char prm[BUFSIZ];
45 char syspart[8], rest[MAXFULLNAME];
46 char xsys[8], local[8];
47 FILE *fprx, *fpc, *fpd, *fp;
48 FILE *xqtstr();
49 extern char *getprm(), *index(), *lastpart();
50 int uid, ret;
51 char redir = '\0';
52
53 uucpname(Myname);
54 Ofn = 1;
55 Ifn = 0;
56 while (argc>1 && argv[1][0] == '-') {
57 switch(argv[1][1]){
58 case 'p':
59 case '\0':
60 pipein = 1;
61 break;
62 case 'r':
63 startjob = 0;
64 break;
65 case 'x':
66 Debug = atoi(&argv[1][2]);
67 if (Debug <= 0)
68 Debug = 1;
69 break;
70 default:
71 sprintf(stderr, "unknown flag %s\n", argv[1]);
72 break;
73 }
74 --argc; argv++;
75 }
76
77 DEBUG(4, "\n\n** %s **\n", "START");
78
79 inargs[0] = '\0';
80 for (argv++; argc > 1; argc--) {
81 DEBUG(4, "arg - %s:", *argv);
82 strcat(inargs, " ");
83 strcat(inargs, *argv++);
84 }
85 DEBUG(4, "arg - %s\n", inargs);
86 ret = gwd(Wrkdir);
87 if (ret != 0) {
88 fprintf(stderr, "can't get working directory; will try to continue\n");
89 strcpy(Wrkdir, "/UNKNOWN");
90 }
91 chdir(Spool);
92 uid = getuid();
93 guinfo(uid, User, path);
94
95 sprintf(local, "%.7s", Myname);
96 cmdp = cmd;
97 *cmdp = '\0';
98 gename(DATAPRE, local, 'X', rxfile);
99 fprx = fopen(rxfile, "w");
100 ASSERT(fprx != NULL, "CAN'T OPEN %s", rxfile);
101 chmod(rxfile, 0666);
102 gename(DATAPRE, local, 'T', tcfile);
103 fpc = fopen(tcfile, "w");
104 ASSERT(fpc != NULL, "CAN'T OPEN %s", tcfile);
105 chmod(tcfile, 0666);
106 fprintf(fprx, "%c %s %s\n", X_USER, User, local);
107
108 /* find remote system name */
109 ap = inargs;
110 while ((ap = getprm(ap, prm)) != NULL) {
111 if (prm[0] == '>' || prm[0] == '<') {
112 ap = getprm(ap, prm);
113 continue;
114 }
115
116 if (prm[0] == ';') {
117 APPCMD(prm);
118 continue;
119 }
120
121 split(prm, xsys, rest);
122 if (xsys[0] == '\0')
123 strcpy(xsys, local);
124 break;
125 }
126 DEBUG(4, "xsys %s\n", xsys);
127 if (versys(xsys) != 0) {
128 /* bad system name */
129 fprintf(stderr, "bad system name: %s\n", xsys);
130 fclose(fprx);
131 fclose(fpc);
132 unlink(rxfile);
133 unlink(tcfile);
134 cleanup(101);
135 }
136
137 if (pipein) {
138 gename(DATAPRE, xsys, 'B', dfile);
139 fpd = fopen(dfile, "w");
140 ASSERT(fpd != NULL, "CAN'T OPEN %s", dfile);
141 chmod(dfile, 0666);
142 while (fgets(buf, BUFSIZ, stdin) != NULL)
143 fputs(buf, fpd);
144 fclose(fpd);
145 if (strcmp(local, xsys) != SAME) {
146 GENSEND(fpc, dfile, dfile, User, dfile);
147 cflag++;
148 }
149 fprintf(fprx, "%c %s\n", X_RQDFILE, dfile);
150 fprintf(fprx, "%c %s\n", X_STDIN, dfile);
151 }
152 /* parse command */
153 ap = inargs;
154 while ((ap = getprm(ap, prm)) != NULL) {
155 DEBUG(4, "prm - %s\n", prm);
156 if (prm[0] == '>' || prm[0] == '<') {
157 redir = prm[0];
158 continue;
159 }
160
161 if (prm[0] == '|' || prm[0] == '^') {
162 if (cmdp != cmd)
163 APPCMD(prm);
164 continue;
165 }
166
167 /* process command or file or option */
168 ret = split(prm, syspart, rest);
169 DEBUG(4, "s - %s, ", syspart);
170 DEBUG(4, "r - %s, ", rest);
171 DEBUG(4, "ret - %d\n", ret);
172 if (syspart[0] == '\0')
173 strcpy(syspart, local);
174
175 if (cmdp == cmd && redir == '\0') {
176 /* command */
177 APPCMD(rest);
178 continue;
179 }
180
181 /* process file or option */
182 DEBUG(4, "file s- %s, ", syspart);
183 DEBUG(4, "local - %s\n", local);
184 /* process file */
185 if (redir == '>') {
186 if (rest[0] != '~')
187 expfile(rest);
188 fprintf(fprx, "%c %s %s\n", X_STDOUT, rest,
189 syspart);
190 redir = '\0';
191 continue;
192 }
193
194 if (ret == NOSYSPART) {
195 /* option */
196 APPCMD(rest);
197 continue;
198 }
199
200 if (strcmp(xsys, local) == SAME
201 && strcmp(xsys, syspart) == SAME) {
202 expfile(rest);
203 if (redir == '<')
204 fprintf(fprx, "%c %s\n", X_STDIN, rest);
205 else
206 APPCMD(rest);
207 redir = '\0';
208 continue;
209 }
210
211 if (strcmp(syspart, local) == SAME) {
212 /* generate send file */
213 expfile(rest);
214 gename(DATAPRE, xsys, 'A', dfile);
215 DEBUG(4, "rest %s\n", rest);
216 if ((chkpth(User, "", rest) || anyread(rest)) != 0) {
217 fprintf(stderr, "permission denied %s\n", rest);
218 cleanup(1);
219 }
220 if (xcp(rest, dfile) != 0) {
221 fprintf(stderr, "can't copy %s to %s\n", rest, dfile);
222 cleanup(1);
223 }
224 GENSEND(fpc, rest, dfile, User, dfile);
225 cflag++;
226 if (redir == '<') {
227 fprintf(fprx, "%c %s\n", X_STDIN, dfile);
228 fprintf(fprx, "%c %s\n", X_RQDFILE, dfile);
229 }
230 else {
231 APPCMD(lastpart(rest));
232 fprintf(fprx, "%c %s %s\n", X_RQDFILE,
233 dfile, lastpart(rest));
234 }
235 redir = '\0';
236 continue;
237 }
238
239 if (strcmp(local, xsys) == SAME) {
240 /* generate local receive */
241 gename(CMDPRE, syspart, 'R', tfile);
242 strcpy(dfile, tfile);
243 dfile[0] = DATAPRE;
244 fp = fopen(tfile, "w");
245 ASSERT(fp != NULL, "CAN'T OPEN %s", tfile);
246 chmod(tfile, 0666);
247 expfile(rest);
248 GENRCV(fp, rest, dfile, User);
249 fclose(fp);
250 rflag++;
251 if (rest[0] != '~')
252 expfile(rest);
253 if (redir == '<') {
254 fprintf(fprx, "%c %s\n", X_RQDFILE, dfile);
255 fprintf(fprx, "%c %s\n", X_STDIN, dfile);
256 }
257 else {
258 fprintf(fprx, "%c %s %s\n", X_RQDFILE, dfile,
259 lastpart(rest));
260 APPCMD(lastpart(rest));
261 }
262
263 redir = '\0';
264 continue;
265 }
266
267 if (strcmp(syspart, xsys) != SAME) {
268 /* generate remote receives */
269 gename(DATAPRE, syspart, 'R', dfile);
270 strcpy(tfile, dfile);
271 tfile[0] = CMDPRE;
272 fpd = fopen(dfile, "w");
273 ASSERT(fpd != NULL, "CAN'T OPEN %s", dfile);
274 chmod(dfile, 0666);
275 gename(DATAPRE, xsys, 'T', t2file);
276 GENRCV(fpd, rest, t2file, User);
277 fclose(fpd);
278 GENSEND(fpc, dfile, tfile, User, dfile);
279 cflag++;
280 if (redir == '<') {
281 fprintf(fprx, "%c %s\n", X_RQDFILE, t2file);
282 fprintf(fprx, "%c %s\n", X_STDIN, t2file);
283 }
284 else {
285 fprintf(fprx, "%c %s %s\n", X_RQDFILE, t2file,
286 lastpart(rest));
287 APPCMD(lastpart(rest));
288 }
289 redir = '\0';
290 continue;
291 }
292
293 /* file on remote system */
294 if (rest[0] != '~')
295 expfile(rest);
296 if (redir == '<')
297 fprintf(fprx, "%c %s\n", X_STDIN, rest);
298 else
299 APPCMD(rest);
300 redir = '\0';
301 continue;
302
303 }
304
305 fprintf(fprx, "%c %s\n", X_CMD, cmd);
306 fclose(fprx);
307
308 strcpy(tfile, rxfile);
309 tfile[0] = XQTPRE;
310 if (strcmp(xsys, local) == SAME) {
311 link(rxfile, tfile);
312 unlink(rxfile);
313 if (startjob)
314 if (rflag)
315 xuucico("");
316 else
317 xuuxqt();
318 }
319 else {
320 GENSEND(fpc, rxfile, tfile, User, rxfile);
321 cflag++;
322 }
323
324 fclose(fpc);
325 if (cflag) {
326 gename(CMDPRE, xsys, 'A', cfile);
327 link(tcfile, cfile);
328 unlink(tcfile);
329 if (startjob)
330 xuucico(xsys);
331 cleanup(0);
332 }
333 else
334 unlink(tcfile);
335}
336
337
338cleanup(code)
339int code;
340{
341 rmlock(NULL);
342 DEBUG(1, "exit code %d\n", code);
343 exit(code);
344}