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