slang-users mailing list

[2021 Date Index] [2021 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] Qualifiers and C


Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
> Hi,
> I would like to make a data qualifier for a function that I export with
> Slirp:
>
> action("EditFile"; data="/etc/bashrc");
>
> I could have used SLang_get_string_qualifier() for this, which is OK.
> However I also would like to allow integer qualifiers. like e.g.:
>
> action("MoveUp"; data=5)
>
> I don't see any function that would allow to test the qualifier type.
> Should I just first try to get integer with some special default value?

There is no API function that does this.

It seems to me that somewhere you need to map the strings "EditFile"
and "MoveUp" to the corresponding functions.  Also, if the data
qualifier is not present, default values will have to be provided.
Here is one suggestion: Regard the following as pseudocode:

   typedef struct _Action_Type
   {
      char *action_name;
      int qualifier_type;
      int (*action_funct)(struct _Action_Type *a, void *vdata);
   }
   Action_Type;

   static int editfile_func (Action_Type *a, void *vdata)
   {
      char *data = "default";
      if (vdata ! NULL) data = (char *)vdata;
         .
         .
   }

   static int moveup_func (Action_Type *a, void *vdata)
   {
      int data = 1;
      if (vdata ! NULL) data = *(int *)vdata;
         .
         .
   }

   Action_Type Action_Table [] =
   {
     {"EditFile", SLANG_STRING_TYPE, editfile_func},
     {"MoveUp", SLANG_INT_TYPE, moveup_func},
        .
        .
     {NULL, -1}
   }

   void action_intrinsic (char *action_name)
   {
      Action_Type *a;
      a = Action_Table;
      while (a->action_name != NULL)
       {
          if (0 == strcmp (a->action_name, action_name))
            {
               int status;

               if (0 == SLang_qualifier_exists ("data"))
                 return (*a->action_funct)(a, NULL);

               switch (a->qualifier_type)
                 {
                    int i; char *s;

                  case SLANG_INT_TYPE:
                    if (-1 == SLang_get_int_qualifier (a, &i, 0))
                      return -1;
                    status = (*a->action_funct)(a, &i);
                    break;

                  case SLANG_STRING_TYPE:
                    if (-1 == SLang_get_string_qualifier (a, &s, NULL))
                      return -1;
                    status = (*a->action_funct)(a, s);
                    SLang_free_slstring (s);
                    break;
                 }
               if (status == -1)
                 {
                    /* Handle error */
                 }
               return;
            }
        a++;
       }

     /* Error, action_name not supported */
   }

> Also, is it possible to pass a struct as a qualifiers? How to get such
> struct on the C side? Like e.g.:
>
> stru = @ActionData_Type;
> stru.filename="/etc/bashrc";
> stru.mode="rw";
> action("EditFile"; data=stru);

This is not supported by the API.  For complex cases such as this, I
suggest that you split the function between a high-level "public"
interface written in slang, and a lower level "private" interface that
accepts a fixed argument list.

I hope this helps.
--John
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.


[2021 date index] [2021 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]