home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / forut062.zip / ForUtil-0.62 / commons / get_common.c next >
C/C++ Source or Header  |  1996-08-28  |  5KB  |  237 lines

  1. #ifndef lint
  2. static char rcsId[]="$Header: /usr/local/rcs/ForUtil/Commons/RCS/get_common.c,v 1.7 1996/08/28 17:44:10 koen Exp koen $";
  3. #endif
  4. /*****
  5. * get_common.c : lookup the definition of a common in a database
  6. *
  7. * This file Version    $Revision: 1.7 $
  8. *
  9. * Creation date:    Fri Mar 15 02:50:55 GMT+0100 1996
  10. * Last modification:     $Date: 1996/08/28 17:44:10 $
  11. * By:            $Author: koen $
  12. * Current State:    $State: Exp $
  13. *
  14. * Author:        koen
  15. * (C)Copyright 1995 Ripley Software Development
  16. * All Rights Reserved
  17. *****/
  18. /*****
  19. * ChangeLog 
  20. * $Log: get_common.c,v $
  21. * Revision 1.7  1996/08/28 17:44:10  koen
  22. * Added print_version_id and corrected usage message.
  23. *
  24. * Revision 1.6  1996/08/27 19:10:27  koen
  25. * Added get_filename_part and updated usage reporting.
  26. *
  27. * Revision 1.5  1996/08/02 14:48:56  koen
  28. * moved all system dependencies to lib/sysdeps.h
  29. *
  30. * Revision 1.4  1996/07/30 01:55:23  koen
  31. * updated command line options.
  32. *
  33. * Revision 1.3  1996/05/06 00:32:32  koen
  34. * Adapted for MSDOS
  35. *
  36. * Revision 1.2  1996/04/25 02:26:55  koen
  37. * Cleanup and comments added.
  38. *
  39. * Revision 1.1  1996/03/15 04:16:28  koen
  40. * Initial Revision
  41. *
  42. *****/ 
  43. #ifdef HAVE_CONFIG_H
  44. #include "autoconf.h"
  45. #endif
  46.  
  47. #include "lib_commons.h"
  48. #include "forutil.h"
  49. #include "memmacros.h"
  50. #include "version.h"
  51.  
  52. static char progname[32];
  53.  
  54. static GDBM_FILE db_file;
  55. static int showinfo;
  56. static int senscase;
  57. char *database;
  58.  
  59. static char *usage = {"Usage: %s [-cv] [-ddatabase] <name of common>\n"
  60. "\n Options: \n"
  61. "\t-c: case sensitive search, default is off.\n"
  62. "\t-h, --help: print this help\n"
  63. "\t-v : show database version information\n"
  64. "\t--version: display version number\n"
  65. "\t-d[database]: database to search. If not given, the value of \n"
  66. "\t              %s is checked.\n\n"
  67. "(C)Copyright 1995-1996 by Ripley Software Development\n"};
  68.  
  69. #define SHUTUP(text,exitnum)    {  \
  70.     fprintf(stderr, "\n%s: %s\n", progname, text); \
  71.     if(db_file) { \
  72.         fprintf(stderr, "Syncing and closing database\n");\
  73.         gdbm_close(db_file); \
  74.     } \
  75.     fprintf(stderr, "(aborted with signal %s)\n", #exitnum); \
  76.     exit(exitnum+20); }
  77.  
  78. /****
  79. * Signal handler for a nice and clean exit 
  80. ****/
  81. static void
  82. sig_handler(int signum)
  83. {
  84.     switch(signum)
  85.     {
  86. #ifdef SIGBUS
  87.         case SIGBUS:
  88.             SHUTUP("Aaaiieee, a bus error.", SIGBUS);
  89. #endif /* SIGBUS */
  90.         case SIGSEGV:
  91.             SHUTUP("Oops, Segmentation violation!", SIGSEGV);
  92. #ifdef SIGALRM
  93.         case SIGALRM:
  94.             SHUTUP("Aborted.", SIGALRM); 
  95. #endif
  96.         case SIGUSR1:
  97.             SHUTUP("Memory allocation error.", SIGUSR1);
  98. #ifdef SIGQUIT
  99.         case SIGQUIT:
  100.             SHUTUP("Interrupted", SIGQUIT);
  101. #endif
  102.         case SIGINT:
  103.             SHUTUP("Interrupted", SIGINT);
  104.         default:
  105.             SHUTUP("What did _you_ do? Stopping.", signum);
  106.     }
  107. }
  108.  
  109. /*****
  110. * Scan and set command line options
  111. *****/
  112. int set_global_option(char *arg)
  113.     switch(arg[0])
  114.     {
  115.         case 'c' :    /* case sensitive search */
  116.             senscase = 1;
  117.             break;
  118.         case 'h' :    /* help requested */
  119.             printf(usage, progname, COMMON_LOCATION); 
  120.             exit(2); 
  121.         case 'v' :    /* only adminstritivia for the given database */
  122.             showinfo = 1;
  123.             break;
  124.         case 'd' :    /* database to use */
  125.             SCAN_ARG("-d")
  126.             database = arg + 1;
  127.             return(1);
  128.         case '-' :    /* secondary options */
  129.             if(!strcmp("help", arg+1))    /* help wanted */
  130.             {
  131.                 printf(usage, progname, COMMON_LOCATION); 
  132.                 exit(2); 
  133.             }
  134.             if(!strcmp("version", arg+1))    /* version number */
  135.             {
  136.                 print_version_id(progname, VERSION, "$Revision: 1.7 $");
  137.                 exit(2);
  138.             }        /* fall through */
  139.         default:
  140.             fprintf(stderr,"unknown option -%s\n", arg);
  141.             exit(3);
  142.     }
  143.     return(0); 
  144. }
  145.  
  146.  
  147. int main(int argc, char *argv[])
  148. {
  149.     GDBM_FILE db_file;
  150.     datum key, content;
  151.     char common[12], *chPtr, *arg;
  152.     int narg, i;
  153.  
  154.     database = NULL;
  155.  
  156.     get_filename_part(argv[0], progname);
  157.  
  158.     arg = argv[narg = 1];
  159.     while ( narg < argc && arg[0] == '-' )
  160.     {
  161.         if (arg[0] == '-')
  162.         { 
  163.             int num_opts = strlen(arg) ; 
  164.             for(i = 1 ; i < num_opts ; i++ )
  165.             if(set_global_option(++arg)) 
  166.                 break;
  167.         } 
  168.         arg = argv[++narg];
  169.     }
  170.  
  171.     if(narg == argc && !showinfo)
  172.     {
  173.         fprintf(stderr, "%s: no key given\n", progname);
  174.         printf(usage, progname, COMMON_LOCATION);
  175.         exit(4);
  176.     }
  177.     /*
  178.     * Try to get a database name from the environment specification if no
  179.     * database is given on the command line 
  180.     */
  181.     if(database == NULL)
  182.         database = get_database_location(COMMON_LOCATION);
  183.     if(database == NULL)
  184.     {
  185.         fprintf(stderr, "%s: no database found/specified\n", argv[0]);
  186.         exit(5);
  187.     }
  188.  
  189.     /* Try to open the database */
  190.     db_file = gdbm_open(database, 256, GDBM_READER, DBM_PERM, NULL);
  191.     if(db_file == NULL)
  192.     {
  193.         fprintf(stderr, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
  194.         perror(database);
  195.         exit(6);
  196.     }
  197.  
  198.     /* 
  199.     * Attach signals, we don't want our database messed up when something 
  200.     * happens 
  201.     */
  202.     ATTACH_SIGNALS;
  203.  
  204.     if(!showinfo)
  205.     {
  206.         strcpy(common, argv[narg]);
  207.  
  208.         /* make key uppercase when we must ignore case */
  209.         if(senscase == 0)
  210.             upcase(common); 
  211.         key.dptr = common;
  212.         key.dsize = strlen(common)+1;
  213.         content = gdbm_fetch(db_file, key);
  214.         if(content.dptr == NULL)
  215.         {
  216.             fprintf(stderr, "%s not found in database %s\n", common,
  217.                 database);
  218.             return(-1);
  219.         }
  220.         if(strstr(content.dptr, ","))
  221.         {
  222.             printf("%s defined in multiple files:\n", common);
  223.             for(chPtr = strtok(content.dptr, ","); chPtr != NULL; 
  224.                 chPtr = strtok(NULL, ","))
  225.                 printf("\t%s\n", chPtr);
  226.         }
  227.         else
  228.             printf("%s defined in file %s\n", common, content.dptr);
  229.         free(content.dptr);
  230.     }
  231.     else
  232.         print_commondb_header(db_file);
  233.     gdbm_close(db_file);
  234.     return(0);
  235. }
  236.