@@ -28,6 +28,8 @@ struct fdisk_script {
2828 struct fdisk_context * cxt ;
2929
3030 int refcount ;
31+ char * (* fn_fgets )(struct fdisk_script * , char * , size_t , FILE * );
32+ void * userdata ;
3133
3234 /* parser's state */
3335 size_t nlines ;
@@ -174,6 +176,34 @@ void fdisk_unref_script(struct fdisk_script *dp)
174176 }
175177}
176178
179+ /**
180+ * fdisk_script_set_userdata
181+ * @dp: script
182+ * @data: your data
183+ *
184+ * Sets data usable for example in callbacks (e.g fdisk_script_set_fgets()).
185+ *
186+ * Returns: 0 on success, <0 on error.
187+ */
188+ int fdisk_script_set_userdata (struct fdisk_script * dp , void * data )
189+ {
190+ assert (dp );
191+ dp -> userdata = data ;
192+ return 0 ;
193+ }
194+
195+ /**
196+ * fdisk_script_get_userdata
197+ * @dp: script
198+ *
199+ * Returns: user data or NULL.
200+ */
201+ void * fdisk_script_get_userdata (struct fdisk_script * dp )
202+ {
203+ assert (dp );
204+ return dp -> userdata ;
205+ }
206+
177207static struct fdisk_scriptheader * script_get_header (struct fdisk_script * dp ,
178208 const char * name )
179209{
@@ -937,6 +967,26 @@ int fdisk_script_read_buffer(struct fdisk_script *dp, char *s)
937967 return rc ;
938968}
939969
970+ /**
971+ * fdisk_script_set_fgets:
972+ * @dp: script
973+ * @fn_fgets: callback function
974+ *
975+ * The library uses fgets() function to read the next line from the script.
976+ * This default maybe overrided to another function. Note that the function has
977+ * to return the line terminated by \n (for example readline(3) removes \n).
978+ *
979+ * Return: 0 on success, <0 on error
980+ */
981+ int fdisk_script_set_fgets (struct fdisk_script * dp ,
982+ char * (* fn_fgets )(struct fdisk_script * , char * , size_t , FILE * ))
983+ {
984+ assert (dp );
985+
986+ dp -> fn_fgets = fn_fgets ;
987+ return 0 ;
988+ }
989+
940990/**
941991 * fdisk_script_read_line:
942992 * @dp: script
@@ -959,8 +1009,12 @@ int fdisk_script_read_line(struct fdisk_script *dp, FILE *f, char *buf, size_t b
9591009
9601010 /* read the next non-blank non-comment line */
9611011 do {
962- if (fgets (buf , bufsz , f ) == NULL )
1012+ if (dp -> fn_fgets ) {
1013+ if (dp -> fn_fgets (dp , buf , bufsz , f ) == NULL )
1014+ return 1 ;
1015+ } else if (fgets (buf , bufsz , f ) == NULL )
9631016 return 1 ;
1017+
9641018 dp -> nlines ++ ;
9651019 s = strchr (buf , '\n' );
9661020 if (!s ) {
0 commit comments