home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / GNU Diff Sources / GNU DIFF 1.15b Sources / unix main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-20  |  4.5 KB  |  165 lines  |  [TEXT/ALFA]

  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. #include    <MacHeaders>
  28.  
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include    "diff.h"
  32.  
  33. #define    MAX_ARGS                     50
  34.  
  35. #ifndef    true
  36. #define    true                          1
  37. #define false                          0
  38. #endif
  39.  
  40. #ifdef    COMMENT
  41. static    int            argc            = 1;        /* final argument count  */
  42. static    char        *argv[MAX_ARGS]    = { "" };    /* array of pointers     */
  43. static    char        command[256];                /* input line buffer     */
  44. static    int            filename         = false;    /* TRUE iff file name     */
  45. #endif
  46.  
  47. /*************************************************************************
  48.  *                                                                         *
  49.  *  Local routine to make a "beep" and exit to the shell.                 *
  50.  *                                                                         *
  51.  *************************************************************************/
  52.  
  53. static void punt()
  54.     {
  55.     SysBeep( 5L );
  56.     ExitToShell();
  57.     }
  58.  
  59.  
  60. /*************************************************************************
  61.  *                                                                         *
  62.  *  Local routine to open a file in argv[--argc] after closing it's         *
  63.  *  previous existance.                                                     *
  64.  *                                                                         *
  65.  *************************************************************************/
  66.  
  67.  
  68. static void openfile( file, mode, argc, argv, filename )
  69. char                *mode;                        /* mode for file open     */
  70. FILE                *file;                        /* file pointer to use     */
  71. int    *argc;
  72. char    **argv;
  73. int    *filename;
  74.     {
  75.  
  76.     if ( (file = freopen( argv[--*argc], mode, file ) ) <= (FILE *) NULL)
  77.         punt();
  78.     *filename = false;
  79.     }
  80.  
  81.  
  82. /*************************************************************************
  83.  *                                                                         *
  84.  *  New main routine.  Prompts for command line then calls user's main   *
  85.  * now called "_main" with the argument list and redirected I/O.         *
  86.  *                                                                         *
  87.  *************************************************************************/
  88.  
  89. static    void
  90. domain (char *command, int mainFunc(int argc, char *argv[]))
  91.     {
  92.     char            c;                            /* temp for EOLN check     */
  93.     register char    *cp;                        /* index in command line */
  94.     char            *mode;                        /* local file mode         */
  95.     FILE            *file;                        /* file to change         */
  96.     int i;
  97.     int            argc            = 1;        /* final argument count  */
  98.     char        *argv[MAX_ARGS]    = { "" };    /* array of pointers     */
  99.     int            filename         = false;    /* TRUE iff file name     */
  100.     
  101.     
  102.     cp = &command[0];                            /* start of buffer         */
  103.     argv[0] = "";                                /* program name is NULL  */
  104.     while (argc < MAX_ARGS)
  105.         { /* up to MAX_ARGS entries */
  106.         while (isspace( *cp++ ));
  107.         if ( !*--cp )
  108.             break;
  109.         else if ( *cp == '<' )
  110.             { /* redirect stdin */
  111.             cp++;
  112.             file = stdin;
  113.             mode = "r";
  114.             filename = true;
  115.             }
  116.         else if ( *cp == '>' )
  117.             {
  118.             mode = "w";
  119.             filename = true;
  120.             if (*++cp == '>')
  121.                 {
  122.                 file = stderr;
  123.                 cp++;
  124.                 }
  125.             else
  126.                 file = stdout;
  127.             }
  128.         else
  129.             { /* either an argument or a filename */
  130.             argv[argc++] = cp;
  131.             if (*cp == '\"')  {
  132.                 while (*++cp && (*cp == '\"'));
  133.                 cp++;
  134.             }
  135.             else {
  136.                 while ( *++cp && !isspace( *cp ) );
  137.             }
  138.             c = *cp;
  139.             *cp++ = '\0';
  140.             if (filename)
  141.                 openfile( file, mode, &argc, argv, &filename );
  142.             if (!c)
  143.                 break;
  144.             }
  145.         }
  146.     (*mainFunc) ( argc, argv );
  147.     }
  148.  
  149.  
  150. void
  151. main(int argc,char * *argv)
  152.     {
  153.     char        command[256];                /* input line buffer     */
  154.  
  155.     printf( "Enter Unix command line:\n" );
  156.     gets( &command[0] );                            /* allow user to edit     */
  157.     if ( command[0] == '3' )
  158.     {
  159.         domain (command + 1, main_diff3);
  160.     }
  161.     else
  162.         domain (command, main_diff2);
  163.     
  164.     }
  165.