aboutsummaryrefslogtreecommitdiff
path: root/libbutl/manifest-serializer.bash.in
blob: 06770ba2c3dd3e71397803927b2fbd568dcfaf87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# file      : libbutl/manifest-serializer.bash.in
# copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
# license   : MIT; see accompanying LICENSE file

if [ "$butl_manifest_serializer" ]; then
  return 0
else
  butl_manifest_serializer=true
fi

@import libbutl/utility@

# Serialize the manifest reading the binary representation from stdin and
# writing to stdout. Unless --long-lines is specified, break lines in values.
#
# Normally you would use the start/finish functions below.
#
function butl_serialize_manifest () # [--long-lines]
{
  "$(butl_path)/manifest" "$@" serialize
}

# Start the manifest serialization co-process setting the following "return"
# variables:
#
#   butl_manifest_serializer_ifd
#   butl_manifest_serializer_ofd
#   butl_manifest_serializer_pid
#
# If <file> is not specified, then write to stdout. Unless --long-lines is
# specified, break lines in values.
#
# The typical usage:
#
#   butl_manifest_serializer_start
#
#   fd="$butl_manifest_serializer_ifd"
#
#   printf ":1\0"             >&"$fd"
#   printf "name:foo\0"       >&"$fd"
#   printf "version:1.2.3\0"  >&"$fd"
#
#   butl_manifest_serializer_finish
#
function butl_manifest_serializer_start () # [--long-lines] [<file>]
{
  local ops=()
  while [ $# -gt 0 ]; do
    case "$1" in
      --long-lines)
        ops+=("$1")
        shift
        ;;
      *)
        break
        ;;
    esac
  done

  if [ "$#" -gt 0 ]; then
    exec {butl_manifest_serializer_ofd}>"$1"
  else
    exec {butl_manifest_serializer_ofd}>&1
  fi

  # See notes in butl_manifest_parser_start() on bash co-process issues.
  #
  coproc { butl_serialize_manifest "${ops[@]}"; } >&"$butl_manifest_serializer_ofd"
  butl_manifest_serializer_ifd="${COPROC[1]}"
  butl_manifest_serializer_pid="$COPROC_PID"
}

# Finish the manifest serialization co-process.
#
function butl_manifest_serializer_finish ()
{
  exec {butl_manifest_serializer_ifd}<&-
  wait "$butl_manifest_serializer_pid"
  exec {butl_manifest_serializer_ofd}<&-
}