瀏覽代碼

Sanitize cell char

Björn Åström 1 年之前
父節點
當前提交
1e8a8ecfcc
共有 1 個文件被更改,包括 29 次插入4 次删除
  1. 29 4
      term.c

+ 29 - 4
term.c

@@ -130,11 +130,15 @@ static void term_param(struct term *t, char c) {
 
 static void term_execute(struct term *t, char c) {
 	switch (c) {
-		case '\n': term_line_feed(t); break;
+		case '\b': term_cursor_move_left(t, 1); break;
+
+		case '\f':
+		case '\n':
+		case '\v':
+		term_line_feed(t); break;
 
 		case '\r': t->cursor.x = 0; break;
 
-		case '\b': term_cursor_move_left(t, 1); break;
 
 		case '\t':
 			term_print(t, ' ');
@@ -371,23 +375,44 @@ void term_put_string(struct term *t, char *str) {
 	}
 }
 
+static char *xml_entity(char *str, char c) {
+	switch (c) {
+
+		case '<':
+			strncpy(str, "&#x003C;", 9);
+			break;
+		
+		case '>':
+			strncpy(str, "&#x003E;", 9);
+			break;
+
+		default:
+			str[0] = c;
+			str[1] = '\0';
+			break;
+	}
+
+	return str;
+}
+
 char *term_to_string(struct term *t)
 {
 	// TODO: don't allocate too much if pango is not used.
 	int str_size = t->num_rows * t->num_cols * 128 + t->num_rows;
 	char *ret = calloc(str_size, sizeof(char));
 	char *s = ret;
+	char cell_str[9];
 
 	for (int i = 0; i < t->num_rows; i++) {
 		for (int j = 0; j < t->num_cols; j++) {
 			struct cell cur_cell = t->rows[i].cells[j];
 			if (t->pango && 0 == cur_cell.sgra.normal) {
 				// TODO: use snprintf...
-				s += sprintf(s, "<span weight='%s' fgcolor='#%s' bgcolor='#%s'>%c</span>",
+				s += sprintf(s, "<span weight='%s' fgcolor='#%s' bgcolor='#%s'>%s</span>",
 						cur_cell.sgra.bold ? "bold" : "normal",
 						colors[cur_cell.sgra.fg],
 						colors[cur_cell.sgra.bg],
-						cur_cell.c);
+						xml_entity(cell_str, cur_cell.c));
 			} else {
 				*s++ = cur_cell.c;
 			}