CS253: Software Development with C++

Spring 2021

HW 4

CS253 HW4: Options!                

Changes                

The format of -v (verbose) output was inconsistently specified. Do it like this, with a leading Executing: and "quotes":

Executing:
0: "echo"
1: "hi"
2: "there"

Description                

For this assignment, you will improve upon your previous work in HW2, adding command-line options. This is a complete program, not a library.                 

Arguments                

The first command-line arguments should be options:                 

-v
Specify verbose output. Just before executing the command, write the command & arguments to standard output, thus:
Executing:
0: "echo"
1: "hi"
2: "there"
-m max-args
If the command has more than the given number of arguments, complain to standard error and stop the program. For example, -m2 with input of winter(spring summer fall) will fail, because that commmand has three arguments. The error message must contain the maximum number of allowed arguments, and the entire original offending input line.
-s singleton-characters
This option identifies which characters are to be treated as separate words, or singletons, even without surrounding whitespace, as only () were treated in HW2. If this option is not given, treat () as singletons, as in HW2. Whitespace characters as the argument to -s, e.g., -s ' ', invokes undefined behavior. If you specify -s but don’t give ( and ) as part of the string, then simple input lines such as date() will fail, but date ( ) will succeed.
If a singleton character is escaped via a backslash, it should not be treated as a separate word. If the singleton characters are special to bash, such as > or |, then you should wrap them in "quotes" to protect them from bash.

This is the Colorado State University CS253 web page https://cs.colostate.edu/~cs253/Spring21/HW4 fetched by unknown <unknown> with Linux UID 65535 at 2024-11-21T19:04:01 from IP address 3.129.63.252. Registered CSU students are permitted to copy this web page for personal use, but it is forbidden to repost the information from this web page to the internet. Doing so is a violation of the rules in the CS253 syllabus, will be considered cheating, and will get you an F in CS253.

Sample Runs                

Here are sample runs, where % is my prompt.                 

% cat CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(hw4)

# Are we in the wrong directory?
if(CMAKE_SOURCE_DIR MATCHES "[Hh][Ww]([0-9])$")
   if(PROJECT_NAME MATCHES "[^${CMAKE_MATCH_1}]$")
      message(FATAL_ERROR "Building ${PROJECT_NAME} in ${CMAKE_SOURCE_DIR}")
   endif()
endif()

# Using -Wall is required:
add_compile_options(-Wall)

# These compile flags are highly recommended, but not required:
add_compile_options(-Wextra -Wpedantic)

# Optional super-strict mode:
add_compile_options(-fmessage-length=80 -fno-diagnostics-show-option
    -fstack-protector-all -g -O3 -std=c++17 -Walloc-zero -Walloca
    -Wctor-dtor-privacy -Wduplicated-cond -Wduplicated-branches
    -Werror -Wextra-semi -Wfatal-errors -Winit-self -Wlogical-op
    -Wold-style-cast -Wshadow -Wunused-const-variable=1
    -Wzero-as-null-pointer-constant)

# add_compile_options must be BEFORE add_executable.

# Create the executable from the source file main.cc:
add_executable(${PROJECT_NAME} main.cc)

# Create a tar file every time:
add_custom_target(${PROJECT_NAME}.tar ALL COMMAND
    tar -cf ${PROJECT_NAME}.tar *.cc *.h CMakeLists.txt)
% cmake . && make
… cmake output appears here …
… make output appears here …
% cat data1
uname ( -sn -p )
expr(2 + 42)
% cat data2
echo(alpha 177777)
% ./hw4 <data1
Linux greybull x86_64
44
% ./hw4 -v data1 data2
Executing:
0: "uname"
1: "-sn"
2: "-p"
Linux greybull x86_64
Executing:
0: "expr"
1: "2"
2: "+"
3: "42"
44
Executing:
0: "echo"
1: "alpha"
2: "177777"
alpha 177777
% ./hw4 -s')p(' -v data2
Executing:
0: "echo"
1: "al"
2: "p"
3: "ha"
4: "177777"
al p ha 177777

Debugging                

If you encounter “STACK FRAME LINK OVERFLOW”, then try this:

    export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a

Hints                

Requirements                

All requirements from HW2 still apply, with these additions:

Tar file                

    cmake . && make

How to submit your work:                

In Canvas, check in the file hw4.tar to the assignment “HW4”. It’s due 10:00:00ᴘᴍ MT Saturday, with a 24-hour late period for a 25% penalty.                 

How to receive negative points:                

Turn in someone else’s work.