home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / pathalias10 / part03 / main.c < prev    next >
C/C++ Source or Header  |  1990-06-07  |  4KB  |  159 lines

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