zsh-users
 help / color / mirror / code / Atom feed
* exit to shell from nested function calls.
@ 2024-05-16 14:21 Ray Andrews
  2024-05-16 20:26 ` Roman Perepelitsa
  0 siblings, 1 reply; 14+ messages in thread
From: Ray Andrews @ 2024-05-16 14:21 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 4945 bytes --]

Don't read this closely, there's only one thing that's relevant, see  below:


# Create a directory:
_overlay_mkdir ()
{
     # Create backup directory if it hasn't already been done.
     [ -d $1 ] && { infomsg "\"$1\" already exists."; return 0 }

     mkdir -vp $1 ||
         { errormsg "Couldn't create directory: \"$1\"." && return 1 }
     return 0
}

# Synchronize two directories:
_overlay_rsync ()
{
     actionmsg "Synching \"$1\" to \"$2\" ..."
     # greysky uses: --delete-after

     rsync -aX --del --inplace --no-whole-file $1/* $2 ||
         { errormsg "Can't rsync \"$1\" to \"$2\"." && return 1 }
     return 0
}

# Run for each directory to be overlaid:
overlay ()
{
     [[ ! "$1" || "$1" == '-h' ]] && _overlay_syntax && return 0

     # NB, this check must run before initializing OR normal run.
     # '-x' = exact match. Note, even the firefox 'can't find profile' 
error msg. box triggers this:
     [ -n "$( pgrep -x ${program} )" ] &&
     errormsg "\"${program}\" is running, please quit before 
continuing." && return 1

     actionmsg "\nInitializing overlay for \"$1\" ..."

     local subject="$1" # Full path of directory to be overlayed.

     # renamed subject dir. (must borrow real name to create link to 
overlay).
     local active="${subject}-active"
     local backup="/aMisc/Overlay$subject"    # Backup.
     local target="/run/user/0$subject"        # The overlay directory.
     local working="${target}-working"        # Used by 'mount' internally.


# Rescue if we did not unoverlay properly, as when computer is shut down 
without quitting facebook first.  If subject is a link pointing to 
target we almost surely did an improper shut down:
[[ -h "$subject" && $( readlink -m "$subject" ) == "$target" ]] &&
{
     # Recreate target:
     _overlay_mkdir ${target} || return 1
     # And remount:
     mount -vt overlay -o 
lowerdir=${active},upperdir=${target},workdir=${working} overlay 
${target} || return 1
     return 0
}

     [[ -d "$subject" ]] ||
         { errormsg "\"${subject}\" seems not to be a directory,\n but 
we can only overlay directories!"; return 1 }

     # rsync subject to the backup directory: Do this here so that we 
get the backup no matter what.  This way the pointer does not exist yet.
     _overlay_mkdir ${backup}  || return 1
     _overlay_rsync "$subject" "$backup" || return 1

#"$subject" is: |/root/.mozilla|
#"$backup" is:  |/aOverlay/root/.mozilla-backup|
#"$active" is:  |/root/.mozilla-active|
#"$target" is:  |/run/user/0/root/.mozilla|
#"$working" is: |/run/user/0/root/.mozilla-working|

     if [[ -h "$subject" &&  $( readlink -m "$subject" ) == "$target" && \
     -d "$backup" && -d "$active" && -d "$target" && -d "$working" ]]; then
             infomsg "\"${subject}\" overlay is already initialized."
     else
         # Create directories if it hasn't already been done.
         _overlay_mkdir ${target}  || return 1
         _overlay_mkdir ${working} || return 1

         # Subject directory is renamed, and ...
         mv -v "$subject" "$active" || return 1
         # ... 'subject' is now a link to overlay.
         ln -sv "$target" "$subject" ||
             { errormsg "Couldn't link \"$subject\" to \"$target\"."; 
return 1 }

         actionmsg "\nCreating overlay: lowerdir=\"$active\" 
overlay=\"$target\" ..."

         # -v = verbose, -t overlay = type is overlay.  lowerdir is 
hidden by upperdir.  workdir is internal to mount.
         mount -vt overlay -o 
lowerdir=${active},upperdir=${target},workdir=${working} overlay ${target}
     fi
     return 0
}

---------------------------------------------------------------------------------------

... the above is part of a function hierarchy can can be four levels 
deep and if there's an error (it's being debugged so there's error tests 
everywhere), I need these 'return 1 ... return 1 ... return 1 ... return 
1 ... to break back to the shell.  Now, didn't we discuss something like 
this a few months back?  ... to do with zmv ... 'err-exit' or something 
like that.  I was complaining that zmv should NOT crash -- but Bart 
pointed out that it's not a crash it's a controlled thing, of course.  
And zmv was modified accordingly IIRC.

Anyway, it occurs to me that in the above, I want the opposite -- I DO 
want any of these errors, however many levels deep it might be, to 
exit/return directly back to the shell (but 'exit' itself quits the 
shell entirely) without having to return from each level one at a time.  
As always I wish I could search the archives and dig this up, because it 
was the same issue.

'err-exit'?  Or can a trap do this? or, if it was run in a subshell then 
'exit' would return to the calling shell?


[-- Attachment #2: Type: text/html, Size: 6424 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-05-18  2:10 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-16 14:21 exit to shell from nested function calls Ray Andrews
2024-05-16 20:26 ` Roman Perepelitsa
2024-05-16 21:28   ` Ray Andrews
2024-05-17 15:40     ` Mark J. Reed
2024-05-17 15:58       ` Ray Andrews
2024-05-17 16:46         ` Mark J. Reed
2024-05-17 17:04         ` Bart Schaefer
2024-05-17 17:36           ` Bart Schaefer
2024-05-17 18:52             ` Ray Andrews
2024-05-17 19:00               ` Mark J. Reed
2024-05-17 20:28                 ` Ray Andrews
2024-05-18  2:09                   ` Bart Schaefer
2024-05-17 17:29     ` Thomas Lauer
2024-05-17 18:43       ` Ray Andrews

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).