home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / index-db / part01 / printdb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-23  |  2.5 KB  |  128 lines

  1. #ifndef lint
  2. static char *RCSid = "$Header: /u5/davy/progs/index/RCS/printdb.c,v 1.1 89/08/09 11:06:47 davy Exp $";
  3. #endif
  4. /*
  5.  * printdb.c - print entries from the database.
  6.  *
  7.  * David A. Curry
  8.  * Research Institute for Advanced Computer Science
  9.  * Mail Stop 230-5
  10.  * NASA Ames Research Center
  11.  * Moffett Field, CA 94035
  12.  * davy@riacs.edu
  13.  *
  14.  * $Log:    printdb.c,v $
  15.  * Revision 1.1  89/08/09  11:06:47  davy
  16.  * Initial revision
  17.  * 
  18.  */
  19. #include <sys/file.h>
  20. #include <stdio.h>
  21. #include "defs.h"
  22.  
  23. /*
  24.  * print_db - print out entries marked DB_PRINT in the database.
  25.  */
  26. print_db(dbname, filter)
  27. char *dbname, *filter;
  28. {
  29.     FILE *pp;
  30.     FILE *popen();
  31.     char buf[BUFSIZ];
  32.     register int i, j;
  33.     register char *tab;
  34.  
  35.     /*
  36.      * If no filter was specified, we just spit the entries out,
  37.      * with their field names, to standard output.
  38.      */
  39.     if (filter == NULL) {
  40.         for (i=0; i < dbentries; i++) {
  41.             if ((db[i].db_flag & DB_VALID) == 0)
  42.                 continue;
  43.             if ((db[i].db_flag & DB_PRINT) == 0)
  44.                 continue;
  45.  
  46.             for (j=0; j < idx.idx_nlines; j++) {
  47.                 if (!verbose) {
  48.                     if (db[i].db_lines[j][0] == '\0')
  49.                         continue;
  50.                 }
  51.  
  52.                 sprintf(buf, "%s%s", idx.idx_lines[j],
  53.                     idx.idx_lines[j][0] ? ":" : "");
  54.                 printf("%-*s%s\n", idx.idx_maxlen + 2,
  55.                        buf, db[i].db_lines[j]);
  56.             }
  57.  
  58.             putchar('\n');
  59.         }
  60.  
  61.         return;
  62.     }
  63.  
  64.     /*
  65.      * Otherwise, we set up a pipe to the filter, and print
  66.      * first the field names, and then the fields.  We do
  67.      * this one entry per line, with fields separated by
  68.      * tabs.
  69.      */
  70.  
  71.     /*
  72.      * Create the path to a formatting program in the database
  73.      * directory.
  74.      */
  75.     sprintf(buf, "%s/%s%s", dbasedir, filter, FMTFILE_SUFFIX);
  76.  
  77.     /*
  78.      * If that's not there, then assume they gave us some
  79.      * program name (like "more" or something), and just
  80.      * stick it in there.
  81.      */
  82.     if (access(buf, X_OK) < 0)
  83.         strcpy(buf, filter);
  84.  
  85.     /*
  86.      * Open the pipe.
  87.      */
  88.     if ((pp = popen(buf, "w")) == NULL) {
  89.         error("%s: cannot execute \"%s\".\n", pname, filter, 0);
  90.         exit(1);
  91.     }
  92.  
  93.     /*
  94.      * Print the field names, separated by tabs.
  95.      */
  96.     tab = "";
  97.     for (i=0; i < idx.idx_nlines; i++) {
  98.         fprintf(pp, "%s%s", tab, idx.idx_lines[i]);
  99.         tab = "\t";
  100.     }
  101.  
  102.     putc('\n', pp);
  103.  
  104.     /*
  105.      * Print the entries, with fields separated
  106.      * by tabs.
  107.      */
  108.     for (i=0; i < dbentries; i++) {
  109.         if ((db[i].db_flag & DB_VALID) == 0)
  110.             continue;
  111.         if ((db[i].db_flag & DB_PRINT) == 0)
  112.             continue;
  113.  
  114.         tab = "";
  115.         for (j=0; j < idx.idx_nlines; j++) {
  116.             fprintf(pp, "%s%s", tab, db[i].db_lines[j]);
  117.             tab = "\t";
  118.         }
  119.  
  120.         putc('\n', pp);
  121.     }
  122.  
  123.     /*
  124.      * Close the pipe.
  125.      */
  126.     pclose(pp);
  127. }
  128.