tred.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @file
  3. * @brief
  4. * [transitive reduction](https://en.wikipedia.org/wiki/Transitive_reduction)
  5. * filter for directed graphs
  6. */
  7. /*************************************************************************
  8. * Copyright (c) 2011 AT&T Intellectual Property
  9. * All rights reserved. This program and the accompanying materials
  10. * are made available under the terms of the Eclipse Public License v1.0
  11. * which accompanies this distribution, and is available at
  12. * https://www.eclipse.org/legal/epl-v10.html
  13. *************************************************************************/
  14. /*
  15. * Written by Stephen North
  16. * Updated by Emden Gansner
  17. */
  18. /*
  19. * reads a sequence of graphs on stdin, and writes their
  20. * transitive reduction
  21. */
  22. #include <cgraph/cgraph.h>
  23. #include <cgraph/ingraphs.h>
  24. #include <stdbool.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <getopt.h>
  28. #include <util/exit.h>
  29. #include <util/unreachable.h>
  30. static char **Files;
  31. static char *CmdName;
  32. typedef graphviz_tred_options_t opts_t;
  33. static char *useString = "Usage: %s [-vr?] <files>\n\
  34. -o FILE - redirect output (default to stdout)\n\
  35. -v - verbose (to stderr)\n\
  36. -r - print removed edges to stderr\n\
  37. -? - print usage\n\
  38. If no files are specified, stdin is used\n";
  39. static void usage(int v)
  40. {
  41. printf(useString, CmdName);
  42. graphviz_exit(v);
  43. }
  44. static void init(opts_t *opts, int argc, char *argv[]) {
  45. int c;
  46. CmdName = argv[0];
  47. opterr = 0;
  48. while ((c = getopt(argc, argv, "o:vr?")) != -1) {
  49. switch (c) {
  50. case 'o':
  51. (void)fclose(opts->out);
  52. opts->out = fopen(optarg, "w");
  53. if (opts->out == NULL) {
  54. fprintf(stderr, "cannot open %s for writing\n",
  55. optarg);
  56. usage(1);
  57. }
  58. break;
  59. case 'v':
  60. opts->Verbose = true;
  61. break;
  62. case 'r':
  63. opts->PrintRemovedEdges = true;
  64. break;
  65. case '?':
  66. if (optopt == '\0' || optopt == '?')
  67. usage(0);
  68. else {
  69. fprintf(stderr, "%s: option -%c unrecognized\n",
  70. CmdName, optopt);
  71. usage(1);
  72. }
  73. break;
  74. default:
  75. UNREACHABLE();
  76. }
  77. }
  78. argv += optind;
  79. argc -= optind;
  80. if (argc)
  81. Files = argv;
  82. }
  83. int main(int argc, char **argv)
  84. {
  85. Agraph_t *g;
  86. ingraph_state ig;
  87. opts_t opts = {.out = stdout, .err = stderr};
  88. init(&opts, argc, argv);
  89. newIngraph(&ig, Files);
  90. while ((g = nextGraph(&ig)) != 0) {
  91. if (agisdirected(g))
  92. graphviz_tred(g, &opts);
  93. agclose(g);
  94. }
  95. graphviz_exit(0);
  96. }