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                
- Use getopt(). Seriously, use it. Don’t do this yourself. You will
have to actually read the manual page for getopt(). This is a
skill that you must acquire to be a good programmer.
Requirements                
All requirements from HW2 still apply, with these additions:
- If more than one problem is detected, pick one.
- Produce an error message and stop the program if:
- a bad option or bad option argument is given
(the message must contain the bad option and argument, if present)
-m
or -s
are given multiple times.
- the argument to
-m
is not an integer, or outside the
range 1…100, inclusive.
- A space after an option taking an argument (
-m
, -s
)
is optional. -m42
is valid, as is -m 42
, -m "42"
,
or -m '42'
.
- Options must precede filenames. Option processing must stop at the
first argument that isn’t an option (or an argument to an option,
e.g.,
-s "cba"
). This command must attempt to treat -v
as
a file, which will probably fail:
./hw4 -s "*" infile -v
- Option bundling:
./hw4 -vm 3
is the same as
./hw4 -v -m 3
./hw4 -v-m 3
is invalid.
- The next non-space character after an option that requires an
argument must be the argument itself, not another option flag.
./hw4 -mv 2 file
will treat v
as the faulty argument
to -m
, and treat 2
and file
as filenames.
- “This is too hard!”
Tar file                
- The tar file for this assignment must be called:
hw4.tar
- It must contain:
- source files (
*.cc
) required to build
- any header files (
*.h
) required to build
CMakeLists.txt
- This command must produce the program
hw4
(note the dot):
cmake . && make
- At least
-Wall
must be used every time g++ runs.
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.