Home | History | Annotate | Line # | Download | only in common
interp_backslash.c revision 1.2.2.2
      1 /*	$NetBSD: interp_backslash.c,v 1.2.2.2 2006/04/22 11:37:38 simonb Exp $	*/
      2 
      3 /*-
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * Jordan K. Hubbard
     14  * 29 August 1998
     15  *
     16  * Routine for doing backslash elimination.
     17  */
     18 
     19 #include <sys/cdefs.h>
     20 
     21 #include <lib/libsa/stand.h>
     22 #include <lib/libkern/libkern.h>
     23 
     24 #include "bootstrap.h"
     25 
     26 #define DIGIT(x) (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
     27 
     28 /*
     29  * backslash: Return malloc'd copy of str with all standard "backslash
     30  * processing" done on it.  Original can be free'd if desired.
     31  */
     32 char *
     33 backslash(char *str)
     34 {
     35     /*
     36      * Remove backslashes from the strings. Turn \040 etc. into a single
     37      * character (we allow eight bit values). Currently NUL is not
     38      * allowed.
     39      *
     40      * Turn "\n" and "\t" into '\n' and '\t' characters. Etc.
     41      *
     42      */
     43     char *new_str;
     44     int seenbs = 0;
     45     int i = 0;
     46 
     47     if ((new_str = strdup(str)) == NULL)
     48 	return NULL;
     49 
     50     while (*str) {
     51 	if (seenbs) {
     52 	    seenbs = 0;
     53 	    switch (*str) {
     54 	    case '\\':
     55 		new_str[i++] = '\\';
     56 		str++;
     57 		break;
     58 
     59 	    /* preserve backslashed quotes, dollar signs */
     60 	    case '\'':
     61 	    case '"':
     62 	    case '$':
     63 		new_str[i++] = '\\';
     64 		new_str[i++] = *str++;
     65 		break;
     66 
     67 	    case 'b':
     68 		new_str[i++] = '\b';
     69 		str++;
     70 		break;
     71 
     72 	    case 'f':
     73 		new_str[i++] = '\f';
     74 		str++;
     75 		break;
     76 
     77 	    case 'r':
     78 		new_str[i++] = '\r';
     79 		str++;
     80 		break;
     81 
     82 	    case 'n':
     83 		new_str[i++] = '\n';
     84 		str++;
     85 		break;
     86 
     87 	    case 's':
     88 		new_str[i++] = ' ';
     89 		str++;
     90 		break;
     91 
     92 	    case 't':
     93 		new_str[i++] = '\t';
     94 		str++;
     95 		break;
     96 
     97 	    case 'v':
     98 		new_str[i++] = '\13';
     99 		str++;
    100 		break;
    101 
    102 	    case 'z':
    103 		str++;
    104 		break;
    105 
    106 	    case '0': case '1': case '2': case '3': case '4':
    107 	    case '5': case '6': case '7': case '8': case '9': {
    108 		char val;
    109 
    110 		/* Three digit octal constant? */
    111 		if (*str >= '0' && *str <= '3' &&
    112 		    *(str + 1) >= '0' && *(str + 1) <= '7' &&
    113 		    *(str + 2) >= '0' && *(str + 2) <= '7') {
    114 
    115 		    val = (DIGIT(*str) << 6) + (DIGIT(*(str + 1)) << 3) +
    116 			DIGIT(*(str + 2));
    117 
    118 		    /* Allow null value if user really wants to shoot
    119                        at feet, but beware! */
    120 		    new_str[i++] = val;
    121 		    str += 3;
    122 		    break;
    123 		}
    124 
    125 		/* One or two digit hex constant?
    126 		 * If two are there they will both be taken.
    127 		 * Use \z to split them up if this is not wanted.
    128 		 */
    129 		if (*str == '0' &&
    130 		    (*(str + 1) == 'x' || *(str + 1) == 'X') &&
    131 		    isxdigit(*(str + 2))) {
    132 		    val = DIGIT(*(str + 2));
    133 		    if (isxdigit(*(str + 3))) {
    134 			val = (val << 4) + DIGIT(*(str + 3));
    135 			str += 4;
    136 		    }
    137 		    else
    138 			str += 3;
    139 		    /* Yep, allow null value here too */
    140 		    new_str[i++] = val;
    141 		    break;
    142 		}
    143 	    }
    144 	    break;
    145 
    146 	    default:
    147 		new_str[i++] = *str++;
    148 		break;
    149 	    }
    150 	}
    151         else {
    152             if (*str == '\\') {
    153                 seenbs = 1;
    154                 str++;
    155             }
    156 	    else
    157 		new_str[i++] = *str++;
    158         }
    159     }
    160 
    161     if (seenbs) {
    162         /*
    163          * The final character was a '\'. Put it in as a single backslash.
    164          */
    165 	new_str[i++] = '\\';
    166     }
    167     new_str[i] = '\0';
    168     return new_str;
    169 }
    170