aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-04-09 01:43:54 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-04-09 01:43:54 +0000
commitbd9b32bc0df1039ef1962b558ba258e4bdb1bbfd (patch)
treea660c7d1173444d2d91890b76758daafc04148a9
parent8d6395d41a4784352cbba5b61e73bfc8afec3f31 (diff)
downloadbusybox-w32-bd9b32bc0df1039ef1962b558ba258e4bdb1bbfd.tar.gz
busybox-w32-bd9b32bc0df1039ef1962b558ba258e4bdb1bbfd.tar.bz2
busybox-w32-bd9b32bc0df1039ef1962b558ba258e4bdb1bbfd.zip
Label ends at a newline, update comments, rename linked list field
-rw-r--r--editors/sed.c73
1 files changed, 37 insertions, 36 deletions
diff --git a/editors/sed.c b/editors/sed.c
index a616c992c..db3171879 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -4,6 +4,7 @@
4 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley 4 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
5 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org> 5 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
6 * Copyright (C) 2002 Matt Kraai 6 * Copyright (C) 2002 Matt Kraai
7 * Copyright (C) 2003 by Glenn McGrath <bug1@optushome.com.au>
7 * 8 *
8 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -31,6 +32,9 @@
31 - file commands: (r)ead 32 - file commands: (r)ead
32 - backreferences in substitution expressions (\1, \2...\9) 33 - backreferences in substitution expressions (\1, \2...\9)
33 - grouped commands: {cmd1;cmd2} 34 - grouped commands: {cmd1;cmd2}
35 - transliteration (y/source-chars/dest-chars/)
36 - pattern space hold space storing / swapping (g, h, x)
37 - labels / branching (: label, b, t)
34 38
35 (Note: Specifying an address (range) to match is *optional*; commands 39 (Note: Specifying an address (range) to match is *optional*; commands
36 default to the whole pattern space if no specific address match was 40 default to the whole pattern space if no specific address match was
@@ -38,11 +42,13 @@
38 42
39 Unsupported features: 43 Unsupported features:
40 44
41 - transliteration (y/source-chars/dest-chars/) (use 'tr') 45 - GNU extensions
42 - no pattern space hold space storing / swapping (x, etc.)
43 - no labels / branching (: label, b, t, and friends)
44 - and lots, lots more. 46 - and lots, lots more.
45 47
48 Bugs:
49
50 - Cant subst globally using ^ or $ in regex, eg. "aah" | sed 's/^a/b/g'
51
46 Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html 52 Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
47*/ 53*/
48 54
@@ -55,19 +61,6 @@
55#include <stdlib.h> 61#include <stdlib.h>
56#include "busybox.h" 62#include "busybox.h"
57 63
58/* the spec says label must be at least 8 chars, behavious is unspecified if more than 8 chars */
59#define SED_LABEL_LENGTH 8
60
61/* externs */
62extern void xregcomp(regex_t * preg, const char *regex, int cflags);
63extern int optind; /* in unistd.h */
64extern char *optarg; /* ditto */
65
66/* options */
67static int be_quiet = 0;
68static const char bad_format_in_subst[] =
69 "bad format in substitution expression";
70
71typedef struct sed_cmd_s { 64typedef struct sed_cmd_s {
72 /* Order by alignment requirements */ 65 /* Order by alignment requirements */
73 66
@@ -109,14 +102,25 @@ typedef struct sed_cmd_s {
109 int invert; /* the '!' after the address */ 102 int invert; /* the '!' after the address */
110 103
111 /* Branch commands */ 104 /* Branch commands */
112 char label[SED_LABEL_LENGTH + 1]; 105 char *label;
113 106
114 /* next command in list (sequential list of specified commands) */ 107 /* next command in list (sequential list of specified commands) */
115 struct sed_cmd_s *linear; 108 struct sed_cmd_s *next;
116 109
117} sed_cmd_t; 110} sed_cmd_t;
118 111
112
113/* externs */
114extern void xregcomp(regex_t * preg, const char *regex, int cflags);
115extern int optind; /* in unistd.h */
116extern char *optarg; /* ditto */
117
119/* globals */ 118/* globals */
119/* options */
120static int be_quiet = 0;
121static const char bad_format_in_subst[] =
122 "bad format in substitution expression";
123
120/* linked list of sed commands */ 124/* linked list of sed commands */
121static sed_cmd_t sed_cmd_head; 125static sed_cmd_t sed_cmd_head;
122static sed_cmd_t *sed_cmd_tail = &sed_cmd_head; 126static sed_cmd_t *sed_cmd_tail = &sed_cmd_head;
@@ -126,13 +130,14 @@ static int in_block = 0;
126const char *const semicolon_whitespace = "; \n\r\t\v\0"; 130const char *const semicolon_whitespace = "; \n\r\t\v\0";
127static regex_t *previous_regex_ptr = NULL; 131static regex_t *previous_regex_ptr = NULL;
128 132
133
129#ifdef CONFIG_FEATURE_CLEAN_UP 134#ifdef CONFIG_FEATURE_CLEAN_UP
130static void destroy_cmd_strs(void) 135static void destroy_cmd_strs(void)
131{ 136{
132 sed_cmd_t *sed_cmd = sed_cmd_head.linear; 137 sed_cmd_t *sed_cmd = sed_cmd_head.next;
133 138
134 while (sed_cmd) { 139 while (sed_cmd) {
135 sed_cmd_t *sed_cmd_next = sed_cmd->linear; 140 sed_cmd_t *sed_cmd_next = sed_cmd->next;
136 141
137 if (sed_cmd->beg_match) { 142 if (sed_cmd->beg_match) {
138 regfree(sed_cmd->beg_match); 143 regfree(sed_cmd->beg_match);
@@ -445,7 +450,7 @@ static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr)
445/* 450/*
446 * Process the commands arguments 451 * Process the commands arguments
447 */ 452 */
448static char *parse_cmd_str(sed_cmd_t * const sed_cmd, char *cmdstr) 453static char *parse_cmd_str(sed_cmd_t *sed_cmd, char *cmdstr)
449{ 454{
450 /* handle (s)ubstitution command */ 455 /* handle (s)ubstitution command */
451 if (sed_cmd->cmd == 's') { 456 if (sed_cmd->cmd == 's') {
@@ -469,11 +474,8 @@ static char *parse_cmd_str(sed_cmd_t * const sed_cmd, char *cmdstr)
469 int length; 474 int length;
470 475
471 cmdstr += strspn(cmdstr, " "); 476 cmdstr += strspn(cmdstr, " ");
472 length = strcspn(cmdstr, "; "); 477 length = strcspn(cmdstr, "; \n");
473 if (length > SED_LABEL_LENGTH) { 478 sed_cmd->label = strndup(cmdstr, length);
474 length = SED_LABEL_LENGTH;
475 }
476 strncpy(sed_cmd->label, cmdstr, length);
477 cmdstr += length; 479 cmdstr += length;
478 } 480 }
479 /* translation command */ 481 /* translation command */
@@ -595,8 +597,8 @@ static char *add_cmd(sed_cmd_t * sed_cmd, char *cmdstr)
595 cmdstr = parse_cmd_str(sed_cmd, cmdstr); 597 cmdstr = parse_cmd_str(sed_cmd, cmdstr);
596 598
597 /* Add the command to the command array */ 599 /* Add the command to the command array */
598 sed_cmd_tail->linear = sed_cmd; 600 sed_cmd_tail->next = sed_cmd;
599 sed_cmd_tail = sed_cmd_tail->linear; 601 sed_cmd_tail = sed_cmd_tail->next;
600 602
601 return (cmdstr); 603 return (cmdstr);
602} 604}
@@ -805,9 +807,8 @@ static int do_subst_command(sed_cmd_t * sed_cmd, char **line)
805static sed_cmd_t *branch_to(const char *label) 807static sed_cmd_t *branch_to(const char *label)
806{ 808{
807 sed_cmd_t *sed_cmd; 809 sed_cmd_t *sed_cmd;
808 810 for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
809 for (sed_cmd = sed_cmd_head.linear; sed_cmd; sed_cmd = sed_cmd->linear) { 811 if ((sed_cmd->label) && (strcmp(sed_cmd->label, label) == 0)) {
810 if (strcmp(sed_cmd->label, label) == 0) {
811 break; 812 break;
812 } 813 }
813 } 814 }
@@ -844,8 +845,8 @@ static void process_file(FILE * file)
844 force_print = 0; 845 force_print = 0;
845 846
846 /* for every line, go through all the commands */ 847 /* for every line, go through all the commands */
847 for (sed_cmd = sed_cmd_head.linear; sed_cmd; 848 for (sed_cmd = sed_cmd_head.next; sed_cmd;
848 sed_cmd = sed_cmd->linear) { 849 sed_cmd = sed_cmd->next) {
849 int deleted = 0; 850 int deleted = 0;
850 851
851 /* 852 /*
@@ -952,8 +953,8 @@ static void process_file(FILE * file)
952 } 953 }
953#endif 954#endif
954 altered |= substituted; 955 altered |= substituted;
955 if (!be_quiet && altered && ((sed_cmd->linear == NULL) 956 if (!be_quiet && altered && ((sed_cmd->next == NULL)
956 || (sed_cmd->linear->cmd != 957 || (sed_cmd->next->cmd !=
957 's'))) { 958 's'))) {
958 force_print = 1; 959 force_print = 1;
959 } 960 }
@@ -1160,7 +1161,7 @@ extern int sed_main(int argc, char **argv)
1160 1161
1161 /* if we didn't get a pattern from a -e and no command file was specified, 1162 /* if we didn't get a pattern from a -e and no command file was specified,
1162 * argv[optind] should be the pattern. no pattern, no worky */ 1163 * argv[optind] should be the pattern. no pattern, no worky */
1163 if (sed_cmd_head.linear == NULL) { 1164 if (sed_cmd_head.next == NULL) {
1164 if (argv[optind] == NULL) 1165 if (argv[optind] == NULL)
1165 bb_show_usage(); 1166 bb_show_usage();
1166 else { 1167 else {