Added \n to the isspace rule.
[yaz-moved-to-github.git] / util / readconf.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: readconf.c,v $
7  * Revision 1.3  1996-05-29 15:48:48  quinn
8  * Added \n to the isspace rule.
9  *
10  * Revision 1.2  1996/05/29  10:05:01  quinn
11  * Changed space criteria to support 8-bit characters
12  *
13  * Revision 1.1  1995/11/01  13:55:06  quinn
14  * Minor adjustments
15  *
16  * Revision 1.2  1995/10/30  13:54:27  quinn
17  * iRemoved fclose().
18  *
19  * Revision 1.1  1995/10/10  16:28:18  quinn
20  * Initial revision
21  *
22  *
23  */
24
25 #include <stdio.h>
26 #include <ctype.h>
27
28 #include <log.h>
29
30 #define l_isspace(c) ((c) == '\t' || (c) == ' ' || (c) == '\n')
31
32 int readconf_line(FILE *f, char *line, int len, char *argv[], int num)
33 {
34     char *p;
35     int argc;
36
37     while ((p = fgets(line, len, f)))
38     {
39         while (*p && isspace(*p))
40             p++;
41         if (*p && *p != '#')
42             break;
43     }
44     if (!p)
45         return 0;
46
47     for (argc = 0; *p ; argc++)
48     {
49         if (*p == '#')  /* trailing comment */
50             break;
51         argv[argc] = p;
52         while (*p && !l_isspace(*p))
53             p++;
54         if (*p)
55         {
56             *(p++) = '\0';
57             while (*p && l_isspace(*p))
58                 p++;
59         }
60     }
61     return argc;
62 }
63
64 /*
65  * Read lines of a configuration file.
66  */
67 int readconf(char *name, void *private,
68     int (*fun)(char *name, void *private, int argc, char *argv[]))
69 {
70     FILE *f;
71     char line[512], *m_argv[50];
72     int m_argc;
73
74     if (!(f = fopen(name, "r")))
75     {
76         logf(LOG_WARN|LOG_ERRNO, "readconf: %s", name);
77         return -1;
78     }
79     for (;;)
80     {
81         int res;
82
83         if (!(m_argc = readconf_line(f, line, 512, m_argv, 50)))
84         {
85             fclose(f);
86             return 0;
87         }
88
89         if ((res = (*fun)(name, private, m_argc, m_argv)))
90         {
91             fclose(f);
92             return res;
93         }
94     }
95 }