Browse Source

made zuper idempotent (you can "source zuper" several times without errors)

KatolaZ 3 years ago
parent
commit
ad957af625
1 changed files with 52 additions and 1 deletions
  1. 52 1
      zuper

+ 52 - 1
zuper

@@ -22,11 +22,19 @@
22 22
 # Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 23
 
24 24
 
25
+if [[ ! -z ${zuper_version} ]]; then 
26
+    warning "zuper version ::1 version:: was already loaded -- doing nothing" ${zuper_version}
27
+    return;
28
+fi
29
+
30
+
25 31
 ##########################
26 32
 typeset -aU vars
27 33
 typeset -aU arrs
28 34
 typeset -aU maps
29 35
 
36
+typeset -aU funs
37
+
30 38
 vars=(DEBUG QUIET LOG)
31 39
 arrs=(req freq)
32 40
 
@@ -229,7 +237,13 @@ TRAPSTOP() { endgame STOP;  return $? }
229 237
 # TRAPZERR() { func "function returns non-zero." }
230 238
 
231 239
 
232
-endgame() {
240
+funs+=(__test_fn)
241
+
242
+__test_fn(){
243
+    echo "foo"
244
+}
245
+
246
+function zuper_end endgame() {
233 247
     fn "endgame $*"
234 248
 
235 249
     # execute all no matter what
@@ -240,9 +254,46 @@ endgame() {
240 254
         fn "destructor: $d"
241 255
         $d
242 256
     done
257
+
258
+    # unset all the variables included  in "vars" 
259
+    for v in $vars; do 
260
+        unset $v
261
+    done
262
+
263
+    # unset all the assoc-arrays included  in "arrs" 
264
+    for a in $arrs; do 
265
+        unset $a
266
+    done
267
+
268
+    # unset all the maps included  in "maps" 
269
+    for m in $maps; do 
270
+        unset $m
271
+    done
272
+    
273
+    ## We should also undefine the core zuper functions to make it
274
+    ## really idempotent. I have added an array "funs" which contains
275
+    ## the names of the functions to be undefined by endgame/zuper_end
276
+    ## FIXME!!!! The only "registered" function so far is __test_fn,
277
+    ## but if we like this we should register all the core zuper
278
+    ## functions as soon as they are declared
279
+    for f in $funs; do 
280
+        unfunction $f
281
+    done
282
+    unset maps
283
+    unset arrs
284
+    unset vars
285
+    unset funs
286
+
243 287
     return 0
244 288
 }
245 289
 
290
+## This function should reinitialise zuper and all the variables
291
+# zuper_restart(){
292
+#     endgame 
293
+#     source zuper
294
+# }
295
+
296
+
246 297
 # Use this to make sure endgame() is called at exit.
247 298
 # unlike TRAPEXIT, the zshexit() hook is not called when functions exit.
248 299
 function zuper.exit zshexit() { endgame EXIT; return $? }