summaryrefslogtreecommitdiff
path: root/ghc/utils/hp2ps/Main.c
blob: 3b5efed51ba2e1c54d3a13e312c74b30669a3759 (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
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
#include "Main.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Defines.h"
#include "AuxFile.h"
#include "AreaBelow.h"
#include "Dimensions.h"
#include "HpFile.h"
#include "PsFile.h"
#include "Reorder.h"
#include "Scale.h"
#include "TopTwenty.h"
#include "TraceElement.h"
#include "Deviation.h"
#include "Error.h"
#include "Utilities.h"

boolish pflag = 0;	/* read auxiliary file			*/
boolish eflag = 0;	/* scaled EPSF 				*/ 
boolish dflag = 0;	/* sort by standard deviation		*/
int     iflag = 0;	/* sort by identifier (3-way flag)      */
boolish gflag = 0;	/* output suitable for previewer	*/
boolish yflag = 0; 	/* ignore marks				*/
boolish bflag = 0; 	/* use a big title box			*/
boolish sflag = 0;	/* use a small title box		*/
int     mflag = 0;	/* max no. of bands displayed (default 20) */
boolish tflag = 0;	/* ignored threshold specified          */
boolish cflag = 0;      /* colour output                        */

boolish filter;		/* true when running as a filter	*/

static floatish WidthInPoints PROTO((char *));		  /* forward */
static FILE *Fp PROTO((char *, char **, char *, char *)); /* forward */

char *hpfile;
char *psfile;
char *auxfile;

char *programname;

static char *pathName;
static char *baseName; /* "basename" is a std C library name (sigh) */

FILE* hpfp;
FILE* psfp;
FILE* auxfp;

floatish xrange = 0.0;
floatish yrange = 0.0;

floatish auxxrange = 0.0;
floatish auxyrange = 0.0;

floatish epsfwidth;
floatish areabelow;

intish nsamples;
intish nmarks;
intish nidents;

floatish THRESHOLD_PERCENT = DEFAULT_THRESHOLD;
int TWENTY = DEFAULT_TWENTY;

int main(argc, argv)
int argc;
char* argv[];
{

    programname = copystring(Basename(argv[0]));

    argc--, argv++;
    while (argc && argv[0][0] == '-') {
        while (*++*argv)
            switch(**argv) {
	    case 'p':
                pflag++;
                break;
	    case 'e':
		eflag++;
                epsfwidth = WidthInPoints(*argv + 1);
                goto nextarg;
	    case 'd':
		dflag++;
                goto nextarg;
	    case 'i':
		switch( *(*argv + 1) ) {
		  case '-':
		    iflag = -1;
		  case '+':
		  default:
		    iflag = 1;
		}
                goto nextarg;
	    case 'g':
		gflag++;
		goto nextarg;
	    case 'y':
		yflag++;
		goto nextarg;
	    case 'b':
		bflag++;
		goto nextarg;
	    case 's':
		sflag++;
		goto nextarg;
	    case 'm':
		mflag++;
		TWENTY = atoi(*argv + 1);
		if (TWENTY > DEFAULT_TWENTY)
		    Usage(*argv-1);
		goto nextarg;
	    case 't':
		tflag++;
		THRESHOLD_PERCENT = (floatish) atof(*argv + 1);
		if (THRESHOLD_PERCENT < 0 || THRESHOLD_PERCENT > 5)
		    Usage(*argv-1);
		goto nextarg;
	    case 'c':
		cflag++;
		goto nextarg;
	    case '?':
	    default:
		Usage(*argv-1);
            }
nextarg: ;
        argc--, argv++;
    }

    hpfile = "stdin";
    psfile = "stdout";

    hpfp = stdin;
    psfp = stdout;

    filter = argc < 1;



    if (!filter) {
	pathName = copystring(argv[0]);
	DropSuffix(pathName, ".hp");
	baseName = copystring(Basename(pathName));

        hpfp  = Fp(pathName, &hpfile, ".hp", "r"); 
	psfp  = Fp(baseName, &psfile, ".ps", "w"); 

	if (pflag) auxfp = Fp(baseName, &auxfile, ".aux", "r");
    }

    GetHpFile(hpfp);

    if (!filter && pflag) GetAuxFile(auxfp);


    TraceElement();          /* Orders on total, Removes trace elements (tflag) */

    if (dflag) Deviation();  /* ReOrders on deviation */

    if (iflag) Identorder(iflag); /* ReOrders on identifier */

    if (pflag) Reorder();    /* ReOrders on aux file */

    if (TWENTY) TopTwenty(); /* Selects top twenty (mflag) */

    Dimensions();

    areabelow = AreaBelow();

    Scale();

    PutPsFile();

    if (!filter) {
        auxfp = Fp(baseName, &auxfile, ".aux", "w");
	PutAuxFile(auxfp);
    } 

    return(0);
}



typedef enum {POINTS, INCHES, MILLIMETRES} pim;

static pim Units PROTO((char *));   /* forward */

static floatish
WidthInPoints(wstr)
  char *wstr;
{
    floatish result;

    result = (floatish) atof(wstr);

    switch (Units(wstr)) {
	case INCHES:  		
	    result *= 72.0;
	    break;
        case MILLIMETRES:	
	    result *= 2.834646;
	    break;
        case POINTS:
	default: ;
    }

    if (result <= 144)   /* Minimum of 2in wide ! */
	Usage(wstr);

    return result;
}

	
static pim
Units(wstr)
  char* wstr;
{
int i;

    i = strlen(wstr) - 2;

    if (wstr[i] == 'p' && wstr[i+1] == 't') {
	return POINTS;
    } else if (wstr[i] == 'i' && wstr[i+1] == 'n') {
	return INCHES;	
    } else if (wstr[i] == 'm' && wstr[i+1] == 'm') {
	return MILLIMETRES;
    } else {
        return POINTS;
    }
}

static FILE *
Fp(rootname, filename, suffix, mode)
  char* rootname; char** filename; char* suffix; char* mode;
{
    *filename = copystring2(rootname, suffix);

    return(OpenFile(*filename, mode));
}

#ifdef DEBUG
void
_stgAssert (filename, linenum)
  char		*filename;
  unsigned int  linenum;
{
    fflush(stdout);
    fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
    fflush(stderr);
    abort();
}
#endif