+/*
+ * url syntax: [repo ['/' cmd [ '/' path]]]
+ * repo: any valid repo url, may contain '/'
+ * cmd: log | commit | diff | tree | view | blob | snapshot
+ * path: any valid path, may contain '/'
+ *
+ */
+void cgit_parse_url(const char *url)
+{
+ char *cmd, *p;
+
+ cgit_repo = NULL;
+ if (!url || url[0] == '\0')
+ return;
+
+ cgit_repo = cgit_get_repoinfo(url);
+ if (cgit_repo) {
+ cgit_query_repo = cgit_repo->url;
+ return;
+ }
+
+ cmd = strchr(url, '/');
+ while (!cgit_repo && cmd) {
+ cmd[0] = '\0';
+ cgit_repo = cgit_get_repoinfo(url);
+ if (cgit_repo == NULL) {
+ cmd[0] = '/';
+ cmd = strchr(cmd + 1, '/');
+ continue;
+ }
+
+ cgit_query_repo = cgit_repo->url;
+ p = strchr(cmd + 1, '/');
+ if (p) {
+ p[0] = '\0';
+ if (p[1])
+ cgit_query_path = trim_end(p + 1, '/');
+ }
+ cgit_cmd = cgit_get_cmd_index(cmd + 1);
+ cgit_query_page = xstrdup(cmd + 1);
+ return;
+ }
+}
+
+static char *iconv_msg(char *msg, const char *encoding)
+{
+ iconv_t msg_conv = iconv_open(PAGE_ENCODING, encoding);
+ size_t inlen = strlen(msg);
+ char *in;
+ char *out;
+ size_t inleft;
+ size_t outleft;
+ char *buf;
+ char *ret;
+ size_t buf_sz;
+ int again, fail;
+
+ if(msg_conv == (iconv_t)-1)
+ return NULL;
+
+ buf_sz = inlen * 2;
+ buf = xmalloc(buf_sz+1);
+ do {
+ in = msg;
+ inleft = inlen;
+
+ out = buf;
+ outleft = buf_sz;
+ iconv(msg_conv, &in, &inleft, &out, &outleft);
+
+ if(inleft == 0) {
+ fail = 0;
+ again = 0;
+ } else if(inleft != 0 && errno == E2BIG) {
+ fail = 0;
+ again = 1;
+
+ buf_sz *= 2;
+ free(buf);
+ buf = xmalloc(buf_sz+1);
+ } else {
+ fail = 1;
+ again = 0;
+ }
+ } while(again && !fail);
+
+ if(fail) {
+ free(buf);
+ ret = NULL;
+ } else {
+ buf = xrealloc(buf, out - buf);
+ *out = 0;
+ ret = buf;
+ }
+
+ iconv_close(msg_conv);
+
+ return ret;
+}
+