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