dtcm: Coverity 89670, 88380 and 88201
[oweals/cde.git] / cde / programs / dtprintinfo / sym2num
1 #! /bin/ksh
2 #
3 # file: sym2num
4 #
5 # purpose: 1. to create a header file containing the identifiers
6 #             and replacement list consisting of the symbols used
7 #             in a message set as well as the messages themselves.
8 #
9 #          2. to provide output to standard out which can be used
10 #             by gencat.
11 #
12
13 (( $# != 2 )) && { print "usage: sym2num <symbol name> <source file>" ;\
14                  exit 1 ; }
15
16 SYMBOL_NAME=$1
17 CAP_SYMBOL_NAME=`echo $1 | tr '[:lower:]' '[:upper:]'`  # capitalized symbol
18 SOURCE_FILE=$2
19 inc_file=${SYMBOL_NAME}_msg.h     # include file
20
21 #### Step 1: Create a header file ####
22 # write first five lines to "<symbolname>_msg.h"
23 cat <<! > ${inc_file}
24 #ifndef _H_${CAP_SYMBOL_NAME}_MSG
25 #define _H_${CAP_SYMBOL_NAME}_MSG
26 #include <limits.h>
27 #include <nl_types.h>
28 #define MF_${CAP_SYMBOL_NAME} "${SYMBOL_NAME}.cat"
29
30
31
32 /* The following was generated from ${SOURCE_FILE}. */
33 !
34
35 # locate all lines which start with "$set", and then increment 
36 # the set counter and reset the message counter to zero.
37 awk '/^\$set/ { SET_NAME=$2; SET_COUNT++; MES_COUNT=0;  
38        printf( "\n/* definitions for set %s */\n", SET_NAME ); 
39        printf( "#define %s %s\n\n", SET_NAME, SET_COUNT) }   #to be continued
40
41 # find all lines which begin with a combination of letters, numbers
42 # and underscores followed by spaces and then a quotation mark,
43 # and increment the message counter. 
44 /^[0-9A-Za-z_]+[ |\t]*\"/ { MES_NAME=$1; MES_COUNT++; 
45       printf( "#define %s %s\n", MES_NAME, MES_COUNT) }
46       END {print "#define LAST_MSG_NO "MES_COUNT"\n\n#endif"}' \
47        ${SOURCE_FILE}  >> ${inc_file}
48
49 #### Step 2: Create input to gencat ####       
50 # cat the definition statements of the include file and cat them to
51 # the source message file.  The awk file only creates define statements
52 # and blank lines.  The output should be sent to the preprocessor.
53 # There are two differences between sym2num and mkcatdefs: sym2num
54 # does not create a $delset line, and sym2num converts all symbols
55 # to numbers, even those in comment statements. 
56 if [[ -f /usr/ccs/lib/cpp ]]   #IBM & SUN
57 then
58    cpp_path=/usr/ccs/lib       
59 elif [[ -f /usr/libexec/cpp ]] #BSD
60 then
61    cpp_path=/usr/libexec
62 elif [[ -f /usr/bin/cpp ]]     #BSD
63 then
64    cpp_path=/usr/bin
65 elif [[ -f /lib/cpp ]]         #HP
66 then
67    cpp_path=/lib
68 fi
69    ( cat ${inc_file} | sed -n /define/p ; cat ${SOURCE_FILE} ) | \
70    ${cpp_path}/cpp -P | sed -e '/^$/d' -e 's/^\$$/\$ /' \
71     -e 's/\"\"$/\"/' -e 's/XDQUOTE/\"/' -e "s/XSQUOTE/\'/" \
72     -e 's/^\([1-9][0-9]*\)[ ]*[\"]*\(\"\)\(.*\)/\1 \2\3/'
73