aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--src/widget-encodings.c69
1 files changed, 57 insertions, 12 deletions
diff --git a/src/widget-encodings.c b/src/widget-encodings.c
index eafb264..acc32b3 100644
--- a/src/widget-encodings.c
+++ b/src/widget-encodings.c
@@ -91,6 +91,8 @@ struct bindle_state
91 size_t res_size; 91 size_t res_size;
92 size_t buff_size; 92 size_t buff_size;
93 size_t buff_len; 93 size_t buff_len;
94 size_t line_wrap;
95 size_t line_off;
94 char * buff; 96 char * buff;
95 char * res; 97 char * res;
96 int fdin; 98 int fdin;
@@ -115,6 +117,7 @@ const char * const bindle_widget_encodings_options[] =
115 " -o file, --output=file use output file instead of stdout", 117 " -o file, --output=file use output file instead of stdout",
116 " -p, --nopadding disable padding", 118 " -p, --nopadding disable padding",
117 " -s string string to encode or decode", 119 " -s string string to encode or decode",
120 " -w cols, --wrap=cols wrap encoded lines to cols",
118 NULL 121 NULL
119}; 122};
120 123
@@ -280,27 +283,30 @@ bindle_widget_encodings(
280 int c; 283 int c;
281 int rc; 284 int rc;
282 int opt_index; 285 int opt_index;
286 char * endptr;
283 const char * str; 287 const char * str;
284 288
285 // getopt options 289 // getopt options
286 static const char * short_opt = BINDLE_COMMON_SHORT "dei:no:ps:"; 290 static const char * short_opt = BINDLE_COMMON_SHORT "b:dei:no:ps:w:";
287 static struct option long_opt[] = 291 static struct option long_opt[] =
288 { 292 {
289 BINDLE_COMMON_LONG, 293 BINDLE_COMMON_LONG,
294 { "break", no_argument, NULL, 'b' },
290 { "decode", no_argument, NULL, 'd' }, 295 { "decode", no_argument, NULL, 'd' },
291 { "encode", no_argument, NULL, 'e' }, 296 { "encode", no_argument, NULL, 'e' },
292 { "input", optional_argument, NULL, 'i' }, 297 { "input", optional_argument, NULL, 'i' },
293 { "newline", no_argument, NULL, 'n' }, 298 { "newline", no_argument, NULL, 'n' },
294 { "nopadding", no_argument, NULL, 'p' }, 299 { "nopadding", no_argument, NULL, 'p' },
295 { "output", optional_argument, NULL, 'o' }, 300 { "output", optional_argument, NULL, 'o' },
301 { "wrap", optional_argument, NULL, 'w' },
296 { NULL, 0, NULL, 0 } 302 { NULL, 0, NULL, 0 }
297 }; 303 };
298 304
299 optind = 1; 305 optind = 1;
300 opt_index = 0; 306 opt_index = 0;
301 str = NULL; 307 str = NULL;
302 cnf->state->fdin = STDIN_FILENO; 308 cnf->state->fdin = STDIN_FILENO;
303 cnf->state->fdout = STDOUT_FILENO; 309 cnf->state->fdout = STDOUT_FILENO;
304 310
305 while((c = bindle_getopt(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1) 311 while((c = bindle_getopt(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1)
306 { 312 {
@@ -366,7 +372,16 @@ bindle_widget_encodings(
366 str = optarg; 372 str = optarg;
367 break; 373 break;
368 374
369 case 'u': 375 case 'b':
376 case 'w':
377 cnf->widget_flags |= BINDLE_FLG_NEWLINE;
378 cnf->state->line_wrap = strtoul(optarg, &endptr, 0);
379 if ((endptr == optarg) || (endptr[0] != '\0'))
380 {
381 fprintf(stderr, "%s: invalid value for option `-%c'\n", PROGRAM_NAME, c);
382 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
383 return(1);
384 };
370 break; 385 break;
371 386
372 case '?': 387 case '?':
@@ -462,17 +477,48 @@ bindle_encodings_action_encode(
462 int method ) 477 int method )
463{ 478{
464 ssize_t len; 479 ssize_t len;
480 ssize_t chunk_len;
481 ssize_t buff_off;
465 bindle_state_t * state; 482 bindle_state_t * state;
466 483
467 state = cnf->state; 484 state = cnf->state;
468 485
469 if ((len = bindle_encode(method, state->res, state->res_size, state->buff, state->buff_len, (cnf->widget_flags & BINDLE_FLG_NOPAD))) == -1) 486 if ((len = bindle_encode(method, state->res, state->res_size, state->buff, state->buff_len, (cnf->widget_flags & BINDLE_FLG_NOPAD))) < 0)
470 { 487 {
471 fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno)); 488 fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno));
472 return(-1); 489 return(-1);
473 }; 490 };
474 if (len > 0) 491 state->buff_len = 0;
475 write(cnf->state->fdout, cnf->state->res, (size_t)len); 492
493 if (!(state->line_wrap))
494 {
495 if (write(cnf->state->fdout, state->res, (size_t)len) == -1)
496 {
497 fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno));
498 return(-1);
499 };
500 return(0);
501 };
502
503 buff_off = 0;
504 while(buff_off < len)
505 {
506 chunk_len = (ssize_t)(state->line_wrap - state->line_off);
507 chunk_len = (len < (ssize_t)chunk_len) ? len : chunk_len;
508 chunk_len = (chunk_len > (len-buff_off)) ? (len-buff_off) : chunk_len;
509 if (write(cnf->state->fdout, &cnf->state->res[buff_off], (size_t)chunk_len) == -1)
510 {
511 fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno));
512 return(-1);
513 };
514 buff_off += chunk_len;
515 state->line_off += (size_t)chunk_len;
516 if (state->line_off == state->line_wrap)
517 {
518 state->line_off = 0;
519 write(cnf->state->fdout, "\n", strlen("\n"));
520 };
521 };
476 522
477 return(0); 523 return(0);
478} 524}
@@ -486,12 +532,11 @@ bindle_encodings_src_fileno(
486 ssize_t len; 532 ssize_t len;
487 bindle_state_t * state; 533 bindle_state_t * state;
488 char * buff; 534 char * buff;
489 char * res;
490 535
491 state = cnf->state; 536 state = cnf->state;
492 state->buff_len = 0; 537 state->buff_len = 0;
538 state->line_off = 0;
493 buff = state->buff; 539 buff = state->buff;
494 res = state->res;
495 540
496 while ((len = read(cnf->state->fdin, &buff[state->buff_len], (state->buff_size-state->buff_len))) > 0) 541 while ((len = read(cnf->state->fdin, &buff[state->buff_len], (state->buff_size-state->buff_len))) > 0)
497 { 542 {