#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
#include "lp.h"
#include "filters.h"
static void q_print ( int , char * );
static char *fw_zDblQte (char *zBuf);
int
dumpfilters(char *file)
{
register _FILTER *pf;
register TEMPLATE *pt;
register TYPE *pty;
register char *p,
*sep;
register int fld;
int fd;
if ((fd = open_filtertable(file, "w")) < 0)
return (-1);
printlist_setup ("", "", LP_SEP, "");
if (filters) for (pf = filters; pf->name; pf++) {
for (fld = 0; fld < FL_MAX; fld++) switch (fld) {
case FL_IGN:
break;
case FL_NAME:
p = pf->name;
goto String;
case FL_CMD:
if ((p = fw_zDblQte (pf->command)) != NULL) {
(void)fdprintf (fd, "%s%s", FL_SEP, p);
free (p);
break;
}
p = pf->command;
String: (void)fdprintf (fd, "%s%s", FL_SEP, (p? p : ""));
break;
case FL_TYPE:
(void)fdprintf(fd, "%s%s", FL_SEP,
(pf->type == fl_fast? FL_FAST : FL_SLOW));
break;
case FL_PTYPS:
pty = pf->printer_types;
goto Types;
case FL_ITYPS:
pty = pf->input_types;
goto Types;
case FL_OTYPS:
pty = pf->output_types;
Types: (void)fdprintf(fd, "%s", FL_SEP);
sep = "";
if (pty) {
for (; pty->name; pty++) {
(void)fdprintf(fd, "%s%s", sep,
pty->name);
sep = ",";
}
} else
(void)fdprintf(fd, "%s", NAME_ANY);
break;
case FL_PRTRS:
(void)fdprintf(fd, "%s", FL_SEP);
if (pf->printers)
fdprintlist (fd, pf->printers);
else
(void)fdprintf(fd, "%s", NAME_ANY);
break;
case FL_TMPS:
(void)fdprintf(fd, "%s", FL_SEP);
sep = "";
if ((pt = pf->templates))
for(; pt->keyword; pt++) {
(void)fdprintf(fd, "%s%s ", sep,
pt->keyword);
q_print(fd, pt->pattern);
(void)fdprintf(fd, " = ");
q_print(fd, pt->result);
sep = ",";
}
break;
}
(void)fdprintf(fd, FL_END);
}
close(fd);
return (0);
}
static void
q_print(int fd, char *str)
{
if (!str)
return;
while (*str) {
if (
*str == '\\'
|| strchr(FL_SEP, *str)
|| strchr(LP_SEP, *str)
|| strchr("=", *str)
)
fdputc ('\\', fd);
fdputc (*str, fd);
str++;
}
return;
}
static char *fw_zDblQte (char *zBuf)
{
char *zT;
int i;
int j;
int iNewSize;
for (i = j = 0; zBuf[i]; i++) {
if (zBuf[i] == '"') {
j++;
}
}
iNewSize = (strlen (zBuf) + 3 + j);
if ((zT = malloc (iNewSize)) == NULL) {
return (NULL);
}
j = 0;
zT[j++] = '"';
for (i = 0; zBuf[i]; i++) {
if (zBuf[i] == '"') {
zT[j++] = '\\';
}
zT[j++] = zBuf[i];
}
zT[j++] = '"';
zT[j] = '\0';
return (zT);
}