File: protoize.sh.in

package info (click to toggle)
adesklets 0.6.1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,044 kB
  • ctags: 1,325
  • sloc: ansic: 5,939; sh: 3,983; python: 2,344; perl: 333; makefile: 211; yacc: 80; lex: 42
file content (220 lines) | stat: -rw-r--r-- 8,139 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#! /bin/bash 
#
# protoize.sh version @VERSION@
#
#-------------------------------------------------------------------------------
# Copyright (C) 2004, 2005, 2006 Sylvain Fourmanoit <syfou@users.sourceforge.net>
#  
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in
# all copies of the Software and its documentation and acknowledgment shall be
# given in the documentation and software packages that this Software was
# used.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
#
#-------------------------------------------------------------------------------
# Script used as a base to generate prototypes for all the commands
# of the @PACKAGE@ interpreter by parsing the code and looking
# at installed Imlib2 headers.
#
# To be precise, only the file C function adesklets_events_loop() in 
# ../src/adesklets.c is directly parsed by this script. Some support 
# is also requested from the script ../src/command_enum.sh, that reads 
# the COMMANDS array from ../src/command.c. This script look for
# commands prototypes in form of commented declaration in each case
# clause of the switch(command.type) block from adesklets_events_loop().
# Those declarations have to be located precisely one line below each 
# new case. They may be multi-lines, and should look like this valid example:
#
# case CMD_SOME_COMMAND:
#   /* a pseudo prototype declaration for command 'some_command'
#
#      prototype(int x, 
#                int y) */
#
# Output is a tab-separated three columns of the form:
#
# command_name	help_string	c_like_prototype
#
# c_like_prototypes follow these conventions:
#
# - All ansi-style C prototypes are considered valid.
#   If you prefer, our prototypes set is a superset of all valids C prototypes
# - Therefore, arrays and strings should always be given as pointers 
#   (asterix notation), and not with the [] or & notations, 
#   as in plain old ansi C.
# - the [] notation encloses all arguments when the command can both be
#   invoked 'as is' or with arguments, such as:
#	prototype([int optional_1, char * optionnal_2])
# - All enumerated arguments such as in 'prototype(enum SOME_ENUM arg)'
#   should be declared as 'const char * SOME_ENUM[]' in ../src/command.c,
#   so that ./enums.sh get a chance to build appropriate enumerations.
#   Look at this script for details.
# - All commands without arguments should have 'prototype(void)' as prototypes,
#   and not prototype().
# - All commands having the prototype 'prototype(voidvoid)' will be
#   silently dropped by this script, and no prototypes will be emitted.
#
# Warning: no attempt whatsoever, except for the last point of the previous 
# list, will ever be made by ./protoize.sh to enforce the syntax above:
# prototypes will only get copied, regardless of their content.
# Nevertheless, prototypes will be expected to follow these rules
# to the letter by higher level scripts responsible to generate front-ends.
# 
# Note: Although this script _should_ be portable across UNIXes, 
# it has not been tested (successfully, of course) on anything but:
#
#       - GNU Bourne-Again Shell (GNU bash) 3.00.0 and
#         NetBSD Bourne Shell (ash) 1.6.0
#       - GNU calculator language (GNU bc) 1.06
#
# Please also note it will not likely work without GNU sed
# (tested on GNU Streaming EDitor - GNU sed - 4.0.9 ), since
# it makes use of many GNU extensions (characters classes, \U, etc).
#
# This script is quite lenghty (and slow!) for the task at hand because
# it was written to depend only on these three programs - a low-end 
# POSIX compliant shell (support for functions, test, but not arithmetic), 
# any version of bc and GNU sed.
#
#-------------------------------------------------------------------------------
loop_feed () { sed -n '/switch(command.type)/,/default:/p' ../src/adesklets.c ;}

# Get all imlib2's prototypes
#
IMLIB2=`@CPP@ @IMLIB2_CFLAGS@ protoize.h 2> /dev/null`
if test "x$IMLIB2" = "x" ; then
echo "Could not retrieve imlib2 prototypes." 1>&2
exit 1
fi

# Get all closing parentheses in imlib2's prototypes
#
IMLIB2_CLOSING=`echo "$IMLIB2" | sed -n '/)/='`

# Define function to find first line greater or equal with
# a matching closed parenthese.
imlib2_close_line() {
    for LINE in $IMLIB2_CLOSING ; do
	if test "$LINE" -ge "$1" ; then
	    echo "$LINE"
	    break
	fi
    done
}

# Get all @PACKAGE@ commands
#
COMMANDS=`cd ../src; ./command_enum.sh --with-help 2> /dev/null`
if test "x$COMMANDS" = "x" ; then
echo "Could not retrieve @PACKAGE@ commands names." 1>&2
exit 1
fi

# Read and process adesklets_events_loop():
# - get all cases lines
#
CASES=`loop_feed | sed -n '/case[[:space:]]\+CMD_/ ='`
if test "x$CASES" = "x" ; then
echo "adesklets events loop reading problem" 1>&2
exit 1
fi

# - get all comments lines 
#
COMMENTS=`loop_feed | sed -n '/\/\*/=;/\*\//='`

# - Prepare functions to get comments immediately below 
#   a case declaration */
#
commented_internal() {
    if test "x$1" != "x" ; then
	echo "${COMMENTS}" | sed -n '/^'`echo "$1 + 1" | bc`'$/=' | sed -n '1p'
    fi
}

commented() {
    NEXT_LINE=`commented_internal $1`
    if test "x$NEXT_LINE" != "x" ; then
	echo "$COMMENTS" | sed -n "$NEXT_LINE"',+1p'
	NEXT_LINE=
	return 0;
    else
	return 1;
    fi
}

# - go through all cases one by one
#
#
for CASE in $CASES ; do
    PROTO=
    
    # Get command string for the case
    #
    CASE_STR=`loop_feed | sed -n "$CASE p" | \
              sed 's/^.*case//;s/[[:space:]]//;s/:[[:space:]]*//'`

    # First, try to get prototype from event loop
    if commented $CASE 1> /dev/null ; then
	SEDER=`echo \`commented $CASE\` | sed 's/ /,/;s/$/p/'`
	PROTO=`loop_feed | sed -n "$SEDER"`
	CASE_LINE=`echo "$PROTO" | \
                     sed -n '/[[:space:]]\+prototype[[:space:]]*(/='`
	if test "x$CASE_LINE" != "x" ; then
	    # There is a prototype: extract it 
	    SEDER="$CASE_LINE,`echo "$PROTO" | \
                               sed -n "$CASE_LINE"',$p' | sed -n '/)/='`p"
	    PROTO=`echo "$PROTO" | sed -n "$SEDER"`
	else
	    PROTO=
	fi    
    fi

    # If it does not work, try to get it from imlib2
    if test "x$PROTO" = "x" ; then
    	# 'imlibize' the command string it
    	#
	IMLIB2_STR=`echo $CASE_STR | sed 's/^CMD/imlib/;s/.\+/\L&/'`

    	# try to match it against Imlib2 prototypes
    	#
    	IMLIB2_LINE=`echo "$IMLIB2" | \
                     sed -n '/[^[:alnum:]_]'"$IMLIB2_STR"'[[:space:]]*(/='`
	if test "x$IMLIB2_LINE" != "x" ; then
		# There is a match : Get prototype
		SEDER="$IMLIB2_LINE,"`imlib2_close_line $IMLIB2_LINE`'p'
		PROTO=`echo "$IMLIB2" | sed -n "$SEDER"`
    	fi
    fi
    
    # Finally, filter out the prototype, or print warning message
    # if not found
    if test "x$PROTO" != "x" ; then
	PROTO=`echo "$PROTO" | \
               sed 's/[^(]*(//;s/)[^)]*//;s/^[[:space:]]\+//;s/[[:space:]]\+/ /' | \
               sed -n 'H;${G;x;s/\n//g;p}' | \
               sed 's/Imlib[[:alnum:]_]\+/int/'`
	COMMAND_NAME=`echo "$CASE_STR" | sed 's/CMD_//;s/.*/\L&/'` 
	COMMAND_HELP=`echo "$COMMANDS" | sed -n '/^'"$CASE_STR"'\t/ {s/^.*\t//;p}'`
	test "x`echo "$PROTO" | sed 's/^voidvoid$//'`" != "x" && \
		echo -e "$COMMAND_NAME\t$COMMAND_HELP\t$PROTO"
    else
	echo "error: there is not prototype for '$CASE_STR'" 1>&2
	exit 1
    fi
done

exit 0