#!/usr/local/bin/ici /* * First off we define a function to do the work of * counting the tokens in files. We want our program * to work like a typical Unix filter so it's best if * the work is done in one place where we can call it * from different places in the command line argument * processing. */ /* * count_tokens - return the number of tokens in a file. * * This demonstrates the 'gettoken' function which reads * the next whitespace-delimited token from an input stream * (or string). This is the ICI equivilent of a "wc -w". */ static count_tokens(in) { auto count; count = 0; while (gettoken(in)) ++count; return count; } /* * This is the entry point. After parsing the function * definition above ICI will parse, and evaluate, the * code at the top-level of the file. * */ /* * ICI provides access to the command line arguments via * two pre-defined variables, argc and argv. Argc is the * count of arguments and argv is an array of strings. */ if (argc < 2) printf("%d\n", count_tokens(stdin)); else { auto f, fn, total; total = 0; forall (fn in interval(argv, 1)) { if (fn == "-") count = count_tokens(stdin); else { auto count; count = count_tokens(f = fopen(fn)); close(f); } printf("%s %d\n", fn, count); total += count; } if (argc > 2) printf("Total %d\n", total); }