#!/bin/awk -f # # Written by Jerry James # January 20, 2004 # # Produce a list of Lisp function and variable definitions in a C source file. # The list has elements of the following format: # # source_file DEFUN foo (arg1 &optional arg2 arg3) # source_file DEFUN bar (arg1 &rest arg2) # source_file DEFVAR baz # # Usage: awk -f builtin-defs.awk file1 ... # Save the original field and record splitters and set some defaults BEGIN { OrigRS = RS; OrigFS = FS; skip_docstring = 0; get_arguments = 0; } # Get the names of the arguments to a function get_arguments { FS = OrigFS; get_arguments = 0; arglist = FILENAME " " func_type " " func_name " ("; for (required = 2; required <= min_args + 1; required++) arglist = arglist $required " "; if (max_args > min_args) { arglist = arglist "&optional "; for (optional = min_args + 2; optional <= max_args + 1; optional++) arglist = arglist $optional " "; } ## Strip off the trailing space if (max_args > 0) arglist = substr(arglist, 1, length(arglist) - 1); print arglist ")"; } # Skip over a function docstring skip_docstring { RS = OrigRS; FS = "[\(\),[:space:]]+"; skip_docstring = 0; get_arguments = 1; ## Skip over the whitespace following a docstring getline; } # Find function definitions /^DEFUN/ { ## Get the name of the function and the number of arguments split($0, arguments, "[\(,[:blank:]]+"); func_type = arguments[1]; func_name = substr(arguments[2], 2, length(arguments[2]) - 2); if (NF >= 6) { min_args = arguments[5]; max_args = arguments[6]; } else if (NF == 5) { min_args = arguments[5]; getline; split($0, arguments, "[\(,[:blank:]]+"); max_args = arguments[1]; } else { oldNF = NF; getline; split($0, arguments, "[\(,[:blank:]]+"); min_args = arguments[6 - oldNF]; max_args = arguments[7 - oldNF]; } if (max_args ~ "MANY") { printf("%s %s %s (", FILENAME, func_type, func_name); for (required = 1; required <= min_args; required++) { printf("arg%d ", required); } printf("&rest rest_arg)\n"); } else { ## Now set up to grab the argument list. We have to skip the subsequent ## docstring, then grab the parenthesized list after that. skip_docstring = 1; RS = ".\"\)"; } } # Find variable definitions /^[[:blank:]]*DEFVAR/ { ## Get the name and type of the variable split($2, varname, "\""); printf("%s %s %s\n", FILENAME, $1, varname[2]); }