Command Line Functions
Prev | Chapter 7. Support Function Reference | Next

## Command Line Functions

Command line functions are used by all toolkit programs. They are generic functions but we include them in a special section because they collectively co-operate to convert command line options and arguments into appropriate binary representations and provide concise but meaningful feedback when that is not possible.

### assist

`void assist(` | name, |
| type, |
| list, |
| size, |
| fp`)`; |

`char const * name`;
`char const * type`;
`const struct _code_ list []`;
`size_t size`;
`FILE * fp`;

Show why a symbolic `name` was rejected by function lookup or similar functions. Argument `type` contains a title for the class of names stored in `list`. This function prints an error message on file stream `fp` showing the program name, the `type` string, the symbolic `name` and all names stored in the `list`. The `_code_` structure is declared in types.h. The function is declared in symbol.h and defined in assist.c. Function expect is used to print list names.

### basespec

`uint64_t basespec(` | string, |
| base, |
| size`)`; |

`char const * string`;
`unsigned base`;
`unsigned size`;

Return the unsigned integer equivalent of a numeric `string`. Print an error message on `stderr` and exit the program with status `1` if a syntax error occurs or the result exceeds the capacity of the requested integer `size` in bytes. If `base` is `0`, numeric values may be expressed in decimal, hexadecimal or binary notation where hexadecimal values start with `"0x"` and binary values start with `"0b"`. When `base` is non-zero, the notation in `string` must conform to the corresponding number base rules. Applications should cast the return value to the appropriate data type prevent loss-of-data compiler warnings. This function is typically used to convert and length-check integers entered as command line arguments. The function is declared in number.h and defined in basespec.c.

Like function uintspec, this function both converts and range checks numeric string values, but the minimum and maximum value are implicit in the `size` of the integer. The minimum value is always `0` and the maximum value can be computed by `((1 << size << 3) - 1)`.

### bytespec

`void bytespec(` | string, |
| memory, |
| extent`)`; |

`char const * string`;
`void * memory`;
`size_t extent`;

Encode a `memory` region with the binary equivalent of a fixed-length hexadecimal string. Print an error message on stderr and exit the program with status `1` if a syntax error occurs or the number of octets does not equal `extent`. Hexadecimal octets may be separated by colons for readability but colons are not required. Empty octets are illegal. The function is declared in memory.h and defined in bytespec.c.

This function is typically used to enter fixed-length data, like hardware addresses and encryption keys, on the command line.

### checkfilename

`bool checkfilename(` | filename`)`; |

`char const * filename`;

Return logical `true` if the `filename` argument contains only letters, digits, slashes, periods, underscores and hyphens. This function can be used to detect cases where a user accidentally entered an Ethernet address in place of a filename on the command line. Ethernet address strings are, as it happens, also valid filenames. The function is declared in files.h and defined in checkfilename.c.

### dataspec

`void dataspec(` | string, |
| memory, |
| extent`)`; |

`char const * string`;
`void * memory`;
`size_t extent`;

Encode a `memory` region with the binary equivalent of a variable-length hexadecimal string. Print an error message on stderr and exit the program with the status `1` if a syntax error occurs or the number of octets exceeds `extent`. The number of octets may, however, be less than `extent`. Unlike function bytespec, hexadecimal octets may not be separated by colons. This function is typically used to enter variable-length data from the command line. The function is declared in memory.h and defined in dataspec.c.

### error

`void error(` | exitcode, |
| number, |
| format, |
| ...`)`; |

`intexitcode`;
`errno_t number`;
`char const * format`;
`...`;

This function works like printf except that printed messages appear on `stderr` and are prefixed with the program name and error information. If argument `errno` is non-zero then messages are prefixed with the system error description. If argument `exitcode` is non-zero then function `error` exits the program with value `exitcode` after printing the message and does not return to the caller. The function is declared in error.h and defined in error.c.

This function is used to print informative error messages on the console and prevent program execution from proceeding when input is invalid or some error condition exists.

### expect

`void expect(` | list, |
| size, |
| fp`)`; |

`const struct _code_ list []`;
`size_t size`;
`FILE * fp`;

Display all names in argument `list` on file stream `fp`. This function is called by runction assist to print the list of symbolic names but other function may have use for it. The `_code_` structure is declared in types.h. The function is declared in symbol.h and defined in expect.c.

### getoptv

`int getoptv(` | argc, |
| argv, |
| optv`)`; |

`int argc`;
`char const * argv []`;
`char const * optv []`;

A custom version of the POSIX function `getopt`. It supports standard global variables `optind`, `opterr`, `optopt` and `optarg` and the non-standard variable `optmin`. It extracts the program name from `argv[0]` and sets global string pointer `program_name` for use by functions `alert`, `error` and others. Options `-?` and `--help` both display program information on `stdout`. Options `-!` and `--version` both display program version information on `stdout`. String vector `optv` includes both the standard argument `optstring` and usage text found in many programs. The function is declared in getoptv.h and defined in getoptv.c.

### ipv4spec

`size_t ipv4spec(` | string, |
| memory`)`; |

`char const * string`;
`void * memory`;

Encode a 4-byte `memory` region with an IPv4 dotted-decimal `string` and return the number of bytes encoded. Terminate the program with an error message and exitcode of `1` on conversion error. The value returned by this function is always `4` and memory is always encoded in network byte order. This function is typically used to convert IPv4 strings entered as command line arguments. The function is declared in memory.h and defined in ipv4spec.c.

Dotted-decimal format consists of decimal values in the range 0 through 255. Each value represents one octet or 8-bit value. IPv4 addresses require 4 such values separated by one decimal point. This function permits empty octets and leading zeros within octets. For example, “...” is equivalent to “0.0.0.0” and “127.0.000.001” is equivalent to “127.0.0.1”. The second example will encode memory as follows `{ 0x7F, 0x00, 0x00, 0x01 }` which is in network byte order, or big endian.

### ipv6spec

`size_t ipv6spec(` | string, |
| memory`)`; |

`char const * string`;
`void * memory`;

Encode a 16-byte `memory` region with an IPv6 colon-separated hexadecimal quartet `string` and return the number of bytes encoded. Terminate the program with an error message and exitcode of `1` on conversion error. The value returned by this function is always `16` and memory is always encoded in network byte order. This function is typically used to convert IPv6 strings entered as command line arguments. The function is declared in memory.h and defined in ipv6spec.c.

Colon-separated hexadecimal quartet notation consists of hexadecimal values in the range 0 through FFFF. Each value represents a quartet or a 32-bit value. IPv6 addresses require 8 quartets separated by one colon. By convention, an empty quartet expands with enough zeros to right-justify the remainder of the address. This function permits multiple empty quartets and leading zeros within quartets. When multiple empty quartets appear, only the right-most occurance expands to zeros. For example, “AA12::BB34::CC56::DD78” is equivalent to “AA12:0000:BB34:0000:CC56:0000:0000:DD78” because only the right-most empty field expands. This will encode memory as follows `{ 0xAA, 0x12, 0x00, 0x00, 0xBB, 0x34, 0x00, 0x00, 0xCC, 0x56, 0x00, 0x00, 0x00, 0x00, 0xDD, 0x78 }` which is in network byte order, or big-endian.

### lookup

`signed lookup(` | name, |
| list, |
| size`)`; |

`char const * name`;
`const struct _code_ list []`;
`size_t size`;

Lookup a symbolic `name` in a `list` and return an associated integer or `-1` if the `name` does not appear in the `list`. A typical use is the translation of symbolic command line arguments to integer codes. For example, some Toolkit programs assign symbolic names to field codes so that users can enter names instead of numbers. This approach becomes more useful as the number of permitted codes increases. The `_code_` structure is declared in types.h. The function is declared in symbol.h and defined in lookup.c.

This function offers two benefits: 1) multiple symbols can translate to one code and 2) names can be changed or new names added without affecting program logic. This function is similar to but different from function synonym which returns a character string instead of an integer.

### putoptv

`void putoptv(` | optv`)`; |

`char const * optv []`;

Print program information on `stdout`. Program information is stored in string vector `optv`. String indexes are defined in file `putoptv.h`. String `optv[0]` is the POSIX standard argument `optstring`. This function is called by function `getoptv` whenever option `-?` or `--help` is detected on the command line. There is virtually no need to call this function directly. The function is declared in putoptv.h and defined in putoptv.c.

### synonym

`char const * synonym(` | string, |
| list, |
| size`)`; |

`char const * string`;
`const struct _term_ list []`;
`size_t size`;

Lookup a symbolic `name` in a `list` and return an associated string or the original `string` if the `name` does not appear in the `list`. A typical use is the translation of symbolic command line arguments to their equivalent numeric strings before encoding them. For example, many Toolkit programs convert the command line argument “local” to “00:B0:52:00:00:01” before encoding the device MAC address. The `_term_` structure is declared in types.h. The function is declared in symbol.h and defined in synonym.c.

This function is similar to but different from function lookup which returns an integer instead of a character string.

### uintspec

`uint64_t uintspec(` | string, |
| minimum, |
| maximum`)`; |

`char const * string`;
`uint64_t minimum`;
`uint64_t maximum`;

Return the unsigned integer equivalent of a numeric `string`. Print an error message on `stderr` and exit the program with the value `1` when a syntax error occurs or the result exceeds the specified `minimum` or `maximum` value. Numeric values may be expressed in decimal, hexadecimal or binary notation where hexadecimal values start with `"0x"` and binary values start with `"0b"`. Applications should cast the return value to the appropriate data type to avoid loss-of-data warnings on some compilers. This function is typically used to convert and range-check integer values entered as command-line arguments. The function is declared in number.h and defined in uintspec.c.

### version

`void version(` | `)`; |

Print package and program version information on `stdout`. This function is called by function `getoptv` whenever option `-!` or `--version` is detected on the command line. There is no need to call this function directly. The function is declared in version.h and defined in version.c. Constants `PACKAGE` and `VERSION` define the information that is displayed. They are defined in file version.h and must be maintained by developers.

Prev | Up | Next
Chapter 7. Support Function Reference | Home | Ethernet Functions