computer-system/CMU15-213/labs/LAB5_tshlab/tsh.c
int parseline(const char *cmdline, char **argv) {
    static char array[MAXLINE]; /* holds local copy of command line */
    char *buf = array;          /* ptr that traverses command line */
    char *delim;                /* points to first space delimiter */
    int argc;                   /* number of args */
    int bg;                     /* background job? */

    strcpy(buf, cmdline);
    buf[strlen(buf)-1] = ' ';  /* replace trailing '\n' with space */
    while (*buf && (*buf == ' ')) /* ignore leading spaces */
	buf++;

    /* Build the argv list */
    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 == ' ')) /* ignore spaces */
            buf++;

        if (*buf == '\'') {
            buf++;
            delim = strchr(buf, '\'');
        }
        else {
            delim = strchr(buf, ' ');
        }
    }
    argv[argc] = NULL;
    
    if (argc == 0)  /* ignore blank line */
	return 1;

    /* should the job run in the background? */
    if ((bg = (*argv[argc-1] == '&')) != 0) {
	    argv[--argc] = NULL;
    }
    return bg;
}
/* 
 * parseline - 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 .
int parseline(const char *cmdline, char **argv) {
    static char array[MAXLINE]; 
    char *buf = array;          
    char *delim;                
    int argc;                   
    int bg;                     
    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, ' ');
        }
    }
    argv[argc] = NULL;
    if (argc == 0)  
	return 1;
    if ((bg = (*argv[argc-1] == '&')) != 0) {
	    argv[--argc] = NULL;
    }
    return bg;
}
int parseline ( const char * cmdline , char * * argv ) { static char array [ MAXLINE ] ; char * buf = array ; char * delim ; int argc ; int bg ; 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 , ' ' ) ; } } 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 .
/* 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 */ /* ignore blank line */ /* should the job run in the background? */