home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Tools / misc / t-single.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  5.1 KB  |  266 lines

  1. /* single_parse.c: test program to parse a single address from stdin */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Tools/misc/RCS/t-single.c,v 6.0 1991/12/18 20:30:40 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Tools/misc/RCS/t-single.c,v 6.0 1991/12/18 20:30:40 jpo Rel $
  9.  *
  10.  * $Log: t-single.c,v $
  11.  * Revision 6.0  1991/12/18  20:30:40  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. /* ---------------------------------------------------------------------------
  19.  
  20. This tool allows a user to input one line of addresses from stdin.
  21. Each address on this line id parsed, normalised and output on stdout.
  22.  
  23. The tool is useful for examining (via gdb or such) how the address
  24. parsing and normalising routines operate.
  25.  
  26. As it was derived during the development of the rfc822norm filter, it
  27. takes that filters arguments, in particular -822, -733, -jnt, -bigend,
  28. -littleend and -fold.
  29.  
  30. ---------------------------------------------------------------------------- */
  31.  
  32.  
  33.  
  34. #include    "util.h"
  35. #include    <isode/cmd_srch.h>
  36. #include    "ap.h"
  37. #include    "chan.h"
  38. #include    "retcode.h"
  39.  
  40.  
  41. #define    OPT_822        1
  42. #define OPT_733        2    
  43. #define    OPT_JNT        3
  44. #define    OPT_BIGEND    4
  45. #define OPT_LITTLEEND    5
  46. #define OPT_FOLD    10
  47.  
  48. CMD_TABLE    tbl_options [] = { /* single_parse commandline options */
  49.     "-822",        OPT_822,
  50.     "-733",        OPT_733,
  51.     "-jnt",        OPT_JNT,
  52.     "-bigend",    OPT_BIGEND,
  53.     "-littleend",    OPT_LITTLEEND,
  54.     "-fold",    OPT_FOLD,
  55.     0,        -1
  56.     };
  57.  
  58. typedef enum {maj_none, rfc822, rfc733, jnt} Major_options;
  59. typedef enum {min_none, bigend, littleend} Minor_options;
  60.  
  61. static int    getch();
  62. int        pcol;
  63. int        nonempty;
  64. int        fold_width;
  65. int        order_pref;
  66. char        *myname;
  67. int        nadrs;
  68. extern AP_ptr    ap_pinit();
  69. extern int    ap_outtype;
  70. extern int    ap_perlev;
  71. extern char    *calloc();
  72.  
  73. #define    DEFAULT_FOLD_WIDTH    78
  74.  
  75. /* ARGSUSED */
  76. main(argc,argv)
  77. int    argc;
  78. char    **argv;
  79. {
  80.     /* parse flags */
  81.     Major_options maj = maj_none;
  82.     Minor_options mino = min_none;
  83.  
  84.     myname = *argv++;
  85.     sys_init(myname);
  86. /*    malloc_debug(2);*/
  87.     ap_outtype = AP_PARSE_SAME;
  88.     fold_width = DEFAULT_FOLD_WIDTH;
  89.     order_pref = CH_USA_PREF;
  90.  
  91.     /* parse command arguments */
  92.     while (*argv != NULL) {
  93.         switch(cmd_srch(*argv,tbl_options)) {
  94.             case -1:
  95.             printf("unknown option '%s'",*argv);
  96.             exit(1);
  97.  
  98.             case OPT_FOLD:
  99.             if (*(argv+1) == NULL) {
  100.                 printf("no fold width given");
  101.                 exit(1);
  102.             } else {
  103.                 ++argv;
  104.                 fold_width = atoi(*argv);
  105.             }
  106.             break;
  107.  
  108.             case OPT_822:
  109.             if ((maj == maj_none) || (maj == rfc822)) {
  110.                 ap_outtype |= AP_PARSE_822;
  111.                 maj = rfc822;
  112.                 break;
  113.             } 
  114.  
  115.             case OPT_733:
  116.             if ((maj == maj_none) || (maj == rfc733)) {
  117.                 ap_outtype |= AP_PARSE_733;
  118.                 maj = rfc733;
  119.                 break;
  120.             } 
  121.  
  122.             case OPT_JNT:
  123.             if ((maj == maj_none || maj == jnt)
  124.                 && (mino == min_none || mino == bigend)) {
  125.                 ap_outtype |= AP_PARSE_822;
  126.                 maj = jnt;
  127.                 ap_outtype |= AP_PARSE_BIG;
  128.                 mino = bigend;
  129.                 order_pref = CH_UK_PREF;
  130.                 break;
  131.             }
  132.             printf("multiple major parse options");
  133.             exit(1);
  134.  
  135.             case OPT_BIGEND:
  136.             if (mino == min_none || mino == bigend) {
  137.                 ap_outtype |= AP_PARSE_BIG;
  138.                 mino = bigend;
  139.                 order_pref = CH_UK_PREF;
  140.                 break;
  141.             }
  142.  
  143.             case OPT_LITTLEEND:
  144.             if (mino == min_none || mino == littleend) {
  145.                 mino = littleend;
  146.                 break;
  147.             }
  148.             printf("multiple minor parse options");
  149.             exit(1);
  150.  
  151.             default:
  152.             printf("unknown option '%s'",*argv);
  153.             exit(1);
  154.         }
  155.         argv++;
  156.     }
  157.  
  158.     /* ap_outype set so read in address */
  159.     proc();
  160.     exit(0);
  161. }
  162.  
  163. /*   */
  164. /* parse one line of addresses fro stdin to stdout */
  165. proc ()
  166. {
  167.     int    amp_fail;
  168.     int    res;
  169.     int    done;
  170.  
  171.     ap_clear();
  172.     nadrs = 0;
  173.     amp_fail = FALSE;
  174.     nonempty = FALSE;
  175.     done = FALSE;
  176.     while (done == FALSE) {
  177.         if (ap_pinit(getch) == BADAP) {
  178.             printf("ap_pinit failed\n");
  179.             exit(1);
  180.         }
  181.         res = ap_1adr();    /* parse one adr */
  182.         switch(res) {
  183.             case DONE:    /* done */
  184.             done = TRUE;
  185.             break;
  186.             case NOTOK:
  187.             amp_fail = TRUE;
  188.             break;
  189.             default:
  190.             break;
  191.         }
  192.  
  193.         if (done == FALSE) {
  194.             res = out_adr(ap_pstrt);
  195.         }
  196.         ap_sqdelete(ap_pstrt, NULLAP);
  197.         ap_free(ap_pstrt);
  198.         ap_pstrt = NULLAP;
  199.     }
  200.  
  201.     putchar('\n');
  202.     if (ap_perlev) {
  203.         printf("nested level %d\n",ap_perlev);
  204.         amp_fail++;
  205.     }
  206.     if (amp_fail == TRUE)
  207.         printf("Parse error in original version\n");
  208. }
  209.  
  210. static int    out_adr(ap)
  211. register AP_ptr    ap;
  212. {
  213.     AP_ptr  loc_ptr,        /* -- in case fake personal name needed -- */
  214.         group_ptr,
  215.         name_ptr,
  216.         dom_ptr,
  217.         route_ptr,
  218.         return_tree;
  219.     char    *addrp;
  220.     int    len;
  221.  
  222.     if (ap->ap_obtype == AP_NIL)
  223.         return OK;
  224.  
  225.     if (nadrs != 0) {
  226.         printf(", ");
  227.         pcol += 2;
  228.     }
  229.  
  230.     ap = ap_normalize(ap, order_pref);
  231.  
  232.     return_tree = ap_t2p (ap, &group_ptr, &name_ptr, &loc_ptr,
  233.                   &dom_ptr,    &route_ptr);
  234.  
  235.     if (return_tree == BADAP) {
  236.         printf("error from ap_t2p\n");
  237.         exit(1);
  238.     } else {
  239.         addrp = ap_p2s(group_ptr, name_ptr, loc_ptr, 
  240.                    dom_ptr, route_ptr);
  241.  
  242.         if (addrp == (char *) NOTOK) {
  243.             printf("error from ap_p2s()\n");
  244.         }
  245.     }
  246.  
  247.     if ((len = strlen(addrp)) > 0) { /* print */
  248.         pcol += len;
  249.         if (pcol >= fold_width && nonempty) {
  250.             pcol = 2 + len;
  251.             printf("\n%*s", 2, "");
  252.         } else
  253.             nonempty = TRUE;
  254.         
  255.         printf("%s",addrp);
  256.         nadrs++;
  257.     }
  258.     free(addrp);
  259.     return OK;
  260. }
  261.  
  262. static int    getch()
  263. {
  264.     return    getchar();
  265. }
  266.