| Line | |
|---|
| 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 | |
|---|
| 24 | # include "command.h" |
|---|
| 25 | # include <limits.h> |
|---|
| 26 | # include <string.h> |
|---|
| 27 | |
|---|
| 28 | /* |
|---|
| 29 | * cmd_new() - return a new CMD or 0 if too many args |
|---|
| 30 | */ |
|---|
| 31 | |
|---|
| 32 | CMD * |
|---|
| 33 | cmd_new( |
|---|
| 34 | RULE *rule, |
|---|
| 35 | LIST *targets, |
|---|
| 36 | LIST *sources, |
|---|
| 37 | LIST *shell ) |
|---|
| 38 | { |
|---|
| 39 | CMD *cmd = (CMD *)malloc( sizeof( CMD ) ); |
|---|
| 40 | /* lift line-length limitation entirely when JAMSHELL is just "%" */ |
|---|
| 41 | int no_limit = ( shell && !strcmp(shell->string,"%") && !list_next(shell) ); |
|---|
| 42 | int max_line = MAXLINE; |
|---|
| 43 | int allocated = -1; |
|---|
| 44 | |
|---|
| 45 | cmd->rule = rule; |
|---|
| 46 | cmd->shell = shell; |
|---|
| 47 | cmd->next = 0; |
|---|
| 48 | |
|---|
| 49 | lol_init( &cmd->args ); |
|---|
| 50 | lol_add( &cmd->args, targets ); |
|---|
| 51 | lol_add( &cmd->args, sources ); |
|---|
| 52 | cmd->buf = 0; |
|---|
| 53 | |
|---|
| 54 | do |
|---|
| 55 | { |
|---|
| 56 | free(cmd->buf); /* free any buffer from previous iteration */ |
|---|
| 57 | |
|---|
| 58 | cmd->buf = (char*)malloc(max_line + 1); |
|---|
| 59 | |
|---|
| 60 | if (cmd->buf == 0) |
|---|
| 61 | break; |
|---|
| 62 | |
|---|
| 63 | allocated = var_string( rule->actions->command, cmd->buf, max_line, &cmd->args ); |
|---|
| 64 | |
|---|
| 65 | max_line = max_line * 2; |
|---|
| 66 | } |
|---|
| 67 | while( allocated < 0 && max_line < INT_MAX / 2 ); |
|---|
| 68 | |
|---|
| 69 | if ( !no_limit ) |
|---|
| 70 | { |
|---|
| 71 | /* Bail if the result won't fit in MAXLINE */ |
|---|
| 72 | char *s = cmd->buf; |
|---|
| 73 | while ( *s ) |
|---|
| 74 | { |
|---|
| 75 | size_t l = strcspn( s, "\n" ); |
|---|
| 76 | |
|---|
| 77 | if ( l > MAXLINE ) |
|---|
| 78 | { |
|---|
| 79 | /* We don't free targets/sources/shell if bailing. */ |
|---|
| 80 | cmd_free( cmd ); |
|---|
| 81 | return 0; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | s += l; |
|---|
| 85 | if ( *s ) |
|---|
| 86 | ++s; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | return cmd; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /* |
|---|
| 94 | * cmd_free() - free a CMD |
|---|
| 95 | */ |
|---|
| 96 | |
|---|
| 97 | void |
|---|
| 98 | cmd_free( CMD *cmd ) |
|---|
| 99 | { |
|---|
| 100 | lol_free( &cmd->args ); |
|---|
| 101 | list_free( cmd->shell ); |
|---|
| 102 | free( cmd->buf ); |
|---|
| 103 | free( (char *)cmd ); |
|---|
| 104 | } |
|---|
Note: See
TracBrowser
for help on using the repository browser.