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