Imported Upstream version 0.6.13
[platform/upstream/libsolv.git] / examples / solv / repoinfo_config_urpmi.c
1 #if defined(MANDRIVA) || defined(MAGEIA)
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <dirent.h>
7
8 #include "pool.h"
9 #include "repo.h"
10
11 #include "repoinfo.h"
12 #include "repoinfo_config_urpmi.h"
13
14
15 #define URPMI_CFG "/etc/urpmi/urpmi.cfg"
16
17
18 struct repoinfo *
19 read_repoinfos_urpmi(Pool *pool, int *nrepoinfosp)
20 {
21   char buf[4096], *bp, *arg;
22   FILE *fp;
23   int l, insect = 0;
24   struct repoinfo *cinfo = 0;
25   struct repoinfo *repoinfos = 0;
26   int nrepoinfos = 0;
27
28   if ((fp = fopen(URPMI_CFG, "r")) == 0)
29     {
30       *nrepoinfosp = 0;
31       return 0;
32     }
33   while (fgets(buf, sizeof(buf), fp))
34     {
35       l = strlen(buf);
36       while (l && (buf[l - 1] == '\n' || buf[l - 1] == ' ' || buf[l - 1] == '\t'))
37         buf[--l] = 0;
38       bp = buf;
39       while (l && (*bp == ' ' || *bp == '\t'))
40         {
41           l--;
42           bp++;
43         }
44       if (!l || *bp == '#')
45         continue;
46       if (!insect && bp[l - 1] == '{')
47         {
48           insect++;
49           bp[--l] = 0;
50           if (l > 0)
51             {
52               while (l && (bp[l - 1] == ' ' || bp[l - 1] == '\t'))
53                 bp[--l] = 0;
54             }
55           if (l)
56             {
57               char *bbp = bp, *bbp2 = bp;
58               /* unescape */
59               while (*bbp)
60                 {
61                   if (*bbp == '\\' && bbp[1])
62                     bbp++;
63                   *bbp2++ = *bbp++;
64                 }
65               *bbp2 = 0;
66               repoinfos = solv_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
67               cinfo = repoinfos + nrepoinfos++;
68               memset(cinfo, 0, sizeof(*cinfo));
69               cinfo->alias = strdup(bp);
70               cinfo->type = TYPE_MDK;
71               cinfo->autorefresh = 1;
72               cinfo->priority = 99;
73               cinfo->enabled = 1;
74               cinfo->metadata_expire = METADATA_EXPIRE;
75             }
76           continue;
77         }
78       if (insect && *bp == '}')
79         {
80           insect--;
81           cinfo = 0;
82           continue;
83         }
84       if (!insect || !cinfo)
85         continue;
86       if ((arg = strchr(bp, ':')) != 0)
87         {
88           *arg++ = 0;
89           while (*arg == ' ' || *arg == '\t')
90             arg++;
91           if (!*arg)
92             arg = 0;
93         }
94       if (strcmp(bp, "ignore") == 0)
95         cinfo->enabled = 0;
96       if (strcmp(bp, "mirrorlist") == 0)
97         cinfo->mirrorlist = solv_strdup(arg);
98       if (strcmp(bp, "with-dir") == 0)
99         cinfo->path = solv_strdup(arg);
100     }
101   fclose(fp);
102   *nrepoinfosp = nrepoinfos;
103   return repoinfos;
104 }
105
106 #endif