📄 pgpconf.c
字号:
if (!opt)
return kPGPError_ConfigParseFailureBadOptions;
if (!opt->name) /* In the config file, not found isn't fatal */
return linenum ? 0 : kPGPError_ConfigParseFailureBadOptions;
if ((opt->flags & OPT_TYPEMASK) == OPT_IGNORE)
return 0;
msgarg1.type = PGP_UI_ARG_UNSIGNED;
msgarg1.val.u = linenum;
msgarg2.type = PGP_UI_ARG_BUFFER;
msgarg2.val.buf.buf = (PGPByte *)key;
msgarg2.val.buf.len = keylen;
line += keylen;
line += skipWhite(line);
if (*line != '=') {
/* On the command line, "armor" means "armor=on" */
if (!linenum && *line == '\0' &&
(opt->flags & OPT_TYPEMASK) == OPT_BINARY)
{
if (opt->flags & OPT_FUNC)
(void)configCallFunci(env, opt->var, opt->max,
pri);
else
(void)pgpenvSetInt(env,
(PgpEnvInts)opt->var,
opt->max, pri);
return 0;
}
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_MISSING_EQUAL,
2, &msgarg1, &msgarg2);
return kPGPError_ConfigParseFailure;
}
line++; /* skip '=' */
/* Skip assignment and following whitespace */
line += skipWhite(line);
switch (opt->flags & OPT_TYPEMASK) {
case OPT_BINARY:
i = getWord(line, 0);
if ((i == 3 && xmemicmp("off", line, 3) == 0) ||
(i == 1 && line[0] == '0'))
{
if (opt->flags & OPT_FUNC)
(void)configCallFunci(env, opt->var, opt->min,
pri);
else
(void)pgpenvSetInt(env,
(PgpEnvInts)opt->var,
opt->min, pri);
break;
} else if ((i == 2 && xmemicmp("on", line, 2) == 0) ||
(i == 1 && line[0] == '1'))
{
if (opt->flags & OPT_FUNC)
(void)configCallFunci(env, opt->var, opt->max,
pri);
else
(void)pgpenvSetInt(env,
(PgpEnvInts)opt->var,
opt->max, pri);
break;
} else if (i == 0) {
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_MISSING_BOOLEAN, 2,
&msgarg1, &msgarg2);
break;
}
/* Fall through: default case */
msgarg3.type = PGP_UI_ARG_BUFFER;
msgarg3.val.buf.buf = (PGPByte *)line;
msgarg3.val.buf.len = i;
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_UNKNOWN_BOOLEAN, 3, &msgarg1,
&msgarg2, &msgarg3);
return kPGPError_ConfigParseFailure;
case OPT_INT:
val = strtol (line, &p, 0);
i = getWord (line, 0);
msgarg3.type = PGP_UI_ARG_BUFFER;
msgarg3.val.buf.buf = (PGPByte *)line;
msgarg3.val.buf.len = i;
if (!isspace((int) (*p)) && *p != '\0') {
if (i) {
ui->message
(arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_UNKNOWN_INTEGER, 3,
&msgarg1, &msgarg2, &msgarg3);
} else {
ui->message
(arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_MISSING_INTEGER, 2,
&msgarg1, &msgarg2);
}
return kPGPError_ConfigParseFailure;
}
msgarg4.type = PGP_UI_ARG_INT;
msgarg4.val.i = val;
if (val > (long)opt->max) {
msgarg5.type = PGP_UI_ARG_INT;
msgarg5.val.i = opt->max;
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_INT_TOO_HIGH, 4,
&msgarg1, &msgarg2, &msgarg4, &msgarg5);
val = (long)opt->max;
} else if (val < (long)opt->min) {
msgarg5.type = PGP_UI_ARG_INT;
msgarg5.val.u = opt->min;
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_INT_TOO_LOW, 4,
&msgarg1, &msgarg2, &msgarg4, &msgarg5);
val = (long)opt->min;
}
if ((opt->flags & OPT_FUNC) == 0) {
(void)pgpenvSetInt(env, (PgpEnvInts)opt->var,
(int)val, pri);
} else if (configCallFunci(env, opt->var, (int)val, pri) < 0) {
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_INVALID_INTEGER, 3,
&msgarg1, &msgarg2, &msgarg3);
return kPGPError_ConfigParseFailure;
}
break;
case OPT_STRING:
if (*line == '"') {
i = getQString(ui, arg, linenum, ++line);
if (i == (unsigned)-1)
return kPGPError_ConfigParseFailure;
} else {
i = getString(line);
}
msgarg3.type = PGP_UI_ARG_BUFFER;
msgarg3.val.buf.buf = (PGPByte *)line;
msgarg3.val.buf.len = i;
if (i > (unsigned)opt->max-1) {
msgarg4.type = PGP_UI_ARG_INT;
msgarg4.val.i = opt->max-1;
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_STRING_TOO_LONG, 3,
&msgarg1, &msgarg2, &msgarg4);
return kPGPError_ConfigParseFailure;
/* Call function to set: never any need to copy */
}
{
char c;
int status;
c = line[i];
line[i] = '\0'; /* Null-terminate the string */
if (opt->flags & OPT_FUNC)
status = configCallFuncs (env, opt->var,
line, i, pri);
else
status = pgpenvSetString
(env, (PgpEnvStrings) opt->var,
line, pri);
line[i] = c; /* Restore the string */
if (status >= 0)
break; /* Success */
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_INVALID_STRING, 3,
&msgarg1, &msgarg2, &msgarg3);
return kPGPError_ConfigParseFailure;
}
break;
}
return 0;
}
/*
* "option" may be modified temporarily, but it is restored prior to
* returning.
*/
int
pgpConfigLineProcess(PGPUICb const *ui, void *arg,
PGPEnv *env, char *option, int pri)
{
if (!ui || !ui->message)
return kPGPError_BadParams;
return configLineParse (ui, arg, env, 0, option, pri);
}
#define CPM_EOF 26 /* ^Z - accept this as a file terminator. */
/*
* Read a line from file f, into buf, up to the given length. Anything after
* that is ignored. Strips trailing spaces and line terminator, can read
* LF, CRLF and CR terminated lines. Also accepts CPM_EOF (^Z) as a file
* terminator.
*/
static char *
configLineGet(FILE *f, char *buf, unsigned len)
{
int c;
char *p = buf;
/* Skip leading whitespace */
do {
c = getc(f);
} while (c == ' ' || c == '\t');
while (len > 0 && c != '\n' && c != '\r' && c != EOF && c != CPM_EOF)
{
*p++ = c;
--len;
c = getc(f);
}
/* Return NULL on EOF */
if (p == buf && (c == EOF || c == CPM_EOF)) {
*buf = '\0';
return (char *)0;
}
/*
* Skip to end of line, setting len to non-zero if anything trailing is
* not a space (meaning that any trailing whitespace in the buffer is
* not trailing whitespace on the line and should not be stripped).
*/
len = 0;
while (c != '\n' && c != '\r' && c != EOF && c != CPM_EOF) {
len |= c ^ ' '; /* Avoid branches for fast processors */
c = getc(f);
}
/* Put back CPM_EOF, and character after \r if it's not \n */
if (c == CPM_EOF || (c == '\r' && (c = getc(f)) != '\n'))
ungetc(c, f);
/* If line wasn't too long, skip trailing WS and null-terminate */
if (!len) { /* Skip trailing whitespace, as described above */
while (p >= buf && p[-1] == ' ')
--p;
*p = '\0';
}
return buf;
}
int
pgpConfigFileProcess(PGPUICb const *ui, void *arg,
PGPEnv *env, char const *filename, int pri)
{
FILE *f;
unsigned linenum = 0;
unsigned errors = 0;
int i;
char buf[130]; /* Maximum allowable line size */
PgpUICbArg msgarg1, msgarg2, msgarg3;
if (!ui || !ui->message)
return kPGPError_BadParams;
f = fopen (filename, "r");
if (f == NULL) {
msgarg1.type = PGP_UI_ARG_STRING;
msgarg1.val.s = filename;
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_NO_FILE, 1, &msgarg1);
return 0; /* treat like empty config file */
}
buf[sizeof(buf)-1] = '\0';
while (errors < MAX_ERRORS && configLineGet(f, buf, sizeof(buf))) {
++linenum;
msgarg1.type = PGP_UI_ARG_UNSIGNED;
msgarg1.val.u = linenum;
msgarg2.type = PGP_UI_ARG_STRING;
msgarg2.val.s = buf;
/* Line too long? */
if (buf[sizeof(buf)-1] != '\0') {
buf[sizeof(buf)-1] = '\0';
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_LINE_TOO_LONG, 2,
&msgarg1, &msgarg2);
errors++;
continue;
}
/* Illegal character? */
for (i = 0; buf[i] != '\0'; i++) {
if (!isprint((int) buf[i]) && buf[i] != '\t')
break;
}
if (buf[i] != '\0') {
msgarg3.type = PGP_UI_ARG_INT;
msgarg3.val.i = i;
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_BAD_CHAR, 3,
&msgarg1, &msgarg2, &msgarg3);
errors++;
continue;
}
if (configLineParse (ui, arg, env, linenum, buf, pri)
< 0)
errors++;
}
fclose (f);
if (errors > 0) {
msgarg1.type = PGP_UI_ARG_INT;
msgarg1.val.i = errors;
if (errors >= MAX_ERRORS)
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_MAX_ERRORS, 1, &msgarg1);
else
ui->message (arg, kPGPError_ConfigParseFailure,
PGPMSG_CONFIG_NUM_ERRORS, 1, &msgarg1);
return kPGPError_ConfigParseFailure;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -