diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2022-06-12 18:57:21 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2022-06-12 18:57:21 +0200 |
| commit | b68ca27ff61cc5435fcee75786464e5a6d2242f7 (patch) | |
| tree | a963c01055d3812f2024bfc969817506911279fe /src | |
| parent | 28c6b9090727cc7e9eda7c6a7d8cefdfc02ef6ce (diff) | |
| download | sebastiano.tronto.net-b68ca27ff61cc5435fcee75786464e5a6d2242f7.tar.gz sebastiano.tronto.net-b68ca27ff61cc5435fcee75786464e5a6d2242f7.zip | |
Added blog post
Diffstat (limited to 'src')
| -rw-r--r-- | src/blog/2022-06-12-shell-ide-sed/shell-ide-sed.md | 125 | ||||
| -rw-r--r-- | src/blog/blog.md | 1 | ||||
| -rw-r--r-- | src/blog/feed.xml | 7 |
3 files changed, 133 insertions, 0 deletions
diff --git a/src/blog/2022-06-12-shell-ide-sed/shell-ide-sed.md b/src/blog/2022-06-12-shell-ide-sed/shell-ide-sed.md new file mode 100644 index 0000000..2834253 --- /dev/null +++ b/src/blog/2022-06-12-shell-ide-sed/shell-ide-sed.md | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | # The UNIX shell as an IDE: lookup stuff sed | ||
| 2 | |||
| 3 | Recently I have been working on [nissy](https://nissy.tronto.net), my | ||
| 4 | Rubik's cube solver written in C. It is a faily large project for me, | ||
| 5 | consisting of multiple files for a total of ~8k lines. | ||
| 6 | |||
| 7 | Something that I need to do quite often is quickly checking a structure's | ||
| 8 | or a function's definition. Using a simple text editor without | ||
| 9 | any plugin, my workflow for this at the moment is the following: | ||
| 10 | |||
| 11 | * Open a new terminal in the project's directory (one key-binding in | ||
| 12 | my terminal's configuration) | ||
| 13 | * Open the correct file with `vi src/file.c` (this can be tricky, | ||
| 14 | because I don't always remember in which file the object I am looking | ||
| 15 | for is defined) | ||
| 16 | * Search with `/` | ||
| 17 | |||
| 18 | This is not too bad, but I can do better using a short sed script! | ||
| 19 | |||
| 20 | ## Coding style | ||
| 21 | |||
| 22 | I write my functions like this: | ||
| 23 | |||
| 24 | ``` | ||
| 25 | static int | ||
| 26 | do_thing(int var) | ||
| 27 | { | ||
| 28 | function body... | ||
| 29 | } | ||
| 30 | ``` | ||
| 31 | |||
| 32 | The important part is that the function's name is at the start of the line. | ||
| 33 | In this way when I search for the function's definition I can type | ||
| 34 | `/^do_thing`. Here `^` stands for the beginning of the line, and it | ||
| 35 | is fairly standard across UNIX tools (sed, grep, ed...), so all other | ||
| 36 | uses of the funciton in the same file are ignored. | ||
| 37 | The other important thing is that the closing `}` is also at the beginning | ||
| 38 | of the line, but everyone in their right mind does that (I hope). | ||
| 39 | |||
| 40 | ## The sed command | ||
| 41 | |||
| 42 | If you are like me, 99% of the time you use sed it is to replace some text | ||
| 43 | with something like `sed 's/old text/new text/g`. | ||
| 44 | But this classic program can actually do more: it parses the input line by | ||
| 45 | line, applies a series of commands to each line that matches the given | ||
| 46 | address, and then prints the result. For example | ||
| 47 | |||
| 48 | ``` | ||
| 49 | $ sed '5,10 p' file.c | ||
| 50 | ``` | ||
| 51 | |||
| 52 | prints (`p`) the lines from 5 to 10 of `file.c`. Well, kind of: it prints | ||
| 53 | the whole file, but the lines from 5 to 10 are duplicated. This is because the | ||
| 54 | default behavior, applied to every line, is to do nothing and print the | ||
| 55 | (unmodified) input. We can change this behavior with the `-n` option: | ||
| 56 | |||
| 57 | ``` | ||
| 58 | $ sed -n '5,10 p' file.c | ||
| 59 | ``` | ||
| 60 | |||
| 61 | To print our `do_thing` function, we can find its address in the file using | ||
| 62 | the `/` search. The following command: | ||
| 63 | |||
| 64 | ``` | ||
| 65 | $ sed -n '/^do_/,/^}/ p' file.c | ||
| 66 | ``` | ||
| 67 | |||
| 68 | prints all the lines between one that starts with `/^do_/` and the first | ||
| 69 | one after that that starts with `}`. If you have another function called | ||
| 70 | `do_other_thing`, it will print that one too. | ||
| 71 | |||
| 72 | ## Turning it into a script | ||
| 73 | |||
| 74 | Of course typing all of this every time we want to check out a function from | ||
| 75 | our file is too complicated. So we want to turn this into a script that we | ||
| 76 | can easily call. | ||
| 77 | We will call it `cth`, for "see thing', where the `c` also reminds us | ||
| 78 | that it is based on C's syntax. | ||
| 79 | |||
| 80 | We may start with something like this: | ||
| 81 | |||
| 82 | ``` | ||
| 83 | #!/bin/sh | ||
| 84 | |||
| 85 | sed -n "/^$1/,/^}/ p" | ||
| 86 | ``` | ||
| 87 | |||
| 88 | Using double quotes instead of single quotes is necessary to have the `$1` | ||
| 89 | expand to the first argument. After saving our script to `cth` and making | ||
| 90 | it executable with `chmod +x cth`, we can call it with | ||
| 91 | |||
| 92 | ``` | ||
| 93 | $ ./cth do_ < file.c | ||
| 94 | ``` | ||
| 95 | |||
| 96 | We have to use `<` to redirect the standard input, because our script does | ||
| 97 | not read any other argument that could be interpreted as a file name. | ||
| 98 | To do this, we can do: | ||
| 99 | |||
| 100 | |||
| 101 | ``` | ||
| 102 | #!/bin/sh | ||
| 103 | |||
| 104 | name=$1 | ||
| 105 | shift | ||
| 106 | sed -n "/^$name/,/^}/ p" $@ | ||
| 107 | ``` | ||
| 108 | |||
| 109 | This will save the first argument to a variable called `name`, "shift" the | ||
| 110 | list of arguments and pass every remaining argument to sed with `$@`. In this | ||
| 111 | way we can pass any number of file names. For example if we know our file | ||
| 112 | is in the `src` directory, but we do not remember what its name is, we can | ||
| 113 | [glob](https://en.wikipedia.org/wiki/Glob_(programming)) it with: | ||
| 114 | |||
| 115 | ``` | ||
| 116 | $ cth do_ src/* | ||
| 117 | ``` | ||
| 118 | |||
| 119 | And it works! You can now find | ||
| 120 | [this script](https://git.tronto.net/scripts/file/cth%2Ehtml) | ||
| 121 | among my other [scripts](https://git.tronto.net/scripts). | ||
| 122 | |||
| 123 | *Remark: in bash one can simply do `sed -n "/^$1/,/^}/ p" ${@:2}` to match | ||
| 124 | every argument from the second to the last, but this does not work in other | ||
| 125 | shells such as ksh.* | ||
diff --git a/src/blog/blog.md b/src/blog/blog.md index 18b1fbe..9309682 100644 --- a/src/blog/blog.md +++ b/src/blog/blog.md | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | [RSS Feed](feed.xml) | 3 | [RSS Feed](feed.xml) |
| 4 | 4 | ||
| 5 | * 2022-06-12 [The UNIX shell as an IDE: lookup stuff sed](2022-06-12-shell-ide-sed) | ||
| 5 | * 2022-06-08 [The man page reading club: more(1)](2022-06-08-more) | 6 | * 2022-06-08 [The man page reading club: more(1)](2022-06-08-more) |
| 6 | * 2022-06-04 [The gemini protocol](2022-06-04-gemini) | 7 | * 2022-06-04 [The gemini protocol](2022-06-04-gemini) |
| 7 | * 2022-05-29 [The man page reading club: man(1)](2022-05-29-man) | 8 | * 2022-05-29 [The man page reading club: man(1)](2022-05-29-man) |
diff --git a/src/blog/feed.xml b/src/blog/feed.xml index d84e8d5..0a3c845 100644 --- a/src/blog/feed.xml +++ b/src/blog/feed.xml | |||
| @@ -9,6 +9,13 @@ Thoughts about software, computers and whatever I feel like sharing | |||
| 9 | </description> | 9 | </description> |
| 10 | 10 | ||
| 11 | <item> | 11 | <item> |
| 12 | <title>The UNIX shell as an IDE: lookup stuff sed</title> | ||
| 13 | <link>https://sebastiano.tronto.net/blog/2022-06-12-shell-ide-sed</link> | ||
| 14 | <description>The UNIX shell as an IDE: lookup stuff sed</description> | ||
| 15 | <pubDate>2022-06-12</pubDate> | ||
| 16 | </item> | ||
| 17 | |||
| 18 | <item> | ||
| 12 | <title>The man page reading club: more(1)</title> | 19 | <title>The man page reading club: more(1)</title> |
| 13 | <link>https://sebastiano.tronto.net/blog/2022-06-08-more</link> | 20 | <link>https://sebastiano.tronto.net/blog/2022-06-08-more</link> |
| 14 | <description>The man page reading club: more(1)</description> | 21 | <description>The man page reading club: more(1)</description> |
