home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / glimpse-2.1 / compress / main_cast.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-16  |  3.8 KB  |  136 lines

  1. /* Copyright (c) 1994 Burra Gopal, Udi Manber.  All Rights Reserved. */
  2.  
  3. /*
  4.  * main_cast.c    : uses functions in hash.c and cast.c to implement "cast".
  5.  */
  6.  
  7. #include "defs.h"
  8.  
  9. extern char **environ;
  10. usage(progname)
  11.     char    *progname;
  12. {
  13.     fprintf(stderr, "\nThis is cast version %s. Copyright (c) %s, University of Arizona.\n\n", GLIMPSE_VERSION, GLIMPSE_DATE);
  14.     fprintf(stderr, "usage: %s [-help] [-F] [-H dir] [-V] [-d] [-e] [-o] [-r] [-s] [-y] sourcefiles\n", progname);
  15.     fprintf(stderr, "summary of options (for a more detailed version, see 'man cast'):\n");
  16.     fprintf(stderr, "-help: output this menu\n");
  17.     fprintf(stderr, "-d: DO NOT delete sourcefiles after compress\n");
  18.     fprintf(stderr, "-e: DO NOT compress for easy search\n");
  19.     fprintf(stderr, "-o: DO NOT overwrite the existing compressed file (if any)\n");
  20.     fprintf(stderr, "-r: compress recursively\n");
  21.     fprintf(stderr, "-s: proceed silently and output the names of the compressible files\n");
  22.     fprintf(stderr, "-y: DO NOT prompt and always overwrite when used without -o\n");
  23.     fprintf(stderr, "-F: expect NAMES of files on stdin instead of data if there are no sourcefiles\n");
  24.     fprintf(stderr, "-H dir: the directory for dictionaries is 'dir' (default ~)\n");
  25.     fprintf(stderr, "\n");
  26.     exit(1);
  27. }
  28.  
  29. main(argc, argv)
  30.     int    argc;
  31.     char    *argv[];
  32. {
  33.     char    **filev;
  34.     int    filec;
  35.     int    i;    /* counter on argc, and later, filec */
  36.     char    hash_file[MAX_LINE_LEN];
  37.     char    freq_file[MAX_LINE_LEN];
  38.     char    comp_dir[MAX_LINE_LEN];
  39.     char    name[MAX_LINE_LEN];
  40.     char    outname[MAX_LINE_LEN];
  41.     char    *home;
  42.     int    FLAGS;
  43.  
  44.     filev = (char **)malloc(sizeof(char *) * argc);
  45.     memset(filev, '\0', sizeof(char *)*argc);
  46.     filec = 0;
  47.     comp_dir[0] = '\0';
  48.     hash_file[0] = '\0';
  49.     freq_file[0] = '\0';
  50.     FLAGS = 0;
  51.     FLAGS |= TC_ERRORMSGS | TC_REMOVE | TC_OVERWRITE | TC_EASYSEARCH;
  52.  
  53.     /* Look at options */
  54.     i=1;
  55.     while (i < argc) {
  56.         if (filec == 0 && !strcmp(argv[i], "-d")) {
  57.             FLAGS &= ~TC_REMOVE;
  58.             i ++;
  59.         }
  60.         else if (filec == 0 && !strcmp(argv[i], "-V")) {
  61.             printf("\nThis is cast version %s, %s.\n\n", CAST_VERSION, CAST_DATE);
  62.             return 0;
  63.         }
  64.         else if (filec == 0 && !strcmp(argv[i], "-e")) {
  65.             FLAGS &= ~TC_EASYSEARCH;
  66.             i ++;
  67.         }
  68.         if (filec == 0 && !strcmp(argv[i], "-help")) {
  69.             return usage(argv[0]);
  70.         }
  71.         else if (filec == 0 && !strcmp(argv[i], "-o")) {
  72.             FLAGS &= ~TC_OVERWRITE;
  73.             i ++;
  74.         }
  75.         else if (filec == 0 && !strcmp(argv[i], "-r")) {
  76.             FLAGS |= TC_RECURSIVE;
  77.             i ++;
  78.         }
  79.         else if (filec == 0 && !strcmp(argv[i], "-s")) {
  80.             FLAGS |= TC_SILENT;
  81.             i ++;
  82.         }
  83.         else if (filec == 0 && !strcmp(argv[i], "-y")) {
  84.             FLAGS |= TC_NOPROMPT;
  85.             i ++;
  86.         }
  87.         else if(filec == 0 && !strcmp(argv[1], "-F")) {
  88.             FLAGS |= TC_FILENAMESONSTDIN;
  89.             i ++;
  90.         }
  91.         else if (filec == 0 && !strcmp(argv[i], "-H")) {
  92.             if (i + 1 >= argc) {
  93.                 fprintf(stderr, "%s: directory not specified after -H\n", argv[0]);
  94.                 return usage(argv[0]);
  95.             }
  96.             strcpy(comp_dir, argv[i+1]);
  97.             i+=2;
  98.         }
  99.         else {
  100.             filev[filec++] = argv[i++];
  101.         }
  102.     }
  103.  
  104.     if (comp_dir[0] == '\0') {
  105.         if ((home = (char *)getenv("HOME")) == NULL) {
  106.             getcwd(comp_dir, MAXNAME-1);
  107.             fprintf(stderr, "using working-directory '%s' to locate index\n", comp_dir);
  108.         }
  109.         else strncpy(comp_dir, home, MAXNAME);
  110.     }
  111.     strcpy(hash_file, comp_dir);
  112.     strcat(hash_file, "/");
  113.     strcat(hash_file, DEF_HASH_FILE);
  114.     strcpy(freq_file, comp_dir);
  115.     strcat(freq_file, "/");
  116.     strcat(freq_file, DEF_FREQ_FILE);
  117.     if (!initialize_tcompress(hash_file, freq_file, FLAGS)) exit(2);
  118.  
  119.     if (FLAGS & TC_SILENT) FLAGS &= ~TC_ERRORMSGS;
  120.  
  121.     /* now compress each file in filev array: if no files specified, compress stdin and put it on stdout */
  122.     if (filec == 0) {
  123.         if (FLAGS & TC_FILENAMESONSTDIN) {
  124.             while (fgets(name, MAX_LINE_LEN, stdin) == name) {
  125.                 tcompress_file(name, outname, FLAGS);
  126.             }
  127.         }
  128.         else tcompress(stdin, -1, stdout, -1, FLAGS);
  129.     }
  130.     else for (i=0; i<filec; i++) {
  131.         strcpy(name, filev[i]);
  132.         tcompress_file(name, outname, FLAGS);
  133.     }
  134.     return 0;
  135. }
  136.