C0 | Documentation
About Downloads Tutorial References Courses

C0 at Andrew (Carnegie Mellon)

THIS INFORMATION IS OUTDATED: Please refer to C0 at CMU, which is the page we're currently keeping updated.

THIS INFORMATION IS OUTDATED: Please refer to C0 at CMU, which is the page we're currently keeping updated.

Get connected to AFS

Andrew AFS is a shared file system that can lets you access the same files from multiple computers. Your own personal files, as well as the course tools you will need to compile and run C0 programs, are available through the AFS shared file system.

If you are on a cluster machine like the computer labs in Gates (rooms 5205, 5201, and 3000), then the AFS file system act like they're just files on the computer's local file system. If you're on your laptop, you will have to use SSH to connect to a machine, probably the one named linux.andrew.cmu.edu, and access AFS there. This is nice because you can do it anywhere, but it can be slow - sometimes this is because lots of people are connecting to the same computer, but more frequently it's just because the wireless got flaky.

How SSH Works. SSH sends characters from your machine to a remote machine to be run -- the text output is sent back across the network to be shown in your terminal. The X forwarding option (-X) sends mouse movements and graphics such as your emacs window, as well.

Look at the annotations on the lines above. Sending characters you typed to the remote machine, and having it send characters to display in the terminal, is generally more failproof than sending mouse movements and clicks to the remote machine and having it send graphics back. Sending clicks and images back and forth is generally called "X11 forwarding".

On cluster computers...

Just open a terminal window, and you're already there. (You don't need to connect to linux.andrew.cmu.edu if you're on a cluster computer, since you can access AFS from cluster computers.)

On Linux or OS X Leopard/Snow Leopard/Lion...

Open a terminal window (the program is actually called Terminal on OS X) window and type (replacing andrewid with your actual Andrew ID):

% ssh andrewid@linux.andrew.cmu.edu

If the user name on your computer is the same as your Andrew ID, you can avoid typing your andrewid:

% ssh linux.andrew.cmu.edu

You'll be asked to enter your Andrew password.

To get X11 forwarding working, you just need to add the -X option to ssh:

% ssh -X linux.andrew.cmu.edu

With some operating systems, using -Y instead of -X works as well.

On OS X Mountain Lion...

Everything is the same as it is for the other varieties of OS X, except that in order for X11 forwarding to work you need to first install XQuartz.

On Windows...

For Windows, you'll want to use either PuTTY.

Alex Cappiello has instructions for setting up PuTTY with or without X11 here.

...so, now you're at the command line.

Your terminal window will give you a prompt and you will be in your home directory in your Andrew account. Type ls (to list files) to see what files and directories are there. Type pwd (for "present working directory") to get the name of the directory your in, your home directory. (Back in the day, we used to call folders "directories".) To move to another directory, use the cd command (for "change directory"):

$ cd private

ACADEMIC INTEGRITY NOTE: You should store your program files inside the private directory (or a subdirectory inside this directory) since this directory is automatically set to prevent viewing by other users. Remember that you should protect your code from being viewed by other students as part of the academic integrity policy for this course.

Since you will write a number of programs for this course, it pays to make a subdirectory inside the private directory. Once you cd into the private directory, make a new directory named 15122:

$ mkdir 15122

Let's confirm that our directory now exists. You can see what files and directories are in the current directory like this:

$ ls

Now go into this directory using cd again:

$ cd 15122

To go back up one directory if you need to, use this command:

$ cd ..

You can go up two directories and down another directory (e.g. public) like this:

$ cd ../../public

If you ever get lost while you change from one directory to another, you can use pwd to find your present working directory. Or if you want to go back to your home directory, simply type:

$ cd

You can also access any directory from your home directory using the shell variable $HOME or the "tilde" character ~. For example, you can get to your 15122 directory from any directory on Andrew by typing this:

$ cd $HOME/private/15122

or this:

$ cd ~/private/15122

Finally, you should download and print the Linux quick reference page below for a more complete list of basic Linux commands. Some quick examples:

LOGGING OUT: If you're using a shared computer in a computer lab, remember that you must log out. Closing the terminal window is not enough on a shared computer! To log out, go to the System menu and choose Log Out. You don't want others accessing your account if you leave yourself logged in.

COURTESY NOTE: In the Gates labs, if you don't use your lab computer for a set period of time, the screen saver will launch and you can't access the terminal unless you type your password in. Some students have left the computer this way so they can "reserve" the machine for themselves later. If we find a machine in screen saver mode and no user at that terminal, we will automatically log you out so you may lose work if you're not careful. Please be considerate of others and don't lock the terminal.

More information:

Set up the environment for using C0

NOTE: If you're in the "Great Practical Ideas in Computer Science" course, this step should already be done. As an alternative to this step you can follow the GPI instructions here.

We need to do two things. First, we'll make sure you can access the C0 tools like cc0 and coin. Second, we'll set up either Emacs, Vim, or Sublime Text for editing C0 files. Emacs and vim are editors that live in the terminal, and are functionally very similar (emacs can be used in a popout window if X-11 is setup, although this can be slow over AFS). We reccomend you learn how to use one of these well. However, Sublime text is a GUI text editor which has a plugin to connect to your AFS files. If you find emacs or vim hard to use, sublime text should seem more natural.

Adding the course directory to your path

First, we need to add /afs/andrew/course/15/122/bin to the $PATH a shell variable (like $HOME) that tells your shell where to look for programs to run. In particular, if you're using the default Andrew shell, csh, you need to add the following line to $HOME/.cshrc:

$ setenv PATH ${PATH}:/afs/andrew/course/15/122/bin

If you're not using csh, you'll need to edit the $PATH variable a different way. Type echo $SHELL at the command line to see which shell you're using. If csh is printed, you're using csh. If bash or zsh or something else is printed, you can use a search engine or ask course staff to find out how to edit the $PATH environment variable in your shell.

Setting up Emacs to use C0

You can use any editor you wish to write and edit your programs, but we set up your account so that, if you use Emacs, it has syntax highlighting and other support for C0.

To get Emacs set up, run

$ /afs/andrew/course/15/122/c0-mode/configure

and copy the lines it prints out into your ~/.emacs file. You can use Emacs to edit your Emacs file, just run

$ emacs ~/.emacs

The file will be created if it does not already exist.

We also strong recommend that you add these lines to your ~/.emacs file:

(column-number-mode) ;; Shows the column number in the status bar.
(show-paren-mode) ;; Highlights the matching paren.
(global-linum-mode) ;; Shows the line numbers in a column on the left.
(add-hook 'before-save-hook 'delete-trailing-whitespace) ;; Trailing whitespace is pure evil.
(setq require-final-newline t) ;; Good practice.
(setq-default indent-tabs-mode nil) ;; Don't indent with tabs.
;; If indent-tabs-mode is off, untabify before saving, just in case another program put them there.
(add-hook 'write-file-hooks (lambda () (if (not indent-tabs-mode) (untabify (point-min) (point-max)))))
;; Highlight things like tabs and long lines. Can you tell that I hate tabs?
(require 'whitespace)
(setq whitespace-style '(face lines tabs tab-mark))
(global-whitespace-mode 1)

When you want to edit a program now, you can enter emacs specifying the file name you want to edit. For example, to open the file test.c0 in emacs, you'd go to the directory where this file should be stored and enter:

$ emacs test.c0

To exit emacs, press C-x, C-c (In emacs keyboard shortcuts, capital C stands for Ctrl and capital M stands for "meta", which is usually Alt or Esc.). Note: if you are using the popout version of emacs, you may add an "&" character to the end of the command if you want to continue entering commands in the terminal. To get proficient at using emacs, you should, at minimum look at the first two links below.

More information:

Setting up Vim to use C0

You can also use vim to edit C0 files. Running this command:

$ /afs/andrew.cmu.edu/course/15/122/bin/editor_config/vim/setup.sh

should set up Vim for C0 correctly.

Then, just quit and relaunch vim, and syntax highlighting and indentation should work.

Setting up Sublime Text to use C0

The setup for Sublime Text is more complicated than for the other editors, so we reccomend you try emacs or vim first. It is more intuitive to use for people who have not used emacs or vim before. If you do not have Sublime Text installed, you can download it here. Next, install the Sublime Package Control using the instructions here. To install the SFTP plugin (which allows you to edit files on AFS), use [ctrl|cmd]+shift+p to open the command palette, type "install package", and press enter. A new input for should appear; select SFTP and hit enter. The plugin will now be installed. Finally, go to File->SFTP/FTP->Setup Server, and a new file will open to setup the connection. Delete the template and paste in (substituting your andrew id where appropriate):

    "type": "sftp",
    "sync_down_on_open": true,
    "sync_same_age": true,
    "host": "linux.andrew.cmu.edu",
    "user": "YOUR_ANDREW_ID",
    "remote_path": "/afs/andrew.cmu.edu/usr/YOUR_ANDREW_ID/private/15122",
    "connect_timeout": 30,

Now you can edit a file using File->SFTP/FTP->Browse Server. Saving the file will cause it to be uploaded to AFS, where you can compile and run it. Finally, to turn on syntax highlighting, open a .c0 file, and then select View->Syntax->Open All With Current Extension As->C. (C syntax is close enough to c0 syntax that this option will work fine).

Run coin and cc0

When you used the setup script, it updated your PATH so the system can find the C0 compiler, interpreter, and debugger without you having to explicitly give the full path to the compiler's location.

Let's compile and run a simple C0 program. First copy the example file into your directory:

$ cd $HOME/private/15122
$ mkdir hello
$ cd hello
$ cp /afs/andrew.cmu.edu/course/15/122/examples/hello.c0 .

Now run the C0 compiler on the C0 source file hello.c0 to produce the binary executable "hello":

$ cc0 hello.c0 -o hello

You can now run your program like so:

$ ./hello

REMEMBER: Don't forget to save and compile your source code after making changes! Otherwise, you will find yourself very confused when your executable is doing something different than your source code.

You can use coin, the C0 INterpreter, to quickly evaluate C0 statements without having to compile and run a complete program.

$ coin
--> 2*2;

Coin will then show you the result of "4". To stop a long-running command in coin, you can press Ctrl+C. To exit coin, you can type #quit.

You can use "Code", the C0 DEbugger, to step through the execution of a C0 program, line by line. Use of this feature is not required in 15-122.

$ emacs hello.c0
C-c C-d

You should see each line of the main method be highlighted in emacs. As you press return, the next line will be highlighted. Any output will be shown at the bottom of your emacs buffer.

More information:

View an image

Finally, we want to make sure X11 forwarding is working by having you view an image file that lives on AFS. One such image is ~rjsimmon/public/snippet.png.

$ display ~rjsimmon/public/snippet.png

There are many other display programs you can also use, but if you are on a cluster machine, or if you logged in with X11 forwarding enabled, you should be able to see the image snippet.png (which is a snippet of C0 code).

Being able to view an image over SSH is very helpful for Homework 2. If you can't view images, you'll need to use a program like WinSCP or PSCP to transfer image files to your own computer so that you can look at them there.

(Optional) Install C0 on your laptop

It's also possible to install the C0 tools on your own machine, so that you don't have to log into AFS in order to do your homework. This is not necesary, but instructions and downloads can be found here.

written by Tom Cortina, 1/12/11
updated by Jon Clark, 8/27/12
updated by Rob Simmons, 1/16/13
updated by Matt Dee, 1/13/14