#!/bin/sh -u # $0 [ pathname ] # Compile a program (the optional argument) as C or C++ based on its contents. # Prompt for a missing program name. # Display the errors/warnings with line numbers. # -IAN! idallen@ncf.ca export PATH=/bin:/usr/bin umask 022 # 6 marks # # Validate the number of arguments; issue a good error message. # if [ $# -gt 1 ]; then echo 1>&2 "$0: Expecting 0 or 1 argument; found $# ($*)" exit 1 fi # 7 marks # Prompt for a missing program source file. # Put the file name into a named variable. # if [ $# -eq 0 ]; then echo 1>&2 "Enter the program source file name:" read myprog else myprog="$1" fi # 2 marks echo "You are processing file '$myprog'." # Verify that the input is a non-empty, readable file. # # 5 marks if [ ! -f "$myprog" ]; then echo 1>&2 "$0: '$myprog' is not a file" exit 1 fi # 5 marks if [ ! -r "$myprog" ]; then echo 1>&2 "$0: '$myprog' is not readable" exit 1 fi # 5 marks if [ ! -s "$myprog" ]; then echo 1>&2 "$0: '$myprog' is empty" exit 1 fi # 9 marks # Select the C++ compiler if a string is found indicating a C++ program. # Save the exit status for use later. # if grep 'cout <<' "$myprog" >/dev/null ; then g++ -Wall -o newcpp "$myprog" 2>warnings.out cstatus=$? fi # 9 marks # Select the C compiler if the following string is *not* found. # Save the exit status for use later. # if ! grep '/dev/null ; then gcc -Wall -o newccc "$myprog" 2>warnings.out cstatus=$? fi # 3 marks if [ $cstatus -gt 1 ]; then echo "Compiler exit status was $cstatus." fi # 7 marks # Remove an empty error file. # Rename and display with line numbers any errors. # if [ -s warnings.out ]; then mv warnings.out warnings.txt cat -n warnings.txt chmod 400 warnings.txt else rm warnings.out fi