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