1
1
mirror of https://github.com/wader/fq.git synced 2024-08-18 00:10:29 +03:00

Compare commits

...

30 Commits

Author SHA1 Message Date
Zaraksh Rahman
e3ad574dec
Merge eae6a43d23 into 943743dbaf 2024-05-21 15:46:50 +02:00
Mattias Wadman
943743dbaf
Merge pull request #949 from wader/bump-make-golangci-lint-1.58.2
Update make-golangci-lint to 1.58.2 from 1.58.1
2024-05-20 22:16:07 +02:00
Mattias Wadman
1b85ae622b
Merge pull request #950 from wader/bump-github-golangci-lint-1.58.2
Update github-golangci-lint to 1.58.2 from 1.58.1
2024-05-20 22:16:01 +02:00
bump
f6d7235449 Update github-golangci-lint to 1.58.2 from 1.58.1
Release notes https://github.com/golangci/golangci-lint/releases/tag/v1.58.2
2024-05-20 16:04:14 +00:00
bump
aef47df26a Update make-golangci-lint to 1.58.2 from 1.58.1
Release notes https://github.com/golangci/golangci-lint/releases/tag/v1.58.2
2024-05-20 16:04:10 +00:00
Mattias Wadman
1ec9748046
Merge pull request #947 from wader/bump-make-golangci-lint-1.58.1
Update make-golangci-lint to 1.58.1 from 1.58.0
2024-05-09 18:12:49 +02:00
Mattias Wadman
2e5514fc50
Merge pull request #948 from wader/bump-github-golangci-lint-1.58.1
Update github-golangci-lint to 1.58.1 from 1.58.0
2024-05-09 18:12:44 +02:00
bump
a59ba2a2fa Update github-golangci-lint to 1.58.1 from 1.58.0
Release notes https://github.com/golangci/golangci-lint/releases/tag/v1.58.1
2024-05-09 16:03:55 +00:00
bump
7714fcf423 Update make-golangci-lint to 1.58.1 from 1.58.0
Release notes https://github.com/golangci/golangci-lint/releases/tag/v1.58.1
2024-05-09 16:03:52 +00:00
Mattias Wadman
544cf4cc09
Merge pull request #945 from wader/bump-docker-golang-1.22.3
Update docker-golang to 1.22.3 from 1.22.2
2024-05-09 00:01:16 +02:00
Mattias Wadman
25ad5f1c8f
Merge pull request #946 from wader/bump-github-go-version-1.22.3
Update github-go-version to 1.22.3 from 1.22.2
2024-05-09 00:00:41 +02:00
bump
9ff7da12a7 Update github-go-version to 1.22.3 from 1.22.2 2024-05-08 16:03:46 +00:00
bump
94cfbc670c Update docker-golang to 1.22.3 from 1.22.2 2024-05-08 16:03:44 +00:00
Mattias Wadman
163b3b609c
Merge pull request #944 from wader/bump-gomod-golang-x-net-0.25.0
Update gomod-golang-x-net to 0.25.0 from 0.24.0
2024-05-07 18:31:45 +02:00
bump
cabb67e8ab Update gomod-golang-x-net to 0.25.0 from 0.24.0
Tags https://github.com/golang/net/tags
2024-05-07 16:03:50 +00:00
Mattias Wadman
64df8bdbf5
Merge pull request #943 from wader/bump-gomod-golang-x-crypto-0.23.0
Update gomod-golang-x-crypto to 0.23.0 from 0.22.0
2024-05-06 19:16:07 +02:00
Mattias Wadman
481ac91880
Merge pull request #942 from wader/bump-gomod-ergochat-readline-0.1.1
Update gomod-ergochat-readline to 0.1.1 from 0.1.0
2024-05-06 19:15:51 +02:00
bump
14ada50806 Update gomod-golang-x-crypto to 0.23.0 from 0.22.0
Tags https://github.com/golang/crypto/tags
2024-05-06 16:04:45 +00:00
bump
12f332064c Update gomod-ergochat-readline to 0.1.1 from 0.1.0
Release notes https://github.com/ergochat/readline/releases/tag/v0.1.1
2024-05-06 16:04:43 +00:00
Mattias Wadman
0ff3e53c5f
Merge pull request #941 from wader/bump-gomod-golang-x-term-0.20.0
Update gomod-golang-x-term to 0.20.0 from 0.19.0
2024-05-05 18:16:15 +02:00
bump
586cf142e5 Update gomod-golang-x-term to 0.20.0 from 0.19.0
Tags https://github.com/golang/term/tags
2024-05-05 16:03:41 +00:00
Mattias Wadman
fb20db5eb7
Merge pull request #937 from wader/bump-make-golangci-lint-1.58.0
Update make-golangci-lint to 1.58.0 from 1.57.2
2024-05-04 18:15:24 +02:00
Mattias Wadman
48868bd4ee
Merge pull request #939 from wader/bump-github-golangci-lint-1.58.0
Update github-golangci-lint to 1.58.0 from 1.57.2
2024-05-04 18:12:55 +02:00
Mattias Wadman
c5c8b75c56
Merge pull request #938 from wader/bump-gomod-golang/text-0.15.0
Update gomod-golang/text to 0.15.0 from 0.14.0
2024-05-04 18:12:01 +02:00
bump
a5de74cd23 Update github-golangci-lint to 1.58.0 from 1.57.2
Release notes https://github.com/golangci/golangci-lint/releases/tag/v1.58.0
2024-05-04 16:03:51 +00:00
bump
42730d7586 Update gomod-golang/text to 0.15.0 from 0.14.0
Source diff 0.14.0..0.15.0 https://github.com/golang/text/compare/v0.14.0..v0.15.0
2024-05-04 16:03:49 +00:00
bump
3a683b64f8 Update make-golangci-lint to 1.58.0 from 1.57.2
Release notes https://github.com/golangci/golangci-lint/releases/tag/v1.58.0
2024-05-04 16:03:46 +00:00
Zaraksh Rahman
eae6a43d23
Add examples below synopsis 2023-09-08 16:57:16 +01:00
Zaraksh Rahman
16b89e87fe
Merge branch 'manpage' 2023-09-08 13:02:18 +01:00
Zaraksh Rahman
6a7bc1427d
Add fq(1) man page 2023-09-08 12:42:06 +01:00
7 changed files with 434 additions and 24 deletions

View File

@ -7,7 +7,7 @@ on:
pull_request:
env:
GOLANGCILINT_VERSION: "1.57.2"
GOLANGCILINT_VERSION: "1.58.2"
jobs:
lint:
@ -15,7 +15,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: "1.22.2"
go-version: "1.22.3"
- uses: actions/checkout@v3
- uses: golangci/golangci-lint-action@v3
with:
@ -47,7 +47,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "1.22.2"
go-version: "1.22.3"
- name: Test
env:
GOARCH: ${{ matrix.goarch }}

View File

@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "1.22.2"
go-version: "1.22.3"
- uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser

View File

@ -1,5 +1,5 @@
# bump: docker-golang /FROM golang:([\d.]+)/ docker:golang|^1
FROM golang:1.22.2-bookworm AS base
FROM golang:1.22.3-bookworm AS base
# expect is used to test cli
RUN \

View File

@ -61,7 +61,7 @@ gogenerate: always
lint: always
# bump: make-golangci-lint /golangci-lint@v([\d.]+)/ git:https://github.com/golangci/golangci-lint.git|^1
# bump: make-golangci-lint link "Release notes" https://github.com/golangci/golangci-lint/releases/tag/v$LATEST
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.2 run
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.58.2 run
depgraph.svg: always
go run github.com/kisielk/godepgraph@latest github.com/wader/fq | dot -Tsvg -o godepgraph.svg

410
doc/fq.1 Normal file
View File

@ -0,0 +1,410 @@
.TH FQ "1" "July 2023"
.SH NAME
fq \- jq for binary formats
.SH SYNOPSIS
\fBfq\fR [\fI\,OPTION\/\fR]... [--] \fI\,EXPR [\fI\,FILE\/\fR]...
\fBfq\fR . <FILE>
\fBfq\fR d <FILE>
\fBfq\fR -V '.path[1].value' <FILE>
\fBfq\fR tovalue <FILE>
\fBfq\fR -r to_toml file.yml
\fBfq\fR -s -d html 'map(.html.head.title?)' *.html
\fBfq\fR file.cbor | fq -d cbor torepr
\fBfq\fR 'grep("^main$") | parent' /bin/ls
\fBfq\fR -i
.SH DESCRIPTION
\fBfq\fR is inspired by the well known jq tool and language that allows you to work with binary formats the same way you would using jq. In addition it can present data like a hex viewer, transform, slice and concatenate binary data. It also supports nested formats and has an interactive REPL with auto-completion.
.P
It was originally designed to query, inspect and debug media codecs and containers like mp4, flac, mp3, jpeg. But since then it has been extended to support a variety of formats like executables, packet captures (with TCP reassembly) and serialization formats like JSON, YAML, XML, ASN1 BER, Avro, CBOR, protobuf. In addition it also has functions to work with URLs, convert to/from hex, number bases, search for things etc.
.P
In summary it aims to be jq, hexdump, dd and gdb for files combined into one.
.SH INVOKING FQ
.SS Command Line Arguments
Most of jq's CLI arguments work with fq. But there are some additional ones specific to fq:
.TP
\fB-d\fR, \fB--decode\fR \fI\,FORMAT\/\fR
Use \fI\,FORMAT\/\fR to decode instead of trying to auto-detect format.
.TP
\fB-i\fR, \fB--repl\fR [\fI\,INPUT\/\fR]...
Use interactive REPL.
Can be used with no input, one and multiple inputs, for example just \fBfq -i\fR starts a REPL with null input, \fBfq -i 123\fR with the number 123 as input, \fBfq -i . a b\fR with two files as input. This also works with \fB--slurp\fR. In the REPL it is also possible to start a sub-REPL(s) by ending a query with \fB<query> | repl\fR, use ctrl-D to exit the sub-REPL. The sub-REPL will evaluate separately on each output from the query it was started. Use \fB[<query>] | repl\fR if you want to "slurp" into an array.
.TP
\fB-o\fR, \fB--options\fR <\fIKEY\fR=\fIVALUE\fR|@\fIPATH\fR>
\fIKEY\fR is name of option
\fIVALUE\fR will be interpreted as a JSON value if possible otherwise a string, for example \fB-o name=abc\fR and \fB-o name='"abc"'\fR are equivalent.
@\fBPATH\fR will read string from file at \fBPATH\fR.
Specifying a global option or a format option, for example, \fB-o decode_samples=false\fR would, for some container decoders like mp4 and matroska, disable decoding of samples.
.TP
\fB-V\fR, \fB--value-output\fR
Output JSON value instead of decode tree. Use \fB-Vr\fR if you want raw string (no quotes).
.TP
\fB-M\fR
Disable coloured output.
.TP
\fB-o <NAME>=<VALUE>\fR
Set a decoding/encoding option. A list of supported options are -
.RS
.P
\fBbits_format\fR - string, md5, hex, base64, truncate, snippet
.RS
Controls the representation of raw binary in the output JSON
.RE
.RE
.SS Supported Formats
Use \fBfq --help formats\fR to get a list of supported formats
aac_frame, adts, adts_frame, aiff, amf0, apev2, apple_bookmark, ar, asn1_ber, av1_ccr, av1_frame, av1_obu, avc_annexb, avc_au, avc_dcr, avc_nalu, avc_pps, avc_sei, avc_sps, avi, avro_ocf, bencode, bitcoin_blkdat, bitcoin_block, bitcoin_script, bitcoin_transaction, bits, bplist, bsd_loopback_frame, bson, bytes, bzip2, cbor, csv, dns, dns_tcp, elf, ether8023_frame, exif, fairplay_spc, flac, flac_frame, flac_metadatablock, flac_metadatablocks, flac_picture, flac_streaminfo, gif, gzip, hevc_annexb, hevc_au, hevc_dcr, hevc_nalu, hevc_pps, hevc_sps, hevc_vps, html, icc_profile, icmp, icmpv6, id3v1, id3v11, id3v2, ipv4_packet, ipv6_packet, jpeg, json, jsonl, luajit, macho, macho_fat, markdown, matroska, mp3, mp3_frame, mp3_frame_vbri, mp3_frame_xing, mp4, mpeg_asc, mpeg_es, mpeg_pes, mpeg_pes_packet, mpeg_spu, mpeg_ts, msgpack, ogg, ogg_page, opus_packet, pcap, pcapng, pg_btree, pg_control, pg_heap, png, prores_frame, protobuf, protobuf_widevine, pssh_playready, rtmp, sll2_packet, sll_packet, tar, tcp_segment, tiff, tls, toml, tzif, udp_datagram, vorbis_comment, vorbis_packet, vp8_frame, vp9_cfm, vp9_frame, vpx_ccr, wasm, wav, webp, xml, yaml, zip
.TP
It can also work with some common text formats like URLs, hex, base64, PEM etc and for some serialization formats like XML, YAML, etc. it can transform both from and to jq values.
.TP
Use \fBfq\fR -h formats to get details about each supported file format
.SH CONFIGURATION
Custom functions can be defined in an init.fq which is searched for in the following locations -
.P
macOS - $HOME/Library/Application Support/fq/init.jq
.P
Linux, BSD, etc - $HOME/.config/fq/init.jq
.P
Windows - %AppData%\fq\init.jq
.SS Unicode output
Defining the environment variable \fICLIUNICODE\fR will enable the use of unicode characters for improved output
.SH THE FQ LANGUAGE
\fBfq\fR is based on the \fBjq\fR language and for basic usage its syntax is similar to how object and array access looks in JavaScript or JSON path, .food[10] etc. but it can do much more and is a very expressive language. To get the most out of fq it's recommended to learn more about jq, this manual assumes some familiarity with the jq language.
.SS Types Specific to fq
\fBfq\fR has two additional types compared to jq, \fIdecode value\fR and \fIbinary\fR. In standard jq expressions they will in most cases behave as some standard jq type.
.TP
.B Decode Value
This type is returned by decoders and it is used to represent parts of the decoded input. It can act as all standard jq types, object, array, number, string etc.
Each decode value has an associated bit range in the input which can be accessed as a binary using \fBtobits\fR and \fBtobytes\fR.
Each non-compound decode value has these properties:
.RS
An actual value - This is the decoded representation of the bits, a number, string, bool etc. Can be accessed using \fBtoactual\fR.
An optional symbolic value - Is usually a mapping of the actual to symbolic value, ex: map number to a string value. Can be accessed using \fBtosym\fR.
An optional description - Can be accessed using \fBtodescription\fR
\fBparent\fR is the parent decode value
\fBparents\fR is the all parent decode values
\fBtopath\fR is the jq path for the decode value
\fBtorepr\fR convert decode value to its representation if possible
The value of a decode value is the symbolic value if available and otherwise the actual value. To explicitly access the value use tovalue. In most expressions this is not needed as it will be done automatically.
.RE
.TP
.B Binary
Binaries are raw bits with a unit size, 1 (bits) or 8 (bytes), that can have a non-byte aligned size. Will act as byte padded strings in standard jq expressions.
Use \fBtobits\fR and \fBtobytes\fR to create them from \fIdecode values\fR, \fIstrings\fR, \fInumbers\fR or \fIbinary arrays\fR. \fBtobytes\fR will, if needed zero pad most significant bits to be byte aligned.
There is also \fBtobitsrange\fR and \fBtobytesrange\fR which does the same thing but will preserve its source range when displayed. \" TODO: tobytesrange, padding
.TP
.B Binary Array
Binary arrays are arrays of \fInumbers\fR, \fIstrings\fR, \fIbinaries\fR or other nested \fIbinary arrays\fR. When used as input to \fBtobits\fR/\fBtobytes\fR the following rules are used:
\fBNumber\fR is a byte with value be 0-255
\fBString\fR it's UTF8 codepoint bytes
\fBBinary\fR as is
\fBBinary array\fR used recursively
Binary arrays are similar to and inspired by Erlang iolist.
.SS Functions
All decode functions are available in two forms, just \fI<format>\fR (like mp3) that returns a decode value on error and \fIfrom_<format>\fR which throws error on decode error.
Note that jq sometimes uses the notation name/0, name/1 etc in error messages and documentation which means \fI<function-name>\fR/\fI<arity>\fR, Same function names with different arity are treated as separate functions, but are usually related in some way in practice.
.TP
.B
Functions Added in fq
All standard library functions from jq
A few new general functions:
.RS
.RS
\fBdisplay\fR/\fBd\fR displays a value
\fBprint\fR, \fBprintln\fR, \fBprinterr\fR, \fBprinterrln\fR prints to stdout and stderr.
\fBgroup\fR group values, same as \fBgroup_by(.)\fR.
\fBstreaks\fR, \fBstreaks_by(f)\fR like \fBgroup\fR but groups streaks based on condition.
\fBcount\fR, \fBcount_by(f)\fR like \fBgroup\fR but counts groups lengths.
\fBdebug(f)\fR like \fBdebug\fR but uses \fIarg\fR to produce a debug message.
\fBpath_to_expr\fR convert \fI["key", 1]\fR to \fI".key[1]\fR".
\fBexpr_to_path\fR from \fI".key[1]"\fR to \fI["key", 1]\fR.
\fBdiff($a; $b)\fR produce diff object between two values.
\fBdelta\fR, \fBdelta_by(f)\fR, array with difference between all consecutive pairs.
\fBchunk(f)\fR, split array or string into even chunks
.RE
\fBdisplay\fR is the main function for displaying values and is also the function that will be used if no other output function is explicitly used. If its input is a decode value it will output a dump and tree structure or otherwise it will output as JSON.
\" There are some examples with images in usage.md. TODO: Find a way to accomodate them in the man page
Bitwise functions \fBband\fR, \fBbor\fR, \fBbxor\fR, \fBbsl\fR, \fBbsr\fR and \fBbnot\fR. Works the same as jq math functions, unary uses input and if more than one argument all as arguments ignoring the input.
Some decode value specific functions:
.RS
\fBroot\fR tree root for value
\fBbuffer_root\fR root value of buffer for value
\fBformat_root\fR root value of format for value
\fBparent\fR parent value
\fBparents\fR output parents of value
\fBtopath\fR path of value. Use \fBpath_to_expr\fR to get a string representation.
\fBtovalue\fR, \fBtovalue($opts)\fR symbolic value if available otherwise actual value
\fBtoactual\fR, \fBtoactual($opts)\fR actual value (usually the decoded value)
\fBtosym\fR, \fBtosym($opts)\fR symbolic value (mapped etc)
\fBtodescription\fR description of value
\fBtorepr\fR converts decode value into what it represents. For example convert msgpack decode value into a value representing its JSON representation.
All regexp functions work with binary as input and pattern argument with these differences compared to when using string input:
.RS
All offset and length will be in bytes.
For \fBcapture\fR the \fB.string\fR value is a binary.
If pattern is a binary it will be matched literally and not as a regexp.
If pattern is a binary or flags include "b" each input byte will be read as separate code points
.RE
String functions are not overloaded to support binary for now as some of them might have behaviors that might be confusing.
\fBexplode\fR is overloaded to work with binary. Will explode into array of the unit of the binary. end of binary. instead of possibly multi-byte UTF-8 codepoints. This allows to match raw bytes. Ex: \fBmatch("\u00ff"; "b")\fR will match the byte 0xff and not the UTF-8 encoded codepoint for 255, \fBmatch("[^\u00ff]"; "b")\fR will match all non-0xff bytes.
\fBgrep\fR functions take 1 or 2 arguments. First is a scalar to match, where a string is treated as a regexp. A binary will match exact bytes. Second argument are regexp flags with addition that "b" will treat each byte in the input binary as a code point, this makes it possible to match exact bytes.
.RS
\fBgrep($v)\fR, \fBgrep($v; $flags)\fR recursively match value and binary
\fBvgrep($v)\fR, \fBvgrep($v; $flags)\fR recursively match value
\fBbgrep($v)\fR, \fBbgrep($v; $flags)\fR recursively match binary
\fBfgrep($v)\fR, \fBfgrep($v; $flags)\fR recursively match field name
\fBgrep_by(f)\fR recursively match using a filter. Ex: \fBgrep_by(. > 180 and . < 200)\fR equivalent to \fBfirst(grep_by(format == "id3v2"))\fR.
.RE
Binary:
.RS
\fBtobits\fR - Transform input to binary with bit as unit, does not preserve source range, will start at zero.
\fBtobitsrange\fR - Transform input to binary with bit as unit, preserves source range if possible.
\fBtobytes\fR - Transform input to binary with byte as unit, does not preserve source range, will start at zero.
\fBtobytesrange\fR - Transform input binary with byte as unit, preserves source range if possible.
\fB.[start:end]\fR, \fB.[:end]\fR, \fB.[start:]\fR - Slice binary from start to end preserve source range.
.RE
\fBopen\fR open file for reading
All decode functions take an optional option argument. The only option currently is \fBforce\fR to ignore decoder asserts. For example to decode as mp3 and ignore assets do \fBmp3({force: true})\fR or \fBdecode("mp3"; {force: true})\fR, from command line you currently have to do \fBfq -d bytes 'mp3({force: true})\fR' file.
\fBdecode\fR, \fBdecode("<format>")\fR, \fBdecode("<format>"; $opts)\fR decode format
\fBprobe\fR, \fBprobe($opts)\fR probe and decode format
\fBmp3\fR, \fBmp3($opts)\fR, ..., \fB<format>\fR, \fB<format>($opts)\fR same as \fBdecode("<format>")\fR, \fBdecode("<format>"; $opts)\fR decode as format and return decode value even on decode error.
\fBfrom_mp3\fR, \fBfrom_mp3($opts)\fR, ..., \fBfrom_<format>\fR, \fBfrom_<format>($opts)\fR same as \fBdecode("<format>")\fR, \fBdecode("<format>"; $opts)\fR decode as format but throw error on decode error.
Display shows hexdump/ASCII/tree for decode values and jq value for other types.
.RS
\fBd\fR/\fBd($opts)\fR display value and truncate long arrays and binaries
\fBda\fR/\fBda($opts)\fR display value and don't truncate arrays
\fBdd\fR/\fBdd($opts)\fR display value and don't truncate arrays or binaries
\fBdv\fR/\fBdv($opts)\fR verbosely display value and don't truncate arrays but truncate binaries
\fBddv\fR/\fBddv($opts)\fR verbosely display value and don't truncate arrays or binaries
.RE
\fBhd\fR/\fBhexdump\fR hexdump value
\fBrepl\fR/\fBrepl($opts)\fR nested REPL, must be last in a pipeline. \fB1 | repl\fR, can "slurp" outputs. Ex: \fB1, 2, 3 | repl, [1,2,3] | repl({compact: true})\fR.
\fBslurp("<name>")\fR slurp outputs and save them to \fBRname\fR, must be last in the pipeline. Will be available as a global array \fB$name\fR. Ex \fB1,2,3 | slurp("a"), $a[]\fR same as \fBspew("a")\fR.
\fBspew\fR/\fBspew("<name>")\fR output previously slurped values. \fBspew\fR outputs all slurps as an object, \fBspew("<name>")\fR outputs one slurp. Ex: \fBspew("a")\fR.
\fBpaste\fR read string from stdin until EOF. Useful for pasting text.
.RS
Ex: \fBpaste | from_pem | asn1_ber | repl\fR read from stdin then decode and start a new sub-REPL with result.
.RE
.RE
.RE
.B Naming Inconsistencies
jq's naming conversion is a bit inconsistent, some standard library functions are named tojson while others from_entries. fq follows this tradition a bit by but tries to use snake_case unless there is a good reason.
Here are all the non-snake_case functions added by \fBfq\fR. Most of them deal with decode and binary values which are new "primitive" types:
.RS
\fBtoactual\fR
\fBtobits\fR
\fBtobitsrange\fR
\fBtobytes\fR
\fBtobytesrange\fR
\fBtodescription\fR
\fBtopath\fR
\fBtorepr\fR
\fBtosym\fR
\fBtovalue\fR
.RE
.SH ENCODINGS, SERIALIZATION, & HASHES
In addition to binary formats \fBfq\fR also support reading to and from encodings and serialization formats.
At the moment \fBfq\fR does not have any dedicated argument for serialization formats but raw string input -R slurp -s and raw string output -r can make things easier. The combination -Rs will read all inputs into one string (same as jq).
Note that from* functions output \fBjq\fR values and to* takes \fBjq\fR values as input so in some cases not all information will be properly preserved. For example, for the element and attribute order might change and text and comment nodes might move or be merged. \fByq\fR might be a better tool if that is needed.
.SH USE AS SCRIPT INTERPRETER
\fBfq\fR can be used as a script interpreter using a shebang
.SH EXAMPLES
.TP
\fBfq\fR '.frames[1].header | tovalue' file.mp3
Get the second mp3 frame header as JSON
.TP
\fBfq\fR '.frames[0:10] | map(tobytesrange.start)' file.mp3
Get the byte start position for the first 10 mp3 frames in an array
.TP
\fBfq\fR -d bytes '.[100:] | mp3_frame | d' file.mp3
Decode byte range 100 to end as mp3_frame
.TP
\fBfq\fR '.somefield | tobytesrange[10:] | mp3_frame | d' file.mp3
decode byte range 10 bytes from .somefield and preserve relative position in file
.TP
\fBfq\fR 'def f: .. | select(format=="avc_sps"); diff(input|f; input|f)' a.mp4 b.mp4
Show AVC SPS difference between two mp4 files. \fB-n\fR tells \fBfq\fR to not have an implicit input, \fBf\fR is a function to select out some interesting value, call \fBdiff\fR with two arguments, decoded value for a.mp4 and b.mp4 filtered thru \fBf\fR.
.TP
\fBfq\fR 'first(.. | select(format=="jpeg")) | tobytes' file > file.jpeg
Recursively look for the first value that is a jpeg decode value root. Use tobytes to get bytes for value. Redirect bytes to a file.
.TP
\fBfq\fR '.. | select(.type=="stsz")? as $stsz | .entries | count | max_by(.[1])[1] as $m | ($stsz | topath | path_to_expr), (.[] | "\(.[0]): \((100*.[1]/$m)*"=") \(.[1])") | println' file.mp4
Recursively look for a all sample size boxes "stsz" and use ? to ignore errors when doing .type on arrays etc. Save reference to box, count unique values, save the max, output the path to the box and output a histogram scaled to 0-100.
.TP
\fBfq\fR '.tcp_connections | grep("GET /.* HTTP/1.?")' file.pcap
Find TCP streams that looks like HTTP GET requests in a PCAP file. Use \fBgrep\fR to recursively find strings matching a regexp.
.TP
\fBfq\fR -rn '[inputs | [input_filename, first(.chunks[] | select(.type=="IHDR") | .width)]] | max_by(.[1]) | .[0]' *.png
Find the widest PNG in a directory
.TP
\fBfq\fR '.. | select(scalars and in_bytes_range(0x123))' file
Which values include the bytes at position 0x123 \" TODO: Check if this is a typo
.SH BUGS (+ TRICKS)
\fBselect\fR fails with \fBexpected an ... but got: ...\fR
.RS
Try adding \fBselect(...)?\fR to catch and ignore type errors in the select expression.
.RE
Run interactive mode with no input
.RS
fq -i
.RE
Manual Decode - Sometimes fq fails to decode or you know there is valid data buried inside some binary or maybe you know the format of some gap field. Then you can decode manually:
.RS
# try decode a `mp3_frame` that failed to decode
$ fq -d mp3 '.gap0 | mp3_frame' file.mp3
# skip first 10 bytes then decode as `mp3_frame`
$ fq -d bytes '.[10:] | mp3_frame' file.mp3
.RE
Use . as input and in a positional argument
.RS
The expression \fB.a | f(.b)\fR might not work as expected. \fB.\fR is \fB.a\fR when evaluating the arguments so the positional argument will end up being \fB.a.b\fR. Instead do \fB. as $c | .a | f($c.b)\fR.
.RE
Building array is slow
.RS
Try to use \fBmap\fR or \fBforeach\fR to avoid rebuilding the whole array for each append.
.RE
Use print and println to produce more friendly compact output
repl argument using function or variable causes variable not defined
.RS
\fBtrue as $verbose | repl({verbose: $verbose})\fR will currently fail as repl is implemented by rewriting the query to \fBmap(true as $verbose | .) | repl({verbose: $verbose})\fR.
.RE
\fBerror\fR produces no output
.RS
\fBnull | error\fR behaves as empty
.RE
.SH AUTHOR
\fBfq\fR is written by Mattias Wadman
.SH SEE ALSO
\fBjq\fR - lightweight and flexible command-line JSON processor
\fByq\fR - lightweight and portable command-line YAML, JSON and XML processor

12
go.mod
View File

@ -19,7 +19,7 @@ require (
// bump: gomod-ergochat-readline /github\.com\/ergochat\/readline v(.*)/ https://github.com/ergochat/readline.git|*
// bump: gomod-ergochat-readline command go get -d github.com/ergochat/readline@v$LATEST && go mod tidy
// bump: gomod-ergochat-readline link "Release notes" https://github.com/ergochat/readline/releases/tag/v$LATEST
github.com/ergochat/readline v0.1.0
github.com/ergochat/readline v0.1.1
// bump: gomod-golang-snappy /github\.com\/golang\/snappy v(.*)/ https://github.com/golang/snappy.git|^0
// bump: gomod-golang-snappy command go get -d github.com/golang/snappy@v$LATEST && go mod tidy
@ -48,7 +48,7 @@ require (
// bump: gomod-golang-x-crypto /golang\.org\/x\/crypto v(.*)/ https://github.com/golang/crypto.git|^0
// bump: gomod-golang-x-crypto command go get -d golang.org/x/crypto@v$LATEST && go mod tidy
// bump: gomod-golang-x-crypto link "Tags" https://github.com/golang/crypto/tags
golang.org/x/crypto v0.22.0
golang.org/x/crypto v0.23.0
// has no tags
// go get -d golang.org/x/exp@master && go mod tidy
@ -57,17 +57,17 @@ require (
// bump: gomod-golang-x-net /golang\.org\/x\/net v(.*)/ https://github.com/golang/net.git|^0
// bump: gomod-golang-x-net command go get -d golang.org/x/net@v$LATEST && go mod tidy
// bump: gomod-golang-x-net link "Tags" https://github.com/golang/net/tags
golang.org/x/net v0.24.0
golang.org/x/net v0.25.0
// bump: gomod-golang-x-term /golang\.org\/x\/term v(.*)/ https://github.com/golang/term.git|^0
// bump: gomod-golang-x-term command go get -d golang.org/x/term@v$LATEST && go mod tidy
// bump: gomod-golang-x-term link "Tags" https://github.com/golang/term/tags
golang.org/x/term v0.19.0
golang.org/x/term v0.20.0
// bump: gomod-golang/text /golang\.org\/x\/text v(.*)/ https://github.com/golang/text.git|^0
// bump: gomod-golang/text command go get -d golang.org/x/text@v$LATEST && go mod tidy
// bump: gomod-golang/text link "Source diff $CURRENT..$LATEST" https://github.com/golang/text/compare/v$CURRENT..v$LATEST
golang.org/x/text v0.14.0
golang.org/x/text v0.15.0
// bump: gomod-gopkg.in/yaml.v3 /gopkg\.in\/yaml\.v3 v(.*)/ https://github.com/go-yaml/yaml.git|^3
// bump: gomod-gopkg.in/yaml.v3 command go get -d gopkg.in/yaml.v3@v$LATEST && go mod tidy
@ -79,6 +79,6 @@ require (
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)

24
go.sum
View File

@ -2,8 +2,8 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/creasty/defaults v1.7.0 h1:eNdqZvc5B509z18lD8yc212CAqJNvfT1Jq6L8WowdBA=
github.com/creasty/defaults v1.7.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM=
github.com/ergochat/readline v0.1.0 h1:KEIiAnyH9qGZB4K8oq5mgDcExlEKwmZDcyyocgJiABc=
github.com/ergochat/readline v0.1.0/go.mod h1:o3ux9QLHLm77bq7hDB21UTm6HlV2++IPDMfIfKDuOgY=
github.com/ergochat/readline v0.1.1 h1:C8Uuo3ybB23GWOt0uxmHbGzKM9owmtXary6Clrj84s0=
github.com/ergochat/readline v0.1.1/go.mod h1:o3ux9QLHLm77bq7hDB21UTm6HlV2++IPDMfIfKDuOgY=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomarkdown/markdown v0.0.0-20240328165702-4d01890c35c0 h1:4gjrh/PN2MuWCCElk8/I4OCKRKWCCo2zEct3VKCbibU=
@ -25,18 +25,18 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/wader/gojq v0.12.1-0.20240401131232-6c6bc364201a h1:P881Oecjt9FEXrwkGJ6UObJksxejJaF/fKq1ZfXpiVE=
github.com/wader/gojq v0.12.1-0.20240401131232-6c6bc364201a/go.mod h1:qVrzkUdnBtJvM4twyRQ6xdziPSnSp35dLm4s/DN2iP4=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=