File: config_parser.c

package info (click to toggle)
uswsusp 0.7-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,000 kB
  • ctags: 962
  • sloc: ansic: 6,060; xml: 755; sh: 425; makefile: 176
file content (147 lines) | stat: -rw-r--r-- 2,938 bytes parent folder | download
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
/*
 * config_parser.c
 *
 * Configuration file parser for userland suspend tools
 *
 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
 *
 * This file is released under the GPLv2.
 *
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>

#include "config_parser.h"
#include "encrypt.h"

int parse_line(char *str, struct config_par *parv)
{
	char *dst, *fmt;
	int error = 0;
	int i, k;

	/* Skip white space */
	while (*str == ' ' || *str == '\t' || *str == '\r' || *str == '\n')
		str++;
	/* Skip the lines containing white space only */
	if (!*str)
		goto cleanup;
	/* Ignore comments */
	if (*str == '#')
		goto cleanup;

	/* Compare with parameter names */
	for (i=0; parv[i].name; i++) {
		k = strlen(parv[i].name);
		if (!strncmp(parv[i].name, str, k)) {
			if (!parv[i].ptr)
				break;
			str += k;
			while (*str == ' ' || *str == '\t')
				str++;
			if (*str != ':' && *str != '=') {
				error = -EINVAL;
				break;
			}
			str++;
			while (*str == ' ' || *str == '\t')
				str++;
			if (*str) {
				fmt = parv[i].fmt;
				if (!strncmp(fmt, "%s", 2)) {
					dst = parv[i].ptr;
					k = parv[i].len;
					strncpy(dst, str, k - 1);
					k = strlen(dst) - 1;
					if (dst[k] == '\n')
						dst[k] = '\0';
				} else {
					k = sscanf(str, fmt, parv[i].ptr);
					if (k <= 0)
						error = -EINVAL;
				}
				break;
			}
		}
	}

	if (!parv[i].name) {
		error = -EINVAL;
	}

cleanup:
	return error;
}

/**
 *	parse - read and parse the configuration file
 *	note that a non-present config file is not considered an error here
 */

int parse(char *my_name, char *file_name, struct config_par *parv)
{
	char buf[MAX_STR_LEN];
	char *str;
	struct stat stat_buf;
	FILE *file;
	int error, i;

	if (stat(file_name, &stat_buf)) {
		fprintf(stderr, "%s: Could not stat configuration file\n",
			my_name);
		return 0;
	}
	file = fopen(file_name, "r");
	if (!file) {
		fprintf(stderr, "%s: Could not open configuration file\n",
			my_name);
		return 0;
	}
	error = 0;
	i = 0;
	do {
		str = fgets(buf, MAX_STR_LEN, file);
		if (!str)
			break;
		i++;
		error = parse_line(str, parv);
	} while (!error);
	fclose(file);
	if (error)
		fprintf(stderr, "%s: Error in configuration file, line %d\n",
			my_name, i);
	return error;
}

/* We're abusing struct option a bit. usage() expects an \0 in the
 * name string, and after that a comment.
 */
void usage(char *my_name, struct option *options, const char *short_options)
{
	struct option *opt;

	printf("Usage: %s [options]", my_name);
	for (opt = options; opt->name; opt++)
	{
		const char *descr = opt->name + strlen(opt->name) + 1;
		if (strchr(short_options,opt->val))
			printf("\n  -%c, --%s", opt->val, opt->name);
		else
			printf("\n  --%s", opt->name);

		if (opt->has_arg)
			printf(" <%s>", opt->name);

		if (strlen(descr))
			printf("\t%s",descr);
	}

	printf("\n");
}