macro.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:40k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * macro.c : Custom <vlc> macro handling
  3.  *****************************************************************************
  4.  * Copyright (C) 2001-2005 the VideoLAN team
  5.  * $Id: 22e323177a4ac9e3ab9db8d0f4a946040e24f916 $
  6.  *
  7.  * Authors: Gildas Bazin <gbazin@netcourrier.com>
  8.  *          Laurent Aimar <fenrir@via.ecp.fr>
  9.  *          Christophe Massiot <massiot@via.ecp.fr>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24.  *****************************************************************************/
  25. #ifdef HAVE_CONFIG_H
  26. # include "config.h"
  27. #endif
  28. #include "http.h"
  29. #include "macros.h"
  30. #include "vlc_url.h"
  31. static int MacroParse( macro_t *m, char *psz_src )
  32. {
  33.     char *dup = strdup( (char *)psz_src );
  34.     char *src = dup;
  35.     char *p;
  36.     int     i_skip;
  37. #define EXTRACT( name, l ) 
  38.         src += l;    
  39.         p = strchr( src, '"' );             
  40.         if( p )                             
  41.         {                                   
  42.             *p++ = '';                    
  43.         }                                   
  44.         m->name = strdup( src );            
  45.         if( !p )                            
  46.         {                                   
  47.             break;                          
  48.         }                                   
  49.         src = p;
  50.     /* init m */
  51.     m->id = NULL;
  52.     m->param1 = NULL;
  53.     m->param2 = NULL;
  54.     /* parse */
  55.     src += 4;
  56.     while( *src )
  57.     {
  58.         while( *src == ' ')
  59.         {
  60.             src++;
  61.         }
  62.         if( !strncmp( src, "id="", 4 ) )
  63.         {
  64.             EXTRACT( id, 4 );
  65.         }
  66.         else if( !strncmp( src, "param1="", 8 ) )
  67.         {
  68.             EXTRACT( param1, 8 );
  69.         }
  70.         else if( !strncmp( src, "param2="", 8 ) )
  71.         {
  72.             EXTRACT( param2, 8 );
  73.         }
  74.         else
  75.         {
  76.             break;
  77.         }
  78.     }
  79.     if( strstr( src, "/>" ) )
  80.     {
  81.         src = strstr( src, "/>" ) + 2;
  82.     }
  83.     else
  84.     {
  85.         src += strlen( src );
  86.     }
  87.     if( m->id == NULL )
  88.     {
  89.         m->id = strdup( "" );
  90.     }
  91.     if( m->param1 == NULL )
  92.     {
  93.         m->param1 = strdup( "" );
  94.     }
  95.     if( m->param2 == NULL )
  96.     {
  97.         m->param2 = strdup( "" );
  98.     }
  99.     i_skip = src - dup;
  100.     free( dup );
  101.     return i_skip;
  102. #undef EXTRACT
  103. }
  104. static void MacroClean( macro_t *m )
  105. {
  106.     free( m->id );
  107.     free( m->param1 );
  108.     free( m->param2 );
  109. }
  110. static int StrToMacroType( const char *name )
  111. {
  112.     int i;
  113.     if( !name || *name == '')
  114.     {
  115.         return MVLC_UNKNOWN;
  116.     }
  117.     for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
  118.     {
  119.         if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
  120.         {
  121.             return StrToMacroTypeTab[i].i_type;
  122.         }
  123.     }
  124.     return MVLC_UNKNOWN;
  125. }
  126. static void MacroDo( httpd_file_sys_t *p_args,
  127.                      macro_t *m,
  128.                      char *p_request, int i_request,
  129.                      char **pp_data,  int *pi_data,
  130.                      char **pp_dst )
  131. {
  132.     intf_thread_t  *p_intf = p_args->p_intf;
  133.     intf_sys_t     *p_sys = p_args->p_intf->p_sys;
  134.     char control[512];
  135. #define ALLOC( l ) 
  136.     {               
  137.         int __i__ = *pp_dst - *pp_data; 
  138.         *pi_data += (l);                  
  139.         *pp_data = realloc( *pp_data, *pi_data );   
  140.         *pp_dst = (*pp_data) + __i__;   
  141.     }
  142. #define PRINT( str ) 
  143.     ALLOC( strlen( str ) + 1 ); 
  144.     *pp_dst += sprintf( *pp_dst, "%s", str );
  145. #define PRINTS( str, s ) 
  146.     ALLOC( strlen( str ) + strlen( s ) + 1 ); 
  147.     { 
  148.         char * psz_cur = *pp_dst; 
  149.         *pp_dst += sprintf( *pp_dst, str, s ); 
  150.         while( psz_cur && *psz_cur ) 
  151.         {  
  152.             /* Prevent script injection */ 
  153.             if( *psz_cur == '<' ) *psz_cur = '*'; 
  154.             if( *psz_cur == '>' ) *psz_cur = '*'; 
  155.             psz_cur++ ; 
  156.         } 
  157.     }
  158.     switch( StrToMacroType( m->id ) )
  159.     {
  160.         case MVLC_CONTROL:
  161.             if( i_request <= 0 )
  162.             {
  163.                 break;
  164.             }
  165.             ExtractURIValue( p_request, "control", control, 512 );
  166.             if( *m->param1 && !strstr( m->param1, control ) )
  167.             {
  168.                 msg_Warn( p_intf, "unauthorized control=%s", control );
  169.                 break;
  170.             }
  171.             switch( StrToMacroType( control ) )
  172.             {
  173.                 case MVLC_PLAY:
  174.                 {
  175.                     int i_item;
  176.                     char item[512];
  177.                     ExtractURIValue( p_request, "item", item, 512 );
  178.                     i_item = atoi( item );
  179.                     /* id = 0 : simply ask playlist to play */
  180.                     if( i_item == 0 )
  181.                     {
  182.                         playlist_Play( p_sys->p_playlist );
  183.                         msg_Dbg( p_intf, "requested playlist play" );
  184.                         break;
  185.                     }
  186.                     //TODO: really locked here ?
  187.                     playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
  188.                                       true, NULL,
  189.                                       playlist_ItemGetById( p_sys->p_playlist,
  190.                                       i_item ) );
  191.                     msg_Dbg( p_intf, "requested playlist item: %i", i_item );
  192.                     break;
  193.                 }
  194.                 case MVLC_STOP:
  195.                     playlist_Control( p_sys->p_playlist, PLAYLIST_STOP,
  196.                                       true );
  197.                     msg_Dbg( p_intf, "requested playlist stop" );
  198.                     break;
  199.                 case MVLC_PAUSE:
  200.                     playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE,
  201.                                       true );
  202.                     msg_Dbg( p_intf, "requested playlist pause" );
  203.                     break;
  204.                 case MVLC_NEXT:
  205.                     playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP,
  206.                                       true, 1 );
  207.                     msg_Dbg( p_intf, "requested playlist next" );
  208.                     break;
  209.                 case MVLC_PREVIOUS:
  210.                     playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP,
  211.                                       true, -1 );
  212.                     msg_Dbg( p_intf, "requested playlist previous" );
  213.                     break;
  214.                 case MVLC_FULLSCREEN:
  215.                     if( p_sys->p_input )
  216.                     {
  217.                         vout_thread_t *p_vout;
  218.                         p_vout = input_GetVout( p_sys->p_input );
  219.                         if( p_vout )
  220.                         {
  221.                             var_SetBool( p_vout, "fullscreen", !var_GetBool( p_vout, "fullscreen" ) );
  222.                             vlc_object_release( p_vout );
  223.                             msg_Dbg( p_intf, "requested fullscreen toggle" );
  224.                         }
  225.                     }
  226.                     break;
  227.                 case MVLC_SEEK:
  228.                 {
  229.                     char value[30];
  230.                     ExtractURIValue( p_request, "seek_value", value, 30 );
  231.                     decode_URI( value );
  232.                     HandleSeek( p_intf, value );
  233.                     break;
  234.                 }
  235.                 case MVLC_VOLUME:
  236.                 {
  237.                     char vol[8];
  238.                     audio_volume_t i_volume;
  239.                     int i_value;
  240.                     ExtractURIValue( p_request, "value", vol, 8 );
  241.                     aout_VolumeGet( p_intf, &i_volume );
  242.                     decode_URI( vol );
  243.                     if( vol[0] == '+' )
  244.                     {
  245.                         i_value = atoi( vol + 1 );
  246.                         if( (i_volume + i_value) > AOUT_VOLUME_MAX )
  247.                         {
  248.                             aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
  249.                             msg_Dbg( p_intf, "requested volume set: max" );
  250.                         }
  251.                         else
  252.                         {
  253.                             aout_VolumeSet( p_intf , (i_volume + i_value) );
  254.                             msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
  255.                         }
  256.                     }
  257.                     else if( vol[0] == '-' )
  258.                     {
  259.                         i_value = atoi( vol + 1 );
  260.                         if( (i_volume - i_value) < AOUT_VOLUME_MIN )
  261.                         {
  262.                             aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
  263.                             msg_Dbg( p_intf, "requested volume set: min" );
  264.                         }
  265.                         else
  266.                         {
  267.                             aout_VolumeSet( p_intf , (i_volume - i_value) );
  268.                             msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
  269.                         }
  270.                     }
  271.                     else if( strstr(vol, "%") != NULL )
  272.                     {
  273.                         i_value = atoi( vol );
  274.                         if( (i_value <= 400) && (i_value>=0) ){
  275.                             aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/400+AOUT_VOLUME_MIN);
  276.                             msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
  277.                         }
  278.                     }
  279.                     else
  280.                     {
  281.                         i_value = atoi( vol );
  282.                         if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
  283.                         {
  284.                             aout_VolumeSet( p_intf , atoi( vol ) );
  285.                             msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
  286.                         }
  287.                     }
  288.                     break;
  289.                 }
  290.                 /* playlist management */
  291.                 case MVLC_ADD:
  292.                 {
  293.                     char mrl[1024], psz_name[1024], tmp[1024];
  294.                     char *p, *str;
  295.                     input_item_t *p_input;
  296.                     ExtractURIValue( p_request, "mrl", tmp, 1024 );
  297.                     decode_URI( tmp );
  298.                     ExtractURIValue( p_request, "name", psz_name, 1024 );
  299.                     decode_URI( psz_name );
  300.                     if( !*psz_name )
  301.                     {
  302.                         memcpy( psz_name, tmp, 1024 );
  303.                     }
  304.                     /* addslashes for backward compatibility with the old
  305.                      * http intf */
  306.                     p = mrl; str = tmp;
  307.                     while( *str != '' )
  308.                     {
  309.                         if( *str == '"' || *str == ''' || *str == '\' )
  310.                         {
  311.                             *p++ = '\';
  312.                         }
  313.                         *p++ = *str;
  314.                         str++;
  315.                     }
  316.                     *p = '';
  317.                     p_input = MRLParse( p_intf, mrl, psz_name );
  318.                     char *psz_uri = p_input ? input_item_GetURI( p_input ) : NULL;
  319.                     if( psz_uri && *psz_uri &&
  320.                         playlist_AddInput( p_sys->p_playlist, p_input,
  321.                                            PLAYLIST_APPEND, PLAYLIST_END,
  322.                                            true, false) == VLC_SUCCESS )
  323.                         msg_Dbg( p_intf, "requested mrl add: %s", mrl );
  324.                     else
  325.                         msg_Warn( p_intf, "adding mrl failed: %s", mrl );
  326.                     free( psz_uri );
  327.                     if( p_input )
  328.                         vlc_gc_decref( p_input );
  329.                     break;
  330.                 }
  331.                 case MVLC_DEL:
  332.                 {
  333.                     int i_item, *p_items = NULL, i_nb_items = 0;
  334.                     char item[512];
  335.                     const char *p_parser = p_request;
  336.                     /* Get the list of items to delete */
  337.                     while( (p_parser =
  338.                             ExtractURIValue( p_parser, "item", item, 512 )) )
  339.                     {
  340.                         if( !*item ) continue;
  341.                         i_item = atoi( item );
  342.                         p_items = realloc( p_items, (i_nb_items + 1) *
  343.                                            sizeof(int) );
  344.                         p_items[i_nb_items] = i_item;
  345.                         i_nb_items++;
  346.                     }
  347.                     if( i_nb_items )
  348.                     {
  349.                         int i;
  350.                         for( i = 0; i < i_nb_items; i++ )
  351.                         {
  352.                             playlist_DeleteFromInput( p_sys->p_playlist,
  353.                                                       p_items[i], false );
  354.                             msg_Dbg( p_intf, "requested playlist delete: %d",
  355.                                      p_items[i] );
  356.                             p_items[i] = -1;
  357.                         }
  358.                     }
  359.                     free( p_items );
  360.                     break;
  361.                 }
  362.                 case MVLC_KEEP:
  363.                 {
  364.                     int i_item, *p_items = NULL, i_nb_items = 0;
  365.                     char item[512];
  366.                     const char *p_parser = p_request;
  367.                     int i,j;
  368.                     /* Get the list of items to keep */
  369.                     while( (p_parser =
  370.                        ExtractURIValue( p_parser, "item", item, 512 )) )
  371.                     {
  372.                         if( !*item ) continue;
  373.                         i_item = atoi( item );
  374.                         p_items = realloc( p_items, (i_nb_items + 1) *
  375.                                            sizeof(int) );
  376.                         p_items[i_nb_items] = i_item;
  377.                         i_nb_items++;
  378.                     }
  379.                     for( i = p_sys->p_playlist->items.i_size - 1 ; i >= 0; i-- )
  380.                     {
  381.                         /* Check if the item is in the keep list */
  382.                         for( j = 0 ; j < i_nb_items ; j++ )
  383.                         {
  384.                             if( p_items[j] ==
  385.                                  ARRAY_VAL(p_sys->p_playlist->items,i)
  386.                                                 ->i_id)
  387.                                 break;
  388.                         }
  389.                         if( j == i_nb_items )
  390.                         {
  391.                             playlist_DeleteFromInput( p_sys->p_playlist,
  392.                                      p_sys->p_playlist->items.p_elems[i]->i_id,
  393.                                                       false );
  394.                             msg_Dbg( p_intf, "requested playlist delete: %d",
  395.                                      i );
  396.                         }
  397.                     }
  398.                     free( p_items );
  399.                     break;
  400.                 }
  401.                 case MVLC_EMPTY:
  402.                 {
  403.                     playlist_Clear( p_sys->p_playlist, false );
  404.                     msg_Dbg( p_intf, "requested playlist empty" );
  405.                     break;
  406.                 }
  407.                 case MVLC_SORT:
  408.                 {
  409.                     char type[12];
  410.                     char order[2];
  411.                     char item[512];
  412.                     int i_order;
  413.                     int i_item;
  414.                     ExtractURIValue( p_request, "type", type, 12 );
  415.                     ExtractURIValue( p_request, "order", order, 2 );
  416.                     ExtractURIValue( p_request, "item", item, 512 );
  417.                     i_item = atoi( item );
  418.                     if( order[0] == '0' ) i_order = ORDER_NORMAL;
  419.                     else i_order = ORDER_REVERSE;
  420.                     if( !strcmp( type , "title" ) )
  421.                     {
  422.                         playlist_RecursiveNodeSort( p_sys->p_playlist,
  423.                                                     /* Ugly hack,but not worse than before ... */
  424.                                                     p_sys->p_playlist->p_root_onelevel,
  425.                                                     SORT_TITLE_NODES_FIRST,
  426.                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
  427.                         msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
  428.                     }
  429.                     else if( !strcmp( type , "author" ) )
  430.                     {
  431.                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
  432.                                                     p_sys->p_playlist->p_root_onelevel,
  433.                                                     SORT_ARTIST,
  434.                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
  435.                         msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
  436.                     }
  437.                     else if( !strcmp( type , "shuffle" ) )
  438.                     {
  439.                         playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
  440.                                                     p_sys->p_playlist->p_root_onelevel,
  441.                                                     SORT_RANDOM,
  442.                                                     ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
  443.                         msg_Dbg( p_intf, "requested playlist shuffle");
  444.                     }
  445.                     break;
  446.                 }
  447.                 case MVLC_MOVE:
  448.                 {
  449.                     char psz_pos[6];
  450.                     char psz_newpos[6];
  451.                     int i_pos;
  452.                     int i_newpos;
  453.                     ExtractURIValue( p_request, "psz_pos", psz_pos, 6 );
  454.                     ExtractURIValue( p_request, "psz_newpos", psz_newpos, 6 );
  455.                     i_pos = atoi( psz_pos );
  456.                     i_newpos = atoi( psz_newpos );
  457.                     /* FIXME FIXME TODO TODO XXX XXX
  458.                     ( duplicate from rpn.c )
  459.                     if ( i_pos < i_newpos )
  460.                     {
  461.                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
  462.                     }
  463.                     else
  464.                     {
  465.                         playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
  466.                     }
  467.                     msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
  468.                     FIXME FIXME TODO TODO XXX XXX */
  469.                     break;
  470.                 }
  471.                 /* admin function */
  472.                 case MVLC_CLOSE:
  473.                 {
  474.                     char id[512];
  475.                     ExtractURIValue( p_request, "id", id, 512 );
  476.                     msg_Dbg( p_intf, "requested close id=%s", id );
  477. #if 0
  478.                     if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
  479.                     {
  480.                         msg_Warn( p_intf, "close failed for id=%s", id );
  481.                     }
  482. #endif
  483.                     break;
  484.                 }
  485.                 case MVLC_SHUTDOWN:
  486.                 {
  487.                     msg_Dbg( p_intf, "requested shutdown" );
  488.                     libvlc_Quit( p_intf->p_libvlc );
  489.                     break;
  490.                 }
  491. #ifdef ENABLE_VLM
  492.                 /* vlm */
  493.                 case MVLC_VLM_NEW:
  494.                 case MVLC_VLM_SETUP:
  495.                 {
  496.                     static const char vlm_properties[][9] =
  497.                     {
  498.                         /* no args */
  499.                         "enabled", "disabled", "loop", "unloop",
  500.                         /* args required */
  501.                         "input", "output", "option", "date", "period",
  502.                         "repeat", "append", "",
  503.                     };
  504.                     vlm_message_t *vlm_answer;
  505.                     char name[512];
  506.                     char *psz = malloc( strlen( p_request ) + 1000 );
  507.                     char *p = psz;
  508.                     char *vlm_error;
  509.                     int i;
  510.                     if( p_intf->p_sys->p_vlm == NULL )
  511.                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
  512.                     if( p_intf->p_sys->p_vlm == NULL )
  513.                     {
  514.                         free( psz );
  515.                         break;
  516.                     }
  517.                     ExtractURIValue( p_request, "name", name, 512 );
  518.                     if( StrToMacroType( control ) == MVLC_VLM_NEW )
  519.                     {
  520.                         char type[20];
  521.                         ExtractURIValue( p_request, "type", type, 20 );
  522.                         p += sprintf( psz, "new %s %s", name, type );
  523.                     }
  524.                     else
  525.                     {
  526.                         p += sprintf( psz, "setup %s", name );
  527.                     }
  528.                     /* Parse the request */
  529.                     for( i = 0; vlm_properties[i][0]; i++ )
  530.                     {
  531.                         char val[512];
  532.                         ExtractURIValue( p_request,
  533.                                                vlm_properties[i], val, 512 );
  534.                         decode_URI( val );
  535.                         if( strlen( val ) > 0 && i >= 4 )
  536.                         {
  537.                             p += sprintf( p, " %s %s", vlm_properties[i], val );
  538.                         }
  539.                         else if( TestURIParam( p_request, vlm_properties[i] ) && i < 4 )
  540.                         {
  541.                             p += sprintf( p, " %s", vlm_properties[i] );
  542.                         }
  543.                     }
  544.                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
  545.                     if( vlm_answer->psz_value == NULL ) /* there is no error */
  546.                     {
  547.                         vlm_error = strdup( "" );
  548.                     }
  549.                     else
  550.                     {
  551.                         if( asprintf( &vlm_error , "%s : %s" ,
  552.                                       vlm_answer->psz_name,
  553.                                       vlm_answer->psz_value ) == -1 )
  554.                             vlm_error = NULL;
  555.                     }
  556.                     mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
  557.                     vlm_MessageDelete( vlm_answer );
  558.                     free( vlm_error );
  559.                     free( psz );
  560.                     break;
  561.                 }
  562.                 case MVLC_VLM_DEL:
  563.                 {
  564.                     vlm_message_t *vlm_answer;
  565.                     char name[512];
  566.                     char psz[512+10];
  567.                     if( p_intf->p_sys->p_vlm == NULL )
  568.                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
  569.                     if( p_intf->p_sys->p_vlm == NULL ) break;
  570.                     ExtractURIValue( p_request, "name", name, 512 );
  571.                     sprintf( psz, "del %s", name );
  572.                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
  573.                     /* FIXME do a vlm_answer -> var stack conversion */
  574.                     vlm_MessageDelete( vlm_answer );
  575.                     break;
  576.                 }
  577.                 case MVLC_VLM_PLAY:
  578.                 case MVLC_VLM_PAUSE:
  579.                 case MVLC_VLM_STOP:
  580.                 case MVLC_VLM_SEEK:
  581.                 {
  582.                     vlm_message_t *vlm_answer;
  583.                     char name[512];
  584.                     char psz[512+10];
  585.                     if( p_intf->p_sys->p_vlm == NULL )
  586.                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
  587.                     if( p_intf->p_sys->p_vlm == NULL ) break;
  588.                     ExtractURIValue( p_request, "name", name, 512 );
  589.                     if( StrToMacroType( control ) == MVLC_VLM_PLAY )
  590.                         sprintf( psz, "control %s play", name );
  591.                     else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
  592.                         sprintf( psz, "control %s pause", name );
  593.                     else if( StrToMacroType( control ) == MVLC_VLM_STOP )
  594.                         sprintf( psz, "control %s stop", name );
  595.                     else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
  596.                     {
  597.                         char percent[20];
  598.                         ExtractURIValue( p_request, "percent", percent, 512 );
  599.                         sprintf( psz, "control %s seek %s", name, percent );
  600.                     }
  601.                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
  602.                     /* FIXME do a vlm_answer -> var stack conversion */
  603.                     vlm_MessageDelete( vlm_answer );
  604.                     break;
  605.                 }
  606.                 case MVLC_VLM_LOAD:
  607.                 case MVLC_VLM_SAVE:
  608.                 {
  609.                     vlm_message_t *vlm_answer;
  610.                     char file[512];
  611.                     char psz[512];
  612.                     if( p_intf->p_sys->p_vlm == NULL )
  613.                         p_intf->p_sys->p_vlm = vlm_New( p_intf );
  614.                     if( p_intf->p_sys->p_vlm == NULL ) break;
  615.                     ExtractURIValue( p_request, "file", file, 512 );
  616.                     decode_URI( file );
  617.                     if( StrToMacroType( control ) == MVLC_VLM_LOAD )
  618.                         sprintf( psz, "load %s", file );
  619.                     else
  620.                         sprintf( psz, "save %s", file );
  621.                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
  622.                     /* FIXME do a vlm_answer -> var stack conversion */
  623.                     vlm_MessageDelete( vlm_answer );
  624.                     break;
  625.                 }
  626. #endif /* ENABLE_VLM */
  627.                 default:
  628.                     if( *control )
  629.                     {
  630.                         PRINTS( "<!-- control param(%s) unsupported -->", control );
  631.                     }
  632.                     break;
  633.             }
  634.             break;
  635.         case MVLC_SET:
  636.         {
  637.             char    value[512];
  638.             int     i;
  639.             float   f;
  640.             if( i_request <= 0 ||
  641.                 *m->param1  == '' ||
  642.                 strstr( p_request, m->param1 ) == NULL )
  643.             {
  644.                 break;
  645.             }
  646.             ExtractURIValue( p_request, m->param1,  value, 512 );
  647.             decode_URI( value );
  648.             switch( StrToMacroType( m->param2 ) )
  649.             {
  650.                 case MVLC_INT:
  651.                     i = atoi( value );
  652.                     config_PutInt( p_intf, m->param1, i );
  653.                     break;
  654.                 case MVLC_FLOAT:
  655.                     f = atof( value );
  656.                     config_PutFloat( p_intf, m->param1, f );
  657.                     break;
  658.                 case MVLC_STRING:
  659.                     config_PutPsz( p_intf, m->param1, value );
  660.                     break;
  661.                 default:
  662.                     PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
  663.             }
  664.             break;
  665.         }
  666.         case MVLC_GET:
  667.         {
  668.             char    value[512];
  669.             int     i;
  670.             float   f;
  671.             char    *psz;
  672.             lldiv_t div;
  673.             if( *m->param1  == '' )
  674.             {
  675.                 break;
  676.             }
  677.             switch( StrToMacroType( m->param2 ) )
  678.             {
  679.                 case MVLC_INT:
  680.                     i = config_GetInt( p_intf, m->param1 );
  681.                     sprintf( value, "%d", i );
  682.                     break;
  683.                 case MVLC_FLOAT:
  684.                     f = config_GetFloat( p_intf, m->param1 );
  685.                     div = lldiv( f * 1000000 , 1000000 );
  686.                     sprintf( value, "%lld.%06u", div.quot,
  687.                             (unsigned int)div.rem );
  688.                     break;
  689.                 case MVLC_STRING:
  690.                     psz = config_GetPsz( p_intf, m->param1 );
  691.                     if( psz != NULL )
  692.                     {
  693.                         strlcpy( value, psz,sizeof( value ) );
  694.                         free( psz );
  695.                     }
  696.                     else
  697.                         *value = '';
  698.                     msg_Dbg( p_intf, "%d: value = "%s"", __LINE__, value );
  699.                     break;
  700.                 default:
  701.                     snprintf( value, sizeof( value ),
  702.                               "invalid type(%s) in set", m->param2 );
  703.                     break;
  704.             }
  705.             PRINTS( "%s", value );
  706.             break;
  707.         }
  708.         case MVLC_VALUE:
  709.         {
  710.             char *s;
  711.             const char *v;
  712.             if( m->param1 )
  713.             {
  714.                 EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m->param1 );
  715.                 s = SSPop( &p_args->stack );
  716.                 v = mvar_GetValue( p_args->vars, s );
  717.             }
  718.             else
  719.             {
  720.                 v = s = SSPop( &p_args->stack );
  721.             }
  722.             PRINTS( "%s", v );
  723.             free( s );
  724.             break;
  725.         }
  726.         case MVLC_RPN:
  727.             EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m->param1 );
  728.             break;
  729.         /* Useful to learn stack management */
  730.         case MVLC_STACK:
  731.         {
  732.             int i;
  733.             msg_Dbg( p_intf, "stack" );
  734.             for (i=0;i<(&p_args->stack)->i_stack;i++)
  735.                 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
  736.             break;
  737.         }
  738.         case MVLC_UNKNOWN:
  739.         default:
  740.             PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
  741.             msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
  742.             break;
  743.     }
  744. #undef PRINTS
  745. #undef PRINT
  746. #undef ALLOC
  747. }
  748. static
  749. char *MacroSearch( char *src, char *end, int i_mvlc, bool b_after )
  750. {
  751.     int     i_id;
  752.     int     i_level = 0;
  753.     while( src < end )
  754.     {
  755.         if( src + 4 < end  && !strncmp( (char *)src, "<vlc", 4 ) )
  756.         {
  757.             int i_skip;
  758.             macro_t m;
  759.             i_skip = MacroParse( &m, src );
  760.             i_id = StrToMacroType( m.id );
  761.             switch( i_id )
  762.             {
  763.                 case MVLC_IF:
  764.                 case MVLC_FOREACH:
  765.                     i_level++;
  766.                     break;
  767.                 case MVLC_END:
  768.                     i_level--;
  769.                     break;
  770.                 default:
  771.                     break;
  772.             }
  773.             MacroClean( &m );
  774.             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
  775.                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
  776.             {
  777.                 return src + ( b_after ? i_skip : 0 );
  778.             }
  779.             else if( i_level < 0 )
  780.             {
  781.                 return NULL;
  782.             }
  783.             src += i_skip;
  784.         }
  785.         else
  786.         {
  787.             src++;
  788.         }
  789.     }
  790.     return NULL;
  791. }
  792. void Execute( httpd_file_sys_t *p_args,
  793.                      char *p_request, int i_request,
  794.                      char **pp_data, int *pi_data,
  795.                      char **pp_dst,
  796.                      char *_src, char *_end )
  797. {
  798.     intf_thread_t  *p_intf = p_args->p_intf;
  799.     char *src, *dup, *end;
  800.     char *dst = *pp_dst;
  801.     src = dup = malloc( _end - _src + 1 );
  802.     end = src +( _end - _src );
  803.     memcpy( src, _src, _end - _src );
  804.     *end = '';
  805.     /* we parse searching <vlc */
  806.     while( src < end )
  807.     {
  808.         char *p;
  809.         int i_copy;
  810.         p = (char *)strstr( (char *)src, "<vlc" );
  811.         if( p < end && p == src )
  812.         {
  813.             macro_t m;
  814.             src += MacroParse( &m, src );
  815.             //msg_Dbg( p_intf, "macro_id=%s", m.id );
  816.             switch( StrToMacroType( m.id ) )
  817.             {
  818.                 case MVLC_INCLUDE:
  819.                 {
  820.                     FILE *f;
  821.                     int  i_buffer;
  822.                     char *p_buffer;
  823.                     char psz_file[MAX_DIR_SIZE];
  824.                     char *p;
  825.                     char sep;
  826. #if defined( WIN32 )
  827.                     sep = '\';
  828. #else
  829.                     sep = '/';
  830. #endif
  831.                     if( m.param1[0] != sep )
  832.                     {
  833.                         strcpy( psz_file, p_args->file );
  834.                         p = strrchr( psz_file, sep );
  835.                         if( p != NULL )
  836.                             strcpy( p + 1, m.param1 );
  837.                         else
  838.                             strcpy( psz_file, m.param1 );
  839.                     }
  840.                     else
  841.                     {
  842.                         strcpy( psz_file, m.param1 );
  843.                     }
  844.                     /* We hereby assume that psz_file is in the
  845.                      * local character encoding */
  846.                     if( ( f = fopen( psz_file, "r" ) ) == NULL )
  847.                     {
  848.                         msg_Warn( p_args->p_intf,
  849.                                   "unable to include file %s (%m)",
  850.                                   psz_file );
  851.                         break;
  852.                     }
  853.                     /* first we load in a temporary buffer */
  854.                     FileLoad( f, &p_buffer, &i_buffer );
  855.                     /* we parse executing all  <vlc /> macros */
  856.                     Execute( p_args, p_request, i_request, pp_data, pi_data,
  857.                              &dst, &p_buffer[0], &p_buffer[i_buffer] );
  858.                     free( p_buffer );
  859.                     fclose(f);
  860.                     break;
  861.                 }
  862.                 case MVLC_IF:
  863.                 {
  864.                     bool i_test;
  865.                     char    *endif;
  866.                     EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m.param1 );
  867.                     if( SSPopN( &p_args->stack, p_args->vars ) )
  868.                     {
  869.                         i_test = 1;
  870.                     }
  871.                     else
  872.                     {
  873.                         i_test = 0;
  874.                     }
  875.                     endif = MacroSearch( src, end, MVLC_END, true );
  876.                     if( i_test == 0 )
  877.                     {
  878.                         char *start = MacroSearch( src, endif, MVLC_ELSE, true );
  879.                         if( start )
  880.                         {
  881.                             char *stop  = MacroSearch( start, endif, MVLC_END, false );
  882.                             if( stop )
  883.                             {
  884.                                 Execute( p_args, p_request, i_request,
  885.                                          pp_data, pi_data, &dst, start, stop );
  886.                             }
  887.                         }
  888.                     }
  889.                     else if( i_test == 1 )
  890.                     {
  891.                         char *stop;
  892.                         if( ( stop = MacroSearch( src, endif, MVLC_ELSE, false ) ) == NULL )
  893.                         {
  894.                             stop = MacroSearch( src, endif, MVLC_END, false );
  895.                         }
  896.                         if( stop )
  897.                         {
  898.                             Execute( p_args, p_request, i_request,
  899.                                      pp_data, pi_data, &dst, src, stop );
  900.                         }
  901.                     }
  902.                     src = endif;
  903.                     break;
  904.                 }
  905.                 case MVLC_FOREACH:
  906.                 {
  907.                     char *endfor = MacroSearch( src, end, MVLC_END, true );
  908.                     char *start = src;
  909.                     char *stop = MacroSearch( src, end, MVLC_END, false );
  910.                     if( stop )
  911.                     {
  912.                         mvar_t *index;
  913.                         int    i_idx;
  914.                         mvar_t *v;
  915.                         if( !strcmp( m.param2, "integer" ) )
  916.                         {
  917.                             char *arg = SSPop( &p_args->stack );
  918.                             index = mvar_IntegerSetNew( m.param1, arg );
  919.                             free( arg );
  920.                         }
  921.                         else if( !strcmp( m.param2, "directory" ) )
  922.                         {
  923.                             char *arg = SSPop( &p_args->stack );
  924.                             index = mvar_FileSetNew( p_intf, m.param1, arg );
  925.                             free( arg );
  926.                         }
  927.                         else if( !strcmp( m.param2, "object" ) )
  928.                         {
  929.                             char *arg = SSPop( &p_args->stack );
  930.                             index = mvar_ObjectSetNew( p_intf, m.param1, arg );
  931.                             free( arg );
  932.                         }
  933.                         else if( !strcmp( m.param2, "playlist" ) )
  934.                         {
  935.                             index = mvar_PlaylistSetNew( p_intf, m.param1,
  936.                                                     p_intf->p_sys->p_playlist );
  937.                         }
  938.                         else if( !strcmp( m.param2, "information" ) )
  939.                         {
  940.                             index = mvar_InfoSetNew( m.param1,
  941.                                                      p_intf->p_sys->p_input );
  942.                         }
  943.                         else if( !strcmp( m.param2, "program" )
  944.                                   || !strcmp( m.param2, "title" )
  945.                                   || !strcmp( m.param2, "chapter" )
  946.                                   || !strcmp( m.param2, "audio-es" )
  947.                                   || !strcmp( m.param2, "video-es" )
  948.                                   || !strcmp( m.param2, "spu-es" ) )
  949.                         {
  950.                             index = mvar_InputVarSetNew( p_intf, m.param1,
  951.                                                          p_intf->p_sys->p_input,
  952.                                                          m.param2 );
  953.                         }
  954. #ifdef ENABLE_VLM
  955.                         else if( !strcmp( m.param2, "vlm" ) )
  956.                         {
  957.                             if( p_intf->p_sys->p_vlm == NULL )
  958.                                 p_intf->p_sys->p_vlm = vlm_New( p_intf );
  959.                             index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
  960.                         }
  961. #endif
  962. #if 0
  963.                         else if( !strcmp( m.param2, "hosts" ) )
  964.                         {
  965.                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
  966.                         }
  967.                         else if( !strcmp( m.param2, "urls" ) )
  968.                         {
  969.                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
  970.                         }
  971.                         else if( !strcmp( m.param2, "connections" ) )
  972.                         {
  973.                             index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
  974.                         }
  975. #endif
  976.                         else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
  977.                         {
  978.                             index = mvar_Duplicate( v );
  979.                         }
  980.                         else
  981.                         {
  982.                             msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
  983.                             src = endfor;
  984.                             break;
  985.                         }
  986.                         for( i_idx = 0; i_idx < index->i_field; i_idx++ )
  987.                         {
  988.                             mvar_t *f = mvar_Duplicate( index->field[i_idx] );
  989.                             //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
  990.                             free( f->name );
  991.                             f->name = strdup( m.param1 );
  992.                             mvar_PushVar( p_args->vars, f );
  993.                             Execute( p_args, p_request, i_request,
  994.                                      pp_data, pi_data, &dst, start, stop );
  995.                             mvar_RemoveVar( p_args->vars, f );
  996.                             mvar_Delete( f );
  997.                         }
  998.                         mvar_Delete( index );
  999.                         src = endfor;
  1000.                     }
  1001.                     break;
  1002.                 }
  1003.                 default:
  1004.                     MacroDo( p_args, &m, p_request, i_request,
  1005.                              pp_data, pi_data, &dst );
  1006.                     break;
  1007.             }
  1008.             MacroClean( &m );
  1009.             continue;
  1010.         }
  1011.         i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
  1012.         if( i_copy > 0 )
  1013.         {
  1014.             int i_index = dst - *pp_data;
  1015.             *pi_data += i_copy;
  1016.             *pp_data = realloc( *pp_data, *pi_data );
  1017.             dst = (*pp_data) + i_index;
  1018.             memcpy( dst, src, i_copy );
  1019.             dst += i_copy;
  1020.             src += i_copy;
  1021.         }
  1022.     }
  1023.     *pp_dst = dst;
  1024.     free( dup );
  1025. }