summaryrefslogtreecommitdiff
path: root/dviware/dvipage/args.c
blob: f6ea46127646450697d54487229a079e7fb41c24 (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
/*
 * dvipage: DVI Previewer Program for Suns
 *
 * Neil Hunt (hunt@spar.slb.com)
 *
 * This program is based, in part, upon the program dvisun,
 * distributed by the UnixTeX group, extensively modified by
 * Neil Hunt at the Schlumberger Palo Alto Research Laboratories
 * of Schlumberger Technologies, Inc.
 *
 * Copyright (c) 1988 Schlumberger Technologies, Inc 1988.
 * Anyone can use this software in any manner they choose,
 * including modification and redistribution, provided they make
 * no charge for it, and these conditions remain unchanged.
 *
 * This program is distributed as is, with all faults (if any), and
 * without any warranty. No author or distributor accepts responsibility
 * to anyone for the consequences of using it, or for whether it serves any
 * particular purpose at all, or any other reason.
 *
 * $Log:	args.c,v $
 * Revision 1.1  88/11/28  18:39:09  hunt
 * Initial revision
 * 
 * Stripped from dvipage.c 1.4.
 */

#include <stdio.h>
#include <sys/param.h>		/* For MAXPATHLEN */
#include <suntool/sunview.h>
#include "dvipage.h"

static char *	a_arg_ptr = NULL;
static int	a_arg_index = 0;
static bool	a_escape_seen;

char *		a_prog_name = "Anonymous";

/*
 * a_next:
 *	Returns the next flag in the command line,
 *	or A_ARG if it is not a flag,
 *	or A_END if there are no more args.
 */

char
a_next(argc, argv)
int argc;
char **argv;
{
	char opt;

	/*
	 * Checks.
	 */
	if(argv == NULL || argc < 1)
	{
		fprintf(stderr, "a_next: bad arguments\n");
		exit(2);
	}

	/*
	 * Get program name on first call.
	 */
	if(a_arg_index == 0)
	{
		a_prog_name = argv[0];
		a_arg_index = 1;
	}

	/*
	 * If there is part of the previous word left, then return it.
	 */
	if(a_arg_ptr && *a_arg_ptr)
		return *a_arg_ptr++;

	/*
	 * Return A_END after the end of the list.
	 */
	if(a_arg_index >= argc)
		return A_END;

	/*
	 * Look at the next word.
	 */
	a_arg_ptr = argv[a_arg_index++];

	/*
	 * If we have seen the escape "--",
	 * or if the first char of the word * is not a '-',
	 * or if this is an isolated "-",
	 * then return ARG.
	 */
	if(a_escape_seen || a_arg_ptr[0] != '-' || a_arg_ptr[1] == '\0')
		return A_ARG;

	/*
	 * Look at the next char.
	 */
	a_arg_ptr++;
	opt = *a_arg_ptr++;

	/*
	 * If the next char is '-', then this is the escape.
	 * start over...
	 */
	if(opt == '-')
	{
		a_escape_seen = TRUE;
		return a_next(argc, argv);
	}

	/*
	 * Otherwise, return this option.
	 */
	return opt;
}

/*
 * a_arg:
 *	Returns the next argument in the command line,
 *	or NULL if there are no more args.
 */

char *
a_arg(argc, argv)
int argc;
char **argv;
{
	char *arg;

	/*
	 * Checks.
	 */
	if(argv == NULL || argc < 1)
	{
		fprintf(stderr, "a_arg: bad arguments\n");
		exit(2);
	}

	/*
	 * Get program name on first call.
	 */
	if(a_arg_index == 0)
	{
		a_prog_name = argv[0];
		a_arg_index = 1;
	}

	/*
	 * If there is part of the previous word left, then return it.
	 */
	if(a_arg_ptr && *a_arg_ptr)
	{
		arg = a_arg_ptr;
		a_arg_ptr = NULL;
		return arg;
	}

	/*
	 * Return NULL after the end of the list.
	 */
	if(a_arg_index >= argc)
		return NULL;

	/*
	 * Return the next word.
	 */
	return argv[a_arg_index++];
}

/*
 * a_number:
 *	Interpret the next word or part word as a number.
 */

double
a_number(argc, argv)
int argc;
char **argv;
{
	char *arg;

	if((arg = a_arg(argc, argv)) == NULL)
		return 0.0;
	else
		return atof(arg);
}

/*
 * a_integer:
 *	Interpret the next word or part word as an integer.
 */

int
a_integer(argc, argv)
int argc;
char **argv;
{
	char *arg;

	if((arg = a_arg(argc, argv)) == NULL)
		return 0;
	else
		return atoi(arg);
}