/contrib/groff/src/utils/lookbib/lookbib.cpp

https://bitbucket.org/freebsd/freebsd-head/ · C++ · 143 lines · 112 code · 9 blank · 22 comment · 25 complexity · b4efd1864713a5e555f384f4d5aba94f MD5 · raw file

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989-1992, 2000, 2001, 2002, 2003
  3. Free Software Foundation, Inc.
  4. Written by James Clark (jjc@jclark.com)
  5. This file is part of groff.
  6. groff is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 2, or (at your option) any later
  9. version.
  10. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with groff; see the file COPYING. If not, write to the Free Software
  16. Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
  17. #include "lib.h"
  18. #include <stdlib.h>
  19. #include <assert.h>
  20. #include <errno.h>
  21. #include "errarg.h"
  22. #include "error.h"
  23. #include "cset.h"
  24. #include "refid.h"
  25. #include "search.h"
  26. /* for isatty() */
  27. #include "posix.h"
  28. #include "nonposix.h"
  29. extern "C" {
  30. const char *Version_string;
  31. }
  32. static void usage(FILE *stream)
  33. {
  34. fprintf(stream, "usage: %s [-v] [-i XYZ] [-t N] database ...\n",
  35. program_name);
  36. }
  37. int main(int argc, char **argv)
  38. {
  39. program_name = argv[0];
  40. static char stderr_buf[BUFSIZ];
  41. setbuf(stderr, stderr_buf);
  42. int opt;
  43. static const struct option long_options[] = {
  44. { "help", no_argument, 0, CHAR_MAX + 1 },
  45. { "version", no_argument, 0, 'v' },
  46. { NULL, 0, 0, 0 }
  47. };
  48. while ((opt = getopt_long(argc, argv, "vVi:t:", long_options, NULL)) != EOF)
  49. switch (opt) {
  50. case 'V':
  51. verify_flag = 1;
  52. break;
  53. case 'i':
  54. linear_ignore_fields = optarg;
  55. break;
  56. case 't':
  57. {
  58. char *ptr;
  59. long n = strtol(optarg, &ptr, 10);
  60. if (n == 0 && ptr == optarg) {
  61. error("bad integer `%1' in `t' option", optarg);
  62. break;
  63. }
  64. if (n < 1)
  65. n = 1;
  66. linear_truncate_len = int(n);
  67. break;
  68. }
  69. case 'v':
  70. {
  71. printf("GNU lookbib (groff) version %s\n", Version_string);
  72. exit(0);
  73. break;
  74. }
  75. case CHAR_MAX + 1: // --help
  76. usage(stdout);
  77. exit(0);
  78. break;
  79. case '?':
  80. usage(stderr);
  81. exit(1);
  82. break;
  83. default:
  84. assert(0);
  85. }
  86. if (optind >= argc) {
  87. usage(stderr);
  88. exit(1);
  89. }
  90. search_list list;
  91. for (int i = optind; i < argc; i++)
  92. list.add_file(argv[i]);
  93. if (list.nfiles() == 0)
  94. fatal("no databases");
  95. char line[1024];
  96. int interactive = isatty(fileno(stdin));
  97. for (;;) {
  98. if (interactive) {
  99. fputs("> ", stderr);
  100. fflush(stderr);
  101. }
  102. if (!fgets(line, sizeof(line), stdin))
  103. break;
  104. char *ptr = line;
  105. while (csspace(*ptr))
  106. ptr++;
  107. if (*ptr == '\0')
  108. continue;
  109. search_list_iterator iter(&list, line);
  110. const char *start;
  111. int len;
  112. int count;
  113. for (count = 0; iter.next(&start, &len); count++) {
  114. if (fwrite(start, 1, len, stdout) != (size_t)len)
  115. fatal("write error on stdout: %1", strerror(errno));
  116. // Can happen for last reference in file.
  117. if (start[len - 1] != '\n')
  118. putchar('\n');
  119. putchar('\n');
  120. }
  121. fflush(stdout);
  122. if (interactive) {
  123. fprintf(stderr, "%d found\n", count);
  124. fflush(stderr);
  125. }
  126. }
  127. if (interactive)
  128. putc('\n', stderr);
  129. return 0;
  130. }