"Fossies" - the Fresh Open Source Software Archive

Member "cook-2.34/cook/builtin/dos.c" (25 Sep 2010, 5077 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 "dos.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *      cook - file construction tool
    3  *      Copyright (C) 1998, 1999, 2006-2008 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/ctype.h>
   21 
   22 #include <cook/builtin/dos.h>
   23 #include <common/stracc.h>
   24 #include <common/str_list.h>
   25 #include <common/trace.h>
   26 
   27 
   28 static string_ty *
   29 dos_path(string_ty *arg)
   30 {
   31     stracc          sa;
   32     string_ty       *result;
   33     const char      *cp;
   34 
   35     stracc_constructor(&sa);
   36     sa_open(&sa);
   37     if
   38     (
   39         arg->str_text[0] == '/'
   40     &&
   41         arg->str_text[1] == '/'
   42     &&
   43         isalpha((unsigned char)arg->str_text[2])
   44     &&
   45         (arg->str_text[3] == '/' || arg->str_text[3] == '\0')
   46     )
   47     {
   48         sa_char(&sa, arg->str_text[2]);
   49         sa_char(&sa, ':');
   50         cp = arg->str_text + 3;
   51     }
   52     else
   53         cp = arg->str_text;
   54     while (*cp)
   55     {
   56         if (*cp == '/')
   57             sa_char(&sa, '\\');
   58         else
   59             sa_char(&sa, *cp);
   60         ++cp;
   61     }
   62     result = sa_close(&sa);
   63     stracc_destructor(&sa);
   64     return result;
   65 }
   66 
   67 
   68 static string_ty *
   69 dos_path_undo(string_ty *arg)
   70 {
   71     stracc          sa;
   72     string_ty       *result;
   73     const char      *cp;
   74 
   75     stracc_constructor(&sa);
   76     sa_open(&sa);
   77     if (isalpha((unsigned char)arg->str_text[0]) && arg->str_text[1] == ':')
   78     {
   79         sa_char(&sa, '/');
   80         sa_char(&sa, '/');
   81         sa_char(&sa, arg->str_text[0]);
   82         cp = arg->str_text + 2;
   83         if (*cp != '\\')
   84             sa_char(&sa, '/');
   85     }
   86     else
   87         cp = arg->str_text;
   88     while (*cp)
   89     {
   90         if (*cp == '\\')
   91             sa_char(&sa, '/');
   92         else
   93             sa_char(&sa, *cp);
   94         ++cp;
   95     }
   96     result = sa_close(&sa);
   97     stracc_destructor(&sa);
   98     return result;
   99 }
  100 
  101 
  102 /*
  103  * NAME
  104  *      builtin_dos_path - dos_path strings
  105  *
  106  * SYNOPSIS
  107  *      int builtin_dos_path(string_list_ty *result, string_list_ty *args);
  108  *
  109  * DESCRIPTION
  110  *      Defined is a built-in function of cook, described as follows:
  111  *      This function requires one or more arguments,
  112  *      which will be converted from unix paths to dos paths.
  113  *
  114  * RETURNS
  115  *      It returns the arguments dos paths.
  116  *
  117  * CAVEAT
  118  *      The returned result is in dynamic memory.
  119  *      It is the responsibility of the caller to dispose of
  120  *      the result when it is finished, with a string_list_destructor() call.
  121  */
  122 
  123 static int
  124 dos_path_interpret(string_list_ty *result, const string_list_ty *args,
  125     const struct expr_position_ty *pp, const struct opcode_context_ty *ocp)
  126 {
  127     size_t          j;
  128 
  129     trace(("dos_path\n"));
  130     (void)pp;
  131     (void)ocp;
  132     assert(result);
  133     assert(args);
  134     assert(args->nstrings);
  135     for (j = 1; j < args->nstrings; j++)
  136     {
  137         string_ty       *s;
  138 
  139         s = dos_path(args->string[j]);
  140         string_list_append(result, s);
  141         str_free(s);
  142     }
  143     return 0;
  144 }
  145 
  146 
  147 builtin_ty builtin_dos_path =
  148 {
  149     "dos-path",
  150     dos_path_interpret,
  151     dos_path_interpret,         /* script */
  152 };
  153 
  154 
  155 /*
  156  * NAME
  157  *      builtin_dos_path_undo - dos_path_undo strings
  158  *
  159  * SYNOPSIS
  160  *      int builtin_dos_path_undo(string_list_ty *result, string_list_ty *args);
  161  *
  162  * DESCRIPTION
  163  *      Defined is a built-in function of cook, described as follows:
  164  *      This function requires one or more arguments,
  165  *      which will be converted from dos paths to unix paths.
  166  *
  167  * RETURNS
  168  *      It returns the arguments unix paths.
  169  *
  170  * CAVEAT
  171  *      The returned result is in dynamic memory.
  172  *      It is the responsibility of the caller to dispose of
  173  *      the result when it is finished, with a string_list_destructor() call.
  174  */
  175 
  176 static int
  177 dos_path_undo_interpret(string_list_ty *result, const string_list_ty *args,
  178     const struct expr_position_ty *pp, const struct opcode_context_ty *ocp)
  179 {
  180     size_t          j;
  181 
  182     trace(("dos_path_undo\n"));
  183     (void)pp;
  184     (void)ocp;
  185     assert(result);
  186     assert(args);
  187     assert(args->nstrings);
  188     for (j = 1; j < args->nstrings; j++)
  189     {
  190         string_ty        *s;
  191 
  192         s = dos_path_undo(args->string[j]);
  193         string_list_append(result, s);
  194         str_free(s);
  195     }
  196     return 0;
  197 }
  198 
  199 
  200 builtin_ty builtin_dos_path_undo =
  201 {
  202     "dos-path-undo",
  203     dos_path_undo_interpret,
  204     dos_path_undo_interpret,    /* script */
  205 };
  206 
  207 
  208 builtin_ty builtin_un_dos_path =
  209 {
  210     "un-dos-path",
  211     dos_path_undo_interpret,
  212     dos_path_undo_interpret,    /* script */
  213 };