473,800 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

catching exit

Hi there,

I am trying to reuse a piece of code that was designed as an
application. The code is covered with 'exit' calls. I would like to
reuse it as a library. For that I renamed the 'main' function into
'mymain', but I am stuck as to what I should do for the 'exit'.

AFAIK there is no portable way to catch the exit. The only thing I
can think of is atexit, but that does not work since 'exit' is still
called afterward.

What I am thinking now is that I need to replace all exit(val) with
longjmp(env, val). And make 'env' global.

Comments ?

Thanks
-Mathieu
Aug 13 '08
39 2830
mathieu said:

<snip>
So to paraphrase you, I should
s/should/could/
have said:

...
While working at X, I am trying to reuse a piece of code that was
designed by
Y as an application, in mid 1990. The code is covered with 'exit'
calls...
Yes - it's the same problem, just in a slightly different form.
I am not very keen on 'system' call, as I do not know how
portable they are.
The function itself is portable, insofar as any hosted implementation is
required to support the call - but the meaning of the return value is
implementation-defined (unless you pass it NULL, in which case the value
is 0 if there is NO command processor available, otherwise non-zero), and
of course the semantics of the executed command depends on the system -
for example, system("PAUSE") is well-defined on Windows, but pretty
meaningless on Linux (unless you happen to have a program in your path
named PAUSE, which of course might be a video game, or a word processor,
or anything, really).
So yes I am stuck on reusing this piece of code,
and my goal is to spent little time as possible.
Then system() is probably the way to go - but of course you'll need to
ensure that the command that you invoke has the identical meaning in all
relevant ways on all target systems.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 14 '08 #11
Keith Thompson wrote:
CBFalconer <cb********@yah oo.comwrites:
.... snip ...
>
>If that's what he wants to do that is another case. But the
previous effect was to terminate the program, whch is what exit
does.

He knew that. If he wanted the existing exit calls to terminate
the program, why would he have asked what to do about them?
Well, you read the OP post as showing more knowledge than I did.
If the original code was in the main function (which I believe it
was) it could probably have simply used 'return'.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Aug 14 '08 #12
James Kuyper wrote:
>
CBFalconer wrote:
.... snip ...
>
>If that's what he wants to do that is another case.

No, that's the original case, not another one.
>... But the previous effect was to terminate the program, whch
is what exit does.

And he made it abundantly clear, I thought, that the point of his
question was about not wanting to terminate the program.
Which is easy. Simply set an error flag visible in the caller, and
return. That value may be the value returned by the function.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Aug 14 '08 #13
mathieu wrote:
CBFalconer <cbfalco...@yah oo.comwrote:
>mathieu wrote:
>>I am trying to reuse a piece of code that was designed as an
application . The code is covered with 'exit' calls. I would like
to reuse it as a library. For that I renamed the 'main' function
into 'mymain', but I am stuck as to what I should do for the
'exit'.

AFAIK there is no portable way to catch the exit. The only thing
I can think of is atexit, but that does not work since 'exit' is
still called afterward.

What I am thinking now is that I need to replace all exit(val)
with longjmp(env, val). And make 'env' global.

Just leave it calling exit. The result is as follows:
.... snip ...
>
Thanks I also know how to read the man page of exit :)
That wasn't clear to me. Since the only things you can (portably)
return from main is EXIT_SUCCESS and EXIT_FAILURE (and 0), you have
much more latitude in a function. So simply return from the
function, returning a status, and let the caller decide what to do
about it. I see no point to the exit calls. Maybe you do.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Aug 14 '08 #14
On Thu, 14 Aug 2008 07:31:46 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
>CBFalconer said:
>Keith Thompson wrote:
<snip>
>>>
He has a main program; a call to exit terminates the program.

He wants to turn this main program into a function within a larger
program. Wherever there's a call to exit, he wants to terminate the
function, not the entire (now larger) program.

If that's what he wants to do that is another case.

No, it's the original question. To avoid making such mistakes in future, I
recommend that you save up your replies for an hour or so, and then:

(1) Re-read the original question, and then re-read your original reply. At
this stage, it will sometimes become clear that your reply was based on a
mis-reading of the question which your second reading makes obvious.
Solution: delete your reply without sending it.

(2) By this time, there may well be one or two other replies to the
question, by people whose opinion you respect. Read them.

If they are, in essence, the same as your own reply, delete your reply
without sending it, since it would be pointlessly duplicative (unless your
reply adds something useful that was overlooked by the others).

If they are *not* in essence the same as your reply, consider seriously the
possibility that you have mis-read the question. Read their replies, and
the original question, again, and find out why they have replied as they
did. If you genuinely interpret the question differently to them, then
edit your reply so that it says something like "I've read A's and B's
replies, and they've assumed you meant such-and-such. If so, fine. But it
seems to me that you might mean /this/ instead. If so, then my advice
is..." Then, having changed your reply, save it up for an hour or so, and
then start back at (1) again.

(3) Now send any undeleted replies.

I suggest that using this strategy could save you from a huge amount of
embarrassmen t.
Would that everyone followed that policy, even me.
Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Aug 14 '08 #15
CBFalconer wrote:
Keith Thompson wrote:
>CBFalconer <cb********@yah oo.comwrites:
... snip ...
>>
>>If that's what he wants to do that is another case. But the
previous effect was to terminate the program, whch is what exit
does.

He knew that. If he wanted the existing exit calls to terminate
the program, why would he have asked what to do about them?

Well, you read the OP post as showing more knowledge than I did.
I thought that the original post was pretty clear about the poster's
problem.
If the original code was in the main function (which I believe it
was) it could probably have simply used 'return'.
No. He (the OP) is having to use a program written by someone else as a
*function* in his program. Unfortunately the program is littered with
calls to exit() which he'll obviously have to avoid if he intends to
use the program as a routine.

This sort of transformation is very difficult with a program composed of
tightly coupled routines, which were never written for independent use.

Aug 14 '08 #16
CBFalconer <cb********@yah oo.comwrites:
James Kuyper wrote:
>CBFalconer wrote:
... snip ...
>>
>>If that's what he wants to do that is another case.

No, that's the original case, not another one.
>>... But the previous effect was to terminate the program, whch
is what exit does.

And he made it abundantly clear, I thought, that the point of his
question was about not wanting to terminate the program.

Which is easy. Simply set an error flag visible in the caller, and
return. That value may be the value returned by the function.
Yes, that *would* be easy, and again the OP probably wouldn't have
bothered to post here.

Here's the situation as I understand it.

The OP has a fairly large program, consisting of a main() function and
some number of other functions, along with whatever other external
declarations there might be. He wants to turn the whole mess into a
*library* (he actually used the word "library"). The problem is that
there are calls to exit, not just in the main() function, but in
*other* functions as well.

He wants to call the main function (now renamed to mymain) and have it
behave as it did when it was a separate program. The problem, of
course, is that a call to exit will terminate his new larger program;
to keep the same semantics, he needs it to terminate only the mymain
function.

He's already considered and rejected the use of atexit, and I think
he's currently considering setjmp/longjmp.

I figured all that out by reading the original post. I think you
could have done the same if you'd been a bit more careful.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 14 '08 #17
mathieu <ma************ ***@gmail.comwr ites:
I am trying to reuse a piece of code that was designed as an
application. The code is covered with 'exit' calls. I would like to
reuse it as a library. For that I renamed the 'main' function into
'mymain', but I am stuck as to what I should do for the 'exit'.

AFAIK there is no portable way to catch the exit. The only thing I
can think of is atexit, but that does not work since 'exit' is still
called afterward.

What I am thinking now is that I need to replace all exit(val) with
longjmp(env, val). And make 'env' global.
The real problem, I presume, is that you have exit calls from
functions other than main(). In the original version of the program,
each such call terminates the main program; in your bigger program
with main renamed to mymain, you want to terminate mymain, not your
new main. Right?

Keeping it as a separate program and invoking it with system() might
be your best bet. The behavior of system() is largely
implementation-defined, particularly its return value, but if you're
only targeting a limited number of systems you can deal with that with
a few #ifdef's.

If you want the resulting code to be portable to all conforming
systems, things are a bit more difficult. On any one system, you
should be able to tell from the result returned by system() whether
the invoked program did an exit(EXIT_SUCCE SS) or an exit(EXIT_FAILU RE)
(or some other system-specific value), but there's no portable way to
do that.

You'll also need to take some care to ensure that
system("existin g_program") actually invokes what you want it to
invoke. The details are system-specific, but at least for Unix-like
and Windows-like systems, think about PATH settings.

<OT>
Here's another possibility. I happen to know that there's another
language, whose name is that of our local favorite language with a
couple of plus signs appended to it, which supports the kind of
control flow you're looking for. Rather than calling exit, you can
"throw" an "exception" and "catch" it at whatever level you like
rather than letting it propagate out and terminate the program. It's
not inconceivable that you could recompile this program using a
compiler for this Other Language and make use of the OL's features.
This approach could be fraught (fraught, I say!) with peril. There
are subtle differences between C and the OL, some of which may prevent
valid C code from compiling, and some of which may silently change its
behavior. And if you take that approach, we can't help you here, but
the folks over in comp.lang.thato therlanguage or
comp.lang.thato therlanguage.mo derated will be glad to do so.
</OT>

But still, I think that system() is your best bet, requiring no
changes to the program and (probably) just a little bit of
system-specific scaffolding.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 14 '08 #18
In article <-4************** *************** *@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
....
>To avoid making such mistakes in future, I recommend that you save up
your replies for an hour or so, and then:

(1) Re-read the original question, and then re-read your original reply. At
this stage, it will sometimes become clear that your reply was based on a
mis-reading of the question which your second reading makes obvious.
Solution: delete your reply without sending it.

(2) By this time, there may well be one or two other replies to the
question, by people whose opinion you respect. Read them.

If they are, in essence, the same as your own reply, delete your reply
without sending it, since it would be pointlessly duplicative (unless your
reply adds something useful that was overlooked by the others).

If they are *not* in essence the same as your reply, consider seriously the
possibility that you have mis-read the question. Read their replies, and
the original question, again, and find out why they have replied as they
did. If you genuinely interpret the question differently to them, then
edit your reply so that it says something like "I've read A's and B's
replies, and they've assumed you meant such-and-such. If so, fine. But it
seems to me that you might mean /this/ instead. If so, then my advice
is..." Then, having changed your reply, save it up for an hour or so, and
then start back at (1) again.

(3) Now send any undeleted replies.

I suggest that using this strategy could save you from a huge amount of
embarrassmen t.
This is a keeper. Well done, Mr. H!

Aug 14 '08 #19
On 14 Aug 2008 at 17:21, Kenny McCormack wrote:
Richard Heathfield <rj*@see.sig.in validwrote:
[a strategy to prevent stupid posts from CBF]
>
This is a keeper. Well done, Mr. H!
Yes... but I'm not sure about this bit:
>>(3) Now send any undeleted replies.
Of course, by this stage every single reply should have been deleted,
but just in case one slips through I'd recommend changing this to

(3) Now send any undeleted replies to /dev/null.

Aug 14 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2609
by: lord.zoltar | last post by:
How can I prevent the big close button in the top of the window from closing the window? I want to have and "are you sure?" confirmation so the user must press "Yes" before the program ends. Right now, I've tried catching the FormClosing and FormClosed events. The message box appears at the right time, but since the form is already closing, it doesn't matter if the user presses "Yes" or "No". how do I cancel the FormClosing?
4
1810
by: lovecreatesbea... | last post by:
Is the following code (without any code at lines x and x + 3) correct? Is it better to call exit() or re-throw the exceptions at line x and line x + 3?. What is the better code should we place at line x and line x + 3? try { cnn = env->createConnection(user, pwd, db); } catch (SQLException &esql){ cerr << "DB Exception: " << esql.getMessage(); /* line x ? */
5
3816
by: Stefan Bellon | last post by:
Hi all! I am embedding Python into a GUI application in a way that the GUI is scriptable using Python. Now I have come to a problem that when the user puts a "sys.exit(0)" into his script to end the script, not only the script is terminated, but also the GUI application itself. This is not the intended behaviour. As in Python itself you can catch SystemExit, I think this should be
1
2425
by: Marty | last post by:
I need to catch exceptions thrown by programs started by the os.system function, as indicated by a non-zero return code (e.g. the mount utility). For example, if I get the following results in a bash shell: $mount test mount: can't find /home/marty/test in /etc/fstab or /etc/mtab then I want to catch the same exception from the corresponding os.system() call, i.e. "os.system('mount test')", but it doesn't work as expected:
12
18343
by: Karlo Lozovina | last post by:
I'm not sure if Python can do this, and I can't find it on the web. So, here it goes: try: some_function() except SomeException: some_function2() some_function3() ...
9
11584
by: titanandrews | last post by:
Hi All, Is there any way to catch the exit code from someone calling exit(status) and check the value before the program terminates? Just for an idea, the code I'm thinking of is something like this: void exithandler() { DWORD exitCode = 0;
3
1830
by: Anthony P. | last post by:
Hello Everyone, I am writing a Windows Mobile 5.0 application using VS 2..8 in VB.NET. When someone clicks the "Exit" menu option, which generates a button_click() event, I find it easy to display an "Are you sure?" MessgeBox before the application closes. That way, if they accidentally selected exit, they have a chance to back out. But I also want to catch the _closing() event on the form in case the user clicks the application close X...
16
1569
by: cmdolcet69 | last post by:
I have the below if statement, that should catch if any of the conditions are met.....however for some reasons if my boolDSIFlushGapReading = true and MuxClass.DSIValues.count =1 and my muxclass.COM1Active =1 it will not exit the program??? Why is that can anyone help me Urgent!!!!!!
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.