home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / queuer / part02 / readconf.c < prev    next >
C/C++ Source or Header  |  1990-06-07  |  5KB  |  192 lines

  1. /* Copyright 1990  The President and Fellows of Harvard University
  2.  
  3. Permission to use, copy, modify, and distribute this program for any
  4. purpose and without fee is hereby granted, provided that this
  5. copyright and permission notice appear on all copies and supporting
  6. documentation, the name of Harvard University not be used in advertising
  7. or publicity pertaining to distribution of the program, or to results
  8. derived from its use, without specific prior written permission, and notice
  9. be given in supporting documentation that copying and distribution is by
  10. permission of Harvard University.  Harvard University makes no
  11. representations about the suitability of this software for any purpose.
  12. It is provided "as is" without express or implied warranty.    */
  13.  
  14.  
  15. /* readconf.c - Dan Lanciani '85 */
  16.  
  17. #include <stdio.h>
  18. #include "queue.h"
  19.  
  20. char prog[BUFSIZ], host[BUFSIZ], qm[BUFSIZ], queue[BUFSIZ];
  21. char *hosts[MAXHCNT];
  22. int maxcpu, minnice, maxrun, minload, maxload, mode, priv, efs, restart;
  23. int hcnt, maxtime, nolocal, maxperu;
  24. struct optent *copyin = NULL;
  25. struct optent *copyout = NULL;
  26. struct optent *option = NULL;
  27.  
  28. readconf(p)
  29. char *p;
  30. {
  31.     register FILE *n, *m;
  32.     char name[BUFSIZ], buf[BUFSIZ], action[BUFSIZ], arg[BUFSIZ];
  33.     register int i;
  34.     register struct optent *op;
  35.  
  36.     sprintf(name, "%s/%s", LIBDIR, p);
  37.     if(!(n = fopen(name, "r")))
  38.         return(-1);
  39.     if(fscanf(n, "%s %s %s\n", buf, name, prog) != 3) {
  40.         fclose(n);
  41.         return(-2);
  42.     }
  43.     if(strcmp(buf, "program") || strcmp(name, p)) {
  44.         fclose(n);
  45.         return(-3);
  46.     }
  47.     gethostname(host, BUFSIZ);
  48.     qm[0] = '\0';
  49.     strcpy(queue, "test");
  50.     maxcpu = 0;
  51.     minnice = 0;
  52.     maxrun = 1;
  53.     minload = maxload = 0;
  54.     mode = QM_BACKGROUND;
  55.     priv = 0;
  56.     efs = 0;
  57.     restart = 0;
  58.     hcnt = 0;
  59.     maxtime = 0;
  60.     nolocal = 0;
  61.     maxperu = 0;
  62.     if(p = getenv("QMAXCPU"))
  63.         maxcpu = atoi(p);
  64.     if(p = getenv("QMINNICE"))
  65.         minnice = atoi(p);
  66.     if(p = getenv("QMAXTIME"))
  67.         maxtime = atoi(p);
  68.     if(minnice < 0)
  69.         minnice = 0;
  70.     while(fgets(buf, sizeof(buf), n)) {
  71.         buf[strlen(buf)-1] = '\0';
  72.         if(!strncmp(buf, "copyin ", 7)) {
  73.             op = (struct optent *)malloc(sizeof(struct optent));
  74.             op->op_action = OPA_COPYIN;
  75.             op->op_arg = newstring(buf+7);
  76.             op->op_next = copyin;
  77.             copyin = op;
  78.             continue;
  79.         }
  80.         if(!strncmp(buf, "copyout ", 8)) {
  81.             op = (struct optent *)malloc(sizeof(struct optent));
  82.             op->op_action = OPA_COPYOUT;
  83.             op->op_arg = newstring(buf+8);
  84.             op->op_next = copyout;
  85.             copyout = op;
  86.             continue;
  87.         }
  88.         if(!strncmp(buf, "option ", 7)) {
  89.             i = sscanf(buf+7, "%s %s %s", name, action, arg);
  90.             if(!i)
  91.                 continue;
  92.             op = (struct optent *)malloc(sizeof(struct optent));
  93.             op->op_name = newstring(name);
  94.             if(i != 3) {
  95.                 op->op_action = OPA_IGNORE;
  96.                 op->op_arg = "ARG0";
  97.             }
  98.             else {
  99.                 if(!strcmp(action, "copyin"))
  100.                     op->op_action = OPA_COPYIN;
  101.                 else if(!strcmp(action, "copyout"))
  102.                     op->op_action = OPA_COPYOUT;
  103.                 else op->op_action = OPA_IGNORE;
  104.                 op->op_arg = newstring(arg);
  105.             }
  106.             op->op_next = option;
  107.             option = op;
  108.             continue;
  109.         }
  110.         if(!strncmp(buf, "host ", 5)) {
  111.             strcpy(host, buf+5);
  112.             if(host[0] == '/' && (m = fopen(host, "r"))) {
  113.                 while(fgets(host, sizeof(host), m)) {
  114.                     host[strlen(host)-1] = '\0';
  115.                     if(hcnt < MAXHCNT)
  116.                         hosts[hcnt++] = newstring(host);
  117.                 }
  118.                 fclose(m);
  119.             }
  120.             else
  121.                 if(hcnt < MAXHCNT)
  122.                     hosts[hcnt++] = newstring(host);
  123.             continue;
  124.         }
  125.         if(!strncmp(buf, "qmaster ", 8)) {
  126.             strcpy(qm, buf+8);
  127.             continue;
  128.         }
  129.         if(!strncmp(buf, "maxcpu ", 7)) {
  130.             if((i = atoi(buf+7)) < maxcpu || !maxcpu)
  131.                 maxcpu = i;
  132.             continue;
  133.         }
  134.         if(!strncmp(buf, "minnice ", 8)) {
  135.             if((i = atoi(buf+8)) > minnice)
  136.                 minnice = i;
  137.             continue;
  138.         }
  139.         if(!strncmp(buf, "maxtime ", 8)) {
  140.             if((i = atoi(buf+8)) < maxtime || !maxtime)
  141.                 maxtime = i;
  142.             continue;
  143.         }
  144.         if(!strncmp(buf, "maxrun ", 7)) {
  145.             maxrun = atoi(buf+7);
  146.             continue;
  147.         }
  148.         if(!strncmp(buf, "load ", 5)) {
  149.             sscanf(buf+5, "%d %d", &minload, &maxload);
  150.             continue;
  151.         }
  152.         if(!strncmp(buf, "mode ", 5)) {
  153.             if(!strcmp(buf+5, "batch"))
  154.                 mode = QM_BATCH;
  155.             else if(!strcmp(buf+5, "background"))
  156.                 mode = QM_BACKGROUND;
  157.             else if(!strcmp(buf+5, "interactive"))
  158.                 mode = QM_INTERACTIVE;
  159.             continue;
  160.         }
  161.         if(!strncmp(buf, "queue ", 6)) {
  162.             strcpy(queue, buf+6);
  163.             continue;
  164.         }
  165.         if(!strcmp(buf, "magic")) {
  166.             priv = 1;
  167.             continue;
  168.         }
  169.         if(!strcmp(buf, "efs")) {
  170.             efs = 1;
  171.             continue;
  172.         }
  173.         if(!strcmp(buf, "restart")) {
  174.             restart = 1;
  175.             continue;
  176.         }
  177.         if(!strcmp(buf, "nolocal")) {
  178.             nolocal = 1;
  179.             continue;
  180.         }
  181.         if(!strncmp(buf, "maxperu ", 8)) {
  182.             maxperu = atoi(buf+8);
  183.             continue;
  184.         }
  185.     }
  186.     fclose(n);
  187.     if(!hcnt)
  188.         hosts[hcnt++] = newstring(host);
  189.     hosts[hcnt] = 0;
  190.     return(0);
  191. }
  192.