home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource5 / 379_01 / zoo210s.zoo / zoo.c < prev    next >
C/C++ Source or Header  |  1991-07-12  |  17KB  |  524 lines

  1. #ifndef LINT
  2. /* derived from: zoo.c 2.24 88/01/29 00:55:09 */
  3. static char sccsid[]="$Id: zoo.c,v 1.21 91/07/09 02:36:40 dhesi Exp $";
  4. #endif /* LINT */
  5.  
  6. #if 0
  7. #define TRACEI(item)    printf("line %d: %s= %d\n", __LINE__, #item, item)
  8. #define TRACES(item)    printf("line %d: %s= [%s]\n", __LINE__, #item, item)
  9. #endif
  10.  
  11. extern char version[];
  12.  
  13. /*
  14. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  15. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  16. (C) Copyright 1991 Rahul Dhesi -- All rights reserved
  17. */
  18. #include "options.h"
  19. #include "zooio.h"
  20. #include "various.h"
  21.  
  22. #include "zoo.h"
  23. #include "zoofns.h"
  24.  
  25. #include "errors.i"
  26. #include "zoomem.h"
  27.  
  28. static void ms_help();
  29. static void wait_return();
  30.  
  31. #ifdef TRACE_IO
  32. int verbose = 0;
  33. #endif
  34.  
  35. int instr PARMS ((char *, char *));
  36.  
  37. char *out_buf_adr;      /* points to memory allocated for output buffer(s) */
  38. char *in_buf_adr;       /* points to memory allocated for input buffer */
  39.  
  40. /* static declarations */
  41. int quiet = 0;             /* whether to be quiet */
  42. int next_arg = FIRST_ARG; /* filenames start at this position */
  43. int arg_count;          /* count of arguments supplied to program */
  44. char **arg_vector;      /* vector of arguments supplied to program */
  45.  
  46. main(argc,argv)
  47. register int argc;
  48. register char **argv;
  49. {
  50.    char *zooname;          /* synonym for argv[2] -- to make life easier */
  51. #ifndef OOZ
  52.    static char incorrect_args[] = "Incorrect number of arguments.\n";
  53.    int filecount;          /* how many filespecs supplied */
  54. #endif /* OOZ */
  55.  
  56. #ifdef OOZ
  57. #else
  58. /* else not OOZ */
  59.       static char usage[] = "Usage: zoo {acDeglLPTuUvx}[aAcCdEfInmMNoOpPqu1:/.@n] archive file\n(\"zoo h\" for help, \"zoo H\" for extended help)\n";
  60.       static char nov_usage[] =
  61.           "\nNovice usage:  zoo -cmd archive[.zoo] file...  where -cmd is one of these:\n";
  62.       char *option;
  63.  
  64.       static char nov_cmds[] =
  65.          /* ADD=0EXT=5    MOV=14TES=20PRI=26 DEL=33  LIS=41UPD=47  FRE=55   COMMENT=64 */
  66.            "-add -extract -move -test -print -delete -list -update -freshen -comment\n";
  67.  
  68. #ifdef NOENUM
  69. #define NONE   -1
  70. #define ADD    0
  71. #define EXTRACT 5
  72. #define MOVE   14
  73. #define TEST   20
  74. #define PRINT  26
  75. #define DELETE 33
  76. #define LIST   41
  77. #define UPDATE 47
  78. #define FRESHEN   55
  79. #define COMMENT   64
  80.  
  81. int cmd = NONE;
  82.  
  83. #else
  84.    enum choice {
  85.       NONE = -1, ADD = 0, EXTRACT = 5, MOVE = 14, TEST = 20, PRINT = 26,
  86.       DELETE = 33, LIST = 41, UPDATE = 47, FRESHEN = 55, COMMENT = 64
  87.    };
  88.    enum choice cmd = NONE;          /* assume no Novice command */
  89. #endif
  90.  
  91. #endif /* end of not OOZ */
  92.  
  93. #ifdef SPECINIT
  94.     void spec_init PARMS ((void));
  95.     spec_init();                            /* system-specific startup code */
  96. #endif
  97.  
  98.     /* make sure T_UINT16 is an unsigned 16-bit type, exactly.  This
  99.         code is included only if T_UINT16 was defined by default at the
  100.         end of options.h. */
  101. #ifdef CHECK_TUINT
  102.     {
  103.         T_UINT16 i;
  104.         int status = 0;
  105.         i = ((unsigned) 1) << 15;
  106.         if (i < 0)
  107.             status = 1;
  108.         if (i != ((unsigned) 1) << 15)
  109.             status = 1;
  110.         i *= 2;
  111.         if (i != 0)
  112.             status = 1;
  113.         if (status != 0)
  114.             prterror('w', "Configuration problem: T_UINT16 is not 16 bits\n");
  115.     }
  116. #endif
  117.  
  118.    arg_count = argc;
  119.    arg_vector = argv;
  120.    zooname = argv[FIRST_ARG-1];     /* points to name or archive */
  121.  
  122. #ifdef OOZ
  123.    if (argc < 2) {
  124.       putstr (usage1);
  125.       putstr (usage2);
  126.       zooexit (1);
  127.    }
  128. #else
  129. /* else not OOZ */
  130.    if (argc < 2)
  131.       goto show_usage;
  132.    filecount = argc - 3;
  133.    option = str_dup(argv[1]);
  134.  
  135. #ifdef TRACE_IO
  136.    if (*option == ':') {         /* for debugging output */
  137.       verbose++;
  138.       option++;                  /* hide the : from other functions */
  139.    }
  140. #endif
  141.  
  142. #ifdef WAIT_PROMPT
  143.    if (*option == 'w') {
  144.         option++;                        /* hide w from other functions */
  145.         wait_return();
  146.     }
  147. #endif /* WAIT_PROMPT */
  148.  
  149.    if (*option == 'H') ms_help(option);
  150.    if (*option == 'h' || *option == 'H')
  151.       goto bigusage;
  152.     if (strchr("-acDegflLPTuUvVx", *option) == NULL)
  153.         goto give_list;
  154.  
  155.    if (*option == '-') {
  156.  
  157. #ifdef NOENUM
  158.       cmd = instr (nov_cmds, str_lwr(option));
  159. #else
  160.       cmd = (enum choice) instr (nov_cmds, str_lwr(option));
  161. #endif
  162.  
  163.       if (strlen(option) < 2 || cmd == NONE)
  164.          goto show_usage;
  165.       if (  ((cmd == ADD || cmd == MOVE || cmd == FRESHEN ||
  166.                   cmd == UPDATE || cmd == DELETE) && argc < 4) ||
  167.             ((cmd == EXTRACT || cmd == TEST || cmd == LIST ||
  168.                      cmd == PRINT || cmd == COMMENT) && argc < 3)) {
  169.          fprintf (stderr, incorrect_args);
  170.          goto show_usage;
  171.       }
  172.    } else {
  173.         char *wheresI;        /* will be null if I option not supplied */
  174.         if    (
  175.                 (
  176.                     strchr("au",*option) &&
  177.                     (
  178.                         (((wheresI = strchr(option,'I')) != 0) &&
  179.                             argc != 3) ||
  180.                         wheresI==NULL && argc < 4
  181.                     )
  182.                 ) ||
  183.                  strchr("DU",*option) && argc < 4 ||
  184.              strchr("cexlvVL",*option) && argc < 3 ||
  185.              strchr("TP",*option)   && argc != 3 ||
  186.                  (*option == 'f' && argc != 2) ||
  187.                  (*option == 'g' &&
  188.                     (strchr(option,'A') == NULL && argc < 4 ||
  189.                      strchr(option,'A') != NULL && argc != 3
  190.                     )
  191.                  )
  192.             ) {
  193.          fprintf (stderr, incorrect_args);
  194.          goto show_usage;
  195.       }
  196.    }
  197. #endif /* end of not OOZ */
  198.  
  199. #ifndef OOZ
  200.    /* if not doing a list and no extension in archive name, add default
  201.    extension */
  202.    if (*option != 'f' && cmd != LIST && strchr("lvVL", *option) == NULL &&
  203.          strchr(nameptr (zooname), EXT_CH) == NULL)
  204.       zooname = newcat (zooname, EXT_DFLT);
  205. #endif
  206.  
  207. /*
  208. Here we allocate a large block of memory for the duration of the program.
  209. lzc() and lzd() will use half of it each.  Routine getfile() will use all
  210. of it.  Routine decode() will use the first 8192 bytes of it.  Routine
  211. encode() will use all of it. */
  212.  
  213. /*                          fudge/2           fudge/2
  214. **             [______________||________________|]
  215. **               output buffer    input buffer
  216. */
  217.    out_buf_adr = ealloc (MEM_BLOCK_SIZE);
  218.    in_buf_adr = out_buf_adr + OUT_BUF_SIZE + (FUDGE/2);
  219.  
  220. #ifdef OOZ
  221. zooext(zooname, "\0");     /* just extract -- no fancy stuff   */
  222. zooexit (0);                  /* and exit normally                */
  223. #else
  224. /* else not OOZ -- parse command line and invoke a routine */
  225.    if (cmd != NONE) {
  226.       switch (cmd) {
  227.  
  228.          case ADD:      zooadd (zooname, filecount, &argv[3], "aP:"); break;
  229.          case FRESHEN:  zooadd (zooname, filecount, &argv[3], "auP:"); break;
  230.          case UPDATE:   zooadd (zooname, filecount, &argv[3], "aunP:"); break;
  231.          case MOVE:     zooadd (zooname, filecount, &argv[3], "aMP:"); break;
  232.  
  233.          case EXTRACT:  zooext (zooname, "x"); break;
  234.          case TEST:     zooext (zooname, "xNd"); break;
  235.          case PRINT:    zooext (zooname, "xp"); break;
  236.  
  237.          case DELETE:   zoodel (zooname, "DP",1); break;
  238.          case LIST:     zoolist (&argv[2], "VC", argc-2); break;
  239.          case COMMENT:  comment (zooname, "c"); break;
  240.          default: goto show_usage;
  241.       }
  242.    } else
  243.       switch (*option) {
  244.  
  245.          case 'a':
  246.          case 'u':
  247.          case 'T':   
  248.             zooadd (zooname, filecount, &argv[3], option); break;
  249. #ifdef FILTER
  250.             case 'f':
  251.                 zoofilt (option);  break;
  252. #endif /* FILTER */
  253.          case 'D':
  254.             zoodel (zooname, option, 1); break;
  255.          case 'U':
  256.             zoodel (zooname, option, 0); break;
  257.             case 'g':
  258.                 zoodel (zooname, option, 2); break;
  259.          case 'v':
  260.             case 'V':
  261.          case 'l': 
  262.             zoolist(&argv[2], option, 1); break;
  263.          case 'L': 
  264.             zoolist(&argv[2], option, argc-2); break;
  265.          case 'e':
  266.          case 'x': 
  267.             zooext(zooname, option); break;
  268.          case 'P':
  269.             zoopack (zooname, option); break;
  270.          case 'c':
  271.             comment (zooname, option); break;
  272.          default:
  273.             goto give_list;
  274.       }
  275. zooexit (0);      /* don't fall through */
  276.  
  277. /* usage list including Novice commands */
  278. show_usage:
  279.    fpri