Skip to content

Commit

Permalink
Change comments to conform to standard C (for code that may be built …
Browse files Browse the repository at this point in the history
…as part of DMF tools and somewhat out of common NUT setup)
  • Loading branch information
jimklimov committed Aug 19, 2016
1 parent 675c5ee commit 27cbd4d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 50 deletions.
13 changes: 7 additions & 6 deletions common/dmfsnmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ static void (*xml_push_handler)(ne_xml_parser*,
static int (*xml_parse)(ne_xml_parser*, const char*, size_t);
static void (*xml_destroy)(ne_xml_parser*);

// These used to be needed as extern vars by some legacy code elsewhere...
// also they are referenced below, but I'm not sure it is valid code!
// FIXME: Inspect codebase to see if these are at all needed (used to be in snmp-ups.c/h)
/* These vars used to be needed as extern vars by some legacy code elsewhere...
* also they are referenced below, but I'm not sure it is valid code!
*/
/* FIXME: Inspect codebase to see if these are at all needed (used to be in snmp-ups.{c,h}) */
int input_phases, output_phases, bypass_phases;

#if WITH_DMF_LUA
Expand Down Expand Up @@ -738,7 +739,7 @@ mibdmf_parser_new()
{
mibdmf_parser_t *self = (mibdmf_parser_t *) calloc (1, sizeof (mibdmf_parser_t));
assert (self);
// Preallocate the sentinel in tables
/* Preallocate the sentinel in tables */
self->device_table_counter = 1;
self->device_table = (snmp_device_id_t *)calloc(
self->device_table_counter, sizeof(snmp_device_id_t));
Expand Down Expand Up @@ -1410,7 +1411,7 @@ mibdmf_parse_file(char *file_name, mibdmf_parser_t *dmp)
}
if(!dl_handle_libneon){
falg_libneon = 1;
if(load_neon_lib() == ERR) return ERR; // Errors printed by that loader
if(load_neon_lib() == ERR) return ERR; /* Errors printed by that loader */
}
ne_xml_parser *parser = xml_create ();
xml_push_handler (parser, xml_dict_start_cb,
Expand Down Expand Up @@ -1554,7 +1555,7 @@ mibdmf_parse_dir (char *dir_name, mibdmf_parser_t *dmp)
return ENOENT;
}
if(load_neon_lib() == ERR) {
// Note: do not "die" from the library context; that's up to the caller
/* Note: do not "die" from the library context; that's up to the caller */
upslogx(0, "ERROR: can't load Neon library");
return ERR;
}
Expand Down
10 changes: 5 additions & 5 deletions common/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h> // va_*
#include <stdarg.h> /* get the va_* routines */

#include "str.h"

Expand Down Expand Up @@ -618,19 +618,19 @@ char * str_concat(size_t count, ...)
size_t i, len, null_pos;
char* merged = NULL;

// Find required length to store merged string
/* Find required length to store merged string */
va_start(ap, count);
len = 1; // room for '\0' in the end
len = 1; /* room for '\0' in the end */
for(i=0 ; i<count ; i++)
len += strlen(va_arg(ap, char*));
va_end(ap);

// Allocate memory to concat strings
/* Allocate memory to concat strings */
merged = (char*)calloc(len,sizeof(char));
if (merged == NULL)
return merged;

// Actually concatenate strings
/* Actually concatenate strings */
va_start(ap, count);
null_pos = 0;
for(i=0 ; i<count ; i++)
Expand Down
5 changes: 3 additions & 2 deletions include/dmf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Compatibility layer for renamed file and may later become home for common
// DMF-related constructs reusable for other applications than current SNMP one
/* Compatibility layer for renamed file and may later become home for common
* DMF-related constructs reusable for other applications than current SNMP one
*/

/* dmf.h - Header for (TODO:dmf.c) - the Network UPS Tools XML-driver-loader
*
Expand Down
66 changes: 33 additions & 33 deletions tools/nut-scanner/nut-scanner-reindex-dmfsnmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#endif
#include "dmf.h"

// These strings are embedded into <nut> tags to show their schema version
// Strings must verbatim match the XSD (no trailing slash etc.)
/* These strings are embedded into <nut> tags to show their schema version */
/* Strings must verbatim match the XSD (no trailing slash etc.) */
#ifndef XSD_DMFNUTSCAN_VERSION
#define XSD_DMFNUTSCAN_VERSION "1.0.0"
#endif
Expand Down Expand Up @@ -62,10 +62,10 @@ int main(int argc, char *argv[])
int opt_ret;
int result = 0;
int ret_code = EXIT_SUCCESS;
int proceed_on_errors = 1; // By default, do as much as we can
char *dir_name = NULL; // TODO: Make configurable the dir and/or list of files
int proceed_on_errors = 1; /* By default, do as much as we can */
char *dir_name = NULL; /* TODO: Make configurable the dir and/or list of files */
int dir_name_dynamic = 0;
// TODO: Consider DEFAULT_DMFNUTSCAN_DIR for automatic output mode into a file?
/* TODO: Consider DEFAULT_DMFNUTSCAN_DIR for automatic output mode into a file? */
#ifdef DEFAULT_DMFSNMP_DIR_OVERRIDE
#ifdef DEFAULT_DMFSNMP_DIR
#undef DEFAULT_DMFSNMP_DIR
Expand All @@ -79,8 +79,8 @@ int main(int argc, char *argv[])
dir_name = "./";
#endif

// TODO: Usage (help), Command-line args
// option to append just a few (new) files to existing (large) index
/* TODO: Usage (help), Command-line args */
/* option to append just a few (new) files to existing (large) index */

while((opt_ret = getopt_long(argc, argv, optstring, longopts, NULL))!=-1) {

Expand Down Expand Up @@ -133,7 +133,7 @@ int main(int argc, char *argv[])
mibdmf_parser_t * dmp = mibdmf_parser_new();
if (!dmp) {
fatalx(EXIT_FAILURE,"=== DMF-Reindex: FATAL: Can not allocate the DMF parsing structures\n");
// TODO: Can we pass this code to fatalx?
/* TODO: Can we pass this code to fatalx? */
return ENOMEM;
}

Expand All @@ -142,35 +142,35 @@ int main(int argc, char *argv[])
if ( (result != 0) &&
(result == ERR || proceed_on_errors != 1)
) {
// TODO: Error-checking? Faults in some parses should be fatal or not?
/* TODO: Error-checking? Faults in some parses should be fatal or not? */
fatalx(EXIT_FAILURE,"=== DMF-Reindex: FATAL: Could not find or parse some files (return code %i)\n", result);
// TODO: Can we pass this code to fatalx?
/* TODO: Can we pass this code to fatalx? */
return result;
}

// Loop through discovered device_table and print it back as DMF markup
/* Loop through discovered device_table and print it back as DMF markup */
upsdebugx(2, "=== DMF-Reindex: Print DMF subset for snmp_device_table[]...\n\n");

snmp_device_id_t *devtab = mibdmf_get_device_table(dmp);
if (!devtab)
{
fatalx(EXIT_FAILURE,"=== DMF-Reindex: FATAL: Can not access the parsed device_table\n");
// TODO: Can we pass this code to fatalx?
/* TODO: Can we pass this code to fatalx? */
return ENOMEM;
}

// Below we sprintf the index into a memory string, parse the result as
// a DMF with a new alist and tables (to validate) and test the same data
// is found. And only then output the stdout text.
// TODO: uniquify output, so that an old index that was read in does not
// pollute the parsed results (at least not for completely same items as
// already exist in the table)? What to do about partial hits ~ updates?
/* Below we sprintf the index into a memory string, parse the result as
* a DMF with a new alist and tables (to validate) and test the same data
* is found. And only then output the stdout text. */
/* TODO: uniquify output, so that an old index that was read in does not
* pollute the parsed results (at least not for completely same items as
* already exist in the table)? What to do about partial hits ~ updates? */
size_t i;
size_t newdmf_len=0, newdmf_size=1024;
char *newdmf = (char*)calloc(newdmf_size, sizeof(char));
if (!newdmf) {
fatalx(EXIT_FAILURE,"=== DMF-Reindex: FATAL: Can not allocate the buffer for parsed DMF\n");
// TODO: Can we pass this code to fatalx?
/* TODO: Can we pass this code to fatalx? */
return ENOMEM;
}
newdmf_len += snprintf(newdmf + newdmf_len, (newdmf_size - newdmf_len),
Expand All @@ -181,14 +181,14 @@ int main(int argc, char *argv[])
{
upsdebugx(2,"[num=%zu (lenbefore=%zu)]", i, newdmf_len);

// ASSUMPTION: String increments would not exceed these few bytes
/* ASSUMPTION: String increments would not exceed these few bytes */
if ( (newdmf_size - newdmf_len) < 256)
{
newdmf_size += 1024;
newdmf = (char*)realloc(newdmf, newdmf_size * sizeof(char));
if (!newdmf) {
fatalx(EXIT_FAILURE,"=== DMF-Reindex: FATAL: Can not extend the buffer for parsed DMF\n");
// TODO: Can we pass this code to fatalx?
/* TODO: Can we pass this code to fatalx? */
return ENOMEM;
}
upsdebugx(2, "\nExtended the buffer to %zu bytes\n", newdmf_size);
Expand All @@ -197,17 +197,17 @@ int main(int argc, char *argv[])
newdmf_len += snprintf(newdmf + newdmf_len, (newdmf_size - newdmf_len),
"\t<mib2nut ");

// This attr is always present, even if as an empty string:
/* This attr is always present, even if as an empty string: */
newdmf_len += snprintf(newdmf + newdmf_len, (newdmf_size - newdmf_len),
"auto_check=\"%s\" ", devtab[i].oid ? devtab[i].oid : ""); // [3 oid_auto_check] oid
"auto_check=\"%s\" ", devtab[i].oid ? devtab[i].oid : ""); /* [3 oid_auto_check] oid */

if (devtab[i].mib != NULL)
newdmf_len += snprintf(newdmf + newdmf_len, (newdmf_size - newdmf_len),
"mib_name=\"%s\" ", devtab[i].mib); // [0 mib_name] mib
"mib_name=\"%s\" ", devtab[i].mib); /* [0 mib_name] mib */

if (devtab[i].sysoid != NULL)
newdmf_len += snprintf(newdmf + newdmf_len, (newdmf_size - newdmf_len),
"oid=\"%s\" ", devtab[i].sysoid); // [5 sysOID] sysoid/NULL
"oid=\"%s\" ", devtab[i].sysoid); /* [5 sysOID] sysoid/NULL */

newdmf_len += snprintf(newdmf + newdmf_len, (newdmf_size - newdmf_len),
"/>\n");
Expand All @@ -221,37 +221,37 @@ int main(int argc, char *argv[])
mibdmf_parser_t * newdmp = mibdmf_parser_new();
if (!newdmp) {
fatalx(EXIT_FAILURE,"=== DMF-Reindex: FATAL: Can not allocate the DMF verification parsing structures\n\n");
// TODO: Can we pass this code to fatalx?
/* TODO: Can we pass this code to fatalx? */
return ENOMEM;
}

upsdebugx(1, "=== DMF-Reindex: Loading DMF structures from prepared string (verification)\n\n");
ret_code = mibdmf_parse_str(newdmf, newdmp);
// Error checking for one (just made) document makes sense and is definite
/* Error checking for one (just made) document makes sense and is definite */
if ( result != 0 ) {
fatalx(EXIT_FAILURE, "=== DMF-Reindex: The generated document FAILED syntax verification (return code %d)\n\n", ret_code);
// TODO: Can we pass this code to fatalx?
/* TODO: Can we pass this code to fatalx? */
return ret_code;
}

// Loop through reparsed device_table and compare to original one
/* Loop through reparsed device_table and compare to original one */
upsdebugx(1, "=== DMF-Reindex: Verify reparsed content for snmp_device_table[]...\n\n");
snmp_device_id_t *newdevtab = mibdmf_get_device_table(newdmp);
if (!newdevtab)
{
fatalx(EXIT_FAILURE,"=== DMF-Reindex: FATAL: Can not access the reparsed device_table\n");
// TODO: Can we pass this code to fatalx?
/* TODO: Can we pass this code to fatalx? */
return ENOMEM;
}

size_t j=-1, k=-1;
result=0;
// Make sure that all values we've considered are present in re-parse
/* Make sure that all values we've considered are present in re-parse */
for (k=0; devtab[k].oid != NULL || devtab[k].mib != NULL || devtab[k].sysoid != NULL ; k++)
{
int r = 0;
for (j=0; newdevtab[j].oid != NULL || newdevtab[j].mib != NULL || newdevtab[j].sysoid != NULL ; j++)
{ // Note: OID attribute may be empty or NULL, these are assumed equal
{ /* Note: OID attribute may be empty or NULL, these are assumed equal */
if ( (dmf_streq(newdevtab[j].oid, devtab[k].oid)
||dmf_streq(newdevtab[j].oid, "")
||dmf_streq(newdevtab[j].oid, NULL) )
Expand Down Expand Up @@ -287,7 +287,7 @@ int main(int argc, char *argv[])
if ( result != 0 )
{
fatalx(EXIT_FAILURE,"=== DMF-Reindex: The generated document FAILED content verification (%d issues)\n\n", result);
// TODO: Can we pass this code to fatalx?
/* TODO: Can we pass this code to fatalx? */
return result;
}

Expand Down
8 changes: 4 additions & 4 deletions tools/nut-scanner/nutscan-snmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#ifndef DEVSCAN_SNMP_H
#define DEVSCAN_SNMP_H

#include <stddef.h> //define NULL
#include <stddef.h> /*define NULL */

typedef struct {
char * oid;
Expand Down Expand Up @@ -62,11 +62,11 @@ typedef struct {
# ifndef LIBNUTSCAN_SNMP_DMF
# ifdef DMF_SNMP_H
# define LIBNUTSCAN_SNMP_DMF
// Note: This requires types defined in "dmf.h"
// Variable implemented in scan_snmp.c
/* Note: This requires types defined in "dmf.h" */
/* Variable implemented in scan_snmp.c */
extern char *dmfnutscan_snmp_dir;
extern mibdmf_parser_t *dmfnutscan_snmp_dmp;
// Just reference this to NULLify when client quits and frees DMF stuff
/* Just reference this to NULLify when client quits and frees DMF stuff */
void uninit_snmp_device_table();
# endif /* DMF_SNMP_H already included */
# endif /* LIBNUTSCAN_SNMP_DMF */
Expand Down

0 comments on commit 27cbd4d

Please sign in to comment.