summaryrefslogtreecommitdiffstats
path: root/src/boost/tools/build/src/engine/execvms.cpp
blob: 05b6c7a209c68c2b85cf27ec5d3f35d2b6d41020 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Copyright 1993, 1995 Christopher Seiwald.
 *
 * This file is part of Jam - see jam.c for Copyright information.
 */

/*  This file is ALSO:
 *  Copyright 2001-2004 David Abrahams.
 *  Copyright 2015 Artur Shepilko.
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
 */


/*
 * 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
 * 01/19/15 (shepilko)- adapt for jam-3.1.19
 */

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

#ifdef OS_VMS

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <times.h>
#include <unistd.h>
#include <errno.h>


#define WRTLEN 240

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

#define CHAR_DQUOTE  '"'

#define VMS_PATH_MAX 1024
#define VMS_COMMAND_MAX  1024

#define VMS_WARNING 0
#define VMS_SUCCESS 1
#define VMS_ERROR   2
#define VMS_FATAL   4

char commandbuf[ VMS_COMMAND_MAX ] = { 0 };


static int get_status(int vms_status);
static clock_t get_cpu_time();

/*
 * exec_check() - preprocess and validate the command.
 */

int exec_check
(
    string const * command,
    LIST * * pShell,
    int32_t * error_length,
    int32_t * error_max_length
)
{
    int const is_raw_cmd = 1;

    /* We allow empty commands for non-default shells since we do not really
     * know what they are going to do with such commands.
     */
    if ( !command->size && ( is_raw_cmd || list_empty( *pShell ) ) )
        return EXEC_CHECK_NOOP;

    return is_raw_cmd
        ? EXEC_CHECK_OK
        : check_cmd_for_too_long_lines( command->value, shell_maxline(), error_length,
            error_max_length );
}


/*
 * exec_cmd() - execute system command.
 */

void exec_cmd
(
    string const * command,
    int flags,
    ExecCmdCallback func,
    void * closure,
    LIST * shell
)
{
    char * s;
    char * e;
    char * p;
    int vms_status;
    int status;
    int rstat = EXEC_CMD_OK;
    int exit_reason = EXIT_OK;
    timing_info time_info;
    timestamp start_dt;
    struct tms start_time;
    struct tms end_time;
    char * cmd_string = command->value;


    /* Start the command */

    timestamp_current( &time_info.start );
    times( &start_time );

    /* See if command is more than one line discounting leading/trailing white
     * space.
     */
    for ( s = cmd_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. */

        if ( !*commandbuf )
        {
            OBJECT * tmp_filename = 0;

            tmp_filename = path_tmpfile();


            /* Get tmp file name is VMS-format. */
            {
                string os_filename[ 1 ];
                string_new( os_filename );
                path_translate_to_os( object_str( tmp_filename ), os_filename );
                object_free( tmp_filename );
                tmp_filename = object_new( os_filename->value );
                string_free( os_filename );
            }

            commandbuf[0] = '@';
            strncat( commandbuf + 1, object_str( tmp_filename ),
                     VMS_COMMAND_MAX - 2);
        }


        /* Open tempfile. */
        if ( !( f = fopen( commandbuf + 1, "w" ) ) )
        {
            err_printf( "[errno %d] failed to wite cmd_string file '%s': %s",
                errno, commandbuf + 1, strerror(errno) );
            rstat = EXEC_CMD_FAIL;
            exit_reason = EXIT_FAIL;

            times( &end_time );

            timestamp_current( &time_info.end );
            time_info.system = (double)( end_time.tms_cstime -
                start_time.tms_cstime ) / 100.;
            time_info.user   = (double)( end_time.tms_cutime -
                start_time.tms_cutime ) / 100.;

            (*func)( closure, rstat, &time_info, "" , "", exit_reason  );
            return;
        }


        /* Running from TMP, so explicitly set default to CWD. */
        {
            char * cwd = NULL;
            int cwd_buf_size = VMS_PATH_MAX;

            while ( !(cwd = getcwd( NULL, cwd_buf_size ) ) /* alloc internally */
                     && errno == ERANGE )
            {
                cwd_buf_size += VMS_PATH_MAX;
            }

            if ( !cwd )
            {
                errno_puts( "can not get current working directory" );
                b2::clean_exit( EXITBAD );
            }

            fprintf( f, "$ SET DEFAULT %s\n", cwd);

            free( cwd );
        }


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

            fputc( '$', f );

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

                /* Look for matching "s -- expected in the same line. */
                for ( ; q < qe; ++q )
                    if ( ( *q == CHAR_DQUOTE ) && ( quote = !quote ) )
                        qq = q;

                /* When needs splitting and is inside an open quote,
                 * back up to opening quote and split off at it.
                 * When the quoted string spans over a chunk,
                 * pass string as a whole.
                 * If no matching quote found, dump the rest of command.
                 */
                if (  len > WRTLEN && quote )
                {
                    q = qq;

                    if ( q == cmd_string )
                    {
                        for ( q = qe; q < ( cmd_string + len )
                                      && *q != CHAR_DQUOTE ; ++q) {}
                        q = ( *q == CHAR_DQUOTE) ? ( q + 1 ) : ( cmd_string + len );
                    }
                }

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

                len -= ( q - cmd_string );
                cmd_string = q;

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

        fclose( f );

        if ( DEBUG_EXECCMD )
        {
            FILE * f;
            char buf[ WRTLEN + 1 ] = { 0 };

            if ( (f = fopen( commandbuf + 1, "r" ) ) )
            {
                int nbytes;
                printf( "Command file: %s\n", commandbuf + 1 );

                do
                {
                    nbytes = fread( buf, sizeof( buf[0] ), sizeof( buf ) - 1, f );

                    if ( nbytes ) fwrite(buf, sizeof( buf[0] ), nbytes, stdout);
                }
                while ( !feof(f) );

                fclose(f);
            }
        }

        /* Execute command file */
        vms_status = system( commandbuf );
        status = get_status( vms_status );

        unlink( commandbuf + 1 );
    }
    else
    {
        /* Execute single line command. Strip trailing newline before execing.
         * TODO:Call via popen() with capture of the output may be better here.
         */
        if ( e ) *e = 0;

        status = VMS_SUCCESS;  /* success on empty command */
        if ( *s )
        {
            vms_status = system( s );
            status = get_status( vms_status );
        }
    }


    times( &end_time );

    timestamp_current( &time_info.end );
    time_info.system = (double)( end_time.tms_cstime -
        start_time.tms_cstime ) / 100.;
    time_info.user   = (double)( end_time.tms_cutime -
        start_time.tms_cutime ) / 100.;


    /* Fail for error or fatal error. OK on OK, warning or info exit. */
    if ( ( status == VMS_ERROR ) || ( status == VMS_FATAL ) )
    {
        rstat = EXEC_CMD_FAIL;
        exit_reason = EXIT_FAIL;
    }

    (*func)( closure, rstat, &time_info, "" , "", exit_reason  );
}


void exec_wait()
{
    return;
}


/* get_status() - returns status of the VMS command execution.
   - Map VMS status to its severity (lower 3-bits)
   - W-DCL-IVVERB is returned on unrecognized command -- map to general ERROR
*/
int get_status( int vms_status )
{
#define VMS_STATUS_DCL_IVVERB 0x00038090

    int status;

    switch (vms_status)
    {
    case VMS_STATUS_DCL_IVVERB:
        status = VMS_ERROR;
        break;

    default:
        status = vms_status & 0x07; /* $SEVERITY bits */
    }

    return status;
}


#define __NEW_STARLET 1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ssdef.h>
#include <stsdef.h>
#include <jpidef.h>
#include <efndef.h>
#include <iosbdef.h>
#include <iledef.h>
#include <lib$routines.h>
#include <starlet.h>


/*
 * get_cpu_time() - returns CPU time in CLOCKS_PER_SEC since process start.
 * on error returns (clock_t)-1.
 *
 * Intended to emulate (system + user) result of *NIX times(), if CRTL times()
 * is not available.
*  However, this accounts only for the current process. To account for child
*  processes, these need to be directly spawned/forked via exec().
*  Moreover, child processes should be running a C main program or a program
*  that calls VAXC$CRTL_INIT or DECC$CRTL_INIT.
*/

clock_t get_cpu_time()
{
    clock_t result = (clock_t) 0;

    IOSB iosb;
    int status;
    long cputime = 0;


    ILE3 jpi_items[] = {
        { sizeof( cputime ), JPI$_CPUTIM, &cputime, NULL }, /* longword int, 10ms */
        { 0 },
    };

    status = sys$getjpiw (EFN$C_ENF, 0, 0, jpi_items, &iosb, 0, 0);

    if ( !$VMS_STATUS_SUCCESS( status ) )
    {
        lib$signal( status );

        result = (clock_t) -1;
        return result;
    }


    result = ( cputime / 100 ) * CLOCKS_PER_SEC;

    return result;
}

int32_t shell_maxline()
{
    return MAXLINE;
}


# endif /* VMS */