#include "ctype.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <libintl.h>
#include "lp.h"
#include "printers.h"
#define WHO_AM_I I_AM_LPADMIN
#include "oam.h"
#include "lpadmin.h"
#ifdef LP_USE_PAPI_ATTR
#if defined(CAN_DO_MODULES)
#define OPT_LIST "A:ac:d:D:e:f:F:H:hi:I:lm:Mn:o:p:Q:r:S:s:T:u:U:v:W:x:t:P:"
#else
#define OPT_LIST "A:ac:d:D:e:f:F:hi:I:lm:Mn:o:p:Q:r:S:s:T:u:U:v:W:x:t:P:"
#endif
#else
#if defined(CAN_DO_MODULES)
#define OPT_LIST "A:ac:d:D:e:f:F:H:hi:I:lm:Mo:p:Q:r:S:s:T:u:U:v:W:x:t:P:"
#else
#define OPT_LIST "A:ac:d:D:e:f:F:hi:I:lm:Mo:p:Q:r:S:s:T:u:U:v:W:x:t:P:"
#endif
#endif
#define MALLOC(pointer) \
if (!(pointer = strdup(optarg))) { \
LP_ERRMSG (ERROR, E_LP_MALLOC); \
done (1); \
} else
#define REALLOC(pointer) \
if (!(pointer = realloc(pointer, (unsigned) (strlen(pointer) + 1 + strlen(optarg) + 1)))) { \
LP_ERRMSG (ERROR, E_LP_MALLOC); \
done (1); \
} else if (strcat(pointer, " ")) \
(void)strcat (pointer, optarg); \
else
extern char *optarg;
extern int optind,
opterr,
optopt;
extern double strtod();
extern long strtol();
int a = 0,
banner = -1,
#if defined(DIRECT_ACCESS)
C = 0,
#endif
filebreak = 0,
h = 0,
j = 0,
l = 0,
M = 0,
t = 0,
o = 0,
Q = -1,
W = -1;
char *A = 0,
*c = 0,
*cpi = 0,
*d = 0,
*D = 0,
*e = 0,
*f = 0,
*P = 0,
*F = 0,
**H = 0,
*i = 0,
**I = 0,
*length = 0,
*lpi = 0,
*m = 0,
modifications[128],
#ifdef LP_USE_PAPI_ATTR
*n_opt = NULL,
#endif
*p = 0,
*r = 0,
*s = 0,
*stty_opt= 0,
**o_options = 0,
**S = 0,
**T = 0,
*u = 0,
*U = 0,
*v = 0,
*width = 0,
*x = 0;
SCALED cpi_sdn = { 0, 0 },
length_sdn = { 0, 0 },
lpi_sdn = { 0, 0 },
width_sdn = { 0, 0 };
static char *modp = modifications;
static void oparse();
static char * empty_list[] = { 0 };
void options (argc, argv)
int argc;
char *argv[];
{
int optsw,
ac,
Aflag = 0;
char *cp,
*rest,
**av;
char stroptsw[] = "-X";
#if defined(__STDC__)
typedef char * const * stupid;
#else
typedef char ** stupid;
#endif
av = malloc((argc + 2) * sizeof(char *));
for (ac = 0; ac < argc; ac++)
av[ac] = argv[ac];
av[ac++] = "--";
opterr = 0;
while ((optsw = getopt(ac, (stupid)av, OPT_LIST)) != EOF) {
switch (optsw) {
case 'd':
case 'p':
case 'I':
#if defined(CAN_DO_MODULES)
case 'H':
#endif
if (*optarg == '-') {
optind--;
switch (optsw) {
case 'd':
#if defined(CAN_DO_MODULES)
case 'H':
#endif
optarg = NAME_NONE;
break;
case 'p':
optarg = NAME_ALL;
break;
case 'I':
optarg = 0;
break;
}
}
break;
case 'Q':
case 'W':
case 't':
(void)strtol(optarg, &rest, 10);
if (!rest || !*rest)
break;
case 'A':
case 'c':
case 'e':
case 'f':
case 'P':
case 'F':
case 'i':
case 'm':
#ifdef LP_USE_PAPI_ATTR
case 'n':
#endif
case 'o':
case 'r':
case 'S':
case 's':
case 'T':
case 'u':
case 'U':
case 'v':
case 'x':
if (!*optarg) {
stroptsw[1] = optsw;
LP_ERRMSG1 (ERROR, E_LP_NULLARG, stroptsw);
done (1);
}
if (*optarg == '-') {
stroptsw[1] = optsw;
LP_ERRMSG1 (ERROR, E_LP_OPTARG, stroptsw);
done (1);
}
if (optsw == 'A')
Aflag++;
break;
case 'D':
if (*optarg == '-') {
stroptsw[1] = optsw;
LP_ERRMSG1 (ERROR, E_LP_OPTARG, stroptsw);
done (1);
}
break;
}
switch (optsw) {
case 'a':
a = 1;
break;
case 'A':
if (A)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'A');
MALLOC(A);
Aflag++;
if (!STREQU(A, NAME_QUIET) && !STREQU(A, NAME_LIST))
*modp++ = 'A';
break;
case 'c':
if (c)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'c');
MALLOC(c);
break;
#if defined(DIRECT_ACCESS)
case 'C':
C = 1;
break;
#endif
case 'd':
if (d)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'd');
MALLOC(d);
break;
case 'D':
if (D)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'D');
MALLOC(D);
*modp++ = 'D';
break;
case 'e':
if (e)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'e');
MALLOC(e);
*modp++ = 'e';
break;
case 'f':
if (f)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'f');
MALLOC(f);
break;
case 'P':
if (P)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'P');
MALLOC(P);
break;
case 'F':
if (F)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'F');
MALLOC(F);
*modp++ = 'F';
break;
#if defined(CAN_DO_MODULES)
case 'H':
if (H)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'H');
if (!optarg || !*optarg || STREQU(NAME_NONE, optarg))
H = empty_list;
if (!(H = getlist(optarg, LP_WS, LP_SEP))) {
LP_ERRMSG (ERROR, E_LP_MALLOC);
done(1);
}
*modp++ = 'H';
break;
#endif
case 'h':
h = 1;
*modp++ = 'h';
break;
case 'i':
if (i)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'i');
MALLOC(i);
*modp++ = 'i';
break;
case 'I':
if (I)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'I');
if (!optarg || !*optarg || STREQU(NAME_NONE, optarg))
I = empty_list;
else if (!(I = getlist(optarg, LP_WS, LP_SEP))) {
LP_ERRMSG (ERROR, E_LP_MALLOC);
done (1);
}
*modp++ = 'I';
break;
#if defined(J_OPTION)
case 'j':
j = 1;
(void) printf (gettext("Sorry, the -j option is currently broken\n"));
break;
#endif
case 'l':
l = 1;
*modp++ = 'l';
break;
case 'm':
if (m)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'm');
MALLOC(m);
*modp++ = 'm';
break;
#ifdef LP_USE_PAPI_ATTR
case 'n':
if (n_opt)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'n');
MALLOC(n_opt);
*modp++ = 'n';
break;
#endif
case 'M':
M = 1;
break;
case 'o':
oparse (optarg);
o = 1;
break;
case 'p':
if (p)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'p');
MALLOC(p);
break;
case 'Q':
if (Q != -1)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'Q');
if (STREQU(NAME_ANY, optarg))
Q = 1;
else {
Q = strtol(optarg, &rest, 10);
if (Q < 0) {
LP_ERRMSG1 (ERROR, E_LP_NEGARG, 'Q');
done (1);
}
if (rest && *rest) {
LP_ERRMSG1 (ERROR, E_LP_GARBNMB, 'Q');
done (1);
}
if (Q == 0) {
LP_ERRMSG1 (ERROR, E_ADM_ZEROARG, 'Q');
done (1);
}
}
*modp++ = 'Q';
break;
case 'r':
if (r)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'r');
MALLOC(r);
break;
case 'S':
if (S)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'S');
if (!(S = getlist(optarg, LP_WS, LP_SEP))) {
LP_ERRMSG (ERROR, E_LP_MALLOC);
done (1);
}
*modp++ = 'S';
break;
case 's':
if (s)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 's');
if ((cp = strchr(optarg, '!')))
*cp = '\0';
if ((STREQU(optarg, NAME_NONE)) ||
(STREQU(optarg, "localhost")))
s = Local_System;
else if (STREQU(optarg, Local_System)) {
if (cp) {
LP_ERRMSG (ERROR, E_ADM_NAMEONLOCAL);
done(1);
} else
s = Local_System;
} else {
if (cp)
*cp = '!';
MALLOC(s);
}
*modp++ = 'R';
break;
case 't':
if (t != 0) LP_ERRMSG1 (WARNING, E_LP_2MANY, 't');
t = strtol(optarg, &rest, 10);
if (t <= 0) {
LP_ERRMSG1 (ERROR, E_LP_NEGARG, 't');
done (1);
}
if (rest && *rest) {
LP_ERRMSG1 (ERROR, E_LP_GARBNMB, 't');
done (1);
}
break;
case 'T':
if (T)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'T');
if (!(T = getlist(optarg, LP_WS, LP_SEP))) {
LP_ERRMSG (ERROR, E_LP_MALLOC);
done (1);
}
*modp++ = 'T';
break;
case 'u':
if (u)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'u');
MALLOC(u);
break;
case 'U':
if (U)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'U');
MALLOC(U);
*modp++ = 'U';
break;
case 'v':
if (v)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'v');
MALLOC(v);
*modp++ = 'v';
break;
case 'W':
if (W != -1)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'W');
if (STREQU(NAME_ONCE, optarg))
W = 0;
else {
W = strtol(optarg, &rest, 10);
if (W < 0) {
LP_ERRMSG1 (ERROR, E_LP_NEGARG, 'W');
done (1);
}
if (rest && *rest) {
LP_ERRMSG1 (ERROR, E_LP_GARBNMB, 'W');
done (1);
}
}
*modp++ = 'W';
break;
case 'x':
if (x)
LP_ERRMSG1 (WARNING, E_LP_2MANY, 'x');
MALLOC(x);
break;
default:
if (optopt == '?') {
usage ();
done (0);
} else {
stroptsw[1] = optsw;
if (strchr(OPT_LIST, optopt))
LP_ERRMSG1 (ERROR, E_LP_OPTARG,
stroptsw);
else
LP_ERRMSG1 (ERROR, E_LP_OPTION,
stroptsw);
done (1);
}
}
}
if (optind < argc)
LP_ERRMSG1 (WARNING, E_LP_EXTRA, argv[optind]);
if ((v) && (!Aflag)) {
if (!(A = strdup("write"))) {
LP_ERRMSG (ERROR, E_LP_MALLOC);
done (1);
}
*modp++ = 'A';
}
return;
}
static void oparse (optarg)
char *optarg;
{
register char **list = dashos(optarg);
if (!list)
return;
for ( ; (optarg = *list); list++)
if (STREQU(optarg, "banner")) {
if (banner != -1)
LP_ERRMSG1 (
WARNING,
E_ADM_2MANY,
"banner/nobanner"
);
banner = BAN_ALWAYS;
*modp++ = 'b';
} else if (STREQU(optarg, "nobanner")) {
if (banner != -1)
LP_ERRMSG1 (
WARNING,
E_ADM_2MANY,
"banner/nobanner"
);
banner = BAN_OPTIONAL;
*modp++ = 'b';
} else if (STRNEQU(optarg, "banner=", 7)) {
char *ptr;
ptr = (optarg += 7);
if (banner != -1)
LP_ERRMSG1 ( WARNING, E_ADM_2MANY,
"banner/nobanner/banner=(always|optional|never)"
);
if (strcasecmp(ptr, "always") == 0)
banner = BAN_ALWAYS;
if (strcasecmp(ptr, "optional") == 0)
banner = BAN_OPTIONAL;
if (strcasecmp(ptr, "never") == 0)
banner = BAN_NEVER;
*modp++ = 'b';
} else if (STRNEQU(optarg, "length=", 7)) {
if (length)
LP_ERRMSG1 (
WARNING,
E_ADM_2MANY,
"length="
);
length = (optarg += 7);
if (!*optarg) {
length_sdn.val = 0;
length_sdn.sc = 0;
} else {
length_sdn = _getsdn(optarg, &optarg, 0);
if (errno == EINVAL) {
LP_ERRMSG (ERROR, E_LP_BADSCALE);
done (1);
}
}
*modp++ = 'L';
} else if (STRNEQU(optarg, "width=", 6)) {
if (width)
LP_ERRMSG1 (
WARNING,
E_ADM_2MANY,
"width="
);
width = (optarg += 6);
if (!*optarg) {
width_sdn.val = 0;
width_sdn.sc = 0;
} else {
width_sdn = _getsdn(optarg, &optarg, 0);
if (errno == EINVAL) {
LP_ERRMSG (ERROR, E_LP_BADSCALE);
done (1);
}
}
*modp++ = 'w';
} else if (STRNEQU(optarg, "cpi=", 4)) {
if (cpi)
LP_ERRMSG1 (WARNING, E_ADM_2MANY, "cpi=");
cpi = (optarg += 4);
if (!*optarg) {
cpi_sdn.val = 0;
cpi_sdn.sc = 0;
} else {
cpi_sdn = _getsdn(optarg, &optarg, 1);
if (errno == EINVAL) {
LP_ERRMSG (ERROR, E_LP_BADSCALE);
done (1);
}
}
*modp++ = 'c';
} else if (STRNEQU(optarg, "lpi=", 4)) {
if (lpi)
LP_ERRMSG1 (WARNING, E_ADM_2MANY, "lpi=");
lpi = (optarg += 4);
if (!*optarg) {
lpi_sdn.val = 0;
lpi_sdn.sc = 0;
} else {
lpi_sdn = _getsdn(optarg, &optarg, 0);
if (errno == EINVAL) {
LP_ERRMSG (ERROR, E_LP_BADSCALE);
done (1);
}
}
*modp++ = 'M';
} else if (STRNEQU(optarg, "stty=", 5)) {
optarg += 5;
if (!*optarg)
stty_opt = 0;
else {
if (strchr(LP_QUOTES, *optarg)) {
register int len
= strlen(optarg);
if (optarg[len - 1] == *optarg)
optarg[len - 1] = 0;
optarg++;
}
if (stty_opt)
REALLOC (stty_opt);
else
MALLOC (stty_opt);
}
*modp++ = 's';
} else if (STREQU(optarg, "filebreak")) {
filebreak = 1;
} else if (STREQU(optarg, "nofilebreak")) {
filebreak = 0;
} else if (*optarg) {
if ((addlist(&o_options, optarg)) != 0) {
fprintf(stderr, gettext("System Error %d\n"), errno);
}
*modp++ = 'o';
optarg++;
}
return;
}