aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/blog/2022-06-12-shell-ide-sed/shell-ide-sed.md125
-rw-r--r--src/blog/blog.md1
-rw-r--r--src/blog/feed.xml7
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
3Recently I have been working on [nissy](https://nissy.tronto.net), my
4Rubik's cube solver written in C. It is a faily large project for me,
5consisting of multiple files for a total of ~8k lines.
6
7Something that I need to do quite often is quickly checking a structure's
8or a function's definition. Using a simple text editor without
9any 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
18This is not too bad, but I can do better using a short sed script!
19
20## Coding style
21
22I write my functions like this:
23
24```
25static int
26do_thing(int var)
27{
28 function body...
29}
30```
31
32The important part is that the function's name is at the start of the line.
33In 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
35is fairly standard across UNIX tools (sed, grep, ed...), so all other
36uses of the funciton in the same file are ignored.
37The other important thing is that the closing `}` is also at the beginning
38of the line, but everyone in their right mind does that (I hope).
39
40## The sed command
41
42If you are like me, 99% of the time you use sed it is to replace some text
43with something like `sed 's/old text/new text/g`.
44But this classic program can actually do more: it parses the input line by
45line, applies a series of commands to each line that matches the given
46address, and then prints the result. For example
47
48```
49$ sed '5,10 p' file.c
50```
51
52prints (`p`) the lines from 5 to 10 of `file.c`. Well, kind of: it prints
53the whole file, but the lines from 5 to 10 are duplicated. This is because the
54default 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
61To print our `do_thing` function, we can find its address in the file using
62the `/` search. The following command:
63
64```
65$ sed -n '/^do_/,/^}/ p' file.c
66```
67
68prints all the lines between one that starts with `/^do_/` and the first
69one 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
74Of course typing all of this every time we want to check out a function from
75our file is too complicated. So we want to turn this into a script that we
76can easily call.
77We will call it `cth`, for "see thing', where the `c` also reminds us
78that it is based on C's syntax.
79
80We may start with something like this:
81
82```
83#!/bin/sh
84
85sed -n "/^$1/,/^}/ p"
86```
87
88Using double quotes instead of single quotes is necessary to have the `$1`
89expand to the first argument. After saving our script to `cth` and making
90it executable with `chmod +x cth`, we can call it with
91
92```
93$ ./cth do_ < file.c
94```
95
96We have to use `<` to redirect the standard input, because our script does
97not read any other argument that could be interpreted as a file name.
98To do this, we can do:
99
100
101```
102#!/bin/sh
103
104name=$1
105shift
106sed -n "/^$name/,/^}/ p" $@
107```
108
109This will save the first argument to a variable called `name`, "shift" the
110list of arguments and pass every remaining argument to sed with `$@`. In this
111way we can pass any number of file names. For example if we know our file
112is 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
119And it works! You can now find
120[this script](https://git.tronto.net/scripts/file/cth%2Ehtml)
121among my other [scripts](https://git.tronto.net/scripts).
122
123*Remark: in bash one can simply do `sed -n "/^$1/,/^}/ p" ${@:2}` to match
124every argument from the second to the last, but this does not work in other
125shells 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>

Generated with cgit - Back to sebastiano.tronto.net