1 /*
2  * Copyright 1993, 1995 Christopher Seiwald.
3  *
4  * This file is part of Jam - see jam.c for Copyright information.
5  */
6 
7 /*
8  * command.c - maintain lists of commands
9  *
10  * 01/20/00 (seiwald) - Upgraded from K&R to ANSI C
11  * 09/08/00 (seiwald) - bulletproof PIECEMEAL size computation
12  */
13 
14 # include "jam.h"
15 
16 # include "lists.h"
17 # include "parse.h"
18 # include "variable.h"
19 # include "rules.h"
20 
21 # include "command.h"
22 
23 /*
24  * cmd_new() - return a new CMD or 0 if too many args
25  */
26 
27 CMD *
cmd_new(RULE * rule,LIST * targets,LIST * sources,LIST * shell,int maxline)28 cmd_new(
29 	RULE	*rule,
30 	LIST	*targets,
31 	LIST	*sources,
32 	LIST	*shell,
33 	int	maxline )
34 {
35 	CMD *cmd = (CMD *)malloc( sizeof( CMD ) );
36 
37 	cmd->rule = rule;
38 	cmd->shell = shell;
39 	cmd->next = 0;
40 	cmd->buf = (char *)malloc( maxline );
41 
42 	lol_init( &cmd->args );
43 	lol_add( &cmd->args, targets );
44 	lol_add( &cmd->args, sources );
45 
46 	/* Bail if the result won't fit in maxline */
47 	/* We don't free targets/sources/shell if bailing. */
48 
49 	if( var_string( rule->actions, cmd->buf, maxline, &cmd->args ) < 0 )
50 	{
51 	    cmd_free( cmd );
52 	    return 0;
53 	}
54 
55 	return cmd;
56 }
57 
58 /*
59  * cmd_free() - free a CMD
60  */
61 
62 void
cmd_free(CMD * cmd)63 cmd_free( CMD *cmd )
64 {
65 	lol_free( &cmd->args );
66 	list_free( cmd->shell );
67 	free( (char *)cmd->buf );
68 	free( (char *)cmd );
69 }
70