diff options
author | Karel Zak | 2015-03-25 16:05:14 +0100 |
---|---|---|
committer | Karel Zak | 2015-03-25 16:05:14 +0100 |
commit | 5cdbe36fbc07c7d30c454b764eb5a6e9595a8252 (patch) | |
tree | e8337a6d6bf904dce9d623fbcfeb28d598bed982 /libfdisk/src/script.c | |
parent | libfdisk: fix symbols versioning script (diff) | |
download | kernel-qcow2-util-linux-5cdbe36fbc07c7d30c454b764eb5a6e9595a8252.tar.gz kernel-qcow2-util-linux-5cdbe36fbc07c7d30c454b764eb5a6e9595a8252.tar.xz kernel-qcow2-util-linux-5cdbe36fbc07c7d30c454b764eb5a6e9595a8252.zip |
libfdisk: add fgets() callback for scripts
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk/src/script.c')
-rw-r--r-- | libfdisk/src/script.c | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/libfdisk/src/script.c b/libfdisk/src/script.c index eb4e31d20..a7e0d21d8 100644 --- a/libfdisk/src/script.c +++ b/libfdisk/src/script.c @@ -28,6 +28,8 @@ struct fdisk_script { struct fdisk_context *cxt; int refcount; + char *(*fn_fgets)(struct fdisk_script *, char *, size_t, FILE *); + void *userdata; /* parser's state */ size_t nlines; @@ -174,6 +176,34 @@ void fdisk_unref_script(struct fdisk_script *dp) } } +/** + * fdisk_script_set_userdata + * @dp: script + * @data: your data + * + * Sets data usable for example in callbacks (e.g fdisk_script_set_fgets()). + * + * Returns: 0 on success, <0 on error. + */ +int fdisk_script_set_userdata(struct fdisk_script *dp, void *data) +{ + assert(dp); + dp->userdata = data; + return 0; +} + +/** + * fdisk_script_get_userdata + * @dp: script + * + * Returns: user data or NULL. + */ +void *fdisk_script_get_userdata(struct fdisk_script *dp) +{ + assert(dp); + return dp->userdata; +} + static struct fdisk_scriptheader *script_get_header(struct fdisk_script *dp, const char *name) { @@ -938,6 +968,26 @@ int fdisk_script_read_buffer(struct fdisk_script *dp, char *s) } /** + * fdisk_script_set_fgets: + * @dp: script + * @fn_fgets: callback function + * + * The library uses fgets() function to read the next line from the script. + * This default maybe overrided to another function. Note that the function has + * to return the line terminated by \n (for example readline(3) removes \n). + * + * Return: 0 on success, <0 on error + */ +int fdisk_script_set_fgets(struct fdisk_script *dp, + char *(*fn_fgets)(struct fdisk_script *, char *, size_t, FILE *)) +{ + assert(dp); + + dp->fn_fgets = fn_fgets; + return 0; +} + +/** * fdisk_script_read_line: * @dp: script * @f: file @@ -959,8 +1009,12 @@ int fdisk_script_read_line(struct fdisk_script *dp, FILE *f, char *buf, size_t b /* read the next non-blank non-comment line */ do { - if (fgets(buf, bufsz, f) == NULL) + if (dp->fn_fgets) { + if (dp->fn_fgets(dp, buf, bufsz, f) == NULL) + return 1; + } else if (fgets(buf, bufsz, f) == NULL) return 1; + dp->nlines++; s = strchr(buf, '\n'); if (!s) { |