1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/*
* DSH / dancer's shell or the distributed shell
* Copyright (C) 2001-2004 Junichi Uekawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* A library to read dsh config file style data files.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "libdshconfig.h"
#include "config.h"
/* declaration for a local function required by autoconf */
static void * rpl_malloc (size_t n);
/* local function defining "getline" */
#ifndef HAVE_GETLINE
/* an imcomplete, and wrong implementation of getline */
static ssize_t getline (char **LINEPTR, size_t *N, FILE *STREAM)
{
const int GETLINESIZE = 256;
if (!*LINEPTR)
*LINEPTR= malloc (GETLINESIZE);
if (*N != GETLINESIZE)
*LINEPTR = realloc (*LINEPTR, GETLINESIZE);
if (!*LINEPTR)
{
return -1;
}
if (!fgets (*LINEPTR, GETLINESIZE - 1, STREAM))
return -1;
*N = strlen (*LINEPTR);
return GETLINESIZE;
}
#endif
/**
Function to search member
*/
const char *
dshconfig_searchdata (const dshconfig * d, const char * index )
{
dshconfig_internal * i = d->config;
for (; i; i = i -> next )
{
if (! strcmp (i->title, index ))
return i->data;
}
return NULL;
}
/**
The function used to split a line
@return NULL on failure.
*/
dshconfig_internal *
dshconfig_splitline(const char * original, char delimiter)
{
char * s = strdup (original);
dshconfig_internal * d = malloc (sizeof (dshconfig_internal));
char * pos, * i;
if (!d)
return NULL;
if (!s)
{
fprintf(stderr, "Failed to malloc in dshconfig_internal");
return NULL;
}
d->next =NULL;
if ((pos = strchr(s, delimiter)) == NULL) /* if the line has no delimiter, it is an error */
{
return NULL;
}
*(pos++) = 0; /* terminate the string, and pos points to start of "data" */
/* removing the space at end of title */
for (i = pos - 2; i > s; --i)
{
if (!isspace (*i))
break;
else
*i = 0;
}
/* removing the space in the beginning in title */
for (i = s; i < pos; ++i)
if (!isspace (*i))
break;
d->title=strdup(i);
/* removing the space at the end of data*/
for (i = strlen(pos) + pos - 1; i > pos; --i)
{
if (!isspace (*i))
break;
else
*i = 0;
}
/* removing space at the beginning in data */
for (i = pos; i < pos + strlen (pos); ++i)
{
if (!isspace (*i))
break;
}
d->data=strdup(i);
free (s);
return d;
}
/**
The ugly function to do the config file reading.
It will read one non-commentline and return to the caller.
Return value of NULL indicates termination and/or error.
*/
static dshconfig_internal*
read_oneline (FILE* f, int delimiter)
{
/* read one line and return */
dshconfig_internal * d = NULL;
char* s = NULL;
size_t size = 0;
char* pos;
while (getline (&s, &size, f) != -1)
{
if (0!=(pos = strchr(s, '#'))) /* handle comments, # and not \# */
if ((pos == s) || (*(pos-1) != '\\'))
*pos = 0;
if ((strchr(s, delimiter)) == NULL) /* if the line has no delimiter, get another one */
{
continue;
}
d = dshconfig_splitline(s, delimiter);
if (!d)
continue;
free (s);
return d;
}
if (s) free (s);
return 0;
}
/** reads a dsh config file, and load it up in memory
@return NULL when error.
*/
dshconfig *
open_dshconfig (FILE* file, char delimiter)
{
dshconfig * d = malloc (sizeof (dshconfig));
dshconfig_internal * t, * i ;
if (!d)
return NULL;
if (!file)
return NULL;
d->config = NULL;
while (0!=(t = read_oneline (file, delimiter)))
{
t->next = NULL;
if (NULL != d->config)
{
for (i = d->config; i -> next; i = i-> next);
i -> next = t;
}
else
{
d->config = t;
}
}
return d;
}
/**
Frees up memory for dshconfig_internal.
It is allocated by dshconfig_splitline.
@Version soname 1 adds this feature
*/
void
free_dshconfig_internal(dshconfig_internal * i)
{
free (i->title);
free (i->data);
free (i);
}
/**
Frees up memory allocated by open_dshconfig.
*/
void
free_dshconfig(dshconfig* d)
{
dshconfig_internal * i;
if (!d)
return;
for (i=d->config; i; )
{
dshconfig_internal * tmp = i->next ;
free_dshconfig_internal(i);
i=tmp;
}
free (d);
}
/**
* A GNU-compatible malloc,
* Code to work with autoconf AC_FUNC_MALLOC
*/
#undef malloc
static void *
rpl_malloc (size_t n)
{
if (n == 0)
n = 1;
return malloc (n);
}
|