[29] | 1 | /* |
---|
| 2 | * Copyright 1993, 1995 Christopher Seiwald. |
---|
| 3 | * |
---|
| 4 | * This file is part of Jam - see jam.c for Copyright information. |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /* This file is ALSO: |
---|
| 8 | * Copyright 2001-2004 David Abrahams. |
---|
| 9 | * Distributed under the Boost Software License, Version 1.0. |
---|
| 10 | * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | /* |
---|
| 14 | * command.c - maintain lists of commands |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | # include "jam.h" |
---|
| 18 | |
---|
| 19 | # include "lists.h" |
---|
| 20 | # include "parse.h" |
---|
| 21 | # include "variable.h" |
---|
| 22 | # include "rules.h" |
---|
| 23 | # include "debug.h" |
---|
| 24 | |
---|
| 25 | # include "command.h" |
---|
| 26 | # include <limits.h> |
---|
| 27 | # include <string.h> |
---|
| 28 | |
---|
| 29 | /* |
---|
| 30 | * cmd_new() - return a new CMD or 0 if too many args |
---|
| 31 | */ |
---|
| 32 | |
---|
| 33 | CMD * |
---|
| 34 | cmd_new( |
---|
| 35 | RULE *rule, |
---|
| 36 | LIST *targets, |
---|
| 37 | LIST *sources, |
---|
| 38 | LIST *shell ) |
---|
| 39 | { |
---|
| 40 | CMD *cmd = (CMD *)malloc( sizeof( CMD ) ); |
---|
| 41 | /* lift line-length limitation entirely when JAMSHELL is just "%" */ |
---|
| 42 | int no_limit = ( shell && !strcmp(shell->string,"%") && !list_next(shell) ); |
---|
| 43 | int max_line = MAXLINE; |
---|
| 44 | int allocated = -1; |
---|
| 45 | |
---|
| 46 | if ( DEBUG_PROFILE ) |
---|
| 47 | profile_memory( sizeof( CMD ) ); |
---|
| 48 | |
---|
| 49 | cmd->rule = rule; |
---|
| 50 | cmd->shell = shell; |
---|
| 51 | cmd->next = 0; |
---|
| 52 | |
---|
| 53 | lol_init( &cmd->args ); |
---|
| 54 | lol_add( &cmd->args, targets ); |
---|
| 55 | lol_add( &cmd->args, sources ); |
---|
| 56 | cmd->buf = 0; |
---|
| 57 | |
---|
| 58 | do |
---|
| 59 | { |
---|
| 60 | free(cmd->buf); /* free any buffer from previous iteration */ |
---|
| 61 | |
---|
| 62 | cmd->buf = (char*)malloc(max_line + 1); |
---|
| 63 | |
---|
| 64 | if ( DEBUG_PROFILE ) |
---|
| 65 | profile_memory( max_line + 1 ); |
---|
| 66 | |
---|
| 67 | if (cmd->buf == 0) |
---|
| 68 | break; |
---|
| 69 | |
---|
| 70 | allocated = var_string( rule->actions->command, cmd->buf, max_line, &cmd->args ); |
---|
| 71 | |
---|
| 72 | max_line = max_line * 2; |
---|
| 73 | } |
---|
| 74 | while( allocated < 0 && max_line < INT_MAX / 2 ); |
---|
| 75 | |
---|
| 76 | if ( !no_limit ) |
---|
| 77 | { |
---|
| 78 | /* Bail if the result won't fit in MAXLINE */ |
---|
| 79 | char *s = cmd->buf; |
---|
| 80 | while ( *s ) |
---|
| 81 | { |
---|
| 82 | size_t l = strcspn( s, "\n" ); |
---|
| 83 | |
---|
| 84 | if ( l > MAXLINE ) |
---|
| 85 | { |
---|
| 86 | /* We don't free targets/sources/shell if bailing. */ |
---|
| 87 | cmd_free( cmd ); |
---|
| 88 | return 0; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | s += l; |
---|
| 92 | if ( *s ) |
---|
| 93 | ++s; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | return cmd; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /* |
---|
| 101 | * cmd_free() - free a CMD |
---|
| 102 | */ |
---|
| 103 | |
---|
| 104 | void |
---|
| 105 | cmd_free( CMD *cmd ) |
---|
| 106 | { |
---|
| 107 | lol_free( &cmd->args ); |
---|
| 108 | list_free( cmd->shell ); |
---|
| 109 | free( cmd->buf ); |
---|
| 110 | free( (char *)cmd ); |
---|
| 111 | } |
---|