File: graph.c

package info (click to toggle)
oleo 1.6-16
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,640 kB
  • ctags: 3,139
  • sloc: ansic: 39,221; yacc: 1,737; sh: 362; makefile: 88
file content (983 lines) | stat: -rw-r--r-- 23,256 bytes parent folder | download | duplicates (3)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/*	Copyright (C) 1993 Free Software Foundation, Inc.

This program 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 2, or (at your option)
any later version.

This program 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 this software; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */



#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
#include "global.h"
#include "graph.h"
#include "cmd.h"
#include "line.h"
#include "io-term.h"
#include "info.h"
#include "cell.h"
#include "regions.h"
#include "ref.h"
#include "io-utils.h"

/* Parameters for the next graph that will be drawn. */

/* This determines the type and destination of the ouput.
 * It should be the argument to a  `set term %s'.  For posctscript graphs,
 * we slip in a `set output %s' as well.
 */
static struct line graph_term_cmd;
static struct line graph_output_file;

typedef void (*plotter) (void);

struct get_symbols_frame;
struct write_data_frame;
static void get_symbols_thunk (struct get_symbols_frame * fr,
			       CELL * cell, CELLREF r, CELLREF c);
static void write_data_thunk (struct write_data_frame * fr,
			      CELL * y_cell, CELLREF r, CELLREF c) ;
static void spew_gnuplot (FILE  * fp, struct line * data_files, char * dir,
			  char * dbase); 
static void spew_for_x (void);
static void spew_for_ps (void);

static plotter plot_fn = 0;

char * gnuplot_program = "gnuplot";
char * gnuplot_shell = "/bin/sh";
char * rm_program = "/bin/rm";

char * graph_axis_name [graph_num_axis] =
{
  "x", "y"
};

char *
graph_order_name [graph_num_orders] =
{
  "rows",
  "columns",
};

char *graph_pair_order_name[graph_num_pair_orders] =
{
  "rows of hz pairs",
  "rows of vt pairs",
  "enumerated rows",
  "cols of hz pairs",
  "cols of vt pairs",
  "enumerated cols"
};

int graph_ornt_row_magic [graph_num_pair_orientations] = { 0, 1, 0 };
int graph_ornt_col_magic [graph_num_pair_orientations] = { 1, 0, 0 };


/* `set label %s' for each axis.
 */
static struct line graph_axis_title [graph_num_axis];

/* set logarithmic */
static int graph_logness [graph_num_axis];

/* Arguments to `set range [%s:%s]' */
static struct line graph_rng_lo [graph_num_axis];
static struct line graph_rng_hi [graph_num_axis];

/* The ranges (if any) of the symbolic names
 * for integer coordinates starting at 0.
 * If none, these will have lr == NON_ROW.
 */
static struct rng graph_axis_symbols [graph_num_axis];
static enum graph_ordering graph_axis_ordering [graph_num_axis];

/* Names to print along the axes */
static struct rng graph_axis_labels [graph_num_axis];
static enum graph_pair_ordering graph_axis_label_order [graph_num_axis];

/* Parameters for each dataset. */
#define NUM_DATASETS 10

/* plot .... with %s */
static struct line graph_style [NUM_DATASETS];
static struct line graph_title [NUM_DATASETS];
static struct rng graph_data [NUM_DATASETS];
static enum graph_pair_ordering graph_data_order [graph_num_axis];




enum graph_axis
chr_to_axis (int c)
{
  if (isupper (c))
    c = tolower (c);
  switch (c)
    {
    case 'x':
      return graph_x;
    case 'y':
      return graph_y;
    }
  io_error_msg ("Unkown graph-axis `%c' (try `x' or `y')", c);
  return graph_x;		/* not reached, actualy. */
}


enum graph_ordering
chr_to_graph_ordering (int c)
{
  if (isupper (c))
    c = tolower (c);
  switch (c)
    {
    case 'r':
      return graph_rows;
    case 'c':
      return graph_cols;
    }
  io_error_msg ("Unknown cell ordering `%c' (try `r' or `c').", c);
  return graph_rows;		/* not reached */
}

enum graph_pair_ordering
chrs_to_graph_pair_ordering (int pair, int dir)
{
  int pair_offset;
  if (isupper (pair))
    pair = tolower (pair);
  if (isupper (dir))
    dir = tolower (dir);

  switch (pair)
    {
    case 'h':
      pair_offset = graph_hz;
      break;
    case 'v':
      pair_offset = graph_vt;
      break;
    case 'i':
      pair_offset = graph_implicit;
      break;
    default:
      pair_offset = graph_implicit;
      io_error_msg
	("graph.c: invalid pair ording `%c' (wanted h, v, or i)", pair);
      /* no deposit... */
    }

  return PAIR_ORDER(chr_to_graph_ordering (dir), pair_offset);
}

char *
graph_quoted_str (char *str)
{
  static struct line buf;
  
  set_line (&buf, "\"");
  while (*str)
    {
      switch (*str)
	{
	case '"':
	  catn_line (&buf, "\\\"", 2);
	  break;
	default:
	  catn_line (&buf, str, 1);
	  break;
	}
      ++str;
    }
  catn_line (&buf, "\"", 1);
  return buf.buf;
}



void
graph_x11_mono (void)
{
  set_line (&graph_term_cmd, "x11 # b/w");
  set_line (&graph_output_file, "");
  plot_fn = spew_for_x;
}


void
graph_x11_color (void)
{
  set_line (&graph_term_cmd, "X11 # color");
  set_line (&graph_output_file, "");
  plot_fn = spew_for_x;
}


void
graph_postscript (char * file, int kind, int spectrum, char * font, int pt_size)
{
  if (isupper (kind))
    kind = tolower (kind);
  if (isupper (spectrum))
    spectrum = tolower (spectrum);
  if (!index ("led", kind))
    io_error_msg
      ("Incorrect postscript graph type %c (should be one of l, e, or c)",
       kind);
  if (!index ("mc", spectrum))
    io_error_msg
      ("Incorrect postscript color type %c (should be either m or c)",
       spectrum);
  sprint_line (&graph_term_cmd,
	       "postscript %c %c %s %d  # Postscript",
	       kind, spectrum, graph_quoted_str (font), pt_size);
  set_line (&graph_output_file, file);
  plot_fn = spew_for_ps;
}



void
graph_set_axis_title (int axis_c, char * title)
{
  enum graph_axis axis = chr_to_axis (axis_c);
  set_line (&graph_axis_title [axis], graph_quoted_str (title));
}

void
graph_set_logness (int axis_c, int explicit, int newval)
{
  enum graph_axis axis = chr_to_axis (axis_c);
  static struct line msg_buf;

  if (!explicit)
    newval = !graph_logness [axis];
  else
    newval = (newval > 0);

  graph_logness [axis] = newval;
  sprint_line (&msg_buf, "%slogarithmic %s%s",
	       ((graph_logness [graph_x] || graph_logness [graph_y])
		? "" : "no"),
	       graph_logness [graph_x] ? "x" : "",
	       graph_logness [graph_y] ? "y" : "");
  io_info_msg ("set %s", msg_buf.buf);
}

void
graph_check_range (char * val)
{
  if (says_default (val))
    return;
  while (isspace (*val)) ++val;
  if (*val == '-') ++val;
  while (isspace (*val)) ++val;
  if (!isdigit (*val))
    io_error_msg
      ("Illegal range specifier %s (should be a numer or `def').", val);
  else
    {
      while (*val)
	if (isspace (*val) || isdigit (*val))
	  ++val;
	else
	  break;
      while (isspace (*val)) ++val;
      if (*val == '.') ++val;
      while (*val)
	if (isspace (*val) || isdigit (*val))
	  ++val;
	else
	  io_error_msg
	    ("Illegal range specifier %s (should be a numer or `def').", val);
    }
	    
}


void
graph_set_axis_lo (int axis_c, char * val)
{
  enum graph_axis axis = chr_to_axis (axis_c);
  graph_check_range (val);
  set_line (&graph_rng_lo [axis], val);
  graph_axis_symbols [axis].lr = NON_ROW;
}


void
graph_set_axis_hi (int axis_c, char * val)
{
  enum graph_axis axis = chr_to_axis (axis_c);
  graph_check_range (val);
  set_line (&graph_rng_hi [axis], val);
  graph_axis_symbols [axis].lr = NON_ROW;
}

void
graph_set_axis_symbolic (int axis_c, struct rng * rng, int ordering_c) 
{
  enum graph_axis axis = chr_to_axis (axis_c);
  enum graph_ordering ordering = chr_to_graph_ordering (ordering_c);
  int top = (rng->hr - rng->lr + 1) * (rng->hc - rng->lc + 1) - 1;
  char buf [64];

  sprintf (buf, "%d.5", top);
  graph_set_axis_lo (axis, "-0.5");
  graph_set_axis_hi (axis, buf);
  graph_axis_symbols [axis] = *rng;
  graph_axis_ordering [axis] = ordering;
}

void
graph_set_axis_labels (int axis_c, struct rng * rng, int pair, int dir)
{
  enum graph_pair_ordering order = chrs_to_graph_pair_ordering (pair, dir);
  enum graph_axis axis = chr_to_axis (axis_c);
  graph_axis_labels [axis] = *rng;
  graph_axis_label_order [axis] = order;
}

void
graph_default_axis_labels (int axis_c)
{
  enum graph_axis axis = chr_to_axis (axis_c);
  graph_axis_labels [axis].lr = NON_ROW;
}



static char * graph_plot_styles [] =
{
  "lines",
  "points",
  "linespoints",
  "impulses",
  "dots",
   0
};

int
graph_check_style (char * name)
{
  int x =
    words_member (graph_plot_styles, parray_len (graph_plot_styles), name);
  if (x < 0)
    io_error_msg
      ("Invalid line style %s. (Try M-x describe-function set-graph-style).");
  return x;
}


void
graph_set_style (int data_set, char * style)
{
  int x = graph_check_style (style);
  if ((data_set < 0) || (data_set >= NUM_DATASETS))
    io_error_msg
      ("set-graph-style -- data-set out of range: %d (should be in [0..%d])",
       data_set, NUM_DATASETS);
  set_line (&graph_style [data_set], graph_plot_styles [x]);
}

void
graph_set_data_title (int data_set, char * title)
{
  if ((data_set < 0) || (data_set >= NUM_DATASETS))
    io_error_msg
      ("set-graph-title -- data-set out of range: %d (should be in [0..%d])",
       data_set, NUM_DATASETS);
  set_line (&graph_title [data_set], title);
}

void
graph_set_data (int data_set, struct rng * rng, int pair, int dir)
{
  enum graph_pair_ordering order = chrs_to_graph_pair_ordering (pair, dir);
  if ((data_set < 0) || (data_set >= NUM_DATASETS))
    io_error_msg
      ("set-graph-data -- data-set out of range: %d (should be in [0..%d])",
       data_set, NUM_DATASETS);
  graph_data [data_set] = *rng;
  graph_data_order [data_set] = order;
}




void
graph_presets (void)
{
  if (using_curses)
    graph_postscript ("#plot.ps", 'd', 'm', "TimesRoman", 12);
  else
    graph_x11_mono ();
  {
    enum graph_axis axis;
    for (axis = graph_x; axis < graph_num_axis; ++axis)
      {
	int axis_c = graph_axis_name [axis][0];
	graph_logness [axis] = 0;
	graph_set_axis_title (axis_c, "");
	graph_set_axis_lo (axis_c, "def");
	graph_set_axis_hi (axis_c, "def");
	graph_set_axis_title (axis_c, "");
	graph_default_axis_labels (axis_c);
      }
  }
}





void
graph_clear_datasets (void)
{
  int x;
  for (x = 0; x < NUM_DATASETS; ++x)
    {
      graph_set_style (x, "lines");
      graph_set_data_title (x, "");
      graph_data [x].lr = NON_ROW;
    }
}


void
init_graphing (void)
{
  gnuplot_program = getenv ("GNUPLOT_PROG");
  if (!gnuplot_program)
    gnuplot_program = "gnuplot";
  gnuplot_shell = getenv ("GNUPLOT_SHELL");
  if (!gnuplot_shell)
    gnuplot_shell = "/bin/sh";
  rm_program = getenv ("RM_PROG");
  if (!rm_program)
    rm_program = "/bin/rm";
  graph_presets ();
  graph_clear_datasets ();
}


void
graph_make_info (void)
{
  struct info_buffer * ib = find_or_make_info ("graphing-parameters");
  enum graph_axis axis;

  clear_info (ib);

  {
    print_info
      (ib,
       "");
    print_info
      (ib,
       "Parameter		Value");
    print_info
      (ib,
       "");

    print_info
      (ib,
       "output type		%s",
       graph_term_cmd.buf);
  }

  if (graph_output_file.buf[0])
    print_info
      (ib,
       "output file		%s",
       graph_output_file.buf);
  
  for (axis = graph_x; axis <= graph_y; ++axis)
    print_info
      (ib,
       "%s-axis title		%s",
       graph_axis_name [axis], graph_axis_title[axis].buf);

  {
    print_info
      (ib,
       "logarithmic axes	%s",
       (graph_logness [graph_x]
	? (graph_logness [graph_y] ? "x,y" : "x")
	: (graph_logness [graph_y] ? "y" : "-neither-")));

  }
  for (axis = graph_x; axis <= graph_y; ++axis)
    {
      if (graph_axis_symbols[axis].lr != NON_ROW)
	print_info
	  (ib,
	   	"%s-axis symbols	%s in %s",
	   graph_axis_name [axis],
	   graph_order_name [graph_axis_ordering [axis]],
	   range_name (&graph_axis_symbols [axis]));
      else
	print_info
	  (ib,
		"%s-axis range		[%s..%s]",
	   graph_axis_name [axis],
	   graph_rng_lo [axis].buf, graph_rng_hi [axis].buf,
	   graph_rng_hi [axis].buf, graph_rng_hi [axis].buf);
    }

  for (axis = graph_x; axis <= graph_y; ++axis)
    {
      if (graph_axis_labels[axis].lr != NON_ROW)
	print_info
	  (ib,
	   	"%s-axis labels		%s in %s",
	   graph_axis_name [axis],
	   graph_pair_order_name [graph_axis_label_order [axis]],
	   range_name (&graph_axis_labels [axis]));
    }

  {
    int x;
    for (x = 0; x < NUM_DATASETS; ++x)
      if (graph_data [x].lr != NON_ROW)
      {
	print_info (ib, "");
	print_info (ib,"Data Set %d%s%s",
		    x,
		    graph_title[x].buf[0] ? " entitled " : "",
		    graph_title[x].buf);
	print_info (ib,"  data for this set: %s in %s",
		    graph_pair_order_name [graph_data_order [x]],
		    range_name (&graph_data[x]));
	print_info (ib,"  style for this set: %s",
		    graph_style[x].buf);
	print_info (ib,"");
      }
  }
}



static FILE *
mk_tmp_file (struct line * line, char * dir, char * base)
{
  set_line (line, tmpnam (0));
  return fopen (line->buf, "w");
}


void
for_pairs_in (struct rng * rng, enum graph_pair_ordering order, fpi_thunk thunk, void * frame)
{
  CELLREF r;
  CELLREF c;
  enum graph_pair_orientation ornt = order % graph_num_pair_orientations;
  enum graph_ordering dir = ORDER_OF_PAIRS(order);
  int r_inc = 1 + graph_ornt_row_magic [ornt];
  int c_inc = 1 + graph_ornt_col_magic [ornt];
  if (dir == graph_rows)
    {
      r = rng->lr;
      while (1)
	{
	  c = rng->lc;
	  while (1)
	    {
	      CELLREF y_r = r + graph_ornt_row_magic [ornt];
	      CELLREF y_c = c + graph_ornt_col_magic [ornt];
	      CELL * cell = find_cell (y_r, y_c);
	      thunk (frame, cell, y_r, y_c);
	      if ((rng->hc - c) < c_inc)
		break;
	      c += c_inc;
	    }
	  if ((rng->hr - r) < r_inc)
	    break;
	  r += r_inc;
	}
    }
  else
    {
      c = rng->lc;
      while (1)
	{
	  r = rng->lr;
	  while (1)
	    {
	      CELLREF y_r = r + graph_ornt_row_magic [ornt];
	      CELLREF y_c = c + graph_ornt_col_magic [ornt];
	      CELL * cell = find_cell (y_r, y_c);
	      thunk (frame, cell, y_r, y_c);
	      if ((rng->hr - r) < r_inc)
		break;
	      r += r_inc;
	    }
	  if ((rng->hc - c) < c_inc)
	    break;
	  c += c_inc;
	}
    }
}

struct write_tics_frame
{
  FILE * fp;
  enum graph_axis axis;
  enum graph_pair_orientation ornt;
  int tic_cnt;
};

static void
write_tics_thunk (struct write_tics_frame * fr, CELL * cp, CELLREF r, CELLREF c)
{
  char * str = graph_quoted_str (print_cell (cp));
  if (fr->tic_cnt)
    fputs (", ", fr->fp);
  fprintf (fr->fp, "%s ", str);
  if (fr->ornt == graph_implicit)
    fprintf (fr->fp, "%d", fr->tic_cnt);
  else
    {
      CELLREF x_r = r - graph_ornt_row_magic [fr->ornt];
      CELLREF x_c = c - graph_ornt_col_magic [fr->ornt];
      
      fprintf (fr->fp, "%s", print_cell (find_cell (x_r, x_c)));
    }
  ++fr->tic_cnt;
}


static void
write_tics_command (FILE * fp, enum graph_axis axis, struct rng * rng, enum graph_pair_ordering order)
{
  struct write_tics_frame fr;
  fr.fp = fp;
  fr.axis = axis;
  fr.tic_cnt = 0;
  fr.ornt = order % graph_num_pair_orientations;
  fprintf (fp, "set %stics (", graph_axis_name[axis]);
  for_pairs_in (rng, order, (fpi_thunk)write_tics_thunk, &fr);
  fputs (")\n", fp);
}

struct get_symbols_frame
{
  int symbols;
  char ** names;
};

static void
get_symbols_thunk (struct get_symbols_frame * fr,
		   CELL * cell, CELLREF r, CELLREF c)
{
  fr->names = (char **)ck_realloc (fr->names,
				   (fr->symbols + 1) * sizeof (char *));
  fr->names [fr->symbols] = ck_savestr (print_cell (cell));
  ++fr->symbols;
}


struct write_data_frame
{
  FILE * fp;
  enum graph_pair_ordering ornt;
  int data_cnt;
  struct get_symbols_frame gsf;
};

static void
write_data_thunk (struct write_data_frame * fr,
		  CELL * y_cell, CELLREF r, CELLREF c) 
{
  if (!y_cell || !GET_TYP(y_cell))
    return;
  if (fr->ornt == graph_implicit)
    fprintf (fr->fp, "%d ", fr->data_cnt);
  else
    {
      CELLREF x_r = r - graph_ornt_row_magic [fr->ornt];
      CELLREF x_c = c - graph_ornt_col_magic [fr->ornt];
      CELL * x_cell = find_cell (x_r, x_c);
      if (x_cell && GET_TYP(x_cell))
	{
	  if (graph_axis_symbols [graph_x].lr == NON_ROW)
	    fprintf (fr->fp, "%s ", print_cell (x_cell));
	  else
	    {
	      char * key = print_cell (x_cell);
	      int x;
	      for (x = 0; x < fr->gsf.symbols; ++x)
		if (stricmp (key, fr->gsf.names[x]))
		  {
		    fprintf (fr->fp, "%d ", x);
		    break;
		  }
	    }
	}
    }
  fprintf (fr->fp, " %s\n", print_cell (y_cell));
  ++fr->data_cnt;
}


static void
spew_gnuplot (FILE  * fp, struct line * data_files, char * dir, char * dbase)
{
  fprintf (fp, "set terminal %s\n", graph_term_cmd.buf);
  fprintf (fp, "set output %s\n",
	   (graph_output_file.buf[0]
	    ? graph_quoted_str (graph_output_file.buf)
	    : ""));

  {
    enum graph_axis axis;
    for (axis = graph_x; axis <= graph_y; ++axis)
      {
	fprintf (fp, "set %slabel %s\n", graph_axis_name[axis],
		 (graph_axis_title[axis].buf[0]
		  ? graph_axis_title[axis].buf
		  : ""));

	fprintf (fp, "set %srange [%s:%s]\n",
		 graph_axis_name [axis],
		 (says_default (graph_rng_lo [axis].buf)
		  ? ""
		  : graph_rng_lo[axis].buf),
		 (says_default (graph_rng_hi [axis].buf)
		  ? ""
		  : graph_rng_hi[axis].buf));
	if (   (graph_axis_symbols [axis].lr != NON_ROW)
	    && (graph_axis_labels [axis].lr == NON_ROW))
	  write_tics_command (fp, axis, &graph_axis_symbols [axis],
			      PAIR_ORDER(graph_axis_ordering[axis],
						   graph_implicit));
	else if (graph_axis_labels [axis].lr != NON_ROW)
	  write_tics_command (fp, axis,
			      &graph_axis_labels [axis],
			      graph_axis_label_order [axis]);
	else
	  fprintf (fp, "set %stics\n", graph_axis_name [axis]);
      }
    if (!(graph_logness[graph_x] && graph_logness[graph_y]))
      fprintf (fp, "set nolog %s%s\n",
	       graph_logness[graph_x] ? "" : "x",
	       graph_logness[graph_y] ? "" : "y");

    if (graph_logness[graph_x] || graph_logness[graph_y])
      fprintf (fp, "set log %s%s\n",
	       graph_logness[graph_x] ? "x" : "",
	       graph_logness[graph_y] ? "y" : "");
  }
  {
    int need_comma = 0;
    int x;
    fprintf (fp, "plot ");
    for (x = 0; x < NUM_DATASETS; ++x)
      {
	init_line (&data_files [x]);
	if (graph_data [x].lr != NON_ROW)
	  {
	    FILE * df = mk_tmp_file (&data_files [x], dir, dbase);
	    struct write_data_frame wdf;
	    if (!df)
	      {
		/* a small core leak here... */
		io_error_msg ("Error opening temp file `%s' for graph data.",
			      data_files [x].buf);
	      }
	    wdf.fp = df;
	    wdf.ornt = graph_data_order [x] % graph_num_pair_orientations;
	    wdf.data_cnt = 0;
	    wdf.gsf.symbols = 0;
	    wdf.gsf.names = 0;
	    if (graph_axis_symbols [graph_x].lr != NON_ROW)
	      for_pairs_in (&graph_axis_symbols [graph_x],
			    PAIR_ORDER (graph_axis_ordering [graph_x],
					graph_implicit),
			    (fpi_thunk)get_symbols_thunk,
			    &wdf.gsf);
	    for_pairs_in (&graph_data [x], graph_data_order [x],
			  (fpi_thunk)write_data_thunk, &wdf);
	    if (graph_axis_symbols [graph_x].lr != NON_ROW)
	      {
		int x;
		for (x = 0; x < wdf.gsf.symbols; ++x)
		  free (wdf.gsf.names[x]);
		ck_free (wdf.gsf.names);
	      }
	    fprintf (fp, "%s %s title %s with %s",
		     need_comma ? "," : "",
		     graph_quoted_str (data_files [x].buf),
		     graph_title [x].buf,
		     graph_style [x].buf);
	    need_comma = 1;
	    fclose (df);
	  }
      }
    fprintf (fp, "\n");
  }
}


static void
graph_spew_with_parameters (struct line * shell_script, struct line *
			    gp_script, char * last_cmd, char * dir, char *
			    dbase, char * gbase, char * sbase, int run_gnuplot)
{
  struct line data_files [NUM_DATASETS];
  FILE * fp;
  fp = mk_tmp_file (gp_script, dir, gbase);
  if (!fp)
    {
      /* coreleak filename */
      io_error_msg ("Error opening tmp file `%s' for plot script.",
		    gp_script->buf);
    }
  spew_gnuplot (fp, data_files, dir, dbase);
  if (last_cmd)
    fputs (last_cmd, fp);
  if (sbase)
    {
      FILE * shfp;
      init_line (shell_script);
      shfp = mk_tmp_file (shell_script, dir, sbase);
      fprintf (shfp, "#!%s\n", gnuplot_shell);
      fputs ("\n", shfp);
      if (run_gnuplot)
	fprintf (shfp, "%s %s\n", gnuplot_program, gp_script->buf);
      fprintf (shfp, "%s %s %s  ",
	       rm_program, gp_script->buf, shell_script->buf);
      {
	int x;
	for (x = 0; x < NUM_DATASETS; ++x)
	  if (data_files[x].buf)
	    fprintf (shfp, "%s ", data_files [x].buf);
      }
      fputs ("\n", shfp);
      fclose (shfp);
    }
  fclose (fp);
}


static FILE * pipe_to_gnuplot = 0;
static char * cleanup_script = 0;
static char * gnuplot_script = 0;

static void death_to_gnuplot (void);

static void
gnuplot_exception (int fd)
{
  death_to_gnuplot ();
}

static void
gnuplot_writable (int fd)
{
  FD_CLR (fd, &write_fd_set);
  file_write_hooks [fd].hook_fn = 0;
  fprintf (pipe_to_gnuplot, "\n\nload %s\n", graph_quoted_str (gnuplot_script));
  fflush (pipe_to_gnuplot);
}

static void
ensure_gnuplot_pipe (void)
{
  if (!pipe_to_gnuplot)
    {
      pipe_to_gnuplot = popen (gnuplot_program, "w");
      if (!pipe_to_gnuplot)
	io_error_msg ("Can't popen gnuplot.");
    }
  if (gnuplot_script)
    {
      file_write_hooks [fileno (pipe_to_gnuplot)].hook_fn = gnuplot_writable;
      file_exception_hooks [fileno (pipe_to_gnuplot)].hook_fn = gnuplot_exception;
      FD_SET (fileno (pipe_to_gnuplot), &write_fd_set);
      FD_SET (fileno (pipe_to_gnuplot), &exception_fd_set);
    }
}

static void
death_to_gnuplot (void)
{
  if (pipe_to_gnuplot)
    {
      int fd = fileno (pipe_to_gnuplot);
      file_write_hooks [fd].hook_fn = 0;
      file_exception_hooks [fd].hook_fn = 0;
      FD_CLR (fd, &write_fd_set);
      FD_CLR (fd, &exception_fd_set);
      pclose (pipe_to_gnuplot);
    }
  pipe_to_gnuplot = 0;
  if (cleanup_script)
    system (cleanup_script);
  if (cleanup_script)
    ck_free (cleanup_script);
  if (gnuplot_script)
    ck_free (gnuplot_script);
}





static void
spew_for_x (void)
{
  struct line shell_script;
  struct line gp_script;
  init_line (&shell_script);
  init_line (&gp_script);
  graph_spew_with_parameters (&shell_script, &gp_script, "pause -1\n", 0,
			      "#data", "#plot", "#sh", 0);
  splicen_line (&shell_script, " ", 1, 0);
  splicen_line (&shell_script, gnuplot_shell, strlen (gnuplot_shell), 0);
  if (cleanup_script)
    {
      system (cleanup_script);
      free (cleanup_script);
    }
  cleanup_script = shell_script.buf;
  if (gnuplot_script)
    free (gnuplot_script);
  gnuplot_script = gp_script.buf;
  ensure_gnuplot_pipe ();
}

static void
spew_for_ps (void)
{
  struct line shell_script;
  struct line gp_script;
  init_line (&shell_script);
  init_line (&gp_script);
  graph_spew_with_parameters (&shell_script, &gp_script, 0, "", "#data",
			      "#plot", "#sh", 1); 
  splicen_line (&shell_script, " ", 1, 0);
  splicen_line (&shell_script, gnuplot_shell, strlen (gnuplot_shell), 0);
  system (shell_script.buf);
  free_line (&shell_script);
  free_line (&gp_script);
}

void
graph_plot (void)
{
  plot_fn ();
}