Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * |
Rob Landley | 6a9e5b4 | 2008-01-10 14:34:15 -0600 | [diff] [blame] | 3 | * sed.c - Stream editor. |
| 4 | * |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 5 | * Copyright 2008 Rob Landley <rob@landley.net> |
| 6 | * |
Rob Landley | 6a9e5b4 | 2008-01-10 14:34:15 -0600 | [diff] [blame] | 7 | * See http://www.opengroup.org/onlinepubs/009695399/utilities/sed.c |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 8 | |
Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 9 | USE_SED(NEWTOY(sed, "irne*", TOYFLAG_BIN)) |
| 10 | |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 11 | config SED |
| 12 | bool "sed" |
| 13 | default n |
| 14 | help |
| 15 | usage: sed [-irn] {command | [-e command]...} [FILE...] |
| 16 | |
| 17 | Stream EDitor, transforms text by appling commands to each line |
| 18 | of input. |
| 19 | */ |
Rob Landley | 6a9e5b4 | 2008-01-10 14:34:15 -0600 | [diff] [blame] | 20 | |
| 21 | #include "toys.h" |
| 22 | #include "lib/xregcomp.h" |
| 23 | |
Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 24 | DEFINE_GLOBALS( |
| 25 | struct arg_list *commands; |
| 26 | ) |
| 27 | |
| 28 | #define TT this.sed |
| 29 | |
Rob Landley | 6a9e5b4 | 2008-01-10 14:34:15 -0600 | [diff] [blame] | 30 | struct sed_command { |
| 31 | // Doubly linked list of commands. |
| 32 | struct sed_command *next, *prev; |
| 33 | |
| 34 | // Regexes for s/match/data/ and /match_begin/,/match_end/command |
| 35 | regex_t *match, *match_begin, *match_end; |
| 36 | |
| 37 | // For numeric ranges ala 10,20command |
| 38 | int first_line, last_line; |
| 39 | |
| 40 | // Which match to replace, 0 for all. |
| 41 | int which; |
| 42 | |
| 43 | // s and w commands can write to a file. Slight optimization: we use 0 |
| 44 | // instead of -1 to mean no file here, because even when there's no stdin |
| 45 | // our input file would take fd 0. |
| 46 | int outfd; |
| 47 | |
| 48 | // Data string for (saicytb) |
| 49 | char *data; |
| 50 | |
| 51 | // Which command letter is this? |
| 52 | char command; |
| 53 | }; |
| 54 | |
Rob Landley | 6a9e5b4 | 2008-01-10 14:34:15 -0600 | [diff] [blame] | 55 | void sed_main(void) |
| 56 | { |
| 57 | struct arg_list *test; |
| 58 | |
| 59 | for (test = TT.commands; test; test = test->next) |
| 60 | dprintf(2,"command=%s\n",test->arg); |
| 61 | |
| 62 | printf("Hello world\n"); |
| 63 | } |