| 1 | // ============================================================= // |
|---|
| 2 | // // |
|---|
| 3 | // File : names.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // ============================================================= // |
|---|
| 10 | |
|---|
| 11 | #include <names_server.h> |
|---|
| 12 | #include <names_client.h> |
|---|
| 13 | #include "names.h" |
|---|
| 14 | |
|---|
| 15 | #include <arbdb.h> |
|---|
| 16 | #include <arb_file.h> |
|---|
| 17 | #include <arb_sleep.h> |
|---|
| 18 | |
|---|
| 19 | #include <names_prototypes.h> |
|---|
| 20 | #include <server.h> |
|---|
| 21 | #include <client.h> |
|---|
| 22 | #include <servercntrl.h> |
|---|
| 23 | #include <struct_man.h> |
|---|
| 24 | |
|---|
| 25 | #include <unistd.h> |
|---|
| 26 | #include <cctype> |
|---|
| 27 | #include <list> |
|---|
| 28 | #include <string> |
|---|
| 29 | |
|---|
| 30 | #define na_assert(cond) arb_assert(cond) |
|---|
| 31 | |
|---|
| 32 | #define FULLNAME_LEN_MAX 64 |
|---|
| 33 | #define NAME_LEN_MIN 2 |
|---|
| 34 | |
|---|
| 35 | using namespace std; |
|---|
| 36 | |
|---|
| 37 | // -------------------------------------------------------------------------------- |
|---|
| 38 | |
|---|
| 39 | // overloaded functions to avoid problems with type-punning: |
|---|
| 40 | inline long aisc_find_lib(dll_public *dll, char *key) { return aisc_find_lib(reinterpret_cast<dllpublic_ext*>(dll), key); } |
|---|
| 41 | |
|---|
| 42 | inline void aisc_link(dll_public *dll, AN_shorts *shorts) { aisc_link(reinterpret_cast<dllpublic_ext*>(dll), reinterpret_cast<dllheader_ext*>(shorts)); } |
|---|
| 43 | inline void aisc_link(dll_public *dll, AN_revers *revers) { aisc_link(reinterpret_cast<dllpublic_ext*>(dll), reinterpret_cast<dllheader_ext*>(revers)); } |
|---|
| 44 | |
|---|
| 45 | // -------------------------------------------------------------------------------- |
|---|
| 46 | |
|---|
| 47 | #if defined(DEBUG) |
|---|
| 48 | // #define DUMP_NAME_CREATION |
|---|
| 49 | #endif // DEBUG |
|---|
| 50 | |
|---|
| 51 | #define UPPERCASE(c) do { (c) = toupper(c); } while (0) |
|---|
| 52 | |
|---|
| 53 | // -------------------------------------------------------------------------------- |
|---|
| 54 | |
|---|
| 55 | struct AN_gl_struct { |
|---|
| 56 | aisc_com *cl_link; |
|---|
| 57 | Hs_struct *server_communication; |
|---|
| 58 | T_AN_MAIN cl_main; |
|---|
| 59 | char *server_name; |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | static struct AN_gl_struct AN_global; |
|---|
| 64 | AN_main *aisc_main; // muss so heissen |
|---|
| 65 | |
|---|
| 66 | const int SERVER_VERSION = 5; |
|---|
| 67 | |
|---|
| 68 | // -------------------------------------------------------------------------------- |
|---|
| 69 | |
|---|
| 70 | inline char *an_strlwr(char *str) { |
|---|
| 71 | for (int i = 0; str[i]; i++) { |
|---|
| 72 | str[i] = tolower(str[i]); |
|---|
| 73 | } |
|---|
| 74 | return str; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | inline int an_strnicmp(const char *s1, const char *s2, int len) { |
|---|
| 78 | int cmp = 0; |
|---|
| 79 | |
|---|
| 80 | for (int i = 0; i<len; i++) { |
|---|
| 81 | cmp = tolower(s1[i])-tolower(s2[i]); |
|---|
| 82 | if (cmp || !s1[i]) break; // different or string-end reached in both strings |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | return cmp; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | inline int an_stricmp(const char *s1, const char *s2) { |
|---|
| 89 | int cmp = 0; |
|---|
| 90 | |
|---|
| 91 | for (int i = 0; ; i++) { |
|---|
| 92 | cmp = tolower(s1[i])-tolower(s2[i]); |
|---|
| 93 | if (cmp || !s1[i]) break; // different or string-end reached in both strings |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | return cmp; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | static AN_revers *lookup_an_revers(AN_main *main, const char *shortname) { |
|---|
| 101 | char *key = an_strlwr(strdup(shortname)); |
|---|
| 102 | AN_revers *an_reverse = (AN_revers*)aisc_find_lib(&main->prevers, key); |
|---|
| 103 | |
|---|
| 104 | free(key); |
|---|
| 105 | |
|---|
| 106 | return an_reverse; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | static AN_shorts *lookup_an_shorts(AN_main *main, const char *identifier) { |
|---|
| 110 | // 'identifier' is either '*acc*add_id' or 'name1*name2*S' (see get_short() for details) |
|---|
| 111 | // 'add_id' is the value of an additional DB field and may be empty. |
|---|
| 112 | |
|---|
| 113 | char *key = an_strlwr(strdup(identifier)); |
|---|
| 114 | AN_shorts *an_shorts = (AN_shorts*)aisc_find_lib(&main->pnames, key); |
|---|
| 115 | |
|---|
| 116 | free(key); |
|---|
| 117 | |
|---|
| 118 | return an_shorts; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | // ---------------------------------------- |
|---|
| 122 | // prefix hash |
|---|
| 123 | |
|---|
| 124 | static size_t an_shorts_elems(AN_shorts *sin) { |
|---|
| 125 | size_t count = 0; |
|---|
| 126 | while (sin) { |
|---|
| 127 | sin = sin->next; |
|---|
| 128 | count++; |
|---|
| 129 | } |
|---|
| 130 | return count; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | #define PREFIXLEN 3 |
|---|
| 134 | |
|---|
| 135 | static GB_HASH *an_get_prefix_hash() { |
|---|
| 136 | if (!aisc_main->prefix_hash) { |
|---|
| 137 | AN_shorts *sin = aisc_main->shorts1; |
|---|
| 138 | size_t elems = an_shorts_elems(sin); |
|---|
| 139 | if (elems<100) elems = 100; |
|---|
| 140 | |
|---|
| 141 | GB_HASH *hash = GBS_create_hash(elems, GB_IGNORE_CASE); |
|---|
| 142 | |
|---|
| 143 | while (sin) { |
|---|
| 144 | GBS_write_hash_no_strdup(hash, GB_strndup(sin->shrt, PREFIXLEN), (long)sin); |
|---|
| 145 | sin = sin->next; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | aisc_main->prefix_hash = (long)hash; |
|---|
| 149 | } |
|---|
| 150 | return (GB_HASH*)aisc_main->prefix_hash; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | static const char *an_make_prefix(const char *str) { |
|---|
| 154 | static char buf[] = "xxx"; |
|---|
| 155 | |
|---|
| 156 | buf[0] = str[0]; |
|---|
| 157 | buf[1] = str[1]; |
|---|
| 158 | buf[2] = str[2]; |
|---|
| 159 | |
|---|
| 160 | return buf; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | static AN_shorts *an_find_shrt_prefix(const char *search) { |
|---|
| 164 | return (AN_shorts*)GBS_read_hash(an_get_prefix_hash(), an_make_prefix(search)); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | // ---------------------------------------- |
|---|
| 168 | |
|---|
| 169 | static void an_add_short(const AN_local */*locs*/, const char *new_name, |
|---|
| 170 | const char *parsed_name, const char *parsed_sym, |
|---|
| 171 | const char *shrt, const char *acc, const char *add_id) |
|---|
| 172 | { |
|---|
| 173 | AN_shorts *an_shorts; |
|---|
| 174 | AN_revers *an_revers; |
|---|
| 175 | char *full_name; |
|---|
| 176 | |
|---|
| 177 | if (strlen(parsed_sym)) { |
|---|
| 178 | full_name = (char *)calloc(sizeof(char), strlen(parsed_name) + strlen(" sym")+1); |
|---|
| 179 | sprintf(full_name, "%s sym", parsed_name); |
|---|
| 180 | } |
|---|
| 181 | else { |
|---|
| 182 | full_name = strdup(parsed_name); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | an_shorts = create_AN_shorts(); |
|---|
| 186 | an_revers = create_AN_revers(); |
|---|
| 187 | |
|---|
| 188 | an_shorts->mh.ident = an_strlwr(strdup(new_name)); |
|---|
| 189 | an_shorts->shrt = strdup(shrt); |
|---|
| 190 | an_shorts->full_name = strdup(full_name); |
|---|
| 191 | an_shorts->acc = strdup(acc); |
|---|
| 192 | an_shorts->add_id = strdup(add_id); |
|---|
| 193 | |
|---|
| 194 | aisc_link(&aisc_main->pnames, an_shorts); |
|---|
| 195 | |
|---|
| 196 | an_revers->mh.ident = an_strlwr(strdup(shrt)); |
|---|
| 197 | an_revers->full_name = full_name; |
|---|
| 198 | an_revers->acc = strdup(acc); |
|---|
| 199 | an_revers->add_id = strdup(add_id); |
|---|
| 200 | |
|---|
| 201 | aisc_link(&aisc_main->prevers, an_revers); |
|---|
| 202 | |
|---|
| 203 | GB_HASH *phash = an_get_prefix_hash(); |
|---|
| 204 | GBS_write_hash(phash, an_make_prefix(an_shorts->shrt), (long)an_shorts); // add an_shorts to hash |
|---|
| 205 | GBS_optimize_hash(phash); |
|---|
| 206 | |
|---|
| 207 | aisc_main->touched = 1; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | static void an_remove_short(AN_shorts *an_shorts) { |
|---|
| 211 | /* this should only be used to remove illegal entries from name-server. |
|---|
| 212 | normally removing names does make problems - so use it very rarely */ |
|---|
| 213 | |
|---|
| 214 | GBS_write_hash(an_get_prefix_hash(), an_make_prefix(an_shorts->shrt), 0); // delete an_shorts from hash |
|---|
| 215 | |
|---|
| 216 | AN_revers *an_revers = lookup_an_revers(aisc_main, an_shorts->shrt); |
|---|
| 217 | |
|---|
| 218 | if (an_revers) { |
|---|
| 219 | aisc_unlink((dllheader_ext*)an_revers); |
|---|
| 220 | |
|---|
| 221 | free(an_revers->mh.ident); |
|---|
| 222 | free(an_revers->full_name); |
|---|
| 223 | free(an_revers->acc); |
|---|
| 224 | free(an_revers); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | aisc_unlink((dllheader_ext*)an_shorts); |
|---|
| 228 | |
|---|
| 229 | free(an_shorts->mh.ident); |
|---|
| 230 | free(an_shorts->shrt); |
|---|
| 231 | free(an_shorts->full_name); |
|---|
| 232 | free(an_shorts->acc); |
|---|
| 233 | free(an_shorts); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | static char *nas_string_2_name(const char *str) { |
|---|
| 237 | // converts a string to a valid name |
|---|
| 238 | #if defined(DUMP_NAME_CREATION) |
|---|
| 239 | const char *org_str = str; |
|---|
| 240 | #endif // DUMP_NAME_CREATION |
|---|
| 241 | |
|---|
| 242 | char buf[FULLNAME_LEN_MAX+1]; |
|---|
| 243 | int i; |
|---|
| 244 | int c; |
|---|
| 245 | for (i=0; i<FULLNAME_LEN_MAX;) { |
|---|
| 246 | c = *(str++); |
|---|
| 247 | if (!c) break; |
|---|
| 248 | if (isalpha(c)) buf[i++] = c; |
|---|
| 249 | } |
|---|
| 250 | for (; i<NAME_LEN_MIN; i++) buf[i] = '0'; |
|---|
| 251 | buf[i] = 0; |
|---|
| 252 | #if defined(DUMP_NAME_CREATION) |
|---|
| 253 | printf("nas_string_2_name('%s') = '%s'\n", org_str, buf); |
|---|
| 254 | #endif // DUMP_NAME_CREATION |
|---|
| 255 | return strdup(buf); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | static char *nas_remove_small_vocals(const char *str) { |
|---|
| 259 | #if defined(DUMP_NAME_CREATION) |
|---|
| 260 | const char *org_str = str; |
|---|
| 261 | #endif // DUMP_NAME_CREATION |
|---|
| 262 | char buf[FULLNAME_LEN_MAX+1]; |
|---|
| 263 | int i; |
|---|
| 264 | int c; |
|---|
| 265 | |
|---|
| 266 | for (i=0; i<FULLNAME_LEN_MAX;) { |
|---|
| 267 | c = *str++; |
|---|
| 268 | if (!c) break; |
|---|
| 269 | if (strchr("aeiouy", c)==0) { |
|---|
| 270 | buf[i++] = c; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | for (; i<NAME_LEN_MIN; i++) buf[i] = '0'; |
|---|
| 274 | buf[i] = 0; |
|---|
| 275 | #if defined(DUMP_NAME_CREATION) |
|---|
| 276 | printf("nas_remove_small_vocals('%s') = '%s'\n", org_str, buf); |
|---|
| 277 | #endif // DUMP_NAME_CREATION |
|---|
| 278 | return strdup(buf); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | static void an_complete_shrt(char *shrt, const char *rest_of_full) { |
|---|
| 282 | int len = strlen(shrt); |
|---|
| 283 | |
|---|
| 284 | while (len<5) { |
|---|
| 285 | char c = *rest_of_full++; |
|---|
| 286 | |
|---|
| 287 | if (!c) break; |
|---|
| 288 | shrt[len++] = c; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | while (len<NAME_LEN_MIN) { |
|---|
| 292 | shrt[len++] = '0'; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | shrt[len] = 0; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | static void an_autocaps(char *str) { |
|---|
| 299 | // automatically capitalizes a string if it is completely up- or downcase |
|---|
| 300 | |
|---|
| 301 | bool is_single_case = true; |
|---|
| 302 | { |
|---|
| 303 | bool seen_upper_case = false; |
|---|
| 304 | bool seen_lower_case = false; |
|---|
| 305 | for (int i = 0; str[i] && is_single_case; i++) { |
|---|
| 306 | char c = str[i]; |
|---|
| 307 | if (isalpha(c)) { |
|---|
| 308 | if (islower(c)) seen_lower_case = true; |
|---|
| 309 | else seen_upper_case = true; |
|---|
| 310 | |
|---|
| 311 | if (seen_lower_case == seen_upper_case) { // both cases occurred |
|---|
| 312 | is_single_case = false; |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | if (is_single_case) { |
|---|
| 319 | bool next_is_capital = true; |
|---|
| 320 | |
|---|
| 321 | for (int i = 0; str[i]; i++) { |
|---|
| 322 | char c = str[i]; |
|---|
| 323 | if (isalnum(c)) { |
|---|
| 324 | if (next_is_capital) { |
|---|
| 325 | str[i] = toupper(c); |
|---|
| 326 | next_is_capital = false; |
|---|
| 327 | } |
|---|
| 328 | else { |
|---|
| 329 | str[i] = tolower(c); |
|---|
| 330 | } |
|---|
| 331 | } |
|---|
| 332 | else { |
|---|
| 333 | next_is_capital = true; |
|---|
| 334 | } |
|---|
| 335 | } |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | static char *an_get_short(AN_shorts *IF_ASSERTION_USED(shorts), dll_public *parent, const char *full) { |
|---|
| 340 | AN_shorts *look; |
|---|
| 341 | |
|---|
| 342 | na_assert(full); |
|---|
| 343 | na_assert(shorts == aisc_main->shorts1); // otherwise prefix_hash does not work! |
|---|
| 344 | |
|---|
| 345 | if (full[0]==0) return strdup("ZZZ"); |
|---|
| 346 | |
|---|
| 347 | const char *result = 0; |
|---|
| 348 | char *full1 = strdup(full); |
|---|
| 349 | an_autocaps(full1); |
|---|
| 350 | |
|---|
| 351 | char *full2 = nas_string_2_name(full1); |
|---|
| 352 | |
|---|
| 353 | look = (AN_shorts *)aisc_find_lib((dllpublic_ext*)parent, full2); |
|---|
| 354 | if (look) { // name is already known |
|---|
| 355 | free(full2); |
|---|
| 356 | free(full1); |
|---|
| 357 | return strdup(look->shrt); |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | char *full3 = 0; |
|---|
| 361 | char shrt[10]; |
|---|
| 362 | int len2, len3; |
|---|
| 363 | int p1, p2, p3; |
|---|
| 364 | |
|---|
| 365 | // try first three letters: |
|---|
| 366 | |
|---|
| 367 | strncpy(shrt, full2, 3); |
|---|
| 368 | UPPERCASE(shrt[0]); |
|---|
| 369 | shrt[3] = 0; |
|---|
| 370 | |
|---|
| 371 | look = an_find_shrt_prefix(shrt); |
|---|
| 372 | if (!look) { |
|---|
| 373 | len2 = strlen(full2); |
|---|
| 374 | an_complete_shrt(shrt, len2>=3 ? full2+3 : ""); |
|---|
| 375 | goto found_short; |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | // generate names from first char + consonants: |
|---|
| 379 | |
|---|
| 380 | full3 = nas_remove_small_vocals(full2); |
|---|
| 381 | len3 = strlen(full3); |
|---|
| 382 | |
|---|
| 383 | for (p1=1; p1<(len3-1); p1++) { |
|---|
| 384 | shrt[1] = full3[p1]; |
|---|
| 385 | for (p2=p1+1; p2<len3; p2++) { |
|---|
| 386 | shrt[2] = full3[p2]; |
|---|
| 387 | look = an_find_shrt_prefix(shrt); |
|---|
| 388 | if (!look) { |
|---|
| 389 | an_complete_shrt(shrt, full3+p2+1); |
|---|
| 390 | goto found_short; |
|---|
| 391 | } |
|---|
| 392 | } |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | // generate names from first char + rest characters: |
|---|
| 396 | |
|---|
| 397 | len2 = strlen(full2); |
|---|
| 398 | for (p1=1; p1<(len2-1); p1++) { |
|---|
| 399 | shrt[1] = full2[p1]; |
|---|
| 400 | for (p2=p1+1; p2<len2; p2++) { |
|---|
| 401 | shrt[2] = full2[p2]; |
|---|
| 402 | look = an_find_shrt_prefix(shrt); |
|---|
| 403 | if (!look) { |
|---|
| 404 | an_complete_shrt(shrt, full2+p2+1); |
|---|
| 405 | goto found_short; |
|---|
| 406 | } |
|---|
| 407 | } |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | // generate names containing first char + character from name + one digit: |
|---|
| 411 | |
|---|
| 412 | for (p1=1; p1<len2; p1++) { |
|---|
| 413 | shrt[1] = full2[p1]; |
|---|
| 414 | for (p2=0; p2<=9; p2++) { |
|---|
| 415 | shrt[2] = '0'+p2; |
|---|
| 416 | look = an_find_shrt_prefix(shrt); |
|---|
| 417 | if (!look) { |
|---|
| 418 | an_complete_shrt(shrt, full2+p1+1); |
|---|
| 419 | goto found_short; |
|---|
| 420 | } |
|---|
| 421 | } |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | // generate names containing first char + two digits: |
|---|
| 425 | |
|---|
| 426 | for (p1=1; p1<=99; p1++) { |
|---|
| 427 | shrt[1] = '0'+(p1/10); |
|---|
| 428 | shrt[2] = '0'+(p1%10); |
|---|
| 429 | look = an_find_shrt_prefix(shrt); |
|---|
| 430 | if (!look) { |
|---|
| 431 | an_complete_shrt(shrt, full2+1); |
|---|
| 432 | goto found_short; |
|---|
| 433 | } |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | // failed to produce sth with given name, generate something random now |
|---|
| 437 | |
|---|
| 438 | { |
|---|
| 439 | // use digits first, then use upper-case alpha (methods above use lower-case alpha) |
|---|
| 440 | const char *allowed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|---|
| 441 | const int len = 36; |
|---|
| 442 | |
|---|
| 443 | for (p1='A'; p1<='Z'; p1++) { // first character has to be alpha |
|---|
| 444 | shrt[0] = p1; |
|---|
| 445 | for (p2 = 0; p2<len; p2++) { |
|---|
| 446 | shrt[1] = allowed[p2]; |
|---|
| 447 | for (p3 = 0; p3<len; p3++) { |
|---|
| 448 | shrt[2] = allowed[p3]; |
|---|
| 449 | look = an_find_shrt_prefix(shrt); |
|---|
| 450 | if (!look) { |
|---|
| 451 | an_complete_shrt(shrt, full2); |
|---|
| 452 | goto found_short; |
|---|
| 453 | } |
|---|
| 454 | } |
|---|
| 455 | } |
|---|
| 456 | } |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | shrt[0] = 0; // erase result |
|---|
| 460 | |
|---|
| 461 | found_short : |
|---|
| 462 | result = shrt; |
|---|
| 463 | |
|---|
| 464 | if (result && result[0]) { |
|---|
| 465 | #if defined(DUMP_NAME_CREATION) |
|---|
| 466 | if (isdigit(result[0]) || isdigit(result[1])) { |
|---|
| 467 | printf("generated new short-name '%s' for full-name '%s' full2='%s' full3='%s'\n", shrt, full, full2, full3); |
|---|
| 468 | } |
|---|
| 469 | #endif // DUMP_NAME_CREATION |
|---|
| 470 | |
|---|
| 471 | look = create_AN_shorts(); |
|---|
| 472 | look->mh.ident = strdup(full2); |
|---|
| 473 | look->shrt = strdup(result); |
|---|
| 474 | aisc_link((dllpublic_ext*)parent, (dllheader_ext*)look); |
|---|
| 475 | |
|---|
| 476 | aisc_main->touched = 1; |
|---|
| 477 | } |
|---|
| 478 | else { |
|---|
| 479 | printf("ARB_name_server: Failed to make a short-name for '%s'\n", full); |
|---|
| 480 | result = "ZZZ"; |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | free(full3); |
|---|
| 484 | free(full2); |
|---|
| 485 | free(full1); |
|---|
| 486 | |
|---|
| 487 | return strdup(result); |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | // -------------------------------------------------------------------------------- |
|---|
| 491 | |
|---|
| 492 | static const char *default_full_name = "No name"; |
|---|
| 493 | |
|---|
| 494 | class NameInformation : virtual Noncopyable { |
|---|
| 495 | const char *full_name; |
|---|
| 496 | |
|---|
| 497 | char *parsed_name; |
|---|
| 498 | char *parsed_sym; |
|---|
| 499 | char *parsed_acc; |
|---|
| 500 | char *parsed_add_id; |
|---|
| 501 | |
|---|
| 502 | char *first_name; |
|---|
| 503 | char *rest_of_name; |
|---|
| 504 | |
|---|
| 505 | char *id; |
|---|
| 506 | |
|---|
| 507 | public: |
|---|
| 508 | NameInformation(const AN_local *locs); |
|---|
| 509 | ~NameInformation(); |
|---|
| 510 | |
|---|
| 511 | const char *get_id() const { return id; } |
|---|
| 512 | const char *get_full_name() const { return full_name; } |
|---|
| 513 | const char *get_first_name() const { return first_name; } |
|---|
| 514 | const char *get_rest_of_name() const { return rest_of_name; } |
|---|
| 515 | |
|---|
| 516 | void add_short(const AN_local *locs, const char *shrt) const { |
|---|
| 517 | an_add_short(locs, id, parsed_name, parsed_sym, shrt, parsed_acc, parsed_add_id); |
|---|
| 518 | } |
|---|
| 519 | }; |
|---|
| 520 | |
|---|
| 521 | static bool stralnum(const char *str) { |
|---|
| 522 | bool nonalnum = false; |
|---|
| 523 | for (char c = *str++; c; c = *str++) { |
|---|
| 524 | if (!isalnum(c)) { |
|---|
| 525 | nonalnum = true; |
|---|
| 526 | break; |
|---|
| 527 | } |
|---|
| 528 | } |
|---|
| 529 | return !nonalnum; |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | static char *make_alnum(const char *str) { |
|---|
| 533 | // returns a heap-copy containing all alphanumeric characters of 'str' |
|---|
| 534 | |
|---|
| 535 | char *newStr = (char*)malloc(strlen(str)+1); |
|---|
| 536 | int n = 0; |
|---|
| 537 | |
|---|
| 538 | for (int p = 0; str[p]; ++p) { |
|---|
| 539 | if (isalnum(str[p])) newStr[n++] = str[p]; |
|---|
| 540 | } |
|---|
| 541 | newStr[n] = 0; |
|---|
| 542 | return newStr; |
|---|
| 543 | } |
|---|
| 544 | static char *make_alpha(const char *str) { |
|---|
| 545 | // returns a heap-copy containing all alpha characters of 'str' |
|---|
| 546 | |
|---|
| 547 | char *newStr = (char*)malloc(strlen(str)+1); |
|---|
| 548 | int n = 0; |
|---|
| 549 | |
|---|
| 550 | for (int p = 0; str[p]; ++p) { |
|---|
| 551 | if (isalpha(str[p])) newStr[n++] = str[p]; |
|---|
| 552 | } |
|---|
| 553 | newStr[n] = 0; |
|---|
| 554 | return newStr; |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | #if defined(DEBUG) |
|---|
| 558 | #define assert_alnum(s) na_assert(stralnum(s)) |
|---|
| 559 | #else |
|---|
| 560 | #define assert_alnum(s) |
|---|
| 561 | #endif // DEBUG |
|---|
| 562 | |
|---|
| 563 | NameInformation::NameInformation(const AN_local *locs) { |
|---|
| 564 | full_name = locs->full_name; |
|---|
| 565 | if (!full_name || !full_name[0]) full_name = default_full_name; |
|---|
| 566 | |
|---|
| 567 | parsed_name = GBS_string_eval(full_name, |
|---|
| 568 | "\t= :\"= :'= :" // replace TABs and quotes by space |
|---|
| 569 | "sp.=species:spec.=species:SP.=SPECIES:SPEC.=SPECIES:" // replace common abbreviations of 'species' |
|---|
| 570 | ".= :" // replace dots by spaces |
|---|
| 571 | " = :" // multiple spaces -> 1 space |
|---|
| 572 | , 0); |
|---|
| 573 | |
|---|
| 574 | { |
|---|
| 575 | int leading_spaces = strspn(parsed_name, " "); |
|---|
| 576 | int len = strlen(parsed_name)-leading_spaces; |
|---|
| 577 | memmove(parsed_name, parsed_name+leading_spaces, len); |
|---|
| 578 | |
|---|
| 579 | char *first_space = strchr(parsed_name, ' '); |
|---|
| 580 | if (first_space) { |
|---|
| 581 | char *second_space = strchr(first_space+1, ' '); |
|---|
| 582 | if (second_space) { |
|---|
| 583 | second_space[0] = 0; // skip all beyond 2nd word |
|---|
| 584 | } |
|---|
| 585 | } |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | an_autocaps(parsed_name); |
|---|
| 589 | |
|---|
| 590 | parsed_sym = GBS_string_eval(full_name, "\t= :* * *sym*=S", 0); |
|---|
| 591 | if (strlen(parsed_sym)>1) freedup(parsed_sym, ""); |
|---|
| 592 | |
|---|
| 593 | const char *add_id = locs->add_id[0] ? locs->add_id : aisc_main->add_field_default; |
|---|
| 594 | |
|---|
| 595 | parsed_acc = make_alnum(locs->acc); |
|---|
| 596 | parsed_add_id = make_alnum(add_id); |
|---|
| 597 | first_name = GBS_string_eval(parsed_name, "* *=*1", 0); |
|---|
| 598 | rest_of_name = make_alnum(parsed_name+strlen(first_name)); |
|---|
| 599 | |
|---|
| 600 | freeset(first_name, make_alnum(first_name)); |
|---|
| 601 | |
|---|
| 602 | assert_alnum(parsed_acc); |
|---|
| 603 | assert_alnum(first_name); |
|---|
| 604 | assert_alnum(rest_of_name); |
|---|
| 605 | |
|---|
| 606 | UPPERCASE(rest_of_name[0]); |
|---|
| 607 | |
|---|
| 608 | // build id |
|---|
| 609 | |
|---|
| 610 | id = (strlen(parsed_acc)+strlen(parsed_add_id)) |
|---|
| 611 | ? GBS_global_string_copy("*%s*%s", parsed_acc, parsed_add_id) |
|---|
| 612 | : GBS_global_string_copy("%s*%s*%s", first_name, rest_of_name, parsed_sym); |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | NameInformation::~NameInformation() { |
|---|
| 616 | free(id); |
|---|
| 617 | |
|---|
| 618 | free(rest_of_name); |
|---|
| 619 | free(first_name); |
|---|
| 620 | |
|---|
| 621 | free(parsed_add_id); |
|---|
| 622 | free(parsed_acc); |
|---|
| 623 | free(parsed_sym); |
|---|
| 624 | free(parsed_name); |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | // -------------------------------------------------------------------------------- |
|---|
| 628 | // AISC functions |
|---|
| 629 | |
|---|
| 630 | int del_short(const AN_local *locs) { |
|---|
| 631 | // forget about a short name |
|---|
| 632 | NameInformation info(locs); |
|---|
| 633 | int removed = 0; |
|---|
| 634 | AN_shorts *an_shorts = lookup_an_shorts(aisc_main, info.get_id()); |
|---|
| 635 | |
|---|
| 636 | if (an_shorts) { |
|---|
| 637 | an_remove_short(an_shorts); |
|---|
| 638 | removed = 1; |
|---|
| 639 | } |
|---|
| 640 | |
|---|
| 641 | return removed; |
|---|
| 642 | } |
|---|
| 643 | |
|---|
| 644 | static GB_HASH *nameModHash = 0; // key = default name; value = max. counter tested |
|---|
| 645 | |
|---|
| 646 | aisc_string get_short(const AN_local *locs) { |
|---|
| 647 | // get the short name from the previously set names |
|---|
| 648 | static char *shrt = 0; |
|---|
| 649 | |
|---|
| 650 | freenull(shrt); |
|---|
| 651 | |
|---|
| 652 | NameInformation info(locs); |
|---|
| 653 | AN_shorts *an_shorts = lookup_an_shorts(aisc_main, info.get_id()); |
|---|
| 654 | |
|---|
| 655 | if (an_shorts) { // we already have a short name |
|---|
| 656 | bool recreate = false; |
|---|
| 657 | |
|---|
| 658 | if (!stralnum(an_shorts->shrt)) { // contains non-alphanumeric characters |
|---|
| 659 | recreate = true; |
|---|
| 660 | } |
|---|
| 661 | else if (strcmp(an_shorts->full_name, default_full_name) == 0 && // fullname in name server is default_full_name |
|---|
| 662 | strcmp(info.get_full_name(), an_shorts->full_name) != 0) // and differs from current |
|---|
| 663 | { |
|---|
| 664 | recreate = true; |
|---|
| 665 | } |
|---|
| 666 | if (recreate) { |
|---|
| 667 | an_remove_short(an_shorts); |
|---|
| 668 | an_shorts = 0; |
|---|
| 669 | } |
|---|
| 670 | else { |
|---|
| 671 | shrt = strdup(an_shorts->shrt); |
|---|
| 672 | } |
|---|
| 673 | } |
|---|
| 674 | if (!shrt) { // now there is no short name (or an illegal one) |
|---|
| 675 | char *first_advice=0, *second_advice=0; |
|---|
| 676 | |
|---|
| 677 | if (locs->advice[0] && !stralnum(locs->advice)) { // bad advice |
|---|
| 678 | locs->advice[0] = 0; // delete it |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | if (locs->advice[0]) { |
|---|
| 682 | char *advice = make_alpha(locs->advice); |
|---|
| 683 | |
|---|
| 684 | first_advice = strdup(advice); |
|---|
| 685 | if (strlen(advice) > 3) { |
|---|
| 686 | second_advice = strdup(advice+3); |
|---|
| 687 | first_advice[3] = 0; |
|---|
| 688 | } |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | if (!first_advice) first_advice = strdup("ZZZ"); |
|---|
| 692 | if (!second_advice) second_advice = strdup("ZZZZZ"); |
|---|
| 693 | |
|---|
| 694 | char *first_short; |
|---|
| 695 | int first_len; |
|---|
| 696 | { |
|---|
| 697 | const char *first_name = info.get_first_name(); |
|---|
| 698 | first_short = first_name[0] |
|---|
| 699 | ? an_get_short(aisc_main->shorts1, &(aisc_main->pshorts1), first_name) |
|---|
| 700 | : strdup(first_advice); |
|---|
| 701 | |
|---|
| 702 | na_assert(first_short); |
|---|
| 703 | if (first_short[0] == 0) { // empty? |
|---|
| 704 | freedup(first_short, "ZZZ"); |
|---|
| 705 | } |
|---|
| 706 | first_len = strlen(first_short); |
|---|
| 707 | } |
|---|
| 708 | |
|---|
| 709 | char *second_short = (char*)calloc(10, 1); |
|---|
| 710 | int second_len; |
|---|
| 711 | { |
|---|
| 712 | const char *rest_of_name = info.get_rest_of_name(); |
|---|
| 713 | int restlen = strlen(rest_of_name); |
|---|
| 714 | |
|---|
| 715 | if (!restlen) { |
|---|
| 716 | rest_of_name = second_advice; |
|---|
| 717 | restlen = strlen(rest_of_name); |
|---|
| 718 | if (!restlen) { |
|---|
| 719 | rest_of_name = "ZZZZZ"; |
|---|
| 720 | restlen = 5; |
|---|
| 721 | } |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | second_short[0] = 0; |
|---|
| 725 | |
|---|
| 726 | if (restlen<5 && first_len>3) { |
|---|
| 727 | strcpy(second_short, first_short+3); // take additional characters from first_short |
|---|
| 728 | second_short[5-restlen] = 0; // but not too many |
|---|
| 729 | } |
|---|
| 730 | |
|---|
| 731 | char *strend = strchr(second_short, 0); |
|---|
| 732 | strncpy(strend, rest_of_name, 8); |
|---|
| 733 | second_len = strlen(second_short); |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | if (first_len>3) { |
|---|
| 737 | first_short[3] = 0; |
|---|
| 738 | first_len = 3; |
|---|
| 739 | } |
|---|
| 740 | |
|---|
| 741 | int both_len = first_len+second_len; |
|---|
| 742 | if (both_len<8) { |
|---|
| 743 | freeset(second_short, GBS_global_string_copy("%s00000000", second_short)); |
|---|
| 744 | second_len += 8; |
|---|
| 745 | both_len += 8; |
|---|
| 746 | } |
|---|
| 747 | |
|---|
| 748 | if (both_len>8) { |
|---|
| 749 | second_len = 8-first_len; |
|---|
| 750 | second_short[second_len] = 0; |
|---|
| 751 | both_len = 8; |
|---|
| 752 | } |
|---|
| 753 | |
|---|
| 754 | char test_short[9]; |
|---|
| 755 | sprintf(test_short, "%s%s", first_short, second_short); |
|---|
| 756 | |
|---|
| 757 | na_assert(size_t(both_len) == strlen(test_short)); |
|---|
| 758 | na_assert(second_len>=5 && second_len <= 8); |
|---|
| 759 | |
|---|
| 760 | if (lookup_an_revers(aisc_main, test_short)) { |
|---|
| 761 | if (!nameModHash) nameModHash = GBS_create_hash(100, GB_IGNORE_CASE); |
|---|
| 762 | |
|---|
| 763 | char *test_short_dup = strdup(test_short); |
|---|
| 764 | long count = GBS_read_hash(nameModHash, test_short); |
|---|
| 765 | if (count<2) count = 2; // first name modification uses number 2 |
|---|
| 766 | |
|---|
| 767 | int printOffset = both_len; |
|---|
| 768 | bool foundUnused = false; |
|---|
| 769 | |
|---|
| 770 | // first create alternate name with digits only |
|---|
| 771 | { |
|---|
| 772 | int digLimit[6] = { 0, 9, 99, 999, 9999, 99999 }; |
|---|
| 773 | for (int digits = 1; !foundUnused && digits <= 5; ++digits) { |
|---|
| 774 | int maxOffset = 8-digits; |
|---|
| 775 | int limit = digLimit[digits]; |
|---|
| 776 | |
|---|
| 777 | if (printOffset>maxOffset) printOffset = maxOffset; |
|---|
| 778 | |
|---|
| 779 | char *printAt = test_short+printOffset; |
|---|
| 780 | |
|---|
| 781 | for (; !foundUnused && count <= limit; ++count) { |
|---|
| 782 | IF_ASSERTION_USED(int printed =) sprintf(printAt, "%li", count); |
|---|
| 783 | na_assert((printed+printOffset) <= 8); |
|---|
| 784 | if (!lookup_an_revers(aisc_main, test_short)) foundUnused = true; // name does not exist |
|---|
| 785 | } |
|---|
| 786 | } |
|---|
| 787 | } |
|---|
| 788 | |
|---|
| 789 | // if no unused name found, create one with alpha-chars |
|---|
| 790 | if (!foundUnused) { |
|---|
| 791 | strcpy(test_short, test_short_dup); |
|---|
| 792 | |
|---|
| 793 | long count2 = count-100000; // 100000 numbers were used above |
|---|
| 794 | char *printAt = test_short+3; |
|---|
| 795 | const char *base36 = "0123456789abcdefghijklmnopqrstuvwxyz"; |
|---|
| 796 | |
|---|
| 797 | printAt[5] = 0; |
|---|
| 798 | |
|---|
| 799 | for (; !foundUnused && count2<16796160; ++count2) { // 16796160 = 36^4*10 |
|---|
| 800 | // now print count2 converted to base 36 |
|---|
| 801 | |
|---|
| 802 | int c = count2; |
|---|
| 803 | for (int pos = 0; pos<5; ++pos) { |
|---|
| 804 | int nextc = c/36; |
|---|
| 805 | int rest = c-36*nextc; |
|---|
| 806 | |
|---|
| 807 | printAt[4-pos] = base36[rest]; |
|---|
| 808 | c = nextc; |
|---|
| 809 | |
|---|
| 810 | na_assert(pos != 4 || c == 0); |
|---|
| 811 | } |
|---|
| 812 | |
|---|
| 813 | if (!lookup_an_revers(aisc_main, test_short)) foundUnused = true; // name does not exist |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | count = count2+100000; |
|---|
| 817 | } |
|---|
| 818 | |
|---|
| 819 | na_assert(foundUnused); |
|---|
| 820 | GBS_write_hash(nameModHash, test_short_dup, count); |
|---|
| 821 | GBS_optimize_hash(nameModHash); |
|---|
| 822 | |
|---|
| 823 | free(test_short_dup); |
|---|
| 824 | } |
|---|
| 825 | |
|---|
| 826 | assert_alnum(test_short); |
|---|
| 827 | |
|---|
| 828 | shrt = strdup(test_short); |
|---|
| 829 | info.add_short(locs, shrt); |
|---|
| 830 | |
|---|
| 831 | free(first_short); |
|---|
| 832 | free(second_short); |
|---|
| 833 | free(first_advice); |
|---|
| 834 | free(second_advice); |
|---|
| 835 | } |
|---|
| 836 | |
|---|
| 837 | assert_alnum(shrt); |
|---|
| 838 | return shrt; |
|---|
| 839 | } |
|---|
| 840 | |
|---|
| 841 | int server_save(AN_main *main, int) { |
|---|
| 842 | if (main->touched) { |
|---|
| 843 | int server_date = GB_time_of_file(main->server_file); |
|---|
| 844 | if (server_date>main->server_filedate) { |
|---|
| 845 | printf("Another nameserver changed '%s' - your changes are lost.\n", main->server_file); |
|---|
| 846 | } |
|---|
| 847 | else { |
|---|
| 848 | char *sec_name = (char *)calloc(sizeof(char), strlen(main->server_file)+2); |
|---|
| 849 | sprintf(sec_name, "%s%%", main->server_file); |
|---|
| 850 | printf("Saving '%s'..\n", main->server_file); |
|---|
| 851 | |
|---|
| 852 | FILE *file = fopen(sec_name, "w"); |
|---|
| 853 | if (!file) { |
|---|
| 854 | fprintf(stderr, "ERROR cannot save file '%s'\n", sec_name); |
|---|
| 855 | } |
|---|
| 856 | else { |
|---|
| 857 | save_AN_main(main, file); |
|---|
| 858 | if (fclose(file) == 0) { |
|---|
| 859 | GB_ERROR mv_error = GB_rename_file(sec_name, main->server_file); |
|---|
| 860 | if (mv_error) GB_warning(mv_error); |
|---|
| 861 | else main->touched = 0; |
|---|
| 862 | } |
|---|
| 863 | else { |
|---|
| 864 | GB_ERROR save_error = GB_IO_error("saving", sec_name); |
|---|
| 865 | fprintf(stderr, "Error: %s\n", save_error); |
|---|
| 866 | unlink(sec_name); |
|---|
| 867 | } |
|---|
| 868 | } |
|---|
| 869 | free(sec_name); |
|---|
| 870 | main->server_filedate = GB_time_of_file(main->server_file); |
|---|
| 871 | } |
|---|
| 872 | } |
|---|
| 873 | else { |
|---|
| 874 | printf("No changes to ARB_name_server data.\n"); |
|---|
| 875 | } |
|---|
| 876 | |
|---|
| 877 | return 0; |
|---|
| 878 | } |
|---|
| 879 | |
|---|
| 880 | #if defined(DEBUG) && 0 |
|---|
| 881 | static void check_list(AN_shorts *start) { |
|---|
| 882 | int count = 0; |
|---|
| 883 | while (++count) { |
|---|
| 884 | start = start->next; |
|---|
| 885 | if (!start) { |
|---|
| 886 | fprintf(stderr, "<list has %i elements>\n", count); |
|---|
| 887 | return; |
|---|
| 888 | } |
|---|
| 889 | } |
|---|
| 890 | |
|---|
| 891 | fprintf(stderr, "<ERROR - list is looped>\n"); |
|---|
| 892 | na_assert(0); |
|---|
| 893 | } |
|---|
| 894 | #endif // DEBUG |
|---|
| 895 | |
|---|
| 896 | static void check_for_case_error(AN_main *main) { |
|---|
| 897 | // test for duplicated names or name parts (only differing in case) |
|---|
| 898 | // such names were created by old name server versions |
|---|
| 899 | |
|---|
| 900 | bool case_error_occurred = false; |
|---|
| 901 | int idents_changed = 0; |
|---|
| 902 | // first check name parts |
|---|
| 903 | for (AN_shorts *shrt = main->shorts1; shrt;) { |
|---|
| 904 | AN_shorts *next = shrt->next; |
|---|
| 905 | AN_shorts *found = an_find_shrt_prefix(shrt->shrt); |
|---|
| 906 | if (found != shrt) { |
|---|
| 907 | fprintf(stderr, "- Correcting error in name-database: '%s' equals '%s'\n", |
|---|
| 908 | found->shrt, shrt->shrt); |
|---|
| 909 | an_remove_short(shrt); |
|---|
| 910 | case_error_occurred = true; |
|---|
| 911 | } |
|---|
| 912 | shrt = next; |
|---|
| 913 | } |
|---|
| 914 | |
|---|
| 915 | // then check full short-names |
|---|
| 916 | for (AN_shorts *shrt = main->names; shrt;) { |
|---|
| 917 | AN_shorts *next = shrt->next; |
|---|
| 918 | AN_revers *found = lookup_an_revers(main, shrt->shrt); |
|---|
| 919 | |
|---|
| 920 | if (found && (shrt->acc && found->acc && an_stricmp(shrt->acc, found->acc) != 0)) { |
|---|
| 921 | fprintf(stderr, "- Correcting error in name-database: '%s' equals '%s' (but acc differs)\n", |
|---|
| 922 | found->mh.ident, shrt->shrt); |
|---|
| 923 | |
|---|
| 924 | an_remove_short(shrt); |
|---|
| 925 | case_error_occurred = true; |
|---|
| 926 | } |
|---|
| 927 | else if (found && (shrt->add_id && found->add_id && an_stricmp(shrt->add_id, found->add_id) != 0)) { |
|---|
| 928 | fprintf(stderr, "- Correcting error in name-database: '%s' equals '%s' (but add_id differs)\n", |
|---|
| 929 | found->mh.ident, shrt->shrt); |
|---|
| 930 | |
|---|
| 931 | an_remove_short(shrt); |
|---|
| 932 | case_error_occurred = true; |
|---|
| 933 | } |
|---|
| 934 | else { |
|---|
| 935 | AN_shorts *self_find = lookup_an_shorts(main, shrt->mh.ident); |
|---|
| 936 | if (!self_find) { // stored with wrong key (not lowercase) |
|---|
| 937 | aisc_unlink((dllheader_ext*)shrt); |
|---|
| 938 | an_strlwr(shrt->mh.ident); |
|---|
| 939 | aisc_link(&main->pnames, shrt); |
|---|
| 940 | main->touched = 1; |
|---|
| 941 | |
|---|
| 942 | case_error_occurred = true; |
|---|
| 943 | idents_changed++; |
|---|
| 944 | } |
|---|
| 945 | else if (self_find != shrt) { |
|---|
| 946 | fprintf(stderr, "- Correcting error in name-database: '%s' equals '%s' (case-difference in full_name or acc)\n", |
|---|
| 947 | shrt->mh.ident, self_find->mh.ident); |
|---|
| 948 | an_remove_short(shrt); |
|---|
| 949 | case_error_occurred = true; |
|---|
| 950 | } |
|---|
| 951 | } |
|---|
| 952 | |
|---|
| 953 | shrt = next; |
|---|
| 954 | } |
|---|
| 955 | |
|---|
| 956 | if (case_error_occurred) { |
|---|
| 957 | int regen_name_parts = 0; |
|---|
| 958 | int deleted_names = 0; |
|---|
| 959 | |
|---|
| 960 | // now capitalize all name parts |
|---|
| 961 | { |
|---|
| 962 | list<string> idents_to_recreate; |
|---|
| 963 | |
|---|
| 964 | for (AN_shorts *shrt = main->shorts1; shrt;) { |
|---|
| 965 | char *cap_name = strdup(shrt->shrt); |
|---|
| 966 | an_autocaps(cap_name); |
|---|
| 967 | |
|---|
| 968 | if (strcmp(cap_name, shrt->shrt) != 0) { |
|---|
| 969 | idents_to_recreate.push_back(shrt->mh.ident); |
|---|
| 970 | } |
|---|
| 971 | free(cap_name); |
|---|
| 972 | |
|---|
| 973 | AN_shorts *next = shrt->next; |
|---|
| 974 | an_remove_short(shrt); |
|---|
| 975 | shrt = next; |
|---|
| 976 | } |
|---|
| 977 | |
|---|
| 978 | list<string>::const_iterator end = idents_to_recreate.end(); |
|---|
| 979 | for (list<string>::const_iterator i = idents_to_recreate.begin(); i != end; ++i) { |
|---|
| 980 | const char *ident = i->c_str(); |
|---|
| 981 | free(an_get_short(main->shorts1, &(main->pshorts1), ident)); |
|---|
| 982 | regen_name_parts++; |
|---|
| 983 | } |
|---|
| 984 | } |
|---|
| 985 | |
|---|
| 986 | // now capitalize all short names |
|---|
| 987 | for (AN_shorts *shrt = main->names; shrt;) { |
|---|
| 988 | AN_shorts *next = shrt->next; |
|---|
| 989 | char *cap_name = strdup(shrt->shrt); |
|---|
| 990 | an_autocaps(cap_name); |
|---|
| 991 | |
|---|
| 992 | if (strcmp(cap_name, shrt->shrt) != 0) { |
|---|
| 993 | an_remove_short(shrt); |
|---|
| 994 | deleted_names++; |
|---|
| 995 | } |
|---|
| 996 | shrt = next; |
|---|
| 997 | free(cap_name); |
|---|
| 998 | } |
|---|
| 999 | |
|---|
| 1000 | if (idents_changed) fprintf(stderr, "* Changed case of %i identifiers.\n", idents_changed); |
|---|
| 1001 | if (regen_name_parts) fprintf(stderr, "* Regenerated %i prefix names.\n", regen_name_parts); |
|---|
| 1002 | if (deleted_names) fprintf(stderr, "* Removed %i names with wrong case.\n" |
|---|
| 1003 | "=> This leads to name changes when generating new names (which is recommended now).\n", deleted_names); |
|---|
| 1004 | } |
|---|
| 1005 | } |
|---|
| 1006 | |
|---|
| 1007 | static void check_for_illegal_chars(AN_main *main) { |
|---|
| 1008 | // test for names containing illegal characters |
|---|
| 1009 | int illegal_names = 0; |
|---|
| 1010 | |
|---|
| 1011 | // first check name parts |
|---|
| 1012 | for (AN_shorts *shrt = main->shorts1; shrt;) { |
|---|
| 1013 | AN_shorts *next = shrt->next; |
|---|
| 1014 | if (!stralnum(shrt->shrt)) { |
|---|
| 1015 | fprintf(stderr, "- Fixing illegal chars in '%s'\n", shrt->shrt); |
|---|
| 1016 | an_remove_short(shrt); |
|---|
| 1017 | illegal_names++; |
|---|
| 1018 | } |
|---|
| 1019 | shrt = next; |
|---|
| 1020 | } |
|---|
| 1021 | // then check full short-names |
|---|
| 1022 | for (AN_shorts *shrt = main->names; shrt;) { |
|---|
| 1023 | AN_shorts *next = shrt->next; |
|---|
| 1024 | if (!stralnum(shrt->shrt)) { |
|---|
| 1025 | fprintf(stderr, "- Fixing illegal chars in '%s'\n", shrt->shrt); |
|---|
| 1026 | an_remove_short(shrt); |
|---|
| 1027 | illegal_names++; |
|---|
| 1028 | } |
|---|
| 1029 | shrt = next; |
|---|
| 1030 | } |
|---|
| 1031 | |
|---|
| 1032 | if (illegal_names>0) { |
|---|
| 1033 | fprintf(stderr, "* Removed %i names containing illegal characters.\n" |
|---|
| 1034 | "=> This leads to name changes when generating new names (which is recommended now).\n", illegal_names); |
|---|
| 1035 | } |
|---|
| 1036 | } |
|---|
| 1037 | |
|---|
| 1038 | static void set_empty_addids(AN_main *main) { |
|---|
| 1039 | // fill all empty add.ids with default value |
|---|
| 1040 | |
|---|
| 1041 | if (main->add_field_default[0]) { |
|---|
| 1042 | // if we use a non-empty default, old entries need to be changed |
|---|
| 1043 | // (empty default was old behavior) |
|---|
| 1044 | |
|---|
| 1045 | long count = 0; |
|---|
| 1046 | for (AN_shorts *shrt = main->names; shrt;) { |
|---|
| 1047 | AN_shorts *next = shrt->next; |
|---|
| 1048 | if (!shrt->add_id[0]) { |
|---|
| 1049 | aisc_unlink((dllheader_ext*)shrt); |
|---|
| 1050 | |
|---|
| 1051 | freedup(shrt->add_id, main->add_field_default); |
|---|
| 1052 | na_assert(strchr(shrt->mh.ident, 0)[-1] == '*'); |
|---|
| 1053 | freeset(shrt->mh.ident, GBS_global_string_copy("%s%s", shrt->mh.ident, main->add_field_default)); |
|---|
| 1054 | |
|---|
| 1055 | aisc_link(&main->pnames, shrt); |
|---|
| 1056 | |
|---|
| 1057 | count++; |
|---|
| 1058 | } |
|---|
| 1059 | shrt = next; |
|---|
| 1060 | } |
|---|
| 1061 | if (count>0) { |
|---|
| 1062 | printf(" Filled in default value '%s' for %li names\n", main->add_field_default, count); |
|---|
| 1063 | main->touched = 1; |
|---|
| 1064 | } |
|---|
| 1065 | } |
|---|
| 1066 | } |
|---|
| 1067 | |
|---|
| 1068 | static GB_ERROR server_load(AN_main *main) |
|---|
| 1069 | { |
|---|
| 1070 | FILE *file; |
|---|
| 1071 | GB_ERROR error = 0; |
|---|
| 1072 | AN_shorts *shrt; |
|---|
| 1073 | AN_revers *revers; |
|---|
| 1074 | |
|---|
| 1075 | fprintf(stderr, "Starting ARB_name_server..\n"); |
|---|
| 1076 | |
|---|
| 1077 | file = fopen(main->server_file, "r"); |
|---|
| 1078 | if (!file) { |
|---|
| 1079 | error = GBS_global_string("No such file '%s'", main->server_file); |
|---|
| 1080 | } |
|---|
| 1081 | else { |
|---|
| 1082 | fprintf(stderr, "* Loading %s\n", main->server_file); |
|---|
| 1083 | int err_code = load_AN_main(main, file); |
|---|
| 1084 | if (err_code) { |
|---|
| 1085 | error = GBS_global_string("Error #%i while loading '%s'", err_code, main->server_file); |
|---|
| 1086 | } |
|---|
| 1087 | } |
|---|
| 1088 | |
|---|
| 1089 | if (!error) { |
|---|
| 1090 | fprintf(stderr, "* Parsing data\n"); |
|---|
| 1091 | long nameCount = 0; |
|---|
| 1092 | for (shrt = main->names; shrt; shrt = shrt->next) { |
|---|
| 1093 | revers = create_AN_revers(); |
|---|
| 1094 | revers->mh.ident = an_strlwr(strdup(shrt->shrt)); |
|---|
| 1095 | revers->full_name = strdup(shrt->full_name); |
|---|
| 1096 | revers->acc = strdup(shrt->acc); |
|---|
| 1097 | revers->add_id = shrt->add_id ? strdup(shrt->add_id) : 0; |
|---|
| 1098 | aisc_link(&main->prevers, revers); |
|---|
| 1099 | nameCount++; |
|---|
| 1100 | } |
|---|
| 1101 | |
|---|
| 1102 | int namesDBversion = main->dbversion; // version used to save names.dat |
|---|
| 1103 | fprintf(stderr, "* Loaded NAMEDB v%i (contains %li names)\n", namesDBversion, nameCount); |
|---|
| 1104 | |
|---|
| 1105 | if (namesDBversion < SERVER_VERSION) { |
|---|
| 1106 | if (namesDBversion<4) { |
|---|
| 1107 | fprintf(stderr, "* Checking for case-errors\n"); |
|---|
| 1108 | check_for_case_error(main); |
|---|
| 1109 | |
|---|
| 1110 | fprintf(stderr, "* Checking for illegal characters\n"); |
|---|
| 1111 | check_for_illegal_chars(main); |
|---|
| 1112 | } |
|---|
| 1113 | |
|---|
| 1114 | fprintf(stderr, "* NAMEDB version upgrade %i -> %i\n", namesDBversion, SERVER_VERSION); |
|---|
| 1115 | main->dbversion = SERVER_VERSION; |
|---|
| 1116 | main->touched = 1; // force save |
|---|
| 1117 | } |
|---|
| 1118 | if (namesDBversion > SERVER_VERSION) { |
|---|
| 1119 | error = GBS_global_string("NAMEDB is from version %i, but your nameserver can only handle version %i", |
|---|
| 1120 | namesDBversion, SERVER_VERSION); |
|---|
| 1121 | } |
|---|
| 1122 | else { |
|---|
| 1123 | fprintf(stderr, "ARB_name_server is up.\n"); |
|---|
| 1124 | main->server_filedate = GB_time_of_file(main->server_file); |
|---|
| 1125 | } |
|---|
| 1126 | } |
|---|
| 1127 | else { |
|---|
| 1128 | main->server_filedate = -1; |
|---|
| 1129 | } |
|---|
| 1130 | return error; |
|---|
| 1131 | } |
|---|
| 1132 | |
|---|
| 1133 | __ATTR__NORETURN static void names_server_shutdown(int exitcode) { |
|---|
| 1134 | aisc_server_shutdown_and_exit(AN_global.server_communication, exitcode); // never returns |
|---|
| 1135 | } |
|---|
| 1136 | |
|---|
| 1137 | int names_server_save() { |
|---|
| 1138 | server_save(aisc_main, 0); |
|---|
| 1139 | return 0; |
|---|
| 1140 | } |
|---|
| 1141 | |
|---|
| 1142 | int server_shutdown(AN_main */*pm*/, aisc_string passwd) { |
|---|
| 1143 | // password check |
|---|
| 1144 | if (strcmp(passwd, "ldfiojkherjkh")) return 1; |
|---|
| 1145 | printf("\narb_name_server: I got the shutdown message.\n"); |
|---|
| 1146 | |
|---|
| 1147 | // shutdown clients |
|---|
| 1148 | aisc_broadcast(AN_global.server_communication, 0, |
|---|
| 1149 | "SERVER SHUTDOWN BY ADMINISTRATOR!\n"); |
|---|
| 1150 | |
|---|
| 1151 | // shutdown server |
|---|
| 1152 | printf("ARB_name_server: server shutdown by administrator\n"); |
|---|
| 1153 | names_server_shutdown(0); // never returns! |
|---|
| 1154 | return 0; |
|---|
| 1155 | } |
|---|
| 1156 | |
|---|
| 1157 | static void usage(const char *exeName, const char *err) __ATTR__NORETURN; |
|---|
| 1158 | static void usage(const char *exeName, const char *err) { |
|---|
| 1159 | printf("ARB nameserver v%i\n" |
|---|
| 1160 | "Usage: %s command server-parameters\n" |
|---|
| 1161 | "command = -boot\n" |
|---|
| 1162 | " -kill\n" |
|---|
| 1163 | " -look\n" |
|---|
| 1164 | , SERVER_VERSION, exeName); |
|---|
| 1165 | arb_print_server_params(); |
|---|
| 1166 | if (err) printf("Error: %s\n", err); |
|---|
| 1167 | exit(EXIT_FAILURE); |
|---|
| 1168 | } |
|---|
| 1169 | |
|---|
| 1170 | int ARB_main(int argc, char *argv[]) { |
|---|
| 1171 | char *name; |
|---|
| 1172 | int i; |
|---|
| 1173 | Hs_struct *so; |
|---|
| 1174 | arb_params *params; |
|---|
| 1175 | |
|---|
| 1176 | params = arb_trace_argv(&argc, (const char **)argv); |
|---|
| 1177 | const char *executable = argv[0]; |
|---|
| 1178 | |
|---|
| 1179 | if (!params->default_file) usage(executable, "Missing default file"); |
|---|
| 1180 | |
|---|
| 1181 | if (argc==1) { // default command is '-look' |
|---|
| 1182 | char flag[]="-look"; |
|---|
| 1183 | argv[1] = flag; |
|---|
| 1184 | argc = 2; |
|---|
| 1185 | } |
|---|
| 1186 | |
|---|
| 1187 | if (argc!=2) usage(executable, "Too many parameters"); |
|---|
| 1188 | |
|---|
| 1189 | aisc_main = create_AN_main(); |
|---|
| 1190 | |
|---|
| 1191 | // try to open com with running name server |
|---|
| 1192 | if (params->tcp) { |
|---|
| 1193 | name = params->tcp; |
|---|
| 1194 | } |
|---|
| 1195 | else { |
|---|
| 1196 | const char *cname = GBS_read_arb_tcp(GBS_nameserver_tag(NULL)); |
|---|
| 1197 | |
|---|
| 1198 | if (!cname) { |
|---|
| 1199 | GB_print_error(); |
|---|
| 1200 | exit(EXIT_FAILURE); |
|---|
| 1201 | } |
|---|
| 1202 | name = strdup(cname); |
|---|
| 1203 | } |
|---|
| 1204 | |
|---|
| 1205 | GB_ERROR error = NULL; |
|---|
| 1206 | AN_global.cl_link = aisc_open(name, AN_global.cl_main, AISC_MAGIC_NUMBER, &error); |
|---|
| 1207 | |
|---|
| 1208 | if (AN_global.cl_link) { |
|---|
| 1209 | if (!strcmp(argv[1], "-look")) { |
|---|
| 1210 | printf("ARB_name_server: No client - terminating.\n"); |
|---|
| 1211 | aisc_close(AN_global.cl_link, AN_global.cl_main); AN_global.cl_link = 0; |
|---|
| 1212 | exit(EXIT_SUCCESS); |
|---|
| 1213 | } |
|---|
| 1214 | |
|---|
| 1215 | printf("There is another active nameserver. I try to kill it..\n"); |
|---|
| 1216 | aisc_nput(AN_global.cl_link, AN_MAIN, AN_global.cl_main, |
|---|
| 1217 | MAIN_SHUTDOWN, "ldfiojkherjkh", |
|---|
| 1218 | NULL); |
|---|
| 1219 | aisc_close(AN_global.cl_link, AN_global.cl_main); AN_global.cl_link=0; |
|---|
| 1220 | GB_sleep(1, SEC); |
|---|
| 1221 | } |
|---|
| 1222 | |
|---|
| 1223 | if (error) { |
|---|
| 1224 | printf("ARB_name_server: %s\n", error); |
|---|
| 1225 | exit(EXIT_FAILURE); |
|---|
| 1226 | } |
|---|
| 1227 | |
|---|
| 1228 | if (((strcmp(argv[1], "-kill") == 0)) || |
|---|
| 1229 | ((argc==3) && (strcmp(argv[2], "-kill")==0))) { |
|---|
| 1230 | printf("ARB_name_server: Now I kill myself!\n"); |
|---|
| 1231 | exit(EXIT_SUCCESS); |
|---|
| 1232 | } |
|---|
| 1233 | for (i=0, so=0; (i<MAX_TRY) && (!so); i++) { |
|---|
| 1234 | so = open_aisc_server(name, NAME_SERVER_SLEEP*1000L, 0); |
|---|
| 1235 | if (!so) GB_sleep(RETRY_SLEEP, SEC); |
|---|
| 1236 | } |
|---|
| 1237 | if (!so) { |
|---|
| 1238 | printf("AN_SERVER: Gave up on opening the communication socket!\n"); |
|---|
| 1239 | exit(EXIT_FAILURE); |
|---|
| 1240 | } |
|---|
| 1241 | AN_global.server_communication = so; |
|---|
| 1242 | |
|---|
| 1243 | aisc_main->server_file = strdup(params->default_file); |
|---|
| 1244 | aisc_main->server_filedate = GB_time_of_file(aisc_main->server_file); |
|---|
| 1245 | |
|---|
| 1246 | error = server_load(aisc_main); |
|---|
| 1247 | |
|---|
| 1248 | if (!error) { |
|---|
| 1249 | const char *field = params->field; |
|---|
| 1250 | const char *field_default = params->field_default; |
|---|
| 1251 | |
|---|
| 1252 | if (field == 0) { |
|---|
| 1253 | field = ""; // default to no field |
|---|
| 1254 | field_default = ""; |
|---|
| 1255 | } |
|---|
| 1256 | else { |
|---|
| 1257 | if (!params->field_default) { |
|---|
| 1258 | error = GBS_global_string("Missing default value for add.field (option has to be -f%s=defaultValue)", field); |
|---|
| 1259 | } |
|---|
| 1260 | } |
|---|
| 1261 | |
|---|
| 1262 | if (!error) { |
|---|
| 1263 | if (aisc_main->add_field == 0) { // add. field was not defined yet |
|---|
| 1264 | if (field[0]) { |
|---|
| 1265 | printf("* add. field not defined yet -> using '%s'\n", field); |
|---|
| 1266 | } |
|---|
| 1267 | else { |
|---|
| 1268 | fputs("* using no add. field\n", stdout); |
|---|
| 1269 | } |
|---|
| 1270 | aisc_main->add_field = strdup(field); |
|---|
| 1271 | aisc_main->touched = 1; |
|---|
| 1272 | } |
|---|
| 1273 | else { |
|---|
| 1274 | if (strcmp(aisc_main->add_field, field) != 0) { // add. field changed |
|---|
| 1275 | if (aisc_main->add_field[0]) { |
|---|
| 1276 | error = GBS_global_string("This names-DB has to be started with -f%s", aisc_main->add_field); |
|---|
| 1277 | } |
|---|
| 1278 | else { |
|---|
| 1279 | error = "This names-DB has to be started w/o -f option"; |
|---|
| 1280 | } |
|---|
| 1281 | } |
|---|
| 1282 | } |
|---|
| 1283 | } |
|---|
| 1284 | |
|---|
| 1285 | if (!error) { |
|---|
| 1286 | char *field_default_alnum = make_alnum(field_default); |
|---|
| 1287 | |
|---|
| 1288 | if (aisc_main->add_field_default == 0) { // previously no default was defined for add.field |
|---|
| 1289 | reassign(aisc_main->add_field_default, field_default_alnum); |
|---|
| 1290 | set_empty_addids(aisc_main); |
|---|
| 1291 | aisc_main->touched = 1; |
|---|
| 1292 | } |
|---|
| 1293 | else { |
|---|
| 1294 | if (strcmp(aisc_main->add_field_default, field_default_alnum) != 0) { |
|---|
| 1295 | error = GBS_global_string("Default for add.field previously was '%s' (called with '%s')\n" |
|---|
| 1296 | "If you really need to change this - delete the names DB", |
|---|
| 1297 | aisc_main->add_field_default, field_default_alnum); |
|---|
| 1298 | } |
|---|
| 1299 | } |
|---|
| 1300 | free(field_default_alnum); |
|---|
| 1301 | } |
|---|
| 1302 | } |
|---|
| 1303 | |
|---|
| 1304 | long accept_calls_init = NAME_SERVER_TIMEOUT/long(NAME_SERVER_SLEEP); |
|---|
| 1305 | long accept_calls = accept_calls_init; |
|---|
| 1306 | bool isTimeout = true; |
|---|
| 1307 | |
|---|
| 1308 | if (!error && aisc_main->touched) server_save(aisc_main, 0); |
|---|
| 1309 | |
|---|
| 1310 | |
|---|
| 1311 | while (!error && accept_calls>0) { |
|---|
| 1312 | aisc_accept_calls(so); |
|---|
| 1313 | |
|---|
| 1314 | if (aisc_main->ploc_st.cnt <= 0) { // timeout passed and no clients |
|---|
| 1315 | accept_calls--; |
|---|
| 1316 | |
|---|
| 1317 | long server_date = GB_time_of_file(aisc_main->server_file); |
|---|
| 1318 | if (server_date == 0 && aisc_main->server_filedate != 0) { |
|---|
| 1319 | fprintf(stderr, "ARB_name_server data has been deleted.\n"); |
|---|
| 1320 | accept_calls = 0; |
|---|
| 1321 | isTimeout = false; |
|---|
| 1322 | } |
|---|
| 1323 | if (server_date>aisc_main->server_filedate) { |
|---|
| 1324 | fprintf(stderr, "ARB_name_server data changed on disc.\n"); |
|---|
| 1325 | accept_calls = 0; |
|---|
| 1326 | isTimeout = false; |
|---|
| 1327 | } |
|---|
| 1328 | else if (aisc_main->touched) { |
|---|
| 1329 | server_save(aisc_main, 0); |
|---|
| 1330 | accept_calls = accept_calls_init; |
|---|
| 1331 | } |
|---|
| 1332 | } |
|---|
| 1333 | else { // got a client |
|---|
| 1334 | accept_calls = accept_calls_init; |
|---|
| 1335 | } |
|---|
| 1336 | } |
|---|
| 1337 | |
|---|
| 1338 | if (error) { |
|---|
| 1339 | char *message = GBS_global_string_copy("Error in ARB_name_server: %s", error); |
|---|
| 1340 | char *send = GBS_global_string_copy("arb_message \"%s\" &", message); // send async (otherwise deadlock!) |
|---|
| 1341 | |
|---|
| 1342 | fprintf(stderr, "%s\n", message); |
|---|
| 1343 | if (system(send) != 0) fprintf(stderr, "Failed to send error message to ARB\n"); |
|---|
| 1344 | |
|---|
| 1345 | free(send); |
|---|
| 1346 | free(message); |
|---|
| 1347 | } |
|---|
| 1348 | else if (accept_calls == 0) { |
|---|
| 1349 | if (isTimeout) { |
|---|
| 1350 | fprintf(stderr, "Been idle for %i minutes.\n", int(NAME_SERVER_TIMEOUT/60)); |
|---|
| 1351 | } |
|---|
| 1352 | } |
|---|
| 1353 | |
|---|
| 1354 | printf("ARB_name_server terminating...\n"); |
|---|
| 1355 | if (nameModHash) GBS_free_hash(nameModHash); |
|---|
| 1356 | names_server_shutdown(error ? EXIT_FAILURE : EXIT_SUCCESS); // never returns |
|---|
| 1357 | } |
|---|