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

  1. /* Copyright (c) 1994 Burra Gopal, Udi Manber.  All Rights Reserved. */
  2.  
  3. /*
  4.  * main_uncast.c: uses functions in hash.c and uncast.c to implement "uncast".
  5.  */
  6.  
  7. #include "defs.h"
  8.  
  9. extern char **environ;
  10. usage(progname)
  11. char    *progname;
  12. {
  13.     fprintf(stderr, "\nThis is uncast 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] [-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 remove source files after uncompress\n");
  18.     /* fprintf(stderr, "-e: is used with files NOT compressed for easy search\n"); */
  19.     fprintf(stderr, "-o: DO NOT overwrite the existing clear file (if any)\n");
  20.     fprintf(stderr, "-r: uncompress recursively\n");
  21.     fprintf(stderr, "-s: proceed silently and output the names of the uncompressible 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 rather than 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    freq_file[MAX_LINE_LEN];
  37.     char    string_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.     int    num_read;
  44.     char    buffer[SIGNATURE_LEN];
  45.  
  46.     filev = (char **)malloc(sizeof(char *) * argc);
  47.     memset(filev, '\0', sizeof(char *)*argc);
  48.     filec = 0;
  49.     comp_dir[0] = '\0';
  50.     freq_file[0] = '\0';
  51.     string_file[0] = '\0';
  52.     FLAGS = 0;
  53.     FLAGS |= TC_ERRORMSGS | TC_REMOVE | TC_OVERWRITE | TC_EASYSEARCH;
  54.  
  55.     /* Look at options */
  56.     i=1;
  57.     while (i < argc) {
  58.         if (filec == 0 && !strcmp(argv[i], "-d")) {
  59.             FLAGS &= ~TC_REMOVE;
  60.             i ++;
  61.         }
  62.         else if (filec == 0 && !strcmp(argv[i], "-V")) {
  63.             printf("\nThis is uncast version %s, %s.\n\n", CAST_VERSION, CAST_DATE);
  64.             return 0;
  65.         }
  66.         /*
  67.         else if (filec == 0 && !strcmp(argv[i], "-e")) {
  68.             FLAGS &= ~TC_EASYSEARCH;
  69.             i ++;
  70.         }
  71.         */
  72.         if (filec == 0 && !strcmp(argv[i], "-help")) {
  73.             return usage(argv[0]);
  74.         }
  75.         else if (filec == 0 && !strcmp(argv[i], "-o")) {
  76.             FLAGS &= ~TC_OVERWRITE;
  77.             i ++;
  78.         }
  79.         else if (filec == 0 && !strcmp(argv[i], "-r")) {
  80.             FLAGS |= TC_RECURSIVE;
  81.             i ++;
  82.         }
  83.         else if (filec == 0 && !strcmp(argv[i], "-s")) {
  84.             FLAGS |= TC_SILENT;
  85.             i ++;
  86.         }
  87.         else if (filec == 0 && !strcmp(argv[i], "-y")) {
  88.             FLAGS |= TC_NOPROMPT;
  89.             i ++;
  90.         }
  91.         else if(filec == 0 && !strcmp(argv[1], "-F")) {
  92.             FLAGS |= TC_FILENAMESONSTDIN;
  93.             i ++;
  94.         }
  95.         else if (filec == 0 && !strcmp(argv[i], "-H")) {
  96.             if (i + 1 >= argc) {
  97.                 fprintf(stderr, "directory not specified after -H\n");
  98.                 return usage(argv[0]);
  99.             }
  100.             strcpy(comp_dir, argv[i+1]);
  101.             i+=2;
  102.         }
  103.         else {
  104.             filev[filec++] = argv[i++];
  105.         }
  106.     }
  107.  
  108.     if (comp_dir[0] == '\0') {
  109.         if ((home = (char *)getenv("HOME")) == NULL) {
  110.             getcwd(comp_dir, MAXNAME-1);
  111.             fprintf(stderr, "using working-directory '%s' to locate index\n", comp_dir);
  112.         }
  113.         else strncpy(comp_dir, home, MAXNAME);
  114.     }
  115.     strcpy(string_file, comp_dir);
  116.     strcat(string_file, "/");
  117.     strcat(string_file, DEF_STRING_FILE);
  118.     strcpy(freq_file, comp_dir);
  119.     strcat(freq_file, "/");
  120.     strcat(freq_file, DEF_FREQ_FILE);
  121.     if (!initialize_tuncompress(string_file, freq_file, FLAGS)) exit(2);
  122.  
  123.     if (FLAGS & TC_SILENT) FLAGS &= ~TC_ERRORMSGS;
  124.  
  125.     /* now compress each file in filev array: if no files specified, compress stdin and put it on stdout */
  126.     if (filec == 0) {
  127.         if (FLAGS & TC_FILENAMESONSTDIN) {
  128.             while (fgets(name, MAX_LINE_LEN, stdin) == name) {
  129.                 tuncompress_file(name, outname, FLAGS);
  130.             }
  131.         }
  132.         else {
  133.             num_read = fread(buffer, 1, SIGNATURE_LEN - 1, stdin);
  134.             if (!tuncompressible(buffer, num_read)) {
  135.                 fprintf(stderr, "signature does not match -- cannot uncompress %s\n", filev[i]);
  136.                 return -1;
  137.             }
  138.             tuncompress(stdin, -1, stdout, -1, FLAGS);
  139.         }
  140.     }
  141.     else for (i=0; i<filec; i++) {
  142.         strcpy(name, filev[i]);
  143.         tuncompress_file(name, outname, FLAGS);
  144.     }
  145.     return 0;
  146. }
  147.  
  148.