Fix sample PQF
[yaz-moved-to-github.git] / util / readconf.c
1 /*
2  * Copyright (C) 1994-2000, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: readconf.c,v $
7  * Revision 1.10  2000-02-29 13:44:55  adam
8  * Check for config.h (currently not generated).
9  *
10  * Revision 1.9  1999/11/30 13:47:12  adam
11  * Improved installation. Moved header files to include/yaz.
12  *
13  * Revision 1.8  1999/08/27 09:40:32  adam
14  * Renamed logf function to yaz_log. Removed VC++ project files.
15  *
16  * Revision 1.7  1999/06/30 09:10:32  adam
17  * Fixed reading of MS-DOS files.
18  *
19  * Revision 1.6  1998/10/13 16:09:55  adam
20  * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
21  * Added support for multiple attribute set references and tagset references
22  * from an abstract syntax file.
23  * Fixed many bad logs-calls in routines that read the various
24  * specifications regarding data1 (*.abs,*.att,...) and made the messages
25  * consistent whenever possible.
26  * Added extra 'lineno' argument to function readconf_line.
27  *
28  * Revision 1.5  1997/09/04 07:53:02  adam
29  * Added include readconf.h.
30  *
31  * Revision 1.4  1997/05/14 06:54:07  adam
32  * C++ support.
33  *
34  * Revision 1.3  1996/05/29 15:48:48  quinn
35  * Added \n to the isspace rule.
36  *
37  * Revision 1.2  1996/05/29  10:05:01  quinn
38  * Changed space criteria to support 8-bit characters
39  *
40  * Revision 1.1  1995/11/01  13:55:06  quinn
41  * Minor adjustments
42  *
43  * Revision 1.2  1995/10/30  13:54:27  quinn
44  * iRemoved fclose().
45  *
46  * Revision 1.1  1995/10/10  16:28:18  quinn
47  * Initial revision
48  *
49  *
50  */
51 #if HAVE_CONFIG_H
52 #include <config.h>
53 #endif
54
55 #include <stdio.h>
56 #include <ctype.h>
57
58 #include <yaz/log.h>
59 #include <yaz/readconf.h>
60
61 #define l_isspace(c) ((c) == '\t' || (c) == ' ' || (c) == '\n' || (c) == '\r')
62
63 int readconf_line(FILE *f, int *lineno, char *line, int len,
64                   char *argv[], int num)
65 {
66     char *p;
67     int argc;
68     
69     while ((p = fgets(line, len, f)))
70     {
71         (*lineno)++;
72         while (*p && l_isspace(*p))
73             p++;
74         if (*p && *p != '#')
75             break;
76     }
77     if (!p)
78         return 0;
79     
80     for (argc = 0; *p ; argc++)
81     {
82         if (*p == '#')  /* trailing comment */
83             break;
84         argv[argc] = p;
85         while (*p && !l_isspace(*p))
86             p++;
87         if (*p)
88         {
89             *(p++) = '\0';
90             while (*p && l_isspace(*p))
91                 p++;
92         }
93     }
94     return argc;
95 }
96
97 /*
98  * Read lines of a configuration file.
99  */
100 int readconf(char *name, void *rprivate,
101              int (*fun)(char *name, void *rprivate, int argc, char *argv[]))
102 {
103     FILE *f;
104     char line[512], *m_argv[50];
105     int m_argc;
106     int lineno = 0;
107     
108     if (!(f = fopen(name, "r")))
109     {
110         yaz_log(LOG_WARN|LOG_ERRNO, "readconf: %s", name);
111         return -1;
112     }
113     for (;;)
114     {
115         int res;
116         
117         if (!(m_argc = readconf_line(f, &lineno, line, 512, m_argv, 50)))
118         {
119             fclose(f);
120             return 0;
121         }
122
123         if ((res = (*fun)(name, rprivate, m_argc, m_argv)))
124         {
125             fclose(f);
126             return res;
127         }
128     }
129 }