ast_comparator/test/student_data/handout/shlab-handout/cases/53_tsh.c
/*
 * parse_line - Parse the command line and build the argv array.
 *
 * Characters enclosed in single quotes are treated as a single
 * argument.  Return true if the user has requested a BG job, false if
 * the user has requested a FG job.
 */
Parse the command line and build the argv array.
Characters enclosed in single quotes are treated as a single
argument.  Return true if the user has requested a BG job, false if
the user has requested a FG job.
Parse the command line and build the argv array . Characters enclosed in single quotes are treated as a single argument . Return true if the user has requested a BG job false if the user has requested a FG job .
void parse_line(const char *cmdline, char **argv, bool *bg)
{
	static char array[MAXLINE]; 
	char *buf = array;          
	char *delim;                
	int argc;                   
	bool background;            
	if (argv == NULL)
		return;
	strcpy(buf, cmdline);
	buf[strlen(buf) - 1] = ' '; 
	while (*buf && (*buf == ' ')) 
		buf++;
	argc = 0;
	if (*buf == '\'')
	{
		buf++;
		delim = strchr(buf, '\'');
	}
	else
	{
		delim = strchr(buf, ' ');
	}
	while (delim)
	{
		argv[argc++] = buf;
		*delim = '\0';
		buf = delim + 1;
		while (*buf && (*buf == ' ')) 
			buf++;
		if (*buf == '\'')
		{
			buf++;
			delim = strchr(buf, '\'');
		}
		else
		{
			delim = strchr(buf, ' ');
		}
		if (argc >= MAXARGS) 
			break;
	}
	argv[argc] = NULL;
	if (argc == 0)  
		background = true;
	else if ((background = (*argv[argc - 1] == '&')))
	{
		argv[--argc] = NULL;
	}
	if (bg != NULL)
		*bg = background;
}
void parse_line ( const char * cmdline , char * * argv , bool * bg ) { static char array [ MAXLINE ] ; char * buf = array ; char * delim ; int argc ; bool background ; if ( argv == NULL ) return ; strcpy ( buf , cmdline ) ; buf [ strlen ( buf ) - 1 ] = ' ' ; while ( * buf && ( * buf == ' ' ) ) buf ++ ; argc = 0 ; if ( * buf == ' \' ' ) { buf ++ ; delim = strchr ( buf , ' \' ' ) ; } else { delim = strchr ( buf , ' ' ) ; } while ( delim ) { argv [ argc ++ ] = buf ; * delim = ' \0 ' ; buf = delim + 1 ; while ( * buf && ( * buf == ' ' ) ) buf ++ ; if ( * buf == ' \' ' ) { buf ++ ; delim = strchr ( buf , ' \' ' ) ; } else { delim = strchr ( buf , ' ' ) ; } if ( argc >= MAXARGS ) break ; } argv [ argc ] = NULL ; if ( argc == 0 ) background = true ; else if ( ( background = ( * argv [ argc - 1 ] == ' ' ) ) ) { argv [ -- argc ] = NULL ; } if ( bg != NULL ) * bg = background ; }
parse_line - Parse the command line and build the argv array.
parse_line - Parse the command line and build the argv array .
/* holds local copy of command line */ /* ptr that traverses command line */ /* points to first space delimiter */ /* number of args */ /* background job? */ /* replace trailing '\n' with space */ /* ignore leading spaces */ /* Build the argv list */ /* ignore spaces */ /* to prevent buffer overflow */ /* ignore blank line */ /* should the job run in the background? */