#include "hpgltops.h"
#include <ctype.h>
#define MAX_PARAMS 16384
int
ParseCommand(FILE *fp,
char *name,
param_t **params)
{
int num_params,
ch,
done,
i;
char buf[262144],
*bufptr;
static param_t p[MAX_PARAMS];
num_params = 0;
done = 0;
do
{
while ((ch = getc(fp)) != EOF)
if (strchr(" \t\r\n,;", ch) == NULL)
break;
if (ch == EOF)
{
return (-1);
}
if (ch == 0x1b)
switch (getc(fp))
{
case '.' :
i = getc(fp);
if (strchr(")Z", i) != NULL)
{
for (;;)
{
while ((i = getc(fp)) != EOF && i != 0x1b);
if (i == EOF)
return (-1);
if (getc(fp) != '.')
continue;
if ((i = getc(fp)) == '(' ||
i == 'Y')
break;
}
}
else if (strchr("@HIMNTI\003", i) != NULL)
{
while ((i = getc(fp)) != EOF && i != ':');
}
break;
case '%' :
if ((i = getc(fp)) == '-')
if ((i = getc(fp)) == '1')
if ((i = getc(fp)) == '2')
{
while (fgets(buf, sizeof(buf), fp) != NULL)
if (strstr(buf, "ENTER") && strstr(buf, "LANGUAGE"))
break;
break;
}
ungetc(i, fp);
default :
while ((i = getc(fp)) != EOF && !isupper(i & 255));
break;
}
} while (ch < ' ');
name[0] = ch;
name[1] = getc(fp);
name[2] = '\0';
if (strcasecmp(name, "LB") == 0)
{
bufptr = buf;
while ((ch = getc(fp)) != StringTerminator)
if (bufptr < (buf + sizeof(buf) - 1))
*bufptr++ = ch;
*bufptr = '\0';
p[num_params].type = PARAM_STRING;
p[num_params].value.string = strdup(buf);
num_params ++;
}
else if (strcasecmp(name, "SM") == 0)
{
buf[0] = getc(fp);
buf[1] = '\0';
p[num_params].type = PARAM_STRING;
p[num_params].value.string = strdup(buf);
num_params ++;
}
else if (strcasecmp(name, "DT") == 0)
{
if ((buf[0] = getc(fp)) != ';')
{
buf[1] = '\0';
p[num_params].type = PARAM_STRING;
p[num_params].value.string = strdup(buf);
num_params ++;
}
}
else if (strcasecmp(name, "PE") == 0)
{
bufptr = buf;
while ((ch = getc(fp)) != ';')
if (bufptr < (buf + sizeof(buf) - 1))
*bufptr++ = ch;
*bufptr = '\0';
p[num_params].type = PARAM_STRING;
p[num_params].value.string = strdup(buf);
num_params ++;
}
while (!done)
switch (ch = getc(fp))
{
case ',' :
case ' ' :
case '\n' :
case '\r' :
case '\t' :
break;
case '\"' :
fscanf(fp, "%262143[^\"]\"", buf);
if (num_params < MAX_PARAMS)
{
p[num_params].type = PARAM_STRING;
p[num_params].value.string = strdup(buf);
num_params ++;
};
break;
case '-' :
case '+' :
ungetc(ch, fp);
fscanf(fp, "%f", &(p[num_params].value.number));
if (num_params < MAX_PARAMS)
{
p[num_params].type = PARAM_RELATIVE;
num_params ++;
}
break;
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
case '.' :
ungetc(ch, fp);
fscanf(fp, "%f", &(p[num_params].value.number));
if (num_params < MAX_PARAMS)
{
p[num_params].type = PARAM_ABSOLUTE;
num_params ++;
}
break;
default :
ungetc(ch, fp);
done = 1;
break;
}
*params = p;
return (num_params);
}
void
FreeParameters(int num_params,
param_t *params)
{
int i;
for (i = 0; i < num_params; i ++)
if (params[i].type == PARAM_STRING)
free(params[i].value.string);
}