1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-07-31 00:03:08 +03:00

checksrc: update, check all sources, fix fallouts

update from curl:
cff75acfec/scripts/checksrc.pl

Closes #1457
This commit is contained in:
Viktor Szakats
2024-09-27 01:05:47 +02:00
parent d9c2e550ca
commit 1117b677a0
21 changed files with 667 additions and 513 deletions

View File

@ -1,3 +0,0 @@
disable FOPENMODE
disable SNPRINTF
disable TYPEDEFSTRUCT

View File

@ -81,6 +81,4 @@ gen-coverage:
coverage: init-coverage build-coverage gen-coverage coverage: init-coverage build-coverage gen-coverage
checksrc: checksrc:
perl ci/checksrc.pl -i4 -m79 \ ci/checksrc.sh
-Wsrc/libssh2_config.h \
src/*.[ch] include/*.h example/*.c tests/*.[ch]

View File

@ -44,7 +44,6 @@ path = [
"vms/man2help.c", "vms/man2help.c",
"vms/readme.vms", "vms/readme.vms",
# dotfiles # dotfiles
".checksrc",
".editorconfig", ".editorconfig",
".gitignore", ".gitignore",
"docs/.gitignore", "docs/.gitignore",

View File

@ -50,54 +50,58 @@ my @ignore_line;
my %warnings_extended = ( my %warnings_extended = (
'COPYRIGHTYEAR' => 'copyright year incorrect', 'COPYRIGHTYEAR' => 'copyright year incorrect',
'STRERROR', => 'strerror() detected', 'STRERROR', => 'strerror() detected',
'STRNCPY', => 'strncpy() detected',
'STDERR', => 'stderr detected', 'STDERR', => 'stderr detected',
); );
my %warnings = ( my %warnings = (
'ASSIGNWITHINCONDITION' => 'assignment within conditional expression', 'ASSIGNWITHINCONDITION' => 'assignment within conditional expression',
'ASTERISKNOSPACE' => 'pointer declared without space before asterisk', 'ASTERISKNOSPACE' => 'pointer declared without space before asterisk',
'ASTERISKSPACE' => 'pointer declared with space after asterisk', 'ASTERISKSPACE' => 'pointer declared with space after asterisk',
'BADCOMMAND' => 'bad !checksrc! instruction', 'BADCOMMAND' => 'bad !checksrc! instruction',
'BANNEDFUNC' => 'a banned function was used', 'BANNEDFUNC' => 'a banned function was used',
'BANNEDPREPROC' => 'a banned symbol was used on a preprocessor line', 'BANNEDPREPROC' => 'a banned symbol was used on a preprocessor line',
'BRACEELSE' => '} else on the same line', 'BRACEELSE' => '} else on the same line',
'BRACEPOS' => 'wrong position for an open brace', 'BRACEPOS' => 'wrong position for an open brace',
'BRACEWHILE' => 'A single space between open brace and while', 'BRACEWHILE' => 'A single space between open brace and while',
'COMMANOSPACE' => 'comma without following space', 'COMMANOSPACE' => 'comma without following space',
'COMMENTNOSPACEEND' => 'no space before */', 'COMMENTNOSPACEEND' => 'no space before */',
'COMMENTNOSPACESTART' => 'no space following /*', 'COMMENTNOSPACESTART' => 'no space following /*',
'COPYRIGHT' => 'file missing a copyright statement', 'COPYRIGHT' => 'file missing a copyright statement',
'CPPCOMMENTS' => '// comment detected', 'CPPCOMMENTS' => '// comment detected',
'DOBRACE' => 'A single space between do and open brace', 'DOBRACE' => 'A single space between do and open brace',
'EMPTYLINEBRACE' => 'Empty line before the open brace', 'EMPTYLINEBRACE' => 'Empty line before the open brace',
'EQUALSNOSPACE' => 'equals sign without following space', 'EQUALSNOSPACE' => 'equals sign without following space',
'EQUALSNULL' => 'if/while comparison with == NULL', 'EQUALSNULL' => 'if/while comparison with == NULL',
'EXCLAMATIONSPACE' => 'Whitespace after exclamation mark in expression', 'EXCLAMATIONSPACE' => 'Whitespace after exclamation mark in expression',
'FOPENMODE' => 'fopen needs a macro for the mode string', 'FOPENMODE' => 'fopen needs a macro for the mode string',
'INCLUDEDUP', => 'same file is included again', 'INCLUDEDUP', => 'same file is included again',
'INDENTATION' => 'wrong start column for code', 'INDENTATION' => 'wrong start column for code',
'LONGLINE' => "Line longer than $max_column", 'LONGLINE' => "Line longer than $max_column",
'SPACEBEFORELABEL' => 'labels not at the start of the line', 'SPACEBEFORELABEL' => 'labels not at the start of the line',
'MULTISPACE' => 'multiple spaces used when not suitable', 'MULTISPACE' => 'multiple spaces used when not suitable',
'NOSPACEEQUALS' => 'equals sign without preceding space', 'NOSPACEC' => 'missing space around ternary colon operator',
'NOTEQUALSZERO', => 'if/while comparison with != 0', 'NOSPACEEQUALS' => 'equals sign without preceding space',
'ONELINECONDITION' => 'conditional block on the same line as the if()', 'NOSPACEQ' => 'missing space around ternary question mark operator',
'OPENCOMMENT' => 'file ended with a /* comment still "open"', 'NOSPACETHAN' => 'missing space around less or greater than',
'PARENBRACE' => '){ without sufficient space', 'NOTEQUALSZERO', => 'if/while comparison with != 0',
'RETURNNOSPACE' => 'return without space', 'ONELINECONDITION' => 'conditional block on the same line as the if()',
'SEMINOSPACE' => 'semicolon without following space', 'OPENCOMMENT' => 'file ended with a /* comment still "open"',
'SIZEOFNOPAREN' => 'use of sizeof without parentheses', 'PARENBRACE' => '){ without sufficient space',
'SNPRINTF' => 'use of snprintf', 'RETURNNOSPACE' => 'return without space',
'SPACEAFTERPAREN' => 'space after open parenthesis', 'SEMINOSPACE' => 'semicolon without following space',
'SPACEBEFORECLOSE' => 'space before a close parenthesis', 'SIZEOFNOPAREN' => 'use of sizeof without parentheses',
'SPACEBEFORECOMMA' => 'space before a comma', 'SNPRINTF' => 'use of snprintf',
'SPACEBEFOREPAREN' => 'space before an open parenthesis', 'SPACEAFTERPAREN' => 'space after open parenthesis',
'SPACESEMICOLON' => 'space before semicolon', 'SPACEBEFORECLOSE' => 'space before a close parenthesis',
'SPACESWITCHCOLON' => 'space before colon of switch label', 'SPACEBEFORECOMMA' => 'space before a comma',
'TABS' => 'TAB characters not allowed', 'SPACEBEFOREPAREN' => 'space before an open parenthesis',
'TRAILINGSPACE' => 'Trailing whitespace on the line', 'SPACESEMICOLON' => 'space before semicolon',
'TYPEDEFSTRUCT' => 'typedefed struct', 'SPACESWITCHCOLON' => 'space before colon of switch label',
'UNUSEDIGNORE' => 'a warning ignore was not used', 'TABS' => 'TAB characters not allowed',
'TRAILINGSPACE' => 'Trailing whitespace on the line',
'TYPEDEFSTRUCT' => 'typedefed struct',
'UNUSEDIGNORE' => 'a warning ignore was not used',
); );
sub readskiplist { sub readskiplist {
@ -115,11 +119,23 @@ sub readskiplist {
# and since that's already handled via !checksrc! commands there is probably # and since that's already handled via !checksrc! commands there is probably
# little use to add it. # little use to add it.
sub readlocalfile { sub readlocalfile {
my ($file) = @_;
my $i = 0; my $i = 0;
my $rcfile;
open(my $rcfile, "<", "$dir/.checksrc") or return; if(($dir eq ".") && $file =~ /\//) {
my $ldir;
if($file =~ /(.*)\//) {
$ldir = $1;
open($rcfile, "<", "$dir/$ldir/.checksrc") or return;
}
}
else {
open($rcfile, "<", "$dir/.checksrc") or return;
}
while(<$rcfile>) { while(<$rcfile>) {
$windows_os ? $_ =~ s/\r?\n$// : chomp;
$i++; $i++;
# Lines starting with '#' are considered comments # Lines starting with '#' are considered comments
@ -263,7 +279,7 @@ if(!$file) {
} }
readskiplist(); readskiplist();
readlocalfile(); readlocalfile($file);
do { do {
if("$wlist" !~ / $file /) { if("$wlist" !~ / $file /) {
@ -403,6 +419,13 @@ sub scanfile {
checksrc($cmd, $line, $file, $l) checksrc($cmd, $line, $file, $l)
} }
if($l =~ /^#line (\d+) \"([^\"]*)\"/) {
# a #line instruction
$file = $2;
$line = $1;
next;
}
# check for a copyright statement and save the years # check for a copyright statement and save the years
if($l =~ /\* +copyright .* (\d\d\d\d|)/i) { if($l =~ /\* +copyright .* (\d\d\d\d|)/i) {
my $count = 0; my $count = 0;
@ -603,6 +626,62 @@ sub scanfile {
"space after open parenthesis"); "space after open parenthesis");
} }
# check spaces before colon
if($nostr =~ /^(.*[^']\?[^'].*)(\w|\)|\]|')\:/i) {
my $m = $1;
my $e = $nostr;
$e =~ s/'(.)':'(.)'/$1:$2/g; # eliminate chars quotes that surround colon
$e =~ s/':'//g; # ignore these
if($e =~ /^(.*[^']\?[^'].*)(\w|\)|\]|')\:/i) {
checkwarn("NOSPACEC",
$line, length($m)+1, $file, $l,
"missing space before colon");
}
}
# check spaces after colon
if($nostr =~ /^(.*[^'"]\?[^'"].*)\:(\w|\)|\]|')/i) {
my $m = $1;
my $e = $nostr;
$e =~ s/'(.)':'(.)'/$1:$2/g; # eliminate chars quotes that surround colon
$e =~ s/':'//g; # ignore these
if($e =~ /^(.*[^'"]\?[^'"].*)\:(\w|\)|\]|')/i) {
checkwarn("NOSPACEC",
$line, length($m)+1, $file, $l,
"missing space after colon");
}
}
# check spaces before question mark
if($nostr =~ /^(.*)(\w|\)|\]|')\?/i) {
my $m = $1;
my $e = $nostr;
$e =~ s/'?'//g; # ignore these
if($e =~ /^(.*)(\w|\)|\]|')\?/i) {
checkwarn("NOSPACEQ",
$line, length($m)+1, $file, $l,
"missing space before question mark");
}
}
# check spaces after question mark
if($nostr =~ /^(.*)\?\w/i) {
checkwarn("NOSPACEQ",
$line, length($1)+1, $file, $l,
"missing space after question mark");
}
# check spaces before less or greater than
if($nostr =~ /^(.*)(\w|\)|\])[<>]/) {
checkwarn("NOSPACETHAN",
$line, length($1)+1, $file, $l,
"missing space before less or greater than");
}
# check spaces after less or greater than
if($nostr =~ /^(.*)[^-][<>](\w|\(|\[)/) {
checkwarn("NOSPACETHAN",
$line, length($1)+1, $file, $l,
"missing space after less or greater than");
}
# check spaces before close parentheses, unless it was a space or a # check spaces before close parentheses, unless it was a space or a
# close parenthesis! # close parenthesis!
if($l =~ /(.*[^\) ]) \)/) { if($l =~ /(.*[^\) ]) \)/) {
@ -712,7 +791,8 @@ sub scanfile {
strtok| strtok|
v?sprintf| v?sprintf|
(str|_mbs|_tcs|_wcs)n?cat| (str|_mbs|_tcs|_wcs)n?cat|
LoadLibrary(Ex)?(A|W)?) LoadLibrary(Ex)?(A|W)?|
_?w?access)
\s*\( \s*\(
/x) { /x) {
checkwarn("BANNEDFUNC", checkwarn("BANNEDFUNC",
@ -731,6 +811,18 @@ sub scanfile {
} }
} }
} }
if($warnings{"STRNCPY"}) {
# scan for use of banned strncpy. This is not a BANNEDFUNC to
# allow for individual enable/disable of this warning.
if($l =~ /^(.*\W)(strncpy)\s*\(/x) {
if($1 !~ /^ *\#/) {
# skip preprocessor lines
checkwarn("STRNCPY",
$line, length($1), $file, $ol,
"use of $2 is banned");
}
}
}
if($warnings{"STDERR"}) { if($warnings{"STDERR"}) {
# scan for use of banned stderr. This is not a BANNEDFUNC to # scan for use of banned stderr. This is not a BANNEDFUNC to
# allow for individual enable/disable of this warning. # allow for individual enable/disable of this warning.
@ -915,7 +1007,7 @@ sub scanfile {
checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1); checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
} }
# COPYRIGHTYEAR is a extended warning so we must first see if it has been # COPYRIGHTYEAR is an extended warning so we must first see if it has been
# enabled in .checksrc # enabled in .checksrc
if(defined($warnings{"COPYRIGHTYEAR"})) { if(defined($warnings{"COPYRIGHTYEAR"})) {
# The check for updated copyrightyear is overly complicated in order to # The check for updated copyrightyear is overly complicated in order to

View File

@ -6,6 +6,5 @@ set -e
cd "$(dirname "$0")/.." cd "$(dirname "$0")/.."
perl ./ci/checksrc.pl -i4 -m79 \ git ls-files "*.[ch]" | xargs -n1 \
-Wsrc/libssh2_config.h \ ./ci/checksrc.pl -i4 -m79 -AFOPENMODE -ASNPRINTF -ATYPEDEFSTRUCT
src/*.[ch] include/*.h example/*.c tests/*.[ch]

View File

@ -88,7 +88,7 @@ terminator_size(unsigned short ccsid)
/* Return the null-terminator size for the given CCSID. */ /* Return the null-terminator size for the given CCSID. */
/* Fast check usual CCSIDs. */ /* Fast check usual CCSIDs. */
switch (ccsid) { switch(ccsid) {
case CCSID_UTF8: case CCSID_UTF8:
case 0: /* Job CCSID is SBCS EBCDIC. */ case 0: /* Job CCSID is SBCS EBCDIC. */
return 1; return 1;
@ -98,19 +98,19 @@ terminator_size(unsigned short ccsid)
/* Convert an UTF-8 NUL to the target CCSID: use the converted size as /* Convert an UTF-8 NUL to the target CCSID: use the converted size as
result. */ result. */
memset((void *) &outcode, 0, sizeof outcode); memset((void *) &outcode, 0, sizeof(outcode));
outcode.CCSID = ccsid; outcode.CCSID = ccsid;
cd = QtqIconvOpen(&outcode, (QtqCode_T *) &utf8code); cd = QtqIconvOpen(&outcode, (QtqCode_T *) &utf8code);
if (cd.return_value == -1) if(cd.return_value == -1)
return -1; return -1;
inp = ""; inp = "";
ilen = 1; ilen = 1;
outp = buf; outp = buf;
olen = sizeof buf; olen = sizeof(buf);
iconv(cd, &inp, &ilen, &outp, &olen); iconv(cd, &inp, &ilen, &outp, &olen);
iconv_close(cd); iconv_close(cd);
olen = sizeof buf - olen; olen = sizeof(buf - olen);
return olen? olen: -1; return olen ? olen : -1;
} }
static char * static char *
@ -132,31 +132,31 @@ convert_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache,
QtqCode_T outcode; QtqCode_T outcode;
iconv_t cd; iconv_t cd;
if (!instring) { if(!instring) {
if (outlen) if(outlen)
*outlen = 0; *outlen = 0;
return NULL; return NULL;
} }
if (outlen) if(outlen)
*outlen = -1; *outlen = -1;
if (!cache) if(!cache)
return NULL; return NULL;
/* Get terminator size. */ /* Get terminator size. */
termsize = terminator_size(outccsid); termsize = terminator_size(outccsid);
if (termsize < 0) if(termsize < 0)
return NULL; return NULL;
/* Prepare conversion parameters. */ /* Prepare conversion parameters. */
memset((void *) &incode, 0, sizeof incode); memset((void *) &incode, 0, sizeof(incode));
memset((void *) &outcode, 0, sizeof outcode); memset((void *) &outcode, 0, sizeof(outcode));
incode.CCSID = inccsid; incode.CCSID = inccsid;
outcode.CCSID = outccsid; outcode.CCSID = outccsid;
curlen = OFFSET_OF(libssh2_string_cache, string); curlen = OFFSET_OF(libssh2_string_cache, string);
inp = (char *) instring; inp = (char *) instring;
ilen = inlen; ilen = inlen;
buflen = inlen + curlen; buflen = inlen + curlen;
if (inlen < 0) { if(inlen < 0) {
incode.length_option = 1; incode.length_option = 1;
buflen = STRING_GRANULE; buflen = STRING_GRANULE;
ilen = 0; ilen = 0;
@ -164,34 +164,34 @@ convert_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache,
/* Allocate output string buffer and open conversion descriptor. */ /* Allocate output string buffer and open conversion descriptor. */
dst = ALLOC(session, buflen + termsize); dst = ALLOC(session, buflen + termsize);
if (!dst) if(!dst)
return NULL; return NULL;
cd = QtqIconvOpen(&outcode, &incode); cd = QtqIconvOpen(&outcode, &incode);
if (cd.return_value == -1) { if(cd.return_value == -1) {
FREE(session, dst); FREE(session, dst);
return NULL; return NULL;
} }
/* Convert string. */ /* Convert string. */
for (;;) { for(;;) {
outp = dst + curlen; outp = dst + curlen;
olen = buflen - curlen; olen = buflen - curlen;
i = iconv(cd, &inp, &ilen, &outp, &olen); i = iconv(cd, &inp, &ilen, &outp, &olen);
if (inlen < 0 && olen == buflen - curlen) { if(inlen < 0 && olen == buflen - curlen) {
/* Special case: converted 0-length (sub)strings do not store the /* Special case: converted 0-length (sub)strings do not store the
terminator. */ terminator. */
if (termsize) { if(termsize) {
memset(outp, 0, termsize); memset(outp, 0, termsize);
olen -= termsize; olen -= termsize;
} }
} }
curlen = buflen - olen; curlen = buflen - olen;
if (i >= 0 || errno != E2BIG) if(i >= 0 || errno != E2BIG)
break; break;
/* Must expand buffer. */ /* Must expand buffer. */
buflen += STRING_GRANULE; buflen += STRING_GRANULE;
outp = REALLOC(session, dst, buflen + termsize); outp = REALLOC(session, dst, buflen + termsize);
if (!outp) if(!outp)
break; break;
dst = outp; dst = outp;
} }
@ -199,19 +199,19 @@ convert_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache,
iconv_close(cd); iconv_close(cd);
/* Check for error. */ /* Check for error. */
if (i < 0 || !outp) { if(i < 0 || !outp) {
FREE(session, dst); FREE(session, dst);
return NULL; return NULL;
} }
/* Process terminator. */ /* Process terminator. */
if (inlen < 0) if(inlen < 0)
curlen -= termsize; curlen -= termsize;
else if (termsize) else if(termsize)
memset(dst + curlen, 0, termsize); memset(dst + curlen, 0, termsize);
/* Shorten buffer if possible. */ /* Shorten buffer if possible. */
if (curlen < buflen) if(curlen < buflen)
dst = REALLOC(session, dst, curlen + termsize); dst = REALLOC(session, dst, curlen + termsize);
/* Link to cache. */ /* Link to cache. */
@ -220,7 +220,7 @@ convert_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache,
*cache = outstring; *cache = outstring;
/* Return length if required. */ /* Return length if required. */
if (outlen) if(outlen)
*outlen = curlen - OFFSET_OF(libssh2_string_cache, string); *outlen = curlen - OFFSET_OF(libssh2_string_cache, string);
return outstring->string; return outstring->string;
@ -250,8 +250,8 @@ libssh2_release_string_cache(LIBSSH2_SESSION *session,
{ {
libssh2_string_cache *p; libssh2_string_cache *p;
if (cache) if(cache)
while ((p = *cache)) { while((p = *cache)) {
*cache = p->next; *cache = p->next;
FREE(session, (char *) p); FREE(session, (char *) p);
} }

View File

@ -42,8 +42,8 @@
#define LIBSSH2_STDIO_H #define LIBSSH2_STDIO_H
/* /*
* <stdio.h> wrapper. * <stdio.h> wrapper.
* Its goal is to redefine snprintf/vsnprintf which are not supported by QADRT. * Its goal is to redefine snprintf/vsnprintf which are not supported by QADRT.
*/ */
#include <qadrt.h> #include <qadrt.h>

View File

@ -66,7 +66,7 @@
#endif #endif
extern int _libssh2_os400_connect(int sd, extern int _libssh2_os400_connect(int sd,
struct sockaddr * destaddr, int addrlen); struct sockaddr *destaddr, int addrlen);
#ifndef LIBSSH2_DISABLE_QADRT_EXT #ifndef LIBSSH2_DISABLE_QADRT_EXT
#define connect(sd, addr, len) _libssh2_os400_connect((sd), (addr), (len)) #define connect(sd, addr, len) _libssh2_os400_connect((sd), (addr), (len))

View File

@ -102,11 +102,13 @@ libssh2_channel_subsystem(LIBSSH2_CHANNEL *channel, const char *subsystem);
LIBSSH2_API ssize_t LIBSSH2_API ssize_t
libssh2_channel_read(LIBSSH2_CHANNEL *channel, char *buf, size_t buflen); libssh2_channel_read(LIBSSH2_CHANNEL *channel, char *buf, size_t buflen);
LIBSSH2_API ssize_t LIBSSH2_API ssize_t
libssh2_channel_read_stderr(LIBSSH2_CHANNEL *channel, char *buf, size_t buflen); libssh2_channel_read_stderr(LIBSSH2_CHANNEL *channel,
char *buf, size_t buflen);
LIBSSH2_API unsigned long LIBSSH2_API unsigned long
libssh2_channel_window_read(LIBSSH2_CHANNEL *channel); libssh2_channel_window_read(LIBSSH2_CHANNEL *channel);
LIBSSH2_API ssize_t LIBSSH2_API ssize_t
libssh2_channel_write(LIBSSH2_CHANNEL *channel, const char *buf, size_t buflen); libssh2_channel_write(LIBSSH2_CHANNEL *channel,
const char *buf, size_t buflen);
LIBSSH2_API ssize_t LIBSSH2_API ssize_t
libssh2_channel_write_stderr(LIBSSH2_CHANNEL *channel, libssh2_channel_write_stderr(LIBSSH2_CHANNEL *channel,
const char *buf, size_t buflen); const char *buf, size_t buflen);

View File

@ -77,55 +77,54 @@
static int static int
convert_sockaddr(struct sockaddr_storage * dstaddr, convert_sockaddr(struct sockaddr_storage *dstaddr,
const struct sockaddr * srcaddr, int srclen) const struct sockaddr *srcaddr, int srclen)
{ {
const struct sockaddr_un * srcu; const struct sockaddr_un *srcu;
struct sockaddr_un * dstu; struct sockaddr_un *dstu;
unsigned int i; unsigned int i;
unsigned int dstsize; unsigned int dstsize;
/* Convert a socket address into job CCSID, if needed. */ /* Convert a socket address into job CCSID, if needed. */
if(!srcaddr || srclen < offsetof(struct sockaddr, sa_family) + if(!srcaddr || srclen < offsetof(struct sockaddr, sa_family) +
sizeof srcaddr->sa_family || srclen > sizeof *dstaddr) { sizeof(srcaddr->sa_family) || srclen > sizeof(*dstaddr)) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
memcpy((char *) dstaddr, (char *) srcaddr, srclen); memcpy((char *) dstaddr, (char *) srcaddr, srclen);
switch (srcaddr->sa_family) { switch(srcaddr->sa_family) {
case AF_UNIX: case AF_UNIX:
srcu = (const struct sockaddr_un *) srcaddr; srcu = (const struct sockaddr_un *) srcaddr;
dstu = (struct sockaddr_un *) dstaddr; dstu = (struct sockaddr_un *) dstaddr;
dstsize = sizeof *dstaddr - offsetof(struct sockaddr_un, sun_path); dstsize = sizeof(*dstaddr) - offsetof(struct sockaddr_un, sun_path);
srclen -= offsetof(struct sockaddr_un, sun_path); srclen -= offsetof(struct sockaddr_un, sun_path);
i = QadrtConvertA2E(dstu->sun_path, srcu->sun_path, dstsize - 1, srclen); i = QadrtConvertA2E(dstu->sun_path, srcu->sun_path,
dstu->sun_path[i] = '\0'; dstsize - 1, srclen);
i += offsetof(struct sockaddr_un, sun_path); dstu->sun_path[i] = '\0';
srclen = i; i += offsetof(struct sockaddr_un, sun_path);
srclen = i;
} }
return srclen; return srclen;
} }
int int
_libssh2_os400_connect(int sd, struct sockaddr * destaddr, int addrlen) _libssh2_os400_connect(int sd, struct sockaddr *destaddr, int addrlen)
{ {
int i; int i;
struct sockaddr_storage laddr; struct sockaddr_storage laddr;
i = convert_sockaddr(&laddr, destaddr, addrlen); i = convert_sockaddr(&laddr, destaddr, addrlen);
if(i < 0) if(i < 0)
return -1; return -1;
return connect(sd, (struct sockaddr *) &laddr, i); return connect(sd, (struct sockaddr *) &laddr, i);
} }
@ -136,30 +135,31 @@ _libssh2_os400_vsnprintf(char *dst, size_t len, const char *fmt, va_list args)
int i; int i;
char *buf; char *buf;
if (!dst || !len) { if(!dst || !len) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
if (l < len) if(l < len)
l = len; l = len;
buf = alloca(l); buf = alloca(l);
if (!buf) { if(!buf) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
/* !checksrc! disable BANNEDFUNC 1 */ /* FIXME */
i = vsprintf(buf, fmt, args); i = vsprintf(buf, fmt, args);
if (i < 0) if(i < 0)
return i; return i;
if (--len > i) if(--len > i)
len = i; len = i;
if (len) if(len)
memcpy(dst, buf, len); memcpy(dst, buf, len);
dst[len] = '\0'; dst[len] = '\0';
@ -188,11 +188,11 @@ _libssh2_os400_inflateInit_(z_streamp strm,
char *ebcversion; char *ebcversion;
int i; int i;
if (!version) if(!version)
return Z_VERSION_ERROR; return Z_VERSION_ERROR;
i = strlen(version); i = strlen(version);
ebcversion = alloca(i + 1); ebcversion = alloca(i + 1);
if (!ebcversion) if(!ebcversion)
return Z_VERSION_ERROR; return Z_VERSION_ERROR;
i = QadrtConvertA2E(ebcversion, version, i, i - 1); i = QadrtConvertA2E(ebcversion, version, i, i - 1);
ebcversion[i] = '\0'; ebcversion[i] = '\0';
@ -206,11 +206,11 @@ _libssh2_os400_deflateInit_(z_streamp strm, int level,
char *ebcversion; char *ebcversion;
int i; int i;
if (!version) if(!version)
return Z_VERSION_ERROR; return Z_VERSION_ERROR;
i = strlen(version); i = strlen(version);
ebcversion = alloca(i + 1); ebcversion = alloca(i + 1);
if (!ebcversion) if(!ebcversion)
return Z_VERSION_ERROR; return Z_VERSION_ERROR;
i = QadrtConvertA2E(ebcversion, version, i, i - 1); i = QadrtConvertA2E(ebcversion, version, i, i - 1);
ebcversion[i] = '\0'; ebcversion[i] = '\0';

View File

@ -116,7 +116,7 @@ bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt,
if(rounds < 1) if(rounds < 1)
return -1; return -1;
if(passlen == 0 || saltlen == 0 || keylen == 0 || if(passlen == 0 || saltlen == 0 || keylen == 0 ||
keylen > sizeof(out) * sizeof(out) || saltlen > 1<<20) keylen > sizeof(out) * sizeof(out) || saltlen > 1 << 20)
return -1; return -1;
countsalt = calloc(1, saltlen + 4); countsalt = calloc(1, saltlen + 4);
if(!countsalt) if(!countsalt)

View File

@ -2180,7 +2180,7 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
"channel_read() got %ld of data from %u/%u/%d%s", "channel_read() got %ld of data from %u/%u/%d%s",
(long)bytes_want, channel->local.id, (long)bytes_want, channel->local.id,
channel->remote.id, stream_id, channel->remote.id, stream_id,
unlink_packet?" [ul]":"")); unlink_packet ? " [ul]" : ""));
/* copy data from this struct to the target buffer */ /* copy data from this struct to the target buffer */
memcpy(&buf[bytes_read], memcpy(&buf[bytes_read],

View File

@ -4071,7 +4071,7 @@ _libssh2_ecdsa_create_key(LIBSSH2_SESSION *session,
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
if(ctx && if(ctx &&
EVP_PKEY_keygen_init(ctx) >0 && EVP_PKEY_keygen_init(ctx) > 0 &&
EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, curve_type) > 0) { EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, curve_type) > 0) {
ret = EVP_PKEY_keygen(ctx, &private_key); ret = EVP_PKEY_keygen(ctx, &private_key);
} }

View File

@ -367,7 +367,7 @@ _libssh2_random(unsigned char *buf, size_t len)
set_EC_length(errcode, sizeof(errcode)); set_EC_length(errcode, sizeof(errcode));
Qc3GenPRNs(buf, len, Qc3GenPRNs(buf, len,
Qc3PRN_TYPE_NORMAL, Qc3PRN_NO_PARITY, (char *) &errcode); Qc3PRN_TYPE_NORMAL, Qc3PRN_NO_PARITY, (char *) &errcode);
return errcode.Bytes_Available? -1: 0; return errcode.Bytes_Available ? -1 : 0;
} }
_libssh2_bn * _libssh2_bn *
@ -967,7 +967,7 @@ _libssh2_os400qc3_hash_init(Qc3_Format_ALGD0100_T *x, unsigned int algorithm)
set_EC_length(errcode, sizeof(errcode)); set_EC_length(errcode, sizeof(errcode));
Qc3CreateAlgorithmContext((char *) &algd, Qc3_Alg_Hash, Qc3CreateAlgorithmContext((char *) &algd, Qc3_Alg_Hash,
x->Alg_Context_Token, &errcode); x->Alg_Context_Token, &errcode);
return errcode.Bytes_Available? 0: 1; return errcode.Bytes_Available ? 0 : 1;
} }
int int
@ -981,7 +981,7 @@ _libssh2_os400qc3_hash_update(Qc3_Format_ALGD0100_T *ctx,
set_EC_length(errcode, sizeof(errcode)); set_EC_length(errcode, sizeof(errcode));
Qc3CalculateHash((char *) data, &len, Qc3_Data, (char *) ctx, Qc3CalculateHash((char *) data, &len, Qc3_Data, (char *) ctx,
Qc3_Alg_Token, anycsp, NULL, dummy, &errcode); Qc3_Alg_Token, anycsp, NULL, dummy, &errcode);
return errcode.Bytes_Available? 0: 1; return errcode.Bytes_Available ? 0 : 1;
} }
int int
@ -996,7 +996,7 @@ _libssh2_os400qc3_hash_final(Qc3_Format_ALGD0100_T *ctx, unsigned char *out)
anycsp, NULL, (char *) out, &errcode); anycsp, NULL, (char *) out, &errcode);
Qc3DestroyAlgorithmContext(ctx->Alg_Context_Token, (char *) &ecnull); Qc3DestroyAlgorithmContext(ctx->Alg_Context_Token, (char *) &ecnull);
memset(ctx->Alg_Context_Token, 0, sizeof(ctx->Alg_Context_Token)); memset(ctx->Alg_Context_Token, 0, sizeof(ctx->Alg_Context_Token));
return errcode.Bytes_Available? 0: 1; return errcode.Bytes_Available ? 0 : 1;
} }
int int
@ -1036,7 +1036,7 @@ libssh2_os400qc3_hmac_init(_libssh2_os400qc3_crypto_ctx *ctx,
Qc3CreateKeyContext((char *) key, &keylen, binstring, &algo, qc3clear, Qc3CreateKeyContext((char *) key, &keylen, binstring, &algo, qc3clear,
NULL, NULL, ctx->key.Key_Context_Token, NULL, NULL, ctx->key.Key_Context_Token,
(char *) &errcode); (char *) &errcode);
return errcode.Bytes_Available? 0: 1; return errcode.Bytes_Available ? 0 : 1;
} }
int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx) int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx)
@ -1091,7 +1091,7 @@ int _libssh2_hmac_update(libssh2_hmac_ctx *ctx,
Qc3CalculateHMAC((char *) data, &len, Qc3_Data, (char *) &ctx->hash, Qc3CalculateHMAC((char *) data, &len, Qc3_Data, (char *) &ctx->hash,
Qc3_Alg_Token, ctx->key.Key_Context_Token, Qc3_Key_Token, Qc3_Alg_Token, ctx->key.Key_Context_Token, Qc3_Key_Token,
anycsp, NULL, dummy, (char *) &errcode); anycsp, NULL, dummy, (char *) &errcode);
return errcode.Bytes_Available? 0: 1; return errcode.Bytes_Available ? 0 : 1;
} }
int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *out) int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *out)
@ -1104,7 +1104,7 @@ int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *out)
Qc3CalculateHMAC((char *) data, &zero, Qc3_Data, (char *) &ctx->hash, Qc3CalculateHMAC((char *) data, &zero, Qc3_Data, (char *) &ctx->hash,
Qc3_Alg_Token, ctx->key.Key_Context_Token, Qc3_Key_Token, Qc3_Alg_Token, ctx->key.Key_Context_Token, Qc3_Key_Token,
anycsp, NULL, (char *) out, (char *) &errcode); anycsp, NULL, (char *) out, (char *) &errcode);
return errcode.Bytes_Available? 0: 1; return errcode.Bytes_Available ? 0 : 1;
} }
void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx) void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx)
@ -1182,7 +1182,7 @@ _libssh2_cipher_crypt(_libssh2_cipher_ctx *ctx,
ctx->key.Key_Context_Token, Qc3_Key_Token, anycsp, NULL, ctx->key.Key_Context_Token, Qc3_Key_Token, anycsp, NULL,
(char *) block, &blksize, &outlen, (char *) &errcode); (char *) block, &blksize, &outlen, (char *) &errcode);
return errcode.Bytes_Available? -1: 0; return errcode.Bytes_Available ? -1 : 0;
} }
@ -1565,7 +1565,7 @@ parse_pkcs5_algorithm(LIBSSH2_SESSION *session, pkcs5params *pkcs5,
for(; *algotable; algotable++) for(; *algotable; algotable++)
if(!oidcmp(&oid, (*algotable)->oid)) if(!oidcmp(&oid, (*algotable)->oid))
return (*(*algotable)->parse)(session, pkcs5, *algotable, return (*(*algotable)->parse)(session, pkcs5, *algotable,
param.header? &param: NULL); param.header ? &param : NULL);
return -1; return -1;
} }
@ -2456,7 +2456,7 @@ _libssh2_rsa_sha2_verify(libssh2_rsa_ctx *rsa, size_t hash_len,
(char *) &algd, Qc3_Alg_Public_Key, (char *) &algd, Qc3_Alg_Public_Key,
(char *) &rsa->key, Qc3_Key_Token, anycsp, (char *) &rsa->key, Qc3_Key_Token, anycsp,
NULL, (char *) &errcode); NULL, (char *) &errcode);
return errcode.Bytes_Available? -1: 0; return errcode.Bytes_Available ? -1 : 0;
} }
int int

View File

@ -362,8 +362,8 @@ window_adjust:
1, NULL); 1, NULL);
/* store the state so that we continue with the correct /* store the state so that we continue with the correct
operation at next invoke */ operation at next invoke */
sftp->packet_state = (rc == LIBSSH2_ERROR_EAGAIN)? sftp->packet_state = (rc == LIBSSH2_ERROR_EAGAIN) ?
libssh2_NB_state_sent: libssh2_NB_state_sent :
libssh2_NB_state_idle; libssh2_NB_state_idle;
if(rc == LIBSSH2_ERROR_EAGAIN) if(rc == LIBSSH2_ERROR_EAGAIN)
@ -1166,7 +1166,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
/* packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) + /* packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) +
flags(4) */ flags(4) */
sftp->open_packet_len = (uint32_t)(filename_len + 13 + sftp->open_packet_len = (uint32_t)(filename_len + 13 +
(open_file? (4 + sftp_attrsize(attrs.flags)) : 0)); (open_file ? (4 + sftp_attrsize(attrs.flags)) : 0));
/* surprise! this starts out with nothing sent */ /* surprise! this starts out with nothing sent */
sftp->open_packet_sent = 0; sftp->open_packet_sent = 0;
@ -1183,7 +1183,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
LIBSSH2_SFTP_ATTR_PFILETYPE_DIR); LIBSSH2_SFTP_ATTR_PFILETYPE_DIR);
_libssh2_store_u32(&s, sftp->open_packet_len - 4); _libssh2_store_u32(&s, sftp->open_packet_len - 4);
*(s++) = open_file? SSH_FXP_OPEN : SSH_FXP_OPENDIR; *(s++) = open_file ? SSH_FXP_OPEN : SSH_FXP_OPENDIR;
sftp->open_request_id = sftp->request_id++; sftp->open_request_id = sftp->request_id++;
_libssh2_store_u32(&s, sftp->open_request_id); _libssh2_store_u32(&s, sftp->open_request_id);
_libssh2_store_str(&s, filename, filename_len); _libssh2_store_str(&s, filename, filename_len);
@ -1194,7 +1194,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
} }
_libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Sending %s open request", _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Sending %s open request",
open_file? "file" : "directory")); open_file ? "file" : "directory"));
sftp->open_state = libssh2_NB_state_created; sftp->open_state = libssh2_NB_state_created;
} }

View File

@ -1212,7 +1212,7 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session,
int firstlast = i == 0 ? FIRST_BLOCK : int firstlast = i == 0 ? FIRST_BLOCK :
(!CRYPT_FLAG_L(session, INTEGRATED_MAC) (!CRYPT_FLAG_L(session, INTEGRATED_MAC)
&& (i == packet_length - session->local.crypt->blocksize) && (i == packet_length - session->local.crypt->blocksize)
? LAST_BLOCK: MIDDLE_BLOCK); ? LAST_BLOCK : MIDDLE_BLOCK);
/* In the AAD case, the last block would be only 4 bytes /* In the AAD case, the last block would be only 4 bytes
because everything is offset by 4 since the initial because everything is offset by 4 since the initial
packet_length isn't encrypted. In this case, combine that last packet_length isn't encrypted. In this case, combine that last

View File

@ -1,3 +1,7 @@
/* Copyright (C) The libssh2 project and its contributors.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>

View File

@ -1,3 +1,7 @@
/* Copyright (C) The libssh2 project and its contributors.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -1,3 +1,7 @@
/* Copyright (C) The libssh2 project and its contributors.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <inttypes.h> #include <inttypes.h>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);

View File

@ -1,3 +1,8 @@
/* Copyright (C) The libssh2 project and its contributors.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef LIBSSH2_CONFIG_H #ifndef LIBSSH2_CONFIG_H
#ifdef __VMS #ifdef __VMS

View File

@ -1,3 +1,8 @@
/* Copyright (C) The libssh2 project and its contributors.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -10,12 +15,12 @@
#include <descrip.h> #include <descrip.h>
#include <rms.h> #include <rms.h>
typedef struct manl{ typedef struct manl {
struct manl *next; struct manl *next;
char *filename; char *filename;
}man, *manPtr; } man, *manPtr;
typedef struct pf_fabnam{ typedef struct pf_fabnam {
struct FAB dfab; struct FAB dfab;
struct RAB drab; struct RAB drab;
struct namldef dnam; struct namldef dnam;
@ -24,491 +29,536 @@ typedef struct pf_fabnam{
/*----------------------------------------------------------*/ /*----------------------------------------------------------*/
fpcopy( char *output, char *input, int len ) fpcopy(char *output, char *input, int len)
{ {
char *is, *os; char *is, *os;
int i; int i;
if ( len ){ if(len) {
for ( is = input, os = output, i = 0; i < len ; ++i, ++is, ++os){ for(is = input, os = output, i = 0; i < len; ++i, ++is, ++os) {
*os = *is; *os = *is;
}
*os = 0;
}
else {
output[0] = 0;
} }
*os = 0;
}else{
output[0] = 0;
}
} }
/*----------------------------------------------------------*/ /*----------------------------------------------------------*/
/* give part of ilename in partname. See code for proper /* give part of ilename in partname. See code for proper
value of i ( 0 = node, 1 = dev, 2 = dir,3 = name etc. value of i ( 0 = node, 1 = dev, 2 = dir, 3 = name etc.
*/ */
int fnamepart( char *inputfile, char *part, int whatpart ) int fnamepart(char *inputfile, char *part, int whatpart)
{ {
pfnPtr pf; pfnPtr pf;
int status; int status;
char ipart[6][256], *i, *p; char ipart[6][256], *i, *p;
pf = calloc( 1, sizeof( pfn ) ); pf = calloc(1, sizeof(pfn));
pf->dfab = cc$rms_fab; pf->dfab = cc$rms_fab;
pf->drab = cc$rms_rab; pf->drab = cc$rms_rab;
pf->dnam = cc$rms_naml; pf->dnam = cc$rms_naml;
pf->dfab.fab$l_naml = &pf->dnam; pf->dfab.fab$l_naml = &pf->dnam;
pf->dfab.fab$l_fna = (char *) -1; pf->dfab.fab$l_fna = (char *) -1;
pf->dfab.fab$l_dna = (char *) -1; pf->dfab.fab$l_dna = (char *) -1;
pf->dfab.fab$b_fns = 0; pf->dfab.fab$b_fns = 0;
pf->dfab.fab$w_ifi = 0; pf->dfab.fab$w_ifi = 0;
pf->dnam.naml$l_long_defname = NULL; //inputfile; pf->dnam.naml$l_long_defname = NULL; /* inputfile; */
pf->dnam.naml$l_long_defname_size = 0;//strlen( inputfile ); pf->dnam.naml$l_long_defname_size = 0; /* strlen(inputfile); */
pf->dnam.naml$l_long_filename = inputfile; pf->dnam.naml$l_long_filename = inputfile;
pf->dnam.naml$l_long_filename_size = strlen( inputfile ); pf->dnam.naml$l_long_filename_size = strlen(inputfile);
pf->dnam.naml$l_long_expand = pf->expanded_filename; pf->dnam.naml$l_long_expand = pf->expanded_filename;
pf->dnam.naml$l_long_expand_alloc = NAM$C_MAXRSS ; pf->dnam.naml$l_long_expand_alloc = NAM$C_MAXRSS;
pf->dnam.naml$b_nop |= NAML$M_SYNCHK | NAML$M_PWD; pf->dnam.naml$b_nop |= NAML$M_SYNCHK | NAML$M_PWD;
status = sys$parse( &pf->dfab, 0, 0 ); status = sys$parse(&pf->dfab, 0, 0);
if ( !(status&1) ){ if(!(status&1)) {
free( pf ); free(pf);
return( status ); return status;
} }
fpcopy ( ipart[0], pf->dnam.naml$l_long_node , pf->dnam.naml$l_long_node_size); fpcopy(ipart[0], pf->dnam.naml$l_long_node,
fpcopy ( ipart[1], pf->dnam.naml$l_long_dev , pf->dnam.naml$l_long_dev_size); pf->dnam.naml$l_long_node_size);
fpcopy ( ipart[2], pf->dnam.naml$l_long_dir , pf->dnam.naml$l_long_dir_size); fpcopy(ipart[1], pf->dnam.naml$l_long_dev,
fpcopy ( ipart[3], pf->dnam.naml$l_long_name , pf->dnam.naml$l_long_name_size); pf->dnam.naml$l_long_dev_size);
fpcopy ( ipart[4], pf->dnam.naml$l_long_type , pf->dnam.naml$l_long_type_size); fpcopy(ipart[2], pf->dnam.naml$l_long_dir,
fpcopy ( ipart[5], pf->dnam.naml$l_long_ver , pf->dnam.naml$l_long_ver_size); pf->dnam.naml$l_long_dir_size);
fpcopy(ipart[3], pf->dnam.naml$l_long_name,
pf->dnam.naml$l_long_name_size);
fpcopy(ipart[4], pf->dnam.naml$l_long_type,
pf->dnam.naml$l_long_type_size);
fpcopy(ipart[5], pf->dnam.naml$l_long_ver,
pf->dnam.naml$l_long_ver_size);
for( i = ipart[ whatpart ], p = part; *i; ++i, ++p){ for(i = ipart[whatpart], p = part; *i; ++i, ++p) {
if ( p == part ){ if(p == part) {
*p = toupper( *i ); *p = toupper(*i);
}else{ }
*p = tolower( *i ); else {
} *p = tolower(*i);
} }
*p = 0; }
*p = 0;
free( pf ); free(pf);
return(1); return 1;
} }
/*----------------------------------------------------------*/ /*----------------------------------------------------------*/
int find_file(char *filename,char *gevonden,int *findex) int find_file(char *filename, char *gevonden, int *findex)
{ {
int status; int status;
struct dsc$descriptor gevondend; struct dsc$descriptor gevondend;
struct dsc$descriptor filespec; struct dsc$descriptor filespec;
char gevonden_file[NAM$C_MAXRSS + 1]; char gevonden_file[NAM$C_MAXRSS + 1];
filespec.dsc$w_length = strlen(filename); filespec.dsc$w_length = strlen(filename);
filespec.dsc$b_dtype = DSC$K_DTYPE_T; filespec.dsc$b_dtype = DSC$K_DTYPE_T;
filespec.dsc$b_class = DSC$K_CLASS_S; filespec.dsc$b_class = DSC$K_CLASS_S;
filespec.dsc$a_pointer = filename; filespec.dsc$a_pointer = filename;
gevondend.dsc$w_length = NAM$C_MAXRSS; gevondend.dsc$w_length = NAM$C_MAXRSS;
gevondend.dsc$b_dtype = DSC$K_DTYPE_T; gevondend.dsc$b_dtype = DSC$K_DTYPE_T;
gevondend.dsc$b_class = DSC$K_CLASS_S; gevondend.dsc$b_class = DSC$K_CLASS_S;
gevondend.dsc$a_pointer = gevonden_file; gevondend.dsc$a_pointer = gevonden_file;
status=lib$find_file(&filespec,&gevondend,findex,0,0,0,0); status = lib$find_file(&filespec, &gevondend, findex, 0, 0, 0, 0);
if ( (status & 1) == 1 ){ if((status & 1) == 1) {
strcpy(gevonden,strtok(gevonden_file," ")); /* !checksrc! disable BANNEDFUNC 1 */ /* FIXME */
}else{ strcpy(gevonden, strtok(gevonden_file, " "));
gevonden[0] = 0;
}
return(status);
}
/*--------------------------------------------*/
manPtr addman( manPtr *manroot,char *filename )
{
manPtr m,f;
m = calloc( 1, sizeof( man ) );
if ( !m ) return( NULL );
m->filename = strdup( filename );
if ( *manroot == NULL ){
*manroot = m;
}else{
for( f = *manroot; f->next ; f = f->next );
f->next = m;
}
return(m);
}
/*--------------------------------------------*/
void freeman( manPtr *manroot )
{
manPtr m,n;
for( m = *manroot; m ; m = n ){
free( m->filename );
n = m->next;
free( m );
}
*manroot = NULL;
}
/*--------------------------------------------*/
int listofmans( char *filespec, manPtr *manroot )
{
manPtr r;
int status;
int ffindex=0;
char gevonden[NAM$C_MAXRSS + 1];
while(1){
status = find_file( filespec, gevonden, &ffindex );
if ( (status&1) ){
r = addman( manroot, gevonden );
if ( r == NULL ) return(2);
}else{
if ( !( status&1 ) ) break;
} }
else {
gevonden[0] = 0;
}
return status;
} }
lib$find_file_end( &ffindex );
if ( status == RMS$_NMF) status = 1;
/*--------------------------------------------*/
return( status ); manPtr addman(manPtr *manroot, char *filename)
{
manPtr m, f;
m = calloc(1, sizeof(man));
if(!m)
return NULL;
m->filename = strdup(filename);
if(!*manroot) {
*manroot = m;
}
else {
for(f = *manroot; f->next; f = f->next)
;
f->next = m;
}
return m;
}
/*--------------------------------------------*/
void freeman(manPtr *manroot)
{
manPtr m, n;
for(m = *manroot; m; m = n) {
free(m->filename);
n = m->next;
free(m);
}
*manroot = NULL;
} }
/*--------------------------------------------*/ /*--------------------------------------------*/
int convertman ( char *filespec, FILE *hlp , int base_level, int add_parentheses ) int listofmans(char *filespec, manPtr *manroot)
{ {
FILE *man; manPtr r;
char *in, *uit; int status;
char *m,*h; int ffindex = 0;
size_t len, thislen, maxlen= 50000; char gevonden[NAM$C_MAXRSS + 1];
int bol,mode, return_status=1;
char subjectname[ NAM$C_MAXRSS + 1 ];
in = calloc( 1, maxlen + 1 ); for(;;) {
uit = calloc( 1, maxlen + 1 ); status = find_file(filespec, gevonden, &ffindex);
if ( in == NULL || uit == NULL ) return(2); if((status&1)) {
r = addman(manroot, gevonden);
if(!r)
return 2;
}
else {
if(!(status&1))
break;
}
}
man = fopen( filespec, "r" ); lib$find_file_end(&ffindex);
if ( man == NULL ) return(vaxc$errno); if(status == RMS$_NMF)
status = 1;
for( len = 0; !feof( man ) && len < maxlen ; len += thislen ){ return status;
thislen = fread( in + len, 1, maxlen - len, man );
} }
fclose(man); /*--------------------------------------------*/
m = in; int convertman(char *filespec, FILE *hlp, int base_level, int add_parentheses)
h = uit; {
FILE *man;
char *in, *uit;
char *m, *h;
size_t len, thislen, maxlen = 50000;
int bol, mode, return_status = 1;
char subjectname[NAM$C_MAXRSS + 1];
*(m + len ) = 0; in = calloc(1, maxlen + 1);
uit = calloc(1, maxlen + 1);
for ( mode = 0, bol = 1 ; *m; ++m ){ if(!in || !uit)
return 2;
switch ( mode ){ man = fopen(filespec, "r");
if(!man)
return vaxc$errno;
for(len = 0; !feof(man) && len < maxlen; len += thislen) {
thislen = fread(in + len, 1, maxlen - len, man);
}
fclose(man);
m = in;
h = uit;
*(m + len) = 0;
for(mode = 0, bol = 1; *m; ++m) {
switch(mode) {
case 0: case 0:
switch(*m){ switch(*m) {
case '.': case '.':
if ( bol ){ if(bol) {
mode = 1; mode = 1;
}else{ }
else {
*h = *m; *h = *m;
++h; ++h;
} }
break; break;
case '\\': case '\\':
if ( bol ){ if(bol) {
*h = ' ';++h; *h = ' '; ++h;
*h = ' ';++h; *h = ' '; ++h;
} }
mode = 2; mode = 2;
break; break;
default: default:
if ( bol ){ if(bol) {
*h = ' ';++h; *h = ' '; ++h;
*h = ' ';++h; *h = ' '; ++h;
} }
*h = *m; *h = *m;
++h; ++h;
break; break;
} }
break; break;
case 1: /* after . at bol */
switch(*m){ case 1: /* after . at bol */
switch(*m) {
case '\\': case '\\':
while( *m != '\n' && *m != '\r' && *m )++m; while(*m != '\n' && *m != '\r' && *m)
++m;
mode = 0; mode = 0;
break; break;
case 'B': case 'B':
++m; ++m;
*h = ' ';++h; *h = ' '; ++h;
mode = 0; mode = 0;
break; break;
case 'I': case 'I':
/* remove preceding eol */ /* remove preceding eol */
if ( *(m+1) != 'P' ){ if(*(m + 1) != 'P') {
--h;
while((*h == '\n' || *h == '\r') && h > uit)
--h; --h;
while ( (*h == '\n' || *h == '\r') && h > uit )--h; ++h;
++h; }
}
/* skip .Ix */ /* skip .Ix */
for(;*m != ' ' && *m != '\n' && *m != '\r'; ++m); for(; *m != ' ' && *m != '\n' && *m != '\r'; ++m)
;
/* copy line up to EOL */ /* copy line up to EOL */
for(;*m != '\n' && *m != '\r' && *m; ++m, ++h)*h = *m; for(; *m != '\n' && *m != '\r' && *m; ++m, ++h)
*h = *m;
/* if line ends in ., this is an EOL */ /* if line ends in ., this is an EOL */
if ( *(h-1) == '.'){ if(*(h-1) == '.') {
--h; --h;
--m; --m;
}else{ }
/* if line does not end in ., skip EOL in source */ else {
/* if line does not end in ., skip EOL in source */
if ( *(m+1) == '\n' || *(m+1) == '\r')++m; if(*(m + 1) == '\n' || *(m + 1) == '\r')
} ++m;
mode = 0; }
break; mode = 0;
break;
case 'S': case 'S':
if ( *(m+1) == 'H' ){ if(*(m + 1) == 'H') {
*h = '\n';++h; *h = '\n';++h;
if ( strncmp( m+3 ,"NAME",4) == 0 || if(strncmp(m + 3, "NAME", 4) == 0 ||
strncmp( m+3 ,"SYNOPSIS",8) == 0 || strncmp(m + 3, "SYNOPSIS", 8) == 0 ||
strncmp( m+3 ,"DESCRIPTION",11) == 0 ){ strncmp(m + 3, "DESCRIPTION", 11) == 0) {
while( *m != '\n' && *m != '\r')++m; while(*m != '\n' && *m != '\r')
++m;
mode = 0; mode = 0;
}else{ }
else {
++m; ++m;
/* write help level, and flag it */ /* write help level, and flag it */
*h = '0' + base_level + 1;++h; *h = '0' + base_level + 1; ++h;
return_status |= 2; return_status |= 2;
*h = ' ';++h; *h = ' '; ++h;
/* skip H (or whatever after S) and blank */ /* skip H (or whatever after S) and blank */
++m;++m; ++m; ++m;
for(;*m != '\n' && *m != '\r' && *m; ++m, ++h){ for(; *m != '\n' && *m != '\r' && *m; ++m, ++h) {
/* write help label in lowercase, skip quotes */ /* write help label in lowercase, skip quotes */
/* fill blanks with underscores */ /* fill blanks with underscores */
if ( *m != '\"' ){ if(*m != '\"') {
*h = tolower( *m ); *h = tolower(*m);
if (*h == ' ') *h = '_'; if(*h == ' ')
}else{ *h = '_';
--h; }
else {
--h;
} }
} }
/* Add a linefeed or two */ /* Add a linefeed or two */
*h = *m;++h; *h = *m; ++h;
*h = *m;++h; *h = *m; ++h;
mode = 0; mode = 0;
} }
} }
break; break;
case 'T': case 'T':
if ( *(m+1) == 'H' ){ if(*(m + 1) == 'H') {
*h = '0' + base_level; ++h; *h = '0' + base_level; ++h;
return_status |= 2; return_status |= 2;
*h = ' ';++h; *h = ' '; ++h;
for ( m = m + 3; *m != ' ' && *m ; ++m, ++h ){ for(m = m + 3; *m != ' ' && *m; ++m, ++h) {
*h = *m; *h = *m;
} }
if ( add_parentheses ){ if(add_parentheses) {
*h = '(';++h; *h = '('; ++h;
*h = ')';++h; *h = ')'; ++h;
} }
while( *m != '\n' && *m != '\r' && *m )++m; while(*m != '\n' && *m != '\r' && *m)
++m;
mode = 0; mode = 0;
} }
break; break;
default: default:
++m; ++m;
mode = 0; mode = 0;
break; break;
} }
break; break;
case 2: /* after \ skip two characters or print the backslash */ case 2: /* after \ skip two characters or print the backslash */
switch(*m){ switch(*m) {
case '\\': case '\\':
*h = *m; *h = *m;
++h; ++h;
mode = 0; mode = 0;
break;
default:
++m;
mode = 0;
break;
}
break;
} /*end switch mode */
bol = 0;
if ( *m == '\n' || *m == '\r') bol = 1;
}/* end for mode */
*h = 0;
if ( (return_status&2) ){
fprintf( hlp, "%s\n\n", uit);
}else{
fnamepart( filespec, subjectname,3);
if ( *subjectname ){
fprintf( hlp, "%d %s\n\n%s\n\n", base_level, subjectname, uit);
}else{
/* No filename (as is the case with a logical), use first word as subject name */
char *n,*s;
for(n = in; isspace( *n );++n);
for(s = subjectname; !(isspace( *n )); ++n,++s)*s = *n;
*s = 0;
fprintf( hlp, "%d %s\n\n%s\n\n", base_level, subjectname, uit);
}
}
/*
printf( "read %d from %s, written %d to helpfile, return_status = %d\n",
len, filespec, strlen(uit), return_status );
*/
free( m );
free( h );
return ( 1 );
}
/*--------------------------------------------*/
int convertmans( char *filespec, char *hlpfilename, int base_level, int append, int add_parentheses )
{
int status=1;
manPtr manroot=NULL, m;
FILE *hlp;
if ( append ){
hlp = fopen( hlpfilename,"a+");
}else{
hlp = fopen( hlpfilename,"w");
}
if ( hlp == NULL ) return( vaxc$errno );
status = listofmans( filespec, &manroot );
if ( !(status&1) ) return( status );
for ( m = manroot ; m ; m = m->next ){
status = convertman( m->filename, hlp , base_level, add_parentheses );
if ( !(status&1) ){
fprintf(stderr,"Convertman of %s went wrong\n", m->filename);
break;
}
}
freeman( &manroot );
return( status );
}
/*--------------------------------------------*/
void print_help()
{
fprintf( stderr, "Usage: [-a] [-b x] convertman <manfilespec> <helptextfile>\n" );
fprintf( stderr, " -a append <manfilespec> to <helptextfile>\n" );
fprintf( stderr, " -b <baselevel> if no headers found create one with level <baselevel>\n" );
fprintf( stderr, " and the filename as title.\n" );
fprintf( stderr, " -p add parentheses() to baselevel help items.\n" );
}
/*--------------------------------------------*/
main ( int argc, char **argv )
{
int status;
int i,j;
int append, base_level, basechange, add_parentheses;
char *manfile=NULL;
char *helpfile=NULL;
if ( argc < 3 ){
print_help();
return( 1 ) ;
}
append = 0;
base_level = 1;
basechange = 0;
add_parentheses = 0;
for ( i = 1; i < argc; ++i){
if ( argv[i][0] == '-' ){
for( j = 1; argv[i][j] ; ++j ){
switch( argv[i][j] ){
case 'a':
append = 1;
break; break;
case 'b': default:
if ( (i+1) < argc ){ ++m;
base_level = atoi( argv[ i + 1 ] ); mode = 0;
basechange = 1;
}
break;
case 'p':
add_parentheses = 1;
break; break;
} }
break;
} /* end switch mode */
bol = 0;
if(*m == '\n' || *m == '\r')
bol = 1;
} /* end for mode */
*h = 0;
if(return_status & 2) {
fprintf(hlp, "%s\n\n", uit);
}
else {
fnamepart(filespec, subjectname, 3);
if(*subjectname) {
fprintf(hlp, "%d %s\n\n%s\n\n", base_level, subjectname, uit);
} }
if ( basechange){ else {
basechange = 0; /* No filename (as is the case with a logical),
i = i + 1; use first word as subject name */
} char *n, *s;
}else{
if ( manfile == NULL ){ for(n = in; isspace(*n); ++n)
manfile = strdup( argv[i]); ;
} else if ( helpfile == NULL ){ for(s = subjectname; !(isspace(*n)); ++n, ++s)
helpfile = strdup( argv[i]); *s = *n;
} else { *s = 0;
fprintf( stderr, "Unrecognized parameter : %s\n", argv[i]);
fprintf(hlp, "%d %s\n\n%s\n\n", base_level, subjectname, uit);
} }
} }
/*
printf("read %d from %s, written %d to helpfile, return_status = %d\n",
len, filespec, strlen(uit), return_status);
*/
free(m);
free(h);
return 1;
} }
/*--------------------------------------------*/
/* fprintf( stderr,"manfile: %s, helpfile: %s, append: %d, base_level : %d\n", int convertmans(char *filespec, char *hlpfilename, int base_level, int append,
manfile, helpfile, append, base_level); int add_parentheses)
*/ {
int status = 1;
manPtr manroot = NULL, m;
FILE *hlp;
status = convertmans( manfile, helpfile, base_level, append, add_parentheses ); if(append) {
hlp = fopen(hlpfilename, "a+");
}
else {
hlp = fopen(hlpfilename, "w");
}
free( manfile ); if(!hlp)
free( helpfile ); return vaxc$errno;
return( status ); status = listofmans(filespec, &manroot);
if(!(status&1))
return status;
for(m = manroot; m; m = m->next) {
status = convertman(m->filename, hlp, base_level, add_parentheses);
if(!(status&1)) {
fprintf(stderr, "Convertman of %s went wrong\n", m->filename);
break;
}
}
freeman(&manroot);
return status;
}
/*--------------------------------------------*/
void print_help(void)
{
fprintf(stderr,
"Usage: [-a] [-b x] convertman <manfilespec> <helptextfile>\n"
" -a append <manfilespec> to <helptextfile>\n"
" -b <baselevel> if no headers found create one "
"with level <baselevel>\n"
" and the filename as title.\n"
" -p add parentheses() to baselevel help items.\n");
}
/*--------------------------------------------*/
main(int argc, char **argv)
{
int status;
int i, j;
int append, base_level, basechange, add_parentheses;
char *manfile = NULL;
char *helpfile = NULL;
if(argc < 3) {
print_help();
return 1;
}
append = 0;
base_level = 1;
basechange = 0;
add_parentheses = 0;
for(i = 1; i < argc; ++i) {
if(argv[i][0] == '-') {
for(j = 1; argv[i][j]; ++j) {
switch(argv[i][j]) {
case 'a':
append = 1;
break;
case 'b':
if((i + 1) < argc) {
base_level = atoi(argv[i + 1]);
basechange = 1;
}
break;
case 'p':
add_parentheses = 1;
break;
}
}
if(basechange) {
basechange = 0;
i = i + 1;
}
}
else {
if(!manfile) {
manfile = strdup(argv[i]);
}
else if(!helpfile) {
helpfile = strdup(argv[i]);
}
else {
fprintf(stderr, "Unrecognized parameter : %s\n", argv[i]);
}
}
}
/*
fprintf(stderr,"manfile: %s, helpfile: %s, append: %d, base_level : %d\n",
manfile, helpfile, append, base_level);
*/
status = convertmans(manfile, helpfile, base_level, append,
add_parentheses);
free(manfile);
free(helpfile);
return status;
} }