Blame bin/gegl-options.c

Packit bc1512
/* This file is part of GEGL editor -- a gtk frontend for GEGL
Packit bc1512
 *
Packit bc1512
 * This program is free software; you can redistribute it and/or modify
Packit bc1512
 * it under the terms of the GNU General Public License as published by
Packit bc1512
 * the Free Software Foundation; either version 3 of the License, or
Packit bc1512
 * (at your option) any later version.
Packit bc1512
 *
Packit bc1512
 * This program is distributed in the hope that it will be useful,
Packit bc1512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit bc1512
 * GNU General Public License for more details.
Packit bc1512
 *
Packit bc1512
 * You should have received a copy of the GNU General Public License
Packit bc1512
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
 *
Packit bc1512
 * Copyright (C) 2003, 2004, 2006 Øyvind Kolås
Packit bc1512
 */
Packit bc1512
Packit bc1512
#include "config.h"
Packit bc1512
Packit bc1512
#include <string.h>
Packit bc1512
#include <stdlib.h>
Packit bc1512
#include <stdio.h>
Packit bc1512
Packit bc1512
#include "gegl-options.h"
Packit bc1512
Packit bc1512
static GeglOptions *opts_new (void)
Packit bc1512
{
Packit bc1512
  GeglOptions *o = g_malloc0 (sizeof (GeglOptions));
Packit bc1512
Packit bc1512
  o->mode     = GEGL_RUN_MODE_DISPLAY;
Packit bc1512
  o->xml      = NULL;
Packit bc1512
  o->output   = NULL;
Packit bc1512
  o->files    = NULL;
Packit bc1512
  o->file     = NULL;
Packit bc1512
  o->rest     = NULL;
Packit bc1512
  return o;
Packit bc1512
}
Packit bc1512
Packit bc1512
static G_GNUC_NORETURN void
Packit bc1512
usage (char *application_name)
Packit bc1512
{
Packit bc1512
    fprintf (stderr,
Packit bc1512
"usage: %s [options] <file | -- [op [op] ..]>\n"
Packit bc1512
"\n"
Packit bc1512
"  Options:\n"
Packit bc1512
"     -h, --help      this help information\n"
Packit bc1512
"\n"
Packit bc1512
"     -i, --file      read xml from named file\n"
Packit bc1512
"\n"
Packit bc1512
"     -x, --xml       use xml provided in next argument\n"
Packit bc1512
"\n"
Packit bc1512
"     --dot           output a graphviz graph description\n"
Packit bc1512
"\n"
Packit bc1512
"     -o, --output    output generated image to named file, type based\n"
Packit bc1512
"                     on extension.\n"
Packit bc1512
"\n"
Packit bc1512
"     -p              increment frame counters of various elements when\n"
Packit bc1512
"                     processing is done.\n"
Packit bc1512
"\n"
Packit bc1512
"     -X              output the XML that was read in\n"
Packit bc1512
"\n"
Packit bc1512
"     -v, --verbose   print diagnostics while running\n"
Packit bc1512
"\n"
Packit bc1512
"All parameters following -- are considered ops to be chained together\n"
Packit bc1512
"into a small composition instead of using an xml file, this allows for\n"
Packit bc1512
"easy testing of filters. Be aware that the default value will be used\n"
Packit bc1512
"for all properties.\n"
Packit bc1512
, application_name);
Packit bc1512
    exit (0);
Packit bc1512
}
Packit bc1512
Packit bc1512
#define match(string) (!strcmp (*curr, (string)))
Packit bc1512
#define assert_argument() do {\
Packit bc1512
    if (!curr[1] || curr[1][0]=='-') {\
Packit bc1512
        fprintf (stderr, "ERROR: '%s' option expected argument\n", *curr);\
Packit bc1512
        exit(-1);\
Packit bc1512
    }\
Packit bc1512
}while(0)
Packit bc1512
Packit bc1512
#define get_float(var) do{\
Packit bc1512
    assert_argument();\
Packit bc1512
    curr++;\
Packit bc1512
    (var)=atof(*curr);\
Packit bc1512
}while(0)
Packit bc1512
Packit bc1512
#define get_int(var) do{\
Packit bc1512
    assert_argument();\
Packit bc1512
    curr++;\
Packit bc1512
    (var)=atoi(*curr);\
Packit bc1512
}while(0)
Packit bc1512
Packit bc1512
#define get_string(var) do{\
Packit bc1512
    assert_argument();\
Packit bc1512
    curr++;\
Packit bc1512
    (var)=*curr;\
Packit bc1512
}while(0)
Packit bc1512
Packit bc1512
#define get_string_forced(var) do{\
Packit bc1512
    curr++;\
Packit bc1512
    (var)=*curr;\
Packit bc1512
}while(0)
Packit bc1512
Packit bc1512
static GeglOptions *
Packit bc1512
parse_args (gint    argc,
Packit bc1512
            gchar **argv);
Packit bc1512
Packit bc1512
static void
Packit bc1512
print_opts (GeglOptions *o)
Packit bc1512
{
Packit bc1512
  char *mode_str;
Packit bc1512
  switch (o->mode)
Packit bc1512
    {
Packit bc1512
      case GEGL_RUN_MODE_DISPLAY:
Packit bc1512
        mode_str = "Display on screen"; break;
Packit bc1512
      case GEGL_RUN_MODE_XML:
Packit bc1512
        mode_str = "Print XML"; break;
Packit bc1512
      case GEGL_RUN_MODE_OUTPUT:
Packit bc1512
        mode_str = "Output in a file"; break;
Packit bc1512
      case GEGL_RUN_MODE_HELP:
Packit bc1512
        mode_str = "Display help information"; break;
Packit bc1512
      default:
Packit bc1512
        g_warning ("Unknown GeglOption mode: %d", o->mode);
Packit bc1512
        break;
Packit bc1512
    }
Packit bc1512
Packit bc1512
    fprintf (stderr,
Packit bc1512
"Parsed commandline:\n"
Packit bc1512
"\tmode:   %s\n"
Packit bc1512
"\tfile:   %s\n"
Packit bc1512
"\txml:    %s\n"
Packit bc1512
"\toutput: %s\n"
Packit bc1512
"\trest:   %s\n"
Packit bc1512
"\t\n",
Packit bc1512
    mode_str,
Packit bc1512
    o->file==NULL?"(null)":o->file,
Packit bc1512
    o->xml==NULL?"(null)":o->xml,
Packit bc1512
    o->output==NULL?"(null)":o->output,
Packit bc1512
    o->rest==NULL?"":"yes"
Packit bc1512
);
Packit bc1512
    {
Packit bc1512
      GList *files = o->files;
Packit bc1512
      while (files)
Packit bc1512
        {
Packit bc1512
          fprintf (stderr, "\t%s\n", (gchar*)files->data);
Packit bc1512
          files = g_list_next (files);
Packit bc1512
        }
Packit bc1512
    }
Packit bc1512
}
Packit bc1512
Packit bc1512
GeglOptions *
Packit bc1512
gegl_options_parse (gint    argc,
Packit bc1512
                    gchar **argv)
Packit bc1512
{
Packit bc1512
    GeglOptions *o;
Packit bc1512
Packit bc1512
    o = parse_args (argc, argv);
Packit bc1512
    if (o->verbose)
Packit bc1512
        print_opts (o);
Packit bc1512
    return o;
Packit bc1512
}
Packit bc1512
Packit bc1512
gboolean
Packit bc1512
gegl_options_next_file (GeglOptions *o)
Packit bc1512
{
Packit bc1512
  GList *current = g_list_find (o->files, o->file);
Packit bc1512
  current = g_list_next (current);
Packit bc1512
  if (current)
Packit bc1512
    {
Packit bc1512
      g_warning ("%s", o->file);
Packit bc1512
      o->file = current->data;
Packit bc1512
      g_warning ("%s", o->file);
Packit bc1512
      return TRUE;
Packit bc1512
    }
Packit bc1512
  return FALSE;
Packit bc1512
}
Packit bc1512
Packit bc1512
gboolean
Packit bc1512
gegl_options_previous_file (GeglOptions *o)
Packit bc1512
{
Packit bc1512
  GList *current = g_list_find (o->files, o->file);
Packit bc1512
  current = g_list_previous (current);
Packit bc1512
  if (current)
Packit bc1512
    {
Packit bc1512
      o->file = current->data;
Packit bc1512
      return TRUE;
Packit bc1512
    }
Packit bc1512
  return FALSE;
Packit bc1512
}
Packit bc1512
Packit bc1512
Packit bc1512
static GeglOptions *
Packit bc1512
parse_args (int    argc,
Packit bc1512
            char **argv)
Packit bc1512
{
Packit bc1512
    GeglOptions *o;
Packit bc1512
    char **curr;
Packit bc1512
Packit bc1512
    if (argc==1) {
Packit bc1512
        usage (argv[0]);
Packit bc1512
    }
Packit bc1512
Packit bc1512
    o = opts_new ();
Packit bc1512
    curr = argv+1;
Packit bc1512
Packit bc1512
    while (*curr && !o->rest) {
Packit bc1512
        if (match ("-h")    ||
Packit bc1512
            match ("--help")) {
Packit bc1512
            o->mode = GEGL_RUN_MODE_HELP;
Packit bc1512
            usage (argv[0]);
Packit bc1512
        }
Packit bc1512
Packit bc1512
        else if (match ("--verbose") ||
Packit bc1512
                 match ("-v")) {
Packit bc1512
            o->verbose=1;
Packit bc1512
        }
Packit bc1512
Packit bc1512
        else if (match ("--g-fatal-warnings") ||
Packit bc1512
                 match ("-v")) {
Packit bc1512
            o->fatal_warnings=1;
Packit bc1512
        }
Packit bc1512
Packit bc1512
        else if (match ("-p")){
Packit bc1512
            o->play=TRUE;
Packit bc1512
        }
Packit bc1512
Packit bc1512
        else if (match ("--file") ||
Packit bc1512
                 match ("-i")) {
Packit bc1512
            const gchar *file_path;
Packit bc1512
            get_string (file_path);
Packit bc1512
            o->files = g_list_append (o->files, g_strdup (file_path));
Packit bc1512
        }
Packit bc1512
Packit bc1512
        else if (match ("--xml") ||
Packit bc1512
                 match ("-x")) {
Packit bc1512
            get_string (o->xml);
Packit bc1512
        }
Packit bc1512
Packit bc1512
        else if (match ("--output") ||
Packit bc1512
                 match ("-o")) {
Packit bc1512
            get_string_forced (o->output);
Packit bc1512
            o->mode = GEGL_RUN_MODE_OUTPUT;
Packit bc1512
        }
Packit bc1512
Packit bc1512
        else if (match ("-X")) {
Packit bc1512
            o->mode = GEGL_RUN_MODE_XML;
Packit bc1512
        }
Packit bc1512
Packit bc1512
        else if (match ("--")) {
Packit bc1512
            o->rest = curr+1;
Packit bc1512
            break;
Packit bc1512
        }
Packit bc1512
Packit bc1512
        else if (*curr[0]=='-') {
Packit bc1512
            fprintf (stderr, "\n\nunknown parameter '%s' giving you help instead\n\n\n", *curr);
Packit bc1512
            usage (argv[0]);
Packit bc1512
        }
Packit bc1512
Packit bc1512
        else
Packit bc1512
          {
Packit bc1512
            o->files = g_list_append (o->files, g_strdup (*curr));
Packit bc1512
          }
Packit bc1512
        curr++;
Packit bc1512
    }
Packit bc1512
Packit bc1512
    if (o->files)
Packit bc1512
      o->file = o->files->data;
Packit bc1512
    return o;
Packit bc1512
}
Packit bc1512
#undef match
Packit bc1512
#undef assert_argument