home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / pathalias2 / part2 / addlink.c next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  3.3 KB  |  188 lines

  1. /* pathalias -- by steve bellovin, as told to peter honeyman */
  2. #ifndef lint
  3. static char    *sccsid = "@(#)addlink.c    8.1 (down!honey) 86/01/19";
  4. #endif lint
  5.  
  6. #include "def.h"
  7.  
  8. static link    *Trace[NTRACE];
  9. static int    Tracecount;
  10.  
  11. link    *
  12. addlink(from, to, cost, netchar, netdir)
  13. node    *from;
  14. register node    *to;
  15. Cost    cost;
  16. char    netchar;
  17. char    netdir;
  18. {
  19.     register link    *l, *prev = 0;
  20.  
  21.     if (Tflag)
  22.         ltrace(from, to, cost, netchar, netdir);
  23.     /* maintain uniqueness for dead links (only) */
  24.     for (l = from->n_link; l && l->l_flag & LDEAD; l = l->l_next) {
  25.         if (to == l->l_to) {
  26.             /* what the hell, use cheaper cost */
  27.             if (cost < l->l_cost) {
  28.                 l->l_cost = cost;
  29.                 netbits(l, netchar, netdir);
  30.             }
  31.             return(l);
  32.         }
  33.         prev = l;
  34.     }
  35.  
  36.     /* allocate and link in the new link struct */
  37.     l = newlink();
  38.     if (cost != INF)    /* ignore back links */
  39.         Lcount++;
  40.     if (prev) {
  41.         l->l_next = prev->l_next;
  42.         prev->l_next = l;
  43.     } else {
  44.         l->l_next = from->n_link;
  45.         from->n_link = l;
  46.     }
  47.  
  48.     l->l_to = to;
  49.     l->l_cost = cost;
  50.     if (netchar == 0) {
  51.         netchar = DEFNET;
  52.         netdir = DEFDIR;
  53.     }
  54.     netbits(l, netchar, netdir);
  55.  
  56.     return(l);
  57. }
  58.  
  59. link    *
  60. addgateway(from, to, cost, netchar, netdir)
  61. node    *from;
  62. node    *to;
  63. Cost    cost;
  64. char    netchar;
  65. char    netdir;
  66. {
  67.     register link    *l;
  68.  
  69.     l = addlink(from, to, cost, netchar, netdir);
  70.     l->l_flag |= LGATEWAY;
  71.     return(l);
  72. }
  73.  
  74. deadlink(s) 
  75. char    *s;
  76. {
  77.     char    *t, c;
  78.     link    *l;
  79.  
  80.     t = index(s, '!');
  81.     if (t) {
  82.         c = *t;
  83.         *t = 0;
  84.         l = addlink(addnode(s), addnode(t + 1), INF / 2, c, DEFDIR);
  85.         l->l_flag |= LDEAD;
  86.     } else 
  87.         addnode(s)->n_flag |= NDEAD;
  88. }
  89.  
  90. netbits(l, netchar, netdir)
  91. link    *l;
  92. char    netchar, netdir;
  93. {
  94.     char    *nptr;
  95.  
  96.     if ((nptr = index(Netchars, netchar)) == 0) {
  97.         fprintf(stderr, "%s: unknown network operator: %c\n",
  98.                                 ProgName, netchar);
  99.         badmagic(1);
  100.     }
  101.     l->l_flag &= ~(LNETCHARS|LDIR);
  102.     l->l_flag |= (nptr - Netchars) | dirbits(netdir);
  103. }
  104.  
  105. tracelink(arg) 
  106. char    *arg;
  107. {
  108.     char    *bang;
  109.     link    *l;
  110.  
  111.     if (Tracecount >= NTRACE)
  112.         return(-1);
  113.     l = newlink();
  114.     bang = index(arg, '!');
  115.     if (bang) {
  116.         *bang = 0;
  117.         l->l_to = addnode(bang+1);
  118.     } else 
  119.         l->l_to = 0;
  120.  
  121.     l->l_from = (link *) addnode(arg);
  122.     Trace[Tracecount++] = l;
  123.     return(0);
  124. }
  125.  
  126. STATIC
  127. ltrace(from, to, cost, netchar, netdir)
  128. node    *from, *to;
  129. Cost    cost;
  130. char    netchar;
  131. char    netdir;
  132. {
  133.     link    *l;
  134.     int    i;
  135.  
  136.     for (i = 0; i < Tracecount; i++) {
  137.         l = Trace[i];
  138.         /* overkill -- you asked for it! */
  139.         if ((l->l_to == 0
  140.           && (from == (node *) l->l_from || to == (node *) l->l_from))
  141.          || (from == (node *) l->l_from && to == l->l_to)
  142.          || (to == (node *) l->l_from && from == l->l_to)) {
  143.             ltrprint(from, to, cost, netchar, netdir);
  144.             return;
  145.         }
  146.     }
  147. }
  148.  
  149. /* print a trace item */
  150. STATIC
  151. ltrprint(from, to, cost, netchar, netdir)
  152. node    *from, *to;
  153. Cost    cost;
  154. char    netchar;
  155. char    netdir;
  156. {
  157.     char    buf[256], *bptr = buf;
  158.  
  159.     strcpy(bptr, from->n_name);
  160.     bptr += strlen(bptr);
  161.     *bptr++ = ' ';
  162.     if (netdir == LRIGHT)            /* @% */
  163.         *bptr++ = netchar;
  164.     strcpy(bptr, to->n_name);
  165.     bptr += strlen(bptr);
  166.     if (netdir == LLEFT)            /* !: */
  167.         *bptr++ = netchar;
  168.     sprintf(bptr, "(%ld)", cost);
  169.     yyerror(buf);
  170. }
  171.  
  172. atrace(n1, n2)
  173. node    *n1, *n2;
  174. {
  175.     link    *l;
  176.     int    i;
  177.     char    buf[256];
  178.  
  179.     for (i = 0; i < Tracecount; i++) {
  180.         l = Trace[i];
  181.         if (l->l_to == 0 && ((node *) l->l_from == n1 || (node *) l->l_from == n2)) {
  182.             sprintf(buf, "%s = %s", n1->n_name, n2->n_name);
  183.             yyerror(buf);
  184.             return;
  185.         }
  186.     }
  187. }
  188.