home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Snippets / MacLZSS 1.0.3 / unix main.c < prev   
Encoding:
C/C++ Source or Header  |  1996-07-06  |  4.2 KB  |  141 lines  |  [TEXT/CWIE]

  1. /*************************************************************************
  2.  *                                                                         *
  3.  *  Module:        unix main.c                                                 *
  4.  *    Programmer:    Steve Adams                                                 *
  5.  *                                                                         *
  6.  *  (C) Copyright 1985, THINK Technologies, Inc.  All rights reserved.   *
  7.  *                                                                         *
  8.  *  Alternate main program to handle Unix command lines under Lightspeed *
  9.  *  C.  The user is prompted for the command line on program entry.  The *
  10.  *  line is disected and placed in an "argv" array up to a maximum of    *
  11.  *  MAX_ARGS entries.  I/O redirection is also supported, as follows:    *
  12.  *                                                                         *
  13.  *        >         redirects stdout to following file name                     *
  14.  *        <        redirects stdin to following file name                     *
  15.  *        >>        redirects stderr to following file name                     *
  16.  *                                                                         *
  17.  *  File names following a redirection may be immediately after the      *
  18.  *  redirector, or at what appears to be the next argument.  If a file   *
  19.  *  open error occurs, then the program calls "SysBeep" and exits to the *
  20.  *  shell.                                                                 *
  21.  *                                                                         *
  22.  *  TO USE: change the "main" function in you application to "_main" and *
  23.  *  include this file in your project.                                     *
  24.  *                                                                         *
  25.  *************************************************************************/
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30.  
  31. #define    MAX_ARGS                     50
  32.  
  33. #ifndef    true
  34. #define    true                          1
  35. #define false                          0
  36. #endif
  37.  
  38. static    int            argc            = 1;        /* final argument count  */
  39. static    char        *argv[MAX_ARGS]    = { "" };    /* array of pointers     */
  40. static    char        command[256];                /* input line buffer     */
  41. static    int            filename         = false;    /* TRUE iff file name     */
  42.  
  43.  
  44. /*************************************************************************
  45.  *                                                                         *
  46.  *  Local routine to make a "beep" and exit to the shell.                 *
  47.  *                                                                         *
  48.  *************************************************************************/
  49.  
  50. static void punt()
  51.     {
  52.     SysBeep( 5L );
  53.     ExitToShell();
  54.     }
  55.  
  56.  
  57. /*************************************************************************
  58.  *                                                                         *
  59.  *  Local routine to open a file in argv[--argc] after closing it's         *
  60.  *  previous existance.                                                     *
  61.  *                                                                         *
  62.  *************************************************************************/
  63.  
  64.  
  65. static void openfile( file, mode )
  66. char                *mode;                        /* mode for file open     */
  67. FILE                *file;                        /* file pointer to use     */
  68.     {
  69.  
  70.     if ( (file = freopen( argv[--argc], mode, file ) ) <= (FILE *) NULL)
  71.         punt();
  72.     filename = false;
  73.     }
  74.  
  75.  
  76. /*************************************************************************
  77.  *                                                                         *
  78.  *  New main routine.  Prompts for command line then calls user's main   *
  79.  * now called "_main" with the argument list and redirected I/O.         *
  80.  *                                                                         *
  81.  *************************************************************************/
  82.  
  83.  
  84. void main()
  85.     {
  86.     char            c;                            /* temp for EOLN check     */
  87.     register char    *cp;                        /* index in command line */
  88.     char            *mode;                        /* local file mode         */
  89.     FILE            *file;                        /* file to change         */
  90.     int i;
  91.     
  92.     printf(    "LZSS.C File Compression Utility - by Haruhiko Okumura (6-Apr-89)\n"
  93.             "adapted from MS-DOS to Macintosh by Rob Elliott (27-May-89)\n"
  94.             "and ported to CodeWarrior 9 by Paul Celestin (04-Jul-96)\n\n"
  95.             "EXPERIMENTAL: See 'About MacLZSS' for more information!\n"
  96.             "Commands: 'e file1 file2' encodes file1 into file2.\n"
  97.             "          'd file2 file1' decodes file2 into file1.\n\n"
  98.             "Enter command line:\n");
  99.  
  100.     gets( command );                            /* allow user to edit     */
  101.     cp = &command[0];                            /* start of buffer         */
  102.     argv[0] = "";                                /* program name is NULL  */
  103.     while (argc < MAX_ARGS)
  104.         { /* up to MAX_ARGS entries */
  105.         while (isspace( *cp++ ));
  106.         if ( !*--cp )
  107.             break;
  108.         else if ( *cp == '<' )
  109.             { /* redirect stdin */
  110.             cp++;
  111.             file = stdin;
  112.             mode = "r";
  113.             filename = true;
  114.             }
  115.         else if ( *cp == '>' )
  116.             {
  117.             mode = "w";
  118.             filename = true;
  119.             if (*++cp == '>')
  120.                 {
  121.                 file = stderr;
  122.                 cp++;
  123.                 }
  124.             else
  125.                 file = stdout;
  126.             }
  127.         else
  128.             { /* either an argument or a filename */
  129.             argv[argc++] = cp;
  130.             while ( *++cp && !isspace( *cp ) );
  131.             c = *cp;
  132.             *cp++ = '\0';
  133.             if (filename)
  134.                 openfile( file, mode );
  135.             if (!c)
  136.                 break;
  137.             }
  138.         }
  139.     _main( argc, argv );
  140.     }
  141.