aboutsummaryrefslogtreecommitdiff
path: root/tests/wyinit.c
blob: f5f0302edc16c59c59aa08761d9be949a592a566 (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
/*
  NAME

  wyinit - initialize wydawca testsuite directories

  SYNOPSIS

  wyinit FILE

  DESCRIPTION

  Parses wydawca configuration file FILE.  For each "source" and "destination"
  keyword found, ensures that its argument exists and is a directory.  Creates
  missing directories.
*/

#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include "grecs.h"

static char *progname;

static void
mkdir_p(char *dir)
{
    char *p;
    struct stat st;

    p = dir;
    if (*p == '/')
	p++;
    while (1) {
	if (*p == 0 || *p == '/') {
	    int c = *p;
	    *p = 0;
	    if (stat(dir, &st)) {
		if (errno == ENOENT) {
		    if (mkdir(dir, 0700)) {
			fprintf(stderr, "%s: cannot create %s: %s\n",
				progname, dir, strerror(errno));
			exit(1);
		    }
		} else {
		    fprintf(stderr, "%s: cannot stat %s: %s\n",
			    progname, dir, strerror(errno));
		    exit(1);
		}
	    } else if (!S_ISDIR(st.st_mode)) {
		fprintf(stderr, "%s: component \"%s\" is not a directory\n",
			progname, dir);
		exit(1);
	    }
	    *p = c;
	    if (c == 0)
		break;
	}
	p++;
    }
}

static void
mkdir_recursive(const char *dir)
{
    char *p = grecs_strdup(dir);
    mkdir_p(p);
    free(p);
}

void
makehier(struct grecs_node *tree, char *path)
{
    struct grecs_node *node;
    grecs_match_buf_t match_buf;
    
    for (node = grecs_match_first(tree, path, &match_buf);
	 node;
	 node = grecs_match_next(match_buf)) {
	if (node->type != grecs_node_stmt)
	    continue;
	if (node->v.value->type != GRECS_TYPE_STRING)
	    continue;
	mkdir_recursive(node->v.value->v.string);
    }
    grecs_match_buf_free(match_buf);
}


int
main(int argc, char **argv)
{
    struct grecs_node *tree;
    
    progname = strrchr(argv[0], '/');
    if (!progname)
	progname = argv[0];
    else
	progname++;

    if (argc != 2) {
	fprintf(stderr, "usage: %s FILE\n", progname);
	fprintf(stderr, "creates source and destination directories for wydawca\n");
	fprintf(stderr, "configuration file.\n");
	exit(1);
    }

    grecs_log_to_stderr = 1;
    grecs_parser_options = GRECS_OPTION_QUOTED_STRING_CONCAT;    
    tree = grecs_parse(argv[1]);
    if (!tree)
	exit(2);
    makehier(tree, ".spool.source");
    makehier(tree, ".spool.destination");
    return 0;
}
	
    

Return to:

Send suggestions and report system problems to the System administrator.