[629] | 1 | /* |
---|
| 2 | * |
---|
[1939] | 3 | * Copyright (C) 1988, 1989 by the Massachusetts Institute of Technology |
---|
| 4 | * Developed by the MIT Student Information Processing Board (SIPB). |
---|
| 5 | * For copying information, see the file mit-copyright.h in this release. |
---|
| 6 | * |
---|
| 7 | */ |
---|
| 8 | /* |
---|
| 9 | * |
---|
[629] | 10 | * expunge -- program to expunge a meeting; i.e. really delete those |
---|
| 11 | * deleted transaction. This program is linked to a server |
---|
| 12 | * so it can use the privileged procedure of create_mtg, and |
---|
| 13 | * the like. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #include <stdio.h> |
---|
[22404] | 18 | #include <stdlib.h> |
---|
[6571] | 19 | #include <string.h> |
---|
[12439] | 20 | #if HAVE_FCNTL_H |
---|
[6571] | 21 | #include <fcntl.h> |
---|
| 22 | #endif |
---|
[2746] | 23 | #include <ctype.h> |
---|
[629] | 24 | #include <sys/types.h> |
---|
| 25 | #include <sys/file.h> |
---|
| 26 | #include <sys/stat.h> |
---|
[17452] | 27 | #include <unistd.h> |
---|
[629] | 28 | |
---|
[1504] | 29 | #include <discuss/types.h> |
---|
| 30 | #include <discuss/dsc_et.h> |
---|
| 31 | #include <discuss/tfile.h> |
---|
| 32 | #include <discuss/interface.h> |
---|
| 33 | #include <discuss/acl.h> |
---|
| 34 | #include "mtg.h" |
---|
| 35 | |
---|
[629] | 36 | #define min(a, b) (a < b ? a : b) |
---|
| 37 | |
---|
| 38 | static int tempf; |
---|
| 39 | static char *mtg_name = NULL, *location = NULL, *chairman = NULL, *trn_file = NULL; |
---|
| 40 | static char *backup_location = NULL; |
---|
[1508] | 41 | static char *future_location = NULL; |
---|
[629] | 42 | static int found_eof = 0; |
---|
| 43 | static int error_occurred = 0; |
---|
[1937] | 44 | static char *temp_dir = "/tmp"; |
---|
[2746] | 45 | static int daemon_flag = FALSE; |
---|
[629] | 46 | |
---|
| 47 | tfile unix_tfile (); |
---|
| 48 | |
---|
[2746] | 49 | static lower(), strip_addr(), extract_full_name(); |
---|
| 50 | static char *get_header(); |
---|
| 51 | |
---|
[1504] | 52 | extern char rpc_caller[]; |
---|
[629] | 53 | extern int has_privs; |
---|
| 54 | extern int errno; |
---|
[1626] | 55 | extern int no_nuke, use_zephyr; |
---|
[629] | 56 | |
---|
| 57 | main (argc, argv) |
---|
| 58 | int argc; |
---|
| 59 | char **argv; |
---|
| 60 | { |
---|
[1508] | 61 | int i,n,low,high; |
---|
[629] | 62 | mtg_info old_mtg_info,new_mtg_info; |
---|
[2746] | 63 | trn_info3 old_trn_info; |
---|
[629] | 64 | int result; |
---|
[1504] | 65 | dsc_acl *acl_list,*new_acl_list; |
---|
| 66 | dsc_acl_entry *ae; |
---|
[2746] | 67 | char *new_modes, *signature; |
---|
| 68 | char new_signature[50],dtest[10]; |
---|
[629] | 69 | tfile tf; |
---|
[1508] | 70 | char control_name[256]; |
---|
| 71 | int control_fd; |
---|
[7186] | 72 | static struct flock lock; |
---|
[1626] | 73 | |
---|
[7186] | 74 | |
---|
[23271] | 75 | #if defined(__APPLE__) && defined(__MACH__) |
---|
| 76 | add_error_table(&et_dsc_error_table); |
---|
| 77 | #else |
---|
[22864] | 78 | initialize_dsc_error_table(); |
---|
[23271] | 79 | #endif |
---|
[629] | 80 | |
---|
| 81 | for (i = 1; i < argc; i++) { |
---|
| 82 | if (*argv[i] == '-') switch (argv[i][1]) { |
---|
| 83 | case 'c': |
---|
| 84 | if (++i < argc) |
---|
| 85 | chairman = argv[i]; |
---|
| 86 | continue; |
---|
| 87 | |
---|
| 88 | case 'n': |
---|
| 89 | if (++i < argc) |
---|
| 90 | mtg_name = argv[i]; |
---|
| 91 | continue; |
---|
| 92 | |
---|
[1937] | 93 | case 't': |
---|
| 94 | if (++i < argc) |
---|
| 95 | temp_dir = argv[i]; |
---|
| 96 | continue; |
---|
| 97 | |
---|
[2746] | 98 | case 'd': |
---|
| 99 | daemon_flag = TRUE; |
---|
| 100 | continue; |
---|
| 101 | |
---|
[629] | 102 | default: |
---|
| 103 | goto lusage; |
---|
| 104 | } |
---|
| 105 | if (location == NULL) |
---|
| 106 | location = argv[i]; |
---|
| 107 | else goto lusage; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | if (location == NULL) |
---|
| 111 | goto lusage; /* required */ |
---|
| 112 | |
---|
[1626] | 113 | has_privs = TRUE; /* Tell discuss we're special */ |
---|
| 114 | use_zephyr = 0; /* Don't notify of every trn copied */ |
---|
[629] | 115 | strcpy (rpc_caller, "expunger"); |
---|
| 116 | |
---|
| 117 | /* First, we get the mtg info to make sure it exists */ |
---|
| 118 | get_mtg_info (location, &old_mtg_info, &result); |
---|
| 119 | if (result != 0) { |
---|
| 120 | fprintf(stderr, "%s: %s while getting mtg info\n", location, error_message(result)); |
---|
| 121 | exit (1); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | get_acl (location, &result, &acl_list); |
---|
| 125 | if (result != 0) { |
---|
| 126 | fprintf(stderr, "%s: %s while getting acl\n", location, error_message(result)); |
---|
| 127 | exit (1); |
---|
| 128 | } |
---|
| 129 | |
---|
[1508] | 130 | /* Create the new meeting */ |
---|
[629] | 131 | backup_location = malloc (strlen(location)+5); /* be generous */ |
---|
| 132 | strcpy (backup_location, location); |
---|
| 133 | strcat (backup_location, "~"); |
---|
| 134 | |
---|
[1508] | 135 | future_location = malloc (strlen(location)+5); /* be generous */ |
---|
| 136 | strcpy (future_location, location); |
---|
| 137 | strcat (future_location, "#"); |
---|
| 138 | |
---|
| 139 | printf("Creating new meeting\n"); |
---|
[629] | 140 | fflush(stdout); |
---|
| 141 | |
---|
| 142 | if (mtg_name == NULL) { |
---|
| 143 | mtg_name = old_mtg_info.long_name; |
---|
| 144 | } |
---|
| 145 | if (chairman == NULL) { |
---|
| 146 | chairman = old_mtg_info.chairman; |
---|
| 147 | } |
---|
[1508] | 148 | |
---|
[629] | 149 | /* get acl's on old meeting, so we can make it new one */ |
---|
[1508] | 150 | get_acl (location, &result, &new_acl_list); |
---|
[629] | 151 | if (result != 0) { |
---|
| 152 | fprintf(stderr, "%s: %s while getting acl\n", backup_location, error_message(result)); |
---|
| 153 | exit (1); |
---|
| 154 | } |
---|
| 155 | |
---|
[1508] | 156 | create_mtg_priv (backup_location, mtg_name, old_mtg_info.public_flag, |
---|
[1504] | 157 | old_mtg_info.date_created, chairman, |
---|
| 158 | new_acl_list, &result); |
---|
[629] | 159 | if (result != 0) { |
---|
[1504] | 160 | fprintf (stderr, "%s: %s while creating new meeting\n", |
---|
| 161 | location, error_message(result)); |
---|
[629] | 162 | exit (1); |
---|
| 163 | } |
---|
[1508] | 164 | |
---|
[629] | 165 | /* now, do the actual expunging */ |
---|
[1508] | 166 | low = old_mtg_info.lowest; |
---|
| 167 | high = old_mtg_info.highest; |
---|
[629] | 168 | create_temp (); |
---|
| 169 | |
---|
[1508] | 170 | expunge_range: |
---|
| 171 | for (i = low; i <= high; i++) { |
---|
[2746] | 172 | get_trn_info3 (location, i, &old_trn_info, &result); |
---|
[629] | 173 | if (result != 0 && result != DELETED_TRN && result != EXPUNGED_TRN) { |
---|
[1504] | 174 | fprintf(stderr, |
---|
| 175 | "Error getting info for transaction [%04d]: %s\n", |
---|
| 176 | i, error_message(result)); |
---|
[629] | 177 | error_occurred = TRUE; |
---|
| 178 | } else if (result != 0) { /* expunge it */ |
---|
[1508] | 179 | no_nuke = TRUE; |
---|
[629] | 180 | printf("Expunging transaction [%04d]\n", i); |
---|
[1508] | 181 | expunge_trn (backup_location, i, &result); |
---|
| 182 | no_nuke = FALSE; |
---|
[629] | 183 | if (result != 0) { |
---|
[1504] | 184 | fprintf(stderr, |
---|
| 185 | "Error expunging transaction [%04d]: %s\n", |
---|
| 186 | i, error_message(result)); |
---|
[629] | 187 | error_occurred = TRUE; |
---|
| 188 | } |
---|
| 189 | } else if (result == 0) { |
---|
[1080] | 190 | ftruncate(tempf,0); |
---|
[17452] | 191 | lseek(tempf,0,SEEK_SET); |
---|
[1080] | 192 | tf = unix_tfile (tempf); |
---|
[629] | 193 | |
---|
[1508] | 194 | get_trn (location, i, tf, &result); |
---|
[629] | 195 | if (result != 0) { |
---|
[1504] | 196 | fprintf(stderr, "Error getting transaction [%04d]: %s\n", |
---|
| 197 | i, error_message(result)); |
---|
[629] | 198 | error_occurred = TRUE; |
---|
| 199 | free(old_trn_info. author); |
---|
| 200 | free(old_trn_info.subject); |
---|
| 201 | continue; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | tdestroy (tf); |
---|
[2746] | 205 | |
---|
| 206 | signature = old_trn_info.signature; |
---|
| 207 | if (daemon_flag && old_trn_info.signature != NULL) { |
---|
[7186] | 208 | memmove(dtest, old_trn_info.signature, 7); |
---|
[2746] | 209 | dtest[7] = '\0'; |
---|
| 210 | if (!strcmp(dtest,"daemon@")) { |
---|
| 211 | if (get_from_signature(tempf, new_signature, sizeof(new_signature))) |
---|
| 212 | signature = new_signature; |
---|
| 213 | } |
---|
| 214 | } |
---|
[17452] | 215 | lseek(tempf,0,SEEK_SET); |
---|
[1080] | 216 | tf = unix_tfile (tempf); |
---|
[1508] | 217 | no_nuke = TRUE; |
---|
| 218 | add_trn_priv (backup_location, tf, old_trn_info.subject, |
---|
[2746] | 219 | signature, old_trn_info.pref, |
---|
| 220 | old_trn_info.current, old_trn_info.author, |
---|
| 221 | old_trn_info.date_entered, old_trn_info.flags, |
---|
[1504] | 222 | &n, &result); |
---|
[1508] | 223 | no_nuke = FALSE; |
---|
[629] | 224 | if (result != 0) { |
---|
[1504] | 225 | fprintf(stderr, |
---|
| 226 | "Error getting info for transaction %d: %s\n", i, |
---|
| 227 | error_message(result)); |
---|
[629] | 228 | error_occurred = TRUE; |
---|
| 229 | } |
---|
[2876] | 230 | tdestroy(tf); |
---|
[629] | 231 | free(old_trn_info.author); |
---|
| 232 | free(old_trn_info.subject); |
---|
[2746] | 233 | free(old_trn_info.signature); |
---|
[629] | 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
[1508] | 237 | /* Check if any new transactions have been added */ |
---|
| 238 | free(old_mtg_info.long_name); |
---|
| 239 | free(old_mtg_info.chairman); |
---|
| 240 | free(old_mtg_info.location); |
---|
| 241 | get_mtg_info(location, &old_mtg_info, &result); |
---|
| 242 | if (result != 0) { |
---|
| 243 | fprintf(stderr, "%s: %s while getting mtg info\n", location, error_message(result)); |
---|
| 244 | error_occurred = TRUE; |
---|
| 245 | } else if (old_mtg_info.highest > high) { /* New transactions added */ |
---|
| 246 | low = high+1; |
---|
| 247 | high = old_mtg_info.highest; |
---|
| 248 | goto expunge_range; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | strcpy(control_name, location); |
---|
| 252 | strcat(control_name, "/control"); |
---|
| 253 | if ((control_fd = open (control_name, O_RDWR, 0700)) < 0) { |
---|
| 254 | error_occurred = TRUE; |
---|
| 255 | } else { |
---|
[7186] | 256 | lock.l_type = F_WRLCK; |
---|
| 257 | lock.l_start = 0; |
---|
| 258 | lock.l_whence = 0; |
---|
| 259 | lock.l_len = 0; |
---|
[9551] | 260 | fcntl(control_fd, F_SETLKW, &lock); |
---|
[12439] | 261 | |
---|
[1508] | 262 | free(old_mtg_info.long_name); |
---|
| 263 | free(old_mtg_info.chairman); |
---|
| 264 | free(old_mtg_info.location); |
---|
| 265 | |
---|
| 266 | no_nuke = TRUE; |
---|
| 267 | get_mtg_info(location, &old_mtg_info, &result); |
---|
| 268 | if (result != 0) { |
---|
| 269 | fprintf(stderr, "%s: %s while getting mtg info\n", location, error_message(result)); |
---|
| 270 | error_occurred = TRUE; |
---|
[12439] | 271 | |
---|
| 272 | lock.l_type = F_UNLCK; |
---|
| 273 | lock.l_start = 0; |
---|
| 274 | lock.l_whence = 0; |
---|
| 275 | lock.l_len = 0; |
---|
| 276 | fcntl(control_fd, F_SETLK, &lock); |
---|
| 277 | |
---|
[1508] | 278 | close(control_fd); |
---|
| 279 | } else if (old_mtg_info.highest > high) { /* New transactions added */ |
---|
| 280 | low = high + 1; |
---|
| 281 | high = old_mtg_info.highest; |
---|
[7186] | 282 | lock.l_type = F_UNLCK; |
---|
| 283 | lock.l_start = 0; |
---|
| 284 | lock.l_whence = 0; |
---|
| 285 | lock.l_len = 0; |
---|
| 286 | fcntl(control_fd, F_SETLK, &lock); |
---|
[1508] | 287 | close(control_fd); |
---|
| 288 | goto expunge_range; |
---|
[629] | 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
[1508] | 292 | /* When we get here, we have the old meeting locked. Now we do the move |
---|
| 293 | as atomically as we can */ |
---|
[629] | 294 | if (!error_occurred) { |
---|
[1508] | 295 | if (rename(location, future_location) < 0) { |
---|
| 296 | perror("rename of old meeting failed"); |
---|
| 297 | exit (1); |
---|
| 298 | } |
---|
| 299 | if (rename(backup_location, location) < 0) { |
---|
| 300 | perror("rename of new meeting"); |
---|
| 301 | exit (1); |
---|
| 302 | } |
---|
| 303 | remove_mtg (future_location, &result); |
---|
[629] | 304 | if (result != 0) { |
---|
[1508] | 305 | fprintf(stderr, "%s: %s while removing new meeting.\n", |
---|
[1504] | 306 | location, error_message(result)); |
---|
[629] | 307 | exit (1); |
---|
| 308 | } |
---|
| 309 | } else exit (1); /* error occurred */ |
---|
| 310 | |
---|
| 311 | exit (0); |
---|
| 312 | |
---|
| 313 | lusage: |
---|
| 314 | fprintf(stderr, "usage: expunge mtg_location {-c chairman} {-n name}\n"); |
---|
| 315 | exit (1); |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /* |
---|
| 319 | * |
---|
| 320 | * create_temp () -- Create temp file, and let it be tempf. |
---|
| 321 | * |
---|
| 322 | */ |
---|
| 323 | create_temp() |
---|
| 324 | { |
---|
[1937] | 325 | char *filename; |
---|
[629] | 326 | |
---|
[1937] | 327 | filename = malloc (strlen (temp_dir) + 20); |
---|
| 328 | strcpy (filename, temp_dir); |
---|
| 329 | strcat (filename, "/rcXXXXXX"); |
---|
[629] | 330 | mktemp (filename); |
---|
| 331 | |
---|
| 332 | tempf = open (filename, O_RDWR | O_CREAT, 0700); |
---|
| 333 | if (tempf < 0) { |
---|
| 334 | fprintf (stderr, "Cannot open temp file\n"); |
---|
| 335 | exit (1); |
---|
| 336 | } |
---|
| 337 | } |
---|
[2746] | 338 | |
---|
| 339 | /* |
---|
| 340 | * |
---|
| 341 | * get_from_signature () -- Look in the file file_no, looking for a |
---|
| 342 | * From: line. Construct a signature from this from line, returning |
---|
| 343 | * it in sign, with a maximum length of sign_len. |
---|
| 344 | * |
---|
| 345 | */ |
---|
| 346 | |
---|
| 347 | int |
---|
| 348 | get_from_signature(file_no, sign, sign_len) |
---|
| 349 | int file_no; |
---|
| 350 | char *sign; |
---|
| 351 | int sign_len; |
---|
| 352 | { |
---|
| 353 | char buf[2048]; |
---|
| 354 | int len; |
---|
| 355 | char *cp, *hp; |
---|
| 356 | |
---|
[17452] | 357 | lseek(file_no, 0, SEEK_SET); |
---|
[2746] | 358 | len = read(file_no, buf, sizeof(buf)-1); |
---|
| 359 | buf[len] = '\0'; |
---|
| 360 | |
---|
| 361 | hp = get_header(buf, "from"); |
---|
| 362 | if (hp == NULL) |
---|
| 363 | return(0); |
---|
| 364 | |
---|
[7186] | 365 | cp = strchr(hp, ':'); |
---|
[2746] | 366 | if (cp == NULL) |
---|
| 367 | return(0); |
---|
| 368 | |
---|
| 369 | cp++; |
---|
| 370 | |
---|
| 371 | extract_full_name(cp, sign, sign_len); |
---|
| 372 | return(1); |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | /* Stolen from TechMail */ |
---|
| 376 | |
---|
| 377 | static |
---|
| 378 | lower(s) |
---|
| 379 | char *s; |
---|
| 380 | { |
---|
| 381 | while (*s) { |
---|
| 382 | if (isupper(*s)) |
---|
| 383 | *s = tolower(*s); |
---|
| 384 | s++; |
---|
| 385 | } |
---|
| 386 | return; |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | static |
---|
| 390 | strip_addr(addr, dest, dest_size) |
---|
| 391 | char *addr,*dest; |
---|
| 392 | int dest_size; |
---|
| 393 | { |
---|
| 394 | char *dest_end,*dp,*sp,*quote_start; |
---|
| 395 | int paren_level,found_angle; |
---|
| 396 | |
---|
| 397 | dest_end = &dest[dest_size-1]; |
---|
| 398 | dp = dest; |
---|
| 399 | sp = addr; |
---|
| 400 | paren_level = 0; |
---|
| 401 | |
---|
| 402 | eat_white: |
---|
| 403 | while (isspace(*sp) && *sp != '\n') |
---|
| 404 | sp++; |
---|
| 405 | |
---|
| 406 | if (*sp == '(') |
---|
| 407 | goto eat_comment; |
---|
| 408 | |
---|
| 409 | if (*sp == '"') |
---|
| 410 | goto eat_string; |
---|
| 411 | |
---|
| 412 | if (*sp == '<') { |
---|
| 413 | dp = dest; |
---|
| 414 | sp++; |
---|
| 415 | found_angle = TRUE; |
---|
| 416 | goto eat_white; |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | if (*sp == '>' && found_angle) { |
---|
| 420 | *sp++; |
---|
| 421 | goto eat_white; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | if (*sp == '\0' || *sp == '\n') { |
---|
| 425 | *dp++ = '\0'; |
---|
| 426 | goto post_proc; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | *dp++ = *sp++; |
---|
| 430 | if (dp == dest_end) { |
---|
| 431 | *dp++ = '\0'; |
---|
| 432 | goto post_proc; |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | goto eat_white; |
---|
| 436 | |
---|
| 437 | eat_comment: |
---|
| 438 | paren_level++; |
---|
| 439 | sp++; |
---|
| 440 | |
---|
| 441 | cont_comment: |
---|
| 442 | while (*sp != ')' && *sp != '(' && *sp) { |
---|
| 443 | sp++; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | if (*sp == '\0') { |
---|
| 447 | *dp = '\0'; |
---|
| 448 | goto post_proc; |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | if (*sp == '(') |
---|
| 452 | goto eat_comment; |
---|
| 453 | |
---|
| 454 | sp++; /* ) */ |
---|
| 455 | paren_level--; |
---|
| 456 | if (paren_level <= 0) |
---|
| 457 | goto eat_white; |
---|
| 458 | |
---|
| 459 | goto cont_comment; |
---|
| 460 | |
---|
| 461 | eat_string: |
---|
| 462 | quote_start = sp; |
---|
| 463 | sp++; |
---|
| 464 | |
---|
| 465 | while(*sp != '"' && *sp) |
---|
| 466 | sp++; |
---|
| 467 | |
---|
| 468 | if (!*sp) { |
---|
| 469 | *dp = '\0'; |
---|
| 470 | goto post_proc; |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | if (*++sp == '@') { /* "foo"@bar */ |
---|
| 474 | sp = quote_start; |
---|
| 475 | *dp++ = *sp++; |
---|
| 476 | while (dp < dest_end && *sp != '"') |
---|
| 477 | *dp++ = *sp++; |
---|
| 478 | |
---|
| 479 | if (dp == dest_end) { |
---|
| 480 | *dp = '\0'; |
---|
| 481 | goto post_proc; |
---|
| 482 | } |
---|
| 483 | *dp++ = *sp++; |
---|
| 484 | if (dp == dest_end) { |
---|
| 485 | *dp++ = '\0'; |
---|
| 486 | goto post_proc; |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | goto eat_white; |
---|
| 490 | |
---|
| 491 | /* No post processing */ |
---|
| 492 | post_proc: |
---|
| 493 | return; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | /* |
---|
| 497 | * |
---|
| 498 | * Routine to extract a full name from an address. If no full name |
---|
| 499 | * can be found, then we simply return the stripped address. |
---|
| 500 | * |
---|
| 501 | */ |
---|
| 502 | |
---|
| 503 | static |
---|
| 504 | extract_full_name(addr, dest, dest_size) |
---|
| 505 | char *addr,*dest; |
---|
| 506 | int dest_size; |
---|
| 507 | { |
---|
| 508 | char *dest_end,*dp,*sp,*bracket,*close_paren; |
---|
| 509 | int paren_level,non_white; |
---|
| 510 | |
---|
| 511 | dest_end = &dest[dest_size-1]; |
---|
| 512 | dp = dest; |
---|
| 513 | sp = addr; |
---|
| 514 | |
---|
| 515 | /* Find angle bracket (if possible) */ |
---|
| 516 | while (*sp && *sp != '<' && *sp != '\n') |
---|
| 517 | sp++; |
---|
| 518 | |
---|
| 519 | bracket = NULL; |
---|
| 520 | if (*sp == '<') |
---|
| 521 | bracket = sp; |
---|
| 522 | |
---|
| 523 | non_white = 0; |
---|
| 524 | if (bracket != NULL) { |
---|
| 525 | for (sp = addr; sp < bracket; sp++) { |
---|
| 526 | if (!isspace(*sp) && *sp != '"') |
---|
| 527 | non_white++; |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | if (non_white > 1) { /* We have a name */ |
---|
| 532 | sp = addr; |
---|
| 533 | while (isspace(*sp) || *sp == '"') /* Skip leading spaces */ |
---|
| 534 | sp++; |
---|
| 535 | |
---|
| 536 | while (isspace(*(bracket-1)) || *(bracket-1) == '"') /* Skip trailing spaces */ |
---|
| 537 | bracket--; |
---|
| 538 | |
---|
| 539 | /* Copy it over */ |
---|
| 540 | while (sp < bracket && dp < dest_end) |
---|
| 541 | *dp++ = *sp++; |
---|
| 542 | |
---|
| 543 | *dp++ = '\0'; |
---|
| 544 | return; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | /* Now, let's see if we have name in a comment (look back from the |
---|
| 548 | end for a parenthesis. */ |
---|
| 549 | for (sp = addr; *sp && *sp != '\n'; sp++) |
---|
| 550 | ; |
---|
| 551 | |
---|
| 552 | sp--; |
---|
| 553 | while (sp > addr && isspace(*sp)) |
---|
| 554 | sp--; |
---|
| 555 | |
---|
| 556 | if (*sp == ')') { /* Name in comment */ |
---|
| 557 | close_paren = sp; |
---|
| 558 | paren_level = 1; |
---|
| 559 | sp--; |
---|
| 560 | |
---|
| 561 | for (;sp > addr; sp--) { |
---|
| 562 | if (*sp == ')') |
---|
| 563 | paren_level++; |
---|
| 564 | else if (*sp == '(') { |
---|
| 565 | paren_level--; |
---|
| 566 | if (paren_level == 0) |
---|
| 567 | break; |
---|
| 568 | } |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | if (*sp == '(') { /* Copy it over */ |
---|
| 572 | sp++; |
---|
| 573 | |
---|
| 574 | while(isspace(*sp)) |
---|
| 575 | sp++; |
---|
| 576 | |
---|
| 577 | while (sp < close_paren && dp < dest_end) |
---|
| 578 | *dp++ = *sp++; |
---|
| 579 | |
---|
| 580 | *dp = '\0'; |
---|
| 581 | return; |
---|
| 582 | } |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | strip_addr(addr, dest, dest_size); |
---|
| 586 | return; |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | static char * |
---|
| 590 | get_header(hstring, hname) |
---|
| 591 | char *hstring, *hname; |
---|
| 592 | { |
---|
| 593 | char *eolp, *colonp, *cp; |
---|
| 594 | int name_len; |
---|
| 595 | char field[33]; |
---|
| 596 | |
---|
| 597 | cp = hstring; |
---|
| 598 | name_len = strlen(hname); |
---|
| 599 | |
---|
| 600 | while (*cp != '\0') { |
---|
[7186] | 601 | eolp = strchr(cp, '\n'); |
---|
| 602 | colonp = strchr(cp, ':'); |
---|
[2746] | 603 | |
---|
| 604 | if (eolp == NULL || colonp == NULL || eolp == cp) |
---|
| 605 | return(0); |
---|
| 606 | |
---|
| 607 | if (colonp > eolp || colonp - cp != name_len) { |
---|
| 608 | cp = eolp+1; |
---|
| 609 | continue; |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | /* Chance of a match, copy header over to name, and lower it */ |
---|
[7186] | 613 | memmove(field,cp, colonp - cp); |
---|
[2746] | 614 | field[colonp - cp] = '\0'; |
---|
| 615 | lower(field); |
---|
| 616 | if (!strcmp(field,hname)) |
---|
| 617 | return(cp); |
---|
| 618 | cp = eolp + 1; |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | return(NULL); |
---|
| 622 | } |
---|