source: git/factory/fex/runfex @ 085370c

spielwiese
Last change on this file since 085370c was 085370c, checked in by Jens Schmidt <schmidt@…>, 26 years ago
***** merge from branch `factory-gcd' to main trunk git-svn-id: file:///usr/local/Singular/svn/trunk@1209 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100755
File size: 10.4 KB
Line 
1#! /home/schmidt/bin/bash
2# $Id: runfex,v 1.6 1998-03-11 16:11:28 schmidt Exp $
3
4#{{{ docu
5#
6# runfex - run factory example collection.
7#
8#}}}
9
10set -o nounset
11set -o noglob
12
13#{{{ global constants
14#{{{ docu
15#
16# - global constants.
17#
18# ExecName: used to prefix error messages.  Used by warn().
19# XXColWidth: default width for data columns.  Used by main() and
20#   example().
21# EvalAlg: algorithm to read run/collection data from.  Used by
22#   main().
23#
24#}}}
25readonly ExecName="$0"
26
27typeset -ir TBColWidth=80
28typeset -ir ROColWidth=80
29
30readonly EvalAlg="feval"
31#}}}
32
33#{{{ global variables
34#{{{ docu
35#
36# - global variables.
37#
38# runOptions, collectionOptions, runEnvironment,
39# collectionEnvironment: options and environment for the
40#   examples.  Used by runAlgorithm().
41# runConfiguration: which configurations' executables to use.
42#   Used by runAlgorithm().
43# collectionROOptions: options to print run overview.  Use by
44#   main().
45# runTBOptions, collectionTBOptions: options to print XX tables.
46#   Used by example().
47# debugMode: do not execute anything, just print what we would
48#   have executed.  Used by main(), example(), and
49#   runAlgorithm().
50#
51# The run* variables and debugMode are set in the main program,
52# the rest of the collection* variables in collection().
53#
54#}}}
55runOptions=""
56runEnvironment=""
57runTBOptions=""
58runConfiguration="opt"
59
60collectionOptions=""
61collectionEnvironment=""
62collectionTBOptions=""
63collectionROOptions=""
64
65debugMode=""
66#}}}
67
68#
69# - functions.
70#
71
72#{{{ warn ()
73#{{{ docu
74#
75# warn() - print arguments to stderr prefixed $ExecName.
76#
77# Global variables used:
78#   ExecName
79#
80#}}}
81warn()
82{
83    echo "$ExecName:" "${@-}" >&2
84}
85#}}}
86
87#{{{ runAlgorithm ()
88#{{{ docu
89#
90# runAlgorithm() - run algorithm with the correct options.
91#
92# $1: algorithm to run
93# $2: output options
94# rest: arguments to algorithm
95#
96# Global variables used:
97#   runOptions, runEnvironment, runConfiguration,
98#   collectionOptions, collectionEnvironment,
99#   exampleOptions, exampleEnvironment,
100#   debugMode
101#
102#}}}
103runAlgorithm()
104{
105    typeset \
106        algorithmName="$1" \
107        outputOptions="$2" \
108        algorithmOptions="$collectionOptions${exampleOptions-}$runOptions" \
109        algorithmEnvironment="$collectionEnvironment${exampleEnvironment-}$runEnvironment"
110    shift 2
111
112    # reset factory environemnt to exclude external influences
113    FTEST_ENV=""
114    FTEST_CIRCLE=""
115    FTEST_ALARM=""
116
117    if [ -n "$debugMode" ]; then
118        echo "calling $algorithmName.$runConfiguration" $algorithmOptions "$outputOptions" "$algorithmEnvironment" "$@"
119    else
120        "$algorithmName.$runConfiguration" $algorithmOptions "$outputOptions" "$algorithmEnvironment" "$@"
121    fi
122}
123#}}}
124
125#{{{ printData ()
126#{{{ docu
127#
128# printData() - print example/run table.
129#
130# $1: options how to print data
131# $2: default width to print data
132# $3: name of example/collection
133# $4: note on example/collection
134#
135# Global variables used:
136#   alg* algorithm information,
137#
138#}}}
139printData()
140{
141    typeset \
142        options="$1" \
143        name="$3" \
144        note="$4" \
145        colWidth
146
147    colWidth="${options//[a-z]/}"
148    colWidth="${colWidth:-$2}"
149    options="${options//[0-9]/}"
150
151    typeset -i i=0 n=0
152
153    while [ -n "$options" ]; do
154        case "$options" in
155            (*h*)
156                # do some pretty printing
157                if [ ${#name} -lt 7 ]; then
158                    echo "$name:                $note."
159                elif  [ ${#name} -lt 15 ]; then
160                    echo "$name:        $note."
161                else
162                    echo "$name: $note."
163                fi
164                 options="${options//h/}" ;;
165            (*p*) echo "$algChar"
166                 options="${options//p/}" ;;
167            (*s*) echo "$algSwitch"
168                 options="${options//s/}" ;;
169            (*v*) echo "$algVars"
170                 options="${options//v/}" ;;
171            (*n*) echo "$algCircle"
172                 options="${options//n/}" ;;
173            (*g*) echo "$algSeed"
174                 options="${options//g/}" ;;
175            (*f*) echo "$algVers"
176                 options="${options//f/}" ;;
177            (*t*) echo "$algTime"
178                 options="${options//t/}" ;;
179            (*c*) echo "$algCheck"
180                 options="${options//c/}" ;;
181            (*r*)
182                n=${#algResult[@]}
183                i=0
184                while [ $i -lt $n ]; do
185                    echo "${algResult[i]}" | fold -s -w$colWidth
186                    i=i+1
187                done
188                options="${options//r/}" ;;
189            (*)   warn "unknown output type specifier '$options' in table specification"
190                 options="" ;;
191        esac
192    done
193}
194#}}}
195
196#{{{ collection ()
197#{{{ docu
198#
199# collection() - set up collection data.
200#
201# All we do is setting the collection* variables (except
202# collectionName) from commandline.
203# We do not set collectionNote and collectionXXOptions if they
204# are already set but reset collectionOptions and
205# collectionEnvironment on each run to allow for multiple calls
206# to collection() in one example collection file (though this is
207# bad style).
208#
209# Global variables used: none
210#
211#}}}
212collection()
213{
214    collectionOptions=""
215    collectionEnvironment=""
216
217    # read collection name
218    if [ "$#" = "0" ]; then
219        warn "no collection name specified"
220        exit 1
221    fi
222    collectionName="$1"
223    shift
224
225    #{{{ read options and environment
226    # read options
227    typeset opt
228    while getopts "n:a:c:t:r:" opt; do
229        case "$opt" in
230            (n) collectionNote="${collectionNote:-$OPTARG}" ;;
231            (a) collectionOptions="$collectionOptions -a$OPTARG" ;;
232            (c) collectionOptions="$collectionOptions -c$OPTARG" ;;
233            (t) collectionTBOptions="${collectionTBOptions:-$OPTARG}" ;;
234            (r) collectionROOptions="${collectionROOptions:-$OPTARG}" ;;
235            (?) warn "bad collection option"; exit 1 ;;
236        esac
237    done
238    # shift options and reset OPTIND
239    typeset -i optind
240    optind=OPTIND-1
241    shift $optind
242    OPTIND=1
243
244    # read environment
245    while [ "${1-}" != "${1+${1#/}}" ]; do
246        collectionEnvironment="$collectionEnvironment $1"
247        shift
248    done
249    #}}}
250}
251#}}}
252
253#{{{ defineSkip ()
254#{{{ docu
255#
256# defineSkip() - define function skipExample().
257#
258#}}}
259defineSkip()
260{
261    typeset regExp=""
262    typeset notRegExp=""
263    typeset rawArg arg
264
265    for rawArg; do
266        # check for leading ^
267        arg="${rawArg#^}"
268        if [ "$rawArg" = "$arg" ]; then
269            regExp="$regExp|$arg"
270        else
271            notRegExp="$notRegExp|$arg"
272        fi
273        shift
274    done
275    regExp="${regExp#|}"
276    notRegExp="${notRegExp#|}"
277
278    if [ -z "$regExp" -a -z "$notRegExp" ]; then
279        eval "
280skipExample()
281{
282    return 1
283}
284"
285    elif [ -n "$regExp" -a -z "$notRegExp" ]; then
286        eval "
287skipExample()
288{
289    case \"\$1\" in
290        ($regExp) return 1 ;;
291        (*) return 0 ;;
292    esac
293}
294"
295    elif [ -z "$regExp" -a -n "$notRegExp" ]; then
296        eval "
297skipExample()
298{
299    case \"\$1\" in
300        ($notRegExp) return 0 ;;
301        (*) return 1 ;;
302    esac
303}
304"
305    else
306        eval "
307skipExample()
308{
309    case \"\$1\" in
310        ($notRegExp) return 0 ;;
311        ($regExp) return 1 ;;
312        (*) return 0 ;;
313    esac
314}
315"
316    fi
317}
318#}}}
319
320#{{{ example ()
321#{{{ docu
322#
323# example() - run an example.
324#
325# Global variables used:
326#   debugMode,
327#   runTBOptions,
328#   collectionTBOptions,
329#   TBColWidth
330#
331#}}}
332example()
333{
334    typeset exampleName="" \
335            exampleNote="" \
336            exampleOptions="" \
337            exampleEnvironment="" \
338            exampleTBOptions=""
339
340    typeset algorithm="" \
341            algorithmTBOptions=""
342
343    #{{{ read example name and skip it if necessary
344    if [ "$#" = "0" ]; then
345        warn "no example name specified"
346        exit 1
347    fi
348    exampleName="$1"
349    shift
350
351    if skipExample "$exampleName"; then
352        if [ -n "$debugMode" ]; then
353            echo "skipping $exampleName"
354        fi
355        return
356    else
357        warn "running $exampleName"
358    fi
359    #}}}
360   
361    #{{{ read options and environment
362    # read options
363    typeset opt
364    while getopts "n:a:c:t:x:" opt; do
365        case "$opt" in
366            (n) exampleNote="$OPTARG" ;;
367            (a) exampleOptions="$exampleOptions -a$OPTARG" ;;
368            (c) exampleOptions="$exampleOptions -c$OPTARG" ;;
369            (t) exampleTBOptions="$OPTARG" ;;
370            (?) warn "bad example option"; exit 1 ;;
371        esac
372    done
373    # shift options and reset OPTIND
374    typeset -i optind
375    optind=OPTIND-1
376    shift $optind
377    OPTIND=1
378
379    # read environment
380    while [ "${1-}" != "${1+${1#/}}" ]; do
381        exampleEnvironment="$exampleEnvironment $1"
382        shift
383    done
384    #}}}
385
386    #{{{ read rest of arguments
387    if [ "$#" = "0" ]; then
388        warn "no example specified"
389        exit 1
390    fi
391    algorithm="$1"
392    shift
393    #}}}
394
395    # check for debug mode
396    if [ -n "$debugMode" ]; then
397        runAlgorithm "$algorithm" -oa "$@"
398        return
399    fi
400
401    # collect output options
402    algorithmTBOptions="${runTBOptions:-${exampleTBOptions:-$collectionTBOptions}}"
403
404    #{{{ read and print data
405    typeset algChar \
406            algSwitch \
407            algVars \
408            algCircle \
409            algSeed \
410            algVers \
411            algTime \
412            algCheck
413   
414    typeset -a algResult
415
416    typeset -i j=0
417
418    # read data
419    runAlgorithm "$algorithm" -oa "$@" \
420        | {
421        read algChar
422        read algSwitch
423        read algVars
424        read algCircle
425        read algSeed
426        read algVers
427        read algTime
428        read algCheck
429
430        # preprocess output and store result in the arrays
431        while read line; do
432            algResult[j]="$line"
433            j=j+1
434        done
435
436        # print table
437        printData "$algorithmTBOptions" "$TBColWidth" "$exampleName" "$exampleNote"
438    }
439    #}}}
440}
441#}}}
442
443#{{{ main program
444#{{{ docu
445#
446# - main program.
447#
448# runROOptions: options for RO table.  Read from commandline.
449# rawCollectionName: collection name with full path
450#
451# Global variables used:
452#   collectionROOptions,
453#   debugMode,
454#   ROColWidth, EvalAlg
455#
456#}}}
457typeset runROOptions=""
458typeset rawCollectionName=""
459
460#{{{ read options and environment
461# read options
462typeset opt
463while getopts "a:c:C:t:r:d" opt; do
464    case "$opt" in
465        (a)  runOptions="$runOptions -a$OPTARG" ;;
466        (c)  runOptions="$runOptions -c$OPTARG" ;;
467        (C)  runConfiguration="$OPTARG" ;;
468        (t)  runTBOptions="$OPTARG" ;;
469        (r)  runROOptions="$OPTARG" ;;
470        (d)  debugMode="1" ;;
471        (?)  warn "bad run option"; exit 1 ;;
472    esac
473done
474# shift options and reset OPTIND
475typeset -i optind
476optind=OPTIND-1
477shift $optind
478OPTIND=1
479
480# read environment
481while [ "${1-}" != "${1+${1#/}}" ]; do
482    runEnvironment="$runEnvironment $1"
483    shift
484done
485#}}}
486
487# process rest of arguments
488if [ "$#" = "0" ]; then
489    warn "no collection name specified"
490    exit 1
491fi
492rawCollectionName="${1%.fex}"
493shift
494
495defineSkip "$@"
496
497# before going on, check for existence of collection
498if [ ! -f "$rawCollectionName.fex" ]; then
499    warn "collection $rawCollectionName.fex not found"
500    exit 1
501fi
502
503# execute collection
504. "$rawCollectionName.fex"
505
506if [ -z "$debugMode" ]; then
507    typeset algChar
508    typeset algSwitch
509    typeset algVars
510    typeset algCircle
511    typeset algSeed
512    typeset algVers
513
514    # read data
515    runAlgorithm "$EvalAlg" -oe \
516        | {
517        read algChar
518        read algSwitch
519        read algVars
520        read algCircle
521        read algSeed
522        read algVers
523
524        # print data
525        typeset options="${runROOptions:-$collectionROOptions}"
526        # remove some options
527        options="${options//tcrd/}"
528
529        printData "$options" "$ROColWidth" "$collectionName" "$collectionNote"
530    }
531fi
532#}}}
Note: See TracBrowser for help on using the repository browser.