aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-17 23:16:16 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-17 23:16:16 +0000
commitef44d9d9f2aa5d0333d5a7a2749e32c6be94365d (patch)
tree037c0440778c656210c64f32dcfa69e27df1cda9
parentae114c235e2bf2987fceb6065b57dee93740c593 (diff)
downloadbusybox-w32-ef44d9d9f2aa5d0333d5a7a2749e32c6be94365d.tar.gz
busybox-w32-ef44d9d9f2aa5d0333d5a7a2749e32c6be94365d.tar.bz2
busybox-w32-ef44d9d9f2aa5d0333d5a7a2749e32c6be94365d.zip
sed,get_line_from_file: improve comments
-rw-r--r--editors/sed.c44
-rw-r--r--libbb/get_line_from_file.c5
2 files changed, 30 insertions, 19 deletions
diff --git a/editors/sed.c b/editors/sed.c
index 23d73faa9..674381b56 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -576,28 +576,33 @@ static void do_subst_w_backrefs(char *line, char *replace)
576 /* go through the replacement string */ 576 /* go through the replacement string */
577 for (i = 0; replace[i]; i++) { 577 for (i = 0; replace[i]; i++) {
578 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */ 578 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
579 if (replace[i] == '\\' && replace[i+1] >= '0' && replace[i+1] <= '9') { 579 if (replace[i] == '\\') {
580 int backref = replace[++i]-'0'; 580 unsigned backref = replace[++i] - '0';
581 581 if (backref <= 9) {
582 /* print out the text held in bbg.regmatch[backref] */ 582 /* print out the text held in bbg.regmatch[backref] */
583 if (bbg.regmatch[backref].rm_so != -1) { 583 if (bbg.regmatch[backref].rm_so != -1) {
584 j = bbg.regmatch[backref].rm_so; 584 j = bbg.regmatch[backref].rm_so;
585 while (j < bbg.regmatch[backref].rm_eo) 585 while (j < bbg.regmatch[backref].rm_eo)
586 pipe_putc(line[j++]); 586 pipe_putc(line[j++]);
587 }
588 continue;
587 } 589 }
590 /* I _think_ it is impossible to get '\' to be
591 * the last char in replace string. Thus we dont check
592 * for replace[i] == NUL. (counterexample anyone?) */
593 /* if we find a backslash escaped character, print the character */
594 pipe_putc(replace[i]);
595 continue;
588 } 596 }
589
590 /* if we find a backslash escaped character, print the character */
591 else if (replace[i] == '\\') pipe_putc(replace[++i]);
592
593 /* if we find an unescaped '&' print out the whole matched text. */ 597 /* if we find an unescaped '&' print out the whole matched text. */
594 else if (replace[i] == '&') { 598 if (replace[i] == '&') {
595 j = bbg.regmatch[0].rm_so; 599 j = bbg.regmatch[0].rm_so;
596 while (j < bbg.regmatch[0].rm_eo) 600 while (j < bbg.regmatch[0].rm_eo)
597 pipe_putc(line[j++]); 601 pipe_putc(line[j++]);
602 continue;
598 } 603 }
599 /* Otherwise just output the character. */ 604 /* Otherwise just output the character. */
600 else pipe_putc(replace[i]); 605 pipe_putc(replace[i]);
601 } 606 }
602} 607}
603 608
@@ -722,6 +727,9 @@ static char *get_next_line(int *last_char)
722 lc = 0; 727 lc = 0;
723 flush_append(); 728 flush_append();
724 while (bbg.current_input_file < bbg.input_file_count) { 729 while (bbg.current_input_file < bbg.input_file_count) {
730 /* Read line up to a newline or NUL byte, inclusive,
731 * return malloc'ed char[]. length of the chunk read
732 * is stored in len. NULL if EOF/error */
725 temp = bb_get_chunk_from_file( 733 temp = bb_get_chunk_from_file(
726 bbg.input_file_list[bbg.current_input_file], &len); 734 bbg.input_file_list[bbg.current_input_file], &len);
727 if (temp) { 735 if (temp) {
@@ -753,7 +761,8 @@ static char *get_next_line(int *last_char)
753 * echo -n thingy >z1 761 * echo -n thingy >z1
754 * echo -n again >z2 762 * echo -n again >z2
755 * >znull 763 * >znull
756 * sed "s/i/z/" z1 z2 znull | hexdump -vC output: 764 * sed "s/i/z/" z1 z2 znull | hexdump -vC
765 * output:
757 * gnu sed 4.1.5: 766 * gnu sed 4.1.5:
758 * 00000000 74 68 7a 6e 67 79 0a 61 67 61 7a 6e |thzngy.agazn| 767 * 00000000 74 68 7a 6e 67 79 0a 61 67 61 7a 6e |thzngy.agazn|
759 * bbox: 768 * bbox:
@@ -771,8 +780,9 @@ static int puts_maybe_newline(char *s, FILE *file, int prev_last_char, int last_
771 last_puts_char = '\n'; 780 last_puts_char = '\n';
772 } 781 }
773 fputs(s, file); 782 fputs(s, file);
774 /* 'x': we don't care what is it, but we know it isn't '\n' */ 783 /* why 'x'? - just something which is not '\n' */
775 if (s[0]) last_puts_char = 'x'; 784 if (s[0])
785 last_puts_char = 'x';
776 if (!(last_char & 0x100)) { /* had trailing '\n' or '\0'? */ 786 if (!(last_char & 0x100)) { /* had trailing '\n' or '\0'? */
777 last_char &= 0xff; 787 last_char &= 0xff;
778 fputc(last_char, file); 788 fputc(last_char, file);
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c
index 5078f49ea..2c9608e9e 100644
--- a/libbb/get_line_from_file.c
+++ b/libbb/get_line_from_file.c
@@ -13,8 +13,9 @@
13 13
14/* This function reads an entire line from a text file, up to a newline 14/* This function reads an entire line from a text file, up to a newline
15 * or NUL byte, inclusive. It returns a malloc'ed char * which must be 15 * or NUL byte, inclusive. It returns a malloc'ed char * which must be
16 * stored and free'ed by the caller. If end is null '\n' isn't considered 16 * stored and free'ed by the caller. If end is NULL '\n' isn't considered
17 * end of line. If end isn't null, length of the chunk read is stored in it. */ 17 * end of line. If end isn't NULL, length of the chunk read is stored in it.
18 * Return NULL if EOF/error */
18 19
19char *bb_get_chunk_from_file(FILE * file, int *end) 20char *bb_get_chunk_from_file(FILE * file, int *end)
20{ 21{