home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / util / gnu / groff_src.lha / groff-1.10src / lookbib / lookbib.cc next >
C/C++ Source or Header  |  1995-06-22  |  3KB  |  129 lines

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