The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

execvms.c

Go to the documentation of this file.
00001 /* 
00002  * Copyright 1993, 1995 Christopher Seiwald.
00003  *
00004  * This file is part of Jam - see jam.c for Copyright information.
00005  */
00006 
00007 # include "jam.h"
00008 # include "lists.h"
00009 # include "execcmd.h"
00010 
00011 # ifdef OS_VMS
00012 
00013 #include <stdio.h>
00014 #include <string.h>
00015 #include <stdlib.h>
00016 #include <iodef.h>
00017 #include <ssdef.h>
00018 #include <descrip.h>
00019 #include <dvidef.h>
00020 #include <clidef.h>
00021 
00022 /*
00023  * execvms.c - execute a shell script, ala VMS
00024  *
00025  * The approach is this:
00026  *
00027  *      If the command is a single line, and shorter than WRTLEN (what we 
00028  *      believe to be the maximum line length), we just system() it.
00029  *
00030  *      If the command is multi-line, or longer than WRTLEN, we write the 
00031  *      command block to a temp file, splitting long lines (using "-" at 
00032  *      the end of the line to indicate contiuation), and then source that 
00033  *      temp file.  We use special logic to make sure we don't continue in
00034  *      the middle of a quoted string.
00035  *
00036  * 05/04/94 (seiwald) - async multiprocess interface; noop on VMS
00037  * 12/20/96 (seiwald) - rewritten to handle multi-line commands well
00038  * 01/14/96 (seiwald) - don't put -'s between "'s
00039  */
00040 
00041 #define WRTLEN 240
00042 
00043 #define MIN( a, b )     ((a) < (b) ? (a) : (b))
00044 
00045 /* 1 for the @ and 4 for the .com */
00046 
00047 char tempnambuf[ L_tmpnam + 1 + 4 ] = {0};
00048 
00049 void
00050 execcmd( 
00051         char *string,
00052         void (*func)( void *closure, int status ),
00053         void *closure,
00054         LIST *shell )
00055 {
00056         char *s, *e, *p;
00057         int rstat = EXEC_CMD_OK;
00058         int status;
00059 
00060         /* See if string is more than one line */
00061         /* discounting leading/trailing white space */
00062 
00063         for( s = string; *s && isspace( *s ); s++ )
00064                 ;
00065 
00066         e = p = strchr( s, '\n' );
00067 
00068         while( p && isspace( *p ) )
00069                 ++p;
00070 
00071         /* If multi line or long, write to com file. */
00072         /* Otherwise, exec directly. */
00073 
00074         if( p && *p || e - s > WRTLEN )
00075         {
00076             FILE *f;
00077 
00078             /* Create temp file invocation "@sys$scratch:tempfile.com" */
00079 
00080             if( !*tempnambuf )
00081             {
00082                 tempnambuf[0] = '@';
00083                 (void)tmpnam( tempnambuf + 1 );
00084                 strcat( tempnambuf, ".com" );
00085             }
00086             
00087             /* Open tempfile */
00088 
00089             if( !( f = fopen( tempnambuf + 1, "w" ) ) )
00090             {
00091                 printf( "can't open command file\n" );
00092                 (*func)( closure, EXEC_CMD_FAIL );
00093                 return;
00094             }
00095 
00096             /* For each line of the string */
00097 
00098             while( *string )
00099             {
00100                 char *s = strchr( string, '\n' );
00101                 int len = s ? s + 1 - string : strlen( string );
00102 
00103                 fputc( '$', f );
00104 
00105                 /* For each chunk of a line that needs to be split */
00106 
00107                 while( len > 0 )
00108                 {
00109                     char *q = string;
00110                     char *qe = string + MIN( len, WRTLEN );
00111                     char *qq = q;
00112                     int quote = 0;
00113 
00114                     /* Look for matching "'s */
00115 
00116                     for( ; q < qe; q++ )
00117                         if( *q == '"' && ( quote = !quote ) )
00118                             qq = q;
00119 
00120                     /* Back up to opening quote, if in one */
00121 
00122                     if( quote )
00123                         q = qq;
00124 
00125                     fwrite( string, ( q - string ), 1, f );
00126 
00127                     len -= ( q - string );
00128                     string = q;
00129 
00130                     if( len )
00131                     {
00132                         fputc( '-', f );
00133                         fputc( '\n', f );
00134                     }
00135                 }
00136             }
00137 
00138             fclose( f );
00139 
00140             status = system( tempnambuf ) & 0x07;
00141 
00142             unlink( tempnambuf + 1 );
00143         }
00144         else
00145         {
00146             /* Execute single line command */
00147             /* Strip trailing newline before execing */
00148             if( e ) *e = 0;
00149             status = system( s ) & 0x07;
00150         }
00151 
00152         /* Fail for error or fatal error */
00153         /* OK on OK, warning, or info exit */
00154 
00155         if( status == 2 || status == 4 )
00156             rstat = EXEC_CMD_FAIL;
00157 
00158         (*func)( closure, rstat );
00159 }
00160 
00161 int 
00162 execwait()
00163 {
00164         return 0;
00165 }
00166 
00167 # endif /* VMS */

Generated on Mon Nov 8 17:07:32 2004 for MPT by  doxygen 1.3.9.1