Bernhard Reutner-Fischer | d9ed35c | 2006-05-19 12:38:30 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2002 by David I. Bell |
| 4 | * Permission is granted to use, distribute, or modify this source, |
| 5 | * provided that this copyright notice remains intact. |
| 6 | * |
| 7 | * The "ed" built-in command (much simplified) |
| 8 | */ |
| 9 | |
Denys Vlasenko | 73225b6 | 2013-11-13 12:45:33 +0100 | [diff] [blame] | 10 | //config:config ED |
| 11 | //config: bool "ed" |
| 12 | //config: default y |
| 13 | //config: help |
| 14 | //config: The original 1970's Unix text editor, from the days of teletypes. |
| 15 | //config: Small, simple, evil. Part of SUSv3. If you're not already using |
| 16 | //config: this, you don't need it. |
| 17 | |
| 18 | //kbuild:lib-$(CONFIG_ED) += ed.o |
| 19 | |
| 20 | //applet:IF_ED(APPLET(ed, BB_DIR_BIN, BB_SUID_DROP)) |
| 21 | |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 22 | //usage:#define ed_trivial_usage "" |
| 23 | //usage:#define ed_full_usage "" |
| 24 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 27 | typedef struct LINE { |
| 28 | struct LINE *next; |
| 29 | struct LINE *prev; |
| 30 | int len; |
| 31 | char data[1]; |
| 32 | } LINE; |
| 33 | |
| 34 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 35 | #define searchString bb_common_bufsiz1 |
| 36 | |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 37 | enum { |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 38 | USERSIZE = sizeof(searchString) > 1024 ? 1024 |
| 39 | : sizeof(searchString) - 1, /* max line length typed in by user */ |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 40 | INITBUF_SIZE = 1024, /* initial buffer size */ |
| 41 | }; |
| 42 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 43 | struct globals { |
| 44 | int curNum; |
| 45 | int lastNum; |
| 46 | int bufUsed; |
| 47 | int bufSize; |
| 48 | LINE *curLine; |
| 49 | char *bufBase; |
| 50 | char *bufPtr; |
| 51 | char *fileName; |
| 52 | LINE lines; |
| 53 | smallint dirty; |
| 54 | int marks[26]; |
| 55 | }; |
| 56 | #define G (*ptr_to_globals) |
| 57 | #define curLine (G.curLine ) |
| 58 | #define bufBase (G.bufBase ) |
| 59 | #define bufPtr (G.bufPtr ) |
| 60 | #define fileName (G.fileName ) |
| 61 | #define curNum (G.curNum ) |
| 62 | #define lastNum (G.lastNum ) |
| 63 | #define bufUsed (G.bufUsed ) |
| 64 | #define bufSize (G.bufSize ) |
| 65 | #define dirty (G.dirty ) |
| 66 | #define lines (G.lines ) |
| 67 | #define marks (G.marks ) |
| 68 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 69 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 70 | } while (0) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 71 | |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 72 | |
| 73 | static void doCommands(void); |
| 74 | static void subCommand(const char *cmd, int num1, int num2); |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 75 | static int getNum(const char **retcp, smallint *retHaveNum, int *retNum); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 76 | static int setCurNum(int num); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 77 | static void addLines(int num); |
| 78 | static int insertLine(int num, const char *data, int len); |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 79 | static void deleteLines(int num1, int num2); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 80 | static int printLines(int num1, int num2, int expandFlag); |
| 81 | static int writeLines(const char *file, int num1, int num2); |
| 82 | static int readLines(const char *file, int num); |
| 83 | static int searchLines(const char *str, int num1, int num2); |
| 84 | static LINE *findLine(int num); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 85 | static int findString(const LINE *lp, const char * str, int len, int offset); |
| 86 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 87 | |
| 88 | static int bad_nums(int num1, int num2, const char *for_what) |
| 89 | { |
| 90 | if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) { |
| 91 | bb_error_msg("bad line range for %s", for_what); |
| 92 | return 1; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | static char *skip_blank(const char *cp) |
| 99 | { |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 100 | while (isblank(*cp)) |
| 101 | cp++; |
| 102 | return (char *)cp; |
| 103 | } |
| 104 | |
| 105 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 106 | int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 107 | int ed_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 108 | { |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 109 | INIT_G(); |
| 110 | |
| 111 | bufSize = INITBUF_SIZE; |
| 112 | bufBase = xmalloc(bufSize); |
| 113 | bufPtr = bufBase; |
| 114 | lines.next = &lines; |
| 115 | lines.prev = &lines; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 117 | if (argv[1]) { |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 118 | fileName = xstrdup(argv[1]); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 119 | if (!readLines(fileName, 1)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 120 | return EXIT_SUCCESS; |
| 121 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 122 | if (lastNum) |
| 123 | setCurNum(1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 124 | dirty = FALSE; |
| 125 | } |
| 126 | |
| 127 | doCommands(); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 128 | return EXIT_SUCCESS; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Read commands until we are told to stop. |
| 133 | */ |
| 134 | static void doCommands(void) |
| 135 | { |
| 136 | const char *cp; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 137 | char *endbuf, buf[USERSIZE]; |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 138 | int len, num1, num2; |
| 139 | smallint have1, have2; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 141 | while (TRUE) { |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 142 | /* Returns: |
| 143 | * -1 on read errors or EOF, or on bare Ctrl-D. |
| 144 | * 0 on ctrl-C, |
| 145 | * >0 length of input string, including terminating '\n' |
| 146 | */ |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 147 | len = read_line_input(NULL, ": ", buf, sizeof(buf), /*timeout*/ -1); |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 148 | if (len <= 0) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 149 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 150 | endbuf = &buf[len - 1]; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 151 | while ((endbuf > buf) && isblank(endbuf[-1])) |
| 152 | endbuf--; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 153 | *endbuf = '\0'; |
| 154 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 155 | cp = skip_blank(buf); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 156 | have1 = FALSE; |
| 157 | have2 = FALSE; |
| 158 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 159 | if ((curNum == 0) && (lastNum > 0)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 160 | curNum = 1; |
| 161 | curLine = lines.next; |
| 162 | } |
| 163 | |
| 164 | if (!getNum(&cp, &have1, &num1)) |
| 165 | continue; |
| 166 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 167 | cp = skip_blank(cp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 168 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 169 | if (*cp == ',') { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 170 | cp++; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 171 | if (!getNum(&cp, &have2, &num2)) |
| 172 | continue; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 173 | if (!have1) |
| 174 | num1 = 1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 175 | if (!have2) |
| 176 | num2 = lastNum; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 177 | have1 = TRUE; |
| 178 | have2 = TRUE; |
| 179 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 180 | if (!have1) |
| 181 | num1 = curNum; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 182 | if (!have2) |
| 183 | num2 = num1; |
| 184 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 185 | switch (*cp++) { |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 186 | case 'a': |
| 187 | addLines(num1 + 1); |
| 188 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 189 | |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 190 | case 'c': |
| 191 | deleteLines(num1, num2); |
| 192 | addLines(num1); |
| 193 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 194 | |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 195 | case 'd': |
| 196 | deleteLines(num1, num2); |
| 197 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 198 | |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 199 | case 'f': |
| 200 | if (*cp && !isblank(*cp)) { |
| 201 | bb_error_msg("bad file command"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 202 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 203 | } |
| 204 | cp = skip_blank(cp); |
| 205 | if (*cp == '\0') { |
| 206 | if (fileName) |
| 207 | printf("\"%s\"\n", fileName); |
| 208 | else |
| 209 | printf("No file name\n"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 210 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 211 | } |
| 212 | free(fileName); |
| 213 | fileName = xstrdup(cp); |
| 214 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 215 | |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 216 | case 'i': |
| 217 | addLines(num1); |
| 218 | break; |
| 219 | |
| 220 | case 'k': |
| 221 | cp = skip_blank(cp); |
| 222 | if ((*cp < 'a') || (*cp > 'z') || cp[1]) { |
| 223 | bb_error_msg("bad mark name"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 224 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 225 | } |
| 226 | marks[*cp - 'a'] = num2; |
| 227 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 228 | |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 229 | case 'l': |
| 230 | printLines(num1, num2, TRUE); |
| 231 | break; |
| 232 | |
| 233 | case 'p': |
| 234 | printLines(num1, num2, FALSE); |
| 235 | break; |
| 236 | |
| 237 | case 'q': |
| 238 | cp = skip_blank(cp); |
| 239 | if (have1 || *cp) { |
| 240 | bb_error_msg("bad quit command"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 241 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 242 | } |
| 243 | if (!dirty) |
| 244 | return; |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 245 | len = read_line_input(NULL, "Really quit? ", buf, 16, /*timeout*/ -1); |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 246 | /* read error/EOF - no way to continue */ |
| 247 | if (len < 0) |
| 248 | return; |
| 249 | cp = skip_blank(buf); |
| 250 | if ((*cp | 0x20) == 'y') /* Y or y */ |
| 251 | return; |
| 252 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 253 | |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 254 | case 'r': |
| 255 | if (*cp && !isblank(*cp)) { |
| 256 | bb_error_msg("bad read command"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 257 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 258 | } |
| 259 | cp = skip_blank(cp); |
| 260 | if (*cp == '\0') { |
| 261 | bb_error_msg("no file name"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 262 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 263 | } |
| 264 | if (!have1) |
| 265 | num1 = lastNum; |
| 266 | if (readLines(cp, num1 + 1)) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 267 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 268 | if (fileName == NULL) |
| 269 | fileName = xstrdup(cp); |
| 270 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 271 | |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 272 | case 's': |
| 273 | subCommand(cp, num1, num2); |
| 274 | break; |
| 275 | |
| 276 | case 'w': |
| 277 | if (*cp && !isblank(*cp)) { |
| 278 | bb_error_msg("bad write command"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 279 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 280 | } |
| 281 | cp = skip_blank(cp); |
| 282 | if (!have1) { |
| 283 | num1 = 1; |
| 284 | num2 = lastNum; |
| 285 | } |
| 286 | if (*cp == '\0') |
| 287 | cp = fileName; |
| 288 | if (cp == NULL) { |
| 289 | bb_error_msg("no file name specified"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 290 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 291 | } |
| 292 | writeLines(cp, num1, num2); |
| 293 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 294 | |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 295 | case 'z': |
| 296 | switch (*cp) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 297 | case '-': |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 298 | printLines(curNum - 21, curNum, FALSE); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 299 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 300 | case '.': |
| 301 | printLines(curNum - 11, curNum + 10, FALSE); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 302 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 303 | default: |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 304 | printLines(curNum, curNum + 21, FALSE); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 305 | break; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 306 | } |
| 307 | break; |
| 308 | |
| 309 | case '.': |
| 310 | if (have1) { |
| 311 | bb_error_msg("no arguments allowed"); |
| 312 | break; |
| 313 | } |
| 314 | printLines(curNum, curNum, FALSE); |
| 315 | break; |
| 316 | |
| 317 | case '-': |
| 318 | if (setCurNum(curNum - 1)) |
| 319 | printLines(curNum, curNum, FALSE); |
| 320 | break; |
| 321 | |
| 322 | case '=': |
| 323 | printf("%d\n", num1); |
| 324 | break; |
| 325 | case '\0': |
| 326 | if (have1) { |
| 327 | printLines(num2, num2, FALSE); |
| 328 | break; |
| 329 | } |
| 330 | if (setCurNum(curNum + 1)) |
| 331 | printLines(curNum, curNum, FALSE); |
| 332 | break; |
| 333 | |
| 334 | default: |
| 335 | bb_error_msg("unimplemented command"); |
| 336 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /* |
| 343 | * Do the substitute command. |
| 344 | * The current line is set to the last substitution done. |
| 345 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 346 | static void subCommand(const char *cmd, int num1, int num2) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 347 | { |
| 348 | char *cp, *oldStr, *newStr, buf[USERSIZE]; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 349 | int delim, oldLen, newLen, deltaLen, offset; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 350 | LINE *lp, *nlp; |
| 351 | int globalFlag, printFlag, didSub, needPrint; |
| 352 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 353 | if (bad_nums(num1, num2, "substitute")) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 354 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 355 | |
| 356 | globalFlag = FALSE; |
| 357 | printFlag = FALSE; |
| 358 | didSub = FALSE; |
| 359 | needPrint = FALSE; |
| 360 | |
| 361 | /* |
| 362 | * Copy the command so we can modify it. |
| 363 | */ |
| 364 | strcpy(buf, cmd); |
| 365 | cp = buf; |
| 366 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 367 | if (isblank(*cp) || (*cp == '\0')) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 368 | bb_error_msg("bad delimiter for substitute"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 369 | return; |
| 370 | } |
| 371 | |
| 372 | delim = *cp++; |
| 373 | oldStr = cp; |
| 374 | |
| 375 | cp = strchr(cp, delim); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 376 | if (cp == NULL) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 377 | bb_error_msg("missing 2nd delimiter for substitute"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 378 | return; |
| 379 | } |
| 380 | |
| 381 | *cp++ = '\0'; |
| 382 | |
| 383 | newStr = cp; |
| 384 | cp = strchr(cp, delim); |
| 385 | |
| 386 | if (cp) |
| 387 | *cp++ = '\0'; |
| 388 | else |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 389 | cp = (char*)""; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 390 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 391 | while (*cp) switch (*cp++) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 392 | case 'g': |
| 393 | globalFlag = TRUE; |
| 394 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 395 | case 'p': |
| 396 | printFlag = TRUE; |
| 397 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 398 | default: |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 399 | bb_error_msg("unknown option for substitute"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 403 | if (*oldStr == '\0') { |
| 404 | if (searchString[0] == '\0') { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 405 | bb_error_msg("no previous search string"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 406 | return; |
| 407 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 408 | oldStr = searchString; |
| 409 | } |
| 410 | |
| 411 | if (oldStr != searchString) |
| 412 | strcpy(searchString, oldStr); |
| 413 | |
| 414 | lp = findLine(num1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 415 | if (lp == NULL) |
| 416 | return; |
| 417 | |
| 418 | oldLen = strlen(oldStr); |
| 419 | newLen = strlen(newStr); |
| 420 | deltaLen = newLen - oldLen; |
| 421 | offset = 0; |
| 422 | nlp = NULL; |
| 423 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 424 | while (num1 <= num2) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 425 | offset = findString(lp, oldStr, oldLen, offset); |
| 426 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 427 | if (offset < 0) { |
| 428 | if (needPrint) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 429 | printLines(num1, num1, FALSE); |
| 430 | needPrint = FALSE; |
| 431 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 432 | offset = 0; |
| 433 | lp = lp->next; |
| 434 | num1++; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 435 | continue; |
| 436 | } |
| 437 | |
| 438 | needPrint = printFlag; |
| 439 | didSub = TRUE; |
| 440 | dirty = TRUE; |
| 441 | |
| 442 | /* |
| 443 | * If the replacement string is the same size or shorter |
| 444 | * than the old string, then the substitution is easy. |
| 445 | */ |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 446 | if (deltaLen <= 0) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 447 | memcpy(&lp->data[offset], newStr, newLen); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 448 | if (deltaLen) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 449 | memcpy(&lp->data[offset + newLen], |
| 450 | &lp->data[offset + oldLen], |
| 451 | lp->len - offset - oldLen); |
| 452 | |
| 453 | lp->len += deltaLen; |
| 454 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 455 | offset += newLen; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 456 | if (globalFlag) |
| 457 | continue; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 458 | if (needPrint) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 459 | printLines(num1, num1, FALSE); |
| 460 | needPrint = FALSE; |
| 461 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 462 | lp = lp->next; |
| 463 | num1++; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 464 | continue; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * The new string is larger, so allocate a new line |
Marek Polacek | b0b8884 | 2011-04-16 17:33:43 +0200 | [diff] [blame] | 469 | * structure and use that. Link it in place of |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 470 | * the old line structure. |
| 471 | */ |
Denys Vlasenko | 9037787 | 2010-01-08 09:07:50 +0100 | [diff] [blame] | 472 | nlp = xmalloc(sizeof(LINE) + lp->len + deltaLen); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 473 | |
| 474 | nlp->len = lp->len + deltaLen; |
| 475 | |
| 476 | memcpy(nlp->data, lp->data, offset); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 477 | memcpy(&nlp->data[offset], newStr, newLen); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 478 | memcpy(&nlp->data[offset + newLen], |
| 479 | &lp->data[offset + oldLen], |
| 480 | lp->len - offset - oldLen); |
| 481 | |
| 482 | nlp->next = lp->next; |
| 483 | nlp->prev = lp->prev; |
| 484 | nlp->prev->next = nlp; |
| 485 | nlp->next->prev = nlp; |
| 486 | |
| 487 | if (curLine == lp) |
| 488 | curLine = nlp; |
| 489 | |
| 490 | free(lp); |
| 491 | lp = nlp; |
| 492 | |
| 493 | offset += newLen; |
| 494 | |
| 495 | if (globalFlag) |
| 496 | continue; |
| 497 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 498 | if (needPrint) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 499 | printLines(num1, num1, FALSE); |
| 500 | needPrint = FALSE; |
| 501 | } |
| 502 | |
| 503 | lp = lp->next; |
| 504 | num1++; |
| 505 | } |
| 506 | |
| 507 | if (!didSub) |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 508 | bb_error_msg("no substitutions found for \"%s\"", oldStr); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | |
| 512 | /* |
| 513 | * Search a line for the specified string starting at the specified |
| 514 | * offset in the line. Returns the offset of the found string, or -1. |
| 515 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 516 | static int findString(const LINE *lp, const char *str, int len, int offset) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 517 | { |
| 518 | int left; |
| 519 | const char *cp, *ncp; |
| 520 | |
| 521 | cp = &lp->data[offset]; |
| 522 | left = lp->len - offset; |
| 523 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 524 | while (left >= len) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 525 | ncp = memchr(cp, *str, left); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 526 | if (ncp == NULL) |
| 527 | return -1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 528 | left -= (ncp - cp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 529 | if (left < len) |
| 530 | return -1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 531 | cp = ncp; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 532 | if (memcmp(cp, str, len) == 0) |
| 533 | return (cp - lp->data); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 534 | cp++; |
| 535 | left--; |
| 536 | } |
| 537 | |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | |
| 542 | /* |
| 543 | * Add lines which are typed in by the user. |
| 544 | * The lines are inserted just before the specified line number. |
| 545 | * The lines are terminated by a line containing a single dot (ugly!), |
| 546 | * or by an end of file. |
| 547 | */ |
| 548 | static void addLines(int num) |
| 549 | { |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 550 | int len; |
| 551 | char buf[USERSIZE + 1]; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 552 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 553 | while (1) { |
| 554 | /* Returns: |
| 555 | * -1 on read errors or EOF, or on bare Ctrl-D. |
| 556 | * 0 on ctrl-C, |
| 557 | * >0 length of input string, including terminating '\n' |
| 558 | */ |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 559 | len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1); |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 560 | if (len <= 0) { |
| 561 | /* Previously, ctrl-C was exiting to shell. |
| 562 | * Now we exit to ed prompt. Is in important? */ |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 563 | return; |
| 564 | } |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 565 | if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0')) |
| 566 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 567 | if (!insertLine(num++, buf, len)) |
| 568 | return; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | |
| 573 | /* |
| 574 | * Parse a line number argument if it is present. This is a sum |
| 575 | * or difference of numbers, '.', '$', 'x, or a search string. |
Bernhard Reutner-Fischer | d9ed35c | 2006-05-19 12:38:30 +0000 | [diff] [blame] | 576 | * Returns TRUE if successful (whether or not there was a number). |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 577 | * Returns FALSE if there was a parsing error, with a message output. |
| 578 | * Whether there was a number is returned indirectly, as is the number. |
| 579 | * The character pointer which stopped the scan is also returned. |
| 580 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 581 | static int getNum(const char **retcp, smallint *retHaveNum, int *retNum) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 582 | { |
| 583 | const char *cp; |
| 584 | char *endStr, str[USERSIZE]; |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 585 | int value, num; |
| 586 | smallint haveNum, minus; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 587 | |
| 588 | cp = *retcp; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 589 | value = 0; |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 590 | haveNum = FALSE; |
| 591 | minus = 0; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 592 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 593 | while (TRUE) { |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 594 | cp = skip_blank(cp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 595 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 596 | switch (*cp) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 597 | case '.': |
| 598 | haveNum = TRUE; |
| 599 | num = curNum; |
| 600 | cp++; |
| 601 | break; |
| 602 | |
| 603 | case '$': |
| 604 | haveNum = TRUE; |
| 605 | num = lastNum; |
| 606 | cp++; |
| 607 | break; |
| 608 | |
| 609 | case '\'': |
| 610 | cp++; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 611 | if ((*cp < 'a') || (*cp > 'z')) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 612 | bb_error_msg("bad mark name"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 613 | return FALSE; |
| 614 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 615 | haveNum = TRUE; |
| 616 | num = marks[*cp++ - 'a']; |
| 617 | break; |
| 618 | |
| 619 | case '/': |
| 620 | strcpy(str, ++cp); |
| 621 | endStr = strchr(str, '/'); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 622 | if (endStr) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 623 | *endStr++ = '\0'; |
| 624 | cp += (endStr - str); |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 625 | } else |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 626 | cp = ""; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 627 | num = searchLines(str, curNum, lastNum); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 628 | if (num == 0) |
| 629 | return FALSE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 630 | haveNum = TRUE; |
| 631 | break; |
| 632 | |
| 633 | default: |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 634 | if (!isdigit(*cp)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 635 | *retcp = cp; |
| 636 | *retHaveNum = haveNum; |
| 637 | *retNum = value; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 638 | return TRUE; |
| 639 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 640 | num = 0; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 641 | while (isdigit(*cp)) |
| 642 | num = num * 10 + *cp++ - '0'; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 643 | haveNum = TRUE; |
| 644 | break; |
| 645 | } |
| 646 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 647 | value += (minus ? -num : num); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 648 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 649 | cp = skip_blank(cp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 650 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 651 | switch (*cp) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 652 | case '-': |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 653 | minus = 1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 654 | cp++; |
| 655 | break; |
| 656 | |
| 657 | case '+': |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 658 | minus = 0; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 659 | cp++; |
| 660 | break; |
| 661 | |
| 662 | default: |
| 663 | *retcp = cp; |
| 664 | *retHaveNum = haveNum; |
| 665 | *retNum = value; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 666 | return TRUE; |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | |
| 672 | /* |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 673 | * Read lines from a file at the specified line number. |
| 674 | * Returns TRUE if the file was successfully read. |
| 675 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 676 | static int readLines(const char *file, int num) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 677 | { |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 678 | int fd, cc; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 679 | int len, lineCount, charCount; |
| 680 | char *cp; |
| 681 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 682 | if ((num < 1) || (num > lastNum + 1)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 683 | bb_error_msg("bad line for read"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 684 | return FALSE; |
| 685 | } |
| 686 | |
| 687 | fd = open(file, 0); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 688 | if (fd < 0) { |
Marek Polacek | 7dd61e3 | 2010-09-12 17:06:43 +0200 | [diff] [blame] | 689 | bb_simple_perror_msg(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 690 | return FALSE; |
| 691 | } |
| 692 | |
| 693 | bufPtr = bufBase; |
| 694 | bufUsed = 0; |
| 695 | lineCount = 0; |
| 696 | charCount = 0; |
| 697 | cc = 0; |
| 698 | |
| 699 | printf("\"%s\", ", file); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 700 | fflush_all(); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 701 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 702 | do { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 703 | cp = memchr(bufPtr, '\n', bufUsed); |
| 704 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 705 | if (cp) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 706 | len = (cp - bufPtr) + 1; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 707 | if (!insertLine(num, bufPtr, len)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 708 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 709 | return FALSE; |
| 710 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 711 | bufPtr += len; |
| 712 | bufUsed -= len; |
| 713 | charCount += len; |
| 714 | lineCount++; |
| 715 | num++; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 716 | continue; |
| 717 | } |
| 718 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 719 | if (bufPtr != bufBase) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 720 | memcpy(bufBase, bufPtr, bufUsed); |
| 721 | bufPtr = bufBase + bufUsed; |
| 722 | } |
| 723 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 724 | if (bufUsed >= bufSize) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 725 | len = (bufSize * 3) / 2; |
Denys Vlasenko | 9037787 | 2010-01-08 09:07:50 +0100 | [diff] [blame] | 726 | cp = xrealloc(bufBase, len); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 727 | bufBase = cp; |
| 728 | bufPtr = bufBase + bufUsed; |
| 729 | bufSize = len; |
| 730 | } |
| 731 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 732 | cc = safe_read(fd, bufPtr, bufSize - bufUsed); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 733 | bufUsed += cc; |
| 734 | bufPtr = bufBase; |
| 735 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 736 | } while (cc > 0); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 737 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 738 | if (cc < 0) { |
Marek Polacek | 7dd61e3 | 2010-09-12 17:06:43 +0200 | [diff] [blame] | 739 | bb_simple_perror_msg(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 740 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 741 | return FALSE; |
| 742 | } |
| 743 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 744 | if (bufUsed) { |
| 745 | if (!insertLine(num, bufPtr, bufUsed)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 746 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 747 | return -1; |
| 748 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 749 | lineCount++; |
| 750 | charCount += bufUsed; |
| 751 | } |
| 752 | |
| 753 | close(fd); |
| 754 | |
| 755 | printf("%d lines%s, %d chars\n", lineCount, |
| 756 | (bufUsed ? " (incomplete)" : ""), charCount); |
| 757 | |
| 758 | return TRUE; |
| 759 | } |
| 760 | |
| 761 | |
| 762 | /* |
| 763 | * Write the specified lines out to the specified file. |
| 764 | * Returns TRUE if successful, or FALSE on an error with a message output. |
| 765 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 766 | static int writeLines(const char *file, int num1, int num2) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 767 | { |
| 768 | LINE *lp; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 769 | int fd, lineCount, charCount; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 770 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 771 | if (bad_nums(num1, num2, "write")) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 772 | return FALSE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 773 | |
| 774 | lineCount = 0; |
| 775 | charCount = 0; |
| 776 | |
| 777 | fd = creat(file, 0666); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 778 | if (fd < 0) { |
Marek Polacek | 7dd61e3 | 2010-09-12 17:06:43 +0200 | [diff] [blame] | 779 | bb_simple_perror_msg(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 780 | return FALSE; |
| 781 | } |
| 782 | |
| 783 | printf("\"%s\", ", file); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 784 | fflush_all(); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 785 | |
| 786 | lp = findLine(num1); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 787 | if (lp == NULL) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 788 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 789 | return FALSE; |
| 790 | } |
| 791 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 792 | while (num1++ <= num2) { |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 793 | if (full_write(fd, lp->data, lp->len) != lp->len) { |
Marek Polacek | 7dd61e3 | 2010-09-12 17:06:43 +0200 | [diff] [blame] | 794 | bb_simple_perror_msg(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 795 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 796 | return FALSE; |
| 797 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 798 | charCount += lp->len; |
| 799 | lineCount++; |
| 800 | lp = lp->next; |
| 801 | } |
| 802 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 803 | if (close(fd) < 0) { |
Marek Polacek | 7dd61e3 | 2010-09-12 17:06:43 +0200 | [diff] [blame] | 804 | bb_simple_perror_msg(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 805 | return FALSE; |
| 806 | } |
| 807 | |
| 808 | printf("%d lines, %d chars\n", lineCount, charCount); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 809 | return TRUE; |
| 810 | } |
| 811 | |
| 812 | |
| 813 | /* |
| 814 | * Print lines in a specified range. |
| 815 | * The last line printed becomes the current line. |
| 816 | * If expandFlag is TRUE, then the line is printed specially to |
| 817 | * show magic characters. |
| 818 | */ |
| 819 | static int printLines(int num1, int num2, int expandFlag) |
| 820 | { |
| 821 | const LINE *lp; |
| 822 | const char *cp; |
| 823 | int ch, count; |
| 824 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 825 | if (bad_nums(num1, num2, "print")) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 826 | return FALSE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 827 | |
| 828 | lp = findLine(num1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 829 | if (lp == NULL) |
| 830 | return FALSE; |
| 831 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 832 | while (num1 <= num2) { |
| 833 | if (!expandFlag) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 834 | write(STDOUT_FILENO, lp->data, lp->len); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 835 | setCurNum(num1++); |
| 836 | lp = lp->next; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 837 | continue; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Show control characters and characters with the |
| 842 | * high bit set specially. |
| 843 | */ |
| 844 | cp = lp->data; |
| 845 | count = lp->len; |
| 846 | |
| 847 | if ((count > 0) && (cp[count - 1] == '\n')) |
| 848 | count--; |
| 849 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 850 | while (count-- > 0) { |
Denis Vlasenko | d3c042f | 2007-12-30 01:59:53 +0000 | [diff] [blame] | 851 | ch = (unsigned char) *cp++; |
| 852 | fputc_printable(ch | PRINTABLE_META, stdout); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | fputs("$\n", stdout); |
| 856 | |
| 857 | setCurNum(num1++); |
| 858 | lp = lp->next; |
| 859 | } |
| 860 | |
| 861 | return TRUE; |
| 862 | } |
| 863 | |
| 864 | |
| 865 | /* |
| 866 | * Insert a new line with the specified text. |
| 867 | * The line is inserted so as to become the specified line, |
| 868 | * thus pushing any existing and further lines down one. |
| 869 | * The inserted line is also set to become the current line. |
| 870 | * Returns TRUE if successful. |
| 871 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 872 | static int insertLine(int num, const char *data, int len) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 873 | { |
| 874 | LINE *newLp, *lp; |
| 875 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 876 | if ((num < 1) || (num > lastNum + 1)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 877 | bb_error_msg("inserting at bad line number"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 878 | return FALSE; |
| 879 | } |
| 880 | |
Denys Vlasenko | 9037787 | 2010-01-08 09:07:50 +0100 | [diff] [blame] | 881 | newLp = xmalloc(sizeof(LINE) + len - 1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 882 | |
| 883 | memcpy(newLp->data, data, len); |
| 884 | newLp->len = len; |
| 885 | |
| 886 | if (num > lastNum) |
| 887 | lp = &lines; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 888 | else { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 889 | lp = findLine(num); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 890 | if (lp == NULL) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 891 | free((char *) newLp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 892 | return FALSE; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | newLp->next = lp; |
| 897 | newLp->prev = lp->prev; |
| 898 | lp->prev->next = newLp; |
| 899 | lp->prev = newLp; |
| 900 | |
| 901 | lastNum++; |
| 902 | dirty = TRUE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 903 | return setCurNum(num); |
| 904 | } |
| 905 | |
| 906 | |
| 907 | /* |
| 908 | * Delete lines from the given range. |
| 909 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 910 | static void deleteLines(int num1, int num2) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 911 | { |
| 912 | LINE *lp, *nlp, *plp; |
| 913 | int count; |
| 914 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 915 | if (bad_nums(num1, num2, "delete")) |
| 916 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 917 | |
| 918 | lp = findLine(num1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 919 | if (lp == NULL) |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 920 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 921 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 922 | if ((curNum >= num1) && (curNum <= num2)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 923 | if (num2 < lastNum) |
| 924 | setCurNum(num2 + 1); |
| 925 | else if (num1 > 1) |
| 926 | setCurNum(num1 - 1); |
| 927 | else |
| 928 | curNum = 0; |
| 929 | } |
| 930 | |
| 931 | count = num2 - num1 + 1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 932 | if (curNum > num2) |
| 933 | curNum -= count; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 934 | lastNum -= count; |
| 935 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 936 | while (count-- > 0) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 937 | nlp = lp->next; |
| 938 | plp = lp->prev; |
| 939 | plp->next = nlp; |
| 940 | nlp->prev = plp; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 941 | free(lp); |
| 942 | lp = nlp; |
| 943 | } |
| 944 | |
| 945 | dirty = TRUE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | |
| 949 | /* |
| 950 | * Search for a line which contains the specified string. |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 951 | * If the string is "", then the previously searched for string |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 952 | * is used. The currently searched for string is saved for future use. |
| 953 | * Returns the line number which matches, or 0 if there was no match |
| 954 | * with an error printed. |
| 955 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 956 | static NOINLINE int searchLines(const char *str, int num1, int num2) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 957 | { |
| 958 | const LINE *lp; |
| 959 | int len; |
| 960 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 961 | if (bad_nums(num1, num2, "search")) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 962 | return 0; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 963 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 964 | if (*str == '\0') { |
| 965 | if (searchString[0] == '\0') { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 966 | bb_error_msg("no previous search string"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 967 | return 0; |
| 968 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 969 | str = searchString; |
| 970 | } |
| 971 | |
| 972 | if (str != searchString) |
| 973 | strcpy(searchString, str); |
| 974 | |
| 975 | len = strlen(str); |
| 976 | |
| 977 | lp = findLine(num1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 978 | if (lp == NULL) |
| 979 | return 0; |
| 980 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 981 | while (num1 <= num2) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 982 | if (findString(lp, str, len, 0) >= 0) |
| 983 | return num1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 984 | num1++; |
| 985 | lp = lp->next; |
| 986 | } |
| 987 | |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 988 | bb_error_msg("can't find string \"%s\"", str); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | |
| 993 | /* |
| 994 | * Return a pointer to the specified line number. |
| 995 | */ |
| 996 | static LINE *findLine(int num) |
| 997 | { |
| 998 | LINE *lp; |
| 999 | int lnum; |
| 1000 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1001 | if ((num < 1) || (num > lastNum)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 1002 | bb_error_msg("line number %d does not exist", num); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1003 | return NULL; |
| 1004 | } |
| 1005 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1006 | if (curNum <= 0) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1007 | curNum = 1; |
| 1008 | curLine = lines.next; |
| 1009 | } |
| 1010 | |
| 1011 | if (num == curNum) |
| 1012 | return curLine; |
| 1013 | |
| 1014 | lp = curLine; |
| 1015 | lnum = curNum; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1016 | if (num < (curNum / 2)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1017 | lp = lines.next; |
| 1018 | lnum = 1; |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 1019 | } else if (num > ((curNum + lastNum) / 2)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1020 | lp = lines.prev; |
| 1021 | lnum = lastNum; |
| 1022 | } |
| 1023 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1024 | while (lnum < num) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1025 | lp = lp->next; |
| 1026 | lnum++; |
| 1027 | } |
| 1028 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1029 | while (lnum > num) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1030 | lp = lp->prev; |
| 1031 | lnum--; |
| 1032 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1033 | return lp; |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | /* |
| 1038 | * Set the current line number. |
| 1039 | * Returns TRUE if successful. |
| 1040 | */ |
| 1041 | static int setCurNum(int num) |
| 1042 | { |
| 1043 | LINE *lp; |
| 1044 | |
| 1045 | lp = findLine(num); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1046 | if (lp == NULL) |
| 1047 | return FALSE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1048 | curNum = num; |
| 1049 | curLine = lp; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1050 | return TRUE; |
| 1051 | } |