#!/usr/bin/env awk
#
# Returns all flows whose index appears in 'f' (int or hex)
#
# Parameters:
#   - [f]: the flow index(es) to test (comma or semicolon separated)
#          (ranges may also be specified using a dash (see examples below))
#
# Dependencies:
#   - basicFlow
#
# See Also:
#   - packet
#
# Examples:
#   - tawk 'flow(123)' file.txt          # test for flow 123
#   - tawk 'flow("123,456")' file.txt    # test for flow 123 or 456
#   - tawk 'flow("123;456")' file.txt    # test for flow 123 or 456
#   - tawk 'flow("123-456")' file.txt    # test for flow 123, 124, ... or 456
#   - tawk 'flow("1-3;5;7-9")' file.txt  # test for flow 1 to 3, 5, or 7 to 9
#   - tawk '{ print flow() }' file.txt   # print the flow index

@include "hdr"
@include "isnum"
@include "_validate_col"

function flow(f,        _i, _l, _flows, _rng, _uid) {
    _uid = _validate_col("flowInd", _my_flowInd)
    if (!f || (__PRIHDR__ && hdr())) return $_uid
    _uid = strtonum($_uid)
    if (isnum(f)) return (_uid == f)
    _l = split(f, _flows, /[,;]/)
    for (_i = 1; _i <= _l; _i++) {
        if (match(_flows[_i], /^(0x[[:xdigit:]]+|[0-9]+)-(0x[[:xdigit:]]+|[0-9]+)$/, _rng)) { # range
            if (_uid >= strtonum(_rng[1]) && _uid <= strtonum(_rng[2])) {
                return 1
            }
        } else if (_uid == _flows[_i]) {
            return 1
        }
    }
    return 0
}
