home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / uucp_mods.lha.30.12.93 / uucp_mods / uucico / uuxqt.c < prev   
Encoding:
C/C++ Source or Header  |  1993-12-02  |  5.7 KB  |  289 lines

  1. /*
  2.  *  UUXQT.C by William Loftus
  3.  *  Copyright 1988 by William Loftus.    All rights reserved.
  4.  *  Changes Copyright 1990,1991 by Matthew Dillon, All Rights Reserved
  5.  *
  6.  *  Hierarchical Spool Conversion 1 Dec 93,
  7.  *                             Mike Bruins bruins@hal9000.apana.org.au
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <log.h>
  14. #include <libraries/dos.h>
  15. #include <errno.h>
  16. #include <clib/dos_protos.h>
  17. #include "version.h"
  18. #include "protos.h"
  19. #include <owndevunit.h>
  20.  
  21. IDENT(".12");
  22.  
  23. struct Library *OwnDevUnitBase;
  24.  
  25. static char names[MAXFILES*16];
  26. static char *pointers[MAXFILES];
  27. static int file_pointer;
  28. static int LoError;
  29. static int GloError;
  30. static char *xfile;
  31. static char dfile[128];
  32. static char cmd[2048+1024];
  33. static char ccmd[256];
  34. static char ccmd_args[2048];
  35. static char buf[2048];
  36. static char path[256];
  37. static long numFiles = 0;
  38.  
  39. #define DELIM " \t\n\r"
  40.  
  41.  
  42. struct FileInfoBlock fib;
  43.  
  44. char *xstrtok(char *, const char *);
  45. void myexit(void);
  46.  
  47. int
  48. brk(void)
  49. {
  50.     return(0);
  51. }
  52.  
  53. int
  54. bp_strcmp(s1, s2)
  55. char **s1;
  56. char **s2;
  57. {
  58.     return(strcmp(*s1, *s2));
  59. }
  60.  
  61. int
  62. work_scan(void)
  63. {
  64.     int count;
  65.  
  66.     file_pointer = 0;
  67.  
  68.     count = getfnl("X.#?",names,sizeof(names),0);
  69.  
  70.     if (count > 0) {
  71.     printf("New files have arrived.\n");
  72.  
  73.     if (strbpl(pointers,MAXFILES,names) != count) {
  74.         printf("Too many execute files\n");
  75.         return(0);
  76.     }
  77.     } else {
  78.     return(0);
  79.     }
  80.     qsort(pointers, count, sizeof(char *), bp_strcmp);
  81.     return(1);
  82. }
  83.  
  84. char *
  85. work_next(void)
  86. {
  87.     char *ptr;
  88.  
  89.     if (ptr = pointers[file_pointer])
  90.     ++file_pointer;
  91.     return(ptr);
  92. }
  93.  
  94. int
  95. parse(x)
  96. char *x;
  97. {
  98.     FILE *fp;
  99.     char *tmp;
  100.  
  101.     fp = fopen(x, "r");
  102.     if (fp == NULL) {
  103.     ulog(-1, "Can't open %s", x);
  104.     if (stdout && isatty (fileno(stdout)))
  105.         printf("Can't open file %s\n",x);
  106.     /*chdir(path);*/
  107.     return(0);
  108.     }
  109.     while (fgets(buf, sizeof(buf), fp)) {
  110.     if (strncmp(buf, "F", 1) == 0)
  111.         strcpy(dfile, xstrtok(&buf[1],DELIM));
  112.     else if (strncmp(buf, "C", 1) == 0) {       /*  Sept 90 */
  113.         strcpy(ccmd, xstrtok(&buf[1],DELIM));
  114.         strcpy(ccmd_args, xstrtok(NULL, DELIM));
  115.         while ((tmp = (char *)strtok(NULL, DELIM)) != NULL) {
  116.         strcat(ccmd_args, " ");
  117.         strcat(ccmd_args, tmp);
  118.         }
  119.     }
  120.     }
  121.  
  122.     /*
  123.      *    If unable to find file it could be munge-cased
  124.      */
  125.  
  126.     {
  127.     long lock;
  128.  
  129.     if (lock = Lock(dfile, SHARED_LOCK))
  130.         UnLock(lock);
  131.     else
  132.         mungecase_filename(dfile, dfile);
  133.     }
  134.  
  135.     if (strncmp(ccmd, "rmail", 5) == 0) {
  136.     sprintf(cmd,"%s <%s %s", GetConfigProgram(RMAIL), dfile, ccmd_args);
  137.     } else if (strncmp(ccmd, "cunbatch", 8) == 0) {
  138.     sprintf(cmd,"%s <%s %s", GetConfigProgram(CUNBATCH), dfile, ccmd_args);
  139.     } else if (strncmp(ccmd, "rnews", 5) == 0) {
  140.     sprintf(cmd,"%s <%s", GetConfigProgram(RNEWS), dfile);
  141.     } else if (strncmp(ccmd, "rsmtp", 5) == 0) {
  142.     sprintf(cmd,"%s <%s", GetConfigProgram(RSMTP), dfile);
  143.     } else if (strncmp(ccmd, "rcsmtp", 5) == 0) {
  144.     sprintf(cmd,"%s <%s", GetConfigProgram(RCSMTP), dfile);
  145.     } else {
  146.     printf("Unknown command request %s  - Operation Aborted -\n", ccmd);
  147.     LoError = 1;
  148.     }
  149.     fclose(fp);
  150.     return(1);
  151. }
  152.  
  153. int uuxqt_system(char *sys){               /* hal9000.apana.org.au!bruins */
  154. /* process all files for a given system */
  155.     printf("%s...\n",sys);
  156.     chdir(MakeConfigPath(UUSPOOL,sys));
  157.     while (GloError == 0 && work_scan()) {
  158.     while ((xfile = work_next()) != NULL) {
  159.         ++numFiles;
  160.         LockFile(xfile);
  161.         if (parse(xfile)) {
  162.         int syserr;
  163.  
  164.         syserr = system(cmd);
  165.  
  166.         if (syserr) {
  167.             char *p = strdup(xfile);
  168.             char *q;
  169.             short i;
  170.  
  171.             ulog(-1, "Execute Error: rc=%d cmd=%s", syserr, cmd);
  172.             LoError = 1;
  173.             if (p) {
  174.             for (i = strlen(p); i >= 0; --i) {
  175.                 if (p[i] == '/' || p[i] ==':')
  176.                 break;
  177.             }
  178.             ++i;
  179.  
  180.             if (p[i] == 'X') {
  181.                 p[i] = 'E';
  182.                 if (rename(xfile, p) == 0)
  183.                 ulog(-1, "Renamed %s to %s", xfile, p);
  184.                 else
  185.                 ulog(-1, "Unable to rename %s to %s (Errno=%d, IoErr()=%ld)", xfile, p, errno, IoErr());
  186.             }
  187.             free(p);
  188.             } else {
  189.             ulog(-1, "Ran out of memory trying to rename %s", xfile);
  190.             }
  191.         }
  192.         if (syserr == 0 && LoError != 1) {
  193.             remove(xfile);
  194.             remove(dfile);
  195.         }
  196.         }
  197.         UnLockFile(xfile);
  198.         if (LoError)
  199.         GloError = 1;
  200.         LoError = 0;
  201.     }
  202.     }
  203. }
  204.  
  205. int
  206. main(ac, av)
  207. int ac;
  208. char *av[];
  209. {
  210.  
  211.     LogProgram = "UUXQT";
  212.     char sys[128];
  213.     int i;
  214.     BPTR lock;
  215.  
  216.     if (ac == 0)      /* run from workbench */
  217.     exit(10);
  218.  
  219.     onbreak(brk);
  220.     atexit(myexit);
  221.  
  222.     if ((OwnDevUnitBase = OpenLibrary(ODU_NAME, 0)) == NULL) {
  223.     printf("Unable to open %s\n", ODU_NAME);
  224.     exit(20);
  225.     }
  226.  
  227.     getcwd(path,sizeof(path));
  228.  
  229.     if (FileIsLocked("UUXQT")) {
  230.     ulog(-1, "UUXQT already running!");
  231.     chdir(path);
  232.     return(0);
  233.     }
  234.  
  235.     LockFile("UUXQT");
  236.  
  237.     if (ac > 1) {
  238.       for(i=1;strcpy(sys,av[i]) && i<ac;i++)
  239.          if(is_in_L_sys_file(sys)) 
  240.             uuxqt_system(sys);
  241.     }
  242.     else{
  243.        lock = Lock(GetConfigDir(UUSPOOL), ACCESS_READ); /* read the dir */
  244.        if(lock){
  245.          if(Examine(lock,&fib)) /* Read the first dir entry */
  246.            do
  247.              if(fib.fib_DirEntryType>0){
  248.                strcpy(sys,fib.fib_FileName);
  249.                if(is_in_L_sys_file(sys)) 
  250.                     uuxqt_system(sys);
  251.              }
  252.              while (ExNext(lock, &fib));  /*  Get the next entry 'til error */
  253.          UnLock(lock);
  254.        }
  255.        else
  256.          ulog(-1, "uuxqt_d: couldn't lock uuspool!");
  257.        strcpy(sys,FindConfig(NODENAME));
  258.        uuxqt_system(sys);
  259.     }
  260.     UnLockFile("UUXQT");
  261.     chdir(path);
  262.     ulog(-1, "Processed %d files", numFiles);
  263.     return(0);
  264. }
  265.  
  266. char *
  267. xstrtok(s, toks)
  268. char *s;
  269. const char *toks;
  270. {
  271.     char *r;
  272.  
  273.     if (r = strtok(s, toks))
  274.     return(r);
  275.     return("");
  276. }
  277.  
  278. void
  279. myexit()
  280. {
  281.     UnLockFiles();
  282.  
  283.     if (OwnDevUnitBase) {
  284.     CloseLibrary(OwnDevUnitBase);
  285.     OwnDevUnitBase = NULL;
  286.     }
  287. }
  288.  
  289.