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