~ubuntu-branches/ubuntu/gutsy/plotutils/gutsy

« back to all changes in this revision

Viewing changes to plot/plot.c

  • Committer: Bazaar Package Importer
  • Author(s): Floris Bruynooghe
  • Date: 2007-05-10 19:48:54 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070510194854-mrr3lgwzpxd8hovo
Tags: 2.5-2
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the GNU plotutils package.  Copyright (C) 1989,
 
2
   1990, 1991, 1995, 1996, 1997, 1998, 1999, 2000, 2005, Free Software
 
3
   Foundation, Inc.
 
4
 
 
5
   The GNU plotutils package is free software.  You may redistribute it
 
6
   and/or modify it under the terms of the GNU General Public License as
 
7
   published by the Free Software foundation; either version 2, or (at your
 
8
   option) any later version.
 
9
 
 
10
   The GNU plotutils package is distributed in the hope that it will be
 
11
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
   General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU General Public License along
 
16
   with the GNU plotutils package; see the file COPYING.  If not, write to
 
17
   the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
 
18
   Boston, MA 02110-1301, USA. */
 
19
 
1
20
/* This file is the driving routine for the GNU `plot' program.  It
2
21
   includes code to read a stream of commands, in GNU metafile format, and
3
 
   call libplot functions to draw the graphics.
4
 
 
5
 
   Copyright (C) 1989-2000 Free Software Foundation, Inc. */
 
22
   call libplot functions to draw the graphics. */
6
23
 
7
24
#include "sys-defines.h"
 
25
#include "libcommon.h"
 
26
#include "getopt.h"
 
27
#include "fontlist.h"
8
28
#include "plot.h"
9
 
#include "getopt.h"
10
29
 
11
30
/* Obsolete op codes (no longer listed in plot.h) */
12
31
#define O_COLOR 'C'
44
63
} plot_format;
45
64
 
46
65
const char *progname = "plot";  /* name of this program */
 
66
const char *written = "Written by Robert S. Maier.";
 
67
const char *copyright = "Copyright (C) 2005 Free Software Foundation, Inc.";
47
68
 
48
69
const char *usage_appendage = " [FILE]...\n\
49
70
With no FILE, or when FILE is -, read standard input.\n";
67
88
   of erase()) from the output */
68
89
bool merge_pages = false;
69
90
 
70
 
/* Long options we recognize */
 
91
/* options */
71
92
 
72
93
#define ARG_NONE        0
73
94
#define ARG_REQUIRED    1
74
95
#define ARG_OPTIONAL    2
75
96
 
 
97
const char *optstring = "shlAIOp:F:f:W:T:";
 
98
 
76
99
struct option long_options[] = 
77
100
{
78
 
  /* The most important option */
79
 
  { "display-type",     ARG_REQUIRED,   NULL, 'T'},
 
101
  /* The most important option ("--display-type" is an obsolete variant) */
 
102
  { "output-format",    ARG_REQUIRED,   NULL, 'T'},
 
103
  { "display-type",     ARG_REQUIRED,   NULL, 'T' << 8 }, /* hidden */
80
104
  /* Other frequently used options */
81
105
  { "font-name",        ARG_REQUIRED,   NULL, 'F' },
82
106
  { "font-size",        ARG_REQUIRED,   NULL, 'f' },
107
131
  { NULL,               0,              NULL,  0}
108
132
};
109
133
    
110
 
/* null-terminated list of options that we don't show to the user */
111
 
int hidden_options[] = { (int)'I', 0 };
 
134
/* null-terminated list of options, such as obsolete-but-still-maintained
 
135
   options or undocumented options, which we don't show to the user */
 
136
const int hidden_options[] = { (int)'I', (int)('T' << 8), 0 };
112
137
 
113
138
 
114
139
/* forward references */
115
 
bool read_plot ____P((plPlotter *plotter, FILE *in_stream));
116
 
char *read_string ____P((FILE *input, bool *badstatus));
117
 
double read_float ____P((FILE *input, bool *badstatus));
118
 
double read_int ____P((FILE *input, bool *badstatus));
119
 
int maybe_closepl ____P((plPlotter *plotter));
120
 
int maybe_openpl ____P((plPlotter *plotter));
121
 
int read_true_int ____P((FILE *input, bool *badstatus));
122
 
unsigned char read_byte_as_unsigned_char ____P((FILE *input, bool *badstatus));
123
 
unsigned int read_byte_as_unsigned_int ____P((FILE *input, bool *badstatus));
124
 
/* from libcommon */
125
 
extern int display_fonts ____P((const char *display_type, const char *progname));
126
 
extern int list_fonts ____P((const char *display_type, const char *progname));
127
 
extern void display_usage ____P((const char *progname, const int *omit_vals, const char *appendage, bool fonts));
128
 
extern void display_version ____P((const char *progname)); 
129
 
extern voidptr_t xcalloc ____P ((size_t nmemb, size_t size));
130
 
extern voidptr_t xmalloc ____P ((size_t size));
131
 
extern voidptr_t xrealloc ____P ((voidptr_t p, size_t length));
132
 
extern char *xstrdup ____P ((const char *s));
 
140
bool read_plot (plPlotter *plotter, FILE *in_stream);
 
141
char *read_string (FILE *input, bool *badstatus);
 
142
double read_float (FILE *input, bool *badstatus);
 
143
double read_int (FILE *input, bool *badstatus);
 
144
int maybe_closepl (plPlotter *plotter);
 
145
int maybe_openpl (plPlotter *plotter);
 
146
int read_true_int (FILE *input, bool *badstatus);
 
147
unsigned char read_byte_as_unsigned_char (FILE *input, bool *badstatus);
 
148
unsigned int read_byte_as_unsigned_int (FILE *input, bool *badstatus);
133
149
 
134
150
 
135
151
int
136
 
#ifdef _HAVE_PROTOS
137
152
main (int argc, char *argv[])
138
 
#else
139
 
main (argc, argv)
140
 
     int argc;
141
 
     char *argv[];
142
 
#endif
143
153
{
144
154
  plPlotter *plotter;
145
155
  plPlotterParams *plotter_params;
147
157
  bool show_fonts = false;      /* supply help on fonts? */
148
158
  bool show_usage = false;      /* show usage message? */
149
159
  bool show_version = false;    /* show version message? */
150
 
  char *display_type = (char *)"meta"; /* default libplot output format */
 
160
  char *output_format = (char *)"meta"; /* default libplot output format */
151
161
  int errcnt = 0;               /* errors encountered */
152
162
  int local_page_number;        /* temporary storage */
153
163
  int opt_index;                /* long option index */
155
165
  int retval;                   /* return value */
156
166
 
157
167
  plotter_params = pl_newplparams ();
158
 
  while ((option = getopt_long (argc, argv, "shlAIOp:F:f:W:T:", long_options, &opt_index)) != EOF)
 
168
  while ((option = getopt_long (argc, argv, optstring, long_options, &opt_index)) != EOF)
159
169
    {
160
170
      if (option == 0)
161
171
        option = long_options[opt_index].val;
162
172
      
163
173
      switch (option) 
164
174
        {
165
 
        case 'T':               /* Display type, ARG REQUIRED      */
166
 
          display_type = (char *)xmalloc (strlen (optarg) + 1);
167
 
          strcpy (display_type, optarg);
 
175
        case 'T':               /* Output format, ARG REQUIRED      */
 
176
        case 'T' << 8:
 
177
          output_format = (char *)xmalloc (strlen (optarg) + 1);
 
178
          strcpy (output_format, optarg);
168
179
          break;
169
180
        case 'O':               /* Ascii output */
170
 
          pl_setplparam (plotter_params, "META_PORTABLE", (voidptr_t)"yes");
 
181
          pl_setplparam (plotter_params, "META_PORTABLE", (void *)"yes");
171
182
          break;
172
183
        case 'F':               /* set the initial font */
173
184
          font_name = (char *)xmalloc (strlen (optarg) + 1);
174
185
          strcpy (font_name, optarg);
175
186
          break;
176
187
        case 'e' << 8:          /* emulate color by grayscale */
177
 
          pl_setplparam (plotter_params, "EMULATE_COLOR", (voidptr_t)optarg);
 
188
          pl_setplparam (plotter_params, "EMULATE_COLOR", (void *)optarg);
178
189
          break;
179
190
        case 'C' << 8:          /* set the initial pen color */
180
191
          pen_color = (char *)xmalloc (strlen (optarg) + 1);
185
196
          strcpy (bg_color, optarg);
186
197
          break;
187
198
        case 'B' << 8:          /* Bitmap size */
188
 
          pl_setplparam (plotter_params, "BITMAPSIZE", (voidptr_t)optarg);
 
199
          pl_setplparam (plotter_params, "BITMAPSIZE", (void *)optarg);
189
200
          break;
190
201
        case 'P' << 8:          /* Page size */
191
 
          pl_setplparam (plotter_params, "PAGESIZE", (voidptr_t)optarg);
 
202
          pl_setplparam (plotter_params, "PAGESIZE", (void *)optarg);
192
203
          break;
193
204
        case 'f':               /* set the initial fontsize */
194
205
          {
257
268
          user_specified_input_format = GNU_OLD_PORTABLE;
258
269
          break;
259
270
        case 'r' << 8:          /* Plot rotation angle, ARG REQUIRED    */
260
 
          pl_setplparam (plotter_params, "ROTATION", (voidptr_t)optarg);
 
271
          pl_setplparam (plotter_params, "ROTATION", (void *)optarg);
261
272
          break;
262
273
        case 'M' << 8:          /* Max line length */
263
 
          pl_setplparam (plotter_params, "MAX_LINE_LENGTH", (voidptr_t)optarg);
 
274
          pl_setplparam (plotter_params, "MAX_LINE_LENGTH", (void *)optarg);
264
275
          break;
265
276
        case 's':               /* Merge pages */
266
277
          merge_pages = true;
292
303
    }
293
304
  if (show_version)
294
305
    {
295
 
      display_version (progname);
 
306
      display_version (progname, written, copyright);
296
307
      return EXIT_SUCCESS;
297
308
    }
298
309
  if (do_list_fonts)
299
310
    {
300
311
      int success;
301
312
 
302
 
      success = list_fonts (display_type, progname);
 
313
      success = list_fonts (output_format, progname);
303
314
      if (success)
304
315
        return EXIT_SUCCESS;
305
316
      else
309
320
    {
310
321
      int success;
311
322
 
312
 
      success = display_fonts (display_type, progname);
 
323
      success = display_fonts (output_format, progname);
313
324
      if (success)
314
325
        return EXIT_SUCCESS;
315
326
      else
323
334
 
324
335
  if (bg_color)
325
336
    /* select user-specified background color */
326
 
    pl_setplparam (plotter_params, "BG_COLOR", (voidptr_t)bg_color);
 
337
    pl_setplparam (plotter_params, "BG_COLOR", (void *)bg_color);
327
338
 
328
 
  if ((plotter = pl_newpl_r (display_type, NULL, stdout, stderr,
 
339
  if ((plotter = pl_newpl_r (output_format, NULL, stdout, stderr,
329
340
                             plotter_params)) == NULL)
330
341
    {
331
342
      fprintf (stderr, "%s: error: could not create plot device\n", progname);
417
428
   the file.  Return value indicates whether stream was parsed
418
429
   successfully. */
419
430
bool
420
 
#ifdef _HAVE_PROTOS
421
431
read_plot (plPlotter *plotter, FILE *in_stream)
422
 
#else
423
 
read_plot (plotter, in_stream)
424
 
     plPlotter *plotter;
425
 
     FILE *in_stream;
426
 
#endif
427
432
{
428
433
  bool argerr = false;  /* error occurred while reading argument? */
429
434
  bool display_open = false;    /* display device open? */
1549
1554
}
1550
1555
 
1551
1556
int
1552
 
#ifdef _HAVE_PROTOS
1553
1557
maybe_openpl (plPlotter *plotter)
1554
 
#else
1555
 
maybe_openpl (plotter)
1556
 
     plPlotter *plotter;
1557
 
#endif
1558
1558
{
1559
1559
  if (merge_pages)
1560
1560
    return 0;
1563
1563
}
1564
1564
 
1565
1565
int
1566
 
#ifdef _HAVE_PROTOS
1567
1566
maybe_closepl (plPlotter *plotter)
1568
 
#else
1569
 
maybe_closepl (plotter)
1570
 
     plPlotter *plotter;
1571
 
#endif
1572
1567
{
1573
1568
  if (merge_pages)
1574
1569
    return 0;
1579
1574
 
1580
1575
/* read a single byte from input stream, return as unsigned char (0..255) */
1581
1576
unsigned char
1582
 
#ifdef _HAVE_PROTOS
1583
1577
read_byte_as_unsigned_char (FILE *input, bool *badstatus)
1584
 
#else
1585
 
read_byte_as_unsigned_char (input, badstatus)
1586
 
     FILE *input;
1587
 
     bool *badstatus;
1588
 
#endif
1589
1578
{
1590
1579
  int newint;
1591
1580
 
1605
1594
 
1606
1595
/* read a single byte from input stream, return as unsigned int (0..255) */
1607
1596
unsigned int
1608
 
#ifdef _HAVE_PROTOS
1609
1597
read_byte_as_unsigned_int (FILE *input, bool *badstatus)
1610
 
#else
1611
 
read_byte_as_unsigned_int (input, badstatus)
1612
 
     FILE *input;
1613
 
     bool *badstatus;
1614
 
#endif
1615
1598
{
1616
1599
  int newint;
1617
1600
 
1633
1616
   format for integers or short integers, or perhaps in crufty old 2-byte
1634
1617
   format) */
1635
1618
int
1636
 
#ifdef _HAVE_PROTOS
1637
1619
read_true_int (FILE *input, bool *badstatus)
1638
 
#else
1639
 
read_true_int (input, badstatus)
1640
 
     FILE *input;
1641
 
     bool *badstatus;
1642
 
#endif
1643
1620
{
1644
1621
  int x, zi, returnval;
1645
1622
  short zs;
1705
1682
   (human-readable) format is used, a floating point number may substitute
1706
1683
   for the integer */
1707
1684
double
1708
 
#ifdef _HAVE_PROTOS
1709
1685
read_int (FILE *input, bool *badstatus)
1710
 
#else
1711
 
read_int (input, badstatus)
1712
 
     FILE *input;
1713
 
     bool *badstatus;
1714
 
#endif
1715
1686
{
1716
1687
  int x, zi, returnval;
1717
1688
  short zs;
1780
1751
/* read a floating point quantity from input stream (may be in ascii format
1781
1752
   or system single-precision format) */
1782
1753
double
1783
 
#ifdef _HAVE_PROTOS
1784
1754
read_float (FILE *input, bool *badstatus)
1785
 
#else
1786
 
read_float (input, badstatus)
1787
 
     FILE *input;
1788
 
     bool *badstatus;
1789
 
#endif
1790
1755
{
1791
1756
  float f;
1792
1757
  int returnval;
1828
1793
   string, with \0 replacing \n, is allocated on the heap and may be
1829
1794
   freed. */
1830
1795
char *
1831
 
#ifdef _HAVE_PROTOS
1832
1796
read_string (FILE *input, bool *badstatus)
1833
 
#else
1834
 
read_string (input, badstatus)
1835
 
     FILE *input;
1836
 
     bool *badstatus;
1837
 
#endif
1838
1797
{
1839
1798
  int length = 0, buffer_length = 16; /* initial length */
1840
1799
  char *buffer;