#include "includes.h"
#ifdef AIX
static int strlocate(char *xpLine,char *xpS)
{
int iS,iL,iRet;
char *p;
iS = strlen(xpS);
iL = strlen(xpLine);
iRet = 0;
p = xpLine;
while (iL >= iS)
{
if (strncmp(p,xpS,iS) == 0) {iRet =1;break;};
p++;
iL--;
}
return(iRet);
}
static void ScanQconfig_fn(char *psz,void (*fn)(char *, char *))
{
int iEtat;
XFILE *pfile;
char *line,*p;
pstring name,comment;
line = NULL;
*name = 0;
*comment = 0;
if ((pfile = x_fopen(psz, O_RDONLY, 0)) == NULL)
{
DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
return;
}
iEtat = 0;
for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); safe_free(line))
{
if (*line == '*' || *line == 0)
continue;
switch (iEtat)
{
case 0:
if (*line == '\t' || *line == ' ') continue;
if ((p=strchr_m(line,':')))
{
*p = '\0';
p = strtok(line,":");
if (strcmp(p,"bsh")!=0)
{
pstrcpy(name,p);
iEtat = 1;
continue;
}
}
break;
case 1:
if (*line == '*' || *line == 0) continue;
if (*line != '\t' && *line != ' ')
{
fn(name,comment);
iEtat = 0;
continue;
}
if (strlocate(line,"backend"))
{
iEtat = 0;
}
else if (strlocate(line,"device"))
{
fn(name,comment);
iEtat = 0;
continue;
}
break;
}
}
x_fclose(pfile);
}
static BOOL ScanQconfig(char *psz,char *pszPrintername)
{
int iLg,iEtat;
XFILE *pfile;
char *pName;
char *line;
pName = NULL;
line = NULL;
if ((pszPrintername!= NULL) && ((iLg = strlen(pszPrintername)) > 0))
pName = malloc(iLg+10);
if (pName == NULL)
{
DEBUG(0,(" Unable to allocate memory for printer %s\n",pszPrintername));
return(False);
}
if ((pfile = x_fopen(psz, O_RDONLY, 0)) == NULL)
{
DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
SAFE_FREE(pName);
return(False);
}
slprintf(pName, iLg + 9, "%s:",pszPrintername);
iLg = strlen(pName);
iEtat = 0;
for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); safe_free(line))
{
if (*line == '*' || *line == 0)
continue;
switch (iEtat)
{
case 0:
if (strncmp(line,pName,iLg) == 0)
{
iEtat = 1;
continue;
}
break;
case 1:
if (*line == '*' || *line == 0) continue;
if (*line != '\t' && *line != ' ')
{
free (line);
SAFE_FREE(pName);
x_fclose(pfile);
return(True);
}
if (strlocate(line,"backend"))
{
iEtat = 0;
}
else if (strlocate(line,"device"))
{
free (line);
SAFE_FREE(pName);
x_fclose(pfile);
return(True);
}
break;
}
}
free (pName);
x_fclose(pfile);
return(False);
}
#endif
BOOL pcap_printername_ok(const char *pszPrintername, const char *pszPrintcapname)
{
char *line=NULL;
const char *psz;
char *p,*q;
XFILE *pfile;
if (pszPrintername == NULL || pszPrintername[0] == '\0')
{
DEBUG(0,( "Attempt to locate null printername! Internal error?\n"));
return(False);
}
if ((psz = pszPrintcapname) == NULL || psz[0] == '\0')
if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
{
DEBUG(0,( "No printcap file name configured!\n"));
return(False);
}
#ifdef HAVE_CUPS
if (strequal(psz, "cups"))
return (cups_printername_ok(pszPrintername));
#endif
#ifdef SYSV
if (strequal(psz, "lpstat"))
return (sysv_printername_ok(pszPrintername));
#endif
#ifdef AIX
if (strlocate(psz,"/qconfig"))
return(ScanQconfig(psz,pszPrintername));
#endif
if ((pfile = x_fopen(psz, O_RDONLY, 0)) == NULL)
{
DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
return(False);
}
for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); safe_free(line))
{
if (*line == '#' || *line == 0)
continue;
p = strchr_m(line,':');
if (p) *p = 0;
for (p=line; p; p=q)
{
if ((q = strchr_m(p,'|'))) *q++ = 0;
if (strequal(p,pszPrintername))
{
SAFE_FREE(line);
x_fclose(pfile);
return(True);
}
p = q;
}
}
x_fclose(pfile);
return(False);
}
void pcap_printer_fn(void (*fn)(char *, char *))
{
pstring name,comment;
char *line;
char *psz;
char *p,*q;
XFILE *pfile;
if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
{
DEBUG(0,( "No printcap file name configured!\n"));
return;
}
#ifdef HAVE_CUPS
if (strequal(psz, "cups")) {
cups_printer_fn(fn);
return;
}
#endif
#ifdef SYSV
if (strequal(psz, "lpstat")) {
sysv_printer_fn(fn);
return;
}
#endif
#ifdef AIX
if (strlocate(psz,"/qconfig"))
{
ScanQconfig_fn(psz,fn);
return;
}
#endif
if ((pfile = x_fopen(psz, O_RDONLY, 0)) == NULL)
{
DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
return;
}
for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); safe_free(line))
{
if (*line == '#' || *line == 0)
continue;
p = strchr_m(line,':');
if (p) *p = 0;
*name = 0;
*comment = 0;
for (p=line; p; p=q)
{
BOOL has_punctuation;
if ((q = strchr_m(p,'|'))) *q++ = 0;
has_punctuation = (strchr_m(p,' ') || strchr_m(p,'\t') || strchr_m(p,'(') || strchr_m(p,')'));
if (strlen(p)>strlen(comment) && has_punctuation)
{
pstrcpy(comment,p);
continue;
}
if (strlen(p) <= MAXPRINTERLEN && strlen(p)>strlen(name) && !has_punctuation)
{
if (!*comment) pstrcpy(comment,name);
pstrcpy(name,p);
continue;
}
if (!strchr_m(comment,' ') &&
strlen(p) > strlen(comment))
{
pstrcpy(comment,p);
continue;
}
}
comment[60] = 0;
name[MAXPRINTERLEN] = 0;
if (*name)
fn(name,comment);
}
x_fclose(pfile);
}