home *** CD-ROM | disk | FTP | other *** search
- /*
- * UUXQT.C by William Loftus
- * Copyright 1988 by William Loftus. All rights reserved.
- * Changes Copyright 1990,1991 by Matthew Dillon, All Rights Reserved
- *
- * Hierarchical Spool Conversion 1 Dec 93,
- * Mike Bruins bruins@hal9000.apana.org.au
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <log.h>
- #include <libraries/dos.h>
- #include <errno.h>
- #include <clib/dos_protos.h>
- #include "version.h"
- #include "protos.h"
- #include <owndevunit.h>
-
- IDENT(".12");
-
- struct Library *OwnDevUnitBase;
-
- static char names[MAXFILES*16];
- static char *pointers[MAXFILES];
- static int file_pointer;
- static int LoError;
- static int GloError;
- static char *xfile;
- static char dfile[128];
- static char cmd[2048+1024];
- static char ccmd[256];
- static char ccmd_args[2048];
- static char buf[2048];
- static char path[256];
- static long numFiles = 0;
-
- #define DELIM " \t\n\r"
-
-
- struct FileInfoBlock fib;
-
- char *xstrtok(char *, const char *);
- void myexit(void);
-
- int
- brk(void)
- {
- return(0);
- }
-
- int
- bp_strcmp(s1, s2)
- char **s1;
- char **s2;
- {
- return(strcmp(*s1, *s2));
- }
-
- int
- work_scan(void)
- {
- int count;
-
- file_pointer = 0;
-
- count = getfnl("X.#?",names,sizeof(names),0);
-
- if (count > 0) {
- printf("New files have arrived.\n");
-
- if (strbpl(pointers,MAXFILES,names) != count) {
- printf("Too many execute files\n");
- return(0);
- }
- } else {
- return(0);
- }
- qsort(pointers, count, sizeof(char *), bp_strcmp);
- return(1);
- }
-
- char *
- work_next(void)
- {
- char *ptr;
-
- if (ptr = pointers[file_pointer])
- ++file_pointer;
- return(ptr);
- }
-
- int
- parse(x)
- char *x;
- {
- FILE *fp;
- char *tmp;
-
- fp = fopen(x, "r");
- if (fp == NULL) {
- ulog(-1, "Can't open %s", x);
- if (stdout && isatty (fileno(stdout)))
- printf("Can't open file %s\n",x);
- /*chdir(path);*/
- return(0);
- }
- while (fgets(buf, sizeof(buf), fp)) {
- if (strncmp(buf, "F", 1) == 0)
- strcpy(dfile, xstrtok(&buf[1],DELIM));
- else if (strncmp(buf, "C", 1) == 0) { /* Sept 90 */
- strcpy(ccmd, xstrtok(&buf[1],DELIM));
- strcpy(ccmd_args, xstrtok(NULL, DELIM));
- while ((tmp = (char *)strtok(NULL, DELIM)) != NULL) {
- strcat(ccmd_args, " ");
- strcat(ccmd_args, tmp);
- }
- }
- }
-
- /*
- * If unable to find file it could be munge-cased
- */
-
- {
- long lock;
-
- if (lock = Lock(dfile, SHARED_LOCK))
- UnLock(lock);
- else
- mungecase_filename(dfile, dfile);
- }
-
- if (strncmp(ccmd, "rmail", 5) == 0) {
- sprintf(cmd,"%s <%s %s", GetConfigProgram(RMAIL), dfile, ccmd_args);
- } else if (strncmp(ccmd, "cunbatch", 8) == 0) {
- sprintf(cmd,"%s <%s %s", GetConfigProgram(CUNBATCH), dfile, ccmd_args);
- } else if (strncmp(ccmd, "rnews", 5) == 0) {
- sprintf(cmd,"%s <%s", GetConfigProgram(RNEWS), dfile);
- } else if (strncmp(ccmd, "rsmtp", 5) == 0) {
- sprintf(cmd,"%s <%s", GetConfigProgram(RSMTP), dfile);
- } else if (strncmp(ccmd, "rcsmtp", 5) == 0) {
- sprintf(cmd,"%s <%s", GetConfigProgram(RCSMTP), dfile);
- } else {
- printf("Unknown command request %s - Operation Aborted -\n", ccmd);
- LoError = 1;
- }
- fclose(fp);
- return(1);
- }
-
- int uuxqt_system(char *sys){ /* hal9000.apana.org.au!bruins */
- /* process all files for a given system */
- printf("%s...\n",sys);
- chdir(MakeConfigPath(UUSPOOL,sys));
- while (GloError == 0 && work_scan()) {
- while ((xfile = work_next()) != NULL) {
- ++numFiles;
- LockFile(xfile);
- if (parse(xfile)) {
- int syserr;
-
- syserr = system(cmd);
-
- if (syserr) {
- char *p = strdup(xfile);
- char *q;
- short i;
-
- ulog(-1, "Execute Error: rc=%d cmd=%s", syserr, cmd);
- LoError = 1;
- if (p) {
- for (i = strlen(p); i >= 0; --i) {
- if (p[i] == '/' || p[i] ==':')
- break;
- }
- ++i;
-
- if (p[i] == 'X') {
- p[i] = 'E';
- if (rename(xfile, p) == 0)
- ulog(-1, "Renamed %s to %s", xfile, p);
- else
- ulog(-1, "Unable to rename %s to %s (Errno=%d, IoErr()=%ld)", xfile, p, errno, IoErr());
- }
- free(p);
- } else {
- ulog(-1, "Ran out of memory trying to rename %s", xfile);
- }
- }
- if (syserr == 0 && LoError != 1) {
- remove(xfile);
- remove(dfile);
- }
- }
- UnLockFile(xfile);
- if (LoError)
- GloError = 1;
- LoError = 0;
- }
- }
- }
-
- int
- main(ac, av)
- int ac;
- char *av[];
- {
-
- LogProgram = "UUXQT";
- char sys[128];
- int i;
- BPTR lock;
-
- if (ac == 0) /* run from workbench */
- exit(10);
-
- onbreak(brk);
- atexit(myexit);
-
- if ((OwnDevUnitBase = OpenLibrary(ODU_NAME, 0)) == NULL) {
- printf("Unable to open %s\n", ODU_NAME);
- exit(20);
- }
-
- getcwd(path,sizeof(path));
-
- if (FileIsLocked("UUXQT")) {
- ulog(-1, "UUXQT already running!");
- chdir(path);
- return(0);
- }
-
- LockFile("UUXQT");
-
- if (ac > 1) {
- for(i=1;strcpy(sys,av[i]) && i<ac;i++)
- if(is_in_L_sys_file(sys))
- uuxqt_system(sys);
- }
- else{
- lock = Lock(GetConfigDir(UUSPOOL), ACCESS_READ); /* read the dir */
- if(lock){
- if(Examine(lock,&fib)) /* Read the first dir entry */
- do
- if(fib.fib_DirEntryType>0){
- strcpy(sys,fib.fib_FileName);
- if(is_in_L_sys_file(sys))
- uuxqt_system(sys);
- }
- while (ExNext(lock, &fib)); /* Get the next entry 'til error */
- UnLock(lock);
- }
- else
- ulog(-1, "uuxqt_d: couldn't lock uuspool!");
- strcpy(sys,FindConfig(NODENAME));
- uuxqt_system(sys);
- }
- UnLockFile("UUXQT");
- chdir(path);
- ulog(-1, "Processed %d files", numFiles);
- return(0);
- }
-
- char *
- xstrtok(s, toks)
- char *s;
- const char *toks;
- {
- char *r;
-
- if (r = strtok(s, toks))
- return(r);
- return("");
- }
-
- void
- myexit()
- {
- UnLockFiles();
-
- if (OwnDevUnitBase) {
- CloseLibrary(OwnDevUnitBase);
- OwnDevUnitBase = NULL;
- }
- }
-
-