exp

A simple macro program that expands all environment variables and simple built in functions in its input. Variables are surrounded by percent signs (%). For example, the line

My search path is: %PATH%

will generate somthing similar to this:

My search path is: c:\ruby\bin;C:\WINDOWS\system32;...C:\WINDOWS;

Usage

exp [variable list] <inputfile >outputfile

The variable list is explained in the Details section.

Details

There are three types of variables expanded by exp. It expands variables passed as command line arguments, built in “function” variables, and environment variables (in that order).

The command line arguments are accessed by number. To get the first argument, use “%1%”.

The built in variables are “include”, “date”, “time”, “dt”, “pwd”, and “!”. Each one is described below.

To output a “%”, use two percent signs (“%%”). To put a “%” inside of a variable name, use “\%”.

%number%

Replaces the variable with an argument specified on the command line, where number is the argument number. “%1%” is replaced by the first argument, “%2%” is replaced by the second, etc.

%include:filename%

This includes the file specified by filename. It does not process the file for variables.

%date:format%

Formats the date and time using the text following the colon. See the table below for the formatting characters. Date and time specifiers are preceded by a “$”. For example, “$Y” would output the current year. The general pattern is a single letter for the simplest form. A zero before the letter adds a leading zero. Higher numbers add to the length of the output. Any digit higher than the specified digits are treated as if the highest valid number had been used. For example, “$3m” is the same as “$2m”. Invalid specifiers are not output. I might eventually add support for Roman numeral years.

Date Time Format Specifiers
Specifier Output Example
y Year, two digits 01 - 99
Y Year, full 1 - 9999
m Month number 1 - 12
0m Month number, leading zero 01 - 12
1m Month name, three letters Jan - Dec
2m Month name January - December
d Day of month 1 - 31
0d Day of month, leading zero 01 - 31
D Day of year 1 - 366
0D Day of year, leading zeros 001 - 366
w Weekday number 1 - 7
0w Weekday number, leading zero 01 - 07
1w Weekday name, one letter S M T W T F S
2w Weekday name, two letters Su M Tu W Th F Sa
3w Weekday name, three letters Sun Mon Tue Wed Thu Fri Sat
4w Weekday name, three or four letters Sun Mon Tues Wed Thurs Fri Sat
5w Weekday name, full Sunday - Saturday
W Week of year 1 - 53
0W Week of year, leading zero 01 - 53
h Hour 1 - 12
0h Hour, leading zero 01 - 12
H Hour, 24 hour 1 - 23
0H Hour, 24 hour, leading zero 01 - 23
n Minute 1 - 59
0n Minute, leading zero 01 - 59
s Second 1 - 59
0s Second, leading zero 01 - 59
a AM/PM, one letter, lower case a p
am AM/PM, two letters, lower case am pm
A AM/PM, one letter, upper case A P
AM AM/PM, two letters, upper case AM PM
t Timezone, number -0500
T Timezone, abbreviated name EST

%date%

Outputs the date in the standard “day/month/year” format (4/5/1983). Equivalent to “%date:$m/$d/$Y%”.

%time%

Outputs the time in the normal “HH:MM:SS A/PM” format (1:05:49 PM). Equivalent to “%date:$h:$0n:$0s $AM%”.

%dt%

Outputs the date and time. Equivalent to “%date:$m/$d/$Y $h:$0n:$0s $AM%” or “%date% %time%”.

%pwd%

Prints the current working directory. This was added because I needed to use it once. I have not used it since. It could be used to print the input file name and path. You would have to pass the filename as a program argument. For example, if you called the program with exp filename <filename, then you could print the full path and filename with “%dir%/%1%”.

%!command%

Run the command following the “!” and substitute it’s output. This is simular to a shell escape in many UNIX programs. The command is interpreted by bash on UNIX/Linux and either command or cmd on windows.

%variable%

Prints the contents of the environment variable specified by variable, if it exists. Otherwise, it results in the empty string.

Bugs

There is currently no simple way to access an environment variable called “1” or similar if there are arguments passed in with the command line. A workaround is to use “%!echo $1%” on UNIX or “%!echo \%1\%%” on Windows.

This is a limitation, not a bug, but variable names can only be up to 1024 characters long.

Help!

If you need help with this program, contact me.

Download

Pre-compiled Windows version: exp.zip (49.8 KB)

Source Code: exp-src.zip (3.67 KB)

The source code includes a simple Makefile to compile the code. Just run “make” on Linux or “make windows” on Windows. Or you can do it maually by running

gcc -o exp exp.c execute-unix.c

You should end up with a file called “exp”. You can do whatever you want with it. I put it in ~/bin/.