diff --git a/.checksrc b/.checksrc deleted file mode 100644 index e6e578fc..00000000 --- a/.checksrc +++ /dev/null @@ -1,3 +0,0 @@ -disable FOPENMODE -disable SNPRINTF -disable TYPEDEFSTRUCT diff --git a/Makefile.am b/Makefile.am index 0a564680..ad579fe2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -81,6 +81,4 @@ gen-coverage: coverage: init-coverage build-coverage gen-coverage checksrc: - perl ci/checksrc.pl -i4 -m79 \ - -Wsrc/libssh2_config.h \ - src/*.[ch] include/*.h example/*.c tests/*.[ch] + ci/checksrc.sh diff --git a/REUSE.toml b/REUSE.toml index 0d2de704..12b29922 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -44,7 +44,6 @@ path = [ "vms/man2help.c", "vms/readme.vms", # dotfiles - ".checksrc", ".editorconfig", ".gitignore", "docs/.gitignore", diff --git a/ci/checksrc.pl b/ci/checksrc.pl index 34664585..0f727e38 100755 --- a/ci/checksrc.pl +++ b/ci/checksrc.pl @@ -50,54 +50,58 @@ my @ignore_line; my %warnings_extended = ( 'COPYRIGHTYEAR' => 'copyright year incorrect', 'STRERROR', => 'strerror() detected', + 'STRNCPY', => 'strncpy() detected', 'STDERR', => 'stderr detected', ); my %warnings = ( 'ASSIGNWITHINCONDITION' => 'assignment within conditional expression', - 'ASTERISKNOSPACE' => 'pointer declared without space before asterisk', - 'ASTERISKSPACE' => 'pointer declared with space after asterisk', - 'BADCOMMAND' => 'bad !checksrc! instruction', - 'BANNEDFUNC' => 'a banned function was used', - 'BANNEDPREPROC' => 'a banned symbol was used on a preprocessor line', - 'BRACEELSE' => '} else on the same line', - 'BRACEPOS' => 'wrong position for an open brace', - 'BRACEWHILE' => 'A single space between open brace and while', - 'COMMANOSPACE' => 'comma without following space', - 'COMMENTNOSPACEEND' => 'no space before */', - 'COMMENTNOSPACESTART' => 'no space following /*', - 'COPYRIGHT' => 'file missing a copyright statement', - 'CPPCOMMENTS' => '// comment detected', - 'DOBRACE' => 'A single space between do and open brace', - 'EMPTYLINEBRACE' => 'Empty line before the open brace', - 'EQUALSNOSPACE' => 'equals sign without following space', - 'EQUALSNULL' => 'if/while comparison with == NULL', - 'EXCLAMATIONSPACE' => 'Whitespace after exclamation mark in expression', - 'FOPENMODE' => 'fopen needs a macro for the mode string', - 'INCLUDEDUP', => 'same file is included again', - 'INDENTATION' => 'wrong start column for code', - 'LONGLINE' => "Line longer than $max_column", - 'SPACEBEFORELABEL' => 'labels not at the start of the line', - 'MULTISPACE' => 'multiple spaces used when not suitable', - 'NOSPACEEQUALS' => 'equals sign without preceding space', - 'NOTEQUALSZERO', => 'if/while comparison with != 0', - 'ONELINECONDITION' => 'conditional block on the same line as the if()', - 'OPENCOMMENT' => 'file ended with a /* comment still "open"', - 'PARENBRACE' => '){ without sufficient space', - 'RETURNNOSPACE' => 'return without space', - 'SEMINOSPACE' => 'semicolon without following space', - 'SIZEOFNOPAREN' => 'use of sizeof without parentheses', - 'SNPRINTF' => 'use of snprintf', - 'SPACEAFTERPAREN' => 'space after open parenthesis', - 'SPACEBEFORECLOSE' => 'space before a close parenthesis', - 'SPACEBEFORECOMMA' => 'space before a comma', - 'SPACEBEFOREPAREN' => 'space before an open parenthesis', - 'SPACESEMICOLON' => 'space before semicolon', - 'SPACESWITCHCOLON' => 'space before colon of switch label', - 'TABS' => 'TAB characters not allowed', - 'TRAILINGSPACE' => 'Trailing whitespace on the line', - 'TYPEDEFSTRUCT' => 'typedefed struct', - 'UNUSEDIGNORE' => 'a warning ignore was not used', + 'ASTERISKNOSPACE' => 'pointer declared without space before asterisk', + 'ASTERISKSPACE' => 'pointer declared with space after asterisk', + 'BADCOMMAND' => 'bad !checksrc! instruction', + 'BANNEDFUNC' => 'a banned function was used', + 'BANNEDPREPROC' => 'a banned symbol was used on a preprocessor line', + 'BRACEELSE' => '} else on the same line', + 'BRACEPOS' => 'wrong position for an open brace', + 'BRACEWHILE' => 'A single space between open brace and while', + 'COMMANOSPACE' => 'comma without following space', + 'COMMENTNOSPACEEND' => 'no space before */', + 'COMMENTNOSPACESTART' => 'no space following /*', + 'COPYRIGHT' => 'file missing a copyright statement', + 'CPPCOMMENTS' => '// comment detected', + 'DOBRACE' => 'A single space between do and open brace', + 'EMPTYLINEBRACE' => 'Empty line before the open brace', + 'EQUALSNOSPACE' => 'equals sign without following space', + 'EQUALSNULL' => 'if/while comparison with == NULL', + 'EXCLAMATIONSPACE' => 'Whitespace after exclamation mark in expression', + 'FOPENMODE' => 'fopen needs a macro for the mode string', + 'INCLUDEDUP', => 'same file is included again', + 'INDENTATION' => 'wrong start column for code', + 'LONGLINE' => "Line longer than $max_column", + 'SPACEBEFORELABEL' => 'labels not at the start of the line', + 'MULTISPACE' => 'multiple spaces used when not suitable', + 'NOSPACEC' => 'missing space around ternary colon operator', + 'NOSPACEEQUALS' => 'equals sign without preceding space', + 'NOSPACEQ' => 'missing space around ternary question mark operator', + 'NOSPACETHAN' => 'missing space around less or greater than', + 'NOTEQUALSZERO', => 'if/while comparison with != 0', + 'ONELINECONDITION' => 'conditional block on the same line as the if()', + 'OPENCOMMENT' => 'file ended with a /* comment still "open"', + 'PARENBRACE' => '){ without sufficient space', + 'RETURNNOSPACE' => 'return without space', + 'SEMINOSPACE' => 'semicolon without following space', + 'SIZEOFNOPAREN' => 'use of sizeof without parentheses', + 'SNPRINTF' => 'use of snprintf', + 'SPACEAFTERPAREN' => 'space after open parenthesis', + 'SPACEBEFORECLOSE' => 'space before a close parenthesis', + 'SPACEBEFORECOMMA' => 'space before a comma', + 'SPACEBEFOREPAREN' => 'space before an open parenthesis', + 'SPACESEMICOLON' => 'space before semicolon', + 'SPACESWITCHCOLON' => 'space before colon of switch label', + 'TABS' => 'TAB characters not allowed', + 'TRAILINGSPACE' => 'Trailing whitespace on the line', + 'TYPEDEFSTRUCT' => 'typedefed struct', + 'UNUSEDIGNORE' => 'a warning ignore was not used', ); sub readskiplist { @@ -115,11 +119,23 @@ sub readskiplist { # and since that's already handled via !checksrc! commands there is probably # little use to add it. sub readlocalfile { + my ($file) = @_; 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>) { + $windows_os ? $_ =~ s/\r?\n$// : chomp; $i++; # Lines starting with '#' are considered comments @@ -263,7 +279,7 @@ if(!$file) { } readskiplist(); -readlocalfile(); +readlocalfile($file); do { if("$wlist" !~ / $file /) { @@ -403,6 +419,13 @@ sub scanfile { 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 if($l =~ /\* +copyright .* (\d\d\d\d|)/i) { my $count = 0; @@ -603,6 +626,62 @@ sub scanfile { "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 # close parenthesis! if($l =~ /(.*[^\) ]) \)/) { @@ -712,7 +791,8 @@ sub scanfile { strtok| v?sprintf| (str|_mbs|_tcs|_wcs)n?cat| - LoadLibrary(Ex)?(A|W)?) + LoadLibrary(Ex)?(A|W)?| + _?w?access) \s*\( /x) { 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"}) { # scan for use of banned stderr. This is not a BANNEDFUNC to # allow for individual enable/disable of this warning. @@ -915,7 +1007,7 @@ sub scanfile { 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 if(defined($warnings{"COPYRIGHTYEAR"})) { # The check for updated copyrightyear is overly complicated in order to diff --git a/ci/checksrc.sh b/ci/checksrc.sh index 49967463..d473a158 100755 --- a/ci/checksrc.sh +++ b/ci/checksrc.sh @@ -6,6 +6,5 @@ set -e cd "$(dirname "$0")/.." -perl ./ci/checksrc.pl -i4 -m79 \ - -Wsrc/libssh2_config.h \ - src/*.[ch] include/*.h example/*.c tests/*.[ch] +git ls-files "*.[ch]" | xargs -n1 \ +./ci/checksrc.pl -i4 -m79 -AFOPENMODE -ASNPRINTF -ATYPEDEFSTRUCT diff --git a/os400/ccsid.c b/os400/ccsid.c index 49ca4b1f..0eb9da24 100644 --- a/os400/ccsid.c +++ b/os400/ccsid.c @@ -88,7 +88,7 @@ terminator_size(unsigned short ccsid) /* Return the null-terminator size for the given CCSID. */ /* Fast check usual CCSIDs. */ - switch (ccsid) { + switch(ccsid) { case CCSID_UTF8: case 0: /* Job CCSID is SBCS EBCDIC. */ 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 result. */ - memset((void *) &outcode, 0, sizeof outcode); + memset((void *) &outcode, 0, sizeof(outcode)); outcode.CCSID = ccsid; cd = QtqIconvOpen(&outcode, (QtqCode_T *) &utf8code); - if (cd.return_value == -1) + if(cd.return_value == -1) return -1; inp = ""; ilen = 1; outp = buf; - olen = sizeof buf; + olen = sizeof(buf); iconv(cd, &inp, &ilen, &outp, &olen); iconv_close(cd); - olen = sizeof buf - olen; - return olen? olen: -1; + olen = sizeof(buf - olen); + return olen ? olen : -1; } static char * @@ -132,31 +132,31 @@ convert_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache, QtqCode_T outcode; iconv_t cd; - if (!instring) { - if (outlen) + if(!instring) { + if(outlen) *outlen = 0; return NULL; } - if (outlen) + if(outlen) *outlen = -1; - if (!cache) + if(!cache) return NULL; /* Get terminator size. */ termsize = terminator_size(outccsid); - if (termsize < 0) + if(termsize < 0) return NULL; /* Prepare conversion parameters. */ - memset((void *) &incode, 0, sizeof incode); - memset((void *) &outcode, 0, sizeof outcode); + memset((void *) &incode, 0, sizeof(incode)); + memset((void *) &outcode, 0, sizeof(outcode)); incode.CCSID = inccsid; outcode.CCSID = outccsid; curlen = OFFSET_OF(libssh2_string_cache, string); inp = (char *) instring; ilen = inlen; buflen = inlen + curlen; - if (inlen < 0) { + if(inlen < 0) { incode.length_option = 1; buflen = STRING_GRANULE; ilen = 0; @@ -164,34 +164,34 @@ convert_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache, /* Allocate output string buffer and open conversion descriptor. */ dst = ALLOC(session, buflen + termsize); - if (!dst) + if(!dst) return NULL; cd = QtqIconvOpen(&outcode, &incode); - if (cd.return_value == -1) { + if(cd.return_value == -1) { FREE(session, dst); return NULL; } /* Convert string. */ - for (;;) { + for(;;) { outp = dst + curlen; olen = buflen - curlen; 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 terminator. */ - if (termsize) { + if(termsize) { memset(outp, 0, termsize); olen -= termsize; } } curlen = buflen - olen; - if (i >= 0 || errno != E2BIG) + if(i >= 0 || errno != E2BIG) break; /* Must expand buffer. */ buflen += STRING_GRANULE; outp = REALLOC(session, dst, buflen + termsize); - if (!outp) + if(!outp) break; dst = outp; } @@ -199,19 +199,19 @@ convert_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache, iconv_close(cd); /* Check for error. */ - if (i < 0 || !outp) { + if(i < 0 || !outp) { FREE(session, dst); return NULL; } /* Process terminator. */ - if (inlen < 0) + if(inlen < 0) curlen -= termsize; - else if (termsize) + else if(termsize) memset(dst + curlen, 0, termsize); /* Shorten buffer if possible. */ - if (curlen < buflen) + if(curlen < buflen) dst = REALLOC(session, dst, curlen + termsize); /* Link to cache. */ @@ -220,7 +220,7 @@ convert_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache, *cache = outstring; /* Return length if required. */ - if (outlen) + if(outlen) *outlen = curlen - OFFSET_OF(libssh2_string_cache, string); return outstring->string; @@ -250,8 +250,8 @@ libssh2_release_string_cache(LIBSSH2_SESSION *session, { libssh2_string_cache *p; - if (cache) - while ((p = *cache)) { + if(cache) + while((p = *cache)) { *cache = p->next; FREE(session, (char *) p); } diff --git a/os400/include/stdio.h b/os400/include/stdio.h index 4d4543fa..79f26af6 100644 --- a/os400/include/stdio.h +++ b/os400/include/stdio.h @@ -42,8 +42,8 @@ #define LIBSSH2_STDIO_H /* - * wrapper. - * Its goal is to redefine snprintf/vsnprintf which are not supported by QADRT. + * wrapper. + * Its goal is to redefine snprintf/vsnprintf which are not supported by QADRT. */ #include diff --git a/os400/include/sys/socket.h b/os400/include/sys/socket.h index 4a343ffe..dbb3f33c 100644 --- a/os400/include/sys/socket.h +++ b/os400/include/sys/socket.h @@ -66,7 +66,7 @@ #endif extern int _libssh2_os400_connect(int sd, - struct sockaddr * destaddr, int addrlen); + struct sockaddr *destaddr, int addrlen); #ifndef LIBSSH2_DISABLE_QADRT_EXT #define connect(sd, addr, len) _libssh2_os400_connect((sd), (addr), (len)) diff --git a/os400/macros.h b/os400/macros.h index 9766489b..fa2b22ee 100644 --- a/os400/macros.h +++ b/os400/macros.h @@ -102,11 +102,13 @@ libssh2_channel_subsystem(LIBSSH2_CHANNEL *channel, const char *subsystem); LIBSSH2_API ssize_t libssh2_channel_read(LIBSSH2_CHANNEL *channel, char *buf, size_t buflen); 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_channel_window_read(LIBSSH2_CHANNEL *channel); 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_channel_write_stderr(LIBSSH2_CHANNEL *channel, const char *buf, size_t buflen); diff --git a/os400/os400sys.c b/os400/os400sys.c index ffaf9235..e0ce568b 100644 --- a/os400/os400sys.c +++ b/os400/os400sys.c @@ -77,55 +77,54 @@ static int -convert_sockaddr(struct sockaddr_storage * dstaddr, - const struct sockaddr * srcaddr, int srclen) - +convert_sockaddr(struct sockaddr_storage *dstaddr, + const struct sockaddr *srcaddr, int srclen) { - const struct sockaddr_un * srcu; - struct sockaddr_un * dstu; - unsigned int i; - unsigned int dstsize; + const struct sockaddr_un *srcu; + struct sockaddr_un *dstu; + unsigned int i; + 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) + - sizeof srcaddr->sa_family || srclen > sizeof *dstaddr) { - errno = EINVAL; - return -1; + if(!srcaddr || srclen < offsetof(struct sockaddr, sa_family) + + sizeof(srcaddr->sa_family) || srclen > sizeof(*dstaddr)) { + errno = EINVAL; + 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: - srcu = (const struct sockaddr_un *) srcaddr; - dstu = (struct sockaddr_un *) dstaddr; - dstsize = sizeof *dstaddr - offsetof(struct sockaddr_un, sun_path); - srclen -= offsetof(struct sockaddr_un, sun_path); - i = QadrtConvertA2E(dstu->sun_path, srcu->sun_path, dstsize - 1, srclen); - dstu->sun_path[i] = '\0'; - i += offsetof(struct sockaddr_un, sun_path); - srclen = i; + case AF_UNIX: + srcu = (const struct sockaddr_un *) srcaddr; + dstu = (struct sockaddr_un *) dstaddr; + dstsize = sizeof(*dstaddr) - offsetof(struct sockaddr_un, sun_path); + srclen -= offsetof(struct sockaddr_un, sun_path); + i = QadrtConvertA2E(dstu->sun_path, srcu->sun_path, + dstsize - 1, srclen); + dstu->sun_path[i] = '\0'; + i += offsetof(struct sockaddr_un, sun_path); + srclen = i; } - return srclen; + return srclen; } int -_libssh2_os400_connect(int sd, struct sockaddr * destaddr, int addrlen) - +_libssh2_os400_connect(int sd, struct sockaddr *destaddr, int addrlen) { - int i; - struct sockaddr_storage laddr; + int i; + struct sockaddr_storage laddr; - i = convert_sockaddr(&laddr, destaddr, addrlen); + i = convert_sockaddr(&laddr, destaddr, addrlen); - if(i < 0) - return -1; + if(i < 0) + 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; char *buf; - if (!dst || !len) { + if(!dst || !len) { errno = EINVAL; return -1; } - if (l < len) + if(l < len) l = len; buf = alloca(l); - if (!buf) { + if(!buf) { errno = ENOMEM; return -1; } + /* !checksrc! disable BANNEDFUNC 1 */ /* FIXME */ i = vsprintf(buf, fmt, args); - if (i < 0) + if(i < 0) return i; - if (--len > i) + if(--len > i) len = i; - if (len) + if(len) memcpy(dst, buf, len); dst[len] = '\0'; @@ -188,11 +188,11 @@ _libssh2_os400_inflateInit_(z_streamp strm, char *ebcversion; int i; - if (!version) + if(!version) return Z_VERSION_ERROR; i = strlen(version); ebcversion = alloca(i + 1); - if (!ebcversion) + if(!ebcversion) return Z_VERSION_ERROR; i = QadrtConvertA2E(ebcversion, version, i, i - 1); ebcversion[i] = '\0'; @@ -206,11 +206,11 @@ _libssh2_os400_deflateInit_(z_streamp strm, int level, char *ebcversion; int i; - if (!version) + if(!version) return Z_VERSION_ERROR; i = strlen(version); ebcversion = alloca(i + 1); - if (!ebcversion) + if(!ebcversion) return Z_VERSION_ERROR; i = QadrtConvertA2E(ebcversion, version, i, i - 1); ebcversion[i] = '\0'; diff --git a/src/bcrypt_pbkdf.c b/src/bcrypt_pbkdf.c index 414c5e4e..73cda435 100644 --- a/src/bcrypt_pbkdf.c +++ b/src/bcrypt_pbkdf.c @@ -116,7 +116,7 @@ bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt, if(rounds < 1) return -1; 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; countsalt = calloc(1, saltlen + 4); if(!countsalt) diff --git a/src/channel.c b/src/channel.c index 2f7d162c..529c2ef8 100644 --- a/src/channel.c +++ b/src/channel.c @@ -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", (long)bytes_want, channel->local.id, channel->remote.id, stream_id, - unlink_packet?" [ul]":"")); + unlink_packet ? " [ul]" : "")); /* copy data from this struct to the target buffer */ memcpy(&buf[bytes_read], diff --git a/src/openssl.c b/src/openssl.c index b2a5eb7a..90732c89 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -4071,7 +4071,7 @@ _libssh2_ecdsa_create_key(LIBSSH2_SESSION *session, EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); 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) { ret = EVP_PKEY_keygen(ctx, &private_key); } diff --git a/src/os400qc3.c b/src/os400qc3.c index 261abf09..9ec9b036 100644 --- a/src/os400qc3.c +++ b/src/os400qc3.c @@ -367,7 +367,7 @@ _libssh2_random(unsigned char *buf, size_t len) set_EC_length(errcode, sizeof(errcode)); Qc3GenPRNs(buf, len, Qc3PRN_TYPE_NORMAL, Qc3PRN_NO_PARITY, (char *) &errcode); - return errcode.Bytes_Available? -1: 0; + return errcode.Bytes_Available ? -1 : 0; } _libssh2_bn * @@ -967,7 +967,7 @@ _libssh2_os400qc3_hash_init(Qc3_Format_ALGD0100_T *x, unsigned int algorithm) set_EC_length(errcode, sizeof(errcode)); Qc3CreateAlgorithmContext((char *) &algd, Qc3_Alg_Hash, x->Alg_Context_Token, &errcode); - return errcode.Bytes_Available? 0: 1; + return errcode.Bytes_Available ? 0 : 1; } int @@ -981,7 +981,7 @@ _libssh2_os400qc3_hash_update(Qc3_Format_ALGD0100_T *ctx, set_EC_length(errcode, sizeof(errcode)); Qc3CalculateHash((char *) data, &len, Qc3_Data, (char *) ctx, Qc3_Alg_Token, anycsp, NULL, dummy, &errcode); - return errcode.Bytes_Available? 0: 1; + return errcode.Bytes_Available ? 0 : 1; } int @@ -996,7 +996,7 @@ _libssh2_os400qc3_hash_final(Qc3_Format_ALGD0100_T *ctx, unsigned char *out) anycsp, NULL, (char *) out, &errcode); Qc3DestroyAlgorithmContext(ctx->Alg_Context_Token, (char *) &ecnull); memset(ctx->Alg_Context_Token, 0, sizeof(ctx->Alg_Context_Token)); - return errcode.Bytes_Available? 0: 1; + return errcode.Bytes_Available ? 0 : 1; } int @@ -1036,7 +1036,7 @@ libssh2_os400qc3_hmac_init(_libssh2_os400qc3_crypto_ctx *ctx, Qc3CreateKeyContext((char *) key, &keylen, binstring, &algo, qc3clear, NULL, NULL, ctx->key.Key_Context_Token, (char *) &errcode); - return errcode.Bytes_Available? 0: 1; + return errcode.Bytes_Available ? 0 : 1; } 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, Qc3_Alg_Token, ctx->key.Key_Context_Token, Qc3_Key_Token, 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) @@ -1104,7 +1104,7 @@ int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *out) Qc3CalculateHMAC((char *) data, &zero, Qc3_Data, (char *) &ctx->hash, Qc3_Alg_Token, ctx->key.Key_Context_Token, Qc3_Key_Token, 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) @@ -1182,7 +1182,7 @@ _libssh2_cipher_crypt(_libssh2_cipher_ctx *ctx, ctx->key.Key_Context_Token, Qc3_Key_Token, anycsp, NULL, (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++) if(!oidcmp(&oid, (*algotable)->oid)) return (*(*algotable)->parse)(session, pkcs5, *algotable, - param.header? ¶m: NULL); + param.header ? ¶m : NULL); 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 *) &rsa->key, Qc3_Key_Token, anycsp, NULL, (char *) &errcode); - return errcode.Bytes_Available? -1: 0; + return errcode.Bytes_Available ? -1 : 0; } int diff --git a/src/sftp.c b/src/sftp.c index 8bd4549e..6ede3111 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -362,8 +362,8 @@ window_adjust: 1, NULL); /* store the state so that we continue with the correct operation at next invoke */ - sftp->packet_state = (rc == LIBSSH2_ERROR_EAGAIN)? - libssh2_NB_state_sent: + sftp->packet_state = (rc == LIBSSH2_ERROR_EAGAIN) ? + libssh2_NB_state_sent : libssh2_NB_state_idle; 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) + flags(4) */ 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 */ sftp->open_packet_sent = 0; @@ -1183,7 +1183,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename, LIBSSH2_SFTP_ATTR_PFILETYPE_DIR); _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++; _libssh2_store_u32(&s, sftp->open_request_id); _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", - open_file? "file" : "directory")); + open_file ? "file" : "directory")); sftp->open_state = libssh2_NB_state_created; } diff --git a/src/transport.c b/src/transport.c index a3cf1b36..e1120656 100644 --- a/src/transport.c +++ b/src/transport.c @@ -1212,7 +1212,7 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, int firstlast = i == 0 ? FIRST_BLOCK : (!CRYPT_FLAG_L(session, INTEGRATED_MAC) && (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 because everything is offset by 4 since the initial packet_length isn't encrypted. In this case, combine that last diff --git a/tests/ossfuzz/ssh2_client_fuzzer.cc b/tests/ossfuzz/ssh2_client_fuzzer.cc index 86d2ab99..047850ea 100644 --- a/tests/ossfuzz/ssh2_client_fuzzer.cc +++ b/tests/ossfuzz/ssh2_client_fuzzer.cc @@ -1,3 +1,7 @@ +/* Copyright (C) The libssh2 project and its contributors. + * + * SPDX-License-Identifier: BSD-3-Clause + */ #include #include #include diff --git a/tests/ossfuzz/standaloneengine.cc b/tests/ossfuzz/standaloneengine.cc index 93eb8d9f..e40cb2b2 100644 --- a/tests/ossfuzz/standaloneengine.cc +++ b/tests/ossfuzz/standaloneengine.cc @@ -1,3 +1,7 @@ +/* Copyright (C) The libssh2 project and its contributors. + * + * SPDX-License-Identifier: BSD-3-Clause + */ #include #include diff --git a/tests/ossfuzz/testinput.h b/tests/ossfuzz/testinput.h index 6ab9b515..6f9241be 100644 --- a/tests/ossfuzz/testinput.h +++ b/tests/ossfuzz/testinput.h @@ -1,3 +1,7 @@ +/* Copyright (C) The libssh2 project and its contributors. + * + * SPDX-License-Identifier: BSD-3-Clause + */ #include extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); diff --git a/vms/libssh2_config.h b/vms/libssh2_config.h index 34d66c3b..2cbe78a7 100644 --- a/vms/libssh2_config.h +++ b/vms/libssh2_config.h @@ -1,3 +1,8 @@ +/* Copyright (C) The libssh2 project and its contributors. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + #ifndef LIBSSH2_CONFIG_H #ifdef __VMS diff --git a/vms/man2help.c b/vms/man2help.c index 49ef10d2..2cdad29d 100644 --- a/vms/man2help.c +++ b/vms/man2help.c @@ -1,3 +1,8 @@ +/* Copyright (C) The libssh2 project and its contributors. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + #include #include #include @@ -10,12 +15,12 @@ #include #include -typedef struct manl{ +typedef struct manl { struct manl *next; char *filename; -}man, *manPtr; +} man, *manPtr; -typedef struct pf_fabnam{ +typedef struct pf_fabnam { struct FAB dfab; struct RAB drab; 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; -int i; + char *is, *os; + int i; -if ( len ){ - for ( is = input, os = output, i = 0; i < len ; ++i, ++is, ++os){ + if(len) { + for(is = input, os = output, i = 0; i < len; ++i, ++is, ++os) { *os = *is; + } + *os = 0; + } + else { + output[0] = 0; } - *os = 0; -}else{ - output[0] = 0; -} } /*----------------------------------------------------------*/ /* 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; -int status; -char ipart[6][256], *i, *p; + pfnPtr pf; + int status; + char ipart[6][256], *i, *p; -pf = calloc( 1, sizeof( pfn ) ); + pf = calloc(1, sizeof(pfn)); -pf->dfab = cc$rms_fab; -pf->drab = cc$rms_rab; -pf->dnam = cc$rms_naml; + pf->dfab = cc$rms_fab; + pf->drab = cc$rms_rab; + 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_dna = (char *) -1; -pf->dfab.fab$b_fns = 0; -pf->dfab.fab$w_ifi = 0; + pf->dfab.fab$l_fna = (char *) -1; + pf->dfab.fab$l_dna = (char *) -1; + pf->dfab.fab$b_fns = 0; + pf->dfab.fab$w_ifi = 0; -pf->dnam.naml$l_long_defname = NULL; //inputfile; -pf->dnam.naml$l_long_defname_size = 0;//strlen( inputfile ); + pf->dnam.naml$l_long_defname = NULL; /* inputfile; */ + pf->dnam.naml$l_long_defname_size = 0; /* strlen(inputfile); */ -pf->dnam.naml$l_long_filename = inputfile; -pf->dnam.naml$l_long_filename_size = strlen( inputfile ); + pf->dnam.naml$l_long_filename = 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_alloc = NAM$C_MAXRSS ; + pf->dnam.naml$l_long_expand = pf->expanded_filename; + 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 ); -if ( !(status&1) ){ - free( pf ); - return( status ); -} + status = sys$parse(&pf->dfab, 0, 0); + if(!(status&1)) { + free(pf); + return status; + } -fpcopy ( ipart[0], pf->dnam.naml$l_long_node , pf->dnam.naml$l_long_node_size); -fpcopy ( ipart[1], pf->dnam.naml$l_long_dev , pf->dnam.naml$l_long_dev_size); -fpcopy ( ipart[2], pf->dnam.naml$l_long_dir , 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); + fpcopy(ipart[0], pf->dnam.naml$l_long_node, + pf->dnam.naml$l_long_node_size); + fpcopy(ipart[1], pf->dnam.naml$l_long_dev, + pf->dnam.naml$l_long_dev_size); + fpcopy(ipart[2], pf->dnam.naml$l_long_dir, + 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){ - if ( p == part ){ - *p = toupper( *i ); - }else{ - *p = tolower( *i ); - } -} -*p = 0; + for(i = ipart[whatpart], p = part; *i; ++i, ++p) { + if(p == part) { + *p = toupper(*i); + } + else { + *p = tolower(*i); + } + } + *p = 0; -free( pf ); -return(1); + free(pf); + return 1; } /*----------------------------------------------------------*/ -int find_file(char *filename,char *gevonden,int *findex) +int find_file(char *filename, char *gevonden, int *findex) { -int status; -struct dsc$descriptor gevondend; -struct dsc$descriptor filespec; -char gevonden_file[NAM$C_MAXRSS + 1]; + int status; + struct dsc$descriptor gevondend; + struct dsc$descriptor filespec; + char gevonden_file[NAM$C_MAXRSS + 1]; -filespec.dsc$w_length = strlen(filename); -filespec.dsc$b_dtype = DSC$K_DTYPE_T; -filespec.dsc$b_class = DSC$K_CLASS_S; -filespec.dsc$a_pointer = filename; + filespec.dsc$w_length = strlen(filename); + filespec.dsc$b_dtype = DSC$K_DTYPE_T; + filespec.dsc$b_class = DSC$K_CLASS_S; + filespec.dsc$a_pointer = filename; -gevondend.dsc$w_length = NAM$C_MAXRSS; -gevondend.dsc$b_dtype = DSC$K_DTYPE_T; -gevondend.dsc$b_class = DSC$K_CLASS_S; -gevondend.dsc$a_pointer = gevonden_file; + gevondend.dsc$w_length = NAM$C_MAXRSS; + gevondend.dsc$b_dtype = DSC$K_DTYPE_T; + gevondend.dsc$b_class = DSC$K_CLASS_S; + 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 ){ - strcpy(gevonden,strtok(gevonden_file," ")); -}else{ - 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; + if((status & 1) == 1) { + /* !checksrc! disable BANNEDFUNC 1 */ /* FIXME */ + strcpy(gevonden, strtok(gevonden_file, " ")); } + 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; -char *in, *uit; -char *m,*h; -size_t len, thislen, maxlen= 50000; -int bol,mode, return_status=1; -char subjectname[ NAM$C_MAXRSS + 1 ]; + manPtr r; + int status; + int ffindex = 0; + char gevonden[NAM$C_MAXRSS + 1]; -in = calloc( 1, maxlen + 1 ); -uit = calloc( 1, maxlen + 1 ); + for(;;) { + 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" ); -if ( man == NULL ) return(vaxc$errno); + lib$find_file_end(&ffindex); + if(status == RMS$_NMF) + status = 1; -for( len = 0; !feof( man ) && len < maxlen ; len += thislen ){ - thislen = fread( in + len, 1, maxlen - len, man ); + return status; } -fclose(man); +/*--------------------------------------------*/ -m = in; -h = uit; +int convertman(char *filespec, FILE *hlp, int base_level, int add_parentheses) +{ + 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: - switch(*m){ + switch(*m) { case '.': - if ( bol ){ + if(bol) { mode = 1; - }else{ + } + else { *h = *m; ++h; } break; case '\\': - if ( bol ){ - *h = ' ';++h; - *h = ' ';++h; + if(bol) { + *h = ' '; ++h; + *h = ' '; ++h; } mode = 2; break; default: - if ( bol ){ - *h = ' ';++h; - *h = ' ';++h; + if(bol) { + *h = ' '; ++h; + *h = ' '; ++h; } *h = *m; ++h; break; - } - break; - case 1: /* after . at bol */ + } + break; - switch(*m){ + case 1: /* after . at bol */ + switch(*m) { case '\\': - while( *m != '\n' && *m != '\r' && *m )++m; + while(*m != '\n' && *m != '\r' && *m) + ++m; mode = 0; break; case 'B': - ++m; - *h = ' ';++h; - mode = 0; - break; + ++m; + *h = ' '; ++h; + mode = 0; + break; case 'I': - /* remove preceding eol */ - if ( *(m+1) != 'P' ){ + /* remove preceding eol */ + if(*(m + 1) != 'P') { + --h; + while((*h == '\n' || *h == '\r') && h > uit) --h; - while ( (*h == '\n' || *h == '\r') && h > uit )--h; - ++h; - } + ++h; + } - /* skip .Ix */ - for(;*m != ' ' && *m != '\n' && *m != '\r'; ++m); + /* skip .Ix */ + 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) == '.'){ - --h; - --m; - }else{ - /* if line does not end in ., skip EOL in source */ + if(*(h-1) == '.') { + --h; + --m; + } + else { + /* if line does not end in ., skip EOL in source */ - if ( *(m+1) == '\n' || *(m+1) == '\r')++m; - } - mode = 0; - break; + if(*(m + 1) == '\n' || *(m + 1) == '\r') + ++m; + } + mode = 0; + break; case 'S': - if ( *(m+1) == 'H' ){ + if(*(m + 1) == 'H') { *h = '\n';++h; - if ( strncmp( m+3 ,"NAME",4) == 0 || - strncmp( m+3 ,"SYNOPSIS",8) == 0 || - strncmp( m+3 ,"DESCRIPTION",11) == 0 ){ - while( *m != '\n' && *m != '\r')++m; + if(strncmp(m + 3, "NAME", 4) == 0 || + strncmp(m + 3, "SYNOPSIS", 8) == 0 || + strncmp(m + 3, "DESCRIPTION", 11) == 0) { + while(*m != '\n' && *m != '\r') + ++m; mode = 0; - }else{ + } + else { ++m; /* write help level, and flag it */ - *h = '0' + base_level + 1;++h; + *h = '0' + base_level + 1; ++h; return_status |= 2; - *h = ' ';++h; + *h = ' '; ++h; /* 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 */ /* fill blanks with underscores */ - if ( *m != '\"' ){ - *h = tolower( *m ); - if (*h == ' ') *h = '_'; - }else{ - --h; + if(*m != '\"') { + *h = tolower(*m); + if(*h == ' ') + *h = '_'; + } + else { + --h; } } /* Add a linefeed or two */ - *h = *m;++h; - *h = *m;++h; + *h = *m; ++h; + *h = *m; ++h; mode = 0; } - } - break; + } + break; case 'T': - if ( *(m+1) == 'H' ){ + if(*(m + 1) == 'H') { *h = '0' + base_level; ++h; return_status |= 2; - *h = ' ';++h; - for ( m = m + 3; *m != ' ' && *m ; ++m, ++h ){ - *h = *m; + *h = ' '; ++h; + for(m = m + 3; *m != ' ' && *m; ++m, ++h) { + *h = *m; } - if ( add_parentheses ){ - *h = '(';++h; - *h = ')';++h; + if(add_parentheses) { + *h = '('; ++h; + *h = ')'; ++h; } - while( *m != '\n' && *m != '\r' && *m )++m; + while(*m != '\n' && *m != '\r' && *m) + ++m; mode = 0; - } - break; + } + break; default: ++m; mode = 0; break; - } - break; + } + break; case 2: /* after \ skip two characters or print the backslash */ - switch(*m){ - case '\\': - *h = *m; - ++h; - 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 \n" ); - fprintf( stderr, " -a append to \n" ); - fprintf( stderr, " -b if no headers found create one with level \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; + switch(*m) { + case '\\': + *h = *m; + ++h; + mode = 0; break; - case 'b': - if ( (i+1) < argc ){ - base_level = atoi( argv[ i + 1 ] ); - basechange = 1; - } - break; - case 'p': - add_parentheses = 1; + 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); } - if ( basechange){ - basechange = 0; - i = i + 1; - } - }else{ - if ( manfile == NULL ){ - manfile = strdup( argv[i]); - } else if ( helpfile == NULL ){ - helpfile = strdup( argv[i]); - } else { - fprintf( stderr, "Unrecognized parameter : %s\n", argv[i]); + 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; } +/*--------------------------------------------*/ -/* fprintf( stderr,"manfile: %s, helpfile: %s, append: %d, base_level : %d\n", - manfile, helpfile, append, base_level); -*/ +int convertmans(char *filespec, char *hlpfilename, int base_level, int append, + 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 ); -free( helpfile ); + if(!hlp) + 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 \n" + " -a append to \n" + " -b if no headers found create one " + "with level \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; }