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

  Part of the A-A-P recipe executive: Filetypes and actions

  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>
  See <xref linkend="tutor-actions"/> for a simple example how to define a new
  filetype.
</para>

<bridgehead>Filetype Detection</bridgehead>

<para>
A-A-P detects the type of a file automatically.  This is used to decide
what tools can be used for a certain file.
</para>

<para>
  The detection often uses the name of the file, especially the suffix.
  Extra suffixes like ".in" and ".gz" are ignored.  Sometimes the contents of
  the file is inspected.  for example, on Unix a shell script is recognized
  by "#!/bin/sh" in the first line.
</para>

<para>
To manually set the file type of an item add the "filetype" attribute.  This
overrules the automatic detection.
Example:
</para>
<programlisting>
        foo.o : foo.x {filetype = cpp}
</programlisting>

<para>
Most detection is already built in.  If this is not sufficient for your work,
filetype detection instructions can be used to change the way file type
detection works.  These instructions can either be in a file or directly in
the recipe:
</para>

<programlisting>
        :filetype  filename
        :filetype
            suffix p pascal
            script .*bash$ sh
</programlisting>
        
<para>
The advantage of using a file is that it will also be possible to use it when
running the filetype detection as a separate program.  The advantage of using
the instructions directly in the recipe is that you don't have to create
another file.
</para>

<para>
For the syntax of the file see <xref linkend="ref-filetype"/>.
</para>

<para>
It is sometimes desired to handle a group of files in a different way.  For
example, to use a different optimizer setting when compiling C files.  An easy
way to do this is to give these files a different filetype.  Then define a
compile action specifically for this filetype.  Example:
</para>

<programlisting>
        :attr {filetype = c_opt} bar.c foo.c
        :action compile c_opt
            OPTIMIZE = 3
            :do compile $source
</programlisting>

<!-- <note> -->
<para>
<!-- <indexterm><primary>:filetype</primary><secondary>declare</secondary></indexterm> -->
When you define an action (or a route), &Aap; checks
that the filetypes you use are known filetypes, i.e.&nbsp;mentioned
somewhere in a <link linkend="cmd-filetype">:filetype</link>
command. If you just make up filetypes and use them in actions,
&Aap; will give you a warning.
This helps detect misspellings and the like.
However, for the "optimized C" filetype above,
this leads to a warning where you do not want one:
<literal>c_opt</literal> is a proper filetype in this context.
In order to declare a filetype without giving 
any rules to detect files of that type,
use <literal>declare</literal> in a 
<link linkend="cmd-filetype">:filetype</link> command:
<programlisting>
    :filetype
	declare c_opt
</programlisting>
</para>
<!-- </note> -->

<para>
The detected filetypes never contain an underscore.  A-A-P knows that the
underscore separates the normal filetype from the additional part.  When no
action is found for the whole filetype, it is tried by removing the "_" and
what comes after it.  (This isn't fully implemented yet!)
</para>

<para>
Care should be taken that an action should not invoke itself.  For example, to
always compile "version.c" when linking a program:
</para>

<programlisting>
        :attr {filetype = my_prog} $TARGET
        :action build my_prog object
            :do compile {target = version$OBJSUF} version.c
            :do build {target = $target} {filetype = program}
                                        $source version$OBJSUF
</programlisting>

<para>
Without "{filetype = program}" on the :do build" command, the action would
invoke itself, since the filetype for $TARGET is "my_prog".
</para>

<para>
  Attributes on source arguments of the
  <link linkend="cmd-do">:do</link> that start with "var_" are passed
on to the executed action as variables.  Examples:
</para>

<programlisting>
          :do build foo.c {var_OPTIMIZE = 2}

          :attr {var_OPTIMIZE = 2} foo.c
          :do build foo.c
</programlisting>

<para>
In the same way an attribute starting with "add_" is added to a variable.
Example:
</para>
<programlisting>
          :do build foo.c {add_DEFINE = -DBIG}
</programlisting>
<para>
  The value of the attribute is not added when it already appears in the
  variable.  This avoids adding it two or more times.
</para>

<para>
Variable values for the executed action can also be passed by giving an
attribute just after the action:
</para>
<programlisting>
          :do build {OPTIMIZE = 2} foo.c
</programlisting>

<para>
And yet another method is to define a user scope and use this for the action:
</para>
<programlisting>
          s_foo.OPTIMIZE = 4
          :do build {scope = s_foo} foo.c
</programlisting>


<bridgehead>Executing Actions</bridgehead>

<para>
The user can select how to perform an action for each type of file.  For
example, the user may specify that the "view" action on a file of type "html"
uses Netscape, and the "edit" action on a "html" file uses Vim.
</para>

<para>
To start an application:

<programlisting>
        :do actionname filename
</programlisting>
</para>

<para>
This starts the action "actionname" for file "filename".
Variables to be used by the commands can be specified after the action as
attributes:
</para>

<programlisting>
        :action convert default
            :sys convertcmd $?arg $source >$target
        ...
        :do convert {arg = -r} filename
</programlisting>

<para>
The filetype is automatically detected from the file name(s) and possibly its
contents.
To overrule the automatic filetype detection, specify the filetype as an
attribute on the file:
</para>

<programlisting>
        :do convert filename {filetype = text}
</programlisting>

<para>
  This changes the filetype of the source file, the input of the action.  To
  change the filetype of the output, the target of the action, use a
  "filetype" attribute on the action name:
</para>

<programlisting>
  :do convert {filetype = html} filename {filetype = text}
</programlisting>

<para>
Multiple filename arguments can be used:
</para>

<programlisting>
        :do action file1 file2 file3
</programlisting>

<para>
For some actions a target can or must be specified.  This is done as an
attribute on the action name:
</para>

<programlisting>
        :do convert {target = outfile} infile1 infile2
</programlisting>

<para>
Attributes of the input filenames other than "filetype" are not used to select
the action that will be executed.
</para>

<para>
The "async" attribute can be used to start the application without waiting for
it to finish.  However, this only works for system commands and when
multi-tasking is possible.  Example:
</para>

<programlisting>
        :do email {async} {remove} {to = piet} {subject = done building} logmessage
</programlisting>

<para>
The "remove" attribute specifies that the files are to be deleted after the
command is done, also when it fails.
</para>

<para>
When the filetype contains a "_" and no action can be found for it, the search
for an action is done with the part before the "_".  This is useful for
specifying a variant on a filetype where some commands are different and the
rest is the same.
</para>

<para>
"action" is usually one of these:
  <informaltable frame='none'>
    <tgroup cols='2'>
      <colspec colwidth="150"/>
      <tbody>
        <row>
          <entry>view</entry>
            <entry>Look at the file.  The "readonly" attribute is
              normally set, it can be reset to allow editing.</entry>
        </row>
        <row>
          <entry>edit</entry>
          <entry>Edit the file.</entry>
        </row>
        <row>
          <entry>email</entry>
          <entrytbl cols='2'>
            <tbody>
              <row>
                <entry>
                  Send the file by e-mail.  Relevant attributes:
                </entry>
              </row>
              <row>
                <entry>edit</entry>
                <entry>edit before sending</entry>
              </row>
              <row>
                <entry>subject</entry>
                <entry>subject of the message</entry>
              </row>
              <row>
                <entry>to</entry>
                <entry>destination</entry>
              </row>
            </tbody>
          </entrytbl>
        </row>
        <row>
          <entry>build</entry>
          <entry>Build the file(s), resulting in a program file.  The "target"
            attribute is needed to specify the output file.</entry>
        </row>
        <row>
          <entry>compile</entry>
          <entry>Build the file(s), resulting in object file(s).  The "target"
            attribute may be used to specify the output file.  When "target" is
            not specified the action may use a default, usually
            `src2obj(fname)`.  See <link linkend="python-src2obj">here</link>
          </entry>
        </row>
        <row>
          <entry>extract</entry>
          <entry>Unpack an archive.  Requires the program for unpacking to be
            available.  Unpacks in the current directory.</entry>
        </row>
        <row>
          <entry>preproc</entry>
          <entry>Run the preprocessor on the file.  The "target" attribute can
            be used to specify the output file.  When it is missing the output
            file name is formed from the input file name with
            `src2obj(fname, sufname = None)`  and then appending ".i".
          </entry>
        </row>
        <row>
          <entry>reference</entry>
          <entry>Run a cross referencer, creating or updating a symbol
            database.</entry>
        </row>
        <row>
          <entry>strip</entry>
          <entry>Strip symbols from a program.  Used to make it smaller after
            installing.</entry>
        </row>
      </tbody>
    </tgroup>
  </informaltable>
</para>

<para>
Examples:
</para>

<programlisting>
        :do view COPYRIGHT {filetype = text}
        :do edit configargs.xml
        :do email {edit} bugreport
        :do build {target = myprog} main.c version.c
        :do compile foo.cpp
</programlisting>


<bridgehead>Default Actions</bridgehead>

<para>
These are defined in the default.aap recipe.
<variablelist>
  <varlistentry><term>
      :action edit default
    </term>
    <listitem>
      <para>
        Uses the environment variable $VISUAL or $EDITOR.  Falls back to "vi"
        for Posix systems or "notepad" otherwise.  You can set the &Aap;
        variable <link linkend="var-editor">$EDITOR</link> to the editor
        program to be used.
      </para>
      <para>
        The {line = 999} attribute can be used to specify a line number to
        jump to.  The {byte = 999} attribute can be used to jump to a
        character position in the file.
      </para>
    </listitem>
  </varlistentry>

  <varlistentry><term>
      :action edit gif,jpeg,png
    </term>
    <listitem>
      <para>
        Uses the environment variable $PAINTER.  You can set the &Aap;
        variable <link linkend="var-editor">$PAINTER</link> to the image
        editor program to be used.
      </para>
      <para>
        The {line = 999} attribute can be used to specify a line number to
        jump to.  The {byte = 999} attribute can be used to jump to a
        character position in the file.
      </para>
    </listitem>
  </varlistentry>

  <varlistentry><term>
      :action depend
    </term>
    <listitem>
      <para>
        The default dependency checker.  Works for C and C++ files.
        Uses gcc when available, because it is faster and more reliable.
        Uses an internal function otherwise.
        This attempts to handle the situation that a header file doesn't exist
        (when it needs to be fetched or build)
        but that is not fully implemented yet.
      </para>
    </listitem>
  </varlistentry>

  <varlistentry><term>
      :action view html,xml,php
    </term>
    <listitem>
      <para>
        Uses the environment variable 
        <link linkend="var-browser">$BROWSER</link>.  Searches a list of known
        browsers if it's not set.
      </para>
    </listitem>
  </varlistentry>

  <varlistentry><term>
      :action email default
    </term>
    <listitem>
      <para>
        Uses the environment variable $MAILER.  Falls back to "mail".
      </para>
    </listitem>
  </varlistentry>

  <varlistentry><term>
      :action build default default
    </term>
    <listitem>
      <para>
        Does: ":sys $CC $LDFLAGS $CFLAGS -o $target $source $?LIBS"
      </para>
    </listitem>
  </varlistentry>

  <varlistentry><term>
      :action compile object c,cpp
    </term>
    <listitem>
      <para>
        Does ":sys $CC $CPPFLAGS $CFLAGS -c -o $target" $source
      </para>
    </listitem>
  </varlistentry>

  <varlistentry><term>
      :action preproc default c,cpp
    </term>
    <listitem>
      <para>
        Does ":sys $CC $CPPFLAGS -E $source > $target"
      </para>
    </listitem>
  </varlistentry>

</variablelist>
</para>


<bridgehead id="specify-actions">Specifying Actions</bridgehead>

<para>
Applications for an action-filetype pair can be specified with this command:
</para>

<programlisting>
        :action action in-filetype
                commands
</programlisting>

<para>
This associates the commands with action "action" and filetype "in-filetype".
The commands are A-A-P commands, just like what is used in the build commands
in a dependency.
</para>

<para>
The "in-filetype" is used for the source of the action, the type of the target
is undefined.  It is also possible to specify an action for turning a file of
one filetype into another filetype.  This is like a
<link linkend="cmd-rule">:rule</link> command, but using
filetypes instead of patterns.
</para>

<programlisting>
        :action action out-filetype in-filetype
                commands
</programlisting>

<para>
Actually, specifying an action with one filetype is like using "default" for
the out-filetype.
</para>

<para>
Several variables are set and can be used by the commands:
  <informaltable frame='none'>
    <tgroup cols='2'>
      <colspec colwidth="150"/>
      <tbody>
        <row>
          <entry>$fname</entry>
          <entry>The first file name of the
            <link linkend="cmd-do">:do</link> command.</entry>
      </row>
      <row>
        <entry>$source</entry>
        <entry>All the file names of the
          <link linkend="cmd-do">:do</link> command.</entry>
      </row>
      <row>
        <entry>$filetype</entry>
        <entry>The detected or specified filetype for the input.</entry>
      </row>
      <row>
        <entry>$targettype</entry>
        <entry>The detected or specified filetype for the output.</entry>
      </row>
      <row>
        <entry>$action</entry>
        <entry>The name of the action for which the commands are
          executed.</entry>
        </row>
      </tbody>
    </tgroup>
  </informaltable>
</para>

<para>
Furthermore, all attributes of the action are turned into variables.  Thus
when ":do action {arg = -x} file " is used, $arg is set to "-x".  Example for
using an optional "arg" attribute:
</para>

<programlisting>
        :action view foo
                :sys fooviewer $?arg $source
</programlisting>

<para>
In Python code check if the {edit} attribute is specified:
</para>

<programlisting>
        :action email default
            # when $subject and/or $to is missing editing is required
            @if not globals().get("subject") or not globals().get("to"):
                edit = 1
            @if globals().get("edit"):
                :do edit $fname
            :sys mail -s $subject $to &lt; $fname
</programlisting>

<para>
"action" and "ftype" can also be a comma-separated list of filetypes.  There
can't be any white space though.  Examples:
</para>

<programlisting>
        :action view html,xml,php
            :sys netscape --remote $source
        :action edit,view text,c,cpp
            :sys vim $source
</programlisting>

<para>
"filetype" can be "default" to specify an action used when there is no action
specifically for the filetype.
</para>

<para>
  When using a variable in the commands of an action without specifying a
  scope, or using the "_no" scope, its value is obtained from the first scope
  where it is defined from this list:
<orderedlist>
<listitem><para>
  The local scope, the scope of the action commands.
</para></listitem>
<listitem><para>
  Invoking command blocks.  That is, the scope of the command block that
  invoked the action with a
  <link linkend="cmd-do">:do</link> command.
  Then the scope of the command block that invoked that command block, and so
  on.  This excludes the toplevel.
</para></listitem>
<listitem><para>
 The recipe in which the ":do" command was invoked, its parent and so on.
 This goes on until and including the toplevel.
</para></listitem>
<listitem><para>
 The recipe in which the action was defined, its parent and so on.  This goes
 on until the toplevel.
</para></listitem>
</orderedlist>
</para>

<para>
  The meaning of this all is that when using a variable such as $FOOCMD, you
  can give it a default value in the recipe where the action is defined.
  The location where the action is later invoked from can define a different
  value.  Example:
</para>

<programlisting>
        FOOCMD = foocc
        done =
        :action compile object foo
            :sys $FOOCMD $source > $target
            _recipe.done += $-source
        :action foolist default
            :print Compiled $_recipe.done
</programlisting>

<para>
The action can be used like this:
</para>

<programlisting>
        all:
            FOOCMD = foocc -O            # used instead of the default $FOOCMD
            :do compile {target = opt/piet.o} piet.f
            done = nothing               # This has no effect
            :do foolist any
</programlisting>

<para>
  In action the "done" variable is used with the "_recipe" scope prepended,
  thus a "done" variable in the scope of the commands that invoked this action
  will not be used.
</para>

<para>
  Note that the "async" attribute of the
  <link linkend="cmd-do">:do</link> command may cause the
  <link linkend="cmd-sys">:sys</link>
command to work asynchronously.  That is done because the "async" attribute is
turned into the "async" variable for the
<link linkend="cmd-action">:action</link> commands.  If you don't
want a specific
<link linkend="cmd-sys">:sys</link> command to work asynchronously, reset "async":
</para>

<programlisting>
        :action view foo
            tt = $?async
            async = 
            :sys foo_prepare
            async = $tt
            :sys foo $source
</programlisting>

<para>
  However, since two consecutive
  <link linkend="cmd-sys">:sys</link> commands are executed together, this
should do the same thing:
</para>

<programlisting>
        :action view foo
            :sys foo_prepare
            :sys foo $source
</programlisting>

<para>
Quite often the program to be used depends on whether it is available on the
system.  For example, viewing HTML files can be done with netscape, mozilla,
konquerer or another browser.  We don't want to search the system for all
kinds of programs when starting, it would make the startup time quite long.
And we don't want to search each time the action is invoked, the result of the
first search can be used.  This construct can be used to achieve this:
</para>

<programlisting>
        BROWSER =
        :action view html
            @if not _recipe.BROWSER:
                :progsearch _recipe.BROWSER netscape mozilla konquerer
            :sys $_recipe.BROWSER $source
</programlisting>

<para>
The generic form form :progsearch is:
</para>

<programlisting>
        :progsearch varname progname ...
</programlisting>

<para>
The "_recipe" scope needs to be used for variables set in the commands and
need to be used after the action is finished.
</para>

<para>
The first argument is the variable name to which to assign the resulting
program name.  When none of the programs is found an error message is given.
Note that shell aliases are not found, only executable programs.
</para>

<para>
When the action uses a temporary file, make sure it is deleted even when one
of the commands fail.  The Python try/finally construction is ideal for this.
Example:
</para>

<programlisting>
        :action filter .pdf .txt
            tmp = `tempfname()`
            @try:
                :sys extract_docs $source >$tmp
                :sys docs2pdf $tmp $target
            @finally:
                :delete $tmp
            :print Converted $source to $target
</programlisting>

<para>
When the ":sys" commands execute without trouble the ":delete" and ":print"
commands are executed.
When the first ":sys" commands causes an exception, execution continues with
the ":delete" command and then the exception is handled.  Thus the second
":sys" command is skipped and the ":print" command is not reached.
</para>

<para>
More examples:
</para>
<programlisting>
        :action compile object c,cpp
            :sys $CC $CPPFLAGS $CFLAGS -c $source
        :action build object
            :sys $CC $LDFLAGS $CFLAGS -o $target $source
</programlisting>

<bridgehead>Specifying Actions in Python</bridgehead>

<para>
  When an action needs to be defined with more properties, a Python function
  can be used.  Example:
</para>
<programlisting>
        :python
            define_action("buildme", 1, """
                @if DEFER_ACTION_NAME:
                    :do $DEFER_ACTION_NAME {target = $target} $source
                @else
                    :sys me &lt; $source &gt; $target
                """,
                outtypes = ["me"],
                intypes = ["moo"],
                defer_var_names = ["BUILDME_ACTION"])
</programlisting>
<para>
  This defines an action almost like this :action command
</para>
<programlisting>
        :action buildme {primary} me moo
            @if _no.get("BUILDME_ACTION"):
                :do $BUILDME_ACTION {target = $target} $source
            @else:
                :sys me &lt; $source &gt; $target
</programlisting>
<para>
  An important difference is that when the action defined with $BUILDME_ACTION
  supports different input and output filetypes, the action defined with
  define_action() will use these.  The action defined with ":action" will
  always support "me" and "moo", even though the actually used $BUILDME_ACTION
  action supports different types.
</para>
<para>
  More info about the define_action() function in the 
  <link linkend="python-define-action">reference manual</link>.
</para>
