0 | 0 |
/*
|
1 | 1 |
* ansilove.c
|
2 | |
* Ansilove 4.0.2
|
|
2 |
* Ansilove 4.0.3
|
3 | 3 |
* https://www.ansilove.org
|
4 | 4 |
*
|
5 | 5 |
* Copyright (c) 2011-2019 Stefan Vogt, Brian Cassidy, and Frederic Cambus
|
|
34 | 34 |
|
35 | 35 |
/* prototypes */
|
36 | 36 |
static void synopsis(void);
|
37 | |
|
38 | |
/* following the IEEE Std 1003.1 for utility conventions */
|
|
37 |
static void version(void);
|
|
38 |
|
39 | 39 |
static void
|
40 | 40 |
synopsis(void)
|
41 | 41 |
{
|
42 | |
fprintf(stderr, "\nSYNOPSIS\n"
|
43 | |
" ansilove [-dhirsv] [-b bits] [-c columns] [-f font] [-m mode]"
|
44 | |
" [-o file]\n"
|
45 | |
" [-R factor] file\n\n");
|
|
42 |
fprintf(stdout, "SYNOPSIS\n"
|
|
43 |
" ansilove [-dhiqrsv] [-b bits] [-c columns] [-f font]"
|
|
44 |
" [-m mode] [-o file]\n"
|
|
45 |
" [-R factor] file\n");
|
|
46 |
}
|
|
47 |
|
|
48 |
static void
|
|
49 |
version(void)
|
|
50 |
{
|
|
51 |
fprintf(stdout, "AnsiLove/C %s - ANSI / ASCII art to PNG converter\n"
|
|
52 |
"Copyright (c) 2011-2019 Stefan Vogt, Brian Cassidy, and "
|
|
53 |
"Frederic Cambus.\n", VERSION);
|
46 | 54 |
}
|
47 | 55 |
|
48 | 56 |
int
|
49 | 57 |
main(int argc, char *argv[])
|
50 | 58 |
{
|
51 | |
fprintf(stderr, "AnsiLove/C %s - ANSI / ASCII art to PNG converter\n"
|
52 | |
"Copyright (c) 2011-2019 Stefan Vogt, Brian Cassidy, and "
|
53 | |
"Frederic Cambus.\n", VERSION);
|
|
59 |
FILE *messages = NULL;
|
54 | 60 |
|
55 | 61 |
/* SAUCE record related bool types */
|
56 | 62 |
bool justDisplaySAUCE = false;
|
|
73 | 79 |
|
74 | 80 |
const char *errstr;
|
75 | 81 |
|
76 | |
if (ansilove_init(&ctx, &options) == -1) {
|
77 | |
fprintf(stderr, "\n%s\n", ansilove_error(&ctx));
|
78 | |
return EXIT_FAILURE;
|
79 | |
}
|
80 | |
|
81 | |
if (pledge("stdio cpath rpath wpath", NULL) == -1) {
|
|
82 |
if (ansilove_init(&ctx, &options) == -1)
|
|
83 |
errx(EXIT_FAILURE, "%s", ansilove_error(&ctx));
|
|
84 |
|
|
85 |
if (pledge("stdio cpath rpath wpath", NULL) == -1)
|
82 | 86 |
err(EXIT_FAILURE, "pledge");
|
83 | |
}
|
84 | |
|
85 | |
while ((getoptFlag = getopt(argc, argv, "b:c:df:him:o:rR:sv")) != -1) {
|
|
87 |
|
|
88 |
while ((getoptFlag = getopt(argc, argv, "b:c:df:him:o:qrR:sv")) != -1) {
|
86 | 89 |
switch (getoptFlag) {
|
87 | 90 |
case 'b':
|
88 | 91 |
/* convert numeric command line flags to integer values */
|
89 | 92 |
options.bits = strtonum(optarg, 8, 9, &errstr);
|
90 | 93 |
|
91 | |
if (errstr) {
|
92 | |
fprintf(stderr, "\nInvalid value for bits (must be 8 or 9).\n\n");
|
93 | |
return EXIT_FAILURE;
|
94 | |
}
|
|
94 |
if (errstr)
|
|
95 |
errx(EXIT_FAILURE, "Invalid value for bits (must be 8 or 9).");
|
95 | 96 |
|
96 | 97 |
break;
|
97 | 98 |
case 'c':
|
98 | 99 |
/* convert numeric command line flags to integer values */
|
99 | 100 |
options.columns = strtonum(optarg, 1, 4096, &errstr);
|
100 | 101 |
|
101 | |
if (errstr) {
|
102 | |
fprintf(stderr, "\nInvalid value for columns (must range from 1 to 4096).\n\n");
|
103 | |
return EXIT_FAILURE;
|
104 | |
}
|
|
102 |
if (errstr)
|
|
103 |
errx(EXIT_FAILURE, "\nInvalid value for columns (must range from 1 to 4096).");
|
105 | 104 |
|
106 | 105 |
break;
|
107 | 106 |
case 'd':
|
|
134 | 133 |
case 'o':
|
135 | 134 |
output = optarg;
|
136 | 135 |
break;
|
|
136 |
case 'q':
|
|
137 |
messages = fopen("/dev/null", "w");
|
|
138 |
break;
|
137 | 139 |
case 'r':
|
138 | 140 |
options.scale_factor = 2;
|
139 | 141 |
break;
|
|
141 | 143 |
/* convert numeric command line flags to integer values */
|
142 | 144 |
options.scale_factor = strtonum(optarg, 2, 8, &errstr);
|
143 | 145 |
|
144 | |
if (errstr) {
|
145 | |
fprintf(stderr, "\nInvalid value for retina scale factor (must range from 2 to 8).\n\n");
|
146 | |
return EXIT_FAILURE;
|
147 | |
}
|
|
146 |
if (errstr)
|
|
147 |
errx(EXIT_FAILURE, "Invalid value for retina scale factor (must range from 2 to 8).");
|
148 | 148 |
|
149 | 149 |
break;
|
150 | 150 |
case 's':
|
151 | 151 |
justDisplaySAUCE = true;
|
152 | 152 |
break;
|
153 | 153 |
case 'v':
|
|
154 |
version();
|
154 | 155 |
return EXIT_SUCCESS;
|
155 | 156 |
}
|
156 | 157 |
}
|
|
165 | 166 |
argc -= optind;
|
166 | 167 |
argv += optind;
|
167 | 168 |
|
|
169 |
/* if -q flag was not set, default to stdout */
|
|
170 |
if (!messages)
|
|
171 |
messages = stdout;
|
|
172 |
|
168 | 173 |
/* let's check the file for a valid SAUCE record */
|
169 | 174 |
sauce *record = sauceReadFileName(input);
|
170 | 175 |
|
171 | 176 |
/* record == NULL also means there is no file, we can stop here */
|
172 | 177 |
if (record == NULL) {
|
173 | |
fprintf(stderr, "\nFile %s not found.\n\n", input);
|
174 | |
return EXIT_FAILURE;
|
|
178 |
errx(EXIT_FAILURE, "File %s not found.", input);
|
175 | 179 |
} else {
|
176 | 180 |
/* if we find a SAUCE record, update bool flag */
|
177 | 181 |
if (!strcmp(record->ID, SAUCE_ID)) {
|
|
183 | 187 |
/* create output file name if output is not specified */
|
184 | 188 |
if (!output) {
|
185 | 189 |
/* appending ".png" extension to output file name */
|
186 | |
if (asprintf(&fileName, "%s%s", input, ".png") == -1) {
|
187 | |
fprintf(stderr, "Memory allocation error.\n\n");
|
188 | |
return EXIT_FAILURE;
|
189 | |
}
|
|
190 |
if (asprintf(&fileName, "%s%s", input, ".png") == -1)
|
|
191 |
errx(EXIT_FAILURE, "Memory allocation error.");
|
190 | 192 |
} else {
|
191 | 193 |
fileName = output;
|
192 | 194 |
}
|
193 | 195 |
|
194 | 196 |
/* display name of input and output files */
|
195 | |
fprintf(stderr, "\nInput File: %s\n", input);
|
196 | |
fprintf(stderr, "Output File: %s\n", fileName);
|
|
197 |
fprintf(messages, "Input File: %s\n", input);
|
|
198 |
fprintf(messages, "Output File: %s\n", fileName);
|
197 | 199 |
|
198 | 200 |
/* get file extension */
|
199 | 201 |
char *fext = strrchr(input, '.');
|
|
203 | 205 |
if (!strcmp(fext, "diz"))
|
204 | 206 |
options.diz = true;
|
205 | 207 |
|
206 | |
if (ansilove_loadfile(&ctx, input) == -1) {
|
207 | |
fprintf(stderr, "\n%s\n", ansilove_error(&ctx));
|
208 | |
return EXIT_FAILURE;
|
209 | |
}
|
|
208 |
if (ansilove_loadfile(&ctx, input) == -1)
|
|
209 |
errx(EXIT_FAILURE, "%s", ansilove_error(&ctx));
|
210 | 210 |
|
211 | 211 |
/* adjust the file size if file contains a SAUCE record */
|
212 | 212 |
if (fileHasSAUCE) {
|
|
237 | 237 |
fileIsANSi = true;
|
238 | 238 |
}
|
239 | 239 |
|
240 | |
if (loader(&ctx, &options) == -1) {
|
241 | |
fprintf(stderr, "\n%s\n", ansilove_error(&ctx));
|
242 | |
return EXIT_FAILURE;
|
243 | |
}
|
|
240 |
if (loader(&ctx, &options) == -1)
|
|
241 |
errx(EXIT_FAILURE, "%s", ansilove_error(&ctx));
|
244 | 242 |
|
245 | 243 |
/* create the output file */
|
246 | |
if (ansilove_savefile(&ctx, fileName) == -1) {
|
247 | |
fprintf(stderr, "\n%s\n", ansilove_error(&ctx));
|
248 | |
return EXIT_FAILURE;
|
249 | |
}
|
|
244 |
if (ansilove_savefile(&ctx, fileName) == -1)
|
|
245 |
errx(EXIT_FAILURE, "%s", ansilove_error(&ctx));
|
250 | 246 |
|
251 | 247 |
/* gather information and report to the command line */
|
252 | 248 |
if (fileIsANSi || fileIsBinary ||
|
253 | 249 |
fileIsPCBoard || fileIsTundra) {
|
254 | |
fprintf(stderr, "Font: %s\n", font ? font : "80x25");
|
255 | |
|
256 | |
fprintf(stderr, "Bits: %d\n", options.bits);
|
|
250 |
fprintf(messages, "Font: %s\n", font ? font : "80x25");
|
|
251 |
|
|
252 |
fprintf(messages, "Bits: %d\n", options.bits);
|
257 | 253 |
}
|
258 | 254 |
|
259 | 255 |
if (options.icecolors && (fileIsANSi || fileIsBinary))
|
260 | |
fprintf(stderr, "iCE Colors: enabled\n");
|
|
256 |
fprintf(messages, "iCE Colors: enabled\n");
|
261 | 257 |
|
262 | 258 |
if (fileIsANSi || fileIsBinary || fileIsTundra)
|
263 | |
fprintf(stderr, "Columns: %d\n", options.columns);
|
|
259 |
fprintf(messages, "Columns: %d\n", options.columns);
|
264 | 260 |
|
265 | 261 |
if (options.scale_factor)
|
266 | |
fprintf(stderr, "Scale factor: %d\n", options.scale_factor);
|
|
262 |
fprintf(messages, "Scale factor: %d\n", options.scale_factor);
|
267 | 263 |
}
|
268 | 264 |
|
269 | 265 |
/* either display SAUCE or tell us if there is no record */
|
270 | 266 |
if (!fileHasSAUCE) {
|
271 | |
fprintf(stderr, "\nFile %s does not have a SAUCE record.\n", input);
|
|
267 |
fprintf(messages, "\nFile %s does not have a SAUCE record.\n", input);
|
272 | 268 |
} else {
|
273 | |
fprintf(stderr, "\nId: %s v%s\n", record->ID, record->version);
|
274 | |
fprintf(stderr, "Title: %s\n", record->title);
|
275 | |
fprintf(stderr, "Author: %s\n", record->author);
|
276 | |
fprintf(stderr, "Group: %s\n", record->group);
|
277 | |
fprintf(stderr, "Date: %s\n", record->date);
|
278 | |
fprintf(stderr, "Datatype: %d\n", record->dataType);
|
279 | |
fprintf(stderr, "Filetype: %d\n", record->fileType);
|
|
269 |
fprintf(messages, "\nId: %s v%s\n", record->ID, record->version);
|
|
270 |
fprintf(messages, "Title: %s\n", record->title);
|
|
271 |
fprintf(messages, "Author: %s\n", record->author);
|
|
272 |
fprintf(messages, "Group: %s\n", record->group);
|
|
273 |
fprintf(messages, "Date: %s\n", record->date);
|
|
274 |
fprintf(messages, "Datatype: %d\n", record->dataType);
|
|
275 |
fprintf(messages, "Filetype: %d\n", record->fileType);
|
280 | 276 |
if (record->flags) {
|
281 | |
fprintf(stderr, "Flags: %d\n", record->flags);
|
|
277 |
fprintf(messages, "Flags: %d\n", record->flags);
|
282 | 278 |
}
|
283 | 279 |
if (record->tinfo1) {
|
284 | |
fprintf(stderr, "Tinfo1: %d\n", record->tinfo1);
|
|
280 |
fprintf(messages, "Tinfo1: %d\n", record->tinfo1);
|
285 | 281 |
}
|
286 | 282 |
if (record->tinfo2) {
|
287 | |
fprintf(stderr, "Tinfo2: %d\n", record->tinfo2);
|
|
283 |
fprintf(messages, "Tinfo2: %d\n", record->tinfo2);
|
288 | 284 |
}
|
289 | 285 |
if (record->tinfo3) {
|
290 | |
fprintf(stderr, "Tinfo3: %d\n", record->tinfo3);
|
|
286 |
fprintf(messages, "Tinfo3: %d\n", record->tinfo3);
|
291 | 287 |
}
|
292 | 288 |
if (record->tinfo4) {
|
293 | |
fprintf(stderr, "Tinfo4: %d\n", record->tinfo4);
|
294 | |
}
|
295 | |
fprintf(stderr, "Tinfos: %s\n", record->tinfos);
|
|
289 |
fprintf(messages, "Tinfo4: %d\n", record->tinfo4);
|
|
290 |
}
|
|
291 |
fprintf(messages, "Tinfos: %s\n", record->tinfos);
|
296 | 292 |
if (record->comments > 0) {
|
297 | |
fprintf(stderr, "Comments: ");
|
|
293 |
fprintf(messages, "Comments: ");
|
298 | 294 |
for (int32_t i = 0; i < record->comments; i++) {
|
299 | |
fprintf(stderr, "%s\n", record->comment_lines[i]);
|
|
295 |
fprintf(messages, "%s\n", record->comment_lines[i]);
|
300 | 296 |
}
|
301 | 297 |
}
|
302 | 298 |
}
|