sync/gen_actions.ysl2
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 27 Jan 2021 17:13:24 +0100
branchmime-integrate
changeset 5281 2eeac2d5d012
parent 5169 f4b476b218e4
permissions -rw-r--r--
push before migration
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 // generate conditions and actions
     5 
     6 // Copyleft (c) 2017, 2018, p≡p foundation
     7 
     8 // Written by Volker Birk
     9 
    10 
    11 include yslt.yml2
    12 
    13 decl _func *name (*type) alias - {
    14     template %name=*name, %type=*type, "%type[@name='%name']"
    15         call *type with "content" content;
    16 };
    17 
    18 decl condition is _func (*type="condition");
    19 decl action is _func (*type="action");
    20 decl timeout is _func (*type="fsm");
    21 
    22 tstylesheet {
    23     include standardlib.ysl2
    24     include ./functions.ysl2
    25 
    26     include ./cond_act_*.yml2
    27 
    28     template "/protocol" {
    29         document "generated/{@name}_actions.c", "text" {
    30             ||
    31             /**
    32              * @file        «@name»_actions.c
    33              * @brief       Implementation of «@name» conditions, actions, and timeout handlers defined in «@name»_impl.h,
    34              *              with supporting static (internal) functions
    35              * @generated from ../sync/gen_actions.ysl2
    36              *
    37              * @license   GNU General Public License 3.0 - see LICENSE.txt
    38              */
    39 
    40             #include "pEp_internal.h"
    41             #include "map_asn1.h"
    42 
    43             #include "«@name»_impl.h"
    44             `` for "fsm" | #include "«@name»_fsm.h"
    45 
    46             /**
    47              *  <!--         _TID_greater()       -->
    48              *
    49              *  @internal
    50              *
    51              *  @brief       Compare two traffic identifiers and see if the first is greater than the second
    52              *
    53              *  @param[in]   t1          pointer to the first TID
    54              *  @param[in]   t2          pointer to the second TID
    55              *
    56              *  @retval      true        if t2 is NULL and t1 is not, or the size of t1 is greater than t2, or
    57              *                           the first non-matching byte of t1 is greater than that of t2
    58              *               false       otherwise
    59              */
    60             static bool _TID_greater(TID_t *t1, TID_t *t2)
    61             {
    62                 assert(t1 && t2);
    63                 if (t1 && !t2)
    64                     return true;
    65                 if (!t1)
    66                     return false;
    67 
    68                 if (t1->size > t2->size)
    69                     return true;
    70                 if (t2->size > t1->size)
    71                     return false;
    72 
    73                 return memcmp(t1->buf, t2->buf, t1->size) > 0;
    74             }
    75 
    76             /**
    77              *  <!--         _same_identity()       -->
    78              *
    79              *  @internal
    80              *
    81              *  @brief       Determine if two identity refer to the same identity (by comparing the unique identifier
    82              *               of user_id + address)
    83              *
    84              *  @param[in]   ident1          pointer to the first identity
    85              *  @param[in]   ident2          pointer to the second identity
    86              *
    87              *  @retval      true        if user_id and address match on both identities
    88              *               false       otherwise
    89              */
    90             static bool _same_identity(pEp_identity *ident1, pEp_identity *ident2)
    91             {
    92                 if (!(ident1 && ident1->user_id && ident1->address && ident2 && ident2->user_id && ident2->address))
    93                     return false;
    94 
    95                 return strcmp(ident1->user_id, ident2->user_id) == 0
    96                     && strcmp(ident1->address, ident2->address) == 0;
    97             }
    98 
    99             /**
   100              *  <!--         _have_identity_in()       -->
   101              *
   102              *  @internal
   103              *
   104              *  @brief       Given an identity list and an identity, determine if there is an identity in
   105              *               the list that refers to the same identity as the identity struct.
   106              *
   107              *  @param[in]   il          pointer to the identity list
   108              *  @param[in]   ident       pointer to the identity to search for
   109              *  @param[out]  found       true if an identity with matching unique identifiers is in the list, else false
   110              *
   111              *  @retval      PEP_ILLEGAL_VALUE        any of the input pointers are NULL
   112              *               PEP_OUT_OF_MEMORY        if memory problems occur
   113              *               PEP_STATUS_OK            otherwise
   114              */
   115             static PEP_STATUS _have_identity_in(identity_list *il, pEp_identity *ident, bool *found)
   116             {
   117                 assert(il && ident && found);
   118                 if (!(il && ident && found))
   119                     return PEP_ILLEGAL_VALUE;
   120 
   121                 bool _found = false;
   122                 for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
   123                     if (_same_identity(_il->ident, ident)) {
   124                         _found = true;
   125                         break;
   126                     }
   127                 }
   128                 if (!_found) {
   129                     pEp_identity *_ident = identity_dup(ident);
   130                     if (!_ident)
   131                         return PEP_OUT_OF_MEMORY;
   132                     identity_list *_il = identity_list_add(il, _ident);
   133                     if (!_il) {
   134                         free(_ident);
   135                         return PEP_OUT_OF_MEMORY;
   136                     }
   137                 }
   138 
   139                 *found = _found;
   140                 return PEP_STATUS_OK;
   141             }
   142 
   143             ||
   144             apply "func:distinctName(//condition)", 0;
   145             apply "func:distinctName(//action)", 0;
   146             apply "/protocol/fsm", 0;
   147         }
   148     }
   149 
   150     template "condition" | #error condition «@name» not implemented\n
   151     template "action" | #error action «@name» not implemented\n
   152 
   153     function "condition" {
   154         param "content";
   155         ||
   156         PEP_STATUS «@name»(PEP_SESSION session, bool *result)
   157         {
   158             assert(session && result);
   159             if (!(session && result))
   160                 return PEP_ILLEGAL_VALUE;
   161 
   162         ||
   163         copy "$content";
   164         ||
   165 
   166             return PEP_STATUS_OK;
   167         }
   168 
   169         ||
   170     }
   171 
   172     function "action" {
   173         param "content";
   174         ||
   175         PEP_STATUS «@name»(PEP_SESSION session)
   176         {
   177             assert(session);
   178             if (!session)
   179                 return PEP_ILLEGAL_VALUE;
   180 
   181         ||
   182         copy "$content";
   183         ||
   184 
   185             return PEP_STATUS_OK;
   186         }
   187 
   188         ||
   189     }
   190 
   191     function "fsm" {
   192         param "content";
   193         ||
   194         PEP_STATUS «@name»TimeoutHandler(PEP_SESSION session)
   195         {
   196             assert(session);
   197             if (!session)
   198                 return PEP_ILLEGAL_VALUE;
   199 
   200         ||
   201         copy "$content";
   202         ||
   203 
   204             return PEP_STATUS_OK;
   205         }
   206 
   207         ||
   208     }
   209 }
   210