Listing 5 The Draw function using the Simple Drawing Language in Table 1

#include <stdio.h>

#define PENUP()      pen_up=1
#define PENDOWN()    pen_up=0
#define SETCOLOR(x)  color=x
#define SETWIDTH(x)  width=x
#define BLACK        0
#define RED          1
#define GREEN        2
#define BLUE         3
#define FAT          0
#define MEDIUM       1
#define THIN         2

#define TRUE         1
#define FALSE        0

int pen_up;
int color;
int width;
int x1, x2, y1, y2;
int gotx1, gotx2, goty1, goty2;

/* Function Draw - demonstrates use of the Simple Drawing Language
Does not actually draw lines, simple parses the first parameter and
prints commands on screen */

Draw (char *str, int val)
{
int *pval=&val;      //points to optional parameters
                  //restore defaults
   gotx1=goty1=gotx2=goty2=FALSE;
   x1=x2=y1=y2=0;
   pen_up=1;
   color = BLACK;
   width=MEDIUM;
                  //loop to parse the command string
   while (TRUE)
   {
      switch (*str++)
      {
         case 'p': PENUP(); break;    //pick pen up
         case 'P': PENDOWN(); break;  //put pen down
         case 'c':                    //set color
            switch (*str++)
            {
               case 'R': SETCOLOR(RED); printf ("Color set to red\n");break;
               case 'G': SETCOLOR(GREEN); printf ("Color set to green\n");break;
               case 'B': SETCOLOR(BLUE); printf ("Color set to blue\n");break;
               case 'b': SETCOLOR(BLACK); printf ("Color set to black\n");break;
                default: return -1;
            } //End switch (on character)
            break;
         case 'w':                    //set width
            switch (*str++)
            {
               case 'T':  SETWIDTH(THIN); printf ("Width set to thin\n");break;
               case 'M':  SETWIDTH(MEDIUM); printf ("Width set to medium\n");break;
               case 'F':  SETWIDTH(FAT); printf ("Width set to fat\n");break;
                default:   return -1;
            } //End switch (on character)
            break;
         case '%':                      //get next optional parameter
            switch (*str++)
            {
               case 'x':              //set x coordinate
                  if (gotx2) {x1=x2; x2=*pval++; break;}
                  if (!gotx1) {x1=*pval++; gotx1=TRUE; break;}
                  if (!gotx2) {x2=*pval++; gotx2=TRUE; break;}
                  break;
               case 'y':  //set y coordinate
                  if (goty2) {y1=y2; y2=*pval++; break;}
                  if (!goty1) {y1=*pval++; goty1=TRUE; break;}
                  if (!goty2) {y2=*pval++; goty2=TRUE; break;}
                  break;
                default: return -1;
            } //End switch (token)
            //do we have enough info to draw the line?
            if (gotx2 && goty2 && !pen_up)
            {
               printf ("Drawing line <%d,%d>-<%d,%d>\n", x1,y1,x2,y2);
               x1 = x2;
               y1 = y2;
               goty2 = FALSE;
               gotx2 = FALSE;
            } //end if (got both coordinates - draw line)
            break;
         case '\0':           //end of command string
            return 0;
         default: return -1;
      } //End switch (on character)
   } //End while (TRUE)
   return 0;
} //End function (Draw)
/* End of File */