std::string encode_binary(const std::string& str) { std::string res; res.resize(str.size()); size_t n = 0; for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) { if(needs_escaping(*j)) { res.resize(res.size()+1); res[n++] = escape_char; res[n++] = *j + 1; } else { res[n++] = *j; } } return res; }
void tprint(const char*sss) { char*buffer= NULL; int i= 0; int newlinechar= new_line_char_par; int dolog= 0; int doterm= 0; switch(selector){ case no_print: return; break; case term_only: doterm= 1; break; case log_only: dolog= 1; break; case term_and_log: dolog= 1; doterm= 1; break; case pseudo: while(*sss){ if(tally<trick_count){ trick_buf[tally%error_line]= (packed_ASCII_code)*sss++; tally++; }else{ return; } } return; break; case new_string: append_string((const unsigned char*)sss,(unsigned)strlen(sss)); return; break; default: { char*newstr= xstrdup(sss); char*s; for(s= newstr;*s;s++){ if(*s==newlinechar){ *s= '\n'; } } fputs(newstr,write_file[selector]); free(newstr); return; } break; } if(dolog||doterm){ buffer= xmalloc(strlen(sss)*3); if(dolog){ const unsigned char*ss= (const unsigned char*)sss; while(*ss){ int s= *ss++; if(needs_wrapping(s,file_offset)||s==newlinechar){ t_flush_buffer(log_file,file_offset); } if(s!=newlinechar){ buffer[i++]= s; if(file_offset++==max_print_line){ t_flush_buffer(log_file,file_offset); } } } if(*buffer){ buffer[i++]= '\0'; fputs(buffer,log_file); buffer[0]= '\0'; } i= 0; } if(doterm){ const unsigned char*ss= (const unsigned char*)sss; while(*ss){ int s= *ss++; if(needs_wrapping(s,term_offset)||s==newlinechar){ t_flush_buffer(term_out,term_offset); } if(s!=newlinechar){ if(needs_escaping(s)){ buffer[i++]= s; }else{ buffer[i++]= '^'; buffer[i++]= '^'; buffer[i++]= escaped_char(s); term_offset+= 2; } if(++term_offset==max_print_line){ t_flush_buffer(term_out,term_offset); } } } if(*buffer){ buffer[i++]= '\0'; fputs(buffer,term_out); } } free(buffer); } }
static char * escape_filename(EditLine * el, const char *filename) { size_t original_len = 0; size_t escaped_character_count = 0; size_t offset = 0; size_t newlen; const char *s; char c; size_t s_quoted = 0; /* does the input contain a single quote */ size_t d_quoted = 0; /* does the input contain a double quote */ char *escaped_str; wchar_t *temp = el->el_line.buffer; while (temp != el->el_line.cursor) { /* * If we see a single quote but have not seen a double quote so far * set/unset s_quote */ if (temp[0] == '\'' && !d_quoted) s_quoted = !s_quoted; /* * vice versa to the above condition */ else if (temp[0] == '"' && !s_quoted) d_quoted = !d_quoted; temp++; } /* Count number of special characters so that we can calculate * number of extra bytes needed in the new string */ for (s = filename; *s; s++, original_len++) { c = *s; /* Inside a single quote only single quotes need escaping */ if (s_quoted && c == '\'') { escaped_character_count += 3; continue; } /* Inside double quotes only ", \, ` and $ need escaping */ if (d_quoted && (c == '"' || c == '\\' || c == '`' || c == '$')) { escaped_character_count++; continue; } if (!s_quoted && !d_quoted && needs_escaping(c)) escaped_character_count++; } newlen = original_len + escaped_character_count + 1; if ((escaped_str = el_malloc(newlen)) == NULL) return NULL; for (s = filename; *s; s++) { c = *s; if (!needs_escaping(c)) { /* no escaping is required continue as usual */ escaped_str[offset++] = c; continue; } /* single quotes inside single quotes require special handling */ if (c == '\'' && s_quoted) { escaped_str[offset++] = '\''; escaped_str[offset++] = '\\'; escaped_str[offset++] = '\''; escaped_str[offset++] = '\''; continue; } /* Otherwise no escaping needed inside single quotes */ if (s_quoted) { escaped_str[offset++] = c; continue; } /* No escaping needed inside a double quoted string either * unless we see a '$', '\', '`', or '"' (itself) */ if (d_quoted && c != '"' && c != '$' && c != '\\' && c != '`') { escaped_str[offset++] = c; continue; } /* If we reach here that means escaping is actually needed */ escaped_str[offset++] = '\\'; escaped_str[offset++] = c; } /* close the quotes */ if (s_quoted) escaped_str[offset++] = '\''; else if (d_quoted) escaped_str[offset++] = '"'; escaped_str[offset] = 0; return escaped_str; }