home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / addr / ap_val2str.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  3.4 KB  |  190 lines

  1. /* ap_val2str: converts values to strings */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/addr/RCS/ap_val2str.c,v 6.0 1991/12/18 20:21:24 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/addr/RCS/ap_val2str.c,v 6.0 1991/12/18 20:21:24 jpo Rel $
  9.  *
  10.  * $Log: ap_val2str.c,v $
  11.  * Revision 6.0  1991/12/18  20:21:24  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. /*
  19. This function is just barely usable.  The hole problem of
  20. quoted strings is hard to get right especially when the
  21. mail system is trying to make up for human forgetfulness.
  22.         -DPK-
  23.  
  24. SEK - have improved this somewhat by giving knowledge of
  25. various object types.  Does not handle strings of spaces.
  26. */
  27.  
  28. #include    "util.h"
  29. #include    "ap.h"
  30.  
  31. void ap_val2str (buf, value, obtype) /* -- convert to canonical str -- */
  32. char                    *buf,
  33.             *value,
  34.             obtype;
  35. {
  36.     int             got_spcl;
  37.     int             in_quote;
  38.     register char   *from_ptr,
  39.             *to_ptr;
  40.  
  41.     PP_DBG (("Lib/addr/ap_val2str ('%s', '%d')", value, obtype));
  42.  
  43.     if (obtype == AP_COMMENT) {
  44.         for (from_ptr = value, to_ptr = buf; *from_ptr != '\0';
  45.              *to_ptr++ = *from_ptr++)
  46.  
  47.             switch (*from_ptr) {
  48.             case '\r':
  49.             case '\n':
  50.             case '\\':
  51.             case '(':
  52.             case ')':
  53.                 *to_ptr++ = '\\';
  54.             }
  55.  
  56.         *to_ptr = '\0';
  57.         return;
  58.     }
  59.  
  60.  
  61.     if (obtype == AP_DOMAIN_LITERAL) {
  62.         for (from_ptr = value, to_ptr = buf; *from_ptr != '\0';
  63.              *to_ptr++ = *from_ptr++)
  64.  
  65.             switch (*from_ptr) {
  66.             case '\r':
  67.             case '\n':
  68.             case '\\':
  69.                 *to_ptr++ = '\\';
  70.                 continue;
  71.             case '[':
  72.                 if (from_ptr != value)
  73.                     *to_ptr++ = '\\';
  74.                 continue;
  75.             case ']':
  76.                 if (*(from_ptr + 1) != (char) 0)
  77.                     *to_ptr++ = '\\';
  78.             }
  79.  
  80.         *to_ptr = '\0';
  81.         return;
  82.     }
  83.  
  84.  
  85.     in_quote = FALSE;
  86.  
  87.     for (got_spcl = FALSE, from_ptr = value; *from_ptr != '\0';
  88.          from_ptr++) {
  89.         switch (*from_ptr) {
  90.             case '"':
  91.             /* -- flip-flop -- */
  92.             in_quote = (in_quote == TRUE ? FALSE : TRUE);
  93.             break;
  94.  
  95.             case '\\':
  96.             case '\r':
  97.             case '\n':
  98.             if (in_quote == FALSE) {
  99.                 got_spcl = TRUE;
  100.                 goto copyit;
  101.             }
  102.             break;
  103.  
  104.             case '<':
  105.             case '>':
  106.             case '@':
  107.             case ',':
  108.             case ';':
  109.             case ':':
  110.             case '\t':
  111.             case '[':
  112.             case ']':
  113.             case '(':
  114.             case ')':
  115.             if (in_quote == FALSE) {
  116.                 got_spcl = TRUE;
  117.                 goto copyit;
  118.             }
  119.             break;
  120.  
  121.             case ' ':
  122.             if (in_quote == FALSE)
  123.                 if ((obtype == AP_DOMAIN) ||
  124.                     (obtype == AP_MAILBOX)) {
  125.                     got_spcl = TRUE;
  126.                     goto copyit;
  127.                 }
  128.                 else {
  129.                     /* -- hacked to handle " at " -- */
  130.                     /* -- this really is needed -- */
  131.  
  132.                     if ((uptolow (*(from_ptr + 1))
  133.                          == 'a') &&
  134.                         (uptolow (*(from_ptr + 2))
  135.                          == 't') &&
  136.                         (*(from_ptr + 3)
  137.                          == ' ')) {
  138.                         got_spcl = TRUE;
  139.                         goto copyit;
  140.                     }
  141.                 }
  142.             break;
  143.  
  144.  
  145.             case '.':
  146.             if (in_quote == FALSE &&
  147.                 ((obtype == AP_GROUP_START) ||
  148.                  (obtype == AP_PERSON_START))) {
  149.                 got_spcl = TRUE;
  150.                 goto copyit;
  151.             }
  152.             break;
  153.             default:
  154.             if (!isascii(*from_ptr) || iscntrl(*from_ptr))
  155.                 if (in_quote == FALSE) {
  156.                     got_spcl = TRUE;
  157.                     goto copyit;
  158.                 }
  159.             break;
  160.         }
  161.     }
  162.  
  163.  
  164.  
  165.     /* -- if were in a quote, something's wrong -- */
  166.     got_spcl = in_quote;
  167.  
  168. copyit:
  169.     to_ptr = buf;
  170.  
  171.     if (got_spcl)
  172.         *to_ptr++ = '"';
  173.  
  174.     for (from_ptr = value; *from_ptr != '\0'; *to_ptr++ = *from_ptr++)
  175.         switch (*from_ptr) {
  176.         case '\r':
  177.         case '\n':
  178.         case '\\':
  179.         case '"':
  180.             if (got_spcl)
  181.                 *to_ptr++ = '\\';
  182.         }
  183.  
  184.     if (got_spcl)
  185.         *to_ptr++ = '"';
  186.  
  187.     *to_ptr = '\0';
  188.  
  189. }
  190.