home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume12 / pathalias9 / part01 / main.c < prev    next >
C/C++ Source or Header  |  1987-10-08  |  4KB  |  161 lines

  1. /* pathalias -- by steve bellovin, as told to peter honeyman */
  2. #ifndef lint
  3. static char    *sccsid = "@(#)main.c    9.1 87/10/04";
  4. #endif
  5.  
  6. #define MAIN    /* for sccsid in header files */
  7.  
  8. #include "def.h"
  9.  
  10. /* exports */
  11. extern void die();
  12. char *Cfile;    /* current input file */
  13. char *Graphout;    /* file for dumping edges (-g option) */
  14. char *Linkout;    /* file for dumping shortest path tree */
  15. char **Argv;    /* external copy of argv (for input files) */
  16. node *Home;    /* node for local host */
  17. int Cflag;    /* print costs (-c option) */
  18. int Dflag;    /* penalize routes beyond domains (-D option) */
  19. int Iflag;    /* ignore case (-i option) */
  20. int Tflag;    /* trace links (-t option) */
  21. int Vflag;    /* verbose (-v option) */
  22. int Fflag;    /* print cost of first hop */
  23. int Lineno = 1;    /* line number within current input file */
  24. int Argc;    /* external copy of argc (for input files) */
  25.  
  26. /* imports */
  27. extern long allocation();
  28. extern void wasted(), mapit(), penalize(), hashanalyze(), deadlink();
  29. extern char *local();
  30. extern node *addnode();
  31. extern char *optarg;
  32. extern int optind;
  33. extern long Lcount, Ncount;
  34.  
  35. #define USAGE "usage: %s [-vciDf] [-l localname] [-d deadlink] [-t tracelink] [-g edgeout] [-s treeout] [-a avoid] [files ...]\n"
  36.  
  37. main(argc, argv) 
  38.     register int argc; 
  39.     register char **argv;
  40. {    char *locname = 0, buf[32], *bang;
  41.     register int c;
  42.     int errflg = 0;
  43.  
  44.     setbuf(stderr, (char *) 0);
  45.     (void) allocation();    /* initialize data space monitoring */
  46.     Cfile = "[deadlinks]";    /* for tracing dead links */
  47.     Argv = argv;
  48.     Argc = argc;
  49.  
  50.     while ((c = getopt(argc, argv, "a:cd:Dfg:il:s:t:v")) != EOF)
  51.         switch(c) {
  52.         case 'a':    /* adjust cost out of arg */
  53.             penalize(optarg, DEFPENALTY);
  54.             break;
  55.         case 'c':    /* print cost info */
  56.             Cflag++;
  57.             break;
  58.         case 'd':    /* dead host or link */
  59.             if ((bang = index(optarg, '!')) != 0) {
  60.                 *bang++ = 0;
  61.                 deadlink(addnode(optarg), addnode(bang));
  62.             } else
  63.                 deadlink(addnode(optarg), (node *) 0);
  64.             break;
  65.         case 'D':    /* penalize routes beyond domains */
  66.             Dflag++;
  67.             break;
  68.         case 'f':    /* print cost of first hop */
  69.             Cflag++;
  70.             Fflag++;
  71.             break;
  72.         case 'g':    /* graph output file */
  73.             Graphout = optarg;
  74.             break;
  75.         case 'i':    /* ignore case */
  76.             Iflag++;
  77.             break;
  78.         case 'l':    /* local name */
  79.             locname = optarg;
  80.             break;
  81.         case 's':    /* show shortest path tree */
  82.             Linkout = optarg;
  83.             break;
  84.         case 't':    /* trace this link */
  85.             if (tracelink(optarg) < 0) {
  86.                 fprintf(stderr, "%s: can trace only %d links\n", Argv[0], NTRACE);
  87.                 exit(1);
  88.             }
  89.             Tflag = 1;
  90.             break;
  91.         case 'v':    /* verbose stderr, mixed blessing */
  92.             Vflag++;
  93.             if (Vflag == 1) {
  94.                 /* v8 pi snarf, benign EBADF elsewhere */
  95.                 sprintf(buf, "/proc/%05d\n", getpid());
  96.                 write(3, buf, strlen(buf));
  97.             }
  98.             break;
  99.         default:
  100.             errflg++;
  101.         }
  102.  
  103.     if (errflg) {
  104.         fprintf(stderr, USAGE, Argv[0]);
  105.         exit(1);
  106.     }
  107.     argv += optind;        /* kludge for yywrap() */
  108.  
  109.     if (*argv)
  110.         freopen("/dev/null", "r", stdin);
  111.     else
  112.         Cfile = "[stdin]";
  113.  
  114.     if (!locname) 
  115.         locname = local();
  116.     if (*locname == 0) {
  117.         locname = "lostinspace";
  118.         fprintf(stderr, "%s: using \"%s\" for local name\n",
  119.                 Argv[0], locname);
  120.     }
  121.  
  122.     Home = addnode(locname);    /* add home node */
  123.     Home->n_cost = 0;        /* doesn't cost to get here */
  124.  
  125.     yyparse();            /* read in link info */
  126.  
  127.     if (Vflag > 1)
  128.         hashanalyze();
  129.     vprintf(stderr, "%d vertices, %d edges\n", Ncount, Lcount);
  130.     vprintf(stderr, "allocation is %ldk after parsing\n", allocation());
  131.  
  132.     Cfile = "[backlinks]";    /* for tracing back links */
  133.     Lineno = 0;
  134.  
  135.     /* compute shortest path tree */
  136.     mapit();
  137.     vprintf(stderr, "allocation is %ldk after mapping\n", allocation());
  138.  
  139.     /* traverse tree and print paths */
  140.     printit();
  141.     vprintf(stderr, "allocation is %ldk after printing\n", allocation());
  142.  
  143.     wasted();    /* how much was wasted in memory allocation? */
  144.  
  145.     return 0;
  146. }
  147.  
  148. void
  149. die(s)
  150.     char *s;
  151. {
  152.     fprintf(stderr, "%s: %s; notify the authorities\n", Argv[0], s);
  153. #ifdef DEBUG
  154.         fflush(stdout);
  155.         fflush(stderr);
  156.         abort();
  157. #else
  158.         exit(-1);
  159. #endif
  160. }
  161.