summaryrefslogtreecommitdiff
path: root/tools/build/src/engine/execcmd.c
blob: f751cbff3369a5adcfe4831e6738c00ab24ad1f2 (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
/*
 * Copyright 1993, 1995 Christopher Seiwald.
 * Copyright 2007 Noel Belcourt.
 *
 *   Utility functions shared between different exec*.c platform specific
 * implementation modules.
 *
 * This file is part of Jam - see jam.c for Copyright information.
 */

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

#include <assert.h>
#include <stdio.h>
#include <string.h>


/* Internal interrupt counter. */
static int intr;


/* Constructs a list of command-line elements using the format specified by the
 * given shell list.
 *
 * Given argv array should have at least MAXARGC + 1 elements.
 * Slot numbers may be between 0 and 998 (inclusive).
 *
 * Constructed argv list will be zero terminated. Character arrays referenced by
 * the argv structure elements will be either elements from the give shell list,
 * internal static buffers or the given command string and should thus not
 * considered owned by or released via the argv structure and should be
 * considered invalidated by the next argv_from_shell() call.
 *
 * Shell list elements:
 *   - Starting with '%' - represent the command string.
 *   - Starting with '!' - represent the slot number (increased by one).
 *   - Anything else - used as a literal.
 *   - If no '%' element is found, the command string is appended as an extra.
 */

void argv_from_shell( char const * * argv, LIST * shell, char const * command,
    int const slot )
{
    static char jobno[ 4 ];

    int i;
    int gotpercent = 0;
    LISTITER iter = list_begin( shell );
    LISTITER end = list_end( shell );

    assert( 0 <= slot );
    assert( slot < 999 );
    sprintf( jobno, "%d", slot + 1 );

    for ( i = 0; iter != end && i < MAXARGC; ++i, iter = list_next( iter ) )
    {
        switch ( object_str( list_item( iter ) )[ 0 ] )
        {
            case '%': argv[ i ] = command; ++gotpercent; break;
            case '!': argv[ i ] = jobno; break;
            default : argv[ i ] = object_str( list_item( iter ) );
        }
    }

    if ( !gotpercent )
        argv[ i++ ] = command;

    argv[ i ] = NULL;
}


/* Returns whether the given command string contains lines longer than the given
 * maximum.
 */
int check_cmd_for_too_long_lines( char const * command, int const max,
    int * const error_length, int * const error_max_length )
{
    while ( *command )
    {
        size_t const l = strcspn( command, "\n" );
        if ( l > max )
        {
            *error_length = l;
            *error_max_length = max;
            return EXEC_CHECK_LINE_TOO_LONG;
        }
        command += l;
        if ( *command )
            ++command;
    }
    return EXEC_CHECK_OK;
}


/* Checks whether the given shell list is actually a request to execute raw
 * commands without an external shell.
 */
int is_raw_command_request( LIST * shell )
{
    return !list_empty( shell ) &&
        !strcmp( object_str( list_front( shell ) ), "%" ) &&
        list_next( list_begin( shell ) ) == list_end( shell );
}


/* Returns whether an interrupt has been detected so far. */

int interrupted( void )
{
    return intr != 0;
}


/* Internal interrupt handler. */

void onintr( int disp )
{
    ++intr;
    printf( "...interrupted\n" );
}