home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / cz / part07 / get-header.c < prev   
C/C++ Source or Header  |  1989-10-01  |  3KB  |  131 lines

  1. /*
  2.  * get-header - extract a header from a mail message or news article
  3.  */
  4.  
  5. #ifndef lint
  6. static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
  7. #endif lint
  8.  
  9. /*
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License version 1,
  12.  * as published by the Free Software Foundation.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <howard/port.h>
  26. #include <howard/version.h>
  27. #include <howard/usage.h>
  28.  
  29. MAINVER ("@(#)$Header: get-header.c,v 1.3 89/08/19 11:11:26 howard Exp $");
  30. USAGE ("[-f] [-i] [-w] header [default]");
  31.  
  32. #include <howard/malf.h>
  33. #include <howard/registers.i>
  34.  
  35. #define MLINE 1024 /* Max line length.*/
  36.  
  37. PRIVATE char eIntern[] = "Internal error %s";
  38.  
  39. /* main - main function                            */
  40.  
  41. PUBLIC int main (argc, argv)
  42.    int    argc; /* Number of arguments.*/
  43. R4 bStrT *argv; /* Points to array of argument strings.*/
  44.  
  45. /* Function:
  46.  *    
  47.  * Algorithm:
  48.  *    Decode arguments.  Read stdin until a match or a blank line.
  49.  *    If no match, write the default if there is one.
  50.  *    If there's no full name extraction, just write the field.
  51.  * Notes:
  52.  *    
  53.  */
  54.  
  55. {
  56. R5     rcharT   c;               /* Option letter.*/
  57. R3     bStrT    cp;              /* Steps through args.*/
  58. extern int      optind;          /* See getopt (3).*/
  59. extern cStrT    optarg;          /* See getopt (3).*/
  60. R8     boolT    fullnm = FALSE;  /* Get full name.*/
  61. R2     boolT    igncase = FALSE; /* Ignore case.*/
  62.        unsigned ln = 0;          /* Input line number.*/
  63. R1     bStrT    p;               /* Steps through lb[].*/
  64. R6     boolT    stripwh = FALSE; /* Strip leading spaces and tabs.*/
  65.        byteT    fnb[MLINE];      /* Line buffer.*/
  66.        byteT    lb[MLINE];       /* Line buffer.*/
  67.  
  68. while (EOF != (c = getopt (argc, (cStrT *) argv, "fiw")))
  69.    {
  70.    switch (c)
  71.       {
  72.       case '?':
  73.          usage();
  74.          break;
  75.       case 'f':
  76.          fullnm = TRUE;
  77.          break;
  78.       case 'i':
  79.          igncase = TRUE;
  80.          break;
  81.       case 'w':
  82.          stripwh = TRUE;
  83.          break;
  84.       default:
  85.          malf1 (eIntern, "main 1");
  86.          break;
  87.       }
  88.    }
  89. argv += optind;
  90. cp = *argv++;
  91. if (NULBSTR == cp) usage();
  92. while (NULBSTR != (p = getlin (lb, MLINE, stdin, S("Standard Input"), &ln, 0)))
  93.    {
  94.    p = (igncase ? prefxi (cp, lb) : prefix (cp, lb));
  95.    if ((NULBSTR != p) && (':' == B(*p)) && SPCTAB (B(p[1])))
  96.       {
  97.       p += 2;
  98.       break;
  99.       }
  100.    else if (EOS == B(lb[0]))
  101.       {
  102.       p = NULBSTR;
  103.       break;
  104.       }
  105.    }
  106. if (stripwh && (NULBSTR != p))
  107.    {
  108.    while (SPCTAB (B(*p)))
  109.       ++p;
  110.    }
  111. if ((NULBSTR == p) || (EOS == B(*p)))
  112.    {
  113.    cp = *argv++;
  114.    if (NULBSTR != cp)
  115.       {
  116.       if (NULBSTR != *argv) usage();
  117.       PUTS (cp);
  118.       }
  119.    }
  120. else if (fullnm && msgfn (p, fnb))
  121.    PUTS (fnb);
  122. else
  123.    PUTS (p);
  124. mfflush (stdout, S("Standard Output"));
  125. exit (SUCCESS);
  126.  
  127. #ifdef lint
  128. return (SUCCESS);
  129. #endif
  130. }
  131.