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