First, note that I have already written one. It provides "readline"-like capability to GAWK. It uses a package which is similar to, but different from, "readline", so that you have a scrollback buffer when you are
entering lines at the terminal in GAWK. I wrote is several years ago and
use it extensively. So far, so good.
Basically, what that extension does is, when called, it calls the "getline" function in the other package, then copies the line read from the buffer of the "getline" function into the buffer provided by GAWK. GAWK then picks
it up and everything works as expected.
But here's the thing. I want to write one now that will read the line normally and then do something to the line before returning it to GAWK.
What I don't know how to do is to call GAWK's normal "getline" function
from my extension library. So, what I am thinking of is something like:
/* In my extension code; note that "fd" is passed in as a parameter */
normal_gawk_input(fd,buff);
/* Now examine (and possibly change) buff */
...
/* And return to GAWK */
return awk_true;
Some notes:
2) I thought about using the Linux function getline(3). That would
work, except for one little problem. The problem is that getline
wants a FILE * object, but GAWK deals in "fd"s. You could use
fdopen(3) to convert, but that seems messy. It seems wasteful to
call fdopen() every time the input filter function is called, but I
don't see any entirely safe way to avoid doing that. It would be
nice if there was "fd" version of getline(), but I don't know of
anything like that. (see footnote below at (*))
(*) Part of the problem is that it seems clear to me that fdopen(3)
allocates memory (presumably, using malloc() or similar) under the covers
for the FILE * object that it creates. There doesn't seem to be any clean way to free() that allocated memory.
2) I thought about using the Linux function getline(3). That would
work, except for one little problem. The problem is that getline
wants a FILE * object, but GAWK deals in "fd"s. You could use
fdopen(3) to convert, but that seems messy. It seems wasteful to
call fdopen() every time the input filter function is called, but I
don't see any entirely safe way to avoid doing that. It would be
nice if there was "fd" version of getline(), but I don't know of
anything like that. (see footnote below at (*))
On 28/07/2021 14:21, Kenny McCormack wrote:
2) I thought about using the Linux function getline(3). That would
work, except for one little problem. The problem is that getline
wants a FILE * object, but GAWK deals in "fd"s. You could use
fdopen(3) to convert, but that seems messy. It seems wasteful to
call fdopen() every time the input filter function is called, but I
don't see any entirely safe way to avoid doing that. It would be
nice if there was "fd" version of getline(), but I don't know of
anything like that. (see footnote below at (*))
You don't need to call fdopen() every time, if I understand this page >correctly: ><https://www.gnu.org/software/gawk/manual/html_node/Input-Parsers.html>
I think you need only call it when your XXX_can_take_file() function is >invoked and save the obtained FILE value in a global static.
So that's once per file not once per record.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 21:29:51 |
Calls: | 6,646 |
Calls today: | 1 |
Files: | 12,190 |
Messages: | 5,327,494 |