Example #1
0
int   addlinkID(int n, char *id)
/*
**-------------------------------------------------------------
**  Input:   n = link index
**           id = ID label
**  Output:  returns 0 if ID already in use, 1 if not
**  Purpose: adds a link ID to the Link Hash Table
**--------------------------------------------------------------
*/
{
    if (findlink(id)) return(0);         /* see EPANET.C */
    strncpy(Link[n].ID, id, MAXID);
    HTinsert(Lht, Link[n].ID, n);        /* see HASH.C */
    return(1);
}
Example #2
0
int  newpremise(int logop)
/*
**--------------------------------------------------------------------
**   Adds new premise to current rule.
**   Formats are:
**     IF/AND/OR <object> <id> <variable> <operator> <value>
**     IF/AND/OR  SYSTEM <variable> <operator> <value> (units)
**
**   Calls findmatch() and hour() in INPUT2.C.
**   Calls findnode() and findlink() in EPANET.C.
**---------------------------------------------------------------------
*/
{
   int   i,j,k,m,r,s,v;
   double x;
   struct Premise *p;

   /* Check for correct number of tokens */
   if (Ntokens != 5 && Ntokens != 6) return(201);

   /* Find network object & id if present */
   i = findmatch(Tok[1],Object);
   if (i == r_SYSTEM)
   { 
      j = 0;
      v = findmatch(Tok[2],Varword);
      if (v != r_DEMAND && v != r_TIME && v != r_CLOCKTIME) return(201);
   }
   else
   {
      v = findmatch(Tok[3],Varword);
      if (v < 0) return(201);
      switch (i) 
      {
         case r_NODE:
         case r_JUNC:
         case r_RESERV:
         case r_TANK:   k = r_NODE; break;
         case r_LINK:
         case r_PIPE:
         case r_PUMP:
         case r_VALVE:  k = r_LINK; break;
         default: return(201);
      }
      i = k;
      if (i == r_NODE)
      {
         j = findnode(Tok[2]);
         if (j == 0) return(203);
         switch (v)
         {
            case r_DEMAND:
            case r_HEAD:
            case r_GRADE:
            case r_LEVEL:
            case r_PRESSURE: break;

/*** Updated 9/7/00 ***/
            case r_FILLTIME:
            case r_DRAINTIME: if (j <= Njuncs) return(201); break;

            default: return(201);
         }
      }
      else
      {
         j = findlink(Tok[2]);
         if (j == 0) return(204);
         switch (v)
         {
            case r_FLOW:
            case r_STATUS:
            case r_SETTING: break;
            default: return(201);
         }
      }
   }

   /* Parse relational operator (r) and check for synonyms */
   if (i == r_SYSTEM) m = 3;
   else m = 4;
   k = findmatch(Tok[m],Operator);
   if (k < 0) return(201);
   switch(k)
   {
      case IS:    r = EQ; break;
      case NOT:   r = NE; break;
      case BELOW: r = LT; break;
      case ABOVE: r = GT; break;
      default:    r = k;
   }

   /* Parse for status (s) or numerical value (x) */
   s = 0;
   x = MISSING;
   if (v == r_TIME || v == r_CLOCKTIME)
   {
      if (Ntokens == 6)
         x = hour(Tok[4],Tok[5])*3600.;
      else
         x = hour(Tok[4],"")*3600.;
      if (x < 0.0) return(202);
   }
   else if ((k = findmatch(Tok[Ntokens-1],Value)) > IS_NUMBER) s = k;
   else
   {
      if (!getfloat(Tok[Ntokens-1],&x)) return(202);
      if (v == r_FILLTIME || v == r_DRAINTIME) x = x*3600.0;                   //(2.00.11 - LR)
   }

   
         
   /* Create new premise structure */
   p = (struct Premise *) malloc(sizeof(struct Premise));
   if (p == NULL) return(101);
   p->object = i;
   p->index =  j;
   p->variable = v;
   p->relop = r;
   p->logop = logop;
   p->status   = s;
   p->value    = x;

   /* Add premise to current rule's premise list */
   p->next = NULL;
   if (Plast == NULL) Rule[Nrules].Pchain = p;
   else Plast->next = p;
   Plast = p;
   return(0);
}
Example #3
0
int  newaction()
/*
**----------------------------------------------------------
**   Adds new action to current rule.
**   Format is:
**      THEN/ELSE/AND LINK <id> <variable> IS <value>
**
**   Calls findlink() from EPANET.C.
**   Calls getfloat() and findmatch() from INPUT2.C.
**----------------------------------------------------------
*/
{
   int   j,k,s;
   double x;
   struct Action *a;

   /* Check for correct number of tokens */
   if (Ntokens != 6) return(201);

   /* Check that link exists */
   j = findlink(Tok[2]);
   if (j == 0) return(204);

/***  Updated 9/7/00  ***/
   /* Cannot control a CV */
   if (Link[j].Type == CV) return(207);

   /* Find value for status or setting */
   s = -1;
   x = MISSING;
   if ((k = findmatch(Tok[5],Value)) > IS_NUMBER) s = k;
   else
   {
      if (!getfloat(Tok[5],&x)) return(202);
      if (x < 0.0) return(202);
   }

/*** Updated 9/7/00 ***/
   /* Cannot change setting for a GPV ***/
   if (x != MISSING && Link[j].Type == GPV) return(202);

/*** Updated 3/1/01 ***/
   /* Set status for pipe in case setting was specified */
   if (x != MISSING && Link[j].Type == PIPE)
   {
      if (x == 0.0) s = IS_CLOSED;
      else          s = IS_OPEN;
      x = MISSING;
   }

   /* Create a new action structure */
   a = (struct Action *) malloc(sizeof(struct Action));
   if (a == NULL) return(101);
   a->link = j;
   a->status = s;
   a->setting = x;

   /* Add action to current rule's action list */
   if (RuleState == r_THEN)
   {
     a->next = Rule[Nrules].Tchain;
     Rule[Nrules].Tchain = a;
   }
   else
   {
      a->next = Rule[Nrules].Fchain;
      Rule[Nrules].Fchain = a;
   }
   return(0);
}
Example #4
0
int
main(int argc, char **argv)
{
	int i, maxifno, retval;
	struct ifmibdata ifmd;
	int name[6];
	size_t len;
	int c;
	int dolink = 0;
	void *linkmib;
	size_t linkmiblen;
	printfcn pf;
	char *dname;

	while ((c = getopt(argc, argv, "l")) != -1) {
		switch(c) {
		case 'l':
			dolink = 1;
			break;
		default:
			usage(argv[0]);
		}
	}
	
	retval = 1;

	name[0] = CTL_NET;
	name[1] = PF_LINK;
	name[2] = NETLINK_GENERIC;
	name[3] = IFMIB_SYSTEM;
	name[4] = IFMIB_IFCOUNT;

	len = sizeof maxifno;
	if (sysctl(name, 5, &maxifno, &len, 0, 0) < 0)
		err(EX_OSERR, "sysctl(net.link.generic.system.ifcount)");

	for (i = 1; i <= maxifno; i++) {
		len = sizeof ifmd;
		name[3] = IFMIB_IFDATA;
		name[4] = i;
		name[5] = IFDATA_GENERAL;
		if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0) {
			if (errno == ENOENT)
				continue;

			err(EX_OSERR, "sysctl(net.link.ifdata.%d.general)",
			    i);
		}

		if (!isit(argc - optind, argv + optind, ifmd.ifmd_name))
			continue;

		dname = NULL;
		len = 0;
		name[5] = IFDATA_DRIVERNAME;
		if (sysctl(name, 6, NULL, &len, 0, 0) < 0) {
			warn("sysctl(net.link.ifdata.%d.drivername)", i);
		} else {
			if ((dname = malloc(len)) == NULL)
				err(EX_OSERR, NULL);
			if (sysctl(name, 6, dname, &len, 0, 0) < 0) {
				warn("sysctl(net.link.ifdata.%d.drivername)",
				    i);
				free(dname);
				dname = NULL;
			}
		}
		printit(&ifmd, dname);
		free(dname);
		if (dolink && (pf = findlink(ifmd.ifmd_data.ifi_type))) {
			name[5] = IFDATA_LINKSPECIFIC;
			if (sysctl(name, 6, 0, &linkmiblen, 0, 0) < 0)
				err(EX_OSERR, 
				    "sysctl(net.link.ifdata.%d.linkspec) size",
				    i);
			linkmib = malloc(linkmiblen);
			if (!linkmib)
				err(EX_OSERR, "malloc(%lu)", 
				    (u_long)linkmiblen);
			if (sysctl(name, 6, linkmib, &linkmiblen, 0, 0) < 0)
				err(EX_OSERR, 
				    "sysctl(net.link.ifdata.%d.linkspec)",
				    i);
			pf(linkmib, linkmiblen);
			free(linkmib);
		}
		retval = 0;
	}

	return retval;
}