Add multi-user support
[apps/core/preloaded/print-service.git] / TC / getppd.c
1 /*
2 *       Printservice
3 *
4 * Copyright 2013  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://floralicense.org/license/
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <getppd.h>
23
24 static char **gargv;
25 static pt_db *database;
26
27 void help(void)
28 {
29         fprintf(stderr, "Extract ppd file by model name:\n");
30         fprintf(stderr, "Usage: %s -m <model> -i <database>\n\n", gargv[0]);
31         fprintf(stderr, "Extract ppd file by product name:\n");
32         fprintf(stderr, "Usage: %s -p <product> -i <database>\n\n", gargv[0]);
33         fprintf(stderr, "Extract ppd file either by model name or product name:\n");
34         fprintf(stderr, "Usage: %s -a <key> -i <database>\n", gargv[0]);
35 }
36
37 void clean_all_resources(void)
38 {
39         pt_free_db(database);
40 }
41
42 config *initialization(int argc, char **argv)
43 {
44         int opt;
45         bool product = false;
46         bool model = false;
47         bool drvfile = false;
48
49         int res = atexit(clean_all_resources);
50         if(res) {
51                 perror("atexit() failed");
52         }
53
54         config *cfg = malloc(sizeof(config));
55         if (!cfg) {
56                 fprintf(stderr, "Can't allocate memory\n");
57                 exit(EXIT_FAILURE);
58         }
59         bzero(cfg, sizeof(config));
60
61         while ((opt = getopt(argc, argv, "i:m:p:a:r:")) != -1) {
62                 switch (opt) {
63                 case 'i':
64                         if (!drvfile) {
65                                 cfg->drvstr = strdup(optarg);
66                                 if (!cfg->drvstr) {
67                                         fprintf(stderr, "Can't duplicate string with strdup()\n");
68                                         exit(EXIT_FAILURE);
69                                 }
70                                 drvfile = true;
71                         } else {
72                                 fprintf(stderr, "Don't support extraction from multiple databases\n");
73                                 fprintf(stderr, "Do not provide some \'-i\'' options\n");
74                                 exit(EXIT_FAILURE);
75                         }
76                         break;
77                 case 'p':
78                         if (!product) {
79                                 cfg->product = strdup(optarg);
80                                 if (!cfg->product)
81                                 {
82                                         fprintf(stderr, "Can't duplicate string with strdup()\n");
83                                         exit(EXIT_FAILURE);
84                                 }
85                                 product = true;
86                         } else {
87                                 fprintf(stderr, "Supported extraction only of one ppd file by product\n");
88                                 fprintf(stderr, "Do not provide some \'-p\'' options\n");
89                                 exit(EXIT_FAILURE);
90                         }
91                         break;
92                 case 'm':
93                         if (!model) {
94                                 cfg->model = strdup(optarg);
95                                 if (!cfg->model)
96                                 {
97                                         fprintf(stderr, "Can't duplicate string with strdup()\n");
98                                         exit(EXIT_FAILURE);
99                                 }
100                                 model = true;
101                         } else {
102                                 fprintf(stderr, "Supported extraction only of one ppd file by modelname\n");
103                                 fprintf(stderr, "Do not provide some \'-m\'' options\n");
104                                 exit(EXIT_FAILURE);
105                         }
106                         break;
107                 case 'r':
108                 case 'a':
109                         if (!model) {
110                                 cfg->model = strdup(optarg);
111                                 if (!cfg->model) {
112                                         fprintf(stderr, "Can't allocate memory\n");
113                                         exit(EXIT_FAILURE);
114                                 }
115                                 model = true;
116                         } else {
117                                 fprintf(stderr, "Supported extraction only of one ppd file by modelname\n");
118                                 fprintf(stderr, "Do not provide some \'-m\'' options\n");
119                                 exit(EXIT_FAILURE);
120                         }
121
122                         if (!product) {
123                                 cfg->product = strdup(optarg);
124                                 if (!cfg->product) {
125                                         fprintf(stderr, "Can't duplicate string with strdup()\n");
126                                         exit(EXIT_FAILURE);
127                                 }
128                                 product = true;
129                         } else {
130                                 fprintf(stderr, "Supported extraction only of one ppd file by product\n");
131                                 fprintf(stderr, "Do not provide some \'-p\'' options\n");
132                                 exit(EXIT_FAILURE);
133                         }
134                         break;
135                 default:                                /* '?' */
136                         help();
137                         exit(EXIT_FAILURE);
138                 }
139         }
140
141         cfg->find_product = product;
142         cfg->find_model = model;
143
144         if (!cfg->drvstr) {
145                 fprintf(stderr, "drv file is not provided\n");
146                 exit(EXIT_FAILURE);
147         }
148
149         return cfg;
150 }
151
152 int main(int argc, char **argv)
153 {
154         gargv = argv;
155         config *cfg = initialization(argc, argv);
156         if (!cfg) {
157                 fprintf(stderr, "Unexpected error\n");
158                 exit(EXIT_FAILURE);
159         }
160
161         if(!cfg->drvstr) {
162                 fprintf(stderr, "drvstr is NULL\n");
163                 exit(EXIT_FAILURE);
164         }
165
166         database = pt_create_db(cfg->drvstr);
167         if(!database) {
168                 fprintf(stderr, "database is NULL\n");
169                 exit(EXIT_FAILURE);
170         }
171
172         char *output = NULL;
173
174         if (cfg->find_model && !cfg->find_product) {
175                 output = pt_extract_ppd(database, cfg->model, PT_SEARCH_MODEL);
176                 if(!output) {
177                         fprintf(stderr, "Can't find suitable ppd file by model\n");
178                         exit(EXIT_FAILURE);
179                 }
180         } else if (cfg->find_product && !cfg->find_model) {
181                 output = pt_extract_ppd(database, cfg->product, PT_SEARCH_PROD);
182                 if(!output) {
183                         fprintf(stderr, "Can't find suitable ppd file by product\n");
184                         exit(EXIT_FAILURE);
185                 }
186         } else if (cfg->find_model && cfg->find_product) {
187                 if (strcmp(cfg->product, cfg->model)) {
188                         fprintf(stderr, "Supported extraction only of one ppd file\n");
189                         fprintf(stderr, "Do not provide mixed \'-m\',\'-p\',\'-a\' with different names\n");
190                         exit(EXIT_FAILURE);
191                 }
192
193                 output = pt_extract_ppd(database, cfg->model, PT_SEARCH_MODEL);
194                 if (!output) {
195                         output = pt_extract_ppd(database, cfg->product, PT_SEARCH_PROD);
196                         if(!output) {
197                                 fprintf(stderr, "Can't find suitable ppd file\n");
198                                 exit(EXIT_FAILURE);
199                         }
200                 }
201         } else {
202                 help();
203                 exit(EXIT_FAILURE);
204         }
205
206         printf("%s", output);
207         exit(EXIT_SUCCESS);
208 }