Skip to content
Snippets Groups Projects
Select Git revision
  • 36dcbe0692a0157f05b244042a5d3478c8ea703d
  • main default
2 results

questions

Blame
  • dsh_cli.c 6.89 KiB
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "dshlib.h"
    
    void printDragon() {
    	const unsigned char ascii_art[] = 
    "                                                                        @%%%%                       \n"
    "                                                                     %%%%%%                         \n"
    "                                                                    %%%%%%                          \n"
    "                                                                 % %%%%%%%           @              \n"
    "                                                                %%%%%%%%%%        %%%%%%%           \n"
    "                                       %%%%%%%  %%%%@         %%%%%%%%%%%%@    %%%%%%  @%%%%        \n"
    "                                  %%%%%%%%%%%%%%%%%%%%%%      %%%%%%%%%%%%%%%%%%%%%%%%%%%%          \n"
    "                                %%%%%%%%%%%%%%%%%%%%%%%%%%   %%%%%%%%%%%% %%%%%%%%%%%%%%%           \n"
    "                               %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%     %%%            \n"
    "                             %%%%%%%%%%%%%%%%%%%%%%%%%%%%@ @%%%%%%%%%%%%%%%%%%        %%            \n"
    "                            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%                \n"
    "                            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              \n"
    "                            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@%%%%%%@              \n"
    "      %%%%%%%%@           %%%%%%%%%%%%%%%%        %%%%%%%%%%%%%%%%%%%%%%%%%%      %%                \n"
    "    %%%%%%%%%%%%%         %%@%%%%%%%%%%%%           %%%%%%%%%%% %%%%%%%%%%%%      @%                \n"
    "  %%%%%%%%%%   %%%        %%%%%%%%%%%%%%            %%%%%%%%%%%%%%%%%%%%%%%%                        \n"
    " %%%%%%%%%       %         %%%%%%%%%%%%%             %%%%%%%%%%%%@%%%%%%%%%%%                       \n"
    "%%%%%%%%%@                % %%%%%%%%%%%%%            @%%%%%%%%%%%%%%%%%%%%%%%%%                     \n"
    "%%%%%%%%@                 %%@%%%%%%%%%%%%            @%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  \n"
    "%%%%%%%@                   %%%%%%%%%%%%%%%           %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              \n"
    "%%%%%%%%%%                  %%%%%%%%%%%%%%%          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      %%%%  \n"
    "%%%%%%%%%@                   @%%%%%%%%%%%%%%         %%%%%%%%%%%%@ %%%% %%%%%%%%%%%%%%%%%   %%%%%%%%\n"
    "%%%%%%%%%%                  %%%%%%%%%%%%%%%%%        %%%%%%%%%%%%%      %%%%%%%%%%%%%%%%%% %%%%%%%%%\n"
    "%%%%%%%%%@%%@                %%%%%%%%%%%%%%%%@       %%%%%%%%%%%%%%     %%%%%%%%%%%%%%%%%%%%%%%%  %%\n"
    " %%%%%%%%%%                  % %%%%%%%%%%%%%%@        %%%%%%%%%%%%%%   %%%%%%%%%%%%%%%%%%%%%%%%%% %%\n"
    "  %%%%%%%%%%%%  @           %%%%%%%%%%%%%%%%%%        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %%% \n"
    "   %%%%%%%%%%%%% %%  %  %@ %%%%%%%%%%%%%%%%%%          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %%% \n"
    "    %%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%           @%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %%%%%%% \n"
    "     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              %%%%%%%%%%%%%%%%%%%%%%%%%%%%        %%%   \n"
    "      @%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  %%%%%%%%%%%%%%%%%%%%%%%%%               \n"
    "        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                      %%%%%%%%%%%%%%%%%%%  %%%%%%%          \n"
    "           %%%%%%%%%%%%%%%%%%%%%%%%%%                           %%%%%%%%%%%%%%%  @%%%%%%%%%         \n"
    "              %%%%%%%%%%%%%%%%%%%%           @%@%                  @%%%%%%%%%%%%%%%%%%   %%%        \n"
    "                  %%%%%%%%%%%%%%%        %%%%%%%%%%                    %%%%%%%%%%%%%%%    %         \n"
    "                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                      %%%%%%%%%%%%%%            \n"
    "                %%%%%%%%%%%%%%%%%%%%%%%%%%  %%%% %%%                      %%%%%%%%%%  %%%@          \n"
    "                     %%%%%%%%%%%%%%%%%%% %%%%%% %%                          %%%%%%%%%%%%%@          \n"
    "                                                                                 %%%%%%%@           \n";
        fwrite(ascii_art, sizeof(unsigned char), sizeof(ascii_art) - 1, stdout);
    }
    
    /*
     * Implement your main function by building a loop that prompts the
     * user for input.  Use the SH_PROMPT constant from dshlib.h and then
     * use fgets to accept user input.  Since we want fgets to also handle
     * end of file so we can run this headless for testing we need to check
     * the return code of fgets.  I have provided an example below of how
     * to do this assuming you are storing user input inside of the cmd_buff
     * variable.
     *
     *      while(1){
     *        printf("%s", SH_PROMPT);
     *        if (fgets(cmd_buff, ARG_MAX, stdin) == NULL){
     *           printf("\n");
     *           break;
     *        }
     *        //remove the trailing \n from cmd_buff
     *        cmd_buff[strcspn(cmd_buff,"\n")] = '\0';
     *
     *        //IMPLEMENT THE REST OF THE REQUIREMENTS
     *      }
     *
     *   Also, use the constants in the dshlib.h in this code.
     *      SH_CMD_MAX              maximum buffer size for user input
     *      EXIT_CMD                constant that terminates the dsh program
     *      SH_PROMPT               the shell prompt
     *      OK                      the command was parsed properly
     *      WARN_NO_CMDS            the user command was empty
     *      ERR_TOO_MANY_COMMANDS   too many pipes used
     *
     *   Expected output:
     *
     *      CMD_OK_HEADER      if the command parses properly. You will
     *                         follow this by the command details
     *
     *      CMD_WARN_NO_CMD    if the user entered a blank command
     *      CMD_ERR_PIPE_LIMIT if the user entered too many commands using
     *                         the pipe feature, e.g., cmd1 | cmd2 | ... |
     *
     *  See the provided test cases for output expectations.
     */
    int main()
    {
        char *cmd_buff = malloc(ARG_MAX * sizeof(char));
        int rc = OK;
        command_list_t *clist = malloc(sizeof(command_list_t));
        clist->num = 0;
    	
    	while (1)
        {
            printf("%s", SH_PROMPT);
            if (fgets(cmd_buff, ARG_MAX, stdin) == NULL)
            {
                printf("\n");
                break;
            }
            // remove the trailing \n from cmd_buff
            cmd_buff[strcspn(cmd_buff, "\n")] = '\0';
    		
            // IMPLEMENT THE REST OF THE REQUIREMENTS
    		if (strcmp(cmd_buff, EXIT_CMD) == 0) {
    			exit(rc);
    		}
    
    		if (strcmp(cmd_buff, "dragon") == 0) {
    			printDragon();
    			continue;
    		}
    
    		if (strcmp(cmd_buff, "\0") == 0) {
    			rc = WARN_NO_CMDS;
    			printf(CMD_WARN_NO_CMD);
    			continue;
    		}
    		
    		rc = build_cmd_list(cmd_buff, clist);
    
    		if (rc == ERR_TOO_MANY_COMMANDS) {
    			printf(CMD_ERR_PIPE_LIMIT, CMD_MAX);
    			continue;
    		}
    
    		printf(CMD_OK_HEADER, clist->num);
    
    		for (int i = 0; i < clist->num; i++) {
    			if (strlen(clist->commands[i].args) > 0) {
        			printf("<%d> %s [%s]\n", i + 1, clist->commands[i].exe, clist->commands[i].args);
    			} else {
        			printf("<%d> %s\n", i + 1, clist->commands[i].exe);
    			}
    		}
    	}
        
        free(cmd_buff);
        free(clist);
        return rc;
    }