Schedulator summary now predicts release dates based on your bounce goals,
[wvapps.git] / wvdial / wvdial.cc
blob9b996acec10f0c18947fb135c8f0c0ae3fd27e05
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2003 Net Integration Technologies, Inc.
5 * Standalone WvDial program, for testing the WvDialer class.
7 * Created: Sept 30 1997 D. Coombs
8 */
10 #include "wvdialer.h"
11 #include "wvver.h"
12 #include "wvlog.h"
13 #include "wvlogrcv.h"
14 #include "wvlogfile.h"
15 #include "wvsyslog.h"
16 #include "wvcrash.h"
18 #include <signal.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
22 volatile bool want_to_die = false;
25 // use no prefix string for app "Modem", and an arrow for everything else.
26 // This makes the output of the wvdial application look nicer.
27 class WvDialLogger : public WvLogConsole
28 /**************************************/
30 public:
31 WvDialLogger() : WvLogConsole(dup(2)) // log to stderr (fd 2)
32 { }
34 protected:
35 virtual void _make_prefix();
39 void WvDialLogger::_make_prefix()
40 /*******************************/
42 WvString name = appname(last_source);
43 if(name == "WvDial Modem")
45 prefix = "";
46 prelen = 0;
48 else
50 prefix = "--> ";
51 prelen = 4;
55 static void print_version()
56 /*************************/
58 printf( "%s", wvdial_version_text );
61 static void print_help()
62 /**********************/
64 print_version();
65 printf( "\n%s", wvdial_help_text );
68 static void signalhandler(int sig)
69 /***********************************/
71 fprintf(stderr, "Caught signal %d: Attempting to exit gracefully...\n", sig);
72 want_to_die = true;
73 signal(sig, SIG_DFL);
77 int main(int argc, char **argv)
78 /********************************/
80 #if DEBUG
81 free( malloc( 1 ) ); // for electric fence
82 #endif
84 WvDialLogger rc;
85 WvSyslog *syslog = NULL;
86 WvLogFile *filelog = NULL;
87 UniConfRoot uniconf("temp:");
88 WvConf cfg(uniconf);
89 WvStringList sections;
90 WvStringList cmdlineopts;
91 WvLog log( "WvDial", WvLog::Debug );
92 WvString homedir = getenv("HOME");
93 int haveconfig = 0;
94 int havecmdlineopts = 0;
96 bool chat_mode = false;
97 bool write_syslog = true;
99 signal(SIGTERM, signalhandler);
100 signal(SIGINT, signalhandler);
101 signal(SIGHUP, signalhandler);
103 if(argc > 1)
105 for(int i=1; i < argc; i++)
107 if(!strcmp(argv[i], "--config" ))
109 if (!access(argv[++i < argc ? i : i - 1], F_OK))
111 haveconfig = 1;
112 cfg.load_file(WvString(argv[i]));
113 continue;
115 else
117 log("Error: --config requires a valid argument\n");
118 print_help();
119 return 1;
122 if(strchr(argv[i], '=' ))
124 havecmdlineopts = 1;
125 cmdlineopts.append(new WvString(argv[i]),true);
126 continue;
128 if(!strcmp(argv[i], "--chat" ))
130 chat_mode = true;
131 continue;
133 if(!strcmp( argv[i], "--no-syslog" ))
135 write_syslog = false;
136 continue;
138 if( !strcmp(argv[i], "--help"))
140 print_help();
141 return 1;
143 else if(!strcmp(argv[i], "--version"))
145 print_version();
146 return 1;
148 else if(argv[i][0] == '-')
150 print_help();
151 return 1;
153 sections.append(new WvString("Dialer %s", argv[i]), true);
156 else
158 sections.append(new WvString("Dialer Defaults"), true);
161 if( !haveconfig)
163 // Load the system file first...
164 WvString stdconfig("/etc/wvdial.conf");
166 if (!access(stdconfig, F_OK))
167 cfg.load_file(stdconfig);
169 // Then the user specific one...
170 if (homedir)
172 WvString rcfile("%s/.wvdialrc", homedir);
174 if (!access(rcfile, F_OK))
175 cfg.load_file(rcfile);
179 // Inject all of the command line options on into the cfg file in a new
180 // section called Command-Line if there are command line options.
181 if (havecmdlineopts == 1)
183 WvStringList::Iter i(cmdlineopts);
184 for (i.rewind();i.next();)
186 char *name = i().edit();
187 char *value = strchr(name,'=');
189 // Value should never be null since it can't get into the list
190 // if it doesn't have an = in i()
192 *value = 0;
193 value++;
194 name = trim_string(name);
195 value = trim_string(value);
196 cfg.set("Command-Line", name, value);
198 sections.append(new WvString("Command-Line"), true);
201 if(!cfg.isok())
203 return 1;
206 if (chat_mode)
208 if (write_syslog)
210 WvString buf("wvdial[%s]", getpid());
211 syslog = new WvSyslog( buf, false, WvLog::Debug2,
212 WvLog::Debug2 );
214 else
216 // Direct logging to /dev/null as otherwise WvLog hasn't any
217 // receivers and thus will use WvLogConsole to log to stderr.
218 // That can disturb the communication with the modem on
219 // stdin/stdout. - Fixes a bug reported by SUSE on 04/05/04
220 filelog = new WvLogFile( "/dev/null", WvLog::Debug2 );
224 WvDialer dialer(cfg, &sections, chat_mode);
226 if (!chat_mode)
227 if (dialer.isok() && dialer.options.ask_password)
228 dialer.ask_password();
230 if (dialer.dial() == false)
231 return 1;
233 while (!want_to_die && dialer.isok()
234 && dialer.status() != WvDialer::Idle)
236 dialer.select(100);
237 dialer.callback();
240 int retval;
242 if (want_to_die)
244 // Probably dieing from a user signal
245 retval = 2;
248 if ((dialer.status() != WvDialer::Idle) || !dialer.isok())
250 retval = 1;
252 else
254 retval = 0;
257 dialer.hangup();
259 WVRELEASE(filelog);
260 delete syslog;
262 return(retval);