#include "lpsched.h"
#include <syslog.h>
CLSTATUS **CStatus = NULL;
PSTATUS **PStatus = NULL;
FSTATUS **FStatus = NULL;
PWSTATUS **PWStatus = NULL;
EXEC **Exec_Table = NULL;
EXEC **Exec_Slow = NULL;
EXEC **Exec_Notify = NULL;
RSTATUS *Request_List = NULL;
int ET_SlowSize = 1,
ET_NotifySize = 1;
static void init_printers(),
init_classes(),
init_forms(),
init_pwheels(),
init_exec();
static void init_requests();
void
init_memory(void)
{
init_exec();
init_printers();
init_classes();
init_forms();
init_pwheels();
load_status();
Loadfilters(Lp_A_Filters);
init_requests();
}
static void
init_printers()
{
PRINTER *p;
int i = 0;
while((p = Getprinter(NAME_ALL)) != NULL || errno != ENOENT) {
if ((!p) || (p->remote))
continue;
(void) new_pstatus(p);
syslog(LOG_DEBUG, "Loaded printer: %s", p->name);
}
}
static void
init_classes()
{
CLASS *c;
while((c = Getclass(NAME_ALL)) != NULL) {
(void) new_cstatus(c);
syslog(LOG_DEBUG, "Loaded class: %s", c->name);
}
}
static void
init_forms()
{
_FORM *f;
int i = 0;
while ((f = Getform(NAME_ALL)) != NULL) {
(void) new_fstatus(f);
syslog(LOG_DEBUG, "Loaded form: %s", f->name);
}
}
static void
init_pwheels()
{
PWHEEL *p;
int i = 0;
while((p = Getpwheel(NAME_ALL)) != NULL || errno != ENOENT)
{
if (!p)
continue;
(void) new_pwstatus(p);
syslog(LOG_DEBUG, "Loaded print-wheel: %s", p->name);
}
}
static void
init_requests(void)
{
RSTATUS **table = NULL;
REQUEST *r;
SECURE *s;
char *name;
char *sysdir;
char *sysname;
char *reqfile = NULL;
long addr = -1;
long sysaddr = -1;
short vr_ret;
while((sysname = next_dir(Lp_Requests, &addr)) != NULL) {
RSTATUS *rsp;
sysdir = makepath(Lp_Requests, sysname, NULL);
while((name = next_file(sysdir, &sysaddr)) != NULL) {
reqfile = makepath(sysname, name, NULL);
Free(name);
if ((s = Getsecure(reqfile)) == NULL) {
RSTATUS tmp;
memset(&tmp, 0, sizeof (tmp));
tmp.req_file = reqfile;
rmfiles(&tmp, 0);
free(tmp.req_file);
continue;
}
syslog(LOG_DEBUG, "Loaded request: %s", reqfile);
if((r = Getrequest(reqfile)) == NULL) {
RSTATUS tmp;
memset(&tmp, 0, sizeof (tmp));
tmp.req_file = reqfile;
rmfiles(&tmp, 0);
freesecure(s);
free(tmp.req_file);
continue;
}
syslog(LOG_DEBUG, "Loaded secure: %s", s->req_id);
rsp = new_rstatus(r, s);
r->outcome &= ~RS_ACTIVE;
rsp->req_file = reqfile;
if ((r->outcome & (RS_CANCELLED|RS_FAILED)) &&
!(r->outcome & RS_NOTIFY)) {
rmfiles(rsp, 0);
free_rstatus(rsp);
continue;
}
if (r->outcome & RS_NOTIFY) {
char *file = makereqerr(rsp);
if (Access(file, F_OK) == -1) {
if (!(r->outcome & RS_CANCELLED)) {
Free(file);
rmfiles(rsp, 0);
free_rstatus(rsp);
continue;
}
notify(rsp, NULL, 0, 0, 0);
}
Free(file);
}
if ((vr_ret=validate_request(rsp, NULL, 1)) != MOK) {
if (vr_ret == MNODEST || (rsp->printer == NULL)) {
rmfiles(rsp, 0);
free_rstatus(rsp);
continue;
}
cancel(rsp, 1);
}
list_append((void ***)&table, (void *)rsp);
}
Free(sysdir);
Free(sysname);
sysaddr = -1;
}
if (table != NULL) {
unsigned long i;
for (i = 0; table[i] != NULL; i++);
qsort((void *)table, i, sizeof(RSTATUS *),
(int (*)(const void * , const void *))rsort);
for (i = 0; table[i] != NULL; i++) {
table[i]->next = table[i + 1];
if (table[i + 1] != NULL)
table[i + 1]->prev = table[i];
}
Request_List = *table;
Free(table);
}
}
static void
init_exec()
{
EXEC *ep;
int i;
for (i = 0; i < ET_SlowSize; i++) {
ep = new_exec(EX_SLOWF, NULL);
list_append((void ***)&Exec_Slow, (void *)ep);
}
for (i = 0; i < ET_NotifySize; i++) {
ep = new_exec(EX_NOTIFY, NULL);
list_append((void ***)&Exec_Notify, (void *)ep);
}
}