Merged revisions 11610-11649 via svnmerge from
[wvapps.git] / wvdial / wvdial.cc
blob014b1375054299952cdbb4b9c4b73d703c235833
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2005 Net Integration Technologies, Inc.
5 * Standalone WvDial program, for testing the WvDialer class.
7 * Created: Sept 30 1997 D. Coombs
8 */
10 #include "wvargs.h"
11 #include "wvdialer.h"
12 #include "wvver.h"
13 #include "wvlog.h"
14 #include "wvlogrcv.h"
15 #include "wvlogfile.h"
16 #include "wvsyslog.h"
17 #include "wvcrash.h"
19 #include <signal.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
23 volatile bool want_to_die = false;
26 // use no prefix string for app "Modem", and an arrow for everything else.
27 // This makes the output of the wvdial application look nicer.
28 class WvDialLogger : public WvLogConsole
29 /**************************************/
31 public:
32 WvDialLogger() : WvLogConsole(dup(2)) // log to stderr (fd 2)
33 { }
35 protected:
36 virtual void _make_prefix(time_t now);
40 void WvDialLogger::_make_prefix(time_t now)
41 /*******************************/
43 WvString name = last_source;
44 if(name == "WvDial Modem")
46 prefix = "";
47 prelen = 0;
49 else
51 prefix = "--> ";
52 prelen = 4;
56 static void signalhandler(int sig)
57 /***********************************/
59 fprintf(stderr, "Caught signal %d: Attempting to exit gracefully...\n", sig);
60 want_to_die = true;
61 signal(sig, SIG_DFL);
65 bool haveconfig = false;
66 static bool config_cb(WvStringParm value, void *userdata)
68 WvConf *cfg = reinterpret_cast<WvConf*>(userdata);
69 if (!access(value, F_OK))
71 cfg->load_file(value);
72 haveconfig = true;
73 return true;
75 fprintf(stderr, "Cannot read `%s'\n", value.cstr());
76 return false;
80 int main(int argc, char **argv)
81 /********************************/
83 #if DEBUG
84 free( malloc( 1 ) ); // for electric fence
85 #endif
87 WvDialLogger rc;
88 WvSyslog *syslog = NULL;
89 WvLogFile *filelog = NULL;
90 UniConfRoot uniconf("temp:");
91 WvConf cfg(uniconf);
92 WvStringList sections;
93 WvStringList cmdlineopts;
94 WvLog log( "WvDial", WvLog::Debug );
95 WvString homedir = getenv("HOME");
97 bool chat_mode = false;
98 bool write_syslog = true;
100 signal(SIGTERM, signalhandler);
101 signal(SIGINT, signalhandler);
102 signal(SIGHUP, signalhandler);
104 WvArgs args;
105 args.set_version("WvDial " WVDIAL_VER_STRING "\n"
106 "Copyright (c) 1997-2005 Net Integration Technologies, "
107 "Inc.");
108 args.set_help_header("An intelligent PPP dialer.");
109 args.set_help_footer("Optional SECTION arguments refer to sections in "
110 "configuration file (usually)\n"
111 "/etc/wvdial.conf, $HOME/.wvdialrc or the file "
112 "specified by --config.\n"
113 "Specified sections are all read, with later ones "
114 "overriding previous ones.\n"
115 "Any options not in the listed sections are taken "
116 "from [Dialer Defaults].\n"
117 "\n"
118 "Also, optional OPTION=value parameters allow you "
119 "to override options within\n"
120 "the configuration files.\n");
122 args.add_option('C', "config",
123 "use configfile instead of /etc/wvdial.conf",
124 "configfile", WvArgs::ArgCallback(&config_cb), &cfg);
125 args.add_set_bool_option('c', "chat",
126 "used when running wvdial from pppd", chat_mode);
127 args.add_reset_bool_option('n', "no-syslog",
128 "don't send output to SYSLOG", chat_mode);
129 args.add_optional_arg("SECTION", true);
130 args.add_optional_arg("OPTION=value", true);
132 WvStringList remaining_args;
133 args.process(argc, argv, &remaining_args);
136 WvStringList::Iter i(remaining_args);
137 for (i.rewind(); i.next(); )
139 if (strchr(i(), '=' ))
140 cmdlineopts.append(new WvString(i()),true);
141 else
142 sections.append(new WvString("Dialer %s", i()), true);
146 if (sections.isempty())
147 sections.append(new WvString("Dialer Defaults"), true);
149 if( !haveconfig)
151 // Load the system file first...
152 WvString stdconfig("/etc/wvdial.conf");
154 if (!access(stdconfig, F_OK))
155 cfg.load_file(stdconfig);
157 // Then the user specific one...
158 if (homedir)
160 WvString rcfile("%s/.wvdialrc", homedir);
162 if (!access(rcfile, F_OK))
163 cfg.load_file(rcfile);
167 // Inject all of the command line options on into the cfg file in a new
168 // section called Command-Line if there are command line options.
169 if (!cmdlineopts.isempty())
171 WvStringList::Iter i(cmdlineopts);
172 for (i.rewind();i.next();)
174 char *name = i().edit();
175 char *value = strchr(name,'=');
177 // Value should never be null since it can't get into the list
178 // if it doesn't have an = in i()
180 *value = 0;
181 value++;
182 name = trim_string(name);
183 value = trim_string(value);
184 cfg.set("Command-Line", name, value);
186 sections.prepend(new WvString("Command-Line"), true);
189 if(!cfg.isok())
191 return 1;
194 if (chat_mode)
196 if (write_syslog)
198 WvString buf("wvdial[%s]", getpid());
199 syslog = new WvSyslog( buf, false, WvLog::Debug2,
200 WvLog::Debug2 );
202 else
204 // Direct logging to /dev/null as otherwise WvLog hasn't any
205 // receivers and thus will use WvLogConsole to log to stderr.
206 // That can disturb the communication with the modem on
207 // stdin/stdout. - Fixes a bug reported by SUSE on 04/05/04
208 filelog = new WvLogFile( "/dev/null", WvLog::Debug2 );
212 WvDialer dialer(cfg, &sections, chat_mode);
214 if (!chat_mode)
215 if (dialer.isok() && dialer.options.ask_password)
216 dialer.ask_password();
218 if (dialer.dial() == false)
219 return 1;
221 while (!want_to_die && dialer.isok()
222 && dialer.status() != WvDialer::Idle)
224 dialer.select(100);
225 dialer.callback();
228 int retval;
230 if (want_to_die)
232 // Probably dieing from a user signal
233 retval = 2;
236 if ((dialer.status() != WvDialer::Idle) || !dialer.isok())
238 retval = 1;
240 else
242 retval = 0;
245 dialer.hangup();
247 WVRELEASE(filelog);
248 delete syslog;
250 return(retval);