upload tizen1.0 source
[pkgs/n/native-installer.git] / backend / src / parser / nativeinstallermanifestparser.c
1 /*
2  * rpm-installer
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24
25
26
27 #include <regex.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "nativeinstaller.h"
33 #include "native_installer_util.h"
34
35 static int __pkgmgr_txt_parser_fill_pkginfo(pkginfo *info, char *line);
36
37 char evt_notification[64] = { 0, };
38
39 static int __pkgmgr_txt_parser_fill_pkginfo(pkginfo *info, char *line)
40 {
41         regex_t re;
42         regmatch_t mached[5];
43         int ret = 0;
44         char *retstr;
45
46         ret = regcomp(&re, "[Pp]ackage[\\ ]*:[\\ ]*([.A-Z0-9a-z_\\-]+)",
47                       REG_EXTENDED);
48         if (ret != 0) {
49                 d_msg_backend(DEBUG_ERR, "[fillPkgInfo]regcomp failed(%d)",
50                               ret);
51                 return -1;
52         }
53         ret = regexec(&re, line, 5, mached, 0);
54         if (ret != 0)
55                 regfree(&re);
56         else {
57                 retstr = _substring(line, mached[1].rm_so,
58                                     mached[1].rm_eo - mached[1].rm_so);
59                 d_msg_backend(DEBUG_ERR, "[fillPkgInfo]PackageName=%s\n",
60                               retstr);
61                 strncpy(info->package_name, retstr,
62                         sizeof(info->package_name) - 1);
63                 free(retstr);
64                 regfree(&re);
65                 return 1;
66         }
67
68         ret = regcomp(&re,
69                       "Version[\\ ]*:[\\ ]*([0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3})",
70                       REG_EXTENDED);
71         if (ret != 0) {
72                 d_msg_backend(DEBUG_ERR, "[fillPkgInfo]regcomp failed(%d)",
73                               ret);
74                 return -1;
75         }
76         ret = regexec(&re, line, 5, mached, 0);
77         if (ret != 0)
78                 regfree(&re);
79         else {
80                 retstr = _substring(line, mached[1].rm_so,
81                                     mached[1].rm_eo - mached[1].rm_so);
82                 d_msg_backend(DEBUG_INFO, "[fillPkgInfo]Version=%s\n", retstr);
83                 strncpy(info->version, retstr, sizeof(info->version) - 1);
84                 regfree(&re);
85                 free(retstr);
86                 return 1;
87         }
88
89         ret = regcomp(&re,
90                       "[Aa]pp[Ss]etting[Nn]otification[\\ ]*:[\\ ]*([.A-Z0-9a-z_\\-]+)",
91                       REG_EXTENDED);
92         if (ret != 0) {
93                 d_msg_backend(DEBUG_ERR, "[fillPkgInfo]regcomp failed(%d)",
94                               ret);
95                 return -1;
96         }
97         ret = regexec(&re, line, 5, mached, 0);
98         if (ret != 0)
99                 regfree(&re);
100         else {
101                 retstr = _substring(line, mached[1].rm_so,
102                                     mached[1].rm_eo - mached[1].rm_so);
103                 d_msg_backend(DEBUG_ERR,
104                               "[fillPkgInfo]AppSettingNotification=%s\n",
105                               retstr);
106                 strncpy(evt_notification, retstr, strlen(retstr));
107                 free(retstr);
108                 regfree(&re);
109                 return 1;
110         }
111         return 0;
112 }
113
114 pkginfo *_pkgmgr_txt_parser_read_manifest(char *manifestfile)
115 {
116         FILE *fp1;
117         char line[512];
118         char *c;
119         pkginfo *info = malloc(sizeof(pkginfo));
120         if (info == NULL)
121                 return NULL;
122         memset(info, 0, sizeof(pkginfo));
123
124         fp1 = fopen(manifestfile, "r");
125         if (fp1 == NULL) {
126                 d_msg_backend(DEBUG_ERR, "failed to open file(%s)\n",
127                               manifestfile);
128                 if (info) {
129                         free(info);
130                         info = NULL;
131                 }
132                 return NULL;
133         }
134
135         do {
136                 c = fgets(line, 512, fp1);
137                 if (c != NULL) {
138                         __pkgmgr_txt_parser_fill_pkginfo(info, line);
139                 }
140         } while (c != NULL);
141
142         fclose(fp1);
143         return info;
144 }
145