Debian Codegraph @

src/client/confmodule.in

raw
1 #!/bin/sh
2 # This is a shell library to interface to the Debian configuration management
3 # system.
4
5 ###############################################################################
6 # Initialization.
7
8 _DEBCONF_IMPL=
9 if [ "${DEBCONF_USE_CDEBCONF:-}" ]; then
10 # Use cdebconf if explicitly requested.
11 _DEBCONF_IMPL=cdebconf
12 elif [ -x /usr/share/debconf/frontend ] && \
13 [ ! -h /usr/share/debconf/frontend ]; then
14 # Fall back to debconf, but only if installed. (cdebconf currently
15 # installs /usr/share/debconf/frontend as a symlink to
16 # /usr/lib/cdebconf/debconf.)
17 _DEBCONF_IMPL=debconf
18 else
19 # Last resort: use cdebconf although not explicitly requested.
20 _DEBCONF_IMPL=cdebconf
21 fi
22
23 # Check to see if a FrontEnd is running.
24 if [ ! "${DEBIAN_HAS_FRONTEND:-}" ]; then
25 # Since there is no FrontEnd, this program execs a FrontEnd.
26 # It will then run a new copy of $0 that can talk to it.
27 if [ "$_DEBCONF_IMPL" = cdebconf ]; then
28 exec /usr/lib/cdebconf/debconf -- $0 "$@"
29 else
30 export PERL_DL_NONLAZY=1
31 exec /usr/share/debconf/frontend $0 "$@"
32 fi
33 fi
34
35 # Only do this once.
36 if [ -z "${DEBCONF_REDIR:-}" ]; then
37 # Redirect standard output to standard error. This prevents common
38 # mistakes by making all the output of the postinst or whatever
39 # script is using this library not be parsed as confmodule commands.
40 #
41 # To actually send something to standard output, send it to fd 3.
42 exec 3>&1
43 if [ "$_DEBCONF_IMPL" = cdebconf ]; then
44 exec 1>&5
45 else
46 exec 1>&2
47 fi
48 DEBCONF_REDIR=1
49 export DEBCONF_REDIR
50 fi
51
52 # The original stdin/stdout/stderr fds are duplicated onto fds
53 # DEBCONF_OLD_FD_BASE to DEBCONF_OLD_FD_BASE + 2. These values are exported
54 # so that you can get the original fds back. This is kind of a hack:
55 # ideally, we might use something like a socket for the debconf protocol
56 # instead of stdio.
57 DEBCONF_OLD_FD_BASE='@DEBCONF_OLD_FD_BASE@'
58 export DEBCONF_OLD_FD_BASE
59
60 ###############################################################################
61 # Commands.
62
63 _db_cmd () {
64 local _db_internal_IFS="$IFS"
65 IFS=' '
66 printf '%s\n' "$*" >&3
67 # Set to newline to get whole line.
68 IFS='
69 '
70 read -r _db_internal_line
71 IFS="$_db_internal_IFS"
72
73 # Disgusting, but it's the only good way to split the line,
74 # preserving all other whitespace.
75 RET="${_db_internal_line#[! ][ ]}"
76 local _db_status="${_db_internal_line%%[ ]*}"
77 case "$_db_status" in
78 1) # escaped data
79 if [ "$_DEBCONF_IMPL" = cdebconf ]; then
80 _DEBCONF_ESCAPE=/usr/lib/cdebconf/debconf-escape
81 else
82 _DEBCONF_ESCAPE=debconf-escape
83 fi
84 RET="$(printf '%s' "$RET" | $_DEBCONF_ESCAPE -u)"
85 return 0
86 ;;
87 esac
88 return "$_db_status"
89 }
90
91 REPEAT: db_@command@ () { _db_cmd "@COMMAND@" "$@"; }
92
93 # An old alias for input.
94 db_text () {
95 db_input "$@"
96 }
97
98 # Cannot read a return code, since there is none and it would block.
99 db_stop () {
100 echo STOP >&3
101 }