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 | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 92 | int ed_main(int argc ATTRIBUTE_UNUSED, 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; |
| 122 | char *endbuf, *newname, 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++) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 171 | case 'a': |
| 172 | addLines(num1 + 1); |
| 173 | break; |
| 174 | |
| 175 | case 'c': |
| 176 | deleteLines(num1, num2); |
| 177 | addLines(num1); |
| 178 | break; |
| 179 | |
| 180 | case 'd': |
| 181 | deleteLines(num1, num2); |
| 182 | break; |
| 183 | |
| 184 | case 'f': |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 185 | if (*cp && !isblank(*cp)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 186 | bb_error_msg("bad file command"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 187 | break; |
| 188 | } |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 189 | cp = skip_blank(cp); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 190 | if (*cp == '\0') { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 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; |
| 196 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 197 | newname = strdup(cp); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 198 | if (newname == NULL) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 199 | bb_error_msg("no memory for file name"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 200 | break; |
| 201 | } |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 202 | free(fileName); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 203 | fileName = newname; |
| 204 | break; |
| 205 | |
| 206 | case 'i': |
| 207 | addLines(num1); |
| 208 | break; |
| 209 | |
| 210 | case 'k': |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 211 | cp = skip_blank(cp); |
| 212 | if ((*cp < 'a') || (*cp > 'z') || cp[1]) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 213 | bb_error_msg("bad mark name"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 214 | break; |
| 215 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 216 | marks[*cp - 'a'] = num2; |
| 217 | break; |
| 218 | |
| 219 | case 'l': |
| 220 | printLines(num1, num2, TRUE); |
| 221 | break; |
| 222 | |
| 223 | case 'p': |
| 224 | printLines(num1, num2, FALSE); |
| 225 | break; |
| 226 | |
| 227 | case 'q': |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 228 | cp = skip_blank(cp); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 229 | if (have1 || *cp) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 230 | bb_error_msg("bad quit command"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 231 | break; |
| 232 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 233 | if (!dirty) |
| 234 | return; |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 235 | len = read_line_input("Really quit? ", buf, 16, NULL); |
| 236 | /* read error/EOF - no way to continue */ |
| 237 | if (len < 0) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 238 | return; |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 239 | cp = skip_blank(buf); |
| 240 | if ((*cp | 0x20) == 'y') /* Y or y */ |
| 241 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 242 | break; |
| 243 | |
| 244 | case 'r': |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 245 | if (*cp && !isblank(*cp)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 246 | bb_error_msg("bad read command"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 247 | break; |
| 248 | } |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 249 | cp = skip_blank(cp); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 250 | if (*cp == '\0') { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 251 | bb_error_msg("no file name"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 252 | break; |
| 253 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 254 | if (!have1) |
| 255 | num1 = lastNum; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 256 | if (readLines(cp, num1 + 1)) |
| 257 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 258 | if (fileName == NULL) |
| 259 | fileName = strdup(cp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 260 | break; |
| 261 | |
| 262 | case 's': |
| 263 | subCommand(cp, num1, num2); |
| 264 | break; |
| 265 | |
| 266 | case 'w': |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 267 | if (*cp && !isblank(*cp)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 268 | bb_error_msg("bad write command"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 269 | break; |
| 270 | } |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 271 | cp = skip_blank(cp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 272 | if (!have1) { |
| 273 | num1 = 1; |
| 274 | num2 = lastNum; |
| 275 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 276 | if (*cp == '\0') |
| 277 | cp = fileName; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 278 | if (cp == NULL) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 279 | bb_error_msg("no file name specified"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 280 | break; |
| 281 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 282 | writeLines(cp, num1, num2); |
| 283 | break; |
| 284 | |
| 285 | case 'z': |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 286 | switch (*cp) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 287 | case '-': |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 288 | printLines(curNum - 21, curNum, FALSE); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 289 | break; |
| 290 | case '.': |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 291 | printLines(curNum - 11, curNum + 10, FALSE); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 292 | break; |
| 293 | default: |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 294 | printLines(curNum, curNum + 21, FALSE); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | break; |
| 298 | |
| 299 | case '.': |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 300 | if (have1) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 301 | bb_error_msg("no arguments allowed"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 302 | break; |
| 303 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 304 | printLines(curNum, curNum, FALSE); |
| 305 | break; |
Bernhard Reutner-Fischer | d9ed35c | 2006-05-19 12:38:30 +0000 | [diff] [blame] | 306 | |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 307 | case '-': |
| 308 | if (setCurNum(curNum - 1)) |
| 309 | printLines(curNum, curNum, FALSE); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 310 | break; |
| 311 | |
| 312 | case '=': |
| 313 | printf("%d\n", num1); |
| 314 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 315 | case '\0': |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 316 | if (have1) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 317 | printLines(num2, num2, FALSE); |
| 318 | break; |
| 319 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 320 | if (setCurNum(curNum + 1)) |
| 321 | printLines(curNum, curNum, FALSE); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 322 | break; |
| 323 | |
| 324 | default: |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 325 | bb_error_msg("unimplemented command"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 326 | break; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
| 332 | /* |
| 333 | * Do the substitute command. |
| 334 | * The current line is set to the last substitution done. |
| 335 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 336 | static void subCommand(const char *cmd, int num1, int num2) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 337 | { |
| 338 | char *cp, *oldStr, *newStr, buf[USERSIZE]; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 339 | int delim, oldLen, newLen, deltaLen, offset; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 340 | LINE *lp, *nlp; |
| 341 | int globalFlag, printFlag, didSub, needPrint; |
| 342 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 343 | if (bad_nums(num1, num2, "substitute")) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 344 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 345 | |
| 346 | globalFlag = FALSE; |
| 347 | printFlag = FALSE; |
| 348 | didSub = FALSE; |
| 349 | needPrint = FALSE; |
| 350 | |
| 351 | /* |
| 352 | * Copy the command so we can modify it. |
| 353 | */ |
| 354 | strcpy(buf, cmd); |
| 355 | cp = buf; |
| 356 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 357 | if (isblank(*cp) || (*cp == '\0')) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 358 | bb_error_msg("bad delimiter for substitute"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 359 | return; |
| 360 | } |
| 361 | |
| 362 | delim = *cp++; |
| 363 | oldStr = cp; |
| 364 | |
| 365 | cp = strchr(cp, delim); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 366 | if (cp == NULL) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 367 | bb_error_msg("missing 2nd delimiter for substitute"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | |
| 371 | *cp++ = '\0'; |
| 372 | |
| 373 | newStr = cp; |
| 374 | cp = strchr(cp, delim); |
| 375 | |
| 376 | if (cp) |
| 377 | *cp++ = '\0'; |
| 378 | else |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 379 | cp = (char*)""; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 380 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 381 | while (*cp) switch (*cp++) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 382 | case 'g': |
| 383 | globalFlag = TRUE; |
| 384 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 385 | case 'p': |
| 386 | printFlag = TRUE; |
| 387 | break; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 388 | default: |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 389 | bb_error_msg("unknown option for substitute"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 393 | if (*oldStr == '\0') { |
| 394 | if (searchString[0] == '\0') { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 395 | bb_error_msg("no previous search string"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 396 | return; |
| 397 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 398 | oldStr = searchString; |
| 399 | } |
| 400 | |
| 401 | if (oldStr != searchString) |
| 402 | strcpy(searchString, oldStr); |
| 403 | |
| 404 | lp = findLine(num1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 405 | if (lp == NULL) |
| 406 | return; |
| 407 | |
| 408 | oldLen = strlen(oldStr); |
| 409 | newLen = strlen(newStr); |
| 410 | deltaLen = newLen - oldLen; |
| 411 | offset = 0; |
| 412 | nlp = NULL; |
| 413 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 414 | while (num1 <= num2) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 415 | offset = findString(lp, oldStr, oldLen, offset); |
| 416 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 417 | if (offset < 0) { |
| 418 | if (needPrint) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 419 | printLines(num1, num1, FALSE); |
| 420 | needPrint = FALSE; |
| 421 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 422 | offset = 0; |
| 423 | lp = lp->next; |
| 424 | num1++; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 425 | continue; |
| 426 | } |
| 427 | |
| 428 | needPrint = printFlag; |
| 429 | didSub = TRUE; |
| 430 | dirty = TRUE; |
| 431 | |
| 432 | /* |
| 433 | * If the replacement string is the same size or shorter |
| 434 | * than the old string, then the substitution is easy. |
| 435 | */ |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 436 | if (deltaLen <= 0) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 437 | memcpy(&lp->data[offset], newStr, newLen); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 438 | if (deltaLen) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 439 | memcpy(&lp->data[offset + newLen], |
| 440 | &lp->data[offset + oldLen], |
| 441 | lp->len - offset - oldLen); |
| 442 | |
| 443 | lp->len += deltaLen; |
| 444 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 445 | offset += newLen; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 446 | if (globalFlag) |
| 447 | continue; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 448 | if (needPrint) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 449 | printLines(num1, num1, FALSE); |
| 450 | needPrint = FALSE; |
| 451 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 452 | lp = lp->next; |
| 453 | num1++; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 454 | continue; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * The new string is larger, so allocate a new line |
| 459 | * structure and use that. Link it in in place of |
| 460 | * the old line structure. |
| 461 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 462 | nlp = malloc(sizeof(LINE) + lp->len + deltaLen); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 463 | if (nlp == NULL) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 464 | bb_error_msg("cannot get memory for line"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | |
| 468 | nlp->len = lp->len + deltaLen; |
| 469 | |
| 470 | memcpy(nlp->data, lp->data, offset); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 471 | memcpy(&nlp->data[offset], newStr, newLen); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 472 | memcpy(&nlp->data[offset + newLen], |
| 473 | &lp->data[offset + oldLen], |
| 474 | lp->len - offset - oldLen); |
| 475 | |
| 476 | nlp->next = lp->next; |
| 477 | nlp->prev = lp->prev; |
| 478 | nlp->prev->next = nlp; |
| 479 | nlp->next->prev = nlp; |
| 480 | |
| 481 | if (curLine == lp) |
| 482 | curLine = nlp; |
| 483 | |
| 484 | free(lp); |
| 485 | lp = nlp; |
| 486 | |
| 487 | offset += newLen; |
| 488 | |
| 489 | if (globalFlag) |
| 490 | continue; |
| 491 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 492 | if (needPrint) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 493 | printLines(num1, num1, FALSE); |
| 494 | needPrint = FALSE; |
| 495 | } |
| 496 | |
| 497 | lp = lp->next; |
| 498 | num1++; |
| 499 | } |
| 500 | |
| 501 | if (!didSub) |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 502 | bb_error_msg("no substitutions found for \"%s\"", oldStr); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
| 506 | /* |
| 507 | * Search a line for the specified string starting at the specified |
| 508 | * offset in the line. Returns the offset of the found string, or -1. |
| 509 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 510 | 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] | 511 | { |
| 512 | int left; |
| 513 | const char *cp, *ncp; |
| 514 | |
| 515 | cp = &lp->data[offset]; |
| 516 | left = lp->len - offset; |
| 517 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 518 | while (left >= len) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 519 | ncp = memchr(cp, *str, left); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 520 | if (ncp == NULL) |
| 521 | return -1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 522 | left -= (ncp - cp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 523 | if (left < len) |
| 524 | return -1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 525 | cp = ncp; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 526 | if (memcmp(cp, str, len) == 0) |
| 527 | return (cp - lp->data); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 528 | cp++; |
| 529 | left--; |
| 530 | } |
| 531 | |
| 532 | return -1; |
| 533 | } |
| 534 | |
| 535 | |
| 536 | /* |
| 537 | * Add lines which are typed in by the user. |
| 538 | * The lines are inserted just before the specified line number. |
| 539 | * The lines are terminated by a line containing a single dot (ugly!), |
| 540 | * or by an end of file. |
| 541 | */ |
| 542 | static void addLines(int num) |
| 543 | { |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 544 | int len; |
| 545 | char buf[USERSIZE + 1]; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 546 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 547 | while (1) { |
| 548 | /* Returns: |
| 549 | * -1 on read errors or EOF, or on bare Ctrl-D. |
| 550 | * 0 on ctrl-C, |
| 551 | * >0 length of input string, including terminating '\n' |
| 552 | */ |
| 553 | len = read_line_input("", buf, sizeof(buf), NULL); |
| 554 | if (len <= 0) { |
| 555 | /* Previously, ctrl-C was exiting to shell. |
| 556 | * Now we exit to ed prompt. Is in important? */ |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 557 | return; |
| 558 | } |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 559 | if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0')) |
| 560 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 561 | if (!insertLine(num++, buf, len)) |
| 562 | return; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | |
| 567 | /* |
| 568 | * Parse a line number argument if it is present. This is a sum |
| 569 | * or difference of numbers, '.', '$', 'x, or a search string. |
Bernhard Reutner-Fischer | d9ed35c | 2006-05-19 12:38:30 +0000 | [diff] [blame] | 570 | * Returns TRUE if successful (whether or not there was a number). |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 571 | * Returns FALSE if there was a parsing error, with a message output. |
| 572 | * Whether there was a number is returned indirectly, as is the number. |
| 573 | * The character pointer which stopped the scan is also returned. |
| 574 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 575 | static int getNum(const char **retcp, smallint *retHaveNum, int *retNum) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 576 | { |
| 577 | const char *cp; |
| 578 | char *endStr, str[USERSIZE]; |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 579 | int value, num; |
| 580 | smallint haveNum, minus; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 581 | |
| 582 | cp = *retcp; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 583 | value = 0; |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 584 | haveNum = FALSE; |
| 585 | minus = 0; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 586 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 587 | while (TRUE) { |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 588 | cp = skip_blank(cp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 589 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 590 | switch (*cp) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 591 | case '.': |
| 592 | haveNum = TRUE; |
| 593 | num = curNum; |
| 594 | cp++; |
| 595 | break; |
| 596 | |
| 597 | case '$': |
| 598 | haveNum = TRUE; |
| 599 | num = lastNum; |
| 600 | cp++; |
| 601 | break; |
| 602 | |
| 603 | case '\'': |
| 604 | cp++; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 605 | if ((*cp < 'a') || (*cp > 'z')) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 606 | bb_error_msg("bad mark name"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 607 | return FALSE; |
| 608 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 609 | haveNum = TRUE; |
| 610 | num = marks[*cp++ - 'a']; |
| 611 | break; |
| 612 | |
| 613 | case '/': |
| 614 | strcpy(str, ++cp); |
| 615 | endStr = strchr(str, '/'); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 616 | if (endStr) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 617 | *endStr++ = '\0'; |
| 618 | cp += (endStr - str); |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 619 | } else |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 620 | cp = ""; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 621 | num = searchLines(str, curNum, lastNum); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 622 | if (num == 0) |
| 623 | return FALSE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 624 | haveNum = TRUE; |
| 625 | break; |
| 626 | |
| 627 | default: |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 628 | if (!isdigit(*cp)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 629 | *retcp = cp; |
| 630 | *retHaveNum = haveNum; |
| 631 | *retNum = value; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 632 | return TRUE; |
| 633 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 634 | num = 0; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 635 | while (isdigit(*cp)) |
| 636 | num = num * 10 + *cp++ - '0'; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 637 | haveNum = TRUE; |
| 638 | break; |
| 639 | } |
| 640 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 641 | value += (minus ? -num : num); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 642 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 643 | cp = skip_blank(cp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 644 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 645 | switch (*cp) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 646 | case '-': |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 647 | minus = 1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 648 | cp++; |
| 649 | break; |
| 650 | |
| 651 | case '+': |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 652 | minus = 0; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 653 | cp++; |
| 654 | break; |
| 655 | |
| 656 | default: |
| 657 | *retcp = cp; |
| 658 | *retHaveNum = haveNum; |
| 659 | *retNum = value; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 660 | return TRUE; |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | |
| 666 | /* |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 667 | * Read lines from a file at the specified line number. |
| 668 | * Returns TRUE if the file was successfully read. |
| 669 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 670 | static int readLines(const char *file, int num) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 671 | { |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 672 | int fd, cc; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 673 | int len, lineCount, charCount; |
| 674 | char *cp; |
| 675 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 676 | if ((num < 1) || (num > lastNum + 1)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 677 | bb_error_msg("bad line for read"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 678 | return FALSE; |
| 679 | } |
| 680 | |
| 681 | fd = open(file, 0); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 682 | if (fd < 0) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 683 | perror(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 684 | return FALSE; |
| 685 | } |
| 686 | |
| 687 | bufPtr = bufBase; |
| 688 | bufUsed = 0; |
| 689 | lineCount = 0; |
| 690 | charCount = 0; |
| 691 | cc = 0; |
| 692 | |
| 693 | printf("\"%s\", ", file); |
| 694 | fflush(stdout); |
| 695 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 696 | do { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 697 | cp = memchr(bufPtr, '\n', bufUsed); |
| 698 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 699 | if (cp) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 700 | len = (cp - bufPtr) + 1; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 701 | if (!insertLine(num, bufPtr, len)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 702 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 703 | return FALSE; |
| 704 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 705 | bufPtr += len; |
| 706 | bufUsed -= len; |
| 707 | charCount += len; |
| 708 | lineCount++; |
| 709 | num++; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 710 | continue; |
| 711 | } |
| 712 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 713 | if (bufPtr != bufBase) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 714 | memcpy(bufBase, bufPtr, bufUsed); |
| 715 | bufPtr = bufBase + bufUsed; |
| 716 | } |
| 717 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 718 | if (bufUsed >= bufSize) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 719 | len = (bufSize * 3) / 2; |
| 720 | cp = realloc(bufBase, len); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 721 | if (cp == NULL) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 722 | bb_error_msg("no memory for buffer"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 723 | close(fd); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 724 | return FALSE; |
| 725 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 726 | bufBase = cp; |
| 727 | bufPtr = bufBase + bufUsed; |
| 728 | bufSize = len; |
| 729 | } |
| 730 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 731 | cc = safe_read(fd, bufPtr, bufSize - bufUsed); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 732 | bufUsed += cc; |
| 733 | bufPtr = bufBase; |
| 734 | |
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) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 738 | perror(file); |
| 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) { |
| 778 | perror(file); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 779 | return FALSE; |
| 780 | } |
| 781 | |
| 782 | printf("\"%s\", ", file); |
| 783 | fflush(stdout); |
| 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) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 793 | perror(file); |
| 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) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 803 | perror(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) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 833 | write(1, lp->data, lp->len); |
| 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 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 880 | newLp = malloc(sizeof(LINE) + len - 1); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 881 | if (newLp == NULL) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 882 | bb_error_msg("failed to allocate memory for line"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 883 | return FALSE; |
| 884 | } |
| 885 | |
| 886 | memcpy(newLp->data, data, len); |
| 887 | newLp->len = len; |
| 888 | |
| 889 | if (num > lastNum) |
| 890 | lp = &lines; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 891 | else { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 892 | lp = findLine(num); |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 893 | if (lp == NULL) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 894 | free((char *) newLp); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 895 | return FALSE; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | newLp->next = lp; |
| 900 | newLp->prev = lp->prev; |
| 901 | lp->prev->next = newLp; |
| 902 | lp->prev = newLp; |
| 903 | |
| 904 | lastNum++; |
| 905 | dirty = TRUE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 906 | return setCurNum(num); |
| 907 | } |
| 908 | |
| 909 | |
| 910 | /* |
| 911 | * Delete lines from the given range. |
| 912 | */ |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 913 | static void deleteLines(int num1, int num2) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 914 | { |
| 915 | LINE *lp, *nlp, *plp; |
| 916 | int count; |
| 917 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 918 | if (bad_nums(num1, num2, "delete")) |
| 919 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 920 | |
| 921 | lp = findLine(num1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 922 | if (lp == NULL) |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 923 | return; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 924 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 925 | if ((curNum >= num1) && (curNum <= num2)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 926 | if (num2 < lastNum) |
| 927 | setCurNum(num2 + 1); |
| 928 | else if (num1 > 1) |
| 929 | setCurNum(num1 - 1); |
| 930 | else |
| 931 | curNum = 0; |
| 932 | } |
| 933 | |
| 934 | count = num2 - num1 + 1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 935 | if (curNum > num2) |
| 936 | curNum -= count; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 937 | lastNum -= count; |
| 938 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 939 | while (count-- > 0) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 940 | nlp = lp->next; |
| 941 | plp = lp->prev; |
| 942 | plp->next = nlp; |
| 943 | nlp->prev = plp; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 944 | free(lp); |
| 945 | lp = nlp; |
| 946 | } |
| 947 | |
| 948 | dirty = TRUE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | |
| 952 | /* |
| 953 | * Search for a line which contains the specified string. |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 954 | * If the string is "", then the previously searched for string |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 955 | * is used. The currently searched for string is saved for future use. |
| 956 | * Returns the line number which matches, or 0 if there was no match |
| 957 | * with an error printed. |
| 958 | */ |
| 959 | static int searchLines(const char *str, int num1, int num2) |
| 960 | { |
| 961 | const LINE *lp; |
| 962 | int len; |
| 963 | |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 964 | if (bad_nums(num1, num2, "search")) |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 965 | return 0; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 966 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 967 | if (*str == '\0') { |
| 968 | if (searchString[0] == '\0') { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 969 | bb_error_msg("no previous search string"); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 970 | return 0; |
| 971 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 972 | str = searchString; |
| 973 | } |
| 974 | |
| 975 | if (str != searchString) |
| 976 | strcpy(searchString, str); |
| 977 | |
| 978 | len = strlen(str); |
| 979 | |
| 980 | lp = findLine(num1); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 981 | if (lp == NULL) |
| 982 | return 0; |
| 983 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 984 | while (num1 <= num2) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 985 | if (findString(lp, str, len, 0) >= 0) |
| 986 | return num1; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 987 | num1++; |
| 988 | lp = lp->next; |
| 989 | } |
| 990 | |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 991 | bb_error_msg("cannot find string \"%s\"", str); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | |
| 996 | /* |
| 997 | * Return a pointer to the specified line number. |
| 998 | */ |
| 999 | static LINE *findLine(int num) |
| 1000 | { |
| 1001 | LINE *lp; |
| 1002 | int lnum; |
| 1003 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1004 | if ((num < 1) || (num > lastNum)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 1005 | bb_error_msg("line number %d does not exist", num); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1006 | return NULL; |
| 1007 | } |
| 1008 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1009 | if (curNum <= 0) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1010 | curNum = 1; |
| 1011 | curLine = lines.next; |
| 1012 | } |
| 1013 | |
| 1014 | if (num == curNum) |
| 1015 | return curLine; |
| 1016 | |
| 1017 | lp = curLine; |
| 1018 | lnum = curNum; |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1019 | if (num < (curNum / 2)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1020 | lp = lines.next; |
| 1021 | lnum = 1; |
Denis Vlasenko | d9391b1 | 2007-09-25 11:55:57 +0000 | [diff] [blame] | 1022 | } else if (num > ((curNum + lastNum) / 2)) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1023 | lp = lines.prev; |
| 1024 | lnum = lastNum; |
| 1025 | } |
| 1026 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1027 | while (lnum < num) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1028 | lp = lp->next; |
| 1029 | lnum++; |
| 1030 | } |
| 1031 | |
Denis Vlasenko | 610c4aa | 2006-11-30 20:57:50 +0000 | [diff] [blame] | 1032 | while (lnum > num) { |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1033 | lp = lp->prev; |
| 1034 | lnum--; |
| 1035 | } |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1036 | return lp; |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | /* |
| 1041 | * Set the current line number. |
| 1042 | * Returns TRUE if successful. |
| 1043 | */ |
| 1044 | static int setCurNum(int num) |
| 1045 | { |
| 1046 | LINE *lp; |
| 1047 | |
| 1048 | lp = findLine(num); |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1049 | if (lp == NULL) |
| 1050 | return FALSE; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1051 | curNum = num; |
| 1052 | curLine = lp; |
Rob Landley | 3b89039 | 2006-05-04 20:56:43 +0000 | [diff] [blame] | 1053 | return TRUE; |
| 1054 | } |