"Fossies" - the Fresh Open Source Software Archive

Member "cook-2.34/cook/opcode/touch.c" (25 Sep 2010, 4852 Bytes) of package /linux/misc/old/cook-2.34.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "touch.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *      cook - file construction tool
    3  *      Copyright (C) 1997-1999, 2001, 2004, 2006-2009 Peter Miller
    4  *
    5  *      This program is free software; you can redistribute it and/or modify
    6  *      it under the terms of the GNU General Public License as published by
    7  *      the Free Software Foundation; either version 3 of the License, or
    8  *      (at your option) any later version.
    9  *
   10  *      This program is distributed in the hope that it will be useful,
   11  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  *      GNU General Public License for more details.
   14  *
   15  *      You should have received a copy of the GNU General Public License
   16  *      along with this program. If not, see
   17  *      <http://www.gnu.org/licenses/>.
   18  */
   19 
   20 #include <common/ac/stdio.h>
   21 
   22 #include <common/error_intl.h>
   23 #include <cook/opcode/context.h>
   24 #include <cook/opcode/private.h>
   25 #include <cook/opcode/touch.h>
   26 #include <cook/option.h>
   27 #include <cook/os_interface.h>
   28 #include <common/str_list.h>
   29 #include <common/trace.h>
   30 
   31 
   32 /*
   33  * NAME
   34  *      execute
   35  *
   36  * SYNOPSIS
   37  *      opcode_status_ty execute(opcode_ty *, opcode_context_ty *);
   38  *
   39  * DESCRIPTION
   40  *      The execute function is used to execute the given opcode within
   41  *      the given interpretation context.
   42  *
   43  * RETURNS
   44  *      opcode_status_ty to indicate the result of the execution
   45  */
   46 
   47 static opcode_status_ty
   48 execute(const opcode_ty *op, opcode_context_ty *icp)
   49 {
   50     opcode_status_ty status;
   51     string_list_ty  *value;
   52 
   53     trace(("opcode_touch::execute()\n{\n"));
   54     (void)op;
   55     status = opcode_status_success;
   56     value = opcode_context_string_list_pop(icp);
   57     if (!option_test(OPTION_SILENT))
   58     {
   59         string_ty       *s;
   60         sub_context_ty  *scp;
   61 
   62         /*
   63          * If the command has not been silenced,
   64          * form it into a string and echo it.
   65          */
   66         s = wl2str(value, 0, value->nstrings - 1, (char *)0);
   67         scp = sub_context_new();
   68         sub_var_set_string(scp, "File_Name", s);
   69         error_intl(scp, i18n("touch $filename"));
   70         sub_context_delete(scp);
   71         str_free(s);
   72     }
   73     if (option_test(OPTION_ACTION))
   74     {
   75         size_t          j;
   76 
   77         for (j = 0; j < value->nstrings; j++)
   78         {
   79             if (os_touch(value->string[j]))
   80             {
   81                 /*
   82                  * Error message already printed.
   83                  */
   84                 status = opcode_status_error;
   85             }
   86         }
   87     }
   88     string_list_delete(value);
   89     trace(("return %s;\n", opcode_status_name(status)));
   90     trace(("}\n"));
   91     return status;
   92 }
   93 
   94 
   95 /*
   96  * NAME
   97  *      script
   98  *
   99  * SYNOPSIS
  100  *      opcode_status_ty script(opcode_ty *, opcode_context_ty *);
  101  *
  102  * DESCRIPTION
  103  *      The script function is used to script the given opcode within
  104  *      the given interpretation context.
  105  *
  106  * RETURNS
  107  *      opcode_status_ty to indicate the result
  108  */
  109 
  110 static opcode_status_ty
  111 script(const opcode_ty *op, opcode_context_ty *icp)
  112 {
  113     opcode_status_ty status;
  114     string_list_ty  *value;
  115     size_t          j;
  116     string_ty       *s;
  117 
  118     trace(("opcode_touch::script()\n{\n"));
  119     (void)op;
  120     status = opcode_status_success;
  121     value = opcode_context_string_list_pop(icp);
  122     if (!option_test(OPTION_SILENT))
  123     {
  124         printf("echo touch");
  125         for (j = 0; j < value->nstrings; j++)
  126         {
  127             s = str_quote_shell(value->string[j]);
  128             printf(" %s", s->str_text);
  129             str_free(s);
  130         }
  131         printf("\n");
  132     }
  133     if (option_test(OPTION_ACTION))
  134     {
  135         printf("touch");
  136         for (j = 0; j < value->nstrings; j++)
  137         {
  138             s = str_quote_shell(value->string[j]);
  139             printf(" %s", s->str_text);
  140             str_free(s);
  141         }
  142         printf(" || exit 1\n");
  143     }
  144     string_list_delete(value);
  145     trace(("return %s;\n", opcode_status_name(status)));
  146     trace(("}\n"));
  147     return status;
  148 }
  149 
  150 
  151 /*
  152  * NAME
  153  *      method
  154  *
  155  * DESCRIPTION
  156  *      The method variable describes this class.
  157  *
  158  * CAVEAT
  159  *      This symbol is not exported from this file.
  160  */
  161 
  162 static opcode_method_ty method =
  163 {
  164     "touch",
  165     sizeof(opcode_ty),
  166     0,                          /* destructor */
  167     execute,
  168     script,
  169     0,                          /* disassemble */
  170 };
  171 
  172 
  173 /*
  174  * NAME
  175  *      opcode_touch_new
  176  *
  177  * SYNOPSIS
  178  *      opcode_ty *opcode_touch_new(void);
  179  *
  180  * DESCRIPTION
  181  *      The opcode_touch_new function is used to allocate a new instance
  182  *      of a touch opcode.
  183  *
  184  * RETURNS
  185  *      opcode_ty *; use opcode_delete when you are finished with it.
  186  */
  187 
  188 opcode_ty *
  189 opcode_touch_new(void)
  190 {
  191     opcode_ty       *op;
  192 
  193     trace(("opcode_touch_new()\n{\n"));
  194     op = opcode_new(&method);
  195     trace(("return %p;\n", op));
  196     trace(("}\n"));
  197     return op;
  198 }