The script generates FreeBSD syscall table for FASM assembler
#!/bin/sh

# -----------------------------------------------------------------------
#
# The script generates FreeBSD syscall table for FASM assembler.
# Symbolic constants are generate in form of <syscall_name> equ <syscall_nr> <additional info>
# Examples: 
#    lseek  equ 19 ;; COMPAT
#    write  equ 4  ;; STD
# 
# -----------------------------------------------------------------------
cat /sys/kern/syscalls.master \
| egrep -e '[0-9]+\s[A-Z]+_[A-Z]+\s+[A-Z]' \
| awk 'BEGIN{FS=OFS="\t"} {t=$1; $1=tolower($2); $2="equ "t "\t ;;" ; print} ' \
| sed 's/aue_//g' \
| sed 's/{//g' \
> intermediate.s
# Fixing (commenting out) ranges like: null	equ 245-246
awk '{
  if (/[0-9]+-[0-9]+/) {
    print ";;"$0
    print $0 > "rouge" 
  }
  else if (/getdirentries std/) {
    print ";;"$0
    print $0 >> "rouge"
  }
  else {
    print $0
  }
}' "intermediate.s" > "intermediate2.s"
cat intermediate2.s | column -t > FreeBSDsyscalls.s

echo "---- Rouge entires that has been commented out ----"
cat rouge
rm rouge
rm intermediate.s
rm intermediate2.s
echo ""
echo "Your FreeBSDsyscalls.s is ready"