1 /*
2  * Copyright (c) 2016,2017,2019 Daichi GOTO
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #define VERSION "20170911"
29 #define CMDNAME "embed_ssv1txt"
30 #define ALIAS "ssv1txt"
31 
32 #include <fcntl.h>
33 
34 #include "ttt.h"
35 
36 struct swaprules {
37 	int r;
38 	char *r_arg;
39 	struct swaprules *next;
40 };
41 
42 #define TGT_GYO_PROCESS(GYO_BUFFER,NF) \
43 	if (FLAG_m) \
44 		m = max; \
45 	for (int j = 0; j < tsize; j++) { \
46 		match = 0; \
47 		if (!FLAG_m || (FLAG_m && m > 0)) { \
48 			for (int i = 1; i <= R_ARGC; i++) { \
49 				len = strlen(R_ARGV_ARG1[i]); \
50 				if (0 == strncmp(R_ARGV_ARG1[i], \
51 					tbuf + j, len)) { \
52 					if (R_ARGV[i] <= NF) { \
53 						PRINTOUT(GYO_BUFFER) \
54 					} \
55 					if (FLAG_m) \
56 						--m; \
57 					j += len - 1; \
58 					match = 1; \
59 					break; \
60 				} \
61 			} \
62 		} \
63 		if (!match) \
64 			putchar(*(tbuf + j)); \
65 	}
66 
67 #define PRINTOUT(GYO_BUFFER) \
68 	if (!FLAG_b) { \
69 	 	printf("%s", _ssvstr2str(GYO_BUFFER[R_ARGV[i]])); \
70 	} \
71 	else { \
72 		bbuf_p1 = GYO_BUFFER[R_ARGV[i]]; \
73 		bbuf = calloc(1 + strlen(bbuf_p1), sizeof(char)); \
74 		bbuf_p2 = bbuf; \
75 		while ('\0' != *bbuf_p1) { \
76 			switch (*bbuf_p1) { \
77 			case '\\': \
78 				++bbuf_p1; \
79 				switch (*bbuf_p1) { \
80 				case '\\': \
81 					*bbuf_p2 = '\\'; \
82 					++bbuf_p1; \
83 					++bbuf_p2; \
84 					break; \
85 				case 'n': \
86 					*bbuf_p2 = '\n'; \
87 					++bbuf_p1; \
88 					++bbuf_p2; \
89 					break; \
90 				case 'r': \
91 					++bbuf_p1; \
92 					break; \
93 				default: \
94 					*bbuf_p2 = '\\'; \
95 					++bbuf_p2; \
96 					*bbuf_p2 = *bbuf_p1; \
97 					++bbuf_p1; \
98 					++bbuf_p2; \
99 					break; \
100 				} \
101 				break; \
102 			case '_': \
103 				*bbuf_p2 = ' '; \
104 				++bbuf_p1; \
105 				++bbuf_p2; \
106 				break; \
107 			default: \
108 				*bbuf_p2 = *bbuf_p1; \
109 				++bbuf_p1; \
110 				++bbuf_p2; \
111 				break; \
112 			} \
113 		} \
114 	 	printf("%s", _ssvstr2str(bbuf)); \
115 		free(bbuf); \
116 	}
117