summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-01-09 00:47:50 -0500
committerJune McEnroe <june@causal.agency>2021-01-09 00:47:50 -0500
commit27fdb43e71854a18bd02e7f937b83c1fb7567849 (patch)
tree7317149208a37d66cd235a0bfa10e4ceaa313db3 /bin
parentUpdate tagline (diff)
downloadsrc-27fdb43e71854a18bd02e7f937b83c1fb7567849.tar.gz
src-27fdb43e71854a18bd02e7f937b83c1fb7567849.zip
Add c -t flag to print expression type
Also add missing float case.
Diffstat (limited to 'bin')
-rw-r--r--bin/c.sh33
-rw-r--r--bin/man1/c.17
2 files changed, 38 insertions, 2 deletions
diff --git a/bin/c.sh b/bin/c.sh
index f1143fd3..4439aaee 100644
--- a/bin/c.sh
+++ b/bin/c.sh
@@ -29,10 +29,12 @@ cat >&3 <<EOF
 EOF
 
 expr=
-while getopts 'e:i:' opt; do
+type=
+while getopts 'e:i:t' opt; do
 	case "${opt}" in
 		(e) expr=$OPTARG;;
 		(i) echo "#include <${OPTARG}>" >&3;;
+		(t) type=1;;
 		(?) exit 1;;
 	esac
 done
@@ -45,6 +47,34 @@ int main(int argc, char *argv[]) {
 	$*;
 EOF
 
+if [ -n "${type}" ]; then
+	cat >&3 <<EOF
+	printf(
+		_Generic(
+			${expr},
+			char: "(char) ",
+			char *: "(char *) ",
+			const char *: "(const char *) ",
+			wchar_t *: "(wchar_t *) ",
+			const wchar_t *: "(const wchar_t *) ",
+			signed char: "(signed char) ",
+			short: "(short) ",
+			int: "(int) ",
+			long: "(long) ",
+			long long: "(long long) ",
+			unsigned char: "(unsigned char) ",
+			unsigned short: "(unsigned short) ",
+			unsigned int: "(unsigned int) ",
+			unsigned long: "(unsigned long) ",
+			unsigned long long: "(unsigned long long) ",
+			float: "(float) ",
+			double: "(double) ",
+			default: "(void *) "
+		)
+	);
+EOF
+fi
+
 if [ -n "${expr}" ]; then
 	cat >&3 <<EOF
 	printf(
@@ -65,6 +95,7 @@ if [ -n "${expr}" ]; then
 			unsigned int: "%u\n",
 			unsigned long: "%lu\n",
 			unsigned long long: "%llu\n",
+			float: "%g\n",
 			double: "%g\n",
 			default: "%p\n"
 		),
diff --git a/bin/man1/c.1 b/bin/man1/c.1
index 3ae10945..97384ebe 100644
--- a/bin/man1/c.1
+++ b/bin/man1/c.1
@@ -1,4 +1,4 @@
-.Dd May 31, 2020
+.Dd January  9, 2021
 .Dt C 1
 .Os
 .
@@ -8,6 +8,7 @@
 .
 .Sh SYNOPSIS
 .Nm
+.Op Fl t
 .Op Fl e Ar expr
 .Op Fl i Ar include
 .Op Ar stmts ...
@@ -37,4 +38,8 @@ after executing
 .It Fl i Ar include
 Add the include file
 .Ar include .
+.It Fl t
+With
+.Fl e ,
+print the type of the expression.
 .El