]> git.pond.sub.org Git - empserver/blob - src/lib/subs/sarg.c
Update copyright notice
[empserver] / src / lib / subs / sarg.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  sarg.c: Parse selection arguments
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Markus Armbruster, 2004-2010
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include "misc.h"
38 #include "nat.h"
39 #include "nsc.h"
40 #include "player.h"
41 #include "prototypes.h"
42 #include "xy.h"
43
44 /*
45  * returns one of
46  *
47  *  #1, lx:ly,hx:hy --> NS_AREA
48  *  @x,y:dist  --> NS_DIST
49  *  %d or %d/%d/%d --> NS_LIST
50  *  ~ or letter --> NS_GROUP
51  *  * --> NS_ALL
52  *
53  * or 0 for none of the above.
54  */
55 enum ns_seltype
56 sarg_type(char *str)
57 {
58     int c;
59
60     c = *str;
61     if (c == '@')
62         return NS_DIST;
63     if (c == '*')
64         return NS_ALL;
65     if (c == '#' || strchr(str, ','))
66         return NS_AREA;
67     if (isdigit(c))
68         return NS_LIST;
69     if (c == '~' || isalpha(c))
70         return NS_GROUP;
71     return NS_UNDEF;
72 }
73
74 int
75 sarg_xy(char *str, coord *xp, coord *yp)
76 {
77     coord x, y;
78     struct natstr *np;
79
80     x = strtox(str, &str);
81     if (x < 0 || *str++ != ',')
82         return 0;
83     y = strtoy(str, &str);
84     if (y < 0 || (*str != 0 && !isspace(*str)))
85         return 0;
86     if ((x ^ y) & 1)
87         return 0;
88     np = getnatp(player->cnum);
89     *xp = xabs(np, x);
90     *yp = yabs(np, y);
91     return 1;
92 }
93
94 /*
95  * translate #1 or lx:ly,hx:hy into a result range struct
96  * returns absolute coords
97  */
98 int
99 sarg_area(char *str, struct range *rp)
100 {
101     long rlm;
102     struct natstr *np;
103     struct realmstr realm;
104     char *end;
105
106     if (*str == '#') {
107         /*
108          * realm #X where (X > 0 && X < MAXNOR)
109          * Assumes realms are in abs coordinates
110          */
111         if (*++str) {
112             rlm = strtol(str, &end, 10);
113             if (end == str || (*end != 0 && !isspace(*end))
114                 || rlm < 0 || MAXNOR <= rlm)
115                 return 0;
116         } else
117             rlm = 0;
118         getrealm(rlm, player->cnum, &realm);
119         rp->lx = realm.r_xl;
120         rp->hx = realm.r_xh;
121         rp->ly = realm.r_yl;
122         rp->hy = realm.r_yh;
123     } else {
124         /*
125          * full map specification
126          * LX:LY,HX:HY where
127          * ly, hy are optional.
128          */
129         rp->lx = rp->hx = strtox(str, &str);
130         if (rp->lx < 0)
131             return 0;
132         if (*str == ':') {
133             rp->hx = strtox(str + 1, &str);
134             if (rp->hx < 0)
135                 return 0;
136         }
137         if (*str++ != ',')
138             return 0;
139         rp->ly = rp->hy = strtoy(str, &str);
140         if (rp->ly < 0)
141             return 0;
142         if (*str == ':') {
143             rp->hy = strtoy(str + 1, &str);
144             if (rp->hy < 0)
145                 return 0;
146         }
147         if (*str != 0 && !isspace(*str))
148             return 0;
149         np = getnatp(player->cnum);
150         rp->lx = xabs(np, rp->lx);
151         rp->hx = xabs(np, rp->hx);
152         rp->ly = yabs(np, rp->ly);
153         rp->hy = yabs(np, rp->hy);
154     }
155     xysize_range(rp);
156     return 1;
157 }
158
159 /*
160  * translate @x,y:int into
161  * result params
162  */
163 int
164 sarg_range(char *str, coord *xp, coord *yp, int *dist)
165 {
166     coord x, y;
167     long d;
168     char *end;
169     struct natstr *np;
170
171     if (*str++ != '@')
172         return 0;
173     x = strtox(str, &str);
174     if (x < 0 || *str++ != ',')
175         return 0;
176     y = strtoy(str, &str);
177     if (y < 0 || *str++ != ':')
178         return 0;
179     d = strtol(str, &end, 10);
180     if (end == str || (*end != 0 && !isspace(*end)) || d < 0)
181         return 0;
182     *dist = d;
183     np = getnatp(player->cnum);
184     *xp = xabs(np, x);
185     *yp = yabs(np, y);
186     return 1;
187 }
188
189 /*
190  * list of idents; id/id/id/id/id
191  */
192 int
193 sarg_list(char *str, int *list, int max)
194 {
195     int i, j;
196     long n;
197     char *end;
198
199     i = 0;
200     do {
201         n = strtol(str, &end, 10);
202         if (end == str || n < 0) {
203             pr("Illegal character '%c'\n", *str);
204             return 0;
205         }
206         for (j = 0; j < i; j++) {
207             if (list[j] == n)
208                 break;
209         }
210         if (j == i) {
211             if (i >= max) {
212                 pr("List too long (limit is %d)\n", max);
213                 return 0;
214             }
215             list[i++] = n;
216         }
217         str = end;
218     } while (*str++ == '/');
219
220     if (str[-1] != 0 && !isspace(str[-1])) {
221         pr("Expecting '/', got '%c'\n", str[-1]);
222         return 0;
223     }
224     return i;
225 }