home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / uucp / uux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-01-11  |  6.8 KB  |  328 lines

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