aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/bindump.c
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2010-01-10 16:13:39 -0900
committerDavid M. Syzdek <david@syzdek.net>2010-01-10 16:13:39 -0900
commitfa0bc2d8d448c936ed20a7ca521cecff9128a956 (patch)
tree3cf0a6d73e41a3d24d6827a14e340a6fa5702677 /src/bindump.c
parent214f1fe82fce6c620e6833bc17f2b9312de8472a (diff)
downloaddmstools-fa0bc2d8d448c936ed20a7ca521cecff9128a956.tar.gz
dmstools-fa0bc2d8d448c936ed20a7ca521cecff9128a956.tar.bz2
dmstools-fa0bc2d8d448c936ed20a7ca521cecff9128a956.tar.xz
dmstools-fa0bc2d8d448c936ed20a7ca521cecff9128a956.zip
Adding ANSI code formatting to bindump.
Diffstat (limited to 'src/bindump.c')
-rw-r--r--src/bindump.c93
1 files changed, 72 insertions, 21 deletions
diff --git a/src/bindump.c b/src/bindump.c
index f4678e5..7ccbad9 100644
--- a/src/bindump.c
+++ b/src/bindump.c
@@ -106,6 +106,15 @@
106#define PARAMS(protos) protos 106#define PARAMS(protos) protos
107#endif 107#endif
108 108
109#define MY_TERM_RESET "\x1b[0m"
110#define MY_TERM_BOLD "\x1b[1m"
111#define MY_TERM_DIFF "\x1b[31m"
112
113#define MY_OPT_ALL 0x01
114#define MY_OPT_XTERM 0x02
115#define MY_OPT_NOXTERM 0x04
116
117
109///////////////// 118/////////////////
110// // 119// //
111// Datatypes // 120// Datatypes //
@@ -187,6 +196,7 @@ char * my_byte2str PARAMS((uint8_t data, char * buff));
187int main(int argc, char * argv[]) 196int main(int argc, char * argv[])
188{ 197{
189 int c; 198 int c;
199 int opts;
190 int opt_index; 200 int opt_index;
191 size_t offset; 201 size_t offset;
192 size_t offset_mod; 202 size_t offset_mod;
@@ -198,7 +208,7 @@ int main(int argc, char * argv[])
198 BinDumpFile file2; 208 BinDumpFile file2;
199 209
200 // getopt options 210 // getopt options
201 static char short_opt[] = "hl:o:vV"; 211 static char short_opt[] = "ahl:o:vVXx";
202 static struct option long_opt[] = 212 static struct option long_opt[] =
203 { 213 {
204 {"help", no_argument, 0, 'h'}, 214 {"help", no_argument, 0, 'h'},
@@ -208,6 +218,7 @@ int main(int argc, char * argv[])
208 }; 218 };
209 219
210 len = 0; 220 len = 0;
221 opts = 0;
211 line = 0; 222 line = 0;
212 offset = 0; 223 offset = 0;
213 offset_mod = 0; 224 offset_mod = 0;
@@ -223,6 +234,9 @@ int main(int argc, char * argv[])
223 case -1: /* no more arguments */ 234 case -1: /* no more arguments */
224 case 0: /* long options toggles */ 235 case 0: /* long options toggles */
225 break; 236 break;
237 case 'a':
238 opts |= MY_OPT_ALL;
239 break;
226 case 'h': 240 case 'h':
227 my_usage(); 241 my_usage();
228 return(0); 242 return(0);
@@ -240,6 +254,12 @@ int main(int argc, char * argv[])
240 case 'v': 254 case 'v':
241 verbose++; 255 verbose++;
242 break; 256 break;
257 case 'x':
258 opts |= MY_OPT_NOXTERM;
259 break;
260 case 'X':
261 opts |= MY_OPT_XTERM;
262 break;
243 case '?': 263 case '?':
244 fprintf(stderr, _("Try `%s --help' for more information.\n"), PROGRAM_NAME); 264 fprintf(stderr, _("Try `%s --help' for more information.\n"), PROGRAM_NAME);
245 return(1); 265 return(1);
@@ -328,7 +348,11 @@ int main(int argc, char * argv[])
328 printf("reading data...\n"); 348 printf("reading data...\n");
329 if (file2.filename) 349 if (file2.filename)
330 printf(" "); 350 printf(" ");
351 if (opts & MY_OPT_XTERM)
352 printf("%s", MY_TERM_BOLD);
331 printf("offset 00 01 02 03 04 05 06 07 \n"); 353 printf("offset 00 01 02 03 04 05 06 07 \n");
354 if (opts & MY_OPT_XTERM)
355 printf("%s", MY_TERM_RESET);
332 if (offset_mod) 356 if (offset_mod)
333 { 357 {
334 if ((my_read(&file1, offset_mod, len, verbose) == -1)) 358 if ((my_read(&file1, offset_mod, len, verbose) == -1))
@@ -336,9 +360,9 @@ int main(int argc, char * argv[])
336 if ((my_read(&file2, offset_mod, len, verbose) == -1)) 360 if ((my_read(&file2, offset_mod, len, verbose) == -1))
337 return(my_close(&file1, verbose)); 361 return(my_close(&file1, verbose));
338 if (file2.filename) 362 if (file2.filename)
339 line += my_print_diff(&file1, &file2, offset_mod, len, 0); 363 line += my_print_diff(&file1, &file2, offset_mod, len, opts);
340 else 364 else
341 line += my_print_dump(&file1, offset_mod, len, 0); 365 line += my_print_dump(&file1, offset_mod, len, opts);
342 }; 366 };
343 367
344 // read data from file handle 368 // read data from file handle
@@ -349,14 +373,18 @@ int main(int argc, char * argv[])
349 if ((my_read(&file2, 0, len, verbose) == -1)) 373 if ((my_read(&file2, 0, len, verbose) == -1))
350 return(my_close(&file1, verbose)); 374 return(my_close(&file1, verbose));
351 if (file2.filename) 375 if (file2.filename)
352 line += my_print_diff(&file1, &file2, 0, len, 0); 376 line += my_print_diff(&file1, &file2, 0, len, opts);
353 else 377 else
354 line += my_print_dump(&file1, 0, len, 0); 378 line += my_print_dump(&file1, 0, len, opts);
355 if (!(line % 22)) 379 if (!(line % 22))
356 { 380 {
357 if (file2.filename) 381 if (file2.filename)
358 printf(" "); 382 printf(" ");
383 if (opts & MY_OPT_XTERM)
384 printf("%s", MY_TERM_BOLD);
359 printf("offset 00 01 02 03 04 05 06 07 \n"); 385 printf("offset 00 01 02 03 04 05 06 07 \n");
386 if (opts & MY_OPT_XTERM)
387 printf("%s", MY_TERM_RESET);
360 }; 388 };
361 }; 389 };
362 390
@@ -463,12 +491,9 @@ size_t my_print_diff(BinDumpFile * file1, BinDumpFile * file2, size_t offset,
463 uint8_t diff2[9]; 491 uint8_t diff2[9];
464 492
465 line = 0; 493 line = 0;
466
467 if (opts)
468 return(0);
469
470 max1 = 0; 494 max1 = 0;
471 max2 = 0; 495 max2 = 0;
496
472 if (file1->code > 0) 497 if (file1->code > 0)
473 { 498 {
474 len1 = len ? (len - (file1->pos-offset)) : 8; 499 len1 = len ? (len - (file1->pos-offset)) : 8;
@@ -515,16 +540,27 @@ size_t my_print_diff(BinDumpFile * file1, BinDumpFile * file2, size_t offset,
515 // prints line offset 540 // prints line offset
516 if ((max1) && (diff1[8])) 541 if ((max1) && (diff1[8]))
517 { 542 {
543 if (opts & MY_OPT_XTERM)
544 printf("%s", MY_TERM_BOLD);
518 printf("< 0%04zo0:", (file1->pos/8)); 545 printf("< 0%04zo0:", (file1->pos/8));
546 if (opts & MY_OPT_XTERM)
547 printf("%s", MY_TERM_RESET);
548
519 for(s = 0; s < offset; s++) 549 for(s = 0; s < offset; s++)
520 printf(" "); 550 printf(" ");
521 551
522 for(s = 0; s < max1; s++) 552 for(s = 0; s < max1; s++)
523 { 553 {
524 printf(" %s", (!(diff1[s])) ? 554 if (diff1[s])
525 " " : 555 {
526 my_byte2str(file1->data[s], buff) 556 if (opts & MY_OPT_XTERM)
527 ); 557 printf("%s", MY_TERM_DIFF);
558 printf(" %s", my_byte2str(file1->data[s], buff));
559 if (opts & MY_OPT_XTERM)
560 printf("%s", MY_TERM_RESET);
561 } else {
562 printf(" %s", (opts & MY_OPT_ALL) ? my_byte2str(file1->data[s], buff) : " ");
563 };
528 }; 564 };
529 printf("\n"); 565 printf("\n");
530 566
@@ -535,16 +571,27 @@ size_t my_print_diff(BinDumpFile * file1, BinDumpFile * file2, size_t offset,
535 571
536 if ((max2) && (diff2[8])) 572 if ((max2) && (diff2[8]))
537 { 573 {
574 if (opts & MY_OPT_XTERM)
575 printf("%s", MY_TERM_BOLD);
538 printf("> 0%04zo0:", (file2->pos/8)); 576 printf("> 0%04zo0:", (file2->pos/8));
577 if (opts & MY_OPT_XTERM)
578 printf("%s", MY_TERM_RESET);
579
539 for(s = 0; s < offset; s++) 580 for(s = 0; s < offset; s++)
540 printf(" "); 581 printf(" ");
541 582
542 for(s = 0; s < max2; s++) 583 for(s = 0; s < max2; s++)
543 { 584 {
544 printf(" %s", (!(diff2[s])) ? 585 if (diff2[s])
545 " " : 586 {
546 my_byte2str(file2->data[s], buff) 587 if (opts & MY_OPT_XTERM)
547 ); 588 printf("%s", MY_TERM_DIFF);
589 printf(" %s", my_byte2str(file2->data[s], buff));
590 if (opts & MY_OPT_XTERM)
591 printf("%s", MY_TERM_RESET);
592 } else {
593 printf(" %s", (opts & MY_OPT_ALL) ? my_byte2str(file2->data[s], buff) : " ");
594 };
548 }; 595 };
549 printf("\n"); 596 printf("\n");
550 597
@@ -570,14 +617,15 @@ size_t my_print_dump(BinDumpFile * file, size_t offset, size_t len,
570 size_t s; 617 size_t s;
571 size_t max; 618 size_t max;
572 619
573 if (opts)
574 return(0);
575
576 if (file->code < 1) 620 if (file->code < 1)
577 return(0); 621 return(0);
578 622
579 // prints line offset 623 // prints line offset
580 printf("0%04zo0:", (file->pos/8)); 624 if (opts & MY_OPT_XTERM)
625 printf("%s", MY_TERM_BOLD);
626 printf("< 0%04zo0:", (file->pos/8));
627 if (opts & MY_OPT_XTERM)
628 printf("%s", MY_TERM_RESET);
581 629
582 // prints spaces 630 // prints spaces
583 for(s = 0; s < offset; s++) 631 for(s = 0; s < offset; s++)
@@ -641,11 +689,14 @@ void my_usage()
641 // line. The two strings referenced are: PROGRAM_NAME, and 689 // line. The two strings referenced are: PROGRAM_NAME, and
642 // PACKAGE_BUGREPORT 690 // PACKAGE_BUGREPORT
643 printf(_("Usage: %s [options] file\n" 691 printf(_("Usage: %s [options] file\n"
692 " -a print all data when displaying a line\n"
644 " -h, --help print this help and exit\n" 693 " -h, --help print this help and exit\n"
645 " -l bytes length of data to display\n" 694 " -l bytes length of data to display\n"
646 " -o bytes offset to start reading data\n" 695 " -o bytes offset to start reading data\n"
647 " -V, --version print verbose messages\n" 696 " -V, --version print verbose messages\n"
648 " -v, --verbose print version number and exit\n" 697 " -v, --verbose print version number and exit\n"
698 " -X enable Xterm output\n"
699 " -x disables Xterm output\n"
649 "\n" 700 "\n"
650 "Report bugs to <%s>.\n" 701 "Report bugs to <%s>.\n"
651 ), PROGRAM_NAME, PACKAGE_BUGREPORT 702 ), PROGRAM_NAME, PACKAGE_BUGREPORT