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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/*
* Configurable ps-like program.
* Initialisation file parsing routines.
*
* Copyright (c) 2010 David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*/
#include <errno.h>
#include "ips.h"
#define ALLOC_SIZE 100 /* allocation size for lines */
/*
* Local procedures.
*/
static char * ReadLine(FILE * fp);
/*
* Try to access the initial file in the user's home directory.
* The file may or may not exist.
*/
BOOL
ParseUserInitFile(void)
{
const char * home;
char * fullName;
BOOL result;
/*
* Get the user's home directory and then build the full
* path from it and the user init file name.
*/
home = getenv("HOME");
if (home == NULL)
return TRUE;
fullName = AllocMemory(strlen(home) + sizeof(USER_INIT_FILE) + 2);
strcpy(fullName, home);
strcat(fullName, "/");
strcat(fullName, USER_INIT_FILE);
/*
* Try to parse the file.
*/
result = ParseFile(fullName, TRUE);
free(fullName);
return result;
}
/*
* Try to parse the system initialisation file.
* The file may or may not exist.
*/
BOOL
ParseSystemInitFile(void)
{
/*
* Try to parse the system initialisation file.
*/
return ParseFile(SYSTEM_INIT_FILE, TRUE);
}
/*
* Try to parse the definitions within a specified file.
* If the isOptional flag is set, then the file does not have to exist.
* Returns TRUE if the file was successfully parsed.
*/
BOOL
ParseFile(const char * name, BOOL isOptional)
{
FILE * fp;
char * cp;
char * cmd;
BOOL status;
MACRO_TYPE macroType;
status = TRUE;
/*
* If the file is optional then check whether it exists.
* In not, then return success without doing anything.
*/
if (isOptional && (access(name, F_OK) != 0) && (errno == ENOENT))
return TRUE;
/*
* Check our permissions for reading the file before using it,
* in case we are running suid as root.
*/
if (access(name, R_OK) != 0)
{
fprintf(stderr, "Cannot access \"%s\" for reading: %s\n",
name, strerror(errno));
return FALSE;
}
fp = fopen(name, "r");
if (fp == NULL)
{
fprintf(stderr, "Cannot open \"%s\" for reading: %s\n",
name, strerror(errno));
return FALSE;
}
/*
* For further security, make sure that the first line contains
* the magic string identifying an ips configuration file.
*/
cmd = ReadLine(fp);
if ((cmd == NULL) || (strcmp(cmd, FIRST_LINE) != 0))
{
fclose(fp);
fprintf(stderr,
"The file \"%s\" does not begin with \"%s\".\n",
name, FIRST_LINE);
return FALSE;
}
/*
* OK, read each line of the file and handle it.
*/
while (TRUE)
{
cmd = ReadLine(fp);
if (cmd == NULL)
break;
while (isBlank(*cmd))
cmd++;
/*
* Skip blank lines or lines starting with hash marks.
*/
if ((*cmd == '#') || (*cmd == '\0'))
continue;
cp = cmd;
while ((*cp != '\0') && !isBlank(*cp))
cp++;
if (*cp)
*cp++ = '\0';
while (isBlank(*cp))
cp++;
/*
* If a macro is being defined, then do that.
*/
macroType = MACRO_TYPE_NONE;
if (strcmp(cmd, "option") == 0)
macroType = MACRO_TYPE_OPTION;
else if (strcmp(cmd, "column") == 0)
macroType = MACRO_TYPE_COLUMN;
else if (strcmp(cmd, "expr") == 0)
macroType = MACRO_TYPE_EXPRESSION;
if (macroType != MACRO_TYPE_NONE)
{
cmd = cp;
while ((*cp != '\0') && !isBlank(*cp))
cp++;
if (*cp)
*cp++ = '\0';
while (isBlank(*cp))
cp++;
if (!DefineMacro(macroType, cmd, cp))
status = FALSE;
continue;
}
/*
* The command is unknown.
*/
fprintf(stderr, "%s: Unknown command \"%s\"\n", name, cmd);
status = FALSE;
break;
}
if (ferror(fp))
{
fprintf(stderr, "Error reading \"%s\": %s\n", name,
strerror(errno));
fclose(fp);
return FALSE;
}
(void) fclose(fp);
return status;
}
/*
* Read the next line from the opened file.
* Returns a pointer to a static buffer which contains the null
* terminated line with the newline removed, or a NULL pointer on
* end of file or error. This handles backslashes at the end of lines
* to indicate that the line is continued on the next line.
*/
static char *
ReadLine(FILE * fp)
{
char * tmpBuf;
static char * buf;
static int avail;
static int used;
used = 0;
while (TRUE)
{
/*
* Grow the buffer if we need more room.
*/
if (used + ALLOC_SIZE > avail)
{
if (avail == 0)
tmpBuf = malloc(ALLOC_SIZE);
else
tmpBuf = realloc(buf, avail + ALLOC_SIZE);
if (tmpBuf == NULL)
{
fprintf(stderr, "Cannot allocate memory\n");
return NULL;
}
buf = tmpBuf;
avail += ALLOC_SIZE;
}
/*
* Read some more of the file into the buffer after any
* part of the line which may have already been read.
*/
if (fgets(&buf[used], avail - used, fp) == NULL)
{
if (used)
return buf;
return NULL;
}
/*
* Add to the size of the line read so far.
*/
used += strlen(&buf[used]);
/*
* If the buffer doesn't end in a newline, then we didn't
* read the whole line, so go back and read some more.
*/
if ((used > 0) && (buf[used - 1] != '\n'))
continue;
/*
* Remove the newline from the end of the data.
*/
buf[--used] = '\0';
/*
* See if the last character of the line is now a backslash.
* If so, then replace it with a blank and continue reading.
*/
if ((used > 0) && (buf[used - 1] == '\\'))
{
buf[used - 1] = ' ';
continue;
}
/*
* The line is completely read in, so return it.
*/
return buf;
}
}
/* END CODE */
|