]> 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-2015, 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 "file.h"
38 #include "misc.h"
39 #include "nat.h"
40 #include "nsc.h"
41 #include "player.h"
42 #include "prototypes.h"
43 #include "xy.h"
44
45 /*
46  * returns one of
47  *
48  *  #1, lx:ly,hx:hy --> NS_AREA
49  *  @x,y:dist  --> NS_DIST
50  *  %d or %d/%d/%d --> NS_LIST
51  *  ~ or letter --> NS_GROUP
52  *  * --> NS_ALL
53  *
54  * or 0 for none of the above.
55  */
56 enum ns_seltype
57 sarg_type(char *str)
58 {
59     int c;
60
61     c = *str;
62     if (c == '@')
63         return NS_DIST;
64     if (c == '*')
65         return NS_ALL;
66     if (c == '#' || strchr(str, ','))
67         return NS_AREA;
68     if (isdigit(c))
69         return NS_LIST;
70     if (c == '~' || isalpha(c))
71         return NS_GROUP;
72     return NS_UNDEF;
73 }
74
75 int
76 sarg_xy(char *str, coord *xp, coord *yp)
77 {
78     coord x, y;
79     struct natstr *np;
80
81     x = strtox(str, &str);
82     if (x < 0 || *str++ != ',')
83         return 0;
84     y = strtoy(str, &str);
85     if (y < 0 || (*str != 0 && !isspace(*str)))
86         return 0;
87     if ((x ^ y) & 1)
88         return 0;
89     np = getnatp(player->cnum);
90     *xp = xabs(np, x);
91     *yp = yabs(np, y);
92     return 1;
93 }
94
95 /*
96  * translate #1 or lx:ly,hx:hy into a result range struct
97  * returns absolute coords
98  */
99 int
100 sarg_area(char *str, struct range *rp)
101 {
102     long rlm;
103     struct natstr *np;
104     struct realmstr realm;
105     char *end;
106
107     if (*str == '#') {
108         /*
109          * realm #X where (X > 0 && X < MAXNOR)
110          * Assumes realms are in abs coordinates
111          */
112         if (*++str) {
113             rlm = strtol(str, &end, 10);
114             if (end == str || (*end != 0 && !isspace(*end))
115                 || rlm < 0 || MAXNOR <= rlm)
116                 return 0;
117         } else
118             rlm = 0;
119         getrealm(rlm, player->cnum, &realm);
120         rp->lx = realm.r_xl;
121         rp->hx = realm.r_xh;
122         rp->ly = realm.r_yl;
123         rp->hy = realm.r_yh;
124     } else {
125         /*
126          * full map specification
127          * LX:LY,HX:HY where
128          * ly, hy are optional.
129          */
130         rp->lx = rp->hx = strtox(str, &str);
131         if (rp->lx < 0)
132             return 0;
133         if (*str == ':') {
134             rp->hx = strtox(str + 1, &str);
135             if (rp->hx < 0)
136                 return 0;
137         }
138         if (*str++ != ',')
139             return 0;
140         rp->ly = rp->hy = strtoy(str, &str);
141         if (rp->ly < 0)
142             return 0;
143         if (*str == ':') {
144             rp->hy = strtoy(str + 1, &str);
145             if (rp->hy < 0)
146                 return 0;
147         }
148         if (*str != 0 && !isspace(*str))
149             return 0;
150         np = getnatp(player->cnum);
151         rp->lx = xabs(np, rp->lx);
152         rp->hx = xabs(np, rp->hx);
153         rp->ly = yabs(np, rp->ly);
154         rp->hy = yabs(np, rp->hy);
155     }
156     xysize_range(rp);
157     return 1;
158 }
159
160 /*
161  * translate @x,y:int into
162  * result params
163  */
164 int
165 sarg_range(char *str, coord *xp, coord *yp, int *dist)
166 {
167     coord x, y;
168     long d;
169     char *end;
170     struct natstr *np;
171
172     if (*str++ != '@')
173         return 0;
174     x = strtox(str, &str);
175     if (x < 0 || *str++ != ',')
176         return 0;
177     y = strtoy(str, &str);
178     if (y < 0 || *str++ != ':')
179         return 0;
180     d = strtol(str, &end, 10);
181     if (end == str || (*end != 0 && !isspace(*end)) || d < 0)
182         return 0;
183     *dist = d;
184     np = getnatp(player->cnum);
185     *xp = xabs(np, x);
186     *yp = yabs(np, y);
187     return 1;
188 }
189
190 /*
191  * list of idents; id/id/id/id/id
192  */
193 int
194 sarg_list(char *str, int *list, int max)
195 {
196     int i, j;
197     long n;
198     char *end;
199
200     i = 0;
201     do {
202         n = strtol(str, &end, 10);
203         if (end == str || n < 0) {
204             pr("Illegal character '%c'\n", *str);
205             return 0;
206         }
207         for (j = 0; j < i; j++) {
208             if (list[j] == n)
209                 break;
210         }
211         if (j == i) {
212             if (i >= max) {
213                 pr("List too long (limit is %d)\n", max);
214                 return 0;
215             }
216             list[i++] = n;
217         }
218         str = end;
219     } while (*str++ == '/');
220
221     if (str[-1] != 0 && !isspace(str[-1])) {
222         pr("Expecting '/', got '%c'\n", str[-1]);
223         return 0;
224     }
225     return i;
226 }