| 1 | /* |
|---|
| 2 | * shvar.h |
|---|
| 3 | * |
|---|
| 4 | * Interface for non-destructively reading/writing files containing |
|---|
| 5 | * only shell variable declarations and full-line comments. |
|---|
| 6 | * |
|---|
| 7 | * Includes explicit inheritance mechanism intended for use with |
|---|
| 8 | * Red Hat Linux ifcfg-* files. There is no protection against |
|---|
| 9 | * inheritance loops; they will generally cause stack overflows. |
|---|
| 10 | * Furthermore, they are only intended for one level of inheritance; |
|---|
| 11 | * the value setting algorithm assumes this. |
|---|
| 12 | * |
|---|
| 13 | * Copyright 1999 Red Hat, Inc. |
|---|
| 14 | * |
|---|
| 15 | * This is free software; you can redistribute it and/or modify it |
|---|
| 16 | * under the terms of the GNU General Public License as published by |
|---|
| 17 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 18 | * (at your option) any later version. |
|---|
| 19 | * |
|---|
| 20 | * This program is distributed in the hope that it will be useful, but |
|---|
| 21 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 23 | * General Public License for more details. |
|---|
| 24 | * |
|---|
| 25 | * You should have received a copy of the GNU General Public License |
|---|
| 26 | * along with this program; if not, write to the Free Software |
|---|
| 27 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 28 | * |
|---|
| 29 | */ |
|---|
| 30 | #ifndef _SHVAR_H |
|---|
| 31 | #define _SHVAR_H |
|---|
| 32 | |
|---|
| 33 | #include <glib.h> |
|---|
| 34 | |
|---|
| 35 | #ifdef __cplusplus |
|---|
| 36 | extern "C" { |
|---|
| 37 | #endif /* __cplusplus */ |
|---|
| 38 | |
|---|
| 39 | typedef struct _shvarFile shvarFile; |
|---|
| 40 | struct _shvarFile { |
|---|
| 41 | char *fileName; /* read-only */ |
|---|
| 42 | int fd; /* read-only */ |
|---|
| 43 | char *arena; /* ignore */ |
|---|
| 44 | GList *lineList; /* read-only */ |
|---|
| 45 | GList *freeList; /* ignore */ |
|---|
| 46 | GList *current; /* set implicitly or explicitly, |
|---|
| 47 | points to element of lineList */ |
|---|
| 48 | shvarFile *parent; /* set explicitly */ |
|---|
| 49 | int modified; /* ignore */ |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | /* Create the file <name>, return shvarFile on success, NULL on failure */ |
|---|
| 54 | shvarFile * |
|---|
| 55 | svCreateFile(const char *name); |
|---|
| 56 | |
|---|
| 57 | /* Open the file <name>, return shvarFile on success, NULL on failure */ |
|---|
| 58 | shvarFile * |
|---|
| 59 | svNewFile(const char *name); |
|---|
| 60 | |
|---|
| 61 | /* Get the value associated with the key, and leave the current pointer |
|---|
| 62 | * pointing at the line containing the value. The char* returned MUST |
|---|
| 63 | * be freed by the caller. |
|---|
| 64 | */ |
|---|
| 65 | char * |
|---|
| 66 | svGetValue(shvarFile *s, const char *key); |
|---|
| 67 | |
|---|
| 68 | /* return 1 if <key> resolves to any truth value (e.g. "yes", "y", "true") |
|---|
| 69 | * return 0 if <key> resolves to any non-truth value (e.g. "no", "n", "false") |
|---|
| 70 | * return <def> otherwise |
|---|
| 71 | */ |
|---|
| 72 | int |
|---|
| 73 | svTrueValue(shvarFile *s, const char *key, int def); |
|---|
| 74 | |
|---|
| 75 | /* Set the variable <key> equal to the value <value>. |
|---|
| 76 | * If <key> does not exist, and the <current> pointer is set, append |
|---|
| 77 | * the key=value pair after that line. Otherwise, prepend the pair |
|---|
| 78 | * to the top of the file. |
|---|
| 79 | */ |
|---|
| 80 | void |
|---|
| 81 | svSetValue(shvarFile *s, const char *key, const char *value); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | /* Write the current contents iff modified. Returns -1 on error |
|---|
| 85 | * and 0 on success. Do not write if no values have been modified. |
|---|
| 86 | * The mode argument is only used if creating the file, not if |
|---|
| 87 | * re-writing an existing file, and is passed unchanged to the |
|---|
| 88 | * open() syscall. |
|---|
| 89 | */ |
|---|
| 90 | int |
|---|
| 91 | svWriteFile(shvarFile *s, int mode); |
|---|
| 92 | |
|---|
| 93 | /* Close the file descriptor (if open) and delete the shvarFile. |
|---|
| 94 | * Returns -1 on error and 0 on success. |
|---|
| 95 | */ |
|---|
| 96 | int |
|---|
| 97 | svCloseFile(shvarFile *s); |
|---|
| 98 | |
|---|
| 99 | #ifdef __cplusplus |
|---|
| 100 | } |
|---|
| 101 | #endif /* __cplusplus */ |
|---|
| 102 | |
|---|
| 103 | #endif /* ! _SHVAR_H */ |
|---|