ast_comparator/test/student_data/handout/shlab-handout/cases/reference_22_tsh.c
/* 
 * parseline - Parse the command line and build the argv array.
 *
 * Requires:
 *   "cmdline" is a NUL ('\0') terminated string with a trailing
 *   '\n' character.  "cmdline" must contain less than MAXARGS
 *   arguments.
 *
 * Effects:
 *   Builds "argv" array from space delimited arguments on the command line.
 *   The final element of "argv" is set to NULL.  Characters enclosed in
 *   single quotes are treated as a single argument.  Returns true if
 *   the user has requested a BG job and false if the user has requested
 *   a FG job.
 */
Parse the command line and build the argv array.
Requires:
"cmdline" is a NUL ('\0') terminated string with a trailing
'\n' character.  "cmdline" must contain less than MAXARGS
arguments.

Builds "argv" array from space delimited arguments on the command line.
The final element of "argv" is set to NULL.  Characters enclosed in
single quotes are treated as a single argument.  Returns true if
the user has requested a BG job and false if the user has requested
a FG job.
Parse the command line and build the argv array . Requires : " cmdline " is a NUL ( ' \ 0 ' ) terminated string with a trailing ' \ n ' character . " cmdline " must contain less than MAXARGS arguments . Builds " argv " array from space delimited arguments on the command line . The final element of " argv " is set to NULL . Characters enclosed in single quotes are treated as a single argument . Returns true if the user has requested a BG job and false if the user has requested a FG job .
static int
parseline(const char *cmdline, char **argv) 
{
	int argc;                   
	int bg;                     
	static char array[MAXLINE]; 
	char *buf = array;          
	char *delim;                
	strcpy(buf, cmdline);
	buf[strlen(buf) - 1] = ' ';
	while (*buf != '\0' && *buf == ' ')
		buf++;
	argc = 0;
	if (*buf == '\'') {
		buf++;
		delim = strchr(buf, '\'');
	} else
		delim = strchr(buf, ' ');
	while (delim != NULL) {
		argv[argc++] = buf;
		*delim = '\0';
		buf = delim + 1;
		while (*buf != '\0' && *buf == ' ')	
			buf++;
		if (*buf == '\'') {
			buf++;
			delim = strchr(buf, '\'');
		} else
			delim = strchr(buf, ' ');
	}
	argv[argc] = NULL;
	if (argc == 0)
		return (1);
	if ((bg = (*argv[argc - 1] == '&')) != 0)
		argv[--argc] = NULL;
	return (bg);
}
static int parseline ( const char * cmdline , char * * argv ) { int argc ; int bg ; static char array [ MAXLINE ] ; char * buf = array ; char * delim ; strcpy ( buf , cmdline ) ; buf [ strlen ( buf ) - 1 ] = ' ' ; while ( * buf != ' \0 ' && * buf == ' ' ) buf ++ ; argc = 0 ; if ( * buf == ' \' ' ) { buf ++ ; delim = strchr ( buf , ' \' ' ) ; } else delim = strchr ( buf , ' ' ) ; while ( delim != NULL ) { argv [ argc ++ ] = buf ; * delim = ' \0 ' ; buf = delim + 1 ; while ( * buf != ' \0 ' && * buf == ' ' ) buf ++ ; if ( * buf == ' \' ' ) { buf ++ ; delim = strchr ( buf , ' \' ' ) ; } else delim = strchr ( buf , ' ' ) ; } argv [ argc ] = NULL ; if ( argc == 0 ) return ( 1 ) ; if ( ( bg = ( * argv [ argc - 1 ] == ' ' ) ) != 0 ) argv [ -- argc ] = NULL ; return ( bg ) ; }
parseline - Parse the command line and build the argv array.
parseline - Parse the command line and build the argv array .
// number of args // background job? // local copy of command line // ptr that traverses command line // points to first space delimiter // Replace trailing '\n' with space. // Ignore leading spaces. // Build the argv list. // Ignore spaces. // Ignore blank line. // Should the job run in the background?