1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
|
/* -*- C -*-
* FLIST.C
*
* (c)Copyright 1994 by Tobias Ferber, All Rights Reserved
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 1 of the License,
* or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* $VER: $Id: flist.c,v 1.1 2001-07-18 11:05:53 root Exp $ */
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "debug.h"
#include "flist.h"
/* My favorite macro */
#define NIL(type) (type)0
#ifdef __GNUC__
/* suggested parentheses around assignment used as truth value */
#define if(assignment) if( (assignment) )
#endif /* __GNUC__ */
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif /* !MAXPATHLEN */
/****** flist/--background-- *************************************************
*
* FLIST ist eine auf Dateinamen spezialisierte FIFO Implementierung.
* Der Haupt-Anwendungsbereich d"urfte wohl im Bereich des Command-Line
* Parsings liegen, dem Durchwandern der Argumente (Optionen) eines
* Programms.
*
* Meist liegen im argv[] Array die Optionen und die Dateinamen in
* gemischter Form vor. Da ein Umsortieren von argv[] kritisch ist
* empfiehlt es sich die Liste der Argumente von links nach rechts zu
* durchwandern und die Dateinamen in einer separaten Liste zu sammeln.
* Genau hier setzt FLIST an.
*
******************************************************************************
*
*/
typedef struct fnode { /* filename node */
struct fnode *next; /* next node (command line from left to right) */
char *filename; /* the input filename */
char *hostname; /* where `filename' appeared, NIL == command-line */
long hostline; /* line number in which `filename' appeared */
} fnode_t;
static fnode_t *flist= NIL(fnode_t *), /* very first node of the list */
*fcurr= NIL(fnode_t *), /* head of the remaining sub-list */
*ftail= NIL(fnode_t *); /* very last node of the list */
/*
* >>> PRIVATE <<<
*/
/****** flist/fnode_new ******************************************************
*
* NAME
* fnode_new -- fnode constructor (static)
*
* SYNOPSIS
* fn= fnode_new()
*
* static fnode_t *fnode_new();
*
* FUNCTION
*
* SEE ALSO
* fnode_dispose()
*
******************************************************************************
*
* Ich habe aus den ****i* xxx/yyy **** Kommentaren der `static functions'
* normale Kommentare gemacht, damit ich nicht zwei docfiles erstellen mu\3...
*/
static fnode_t *fnode_new(void)
{
fnode_t *fn;
DD(bug_enter("fnode_new"));
fn= (fnode_t *)malloc( sizeof(struct fnode) );
if(fn)
{
fn->next = NIL(fnode_t *);
fn->filename =
fn->hostname = NIL(char *);
fn->hostline = 0;
}
DD(bug_leave("fnode_new (%s)", fn ? "successful":"failed"));
return fn;
}
/****** flist/fnode_dispose **************************************************
*
* NAME
* fnode_dispose -- fnode destructor (static)
*
* SYNOPSIS
* nil= fnode_dispose(fn)
*
* static fnode_t *fnode_dispose(fnode_t *);
*
* FUNCTION
*
* SEE ALSO
* fnode_new()
*
******************************************************************************
*
*/
static fnode_t *fnode_dispose(fnode_t *fn)
{
DD(bug_enter("fnode_dispose"));
if(fn)
{
if(fn->filename)
{
free(fn->filename);
fn->filename= NIL(char *);
}
fn->hostname= NIL(char *);
free(fn);
}
DD(bug_leave("fnode_dispose"));
return NIL(fnode_t *);
}
/****** flist/flist_addnode **************************************************
*
* NAME
* flist_addnode -- F"ugt eine fnode in die flist ein (static)
*
* SYNOPSIS
* flist_addnode(fn)
*
* static void flist_addnode(fnode_t *);
*
* FUNCTION
* Diese Funktion h"angt die gegebene fnode `fn' an die Liste der fnode
* Strukturen an und aktualisiert `flist', `fcurr' und `ftail'.
*
*
* SEE ALSO
* flist_addfile(), flist_from_file()
*
******************************************************************************
*
*/
static void flist_addnode(fnode_t *fn)
{
DD(bug_enter("flist_addnode"));
if(fn)
{
if(!flist) flist= fn;
if(!fcurr) fcurr= fn;
if(ftail)
ftail->next= fn;
ftail= fn;
}
DD(bug_leave("flist_addnode"));
}
/****** flist/fnode_from_file ************************************************
*
* NAME
* fnode_from_file -- Erzeugt eine fnode aus dem n"achsten Dateinamen
*
* SYNOPSIS
* fn= fnode_from_file(fp);
*
* fnode_t *fnode_from_file(FILE *);
*
* FUNCTION
* Diese Funktion legt eine neue fnode Struktur an und liest deren
* Auspr"agung aus dem Stream `fp' ein. Dabei werden die Felder
* `filename' und `hostline' gesetzt, der hostname jedoch nicht.
* Ein Aufruf dieser Funktion mit NIL(FILE *) als Argument f"uhrt dazu,
* da\3 der line Counter auf 1 zur"uckgesetzt wird.
*
* INPUTS
* fp - Der Stream aus dem der n"achste Dateiname gelesen
* werden soll
*
* RESULT
* fn - Ein Zeiger auf die erstellte fnode Struktur
*
* SEE ALSO
* flist_from_file()
*
******************************************************************************
*
*/
static fnode_t *fnode_from_file(FILE *fp)
{
static long line= 1;
fnode_t *fn= NIL(fnode_t *);
typedef enum { outer_mode,
word_mode,
string_mode,
return_mode,
error_mode
} smode_t;
smode_t smode= outer_mode;
DD(bug_enter("fnode_from_file"));
if(fp) /* -> neue fnode anlegen und Auspr"agung einlesen */
{
if( fn= fnode_new() )
{
char *fname= (char *)malloc( MAXPATHLEN * sizeof(char) );
if( fname )
{
int c, n=0;
fn->filename= fname;
while( smode != return_mode && smode != error_mode && !feof(fp) )
{
c= fgetc(fp);
if(feof(fp) && c!=EOF)
c= EOF;
switch(c)
{
case ' ': case '\t':
switch(smode)
{
case word_mode:
fname[n++]= '\0';
smode= return_mode;
break;
case string_mode:
fname[n++]= c;
break;
case outer_mode:
case return_mode:
case error_mode:
break;
}
break;
case '\n': case '\r':
switch(smode)
{
case word_mode:
fname[n++]= '\0';
smode= return_mode;
break;
case string_mode:
sprintf(fname,"%ld: unterminated string at EOL; missing quotes `\"'",line);
smode= error_mode;
break;
case outer_mode:
case return_mode:
case error_mode:
break;
}
line++;
{ int d= fgetc(fp);
if(!( (c=='\n' && d=='\r') || (c=='\r' && d=='\n') ))
ungetc(d,fp);
}
break;
case '\"':
switch(smode)
{
case outer_mode:
smode= string_mode;
fn->hostline= line;
break;
case string_mode:
fname[n++]= '\0';
smode= return_mode;
break;
case word_mode:
fname[n++]= '\0';
ungetc(c,fp);
smode= return_mode;
break;
case return_mode:
case error_mode:
break;
}
break;
case EOF:
switch(smode)
{
case word_mode:
if( feof(fp) )
{
fname[n++]= '\0';
smode= return_mode;
}
else fname[n++]= c;
break;
case string_mode:
if( feof(fp) )
{
sprintf(fname,"%ld: unterminated string at EOF; missing quotes `\"'",line);
smode= error_mode;
}
else fname[n++]= c;
break;
case outer_mode:
case return_mode:
case error_mode:
break;
}
break;
default:
switch(smode)
{
case outer_mode:
smode= word_mode;
fn->hostline= line;
/* fall through */
case word_mode:
case string_mode:
fname[n++]= c;
break;
case return_mode:
case error_mode:
break;
}
break;
}
if(n >= MAXPATHLEN)
{
sprintf(fname,"%ld: line too long",line);
smode= error_mode;
}
} /* wend */
if(smode != error_mode)
fname[n]= '\0';
else
fn->hostline= 0;
}
else /* !fname */
fn= fnode_dispose(fn);
}
}
else /* !fp || !fname */
line= 1;
DD(bug_leave("fnode_from_file"));
return fn;
}
/*
* >>> PUBLIC <<<
*/
/****** flist/flist_dispose **************************************************
*
* NAME
* flist_dispose -- Gibt die Liste der Dateinamen frei
*
* SYNOPSIS
* flist_dispose()
*
* void flist_dispose(void);
*
* FUNCTION
*
* SEE ALSO
* flist_addfile(), flist_from_file()
*
******************************************************************************
*
*/
void flist_dispose(void)
{
D(bug_enter("flist_dispose"));
while(flist)
{
fnode_t *t= flist;
flist= flist->next;
(void)fnode_dispose(t);
}
fcurr=
ftail= NIL(fnode_t *);
D(bug_leave("flist_dispose"));
}
/****** flist/flist_addfile **************************************************
*
* NAME
* flist_addfile -- F"ugt einen Dateinamen in die Fileliste ein
*
* SYNOPSIS
* error = flist_addfile(filename)
*
* int flist_addfile(char *);
*
* FUNCTION
* Diese Funktion h"angt einen Dateinamen (wie er z.B. von der command
* line kommt) an das Ende der Liste der Dateinamen an.
*
* INPUTS
* filename - Der Name der Datei
*
* RESULT
* error - 0, wenn alles geklappt hat
* 1, wenn nicht genug Speicher verf"ugbar war
*
* SEE ALSO
* flist_dispose()
*
******************************************************************************
*
*/
int flist_addfile(char *fname)
{
fnode_t *fn= NIL(fnode_t *);
D(bug_enter("flist_addfile \"%s\"",fname));
if(fname)
{
if( fn= fnode_new() )
{
fn->hostname = NIL(char *);
fn->hostline = 0;
fn->filename = strdup(fname);
if(fn->filename)
flist_addnode(fn);
else fn= fnode_dispose(fn);
}
}
D(bug_leave("flist_addfile (%s)", fn ? "successful":"failed"));
return fn ? 0:1;
}
/****** flist/flist_from_file ************************************************
*
* NAME
* flist_from_file -- Baut eine Liste aus den Dateinamen in einem File
*
* SYNOPSIS
* error= flist_from_file(filename);
*
* int flist_from_file(char *);
*
* FUNCTION
* Diese Funktion h"angt alle in der angegebenen Datei aufgef"uhrten
* Dateinamen an die Fileliste an.
*
* INPUTS
* filename - Der Name der Datei mit den Filenamen
*
* RESULT
* error - 0, wenn alles geklappt hat
* 1, im Fehlerfall
*
* SEE ALSO
* flist_dispose()
*
******************************************************************************
*
*/
int flist_from_file(char *host)
{
int rc= 0;
FILE *fp;
D(bug_enter("flist_from_file \"%s\"",host));
if( (fp= fopen(host,"r")) )
{
fnode_t *fn;
/* reset the line counter */
fnode_from_file( NIL(FILE *) );
while( !rc && !feof(fp) && !ferror(fp) )
{
if( fn= fnode_from_file(fp) )
{
if(fn->hostline > 0)
{
if(fn->filename && *fn->filename)
{
fn->hostname= host;
flist_addnode(fn);
}
}
else if(fn->filename && *fn->filename)
{
/* fn->filename contains an error message */
fprintf(stderr,"%s: %s\n",host,fn->filename);
rc= 1;
}
else fn= fnode_dispose(fn); /* EOF */
}
else /* !fn */
{
fprintf(stderr,"%s: %ld: ran out of memory!\n", host, fn->hostline);
rc= 2;
}
}
}
else perror(host);
D(bug_leave("flist_from_file (%s)", rc ? "failed":"successful"));
return rc;
}
/**/
char *flist_getname(void)
{
D(bug("flist_getname()"));
return fcurr ? fcurr->filename : NIL(char *);
}
char *flist_gethost(void)
{
D(bug("flist_gethost()"));
return fcurr ? fcurr->hostname : NIL(char *);
}
long flist_gethostline(void)
{
D(bug("flist_gethostline()"));
return fcurr ? fcurr->hostline : 0L;
}
int flist_nextfile(void)
{
D(bug("flist_nextfile()"));
if(fcurr)
fcurr= fcurr->next;
return fcurr ? 1:0;
}
#if defined(DEBUG) || defined(TEST)
void flist_print(FILE *fp)
{
fnode_t *fn= flist;
D(bug_enter("flist_print"));
if(fn)
{
int n;
fprintf(fp,"list of input files:\n");
for(n=0; fn; n++, fn= fn->next)
fprintf(fp,"\t%s:%ld: \"%s\"\n",
(fn->hostname ? fn->hostname
: "command-line"), fn->hostline, fn->filename);
}
else fprintf(fp,"list of input files is empty\n");
D(bug_leave("flist_print"));
}
#endif /* DEBUG || TEST */
#ifdef TEST
int main(int argc, char **argv)
{
int rc= 0;
char *whoami= argv[0];
while(--argc > 0 && !rc)
{
char *arg= *++argv;
if(*arg == '@')
{
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0L;
if(arg && *arg)
{
if( rc= flist_from_file(arg) )
fprintf(stderr,"%s: reading filenames from `%s' failed\n",whoami,arg);
}
else
{
fprintf(stderr,"%s: missing filename after `%s'\n",whoami,*argv);
rc= 1;
}
}
else /* !@... */
{
if( rc= flist_addfile(arg) )
fprintf(stderr,"%s: out of memory... aaaiiiiiieeeeeeeee!\n",whoami);
}
}
if(!rc)
flist_print(stdout);
flist_dispose();
if(rc) fprintf(stderr,"%s: [%s] *** Error %d\n"
"%s terminated abnormally (error %d)\n", whoami, *argv, rc, whoami, rc);
return rc;
}
#endif /* TEST */
|