pre-inst/post.c
author J. Ali Harlow <ali@juiblex.co.uk>
Wed Jul 15 16:04:47 2020 +0100 (2020-07-15)
changeset 98 71127797ca73
child 99 0121592e2512
permissions -rw-r--r--
app-manager --update and --setup are documented to take URIs but they are not treated as such
     1 /*
     2  * Copyright (C) 2020  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include <string.h>
    20 #include <glib.h>
    21 #include <expat.h>
    22 #include <razor.h>
    23 #include <plover/plover.h>
    24 #include "pre-inst/post.h"
    25 
    26 #define PLOVER_XML_NS "http://project.juiblex.co.uk/plover/ns/2009"
    27 
    28 enum post_state {
    29     POST_STATE_BEGIN,
    30     POST_STATE_ROOT,
    31     POST_STATE_ARGUMENT,
    32     POST_STATE_INSTALL_PREFIX,
    33     POST_STATE_REPOSITORY,
    34 };
    35 
    36 struct post_context {
    37     struct post *post;
    38     enum post_state state;
    39     int unknown_elements;
    40     GPtrArray *args;
    41     GString *str;
    42 };
    43 
    44 static void post_start_element(void *data,const char *name,const char **atts)
    45 {
    46     struct post_context *ctx=data;
    47     if (ctx->unknown_elements)
    48 	ctx->unknown_elements++;
    49     else if (ctx->state==POST_STATE_BEGIN &&
    50       !strcmp(name,PLOVER_XML_NS "\xFF" "post"))
    51 	ctx->state=POST_STATE_ROOT;
    52     else if (ctx->state==POST_STATE_ROOT &&
    53       !strcmp(name,PLOVER_XML_NS "\xFF" "command"))
    54     {
    55 	ctx->state=POST_STATE_ARGUMENT;
    56 	if (ctx->args->len>0)
    57 	{
    58 	    ctx->str=g_ptr_array_index(ctx->args,0);
    59 	    g_string_truncate(ctx->str,0);
    60 	}
    61 	else
    62 	{
    63 	    ctx->str=g_string_new(NULL);
    64 	    g_ptr_array_add(ctx->args,ctx->str);
    65 	}
    66     }
    67     else if (ctx->state==POST_STATE_ROOT &&
    68       !strcmp(name,PLOVER_XML_NS "\xFF" "argument"))
    69     {
    70 	ctx->state=POST_STATE_ARGUMENT;
    71 	ctx->str=g_string_new(NULL);
    72 	if (ctx->args->len==0)
    73 	    g_ptr_array_add(ctx->args,NULL);
    74 	g_ptr_array_add(ctx->args,ctx->str);
    75     }
    76     else if (ctx->state==POST_STATE_ARGUMENT)
    77     {
    78 	if (!strcmp(name,PLOVER_XML_NS "\xFF" "install-prefix"))
    79 	    ctx->state=POST_STATE_INSTALL_PREFIX;
    80 	else if (!strcmp(name,PLOVER_XML_NS "\xFF" "repository"))
    81 	    ctx->state=POST_STATE_REPOSITORY;
    82 	else
    83 	    ctx->unknown_elements++;
    84     }
    85     else
    86 	ctx->unknown_elements++;
    87 }
    88 
    89 static void post_end_element(void *data,const char *name)
    90 {
    91     struct post_context *ctx=data;
    92     if (ctx->unknown_elements)
    93 	ctx->unknown_elements--;
    94     else
    95 	switch (ctx->state)
    96 	{
    97 	    case POST_STATE_ROOT:
    98 		ctx->state=POST_STATE_BEGIN;
    99 		break;
   100 	    case POST_STATE_ARGUMENT:
   101 		ctx->state=POST_STATE_ROOT;
   102 		break;
   103 	    case POST_STATE_INSTALL_PREFIX:
   104 		if (ctx->post->install_prefix)
   105 		    g_string_append(ctx->str,ctx->post->install_prefix);
   106 		ctx->state=POST_STATE_ARGUMENT;
   107 		break;
   108 	    case POST_STATE_REPOSITORY:
   109 		if (ctx->post->repository)
   110 		    g_string_append(ctx->str,ctx->post->repository);
   111 		ctx->state=POST_STATE_ARGUMENT;
   112 		break;
   113 	}
   114 }
   115 
   116 static void post_character_data(void *data,const XML_Char *s,int len)
   117 {
   118     struct post_context *ctx=data;
   119     switch (ctx->state)
   120     {
   121 	case POST_STATE_ARGUMENT:
   122 	    g_string_append_len(ctx->str,s,len);
   123 	    break;
   124     }
   125 }
   126 
   127 struct post *pre_install_post_new(const char *repository,
   128   const char *install_prefix)
   129 {
   130     struct post *post;
   131     post=g_new0(struct post,1);
   132     post->repository=g_strdup(repository);
   133     post->install_prefix=g_strdup(install_prefix);
   134     return post;
   135 }
   136 
   137 void pre_install_post_free(struct post *post)
   138 {
   139     g_free(post->repository);
   140     g_free(post->install_prefix);
   141     g_free(post);
   142 }
   143 
   144 gboolean pre_install_post_load_uri(struct post *post,const char *uri,
   145   GError **error)
   146 {
   147     int i;
   148     gboolean retval;
   149     struct post_context ctx={0,};
   150     struct razor_error *tmp_error=NULL;
   151     size_t length;
   152     void *contents;
   153     XML_Parser parser;
   154     contents=razor_uri_get_contents(uri,&length,FALSE,&tmp_error);
   155     if (!contents)
   156     {
   157 	plover_propagate_razor_error(error,tmp_error);
   158 	return FALSE;
   159     }
   160     parser=XML_ParserCreateNS(NULL,'\xFF');
   161     ctx.post=post;
   162     ctx.state=POST_STATE_BEGIN;
   163     ctx.args=g_ptr_array_new();
   164     XML_SetUserData(parser,&ctx);
   165     XML_SetElementHandler(parser,post_start_element,post_end_element);
   166     XML_SetCharacterDataHandler(parser,post_character_data);
   167     retval=XML_Parse(parser,contents,length,TRUE)!=XML_STATUS_ERROR;
   168     if (!retval)
   169 	g_set_error(error,PLOVER_GENERAL_ERROR,PLOVER_GENERAL_ERROR_FAILED,
   170 	  "%s on line %d of '%s'\n",
   171 	  XML_ErrorString(XML_GetErrorCode(parser)),
   172 	  XML_GetCurrentLineNumber(parser),uri);
   173     XML_ParserFree(parser);
   174     razor_uri_free_contents(contents,length);
   175     if (retval)
   176     {
   177 	if (ctx.args->len>0 && !g_ptr_array_index(ctx.args,0))
   178 	    g_warning("post: ignoring arguments because no command specified");
   179 	if (post->argv)
   180 	    g_strfreev(post->argv);
   181 	if (ctx.args->len>0 && g_ptr_array_index(ctx.args,0))
   182 	{
   183 	    post->argc=ctx.args->len;
   184 	    post->argv=g_new(gchar *,post->argc+1);
   185 	    for(i=0;i<post->argc;i++)
   186 		post->argv[i]=
   187 		  g_string_free(g_ptr_array_index(ctx.args,i),FALSE);
   188 	    post->argv[i]=NULL;
   189 	}
   190 	else
   191 	{
   192 	    post->argc=0;
   193 	    post->argv=g_new0(gchar *,1);
   194 	}
   195 	g_ptr_array_free(ctx.args,FALSE);
   196     }
   197     return retval;
   198 }