]> git.pond.sub.org Git - empserver/blob - src/lib/subs/sarg.c
(index, rindex): Obsolete BSDisms; remove. Use standard strchr() and
[empserver] / src / lib / subs / sarg.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  sarg.c: Parse selection arguments
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  */
33
34 #include <ctype.h>
35 #include <string.h>
36 #include "misc.h"
37 #include "player.h"
38 #include "xy.h"
39 #include "nsc.h"
40 #include "sect.h"
41 #include "nat.h"
42 #include "deity.h"
43 #include "file.h"
44 #include "prototypes.h"
45 #include "optlist.h"
46
47 /*
48  * returns one of
49  *
50  *  #1, lx:ly,hx:hy --> NS_AREA
51  *  @x,y:dist  --> NS_DIST
52  *  %d or %d/%d/%d --> NS_LIST
53  *  * --> NS_ALL
54  *
55  * or 0 for none of the above.
56  */
57 int
58 sarg_type(s_char *ptr)
59 {
60     int c;
61
62     c = *ptr;
63     if (c == '@')
64         return NS_DIST;
65     if (c == '*')
66         return NS_ALL;
67     if (c == '#' || strchr(ptr, ',') != 0)
68         return NS_AREA;
69     if (isdigit(c))
70         return NS_LIST;
71     if (c == '~' || isupper(c) || islower(c))
72         return NS_GROUP;
73     return 0;
74 }
75
76 int
77 sarg_xy(s_char *ptr, coord *xp, coord *yp)
78 {
79     if (sarg_type(ptr) != NS_AREA)
80         return 0;
81     *xp = atoip(&ptr);
82     if (*ptr++ != ',')
83         return 0;
84     if (!isdigit(*ptr) && *ptr != '-')
85         return 0;
86     *yp = atoi(ptr);
87     inputxy(xp, yp, player->cnum);
88     if ((*xp ^ *yp) & 01)
89         return 0;
90     return 1;
91 }
92
93 /* returns absolute coords */
94 static int
95 sarg_getrange(s_char *buf, register struct range *rp)
96 {
97     register int rlm;
98     register int c;
99     struct natstr *np;
100     s_char *bp;
101
102     bp = buf;
103     c = *bp;
104     if (c == '#') {
105         /*
106          * realm #X where (X > 0 && X < MAXNOR)
107          * Assumes realms are in abs coordinates
108          */
109         bp++;
110         rlm = atoi(bp);
111         if (rlm < 0 || rlm >= MAXNOR)
112             return 0;
113         np = getnatp(player->cnum);
114         rp->lx = np->nat_b[rlm].b_xl;
115         rp->hx = np->nat_b[rlm].b_xh;
116         rp->ly = np->nat_b[rlm].b_yl;
117         rp->hy = np->nat_b[rlm].b_yh;
118     } else {
119         /*
120          * full map specification
121          * LX:LY,HX:HY where
122          * ly, hy are optional.
123          */
124         if (!isdigit(c) && c != '-')
125             return 0;
126         rp->lx = rp->hx = atoip(&bp);
127         if (*bp == ':') {
128             bp++;
129             rp->hx = atoip(&bp);
130         }
131         if (*bp++ != ',')
132             return 0;
133         if (!isdigit(c) && c != '-')
134             return 0;
135         rp->ly = rp->hy = atoip(&bp);
136         if (*bp == ':') {
137             bp++;
138             rp->hy = atoip(&bp);
139         }
140         inputxy(&rp->lx, &rp->ly, player->cnum);
141         inputxy(&rp->hx, &rp->hy, player->cnum);
142     }
143     xysize_range(rp);
144     return 1;
145 }
146
147 /*
148  * translate #1 or lx:ly,hx:hy into
149  * a result range struct
150  */
151 int
152 sarg_area(s_char *buf, register struct range *rp)
153 {
154     if (!sarg_getrange(buf, rp))
155         return 0;
156     rp->hx += 1;
157     if (rp->hx >= WORLD_X)
158         rp->hx = 0;
159     rp->hy += 1;
160     if (rp->hy >= WORLD_Y)
161         rp->hy = 0;
162     xysize_range(rp);
163     return 1;
164 }
165
166 /*
167  * translate @x,y:int into
168  * result params
169  */
170 int
171 sarg_range(s_char *buf, coord *xp, coord *yp, int *dist)
172 {
173     s_char *bp;
174
175     bp = buf;
176     if (bp == 0 || *bp == 0)
177         return 0;
178     if (*bp++ != '@')
179         return 0;
180     *xp = atoip(&bp);
181     if (*bp++ != ',')
182         return 0;
183     *yp = atoip(&bp);
184     if (*bp++ != ':')
185         return 0;
186     inputxy(xp, yp, player->cnum);
187     *dist = atoi(bp);
188     return 1;
189 }
190
191 /*
192  * list of idents; id/id/id/id/id
193  */
194 int
195 sarg_list(s_char *str, register int *list, int max)
196 {
197     register int i;
198     register int j;
199     register int n;
200     s_char *arg;
201
202     arg = str;
203     for (i = 0; i < max; i++) {
204         if (!isdigit(*arg)) {
205             pr("Illegal character '%c'\n", *arg);
206             return 0;
207         }
208         n = atoip(&arg);
209         for (j = 0; j < i; j++) {
210             if (list[j] == n)
211                 break;
212         }
213         if (j != i) {
214             /* duplicate; ignore */
215             i--;
216         } else
217             list[i] = n;
218         if (*arg == 0)
219             break;
220         if (*arg != '/') {
221             pr("Expecting '/', got '%c'\n", *arg);
222             return 0;
223         }
224         arg++;
225         if (*arg == 0)
226             break;
227     }
228     return i + 1;
229 }