Путеводитель по Руководству Linux

  User  |  Syst  |  Libr  |  Device  |  Files  |  Other  |  Admin  |  Head  |



   libpipeline    ( 3 )

библиотека управления трубопроводом (pipeline manipulation library)

  Name  |  Synopsis  |  Description  |  Environment  |    Examples    |  See also  |

Примеры (Examples)

In the following examples, function names starting with pipecmd_ or
     pipeline_ are real libpipeline functions, while any other function
     names are pseudocode.

The simplest case is simple. To run a single command, such as mv source dest:

pipeline *p = pipeline_new_command_args ("mv", source, dest, NULL); int status = pipeline_run (p);

libpipeline is often used to mimic shell pipelines, such as the following example:

zsoelim < input-file | tbl | nroff -mandoc -Tutf8

The code to construct this would be:

pipeline *p; int status;

p = pipeline_new (); pipeline_want_infile (p, "input-file"); pipeline_command_args (p, "zsoelim", NULL); pipeline_command_args (p, "tbl", NULL); pipeline_command_args (p, "nroff", "-mandoc", "-Tutf8", NULL); status = pipeline_run (p);

You might want to construct a command more dynamically:

pipecmd *manconv = pipecmd_new_args ("manconv", "-f", from_code, "-t", "UTF-8", NULL); if (quiet) pipecmd_arg (manconv, "-q"); pipeline_command (p, manconv);

Perhaps you want an environment variable set only while running a certain command:

pipecmd *less = pipecmd_new ("less"); pipecmd_setenv (less, "LESSCHARSET", lesscharset);

You might find yourself needing to pass the output of one pipeline to several other pipelines, in a 'tee' arrangement:

pipeline *source, *sink1, *sink2;

source = make_source (); sink1 = make_sink1 (); sink2 = make_sink2 (); pipeline_connect (source, sink1, sink2, NULL); /* Pump data among these pipelines until there's nothing left. */ pipeline_pump (source, sink1, sink2, NULL); pipeline_free (sink2); pipeline_free (sink1); pipeline_free (source);

Maybe one of your commands is actually an in-process function, rather than an external program:

pipecmd *inproc = pipecmd_new_function ("in-process", &func, NULL, NULL); pipeline_command (p, inproc);

Sometimes your program needs to consume the output of a pipeline, rather than sending it all to some other subprocess:

pipeline *p = make_pipeline (); const char *line;

pipeline_want_out (p, -1); pipeline_start (p); line = pipeline_peekline (p); if (!strstr (line, "coding: UTF-8")) printf ("Unicode text follows:0); while (line = pipeline_readline (p)) printf (" %s", line); pipeline_free (p);