The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

command.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 1993, 1995 Christopher Seiwald.
00003  *
00004  * This file is part of Jam - see jam.c for Copyright information.
00005  */
00006 
00007 /*  This file is ALSO:
00008  *  (C) Copyright David Abrahams 2001. Permission to copy, use,
00009  *  modify, sell and distribute this software is granted provided this
00010  *  copyright notice appears in all copies. This software is provided
00011  *  "as is" without express or implied warranty, and with no claim as
00012  *  to its suitability for any purpose.
00013  */
00014 
00015 /*
00016  * command.c - maintain lists of commands
00017  */
00018 
00019 # include "jam.h"
00020 
00021 # include "lists.h"
00022 # include "parse.h"
00023 # include "variable.h"
00024 # include "rules.h"
00025 
00026 # include "command.h"
00027 # include <limits.h>
00028 # include <string.h>
00029 
00030 /*
00031  * cmd_new() - return a new CMD or 0 if too many args
00032  */
00033 
00034 CMD *
00035 cmd_new(
00036         RULE    *rule,
00037         LIST    *targets,
00038         LIST    *sources,
00039         LIST    *shell )
00040 {
00041     CMD *cmd = (CMD *)malloc( sizeof( CMD ) );
00042     /* lift line-length limitation entirely when JAMSHELL is just "%" */
00043     int no_limit = ( shell && !strcmp(shell->string,"%") && !list_next(shell) );
00044     int max_line = MAXLINE;
00045     int allocated = -1;
00046 
00047     cmd->rule = rule;
00048     cmd->shell = shell;
00049     cmd->next = 0;
00050 
00051     lol_init( &cmd->args );
00052     lol_add( &cmd->args, targets );
00053     lol_add( &cmd->args, sources );
00054     cmd->buf = 0;
00055 
00056     do
00057     {
00058         free(cmd->buf); /* free any buffer from previous iteration */
00059         
00060         cmd->buf = (char*)malloc(max_line + 1);
00061         
00062         if (cmd->buf == 0)
00063             break;
00064         
00065         allocated = var_string( rule->actions->command, cmd->buf, max_line, &cmd->args );
00066         
00067         max_line = max_line * 2;
00068     }
00069     while( allocated < 0 && max_line < INT_MAX / 2 );
00070 
00071     if ( !no_limit )
00072     {
00073         /* Bail if the result won't fit in MAXLINE */
00074         char *s = cmd->buf;
00075         while ( *s )
00076         {
00077             size_t l = strcspn( s, "\n" );
00078             
00079             if ( l > MAXLINE )
00080             {
00081                 /* We don't free targets/sources/shell if bailing. */
00082                 cmd_free( cmd );
00083                 return 0;
00084             }
00085             
00086             s += l;
00087             if ( *s )
00088                 ++s;
00089         }
00090     }
00091 
00092     return cmd;
00093 }
00094 
00095 /*
00096  * cmd_free() - free a CMD
00097  */
00098 
00099 void
00100 cmd_free( CMD *cmd )
00101 {
00102         lol_free( &cmd->args );
00103         list_free( cmd->shell );
00104     free( cmd->buf );
00105         free( (char *)cmd );
00106 }

Generated on Mon Nov 8 17:07:32 2004 for MPT by  doxygen 1.3.9.1