#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void gsegraf_C_plot ( char *fname, ... )
   {
   /********************************************************************************
   *
   * Function writes a plot-parameter file and then uses gsegraf to create the
   * corresponding plot.
   *
   * Input
   *    fname:           plot-parameter file-name (including path)
   *    keyword1   arg1: first keyword-argument pair
   *    keyword2   arg2: second keyword-argument pair
   *                               etc
   *
   * All inputs must be strings (enclosed in double quotes) and separated by commas.
   * If all or part of an argument needs to be quoted in the plot-parameter file,
   * the double-quote escape sequence (\") must be used for these additional quotes.
   * The list of keyword-argument pairs must be terminated by a NULL pointer. An
   * example of how to call gsegraf_C_plot is:
   *
   *    gsegraf_C_plot("./sinc_param.txt",
   *                   "file_name     \"./sinc.txt\"",
   *                   "file_format   \"%lf %lf\"",
   *                   "plot_type     \"points\"",
   *                   "plot_style    lk",
   *                   "axis_type     \"linear\"",
   *                   "grid          ls",
   *                   "xlabel        \"x axis\"",
   *                   "ylabel        \"y axis\"",
   *                   "title         \"sin(x)/x\"",
   *                   NULL);
   *
   ********************************************************************************/

   /* Declare variables */
   unsigned int size;
   char *str, *command;
   va_list argp;
   FILE *fid;

   /* Write plot-parameter file */
   fid = fopen(fname, "w");
   va_start(argp, fname);
   while ( (str = va_arg(argp, char *)) != NULL )
      fprintf(fid, "%s\n", str);
   fclose(fid);
   va_end(argp);

   /* Plot data */
   size = strlen(fname) + 11;
   command = malloc(size*sizeof(char));
   memset(command, 0, size*sizeof(char));
   sprintf(command, "%s %s %c", "gsegraf", fname, '&');
   system(command);
   free(command);
   system("sleep 1");

   return;
   }

