<!--  vim: set sw=2 sts=2 et ft=docbk:

  Part of the A-A-P recipe executive: Filetype detection

  Copyright (C) 2002-2003 Stichting NLnet Labs
  Permission to copy and use this file is specified in the file COPYING.
  If this file is missing you can find it here: http://www.a-a-p.org/COPYING

-->

<para>
The filetype detection module basically takes a file name and returns the type
of the file.
</para>

<para>
The A-A-P filetype detection is a separate module.
You can use the filetype detection in recipes, as a standalone program and
from any Python program.
</para>

<para>
A filetype name is made of lowercase ASCII letters and digits: a-z and 0-9.
</para>


<bridgehead>The Program</bridgehead>

<para>
Usage:
<literallayout>    <userinput>Filetype.py [-I ruledir] ...  [-f rulefile] ... filename </userinput> </literallayout>
</para>

<para>
This will print the filetype of "filename" on stdout.  When the type could not
be detected the result is the string "None".
</para>

<para>
The "-I ruledir" argument can be used to specify a directory to load *.afd
(Aap Filetype Detection) files from.  These add rules for filetype detection.
These are the default directories which are always scanned:
</para>
<literallayout>	/usr/local/share/aap/afd/
	~/.aap/afd/ </literallayout>

<para>
The "-f rulefile" argument can be used to specify a file to load rules from.
</para>


<bridgehead>Detection</bridgehead>

<para>
Detection is done in this order:
<orderedlist>
<listitem><para>
early Python items
</para></listitem>
<listitem><para>
check the file name extensions
</para></listitem>
<listitem><para>
match the regular expressions with the file name
</para></listitem>
<listitem><para>
check the first line in the file for a matching script name
</para></listitem>
<listitem><para>
later Python items
</para></listitem>
</orderedlist>
</para>

<para>
When on a non-Posix system, the file name is forced to be lower case, so that
case differences are ignored.  The rules must use lower case names for this
to work properly.  Rules with an upper case letter will only match on a Posix
system (this can be used for *.H to be recognized as cpp only on systems that
make a difference between *.h and *.H).
</para>


<bridgehead>The Python Module</bridgehead>

<para>
The "ft_detect" function can be called to detect the type of file "fname":
</para>

<programlisting>
        from Filetype import ft_detect
	type = ft_detect(fname)
</programlisting>

<para>
A string with the detected filetype is returned.  If the type is not
recognized, ft_detect() returns the None value.
</para>

<para>
To ignore extra suffixes like ".in", ".gz", add an extra non-zero argument:
</para>

<programlisting>
	type = ft_detect(fname, 1)
</programlisting>

<para>
To influence the messages given, add an extra "dict" argument.  The
"MESSAGE" item will be used, see its explanation in the main documenation.
</para>

<para>
For more info about the Filetype module, see the comments at the start of
Filetype.py.
</para>


<bridgehead>Format Of Filetype Detection Rules</bridgehead>

<para>
Blank lines and lines starting with "#" (preceded by any amount of white
space) are ignored.
</para>

<para>
These filetype detection lines are supported:
</para>

<variablelist>

  <varlistentry id='filetype-suffix'><term><cmdsynopsis>
    <command>suffix</command>
      <arg choice='plain'><replaceable>suffix</replaceable></arg>
      <arg choice='plain'><replaceable>type</replaceable></arg>
    </cmdsynopsis></term>
    <listitem>
      <para>
        Add detection of a filetype with a file name suffix.
        When a file name ends in .{suffix} it gets filetype
        {type}.  {suffix} is taken literally, it is not a
        regular expression.
        </para>
        <para>
        When {type} is "ignore" filetype detection is done on
        the file name with this suffix is removed.  For
        example, "suffix gz ignore" causes "foo.c.gz" to be
        handled like "foo.c".
        </para>
        <para>
        When {type} is "remove" a previously defined filetype
        detection for {suffix} is removed.  This can be used
        to remove a suffix rule and add another kind of
        detection instead.
        </para>
    </listitem>
  </varlistentry>

  <varlistentry id='filetype-regexp'><term><cmdsynopsis>
    <command>regexp</command>
      <arg choice='plain'><replaceable>regexp</replaceable></arg>
      <arg choice='plain'><replaceable>type</replaceable></arg>
      <arg>append</arg>
      <arg>tail</arg>
    </cmdsynopsis></term>
    <listitem>
      <para>
        Add detection of a filetype with a Python regular
        expression.  When {regexp} matches with the name of a
        file it gets filetype {type}.
        </para>
        <para>
        When "tail" is given, matching is done with the tail
        of the filename (without the path).
        </para>
        <para>
        When {type} is "remove" a previously defined filetype
        detection for {regexp} is removed.
        </para>
        <para>
        When "append" isn't given, the new detection is put
        before existing regexp detections, thus overruling
        them.  When "append" is used it is put after the
        existing regexp detections.
        </para>
    </listitem>
  </varlistentry>

  <varlistentry id='filetype-script'><term><cmdsynopsis>
    <command>script</command>
      <arg choice='plain'><replaceable>script</replaceable></arg>
      <arg choice='plain'><replaceable>type</replaceable></arg>
      <arg>append</arg>
    </cmdsynopsis></term>
    <listitem>
      <para>
        Add detection of a filetype by examining the first
        line of the file.  When it starts with "#!" and
        {script} matches with the script program name it gets
        filetype {type}.
        </para>
        <para>
        {script} is used as a Python regular expression.  It
        must match at the start of the program name.  Use
        ".*" to ignore a path.  End with "$" to match at the
        end of the program name
        </para>
        <para>
        When {type} is "remove" a previously defined filetype
        detection for {script} is removed.
        </para>
        <para>
        When "append" isn't given, the new detection is put
        before existing script detections.  When "append" is
        used the new detection is put after the existing
        script detections.
        </para>
    </listitem>
  </varlistentry>

  <varlistentry id='filetype-python'><term><cmdsynopsis>
    <command>python</command>
      <arg>after</arg>
      <arg>append</arg>
      <arg><replaceable>suffixlist</replaceable></arg>
    <command>&nbsp;</command>
      <arg choice='plain'><replaceable>python-code</replaceable></arg>
    </cmdsynopsis></term>
    <listitem>
      <para>
        Add detection of a filetype by executing Python code.
        When the optional "suffixlist" is specified the Python code is only
        executed when the file name matches a suffix in this
        comma separated list of suffixes.  This speeds up
        detection by only executing the Python code on
        relevant files.  For example, to only check *.bas and
        *.frm files:
        </para>
        <programlisting>
        python bas,frm
</programlisting>
        <para>
        The code is executed with these variables set:
        <informaltable frame='none'>
          <tgroup cols='2'>
            <colspec colwidth="150"/>
            <tbody>
              <row>
                <entry>fname</entry>
                <entry>the name of the file</entry>
                </row>
                <row>
                <entry>fname_base</entry>
                <entry>the last part of the path</entry>
              </row>
              <row>
                <entry>ignore</entry>
                <entry>1 if extra suffixes are to be ignored, 0
                otherwise</entry>
            </row>
            </tbody>
          </tgroup>
          </informaltable>
        </para>

        <para>
        When the code detects the filetype it must assing it
        to the variable "type".
        </para>
        <para>
        An IOError in the code is ignored.  Other errors are
        reported. Thus an open() call can be used without
        handling exceptions (when the file doesn't exist).
        </para>

        <para>
        When "after" isn't given, the detection is done before
        the suffix, regexp and script detection.  When
        "after" is given it's done last.
        </para>
        <para>
        When "append" isn't given, the new detection is put
        before existing python detections.  When "append" is
        used it is put after the existing python detections.
        The Python-code can use the ft_detect() function on a
        modified fname when needed.  Example:
        </para>
        <para>
        <programlisting>
        python after
            if ignore and fname[-1] == '~':
                type = ft_detect(fname[:-1], ignore)
</programlisting>
        </para>
        <para>
        This is actually one of the default rules.  When the
        file name ends in "~" detection is done on the name
        with this character removed.  This finds the type of
        backup files.
        </para>
    </listitem>
  </varlistentry>

  <varlistentry id="filetype-declare"><term><cmdsynopsis>
    <command>declare</command>
    <arg choice="plain"><replaceable>type</replaceable></arg>
    </cmdsynopsis></term>
    <listitem>
    <para>
    <!-- rather inconsistent use of {} - it looks like an attribute -->
    Declare {type} to be a recognized filetype.
    This is needed for filetypes that are recognized through
    Python code <emphasis>only</emphasis>.
    All other filetypes (those that appear in suffix,
    regexp, and script rules) need not be separately declared.
    </para>
    <para>
    When you use an unknown filetype in a recipe,
    &Aap; prints a warning to alert you to the possibility of
    a misspelling.
    The declare rule  is needed
    because &Aap; cannot tell what filetype the 
    Python code is capable of detecting,
    so the declare rule is used to tell &Aap; 
    specifically that the filetype {type} is a known and recognized type.
    </para>
    </listitem>
  </varlistentry>
</variablelist>

<para>
In the above the first argument can be put in quotes to include white space.
{type} can only consist of ASCII lowercase letters and digits.
</para>

