\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[a4paper, margin=0.75in]{geometry}

% set up hyperlink coloring
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan
}

\setlength{\parindent}{0pt} % no indentation

% checkbox list: https://tex.stackexchange.com/questions/247681/how-to-create-checkbox-todo-list
\usepackage{enumitem,amssymb}
\newlist{boxlist}{itemize}{2}
\setlist[boxlist]{label=$\square$}

% code support: listings and fancy verbatim
\usepackage{listings}
\lstset{basicstyle=\ttfamily\footnotesize}
\usepackage{fancyvrb}

\renewcommand{\familydefault}{\sfdefault} % default to sans serif

\usepackage{xcolor}
\newcommand{\code}[1]{\colorbox{lightgray}{\texttt{#1}}}
\newcommand{\sh}[1]{\code{\$ #1}}

%%%%%%%%%%%%%%%%
\begin{document}

\title{Basic 4\\Bash and Regex}
\author{EECS 201 Fall 2020}
\date{}
\maketitle

\section*{Submission Instructions}
This assignment will be submitted as a repository on the \href{https://gitlab.umich.edu}{UMich GitLab server}.
Create a Project on it with the name/path \code{eecs201-basic4} and add \code{brng} as a Reporter.
The repository should have the following directory structure, starting from the repository's root:
\begin{lstlisting}
/
|-- report.txt
|-- grep/
|   |-- cap-vow.sh
|   |-- ing.sh
|   |-- n-letter.sh
|   |-- same-lower-vowel.sh
|
|-- sed/
    |-- c89ify.sh
\end{lstlisting}

\section*{Preface}
I highly suggest that you do this homework in a Linux environment,
be it WSL on Windows (on the Linux filesystem, not the Windows filesystem)
or your Ubuntu virtual machine.
The reasoning for this is that some tools that deal with regular expressions
(namely for this assignment: \code{sed} and \code{grep}) may differ in behavior depending on *nix system.
Linux systems use GNU \code{sed} and \code{grep} while macOS (and FreeBSD)
use BSD \code{sed} and \code{grep} which have some subtle differences in behavior.
\\\\
In this assignment you'll be provided yet another zipped archive containing some starter empty files and scripts.
\begin{lstlisting}[language=bash]
$ wget https://www.eecs.umich.edu/courses/eecs201/files/assignments/basic4.tar.gz
\end{lstlisting}

Initialize a Git repository in the extracted \code{basic4} directory,
adding all of the script files and committing them.
Create a \textbf{private} project with the name/URL \code{eecs201-basic4} on the UMich GitLab (\url{gitlab.umich.edu})
and add the instructor \code{brng} as a \textbf{Reporter}.
Set this UMich GitLab project as your remote: you'll be pushing to it in order to submit.

\section{Regex fun}
As mentioned in lecture, \code{grep} is a utility that finds patterns in files.
These patterns are by default POSIX basic regular expressions;
\code{egrep} or \code{grep} with the \code{-E} flag will interpret patterns as POSIX extended regular expressions.

For this question we'll be looking at an American English dictionary.
\\This file is located under \code{/usr/share/dict/american-english}.
If you don't have it, on Ubuntu you can get it via the \code{wamerican} package
and on Arch you can get it via the \code{words} package.
(\code{/usr/share/dict/words} is a standard file that contains a list of dictionary words; it's probably
symlinked to the appropriate dictionary file. You'll learn about symlinks in the next homework question).

\begin{enumerate}
  \item \code{cd} into the \code{grep} directory.
  \item Run \sh{grep~"world"~/usr/share/dict/american-english}.
        This finds and prints out words in the dictionary that contain ``world'' anywhere in the word.
  \item Run \sh{grep~"\^{}[A-Z]"~/usr/share/dict/american-english}.
        This finds and prints out words in the dictionary that start with a capital letter.
\end{enumerate}

\medskip
Now onto the question proper:
\begin{enumerate}
  \item Implement the functionality described in each of the Bash scripts. You may only use one \code{grep} command
        in each script. Each script takes in the path to some dictionary file. This can be the American English dictionary,
        or your own test dictionary. Feel free to use ERE via \code{-E} or \code{egrep}.
  \item Stage and commit your changes.
\end{enumerate}



\section{Searching and replacing text with \code{sed}}
\code{sed} is a utility that is able to perform pattern searches and replacements.
By default \code{sed} will use POSIX BRE unless a parameter is specified to use ERE.

\begin{enumerate}
  \item \code{cd} into the \code{sed} directory.
  \item Run \sh{echo~"hello~user~hello"~|~sed~-e~"s/hello/world/"}.
        What \code{sed} did is replace the first instance of ``hello'' with ``world'' of each line of input.
        \code{s} is the command to ``substitute'' texts, with the following charactering serving as a
        delimiter (\code{sed~-e~"s@hello@world@"} would also work, with it using '@' as a delimiter instead of '/').
        The \code{s} command is of the format \code{s/pattern/replacement/flags}.
  \item Run \sh{echo~"hello~user~hello"~|~sed~-e~"s/hello/world/g"}.
        Note that both ``hello''s were replaced.
        The \code{g} at the end of the \code{sed} \code{s} command is a flag that
        says to replace all matches, not only the first.
  \item Run \sh{echo~"hello~user~hello"~|~sed~-e~"s/\textbackslash(hello\textbackslash)~\textbackslash(.*\textbackslash)~\textbackslash1/world~\textbackslash2~world/g"}
        Note the use of backreferences in both the pattern and replacement fields.
\end{enumerate}

\medskip
Now onto the question proper. Take a look at \code{saxpy.c}. Note how it uses \code{//} for comments.
\begin{enumerate}
  \item Run \sh{gcc~-std=c99~saxpy.c}. This will compile the C program following the C99 standard.
        It should compile successfully. Feel free to run the \code{a.out} binary that is produced.
  \item Now run \sh{gcc~-std=c89~saxpy.c}. This will compile the C program following the C89 (``ANSI C'') standard.
        It should fail to compile. That is because C++ actually introduced the use of \code{//} for single single line comments, support for which the C99 standard added to the C language. (If your \code{gcc} is aliased to \code{clang} (e.g. on macOS),
        add the \code{-pedantic} flag; \code{clang} is less anal about this particular part of the language).
  \item In the provided \code{c89ify.sh} file, implement the script as described by its usage printout.
        What this script is supposed to do is take in a list of C files as arguments and then for each C file,
        replace the \code{//~comment} comments with \code{/*~comment~*/} style comments and put a
        ``fixed'' version of the file into a new file that tacks on \code{.c89} before the \code{.c} extension.
        For example, \code{./c89ify.sh~saxpy.c~daxpy.c} will produce fixed versions in
        \code{saxpy.c89.c} and \code{daxpy.c89.c}.
  \item To test if the replacement worked, try compiling the files with the \code{-std=c89} flag.
        (By the way, deleting the comments aren't a solution; we can check to see if the actual comment messages are still there :p
  \item Stage and commit the finished \code{c89ify.sh} script.
\end{enumerate}


\section{Conclusion}
\begin{enumerate}
  \item Add and commit any changes you intend to submit.
  \item Create a file called \code{report.txt}.
  \item On the first line provide an integer time in minutes of how long it took
        for you to complete this assignment.
  \item On the second line and beyond, write down what you learned while doing this assignment.
        If you already knew how to do all of this, put down ``N/A''.
  \item Add and commit this \code{report.txt} file.
\end{enumerate}


\newpage
\section*{Symbolic links}
\emph{This section is ungraded and is more for your personal edification.}\\
A ``symbolic link'' is a special type of file that ``points'' to another file.
(This is similar to the idea of a shortcut to a file).
In this question we'll be exploring the use of symbolic links (``symlinks'').
\begin{enumerate}
  \item Use \code{mkdir} to create a directory called \code{symlink} and \code{cd} into it.
        Use \code{touch} to create an empty file named \code{hello}.
  \item Using \code{mkdir}, make a directory called \code{links}.
  \item Let's make a symbolic link to \code{hello} called \code{world}:\\
        run \sh{ln~-s~hello~world}
  \item Open the \code{world} file that gets created in a text editor,
        writing ``\code{Hello world!}'' in it and saving it.
  \item Open the \code{hello} file. Note how the changes to \code{world} appear here.
  \item What \code{ln -s} did is create a special file called \code{world} that links to
        a file called \code{hello} in the \textit{current directory}.
  \item We can see what a symlink points to by running \sh{readlink~world}.
        Alternatively \sh{ls~-l} will also show where symlinks point.
        Note how \code{world} points to \code{hello} by itself.
  \item Using \code{mv}, move the \code{world} file into \code{links} directory.
  \item Now run \sh{cat~links/world}. Note how it can't resolve the target file
        as the link refers to a file named \code{hello} in the same directory as it.
        You can run \sh{readlink~-f~links/world} to see the exact (``canonical'') path
        that the symlink resolves to.
  \item Now run \sh{ln~-s~hello~links/foo} and \sh{ln~-sr~hello~links/bar}\\
        Now run \sh{ls~-l~links}.
        Note the difference in where \code{links/foo} and \code{links/bar} point.
        The \code{-r} flag makes the pointer to the target file \textbf{r}elative to the link's location.
        You can also provide absolute addresses (starting from root \code{/}) as targets to link
        (with all of the issues that they come with: what would happen if I cloned this repo and
        tried to access that symlink?).
\end{enumerate}


\end{document}
