On a second thought, rename block_shutdown plugin to systemd_inhibit

- This is highly systemd-specific functionality so it makes sense to
  name the plugin in a way that makes it (more) obvious. No other
  changes besides renaming.
Make double-quoting work for special %doc (and %license) too

- Up to now, special %doc has been different from everything else in
  %files: double-quoting which is used to escape eg spaces in filenames
  has not worked, but single-quoting and backslash-escapes (which do
  not work elsewhere in %files) "worked" by happenstance due to getting
  passed verbatim to shell/cp. Those and various other %doc hacks people
  have come with stopped working (ticket #858) as starting from
  commit 29677605d44dc9cba3119135653ba0372ab58037 we perform the
  copies in slightly more controlled manner.
- Rather than re-enable old quirks, make %doc and %license behavior
  consistent with the rest of the %files section: double-quoting and
  globs work, other escaping methods do not.
- This does mean a minor (as docs with spaces are relatively rare)
  compatibility rift in specs, the "official workaround" is that
  if compatibility with older rpm versions is required globs can
  be used instead of quotation.
Adjust rpmspec -P to invoke non-build parse

In the query mode, rpmspecQuery puts the parser into non-build mode
by invoking rpmSpecParse with (RPMSPEC_ANYARCH|RPMSPEC_FORCE) flags.
The same flags should be applied in the parse mode, so that it is
possible to preprocess specfiles separately, i.e. without also
installing sources and patches.

Signed-off-by: Panu Matilainen <pmatilai@redhat.com>
Be more careful about skipping shared file/directory creation

- Only skip shared file/dir creation if its actually being created
  by another package. Previously we could've decided to skip an entry
  where the other file was also being skipped for some other reason.
- Ensure %ghost entries always have FA_SKIP on install, previously
  they often were FA_CREATE which makes no sense for %ghost. We dont
  encounter %ghosts at all during install in the FSM as they dont
  exist in the payload, but the file disposition calculations need
  to take them into account now that we're avoiding redundant creates.
- Fixes a regression from commit f7f5f88f9f3d6587e747b034ccb64a3f00ff4e1e
  which unearthed the %ghost FA_CREATE issue, reported here:
  http://lists.fedoraproject.org/pipermail/buildsys/2013-January/004047.html
Acommodate to glibc 2.17 __secure_getenv() renaming

- glibc 2.17 renames __secure_getenv() to secure_getenv() so now we
  need to test for both. Meh.
Split __scm_author to separate name and email address macros

- At least git wants name and email separately, change __scm_author
  to combine the two instead. Related to RhBug:905002.
Setup git identity in __scm_setup_git (RhBug:905002)

- For some reason on my own account git keeps functioning even if
  I remove its configuration, but on a newly created account git commit
  bails out with "Please tell me who you are" unless we set the
  (per-repo) configuration here.
Dont mess with global user identity in __scm_setup_bzr

- Bzr defaults to global value on "bzr whoami", so we would mess
  up user identity on "%autosetup -S bzr". Oops. Move identity setting
  after repo initialization and set the identity per-branch, not global.
  The notion of "per-branch identity" seems odd, but then bzr is...
  I guess this would be bzr-speak for "per-directory identity"
Add testcase for %ghost %config behavior

- Further parametrize configtest.spec to allow for different attributes
- Verify pre-existing %ghost %config is preserved on install,
  that it survives upgrades and gets removed on erase.
  This fails now because it IS broken due to commit
  a712252392eca75443ca45c10a72873cabec7963.
Add testcase for %ghost shared with a real file

- Further parametrize conflicttest.spec to allow for different attributes
- Verify that the real file gets created without conflicts, that
  it doesn't get removed when ghost is removed and gets normally removed
  on erase.
- This is the case commit a712252392eca75443ca45c10a72873cabec7963 was
  supposed to fix (which it does but with, ahem, "slight" side-effects)
Revert the severely broken FA_SKIP on %ghost "fix"

- Reverts the bad half of commit a712252392eca75443ca45c10a72873cabec7963:
  treating files that wont be installed as skipped seems logical enough,
  but rpm disagrees... making %ghosts FA_SKIP on install causes
  upgrades on %ghost %config files to miss the necessary FA_SKIP getting
  set on the erase half via handleInstInstalledFile() and
  rpmfiDecideFate(), causing %ghost %config files to be nuked. Which isn't
  very nice when your /var/lib/rpm/ contents are packaged as
  %ghost %config...
- This fixes the %ghost %config testcase but breaks the shared %ghost
  case in turn.
Dont skip shared file/dir create if the other one is a ghost

- Explicitly test for shared file/dir being ghost when deciding whether
  to skip create: we must never skip the creation of a real file/dir
  because of a ghost as the ghosts do not get created.
- This fixes the shared ghost testcase, ie the thing that commit
  a712252392eca75443ca45c10a72873cabec7963 was supposed to fix,
  without side-effects that I can see.
Adjust ghost config test case to match legacy behavior

- Rpm has traditionally (at least for the last decade) preserved
  existing %ghost %config file on erasure. Whether this was intentional
  or not, changing the behavior now risks data loss in case people
  are relying on this long-standing behavior.
- This fails now: The behavior changed as (an unexpected) side-effect
  of commit 389b1ab706be8eddba9f00c7084759f670ce96ac.
Restore legacy behavior on %ghost %config erasure, mostly

- Make rpmfiConfigConflict() consider any existing %ghost %config
  as "modified". This causes them to be left alone on erasure to match
  long-standing rpm behavior (whether the behavior is intended or not...)
  Ghost config testcase passes again, talk about flip-flop. Sigh.
- Legacy behavior had an extra twist which we are intentionally NOT
  restoring: if a non-empty %ghost exists at build-time, its digest
  is stored in the header despite the file not being present in
  the payload. With previous versions of rpm, the contents of an
  existing  %ghost %config would be compared to the one in header and
  resulting in different behavior on erasure: if the contents matched
  that of build-time, the file would be removed, otherwise it would
  be preserved. Ghosts remembering their identity from previous life
  goes a little bit too far into the land of paranormal for me...
Warn when whitespace is missing before macro body

This will now issue a warning when macro definition is possibly
incorrect or ambigous, such as the one found in FC18 lvm2.spec:

%define util-linux_version 2.22.1
warning: Macro %util needs whitespace before body
Stricter macro substitution syntax

This change introduces a separate routine to parse for valid macro
names.  Valid macro names are either regular 3+ character identifiers,
or special names: "S", "P", "0", "#", "*", "**", macro options such as
"-o" and "-o*", and macro arguments such as "1".  Other names are not
valid.  This fixes a number of bugs seen earlier due to sloppy name
parsing: "%_libdir*" and "%01" were not expanded (these are now expanded
to e.g. "/usr/lib64*" and "<name>1", as expected).  This also fixes
bugs in as-is substitution: "%!foo" was expanded to "%foo", and likewise
"%!!!" was expanded to "%" (and to "%<garbage>" at EOL).

Also, bad names in %name and %{name...} substitutions are now handled
differently.  In %name form, the name is parsed tentatively; a silent
fall-back to as-is substitution is provisioned when no valid name can
be obtain.  In %{name...} form, a failure to obtain a valid name is now
a syntax error.  Furthermore, only 3 variants are syntactically valid:
%{name} proper, %{name:...}, and %{name ...}.  This renders invalid
ambiguous macro substitutions such as the one found in FC18 lvm2.spec:

Requires: util-linux >= %{util-linux_version}
error: Invalid macro syntax: %{util-linux_version}
Warn when macro options evaporate silently

In the %name form, when the name is invalid or when the macro does
not exist, a silent fall-back is provisioned to as-is substitution.
On the contrary, the %-o form has %?name semantics, that is, implies
an existent test.  Hence at the top level, i.e. in specfile sections,
options always evaporate silently.  Given the ambiguity of the %-o form,
this deserves a warning.

For example, the following line found in FC18 xfce4-taskmanager.spec:
- Add patch to fix 0%-CPU bug
gets actually expanded to:
- Add patch to fix 0PU bug
This will now produce the warning:
warning: %-CPU parsed as %{-C}PU
Check for undefined macros

This change introduces a generalized routine rpmExpandMacros() to expand
macros on behalf of user input, with improved diagnostic facilities.
In particular, one of its arguments is a callback function which is
called whenever an undefined macro is encountered in user input.

When specfile parser calls this routine, it supplies a custom function
to handle undefined macros.  This function is keenly aware of specfile
syntax: valid specfile tokens, such as %prep and %setup, when found in
the right context, are not treated as undefined macros.  Otherwise,
there are 3 possibilities.
- Undefined macros in preamble and %pre/%post/... scriptlets raise
  an error, unless a non-build parse is performed; this is to prevent
  malformed packages from being built.
- In other cases, a warning is issued.
- Some tokens are permitted beyond their context (e.g. %ghost in
  %changelog), to comply with traditional/sloppy usage.  Also, undefined
  macros found in comments normally do not produce a warning.

Update:
- When checking for comments, allow leading whitespace.
- In copyNextLineFromOFI, eliminate rstrlcpy completely: instead of
  truncating the expanded output, simply replace spec->lbuf.
Improve macro table performance

In the existing implementation, when a new macro is added, the whole
table has to be sorted again.  Hence the cost of adding n macros is
worse than O(n^2), due to arithmetic progression.

This change drops all qsort(3) stuff altogether, by carefully preserving
table in sorted order.  In findEntry routine, bsearch(3) is replaced
with customized binary search which tracks position for insertion.
In the addMacro routine, if a matching entry is not found, this
position is used for direct insertion, after the rest of the elements
are "shifted to the right" with memmove(3).  Likewise, in delMacro
routine, the elements are shifted back to the left when the last macro
definition is popped.  Technically, shifting half of the array with
memmove(3) is still O(n^2); however, modern CPUs process contiguous
memory in a very efficient manner, and glibc provides a fine-tuned
memmove(3) implementation.

Also, macro table entries are now allocated in a single chunk.

This change reduces rpm startup costs by factor of 6 (see callgrind
annotations below).  Also, this change improves specfile parser
performance by a factor of 2 (e.g. the parse time of texlive.spec
is reduced from 67s to 35s).

Callgrind annotations for 'rpm --eval %_libdir', previous commit:
88,193,084  PROGRAM TOTALS
27,669,049  glibc-2.16-75f0d304/stdlib/msort.c:msort_with_tmp.part.0'2
24,880,649  glibc-2.16-75f0d304/string/../sysdeps/x86_64/strcmp.S:__GI_strcmp
11,111,276  rpmio/macro.c:compareMacroName
 7,048,036  glibc-2.16-75f0d304/string/../sysdeps/x86_64/memcpy.S:__GI_memcpy
 1,943,118  glibc-2.16-75f0d304/stdlib/msort.c:msort_with_tmp.part.0
 1,903,867  glibc-2.16-75f0d304/elf/dl-lookup.c:do_lookup_x
 1,701,781  rpmio/macro.c:rpmLoadMacroFile
 1,491,535  rpmio/macro.c:doDefine
 1,439,321  rpmio/macro.c:sortMacroTable
 1,254,448  glibc-2.16-75f0d304/malloc/malloc.c:_int_malloc
   683,782  glibc-2.16-75f0d304/malloc/malloc.c:_int_free
   482,748  glibc-2.16-75f0d304/elf/dl-lookup.c:_dl_lookup_symbol_x
   397,935  glibc-2.16-75f0d304/string/../sysdeps/x86_64/strcmp.S:strcmp'2
   307,673  glibc-2.16-75f0d304/elf/dl-misc.c:_dl_name_match_p
   306,247  rpm-4.10.2/lib/rpmrc.c:doReadRC
   300,710  glibc-2.16-75f0d304/string/../sysdeps/x86_64/strcpy.S:__GI_strcpy
   263,309  glibc-2.16-75f0d304/malloc/malloc.c:malloc
   254,657  rpm-4.10.2/lib/rpmrc.c:doReadRC'2
   240,893  glibc-2.16-75f0d304/string/../sysdeps/x86_64/strlen.S:__GI_strlen
...

Callgrind annotations for 'rpm --eval %_libdir', this commit:
14,813,520  PROGRAM TOTALS
1,903,161  glibc-2.16-75f0d304/elf/dl-lookup.c:do_lookup_x
1,701,781  rpmio/macro.c:rpmLoadMacroFile
1,491,535  rpmio/macro.c:doDefine
  932,161  glibc-2.16-75f0d304/malloc/malloc.c:_int_malloc
  900,004  glibc-2.16-75f0d304/string/../sysdeps/x86_64/strcmp.S:__GI_strcmp
  520,864  glibc-2.16-75f0d304/malloc/malloc.c:_int_free
  482,756  glibc-2.16-75f0d304/elf/dl-lookup.c:_dl_lookup_symbol_x
  397,574  glibc-2.16-75f0d304/string/../sysdeps/x86_64/strcmp.S:strcmp'2
  307,322  glibc-2.16-75f0d304/elf/dl-misc.c:_dl_name_match_p
  306,247  rpm-4.10.2/lib/rpmrc.c:doReadRC
  286,734  glibc-2.16-75f0d304/string/../sysdeps/x86_64/memcpy.S:__GI_memcpy
  254,657  rpm-4.10.2/lib/rpmrc.c:doReadRC'2
  240,179  glibc-2.16-75f0d304/string/../sysdeps/x86_64/strlen.S:__GI_strlen
...
Optimize argvCount function

Callgrind annotations for 'rpmspec -P anaconda.spec', previous commit:
380,151,860  PROGRAM TOTALS
232,943,797  glibc-2.16-75f0d304/string/../sysdeps/x86_64/strlen.S:__GI_strlen
 63,770,642  rpmio/argv.c:argvCount
 13,702,038  build/parseSpec.c:readLine
...

Callgrind annotations for 'rpmspec -P anaconda.spec', this commit:
364,226,458  PROGRAM TOTALS
232,943,797  glibc-2.16-75f0d304/string/../sysdeps/x86_64/strlen.S:__GI_strlen
 47,845,240  rpmio/argv.c:argvCount
 13,702,038  build/parseSpec.c:readLine
...