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 |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 209 | puts("No file name"); |
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; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 735 | } while (cc > 0); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 736 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 737 | if (cc < 0) { |
Marek Polacek | 7dd61e3 | 2010-09-12 17:06:43 +0200 | [diff] [blame] | 738 | bb_simple_perror_msg(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 739 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 740 | return FALSE; |
| 741 | } |
| 742 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 743 | if (bufUsed) { |
| 744 | if (!insertLine(num, bufPtr, bufUsed)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 745 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 746 | return -1; |
| 747 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 748 | lineCount++; |
| 749 | charCount += bufUsed; |
| 750 | } |
| 751 | |
| 752 | close(fd); |
| 753 | |
| 754 | printf("%d lines%s, %d chars\n", lineCount, |
| 755 | (bufUsed ? " (incomplete)" : ""), charCount); |
| 756 | |
| 757 | return TRUE; |
| 758 | } |
| 759 | |
| 760 | |
| 761 | /* |
| 762 | * Write the specified lines out to the specified file. |
| 763 | * Returns TRUE if successful, or FALSE on an error with a message output. |
| 764 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 765 | static int writeLines(const char *file, int num1, int num2) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 766 | { |
| 767 | LINE *lp; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 768 | int fd, lineCount, charCount; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 769 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 770 | if (bad_nums(num1, num2, "write")) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 771 | return FALSE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 772 | |
| 773 | lineCount = 0; |
| 774 | charCount = 0; |
| 775 | |
| 776 | fd = creat(file, 0666); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 777 | if (fd < 0) { |
Marek Polacek | 7dd61e3 | 2010-09-12 17:06:43 +0200 | [diff] [blame] | 778 | bb_simple_perror_msg(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 779 | return FALSE; |
| 780 | } |
| 781 | |
| 782 | printf("\"%s\", ", file); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 783 | fflush_all(); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 784 | |
| 785 | lp = findLine(num1); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 786 | if (lp == NULL) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 787 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 788 | return FALSE; |
| 789 | } |
| 790 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 791 | while (num1++ <= num2) { |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 792 | if (full_write(fd, lp->data, lp->len) != lp->len) { |
Marek Polacek | 7dd61e3 | 2010-09-12 17:06:43 +0200 | [diff] [blame] | 793 | bb_simple_perror_msg(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 794 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 795 | return FALSE; |
| 796 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 797 | charCount += lp->len; |
| 798 | lineCount++; |
| 799 | lp = lp->next; |
| 800 | } |
| 801 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 802 | if (close(fd) < 0) { |
Marek Polacek | 7dd61e3 | 2010-09-12 17:06:43 +0200 | [diff] [blame] | 803 | bb_simple_perror_msg(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 804 | return FALSE; |
| 805 | } |
| 806 | |
| 807 | printf("%d lines, %d chars\n", lineCount, charCount); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 808 | return TRUE; |
| 809 | } |
| 810 | |
| 811 | |
| 812 | /* |
| 813 | * Print lines in a specified range. |
| 814 | * The last line printed becomes the current line. |
| 815 | * If expandFlag is TRUE, then the line is printed specially to |
| 816 | * show magic characters. |
| 817 | */ |
| 818 | static int printLines(int num1, int num2, int expandFlag) |
| 819 | { |
| 820 | const LINE *lp; |
| 821 | const char *cp; |
| 822 | int ch, count; |
| 823 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 824 | if (bad_nums(num1, num2, "print")) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 825 | return FALSE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 826 | |
| 827 | lp = findLine(num1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 828 | if (lp == NULL) |
| 829 | return FALSE; |
| 830 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 831 | while (num1 <= num2) { |
| 832 | if (!expandFlag) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 833 | write(STDOUT_FILENO, lp->data, lp->len); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 834 | setCurNum(num1++); |
| 835 | lp = lp->next; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 836 | continue; |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Show control characters and characters with the |
| 841 | * high bit set specially. |
| 842 | */ |
| 843 | cp = lp->data; |
| 844 | count = lp->len; |
| 845 | |
| 846 | if ((count > 0) && (cp[count - 1] == '\n')) |
| 847 | count--; |
| 848 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 849 | while (count-- > 0) { |
Denis Vlasenko | d3c042f | 2007-12-30 01:59:53 +0000 | [diff] [blame] | 850 | ch = (unsigned char) *cp++; |
| 851 | fputc_printable(ch | PRINTABLE_META, stdout); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | fputs("$\n", stdout); |
| 855 | |
| 856 | setCurNum(num1++); |
| 857 | lp = lp->next; |
| 858 | } |
| 859 | |
| 860 | return TRUE; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | /* |
| 865 | * Insert a new line with the specified text. |
| 866 | * The line is inserted so as to become the specified line, |
| 867 | * thus pushing any existing and further lines down one. |
| 868 | * The inserted line is also set to become the current line. |
| 869 | * Returns TRUE if successful. |
| 870 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 871 | static int insertLine(int num, const char *data, int len) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 872 | { |
| 873 | LINE *newLp, *lp; |
| 874 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 875 | if ((num < 1) || (num > lastNum + 1)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 876 | bb_error_msg("inserting at bad line number"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 877 | return FALSE; |
| 878 | } |
| 879 | |
Denys Vlasenko | 9037787 | 2010-01-08 09:07:50 +0100 | [diff] [blame] | 880 | newLp = xmalloc(sizeof(LINE) + len - 1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 881 | |
| 882 | memcpy(newLp->data, data, len); |
| 883 | newLp->len = len; |
| 884 | |
| 885 | if (num > lastNum) |
| 886 | lp = &lines; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 887 | else { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 888 | lp = findLine(num); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 889 | if (lp == NULL) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 890 | free((char *) newLp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 891 | return FALSE; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | newLp->next = lp; |
| 896 | newLp->prev = lp->prev; |
| 897 | lp->prev->next = newLp; |
| 898 | lp->prev = newLp; |
| 899 | |
| 900 | lastNum++; |
| 901 | dirty = TRUE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 902 | return setCurNum(num); |
| 903 | } |
| 904 | |
| 905 | |
| 906 | /* |
| 907 | * Delete lines from the given range. |
| 908 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 909 | static void deleteLines(int num1, int num2) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 910 | { |
| 911 | LINE *lp, *nlp, *plp; |
| 912 | int count; |
| 913 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 914 | if (bad_nums(num1, num2, "delete")) |
| 915 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 916 | |
| 917 | lp = findLine(num1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 918 | if (lp == NULL) |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 919 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 920 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 921 | if ((curNum >= num1) && (curNum <= num2)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 922 | if (num2 < lastNum) |
| 923 | setCurNum(num2 + 1); |
| 924 | else if (num1 > 1) |
| 925 | setCurNum(num1 - 1); |
| 926 | else |
| 927 | curNum = 0; |
| 928 | } |
| 929 | |
| 930 | count = num2 - num1 + 1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 931 | if (curNum > num2) |
| 932 | curNum -= count; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 933 | lastNum -= count; |
| 934 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 935 | while (count-- > 0) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 936 | nlp = lp->next; |
| 937 | plp = lp->prev; |
| 938 | plp->next = nlp; |
| 939 | nlp->prev = plp; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 940 | free(lp); |
| 941 | lp = nlp; |
| 942 | } |
| 943 | |
| 944 | dirty = TRUE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | |
| 948 | /* |
| 949 | * Search for a line which contains the specified string. |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 950 | * If the string is "", then the previously searched for string |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 951 | * is used. The currently searched for string is saved for future use. |
| 952 | * Returns the line number which matches, or 0 if there was no match |
| 953 | * with an error printed. |
| 954 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 955 | static NOINLINE int searchLines(const char *str, int num1, int num2) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 956 | { |
| 957 | const LINE *lp; |
| 958 | int len; |
| 959 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 960 | if (bad_nums(num1, num2, "search")) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 961 | return 0; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 962 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 963 | if (*str == '\0') { |
| 964 | if (searchString[0] == '\0') { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 965 | bb_error_msg("no previous search string"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 966 | return 0; |
| 967 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 968 | str = searchString; |
| 969 | } |
| 970 | |
| 971 | if (str != searchString) |
| 972 | strcpy(searchString, str); |
| 973 | |
| 974 | len = strlen(str); |
| 975 | |
| 976 | lp = findLine(num1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 977 | if (lp == NULL) |
| 978 | return 0; |
| 979 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 980 | while (num1 <= num2) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 981 | if (findString(lp, str, len, 0) >= 0) |
| 982 | return num1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 983 | num1++; |
| 984 | lp = lp->next; |
| 985 | } |
| 986 | |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 987 | bb_error_msg("can't find string \"%s\"", str); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | |
| 992 | /* |
| 993 | * Return a pointer to the specified line number. |
| 994 | */ |
| 995 | static LINE *findLine(int num) |
| 996 | { |
| 997 | LINE *lp; |
| 998 | int lnum; |
| 999 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1000 | if ((num < 1) || (num > lastNum)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 1001 | bb_error_msg("line number %d does not exist", num); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1002 | return NULL; |
| 1003 | } |
| 1004 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1005 | if (curNum <= 0) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1006 | curNum = 1; |
| 1007 | curLine = lines.next; |
| 1008 | } |
| 1009 | |
| 1010 | if (num == curNum) |
| 1011 | return curLine; |
| 1012 | |
| 1013 | lp = curLine; |
| 1014 | lnum = curNum; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1015 | if (num < (curNum / 2)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1016 | lp = lines.next; |
| 1017 | lnum = 1; |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 1018 | } else if (num > ((curNum + lastNum) / 2)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1019 | lp = lines.prev; |
| 1020 | lnum = lastNum; |
| 1021 | } |
| 1022 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1023 | while (lnum < num) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1024 | lp = lp->next; |
| 1025 | lnum++; |
| 1026 | } |
| 1027 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1028 | while (lnum > num) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1029 | lp = lp->prev; |
| 1030 | lnum--; |
| 1031 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1032 | return lp; |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | /* |
| 1037 | * Set the current line number. |
| 1038 | * Returns TRUE if successful. |
| 1039 | */ |
| 1040 | static int setCurNum(int num) |
| 1041 | { |
| 1042 | LINE *lp; |
| 1043 | |
| 1044 | lp = findLine(num); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1045 | if (lp == NULL) |
| 1046 | return FALSE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1047 | curNum = num; |
| 1048 | curLine = lp; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1049 | return TRUE; |
| 1050 | } |