/contrib/groff/src/utils/lkbib/lkbib.cpp

https://bitbucket.org/freebsd/freebsd-head/ · C++ · 137 lines · 110 code · 7 blank · 20 comment · 22 complexity · 26da29756b2c84e998b8b62fc1915152 MD5 · raw file

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