I'm developing a built-in for ksh93 that will read a header line from
stdin, parse it, export some environment variables which will be used in
parsing the remaining lines of input - which results in more environment
variables being exported for use inside a loop.

e.g. cat somefile | while my_builtin ; do
somethingWithEnvironmentVariables; done

And my code looks like:

int b_my_builtin(int argc, char *argv, void *context) {
  char * line;

  if ((line = sfgetr(sfstdin,'\n',SF_STRING)) == NULL) {
      return 1;
  }

  if ( /* line is the header line */ ) {

       /* 
        * parse header here, export header variables
        */

       if ((line = sfgetr(sfstdin,'\n',SF_STRING)) == NULL) {
              return 1;
       }
  }

  /*
   * parse non-header line using header environment variables
   * export parsed data to environment variables for use inside a shell
   loop
   */

  return 0;
}

My question is, how should I best use memory in exporting environment
variables using nv_putval(name,value,flags)? Is it safe to use a pointer
to line (from sfgetr) for value (in nv_putval), or should I use malloc
or strdup to create new strings to pass in for value? For the latter
case, can I assume that these will be freed by the shell internals at
some point - e.g. during the next loop iteration when I go to nv_putval
again? Or should I free via an existing value pointer (from nv_getval)
before overwriting, either explicitly or via nv_unset? Incoming data
could potentially be large, and the shell long-running, so I'd like to
prevent leaking memory.

(Also, the built-in will be statically linked-into the shell binary, in
case that makes a difference.)

Thanks in advance,
Carl



_______________________________________________
ast-developers mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-developers

Reply via email to