home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- * *
- * Module: unix main.c *
- * Programmer: Steve Adams *
- * *
- * (C) Copyright 1985, THINK Technologies, Inc. All rights reserved. *
- * *
- * Alternate main program to handle Unix command lines under Lightspeed *
- * C. The user is prompted for the command line on program entry. The *
- * line is disected and placed in an "argv" array up to a maximum of *
- * MAX_ARGS entries. I/O redirection is also supported, as follows: *
- * *
- * > redirects stdout to following file name *
- * < redirects stdin to following file name *
- * >> redirects stderr to following file name *
- * *
- * File names following a redirection may be immediately after the *
- * redirector, or at what appears to be the next argument. If a file *
- * open error occurs, then the program calls "SysBeep" and exits to the *
- * shell. *
- * *
- * TO USE: change the "main" function in you application to "_main" and *
- * include this file in your project. *
- * *
- *************************************************************************/
-
- #include <MacHeaders>
-
- #include <stdio.h>
- #include <ctype.h>
- #include "diff.h"
-
- #define MAX_ARGS 50
-
- #ifndef true
- #define true 1
- #define false 0
- #endif
-
- #ifdef COMMENT
- static int argc = 1; /* final argument count */
- static char *argv[MAX_ARGS] = { "" }; /* array of pointers */
- static char command[256]; /* input line buffer */
- static int filename = false; /* TRUE iff file name */
- #endif
-
- /*************************************************************************
- * *
- * Local routine to make a "beep" and exit to the shell. *
- * *
- *************************************************************************/
-
- static void punt()
- {
- SysBeep( 5L );
- ExitToShell();
- }
-
-
- /*************************************************************************
- * *
- * Local routine to open a file in argv[--argc] after closing it's *
- * previous existance. *
- * *
- *************************************************************************/
-
-
- static void openfile( file, mode, argc, argv, filename )
- char *mode; /* mode for file open */
- FILE *file; /* file pointer to use */
- int *argc;
- char **argv;
- int *filename;
- {
-
- if ( (file = freopen( argv[--*argc], mode, file ) ) <= (FILE *) NULL)
- punt();
- *filename = false;
- }
-
-
- /*************************************************************************
- * *
- * New main routine. Prompts for command line then calls user's main *
- * now called "_main" with the argument list and redirected I/O. *
- * *
- *************************************************************************/
-
- static void
- domain (char *command, int mainFunc(int argc, char *argv[]))
- {
- char c; /* temp for EOLN check */
- register char *cp; /* index in command line */
- char *mode; /* local file mode */
- FILE *file; /* file to change */
- int i;
- int argc = 1; /* final argument count */
- char *argv[MAX_ARGS] = { "" }; /* array of pointers */
- int filename = false; /* TRUE iff file name */
-
-
- cp = &command[0]; /* start of buffer */
- argv[0] = ""; /* program name is NULL */
- while (argc < MAX_ARGS)
- { /* up to MAX_ARGS entries */
- while (isspace( *cp++ ));
- if ( !*--cp )
- break;
- else if ( *cp == '<' )
- { /* redirect stdin */
- cp++;
- file = stdin;
- mode = "r";
- filename = true;
- }
- else if ( *cp == '>' )
- {
- mode = "w";
- filename = true;
- if (*++cp == '>')
- {
- file = stderr;
- cp++;
- }
- else
- file = stdout;
- }
- else
- { /* either an argument or a filename */
- argv[argc++] = cp;
- if (*cp == '\"') {
- while (*++cp && (*cp == '\"'));
- cp++;
- }
- else {
- while ( *++cp && !isspace( *cp ) );
- }
- c = *cp;
- *cp++ = '\0';
- if (filename)
- openfile( file, mode, &argc, argv, &filename );
- if (!c)
- break;
- }
- }
- (*mainFunc) ( argc, argv );
- }
-
-
- void
- main(int argc,char * *argv)
- {
- char command[256]; /* input line buffer */
-
- printf( "Enter Unix command line:\n" );
- gets( &command[0] ); /* allow user to edit */
- if ( command[0] == '3' )
- {
- domain (command + 1, main_diff3);
- }
- else
- domain (command, main_diff2);
-
- }
-