summaryrefslogtreecommitdiff
path: root/execvms.c
blob: 729917d356e08587085fa1a16bd988cb5c7b6095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright 1993, 1995 Christopher Seiwald.
 *
 * This file is part of Jam - see jam.c for Copyright information.
 */

#include "jam.h"
#include "lists.h"
#include "execcmd.h"

#ifdef OS_VMS

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iodef.h>
#include <ssdef.h>
#include <descrip.h>
#include <dvidef.h>
#include <clidef.h>

/*
 * execvms.c - execute a shell script, ala VMS.
 *
 * The approach is this:
 *
 * If the command is a single line, and shorter than WRTLEN (what we believe to
 * be the maximum line length), we just system() it.
 *
 * If the command is multi-line, or longer than WRTLEN, we write the command
 * block to a temp file, splitting long lines (using "-" at the end of the line
 * to indicate contiuation), and then source that temp file. We use special
 * logic to make sure we do not continue in the middle of a quoted string.
 *
 * 05/04/94 (seiwald) - async multiprocess interface; noop on VMS
 * 12/20/96 (seiwald) - rewritten to handle multi-line commands well
 * 01/14/96 (seiwald) - do not put -'s between "'s
 */

#define WRTLEN 240

#define MIN( a, b ) ((a) < (b) ? (a) : (b))

/* 1 for the @ and 4 for the .com */

char tempnambuf[ L_tmpnam + 1 + 4 ] = { 0 };


void exec_cmd
(
    char * string,
    void (* func)( void * closure, int status, timing_info *, char *, char * ),
    void * closure,
    LIST * shell,
    char * rule_name,
    char * target
)
{
    char * s;
    char * e;
    cahr * p;
    int rstat = EXEC_CMD_OK;
    int status;

    /* See if string is more than one line discounting leading/trailing white
     * space.
     */
    for ( s = string; *s && isspace( *s ); ++s );

    e = p = strchr( s, '\n' );

    while ( p && isspace( *p ) )
        ++p;

    /* If multi line or long, write to com file. Otherwise, exec directly. */
    if ( ( p && *p ) || ( e - s > WRTLEN ) )
    {
        FILE * f;

        /* Create temp file invocation "@sys$scratch:tempfile.com". */
        if ( !*tempnambuf )
        {
            tempnambuf[0] = '@';
            (void)tmpnam( tempnambuf + 1 );
            strcat( tempnambuf, ".com" );
        }

        /* Open tempfile. */
        if ( !( f = fopen( tempnambuf + 1, "w" ) ) )
        {
            printf( "can't open command file\n" );
            (*func)( closure, EXEC_CMD_FAIL );
            return;
        }

        /* For each line of the string. */
        while ( *string )
        {
            char * s = strchr( string, '\n' );
            int len = s ? s + 1 - string : strlen( string );

            fputc( '$', f );

            /* For each chunk of a line that needs to be split. */
            while ( len > 0 )
            {
                char * q = string;
                char * qe = string + MIN( len, WRTLEN );
                char * qq = q;
                int quote = 0;

                /* Look for matching "s. */
                for ( ; q < qe; ++q )
                    if ( ( *q == '"' ) && ( quote = !quote ) )
                        qq = q;

                /* Back up to opening quote, if in one. */
                if ( quote )
                    q = qq;

                fwrite( string, ( q - string ), 1, f );

                len -= ( q - string );
                string = q;

                if ( len )
                {
                    fputc( '-', f );
                    fputc( '\n', f );
                }
            }
        }

        fclose( f );

        status = system( tempnambuf ) & 0x07;

        unlink( tempnambuf + 1 );
    }
    else
    {
        /* Execute single line command. Strip trailing newline before execing.
         */
        if ( e ) *e = 0;
        status = system( s ) & 0x07;
    }

    /* Fail for error or fatal error. OK on OK, warning or info exit. */
    if ( ( status == 2 ) || ( status == 4 ) )
        rstat = EXEC_CMD_FAIL;

    (*func)( closure, rstat );
}


int exec_wait()
{
    return 0;
}

# endif /* VMS */