A Glimpse of Icon

This article gives a quick introduction to the programming language Icon, developed at the University of Arizona.
Graphics and User Interfaces

Icon's built-in graphics have about 40 functions and introduce only one new type, the window, which is a special extension of the file type. This contrasts with graphics APIs in other languages where learning graphics means learning 400 or more functions that manipulate several dozen new types of values. Passing strings and integers into a few functions is all you need to write amazing graphics without excessive code.

One demonstration of Icon graphics is Brad Myers' “rectangle-follows-mouse” test, a program that opens up a window in which a rectangle follows a mouse around on the screen. A window is opened (file mode “g”) with an XOR raster drawing operation that causes graphics to erase themselves when redrawn. In the loop, for each user event, the ten-pixel square is erased and redrawn at the new mouse location. &x and &y are Icon keywords which hold the current mouse location and are saved in variables x and y. The variables x and y start out as null. The expression \x fails if x is null, causing the first call to DrawRectangle to be skipped the first time through the loop, since at this point, there is no rectangle to draw.

procedure main()
 w := open("win","g", "drawop=reverse")
 repeat {
 # get mouse/keyboard event
 Event(w)
 # erase old rectangle
 DrawRectangle(w, \x, y, 10, 10)
 # draw new rectangle
 DrawRectangle(w, x := &x, y := &y, 10, 10)
 }
end

Simple graphics programming is easy, but complex graphics are also possible. The Icon Program Library (IPL), a collection of Icon utilities and libraries, offers a more extensive Motif-style user interface toolkit as well as a WYSIWYG (what you see is what you get) interface builder that lets you build interfaces by drawing them. The IPL contains several other examples of graphical games and applications.

POSIX Made Simple

The Unicon flavor of Icon adds an elegant set of UNIX system-level facilities. An ultra-simple version of the ls utility illustrates some of these features. This version takes a directory name on the command line and produces a listing of file information including file size and modified time, sorted by name. (A more interesting version is included in Linux Gazette article.)

ls reads the directory and performs a stat call on each name it finds. In Icon, opening a directory is exactly the same as opening a file for reading; every read returns one file name.

$include "posix.icn"
procedure main(argv)
 f := open(argv[1]) |
 stop("ls: ", sys_errstr(&errno))
 names := list()
 while name := read(f) do
 push(names, name)
 every name := !sort(names) do {
 p := lstat(name)
 write(p.size, "   ", ctime(p.mtime)[5:17],
 " ", name)
 }
end

The lstat function returns a record with all the information that lstat(2) returns. In the Icon version, the mode field is given as a human readable string—not an integer to which you must apply bitwise magic. Also, in Icon, string manipulation is very natural.

Give Icon a try; whether you're a programmer or not, you'll love it.

Resources

Clint Jeffery is an assistant professor in the Division of Computer Science at the University of Texas at San Antonio. He writes and teaches about program monitoring and visualization, programming languages and software engineering. Contact him at jeffery@cs.utsa.edu or read about his research at www.cs.utsa.edu/faculty/jeffery.html. He received his Ph.D. from the University of Arizona.

Shamim Mohamed met UNIX in 1983 and was introduced to Linux at version 0.99 pl12. These days he is a Silicon Valley polymath and factotum, and an instrument-rated pilot flying taildraggers. He can be reached at spm@drones.com or www.drones.com/. He received his Ph.D. from the University of Arizona.

______________________

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix