nitlog: oops, didn't deal properly with posts in months that haven't
[wvapps.git] / wvdial / wvdial.cc
blob874c139d0afb3d576aad66c5df2994ac41950a06
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();
40 void WvDialLogger::_make_prefix()
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");
121 args.set_email("<wvdial-list@lists.nit.ca>");
123 args.add_option('C', "config",
124 "use configfile instead of /etc/wvdial.conf",
125 "configfile", WvArgs::ArgCallback(&config_cb), &cfg);
126 args.add_set_bool_option('c', "chat",
127 "used when running wvdial from pppd", chat_mode);
128 args.add_reset_bool_option('n', "no-syslog",
129 "don't send output to SYSLOG", chat_mode);
130 args.add_optional_arg("SECTION", true);
131 args.add_optional_arg("OPTION=value", true);
133 WvStringList remaining_args;
134 args.process(argc, argv, &remaining_args);
137 WvStringList::Iter i(remaining_args);
138 for (i.rewind(); i.next(); )
140 if (strchr(i(), '=' ))
141 cmdlineopts.append(new WvString(i()),true);
142 else
143 sections.append(new WvString("Dialer %s", i()), true);
147 if (sections.isempty())
148 sections.append(new WvString("Dialer Defaults"), true);
150 if( !haveconfig)
152 // Load the system file first...
153 WvString stdconfig("/etc/wvdial.conf");
155 if (!access(stdconfig, F_OK))
156 cfg.load_file(stdconfig);
158 // Then the user specific one...
159 if (homedir)
161 WvString rcfile("%s/.wvdialrc", homedir);
163 if (!access(rcfile, F_OK))
164 cfg.load_file(rcfile);
168 // Inject all of the command line options on into the cfg file in a new
169 // section called Command-Line if there are command line options.
170 if (!cmdlineopts.isempty())
172 WvStringList::Iter i(cmdlineopts);
173 for (i.rewind();i.next();)
175 char *name = i().edit();
176 char *value = strchr(name,'=');
178 // Value should never be null since it can't get into the list
179 // if it doesn't have an = in i()
181 *value = 0;
182 value++;
183 name = trim_string(name);
184 value = trim_string(value);
185 cfg.set("Command-Line", name, value);
187 sections.prepend(new WvString("Command-Line"), true);
190 if(!cfg.isok())
192 return 1;
195 if (chat_mode)
197 if (write_syslog)
199 WvString buf("wvdial[%s]", getpid());
200 syslog = new WvSyslog( buf, false, WvLog::Debug2,
201 WvLog::Debug2 );
203 else
205 // Direct logging to /dev/null as otherwise WvLog hasn't any
206 // receivers and thus will use WvLogConsole to log to stderr.
207 // That can disturb the communication with the modem on
208 // stdin/stdout. - Fixes a bug reported by SUSE on 04/05/04
209 filelog = new WvLogFile( "/dev/null", WvLog::Debug2 );
213 WvDialer dialer(cfg, &sections, chat_mode);
215 if (!chat_mode)
216 if (dialer.isok() && dialer.options.ask_password)
217 dialer.ask_password();
219 if (dialer.dial() == false)
220 return 1;
222 while (!want_to_die && dialer.isok()
223 && dialer.status() != WvDialer::Idle)
225 dialer.select(100);
226 dialer.callback();
229 int retval;
231 if (want_to_die)
233 // Probably dieing from a user signal
234 retval = 2;
237 if ((dialer.status() != WvDialer::Idle) || !dialer.isok())
239 retval = 1;
241 else
243 retval = 0;
246 dialer.hangup();
248 WVRELEASE(filelog);
249 delete syslog;
251 return(retval);