psql as an execve(2) interpreter

From: <brook(at)biology(dot)nmsu(dot)edu>
To: pgsql-hackers(at)postgresql(dot)org
Cc: brook(at)nmsu(dot)edu
Subject: psql as an execve(2) interpreter
Date: 2005-07-27 03:58:26
Message-ID: 17127.1634.62446.428480@trillium.aquilegia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I would like to use pgsl as an interpreter (in the sense of
execve(2)). In short, if a file begins with the line

#! /path/to/psql -f

it should be interpretable by psql. The normal semantics of execve(2)
ensure that this will work perfectly (indeed a file containing
"#!/path/to/psql -l" works as expected), except for psql's nasty habit
of not interpreting the first line as a comment.

It seems that a simple fix to the following function in
src/bin/psql/input.c would do the trick.

char *
gets_fromFile(FILE *source)
{
PQExpBufferData buffer;
char line[1024];

initPQExpBuffer(&buffer);

while (fgets(line, sizeof(line), source) != NULL)
{
appendPQExpBufferStr(&buffer, line);
if (buffer.data[buffer.len - 1] == '\n')
{
buffer.data[buffer.len - 1] = '\0';
return buffer.data;
}
}

if (buffer.len > 0)
return buffer.data; /* EOF after reading some bufferload(s) */

/* EOF, so return null */
termPQExpBuffer(&buffer);
return NULL;
}

For example, this feature could be achieved by 1) including a static
variable to differentiate the first from subsequent calls and 2)
discarding the first line (and returning the second) on the first call
if the first line begins with #!.

Thus, I have two questions.

- Is this a feature that would be generally accepted and useful for
the postgresql community (i.e., would it be incorporated into the
code base)?

- Is this the correct solution or are there other portions of the code
that need to be considered?

I appreciate any feedback you can give me on this.

Thank you very much.

Cheers,
Brook

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeff Davis 2005-07-27 03:59:11 Re: [HACKERS] Enticing interns to PostgreSQL
Previous Message Gavin Sherry 2005-07-27 03:47:25 Duplicate object names in GRANT