From 8ff1ed986dc8fe086c317bca8e6d3c5450e56da3 Mon Sep 17 00:00:00 2001 From: gdamore Date: Sun, 5 Jan 2025 02:33:35 +0000 Subject: deploy: f77e5a5ec7f8b1373eeda0ea56f47137daf40330 --- ref/api/args.html | 455 +++++++++++++++++++++++++++++++++++++++++++++++ ref/api/cmd_opts.html | 433 -------------------------------------------- ref/api/errors.html | 2 - ref/api/id_map.html | 4 +- ref/indexing.html | 8 +- ref/migrate/nanomsg.html | 2 +- ref/migrate/nng1.html | 7 +- ref/print.html | 129 ++++++++------ ref/proto/index.html | 4 +- ref/searchindex.js | 2 +- ref/searchindex.json | 2 +- ref/toc.html | 2 +- ref/toc.js | 2 +- 13 files changed, 551 insertions(+), 501 deletions(-) create mode 100644 ref/api/args.html delete mode 100644 ref/api/cmd_opts.html diff --git a/ref/api/args.html b/ref/api/args.html new file mode 100644 index 00000000..f2fb8c8d --- /dev/null +++ b/ref/api/args.html @@ -0,0 +1,455 @@ + + + + + + Arguments Parser - NNG Reference Manual (DRAFT) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + +
+ +
+ + + + + + + + +
+
+ +

Command Arguments

+

Some NNG utilities need to parse command line options, +and for this purpose a header library is supplied.

+

To make use of this, the header <nng/args.h> must be included.

+
+

+ + tip +

+

The functionality described here is entirely contained in the +nng/args.h header file, and may be used without previously +initializing the library with nng_init, and may even be used +in programs that are not linked against the NNG library.

+
+

Parse Command Line Arguments

+
typedef struct nng_arg_spec {
+    const char *a_name;  // Long style name (may be NULL for short only)
+    int         a_short; // Short option (no clustering!)
+    int         a_val;   // Value stored on a good parse (>0)
+    bool        a_arg;   // Option takes an argument if true
+} nng_optspec;
+
+#define NNG_ARG_END     (-1)
+#define NNG_ARG_INVAL   (-2)
+#define NNG_ARG_AMBIG   (-3)
+#define NNG_ARG_MISSING (-4)
+
+int nng_args_parse(int argc, char *const *argv,
+                   const nng_optspec *spec, int *val, char **arg, int *idx);
+
+

The nng_args_parse function is intended to facilitate parsing +command-line arguments. +This function exists largely to stand in for getopt from POSIX systems, +but it is available on all platforms, and it includes some capabilities missing from getopt.

+

The function parses arguments from +main1 +(using argc and argv), +starting at the index referenced by idx. +(New invocations typically set the value pointed to by idx to 1.)

+

Options are parsed as specified by spec (see Argument Specification.) +The value of the parsed option will be stored at the address indicated by +val, and the value of idx will be incremented to reflect the next +option to parse.

+
+

+ + tip +

+

For using this to parse command-line like strings that do not include +the command name itself, set the value referenced by idx to zero instead of one.

+
+

If the option had an argument, a pointer to that is returned at the address +referenced by arg.

+

This function should be called repeatedly, until it returns either -1 +(indicating the end of options is reached) or a non-zero error code is +returned.

+

This function may return the following errors:

+
    +
  • [NNG_ARG_AMBIGU]: Parsed option matches more than one specification.
  • +
  • [NNG_ARG_MISSING]: Option requires an argument, but one is not present.
  • +
  • [NNG_ARG_INVAL]: An invalid (unknown) argument is present in argv.
  • +
+

Option Specification

+

The calling program must first create an array of nng_arg_spec structures +describing the options to be supported. +This structure has the following members:

+
    +
  • +

    a_name:

    +

    The long style name for the option, such as “verbose”. +This will be parsed as a long option on the command line when it is prefixed with two dashes. +It may be NULL if only a short option is to be supported.

    +
  • +
  • +

    a_short:

    +

    This is a single letter (at present only ASCII letters are supported). +These options appear as just a single letter, and are prefixed with a single dash on the command line. +The use of a slash in lieu of the dash is not supported, in order to avoid confusion with path name arguments. +This value may be set to 0 if no short option is needed.

    +
  • +
  • +

    o_val:

    +

    This is a numeric value that is unique to this option. +This value is assigned by the application program, and must be non-zero for a valid option. +If this is zero, then it indicates the end of the specifications, and the +rest of this structure is ignored. +The value will be returned to the caller in val by nng_args_parse when +this option is parsed from the command line.

    +
  • +
  • +

    a_arg:

    +

    This value should be set to true if the option should take an argument.

    +
  • +
+

Long Options

+

Long options are parsed from the argv array, and are indicated when +the element being scanned starts with two dashes. +For example, the “verbose” option would be specified as --verbose on +the command line. +If a long option takes an argument, it can either immediately follow +the option as the next element in argv, or it can be appended to +the option, separated from the option by an equals sign (=) or a +colon (:).

+

Short Options

+

Short options appear by themselves in an argv element, prefixed by a dash (-). +If the short option takes an argument, it can either be appended in the +same element of argv, or may appear in the next argv element.

+
+

+ + note +

+

Option clustering, where multiple options can be crammed together in +a single argv element, is not supported by this function (yet).

+
+

Prefix Matching

+

When using long options, the parser will match if it is equal to a prefix +of the a_name member of a option specification, provided that it do so +unambiguously (meaning it must not match any other option specification.)

+

Example

+

The following program fragment demonstrates this function.

+
    enum { OPT_LOGFILE, OPT_VERBOSE };
+    char *logfile; // options to be set
+    bool verbose;
+
+    static nng_arg_spec specs[] = {
+        {
+            .a_name = "logfile",
+            .a_short = 'D',
+            .a_val = OPT_LOGFILE,
+            .a_arg = true,
+        }, {
+            .a_name = "verbose",
+            .a_short = 'V',
+            .a_val = OPT_VERBOSE,
+            .a_arg = false,
+        }, {
+            .a_val = 0; // Terminate array
+        }
+    };
+
+    for (int idx = 1;;) {
+        int rv, opt;
+        char *arg;
+        rv = nng_args_parse(argc, argv, specs, &opt, &arg, &idx);
+        if (rv != 0) {
+            break;
+        }
+        switch (opt) {
+        case OPT_LOGFILE:
+            logfile = arg;
+            break;
+        case OPT_VERBOSE:
+            verbose = true;
+            break;
+        }
+    }
+    if (rv != NNG_ARG_END) {
+        switch (rv) {
+        case NNG_ARG_AMBIG:
+            printf("Options error: ambiguous option\n");
+            break;
+        case NNG_ARG_MISSING:
+            printf("Options error: required option argument missing\n");
+            break;
+        case NNG_ARG_INVAL:
+            printf("Options error: unknown option present\n");
+            break;
+        }
+        exit(1);
+    }
+
+ + + + + +


+

1: Parsing argument strings from other sources can be done as well, +although usually then idx will be initialized to zero.

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + +
+ + diff --git a/ref/api/cmd_opts.html b/ref/api/cmd_opts.html deleted file mode 100644 index 20d5054f..00000000 --- a/ref/api/cmd_opts.html +++ /dev/null @@ -1,433 +0,0 @@ - - - - - - Command Options - NNG Reference Manual (DRAFT) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - -
- -
- - - - - - - - -
-
- -

Command Options

-

Some NNG utilities need to parse command line options, -and the supplementary function here allows applications that -need the same support to benefit from this.

-

To make use of this, the supplemental header <nng/supplemental/util/options.h> -must be included.

-

Parse Command Line Options

-
typedef struct nng_optspec {
-    const char *o_name;  // Long style name (may be NULL for short only)
-    int         o_short; // Short option (no clustering!)
-    int         o_val;   // Value stored on a good parse (>0)
-    bool        o_arg;   // Option takes an argument if true
-} nng_optspec;
-
-int nng_opts_parse(int argc, char *const *argv,
-                   const nng_optspec *spec, int *val, char **arg, int *idx);
-
-

The nng_opts_parse function is a intended to facilitate parsing -command-line arguments. -This function exists largely to stand in for getopt from POSIX systems, -but it is available everywhere that NNG is, and it includes -some capabilities missing from getopt.

-

The function parses arguments from -main1 -(using argc and argv), -starting at the index referenced by idx. -(New invocations typically set the value pointed to by idx to 1.)

-

Options are parsed as specified by spec (see Option Specification.) -The value of the parsed option will be stored at the address indicated by -val, and the value of idx will be incremented to reflect the next -option to parse.

-
-

- - tip -

-

For using this to parse command-line like strings that do not include -the command name itself, set the value referenced by idx to zero instead of one.

-
-

If the option had an argument, a pointer to that is returned at the address -referenced by arg.

-

This function should be called repeatedly, until it returns either -1 -(indicating the end of options is reached) or a non-zero error code is -returned.

-

This function may return the following errors:

-
    -
  • NNG_EAMBIGUOUS: Parsed option matches more than one specification.
  • -
  • NNG_ENOARG: Option requires an argument, but one is not present.
  • -
  • NNG_EINVAL: An invalid (unknown) argument is present.
  • -
-

Option Specification

-

The calling program must first create an array of nng_optspec structures -describing the options to be supported. -This structure has the following members:

-
    -
  • -

    o_name:

    -

    The long style name for the option, such as “verbose”. -This will be parsed as a long option on the command line when it is prefixed with two dashes. -It may be NULL if only a short option is to be supported.

    -
  • -
  • -

    o_short:

    -

    This is a single letter (at present only ASCII letters are supported). -These options appear as just a single letter, and are prefixed with a single dash on the command line. -The use of a slash in lieu of the dash is not supported, in order to avoid confusion with path name arguments. -This value may be set to 0 if no short option is needed.

    -
  • -
  • -

    o_val:

    -

    This is a numeric value that is unique to this option. -This value is assigned by the application program, and must be non-zero for a valid option. -If this is zero, then it indicates the end of the specifications, and the -rest of this structure is ignored. -The value will be returned to the caller in val by nng_opts_parse when -this option is parsed from the command line.

    -
  • -
  • -

    o_arg:

    -

    This value should be set to true if the option should take an argument.

    -
  • -
-

Long Options

-

Long options are parsed from the argv array, and are indicated when -the element being scanned starts with two dashes. -For example, the “verbose” option would be specified as --verbose on -the command line. -If a long option takes an argument, it can either immediately follow -the option as the next element in argv, or it can be appended to -the option, separated from the option by an equals sign (=) or a -colon (:).

-

Short Options

-

Short options appear by themselves in an argv element, prefixed by a dash (-). -If the short option takes an argument, it can either be appended in the -same element of argv, or may appear in the next argv element.

-
-

- - note -

-

Option clustering, where multiple options can be crammed together in -a single argv element, is not supported by this function (yet).

-
-

Prefix Matching

-

When using long options, the parser will match if it is equal to a prefix -of the o_name member of a option specification, provided that it do so -unambiguously (meaning it must not match any other option specification.)

-

Example

-

The following program fragment demonstrates this function.

-
    enum { OPT_LOGFILE, OPT_VERBOSE };
-    char *logfile; // options to be set
-    bool verbose;
-
-    static nng_optspec specs[] = {
-        {
-            .o_name = "logfile",
-            .o_short = 'D',
-            .o_val = OPT_LOGFILE,
-            .o_arg = true,
-        }, {
-            .o_name = "verbose",
-            .o_short = 'V',
-            .o_val = OPT_VERBOSE,
-            .o_arg = false,
-        }, {
-            .o_val = 0; // Terminate array
-        }
-    };
-
-    for (int idx = 1;;) {
-        int rv, opt;
-        char *arg;
-        rv = nng_opts_parse(argc, argv, specs, &opt, &arg, &idx);
-        if (rv != 0) {
-            break;
-        }
-        switch (opt) {
-        case OPT_LOGFILE:
-            logfile = arg;
-            break;
-        case OPT_VERBOSE:
-            verbose = true;
-            break;
-        }
-    }
-    if (rv != -1) {
-        printf("Options error: %s\n", nng_strerror(rv));
-        exit(1);
-    }
-
- - - - - -


-

1: Parsing argument strings from other sources can be done as well, -although usually then idx will be initialized to zero.

- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - -
- - diff --git a/ref/api/errors.html b/ref/api/errors.html index d72d0c33..7186db4c 100644 --- a/ref/api/errors.html +++ b/ref/api/errors.html @@ -277,8 +277,6 @@ as in diagnostic messages or log entries.

NNG_EWRITEONLY25Write only resource. A read operation failed because the object only supports writes. NNG_ECRYPTO26Cryptographic error. Usually indicates an invalid key was used for TLS. NNG_EPEERAUTH27Peer could not be authenticated. -NNG_ENOARG28Option requires argument. A command-line option was supplied without an argument. Only used with nng_opts_parse. -NNG_EAMBIGUOUS29Ambiguous option. The command line option could not be unambiguously resolved. Only used with nng_opts_parse. NNG_EBADTYPE30Incorrect type. A type-specific function was used for an object of the wrong type. NNG_ECONNSHUT31Connection shutdown. The connection was shut down and cannot be used. NNG_ESTOPPED1000Operation stopped. The operation was stopped with nng_aio_stop or [nng_aio_close]. diff --git a/ref/api/id_map.html b/ref/api/id_map.html index d180b156..1f87a173 100644 --- a/ref/api/id_map.html +++ b/ref/api/id_map.html @@ -353,7 +353,7 @@ Given a sufficiently large range, this is unlikely to be a concern.

- @@ -367,7 +367,7 @@ Given a sufficiently large range, this is unlikely to be a concern.

- diff --git a/ref/indexing.html b/ref/indexing.html index a1e3b04e..93fa6995 100644 --- a/ref/indexing.html +++ b/ref/indexing.html @@ -172,7 +172,7 @@ body, 1
BUS protocol, 1
callback, 1
clock, 1
-command-line arguments, 1
+command-line arguments, 1
concurrency, 1
concurrent, 1
condition variable, 1
@@ -184,7 +184,7 @@ error message, 1
event loop, 1
flow control, 1
gather, 1
-getopt, 1
+getopt, 1
half-duplex, 1
header, 1
idempotent, 1
@@ -224,6 +224,8 @@ named pipes, 1
nng_aio_stop, 1
nng_aio_wait, 1
nng_alloc, 1
+nng_arg_spec, 1
+nng_args_parse, 1
nng_bus0_open, 1
nng_bus0_open_raw, 1
nng_clock, 1
@@ -306,8 +308,6 @@ named pipes, 1
NNG_OPT_SOCKET_FD, 1
NNG_OPT_SUB_PREFNEW, 1
NNG_OPT_SURVEYOR_SURVEYTIME, 1
-nng_opts_parse, 1
-nng_optspec, 1
nng_pair0_open, 1
nng_pair0_open_raw, 1
nng_pair1_open, 1
diff --git a/ref/migrate/nanomsg.html b/ref/migrate/nanomsg.html index 6b285430..bed90b48 100644 --- a/ref/migrate/nanomsg.html +++ b/ref/migrate/nanomsg.html @@ -304,7 +304,7 @@ There are some exceptions. Be aware that the numeric values are not the
- + diff --git a/ref/migrate/nng1.html b/ref/migrate/nng1.html index 493c85c7..636a19b5 100644 --- a/ref/migrate/nng1.html +++ b/ref/migrate/nng1.html @@ -298,12 +298,13 @@ This was implemented mostly to aid legacy nanomsg applications, and it was both suboptimal in terms of performance.

Modern code should use one of nng_sendmsg, nng_recvmsg, nng_socket_send, or nng_socket_recv to get the maximum performance benefit. Working directly with nng_msg structures gives more control, reduces copies, and reduces allocation activity.

-

New AIO Error Code NNG_ESTOPPED

+

Error Code Changes

When an operation fails with NNG_ESTOPPED, it means that the associated [nni_aio] object has been permanently stopped and must not be reused. Applications must watch for this error code, and not resubmit an operation that returns it. This is particularly important for callbacks that automatically resubmit operations. Failure to observe this rule will lead to an infinite loop as any further operations on the object will fail immediately with NNG_ESTOPPED.

+

The error codes NNG_EAMBIGUOUS and NNG_ENOARG have been removed.

AIO Provider API changes

The API used for providers for asynchronous I/O operations has changed slightly.

- - @@ -4411,34 +4409,46 @@ Given a sufficiently large range, this is unlikely to be a concern.

} -

Command Options

-

Some NNG utilities need to parse command line options, -and the supplementary function here allows applications that -need the same support to benefit from this.

-

To make use of this, the supplemental header <nng/supplemental/util/options.h> -must be included.

-

Parse Command Line Options

-
typedef struct nng_optspec {
-    const char *o_name;  // Long style name (may be NULL for short only)
-    int         o_short; // Short option (no clustering!)
-    int         o_val;   // Value stored on a good parse (>0)
-    bool        o_arg;   // Option takes an argument if true
+

Command Arguments

+

Some NNG utilities need to parse command line options, +and for this purpose a header library is supplied.

+

To make use of this, the header <nng/args.h> must be included.

+
+

+ + tip +

+

The functionality described here is entirely contained in the +nng/args.h header file, and may be used without previously +initializing the library with nng_init, and may even be used +in programs that are not linked against the NNG library.

+
+

Parse Command Line Arguments

+
typedef struct nng_arg_spec {
+    const char *a_name;  // Long style name (may be NULL for short only)
+    int         a_short; // Short option (no clustering!)
+    int         a_val;   // Value stored on a good parse (>0)
+    bool        a_arg;   // Option takes an argument if true
 } nng_optspec;
 
-int nng_opts_parse(int argc, char *const *argv,
+#define NNG_ARG_END     (-1)
+#define NNG_ARG_INVAL   (-2)
+#define NNG_ARG_AMBIG   (-3)
+#define NNG_ARG_MISSING (-4)
+
+int nng_args_parse(int argc, char *const *argv,
                    const nng_optspec *spec, int *val, char **arg, int *idx);
 
-

The nng_opts_parse function is a intended to facilitate parsing +

The nng_args_parse function is intended to facilitate parsing command-line arguments. This function exists largely to stand in for getopt from POSIX systems, -but it is available everywhere that NNG is, and it includes -some capabilities missing from getopt.

+but it is available on all platforms, and it includes some capabilities missing from getopt.

The function parses arguments from -main1 +main1 (using argc and argv), starting at the index referenced by idx. (New invocations typically set the value pointed to by idx to 1.)

-

Options are parsed as specified by spec (see Option Specification.) +

Options are parsed as specified by spec (see Argument Specification.) The value of the parsed option will be stored at the address indicated by val, and the value of idx will be incremented to reflect the next option to parse.

@@ -4457,27 +4467,27 @@ referenced by arg.

returned.

This function may return the following errors:

    -
  • NNG_EAMBIGUOUS: Parsed option matches more than one specification.
  • -
  • NNG_ENOARG: Option requires an argument, but one is not present.
  • -
  • NNG_EINVAL: An invalid (unknown) argument is present.
  • +
  • [NNG_ARG_AMBIGU]: Parsed option matches more than one specification.
  • +
  • [NNG_ARG_MISSING]: Option requires an argument, but one is not present.
  • +
  • [NNG_ARG_INVAL]: An invalid (unknown) argument is present in argv.

Option Specification

-

The calling program must first create an array of nng_optspec structures +

The calling program must first create an array of nng_arg_spec structures describing the options to be supported. This structure has the following members:

@@ -4516,7 +4526,7 @@ a single argv element, is not supported by this function (yet).

Prefix Matching

When using long options, the parser will match if it is equal to a prefix -of the o_name member of a option specification, provided that it do so +of the a_name member of a option specification, provided that it do so unambiguously (meaning it must not match any other option specification.)

Example

The following program fragment demonstrates this function.

@@ -4524,26 +4534,26 @@ unambiguously (meaning it must not match any other option specification.)

char *logfile; // options to be set bool verbose; - static nng_optspec specs[] = { + static nng_arg_spec specs[] = { { - .o_name = "logfile", - .o_short = 'D', - .o_val = OPT_LOGFILE, - .o_arg = true, + .a_name = "logfile", + .a_short = 'D', + .a_val = OPT_LOGFILE, + .a_arg = true, }, { - .o_name = "verbose", - .o_short = 'V', - .o_val = OPT_VERBOSE, - .o_arg = false, + .a_name = "verbose", + .a_short = 'V', + .a_val = OPT_VERBOSE, + .a_arg = false, }, { - .o_val = 0; // Terminate array + .a_val = 0; // Terminate array } }; for (int idx = 1;;) { int rv, opt; char *arg; - rv = nng_opts_parse(argc, argv, specs, &opt, &arg, &idx); + rv = nng_args_parse(argc, argv, specs, &opt, &arg, &idx); if (rv != 0) { break; } @@ -4556,8 +4566,18 @@ unambiguously (meaning it must not match any other option specification.)

break; } } - if (rv != -1) { - printf("Options error: %s\n", nng_strerror(rv)); + if (rv != NNG_ARG_END) { + switch (rv) { + case NNG_ARG_AMBIG: + printf("Options error: ambiguous option\n"); + break; + case NNG_ARG_MISSING: + printf("Options error: required option argument missing\n"); + break; + case NNG_ARG_INVAL: + printf("Options error: unknown option present\n"); + break; + } exit(1); }
@@ -4567,7 +4587,7 @@ unambiguously (meaning it must not match any other option specification.)


-

1: Parsing argument strings from other sources can be done as well, +

1: Parsing argument strings from other sources can be done as well, although usually then idx will be initialized to zero.

Nanomsg ErrorNNG ErrorNotes
EINTRNNG_EINTR
ENOMEMNNG_ENOMEM
EINVALNNG_EINVAL, NNG_EADDRINVAL, NNG_EBADTYPE, NNG_EAMBIGUOUSNNG discrimates between different types of errors.
EINVALNNG_EINVAL, NNG_EADDRINVAL, NNG_EBADTYPENNG discrimates between different types of errors.
EBUSYNNG_EBUSY
ETIMEDOUTNNG_ETIMEDOUT
ECONNREFUSEDNNG_ECONNREFUSED
NNG_EWRITEONLY25Write only resource. A read operation failed because the object only supports writes.
NNG_ECRYPTO26Cryptographic error. Usually indicates an invalid key was used for TLS.
NNG_EPEERAUTH27Peer could not be authenticated.
NNG_ENOARG28Option requires argument. A command-line option was supplied without an argument. Only used with nng_opts_parse.
NNG_EAMBIGUOUS29Ambiguous option. The command line option could not be unambiguously resolved. Only used with nng_opts_parse.
NNG_EBADTYPE30Incorrect type. A type-specific function was used for an object of the wrong type.
NNG_ECONNSHUT31Connection shutdown. The connection was shut down and cannot be used.
NNG_ESTOPPED1000Operation stopped. The operation was stopped with nng_aio_stop or [nng_aio_close].