program ModemCtl; {$MODE OBJFPC} uses SysUtils, StrUtils, Sockets; type //Connection state Connection = record Call: boolean; Addr: longword; Port: word; Answer: boolean; Hang: boolean; end; var ModemConn: Connection; //State of the modem State: file of Connection; //File storing the state of the modem Addr: in_addr; //Temporary variable begin //Check the arguments if ParamStr (1) = '-c' then begin if ParamCount > 2 then begin writeln ('Usage: modemctl [-c address:port]/-a/-h'); halt (1); end; end else begin if ParamCount > 1 then begin writeln ('Usage: modemctl [-c address:port]/-a/-h'); halt (1); end; end; //Assign the state file assign (State, ExpandFileName ('~/.thingamajig/connection')); //Read existing state if any if FileExists (ExpandFileName ('~/.thingamajig/connection')) then begin try reset (State); read (State, ModemConn); close (State); except end; end //Or else assign a default state else begin ModemConn.Call := false; ModemConn.Addr := 0; ModemConn.Port := 0; ModemConn.Answer := false; ModemConn.Hang := false; end; //Set the connection if ParamStr (1) = '-c' then begin ModemConn.Call := true; try Addr := StrToHostAddr (ExtractDelimited (1, ParamStr (2), [':'])); ModemConn.Addr := Addr.s_addr; except writeln ('Usage: modemctl [-c address:port]/-a/-h'); halt (1); end; try ModemConn.Port := StrToInt (ExtractDelimited (2, ParamStr (2), [':'])); except writeln ('Usage: modemctl [-c address:port]/-a/-h'); halt (1); end; ModemConn.Answer := false; ModemConn.Hang := false; end else if ParamStr (1) = '-a' then begin ModemConn.Call := false; ModemConn.Addr := 0; ModemConn.Port := 0; ModemConn.Answer := true; ModemConn.Hang := false; end else if ParamStr (1) = '-h' then begin ModemConn.Call := false; ModemConn.Addr := 0; ModemConn.Port := 0; ModemConn.Answer := false; ModemConn.Hang := true; end else begin writeln ('Usage: modemctl [-c address:port]/-a/-h'); halt (1); end; //Write the state try rewrite (State); write (State, ModemConn); close (State); except writeln ('Error: could not set the connection'); end; end.