File: config_parser.c

package info (click to toggle)
uswsusp 1.0+20120915-6.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,572 kB
  • sloc: ansic: 7,164; sh: 566; makefile: 223; perl: 65
file content (156 lines) | stat: -rw-r--r-- 3,057 bytes parent folder | download | duplicates (3)
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
/*
 * 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 "config.h"
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>

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

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

	/* Skip white space */
	while (isspace(*str))
		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 (isblank(*str))
				str++;
			if (*str != ':' && *str != '=') {
				error = -EINVAL;
				break;
			}
			str++;
			while (isblank(*str))
				str++;
			end = str + strlen(str);
			while (end > str && isspace(*--end))
				*end = '\0';
			if (*str) {
				fmt = parv[i].fmt;
				if (!strncmp(fmt, "%s", 2))
					strncpy(parv[i].ptr, str,
						parv[i].len - 1);
				else {
					if (sscanf(str, fmt, parv[i].ptr) <= 0)
						error = -EINVAL;
				}
				break;
			}
		}
	}

cleanup:
	return error;
}

/**
 *	parse - read and parse the configuration file
 */

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

	file = fopen(file_name, "r");
	if (!file) {
		fprintf(stderr, "%s: Could not open configuration file\n",
			my_name);
		return -errno;
	}
	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");
}

void version(char *my_name, char *extra_version)
{
	printf(
		(
			"%s (%s) %s %s\n"
			"\n"
			"FEATURES: %s\n"
			"\n"
			"This is free software.  You may redistribute copies of it under the terms of\n"
			"the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"
			"There is NO WARRANTY, to the extent permitted by law.\n"
		),
		my_name,
		PACKAGE_NAME,
		PACKAGE_VERSION,
		extra_version ? extra_version : "",
		CONFIG_FEATURES
	);
}