1 /*
2  * SIE Remote Access (SRA) ASCII tool
3  *
4  *  Copyright (c) 2014-2017 by Farsight Security, Inc.
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 #include "sratool.h"
20 
21 /* extern: infile.c */
22 extern int in_file_cur;
23 extern in_files_t in_files[];
24 
25 /* extern: main.c */
26 extern bool eclose;
27 
28 /* extern: server.c */
29 extern axa_client_t client;
30 
31 static void
vsub_error_msg(const char * p,va_list args)32 vsub_error_msg(const char *p, va_list args)
33 {
34 	clear_prompt();
35 	vfprintf(stderr, p, args);
36 	fputc('\n', stderr);
37 }
38 
39 static void AXA_PF(1,2)
sub_error_msg(const char * p,...)40 sub_error_msg(const char *p, ...)
41 {
42 	va_list args;
43 
44 	va_start(args, p);
45 	vsub_error_msg(p, args);
46 	va_end(args);
47 }
48 
49 void AXA_PF(1,2)
error_msg(const char * p,...)50 error_msg(const char *p, ...)
51 {
52 	va_list args;
53 
54 	va_start(args, p);
55 	vsub_error_msg(p, args);
56 	va_end(args);
57 
58 	error_close(false);
59 }
60 
61 /*
62  * After an error, disconnect from the server if in "error mode".
63  * And after a command error, give up on a command file set with "source".
64  */
65 void
error_close(bool cmd_error)66 error_close(bool cmd_error)
67 {
68 	if (eclose && AXA_CLIENT_OPENED(&client)) {
69 		sub_error_msg("    disconnecting from %s after error",
70 			      client.io.label);
71 		disconnect(false);
72 	}
73 
74 	if (cmd_error && in_file_cur > 0) {
75 		AXA_ASSERT(in_files[in_file_cur].name != NULL);
76 		sub_error_msg("    after line #%d in %s",
77 			  in_files[in_file_cur].lineno,
78 			  in_files[in_file_cur].name);
79 		close_in_files();
80 	}
81 }
82