Skip to content
Snippets Groups Projects
Select Git revision
  • 0262dc9a248db6522abdae4297ca2dfba2be1aed
  • main default
2 results

questions.md

Blame
  • dshlib.c 2.72 KiB
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #include "dshlib.h"
    
    /*
     *  build_cmd_list
     *    cmd_line:     the command line from the user
     *    clist *:      pointer to clist structure to be populated
     *
     *  This function builds the command_list_t structure passed by the caller
     *  It does this by first splitting the cmd_line into commands by spltting
     *  the string based on any pipe characters '|'.  It then traverses each
     *  command.  For each command (a substring of cmd_line), it then parses
     *  that command by taking the first token as the executable name, and
     *  then the remaining tokens as the arguments.
     *
     *  NOTE your implementation should be able to handle properly removing
     *  leading and trailing spaces!
     *
     *  errors returned:
     *
     *    OK:                      No Error
     *    ERR_TOO_MANY_COMMANDS:   There is a limit of CMD_MAX (see dshlib.h)
     *                             commands.
     *    ERR_CMD_OR_ARGS_TOO_BIG: One of the commands provided by the user
     *                             was larger than allowed, either the
     *                             executable name, or the arg string.
     *
     *  Standard Library Functions You Might Want To Consider Using
     *      memset(), strcmp(), strcpy(), strtok(), strlen(), strchr()
     */
    // int build_cmd_list(char *cmd_line, command_list_t *clist)
    // {
    //     printf(M_NOT_IMPL);
    //     return EXIT_NOT_IMPL;
    // }
    
    int build_cmd_list(char *cmd_line, command_list_t *clist)
    {       
        if ( cmd_line == NULL || strlen(cmd_line) == 0) //is null? or empty?
        {
            return WARN_NO_CMDS; //if no command is given
        }
        char *token; //holds each command that's parsed
        char *rest = cmd_line; //track string
        int num_commands = 0;     //count the # of parsed commands
    
        while (( token = strtok_r( rest, PIPE_STRING, &rest ) ) != NULL) //tokenize input
        {
            if (num_commands >= CMD_MAX) //make sure commands don't exceed max. allowed
            {
                return ERR_TOO_MANY_COMMANDS;
             }
    
            //trim all type of spaces
            while ( *token == SPACE_CHAR ) token++; 
            char *end = token + strlen(token) - 1;
            while ( end > token && *end == SPACE_CHAR ) *end-- = '\0';
    
            //get executable name and arguments
            char *arg_pointer = strchr(token, SPACE_CHAR);
            if (arg_pointer)
            { //separae and move
                *arg_pointer = '\0'; 
                arg_pointer++;        
            }
    
            //storage command
            strncpy (clist->commands[num_commands].exe, token, EXE_MAX);
            if (arg_pointer)
            {
                strncpy(clist->commands[num_commands].args, arg_pointer, ARG_MAX );
            }
    
            num_commands ++; //increment
         }
    
        clist -> num = num_commands;
        return OK; //parsing is correct
    }