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