summaryrefslogtreecommitdiff
path: root/graphics/sketch/main.c
blob: a46fb6c227ca37579cbea17e56ca289f573c1e4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* main.c
   Copyright (C) 2005,2006,2007 Eugene K. Ressler, Jr.

This file is part of Sketch, a small, simple system for making 
3d drawings with LaTeX and the PSTricks or TikZ package.

Sketch is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

Sketch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Sketch; see the file COPYING.txt.  If not, see
http://www.gnu.org/copyleft */

#include <stdio.h>
#include "main.h"
#include "cmdline.h"
#include "parse.h"
#include "emit.h"

static CMD_LINE_OPT_ENV wrap_env[1];

int
main (int argc, char *argv[])
{
  OBJECT *scene, *hsr_scene;
  int ret;
  FILE *out_file;
  SYMBOL_TABLE *sym_tab;

  // create the outer symbol table
  sym_tab = new_scope (NULL);

  // make first pass through options to process those that are position-independent
  // save the rest in the wrap environment for later processing
  process_global_options (wrap_env, argc, argv, sym_tab);

  // die if there were errors parsing options
  if (trouble_p ())
    report_errors ();

  // if options show no interest in input
  if (wrap_env->skip_input_p) {
    if (wrap_env->n_files > 0)
      warn (no_line, "input ignored");
    return 0;
  }

  if (wrap_env->out_file_name)
    {
      out_file = fopen (wrap_env->out_file_name, "w");
      if (!out_file)
	err (no_line, "can't open '%s' for output", wrap_env->out_file_name);
    }
  else
    {
      out_file = stdout;
    }

  // die if there were errors preparing output stream
  if (trouble_p ())
    report_errors ();

  // set up the global environment for the parser
  init_global_env (global_env, wrap_env->pst_version);

  // process first set of tag defs and set up first input file
  ret = yywrap ();

  // quits if there were file opening errors in wrap
  if (ret == 0)
    {
      if (trouble_p ())
	report_errors ();
    }
  else
    {
      set_lexer_file ("<stdin>", stdin);
    }

  if (parse (sym_tab) != 0 && !trouble_p ())
    // emit an error to ensure report_errors halts 
    err (line, "parse error");

  // quits if there is trouble
  report_errors ();

  // flatten the object hierarchy into a scene
  scene = flat_scene (parsed_objects (), global_env);

  // painter's algorithm for HSR / HLR
  if (wrap_env->bsp_only_p)
    {
      hsr_scene = hsr_scene_with_bsp (scene);
    }
  else
    {
      hsr_scene = hsr_scene_with_depth_sort (scene);
    }

  // emit PStricks or TikZ
  emit (out_file, hsr_scene, global_env, wrap_env->doc_template_file_name);

  return 0;
}

int
yywrap (void)
{
  char *file_name = advance_to_next_file_name (wrap_env);
  if (file_name)
    {
      FILE *f = fopen (file_name, "r");
      if (f)
	{
	  set_lexer_file (file_name, f);
	  return 0;
	}
      else
	{
	  err (no_line, "can't open file '%s' for input", file_name);
	}
    }
  return 1;
}