home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 317b.lha / RCS_Sources / rsbx.lib / escaping.c < prev    next >
C/C++ Source or Header  |  1989-12-05  |  3KB  |  140 lines

  1. /****** rsbx.lib/EscapeAdditions() *******************************************
  2. *
  3. *   NAME
  4. *    EscapeAdditions -- Return number of characters to escape an argument.
  5. *
  6. *   SYNOPSIS
  7. *    escapes = EscapeAdditions(argv)
  8. *
  9. *    int EscapeAdditions(char *);
  10. *
  11. *   FUNCTION
  12. *    Determines the number of extra characters that are needed to escape
  13. *        a command argument for AmigaDos command parsing.
  14. *
  15. *   INPUTS
  16. *    argv          - Argument to check.
  17. *
  18. *   RESULT
  19. *    escapes       - The number of characters that need to be added to the
  20. *                    argument to escape the characters special to AmigaDos.
  21. *
  22. *   NOTES
  23. *    Characters special to AmigaDos are " [0x22], * [0x2a], ESC [0x1b],
  24. *        LF [0x0a], SP [0x20].
  25. *
  26. *   SEE ALSO
  27. *    EscapeAdditions(), EscapeString().
  28. *
  29. *   BUGS
  30. *    Thinks spaces need an AmigaDos escape character preceeding them.
  31. *
  32. ******************************************************************************
  33. *
  34. */
  35.  
  36. #include <stdio.h>
  37. #include <string.h>
  38.  
  39.  
  40. #define QUOTE '\"'
  41. #define ESCAPE '*'
  42. #define ESC '\033'
  43. #define NL '\n'
  44. #define SPACE ' '
  45.  
  46. #define SPECIAL "\"*\033\n "
  47.  
  48.  
  49. int EscapeAdditions(char *argv);
  50. char *EscapeString(char *buff, char *argv);
  51.  
  52.  
  53. int EscapeAdditions(char *argv)
  54.     {
  55.     int count = 0;
  56.     char *pos = argv;
  57.  
  58.     while (pos = strpbrk(pos, SPECIAL))
  59.         {
  60.         count++;
  61.         pos++;
  62.         }
  63.  
  64.     if (count)
  65.         {
  66.         count += 2;
  67.         }
  68.  
  69.     return count;
  70.     }
  71.  
  72.  
  73. /****** rsbx.lib/EscapeString() **********************************************
  74. *
  75. *   NAME
  76. *    EscapeString -- Copy argument to buffer escaping as needed.
  77. *
  78. *   SYNOPSIS
  79. *    dest = EscapeString(buffer, argv)
  80. *
  81. *    char *EscapeAdditions(char *, char *);
  82. *
  83. *   FUNCTION
  84. *    Copies a string containing a single argument to a buffer, escaping
  85. *        characters special to AmigaDos.
  86. *
  87. *   INPUTS
  88. *    buffer        - Buffer for escaped argument string.
  89. *    argv          - Argument string to escape.
  90. *
  91. *   RESULT
  92. *    dest          - The same as buffer.
  93. *
  94. *   NOTES
  95. *    Characters special to AmigaDos are " [0x22], * [0x2a], ESC [0x1b],
  96. *        LF [0x0a], SP [0x20].
  97. *
  98. *   SEE ALSO
  99. *    EscapeAdditions(), EscapeString().
  100. *
  101. *   BUGS
  102. *    Thinks spaces need an AmigaDos escape character preceeding them.
  103. *
  104. ******************************************************************************
  105. *
  106. */
  107.  
  108. char *EscapeString(char *buff, char *argv)
  109.     {
  110.     char *pos;
  111.  
  112.     pos = buff;
  113.     *pos++ = '\"';
  114.     while (*argv)
  115.         {
  116.         switch (*argv)
  117.             {
  118.             case ESC:
  119.                 *pos++ = ESCAPE;
  120.                 *pos++ = 'E';
  121.                 break;
  122.             case NL:
  123.                 *pos++ = ESCAPE;
  124.                 *pos++ = 'N';
  125.                 break;
  126.             case QUOTE:
  127.             case SPACE:
  128.             case ESCAPE:
  129.                 *pos++ = ESCAPE;
  130.             default:
  131.                 *pos++ = *argv;
  132.             }
  133.         argv++;
  134.         }
  135.     *pos++ = '\"';
  136.     *pos = '\0';
  137.  
  138.     return buff;
  139.     }
  140.