summaryrefslogtreecommitdiffstats
path: root/libopkg/pkg_parse.c
blob: 9b43754e04a6e51dcb305f4a6dc5a65c6cf91703 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* pkg_parse.c - the opkg package management system

   Steven M. Ayer
   
   Copyright (C) 2002 Compaq Computer Corporation

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
*/

#include "includes.h"
#include <errno.h>
#include <ctype.h>
   
#include "pkg.h"
#include "opkg_utils.h"
#include "pkg_parse.h"

int isGenericFieldType(char * type, char * line)
{
    if(!strncmp(line, type, strlen(type)))
	return 1;
    return 0;
}

char * parseGenericFieldType(char * type, char * raw)
{
    char * field_value = raw + (strlen(type) + 1);
    return trim_alloc(field_value);
}

void parseStatus(pkg_t *pkg, char * raw)
{
    char sw_str[64], sf_str[64], ss_str[64];

    sscanf(raw, "Status: %s %s %s", sw_str, sf_str, ss_str);
    pkg->state_want = pkg_state_want_from_str(sw_str);
    pkg->state_flag = pkg_state_flag_from_str(sf_str);
    pkg->state_status = pkg_state_status_from_str(ss_str);
}

char ** parseDependsString(char * raw, int * depends_count)
{
    char ** depends = NULL;
    int line_count = 0;
    char buff[2048], * dest;

    while(raw && *raw && !isspace(*raw)) {
	raw++;
    }

    if(line_is_blank(raw)){
	*depends_count = line_count;
	return NULL;
    }
    while(raw && *raw){
	depends = (char **)realloc(depends, sizeof(char *) * (line_count + 1));
	
	while(isspace(*raw)) raw++;

	dest = buff;
	while((*raw != ',') && *raw)
	    *dest++ = *raw++;

	*dest = '\0';
	depends[line_count] = trim_alloc(buff);
	if(depends[line_count] ==NULL)
	   return NULL;
        line_count++;
	if(*raw == ',')
	    raw++;
    }
    *depends_count = line_count;
    return depends;
}

void parseConffiles(pkg_t * pkg, char * raw)
{
    char file_name[1048], md5sum[1048];  /* please tell me there aren't any longer that 1k */

    if(!strncmp(raw, "Conffiles:", 10))
	raw += strlen("Conffiles:");

    while(*raw && (sscanf(raw, "%s%s", file_name, md5sum) == 2)){
	conffile_list_append(&pkg->conffiles, file_name, md5sum);
	/*	fprintf(stderr, "%s %s ", file_name, md5sum);*/
	while (*raw && isspace(*raw)) {
	    raw++;
	}
	raw += strlen(file_name);
	while (*raw && isspace(*raw)) {
	    raw++;
	}
	raw += strlen(md5sum);
    }
}    

int parseVersion(pkg_t *pkg, char *raw)
{
  char *colon, *eepochcolon;
  char *hyphen;
  unsigned long epoch;

  if (!*raw) {
      fprintf(stderr, "%s: ERROR: version string is empty", __FUNCTION__);
      return EINVAL;
  }

  if (strncmp(raw, "Version:", 8) == 0) {
      raw += 8;
  }
  while (*raw && isspace(*raw)) {
      raw++;
  }
  
  colon= strchr(raw,':');
  if (colon) {
    epoch= strtoul(raw,&eepochcolon,10);
    if (colon != eepochcolon) {
	fprintf(stderr, "%s: ERROR: epoch in version is not number", __FUNCTION__);
	return EINVAL;
    }
    if (!*++colon) {
	fprintf(stderr, "%s: ERROR: nothing after colon in version number", __FUNCTION__);
	return EINVAL;
    }
    raw= colon;
    pkg->epoch= epoch;
  } else {
    pkg->epoch= 0;
  }

  pkg->revision = "";

  if (!pkg->version)
  {
  pkg->version= malloc(strlen(raw)+1);
  if ( pkg->version == NULL ) {
     fprintf(stderr, "%s: out of memory \n", __FUNCTION__);
     return ENOMEM;
  }
  strcpy(pkg->version, raw);
  }

  hyphen= strrchr(pkg->version,'-');

  if (hyphen) {
    *hyphen++= 0;
      pkg->revision = hyphen;
  }

  return 0;
}


/* This code is needed to insert in first position the keyword for the aligning bug */

int alterProvidesLine(char *raw, char *temp)
{


  if (!*raw) {
      fprintf(stderr, "%s: ERROR: Provides string is empty", __FUNCTION__);
      return -EINVAL;
  }

  if ( temp == NULL ) {
     fprintf(stderr, "%s: out of memory \n", __FUNCTION__);
     return -ENOMEM;
  }

  if (strncmp(raw, "Provides:", 9) == 0) {
      raw += 9;
  }
  while (*raw && isspace(*raw)) {
      raw++;
  }      
  
  snprintf ( temp, 35, "Provides: opkg_internal_use_only, ");           /* First part of the line */
  while (*raw) {
     strncat( temp, raw++, 1);
  }
  return 0;
 
}

/* Some random thoughts from Carl:

   This function could be considerably simplified if we just kept
   an array of all the generic string-valued field names, and looped
   through those looking for a match. Also, these fields could perhaps
   be stored in the package as an array as well, (or, probably better,
   as an nv_pair_list_t).

   Fields which require special parsing or storage, (such as Depends:
   and Status:) could be handled as they are now. 
*/
/* XXX: FEATURE: The Suggests: field needs to be changed from a string
   to a dependency list. And, since we already have
   Depends/Pre-Depends and need to add Conflicts, Recommends, and
   Enhances, perhaps we could generalize all of these and save some
   code duplication.
*/
int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest)
{
    int reading_conffiles, reading_description;
    int pkg_false_provides=1;
    char ** lines;
    char * provide=NULL;

    pkg->src = src;
    pkg->dest = dest;

    reading_conffiles = reading_description = 0;

    for (lines = *raw; *lines; lines++) {
	/*	fprintf(stderr, "PARSING %s\n", *lines);*/
	switch (**lines) {
	case 'P':
	    if(isGenericFieldType("Package:", *lines)) 
		pkg->name = parseGenericFieldType("Package", *lines);
	    else if(isGenericFieldType("Priority:", *lines))
		pkg->priority = parseGenericFieldType("Priority", *lines);
	    else if(isGenericFieldType("Provides", *lines)){
/* Here we add the internal_use to align the off by one problem between provides_str and provides */
        	provide = (char * ) malloc(strlen(*lines)+ 35 ); /* Preparing the space for the new opkg_internal_use_only */
        	if ( alterProvidesLine(*lines,provide) ){
        	    return EINVAL;
        	}
		pkg->provides_str = parseDependsString( provide, &pkg->provides_count);
/* Let's try to hack a bit here.
   The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies
   in alot of other places. We will remove it before writing down the status database */
        	pkg_false_provides=0;
        	free(provide);
    	    } 
	    else if(isGenericFieldType("Pre-Depends", *lines))
		pkg->pre_depends_str = parseDependsString(*lines, &pkg->pre_depends_count);
	    break;

	case 'A':
	    if(isGenericFieldType("Architecture:", *lines))
		pkg->architecture = parseGenericFieldType("Architecture", *lines);
	    else if(isGenericFieldType("Auto-Installed:", *lines)) {
		char *auto_installed_value;
		auto_installed_value = parseGenericFieldType("Auto-Installed:", *lines);
		if (strcmp(auto_installed_value, "yes") == 0) {
		    pkg->auto_installed = 1;
		}
		free(auto_installed_value);
	    }
	    break;

	case 'F':
	    if(isGenericFieldType("Filename:", *lines))
		pkg->filename = parseGenericFieldType("Filename", *lines);
	    break;

	case 'S':
	    if(isGenericFieldType("Section:", *lines))
		pkg->section = parseGenericFieldType("Section", *lines);
	    else if(isGenericFieldType("Size:", *lines))
		pkg->size = parseGenericFieldType("Size", *lines);
	    else if(isGenericFieldType("Source:", *lines))
		pkg->source = parseGenericFieldType("Source", *lines);
	    else if(isGenericFieldType("Status", *lines))
		parseStatus(pkg, *lines);
	    else if(isGenericFieldType("Suggests", *lines))
		pkg->suggests_str = parseDependsString(*lines, &pkg->suggests_count);
	    break;

	case 'T':
	    if(isGenericFieldType("Tags:", *lines))
		pkg->tags = parseGenericFieldType("Tags", *lines);
	    break;

	case 'M':
	    if(isGenericFieldType("MD5sum:", *lines))
		pkg->md5sum = parseGenericFieldType("MD5sum", *lines);
	    /* The old opkg wrote out status files with the wrong case for MD5sum,
		let's parse it either way */
	    else if(isGenericFieldType("MD5Sum:", *lines))
		pkg->md5sum = parseGenericFieldType("MD5Sum", *lines);
	    else if(isGenericFieldType("Maintainer", *lines))
		pkg->maintainer = parseGenericFieldType("Maintainer", *lines);
	    break;

	case 'I':
	    if(isGenericFieldType("Installed-Size:", *lines))
		pkg->installed_size = parseGenericFieldType("Installed-Size", *lines);
	    else if(isGenericFieldType("Installed-Time:", *lines)) {
		char *time_str = parseGenericFieldType("Installed-Time", *lines);
		pkg->installed_time = strtoul(time_str, NULL, 0);
		free (time_str);
	    }	    
	    break;

	case 'E':
	    if(isGenericFieldType("Essential:", *lines)) {
		char *essential_value;
		essential_value = parseGenericFieldType("Essential", *lines);
		if (strcmp(essential_value, "yes") == 0) {
		    pkg->essential = 1;
		}
		free(essential_value);
	    }
	    break;

	case 'V':
	    if(isGenericFieldType("Version", *lines))
		parseVersion(pkg, *lines);
	    break;

	case 'C':
	    if(isGenericFieldType("Conffiles", *lines)){
		parseConffiles(pkg, *lines);
		reading_conffiles = 1;
	    }
	    else if(isGenericFieldType("Conflicts", *lines))
		pkg->conflicts_str = parseDependsString(*lines, &pkg->conflicts_count);
	    break;

	case 'D':
	    if(isGenericFieldType("Description", *lines)) {
		pkg->description = parseGenericFieldType("Description", *lines);
		reading_conffiles = 0;
		reading_description = 1;
	    }
	    else if(isGenericFieldType("Depends", *lines))
		pkg->depends_str = parseDependsString(*lines, &pkg->depends_count);
	    break;

	case 'R':
	    if(isGenericFieldType("Recommends", *lines))
	        pkg->recommends_str = parseDependsString(*lines, &pkg->recommends_count);
	    else if(isGenericFieldType("Replaces", *lines))
		pkg->replaces_str = parseDependsString(*lines, &pkg->replaces_count);
	    
	    break;

	case ' ':
	    if(reading_description) {
		/* we already know it's not blank, so the rest of description */      
		pkg->description = realloc(pkg->description,
					   strlen(pkg->description)
					   + 1 + strlen(*lines) + 1);
		strcat(pkg->description, "\n");
		strcat(pkg->description, (*lines));
	    }
	    else if(reading_conffiles)
		parseConffiles(pkg, *lines);
		
	    break;

	default:
	    if(line_is_blank(*lines)) {
		lines++;
		goto out;
	    }
	}
    }
out:;
    
    *raw = lines;
/* If the ipk has not a Provides line, we insert our false line */ 
    if ( pkg_false_provides==1)
    {
       pkg->provides_count = 1;
       pkg->provides_str = malloc (sizeof (char*));
       pkg->provides_str[0] = strdup ("opkg_internal_use_only");
    }

    if (pkg->name) {
	return 0;
    } else {
	return EINVAL;
    }
}

int pkg_valorize_other_field(pkg_t *pkg, char ***raw)
{
    char ** lines;

    for (lines = *raw; *lines; lines++) {
	if(isGenericFieldType("Essential:", *lines)) {
	    char *essential_value;
	    essential_value = parseGenericFieldType("Essential", *lines);
	    if (strcmp(essential_value, "yes") == 0) {
		pkg->essential = 1;
	    }
	    free(essential_value);
	}
    }
    *raw = lines;

    return 0;
}