CSC 161 Grinnell College Fall, 2014
 
Imperative Problem Solving and Data Structures
 
 

Practice with Elementary C plus eSpeak

Class Preparation before Class

Work Started in Class

Timesaving Linux Capabilities

In the introduction to C lab, you edited, compiled, and ran a C program quarts.c. This part of the lab provides experience with several Linux capabilities that can simplify some parts of program development.

Background Processes

  1. Move to your labs subdirectory, compile quarts.c, and run the program. Your work in a terminal window might involve the following:

    cd csc161/labs
    gcc -o quarts quarts.c
    quarts
    

    Now edit quarts.c and try to compile and run the program.

    1. First, type

      emacs quarts.c
      

      This will open emacs just fine, but now switch back to your terminal window. You will notice that it is unavailable for further use (i.e., you won't get another command prompt until emacs is closed). For example, you will not be able to compile and run quarts.c until the emacs editor is closed.

    2. Now close emacs, and then re-open it from the terminal window, but this time add an ampersand character to the end of the command:

      emacs quarts.c &
      

      Now when you return to your terminal window, a prompt is waiting for you, making it easy to do multiple tasks at once — you can compile and run quarts.c without interruption. Adding the ampersand character to a command causes the command to be launched as a "background process," allowing you to continue working with your terminal window.

Autocompletion

  1. Try using "autocompletion" with quarts and quarts.c by typing

    gcc -o q followed by the TAB key
    

    You should find that when you press TAB the system completes the file name for you. In this case, both quarts and quarts.c are files in your directory, so the TAB completes q with the common prefix quarts, giving

    gcc -o quarts 
    

    Type q and the TAB key again:

    gcc -o quarts q followed by the TAB key
    

    Again, autocompletion gives

    gcc -o quarts quarts
    

    Type a period and the TAB, and autocompletion gives

    gcc -o quarts quarts.c
    
  2. This time, start the same as in the previous step, but hit the TAB key twice typing

    gcc -o q TAB TAB
    

    Describe what you see.

Printing

Several commands are available for printing files.

Utility Description
a2ps file
a2ps --sides=duplex file
prints file to default printer (handles many standard file formats)
same, but double-sided
lpr
send file to the default printer
lpq
displays jobs in print queue on default printer
lprm 585
cancels (removes) print job number 585 from default printer queue

You can accomplish printing a C file in at least two ways:

For this course, printing from emacs is not satisfactory. emacs adds a header that confuses the splitting of a file into pages. For multi-page files, printing from emacs therefore often produces awkward page breaks and partial pages.

For this course, always use lpr in a terminal window to print a C program. For example, print quarts.c with the command

lpr quarts.c

The Manual

  1. Use the man command to consult the online Linux manual (Note the q key is used to exit man pages):

    1. What do the following command options mean? Note the ability to smash flags together.

        cp -p
        ls -ltrF
        mkdir -p
      
    2. Why does the cat command have the name "cat", which stands for "concatenate"?

    3. Use the man page for the C function sqrt to identify the parameters and return type for a collection of functions related to sqrt. Also, what "include" statement is needed for sqrt

  2. The command man -k keyword lists commands that seem related to the given keyword. For example, to print a list of man pages that include the word "square" in the name or description fields, you could use "man -k square". Try this command to locate sqrt and to determine various commands related to the keyword "print".

eSpeak

In many applications, it is helpful to include speech synthesis within a C program. Although not perfect (actually far from perfect), the eSpeak package provides a simple way to instruct the workstation to talk, given specific text.

In order to limit interference with the work of others in the lab, you may need earphones to listen to audio from your workstation.

Documentation for the eSpeak package contains an commentary, a header file, and two C programs as examples.

  1. As an example, compile and run quarts-espeak.c.

    1. In the Linux basics lab, you copied quarts-espeak.c to your labs subdirectory. If this did not work properly, copy the file now:

      cp /home/walker/public_html/courses/161.sp14/modules/getting-started/quarts-espeak.c .
      
    2. To compile quarts-espeak.c, you must tell the gcc compiler where to find the eSpeakPackage. This is done by adding a piece to the usual compile line:

      gcc -leSpeakPackage -o quarts-espeak quarts-espeak.c
      
    3. Run the program with the expected name of the compiled program:

      ./quarts-espeak
      
  2. Open quarts-espeak.c in an editor, and find the various eSpeak commands. Review the program with the following notes:

    • eSpeakConnect () sets up the eSpeak environment. As part of this activity, the synthesized voice is set randomly to a male or female voice.
    • eSpeakSetGender (parm) allows the parameter to be "female" or "male", and sets the corresponding voice characteristics.
    • eSpeakTalk ("text goes here") instructs eSpeak to create audio related to the text specified.
    • eSpeakDisconnect () cleans up the eSpeak environment. If this command is omitted, the workstation's speaker may hum for awhile after the program finishes.
  3. Modify quarts-espeak.c in several ways to explore the capabilities of this package.

Homework

Displaying Text Files

  1. Try the following commands that display all or part of this laboratory exercise:

       cat  /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml
       more /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml
       less /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml
       head /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml
       head -n 20 /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml
       tail /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml
       tail -n 20 /home/walker/public_html/courses/161.sp14/modules/getting-started/lab-more-intro-c.shtml
    

    (Note that this is a wonderful time to use the arrow keys to edit previous commands rather than to retype the full lines each time. You can also use the tab autocomplete utility.)

    For the less command, try the arrow keys to move up and down in the file.

Directory and File Permissions

The following steps ask you to review the permissions for your account directories and adjust them for this course.

  1. Move to your home directory and obtain a "long" listing of the files present using the commands:

       cd
       ls -l
    

    (The cd command without parameters takes you to your home directory.)

    Interpret the meaning of each part of the directory and file listings.

Setting Permissions

For CSC 161, it seems likely that you will want others in the class to be able to read your labs, since you are collaborating with others on that material. However, you do not want others to be able to read your supplemental problems. These steps set up this framework.

  1. Move to your home directory.

    1. Allow others to read (but not change) your login directory with the command:

         chmod 755 .
      
    2. Next allow others to read (but not change) files in your labs directory:

         chmod 755 csc161
         chmod 755 csc161/labs
      
    3. Now use the ls -l -a command to check that others can read your home directory and the labs subdirectory, but no other directories.

    4. Team up with another class member to check which directories of theirs you can read.

  2. Now suppose you set your home directory with the command:

       chmod 711 ~
    
    1. Can others obtain a listing of your home directory?

    2. Can others obtain a listing of your labs subdirectory?

    Consider what access you want others to have to your home directory and set it accordingly. Unless you want to protect a file against your own inadventent editing, you likely will want to retain 7 at the beginning of the chmod permission list.

Feedback Welcome

Development of laboratory exercises is an iterative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.