app-manager --update and --setup are documented to take URIs but they are not treated as such
2 * Copyright (C) 2020 J. Ali Harlow <ali@juiblex.co.uk>
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.
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.
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.
23 #include <plover/plover.h>
24 #include "pre-inst/post.h"
26 #define PLOVER_XML_NS "http://project.juiblex.co.uk/plover/ns/2009"
32 POST_STATE_INSTALL_PREFIX,
33 POST_STATE_REPOSITORY,
38 enum post_state state;
44 static void post_start_element(void *data,const char *name,const char **atts)
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"))
55 ctx->state=POST_STATE_ARGUMENT;
58 ctx->str=g_ptr_array_index(ctx->args,0);
59 g_string_truncate(ctx->str,0);
63 ctx->str=g_string_new(NULL);
64 g_ptr_array_add(ctx->args,ctx->str);
67 else if (ctx->state==POST_STATE_ROOT &&
68 !strcmp(name,PLOVER_XML_NS "\xFF" "argument"))
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);
76 else if (ctx->state==POST_STATE_ARGUMENT)
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;
83 ctx->unknown_elements++;
86 ctx->unknown_elements++;
89 static void post_end_element(void *data,const char *name)
91 struct post_context *ctx=data;
92 if (ctx->unknown_elements)
93 ctx->unknown_elements--;
98 ctx->state=POST_STATE_BEGIN;
100 case POST_STATE_ARGUMENT:
101 ctx->state=POST_STATE_ROOT;
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;
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;
116 static void post_character_data(void *data,const XML_Char *s,int len)
118 struct post_context *ctx=data;
121 case POST_STATE_ARGUMENT:
122 g_string_append_len(ctx->str,s,len);
127 struct post *pre_install_post_new(const char *repository,
128 const char *install_prefix)
131 post=g_new0(struct post,1);
132 post->repository=g_strdup(repository);
133 post->install_prefix=g_strdup(install_prefix);
137 void pre_install_post_free(struct post *post)
139 g_free(post->repository);
140 g_free(post->install_prefix);
144 gboolean pre_install_post_load_uri(struct post *post,const char *uri,
149 struct post_context ctx={0,};
150 struct razor_error *tmp_error=NULL;
154 contents=razor_uri_get_contents(uri,&length,FALSE,&tmp_error);
157 plover_propagate_razor_error(error,tmp_error);
160 parser=XML_ParserCreateNS(NULL,'\xFF');
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;
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);
177 if (ctx.args->len>0 && !g_ptr_array_index(ctx.args,0))
178 g_warning("post: ignoring arguments because no command specified");
180 g_strfreev(post->argv);
181 if (ctx.args->len>0 && g_ptr_array_index(ctx.args,0))
183 post->argc=ctx.args->len;
184 post->argv=g_new(gchar *,post->argc+1);
185 for(i=0;i<post->argc;i++)
187 g_string_free(g_ptr_array_index(ctx.args,i),FALSE);
193 post->argv=g_new0(gchar *,1);
195 g_ptr_array_free(ctx.args,FALSE);