/* net_sccs.java Copyright 1998 Kyle R. Burton This is free software; you can redistribute it and/or modify it under the tearms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this software; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ /** * * file: net_sccs.java * * description: this is an implmentation of the client-side of net_sccs. * * * revision list: * Sat May 2 15:31:04 EDT 1998 KRB - merged add, get, checkout, and checkin functions * into this file. Now there's only one file...this was an amlost complete * re-write of the core modules. * Sun May 3 14:27:15 EDT 1998 KRB - added local configuraiton file loading, * and creation code... * */ import java.io.*; import java.net.*; import java.util.*; public class net_sccs { public static final int def_buff_len = 1024; public static final String file_begin_string = ""; public static final String file_end_string = ""; // to read the conf info, we'll search these in the order they're // listed here -- env var, home dir, current dir, and root of c: drive... public String conf_env = "NET_SCCS_HOST"; public String conf_home = "~/.net_sccs"; public String conf_pwd = "./.net_sccs"; public String conf_win = "file://C|/.net_sccs"; public String conf_use = null; public String language = "english"; public String default_host = "localhost"; String file = null; String path = null; String version = null; String uname = null; String password = null; String action = null; String comment = null; String alt_conf = null; boolean ask_for_uname = true; public boolean debug = false; boolean show_help = false; net_msgs msgs = null; Hashtable actions = null; String net_sccs_host = null; BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); public static void main( String args[] ) { net_sccs app = new net_sccs(args); return; } public net_sccs( String args[] ) { actions = new Hashtable(); // these are all the currently supported actions... actions.put( "add", "a" ); actions.put( "get", "g" ); actions.put( "checkout", "o" ); actions.put( "checkin", "i" ); getopts opts = new getopts( "a:c:f:hl:o:p:r:u:v:w:x", args ); action = opts.opt('a') ? opts.get_value('a') : null; comment = opts.opt('c') ? opts.get_value('c') : null; file = opts.opt('f') ? opts.get_value('f') : null; show_help = opts.opt('h'); language = opts.opt('l') ? opts.get_value('l') : null; alt_conf = opts.opt('o') ? opts.get_value('o') : null; path = opts.opt('p') ? opts.get_value('p') : null; default_host = opts.opt('r') ? opts.get_value('r') : null; uname = opts.opt('u') ? opts.get_value('u') : null; version = opts.opt('v') ? opts.get_value('v') : null; password = opts.opt('w') ? opts.get_value('w') : null; debug = opts.opt('x'); String home = System.getProperty("user.home"); if( null != home ) { conf_home = home + "/.net_sccs"; if( debug ) { System.out.println("[client] setting conf_home = " + conf_home ); } } else { // assume dos style platform conf_home = "c:\\.net_sccs"; if( debug ) { System.out.println("[client] setting conf_home = " + conf_home ); } } // try to get the config info... if( !configure() ) { // error System.out.println("[client] Error, unable to find configuration file."); if( create_config_from_user() ) { System.out.println("[client] attempting to re-read the config file."); if( !configure() ) { System.out.println("[client] Error, unable to read configuration file."); System.exit(1); } } else { System.exit(1); } } try { // to get any missing arguments from the user... while( null == uname || uname.length() < 1 ) { System.out.print("[client] User: "); uname = stdin.readLine(); } while( null == password || password.length() < 1 ) { System.out.print("[client] Password: "); password = stdin.readLine(); } while( null == action || action.length() < 1 || !actions.containsKey(action) ) { System.out.print("[client] Action: "); action = stdin.readLine(); if( !actions.containsKey(action) ) { System.out.println("[client] Error, valid actions are: add, get, checkout, and checkin."); action = null; } } while( null == file || file.length() < 1 ) { System.out.print("[client] File: "); file = stdin.readLine(); } while( null == path || path.length() < 1 ) { System.out.print("[client] Path: "); path = stdin.readLine(); } // ask for the version if appropriate... if( 0 == action.compareTo("get") || 0 == action.compareTo("checkout") || 0 == action.compareTo("checkin") ) { if( null == version ) { System.out.print("[client] Version [enter for latest]: "); version = stdin.readLine(); version = version.length() < 1 ? null : version; } } // if( actions.containsKey } catch( IOException ioe ) { System.out.println("[client] Error reading from standard in."); System.out.println("[client] Unable to obtain needed information, aborting."); if( debug ) { System.out.println(ioe); } } if( debug ) { System.out.println("[client] Action: " + action ); System.out.println("[client] File: " + file ); System.out.println("[client] Path: " + path ); System.out.println("[client] Version: " + version ); System.out.println("[client] User: " + uname ); System.out.println("[client] Password: " + password ); System.out.println("[client] [net_sccs] alt_conf = " + alt_conf ); System.out.println("[client] [net_sccs] show_help = " + show_help ); } URL net_sccs_server_url = null; try { // to create the serverr URL net_sccs_server_url = new URL("http://" + net_sccs_host + "/net_sccs/net_sccs.cgi"); } catch( MalformedURLException mue ) { System.out.println("[client] Error creating URL object for communicating " + "with the net_sccs server."); if( debug ) { System.out.println(mue); } System.exit(1); } String http_request = null; String post_data = null; if( 0 == action.compareTo("add") || 0 == action.compareTo("checkin") ) { post_data = "action=" + URLEncoder.encode(action); if( debug ) { post_data += "&debug=true"; } post_data += "&uname=" + URLEncoder.encode(uname); post_data += "&password=" + URLEncoder.encode(password); post_data += "&file_name=" + URLEncoder.encode(file); post_data += "&path=" + URLEncoder.encode(path); post_data += "&file="; if( null != comment ) { post_data += "&comment=" + URLEncoder.encode(comment); } post_data += get_post_data(file); http_request = "POST /cgi-bin/net_sccs/net_sccs.cgi HTTP/1.0\n"; http_request += "Content-type: application/x-www-form-urlencoded\n"; http_request += "Content-length: " + post_data.length() + "\n"; http_request += "\n"; } else if( 0 == action.compareTo("get") || 0 == action.compareTo("checkout") ) { http_request = "GET /cgi-bin/net_sccs/net_sccs.cgi?"; http_request += "action=" + URLEncoder.encode(action); if( debug ) { post_data += "&debug=true"; } http_request += "&uname=" + URLEncoder.encode(uname); http_request += "&password=" + URLEncoder.encode(password); http_request += "&file_name=" + URLEncoder.encode(file); http_request += "&path=" + URLEncoder.encode(path); if( null != version ) { http_request += "&version=" + URLEncoder.encode(version); } http_request += "\n"; } else { System.out.println("[client] Error, don't know how to perform action: " + action ); System.exit(1); } if( debug ) { System.out.println("[client] Request is: " + http_request ); System.out.flush(); } try { // to create a socket, send the request, and get the reply... String host = net_sccs_server_url.getHost(); int port = net_sccs_server_url.getPort(); port = -1 == port ? 80 : port; Socket socket = new Socket(host,port); BufferedReader from_server = null; from_server = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ); PrintWriter to_server = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); to_server.print(http_request); if( null != post_data ) { if( debug ) { System.out.println("[client] Sending post data, length " + post_data.length() ); } to_server.print(post_data); } to_server.flush(); String line = new String(); System.out.println("[client] Server Reply:"); PrintWriter to_file = null; while( true ) { line = from_server.readLine(); if( null == line ) { break; } if( 0 == line.compareTo(file_begin_string) ) { to_file = new PrintWriter( new FileOutputStream(file) ); if( debug ) { System.out.println("[client] Opened file: " + file + " for writing."); } while( true ) { line = from_server.readLine(); if( null == line ) { break; } if( 0 == line.compareTo(file_end_string) ) { if( debug ) { System.out.println("[client] received file, closing local file."); } to_file.close(); break; } // write it to the file... to_file.println(line); } continue; } System.out.println( line ); } System.out.println(); socket.close(); } catch( IOException ioe ) { System.out.println("[client] Error communicating with host, or writing to local file."); if( debug ) { System.out.println(ioe); } } } public String get_post_data( String file ) { String post_data = null; byte data[] = new byte[def_buff_len]; int bytes = 0; try { // to read the local file into a string... post_data = new String(); FileInputStream from_file = new FileInputStream(file); while( true ) { bytes = from_file.read(data); if( 0 == bytes ) { continue; } if( -1 == bytes ) { break; } post_data += URLEncoder.encode( new String(data,0,bytes) ); } from_file.close(); } catch( FileNotFoundException fnfe ) { System.out.println("[client] Error reading from file: " + file); System.out.println("[client] File not found."); if( debug ) { System.out.println(fnfe); } } catch( IOException ioe ) { System.out.println("[client] Error reading from file: " + file ); if( debug ) { System.out.println(ioe); } } return post_data; } public boolean create_config_from_user() { System.out.print("[client] Would you like to create one? (y/n): "); String config_file = null, net_sccs_host = null, language = null; String user_name = null, default_path = null; BufferedWriter br_conf = null; try { // to get settings from the user... String response = stdin.readLine(); if( null == response || 0 == response.compareTo("n") ) { return false; } System.out.println("[client] There are three (default) places where net_sccs looks for "); System.out.println("[client] it's configuration files. They are:"); System.out.println(" " + conf_home ); System.out.println(" ./.net_sccs"); System.out.println(" file://C|/.net_sccs"); System.out.print("[client] Where should net_sccs store it's configuration? [" + conf_home + "]: "); config_file = stdin.readLine(); if( null == config_file || config_file.length() < 1 ) config_file = conf_home; try { // to open the file, and create a BufferedReader from it... br_conf = new BufferedWriter( new FileWriter(config_file) ); } catch( IOException ioe ) { System.out.println("[client] Error opening file : " + config_file ); System.exit(1); } while( null == net_sccs_host ) { System.out.print("[client] Please Enter the Default net_sccs host [none]: "); net_sccs_host = stdin.readLine(); } String def_uname = System.getProperty("user.name"); if( null == def_uname ) def_uname = "none"; System.out.print("[client] Default Username [" + def_uname + "]: "); user_name = stdin.readLine(); if( null == user_name || user_name.length() < 1 ) user_name = def_uname; System.out.println("[client] Using username: " + user_name ); System.out.print("[client] What language would you preferr? [english]: "); language = stdin.readLine(); if( null == language || language.length() < 1 ) language = "english"; System.out.println("[client] Using language: " + language ); System.out.print("[client] Default path [/]: "); default_path = stdin.readLine(); if( null == default_path || default_path.length() < 1 ) default_path = "/"; } catch( IOException ioe ) { System.out.println("[client] Error reading from stdin."); if( debug ) { System.out.println(ioe); } } try { // to write the settings to the config file... String tmp = new String(); tmp = "# net_sccs configuration file -- autocreated on: "; Date now = new Date(); tmp += (new java.util.Date()).toString(); br_conf.write( tmp, 0, tmp.length() ); br_conf.newLine(); tmp = "#"; br_conf.write( tmp, 0, tmp.length() ); br_conf.newLine(); tmp = "language " + language; br_conf.write( tmp, 0, tmp.length() ); br_conf.newLine(); tmp = "net_sccs_host " + net_sccs_host; br_conf.write( tmp, 0, tmp.length() ); br_conf.newLine(); tmp = "default_user " + user_name; br_conf.write( tmp, 0, tmp.length() ); br_conf.newLine(); tmp = "default_path " + default_path; br_conf.write( tmp, 0, tmp.length() ); br_conf.newLine(); // also use a password? br_conf.close(); } catch( IOException ioe ) { System.out.println("[client] Error writing to conf file: " + config_file ); return false; } return true; } public boolean configure() { BufferedReader br_conf = null; if( null != alt_conf ) { if( debug ) { System.out.println("[client] trying alternate config file: " + alt_conf ); } try { // to open the alternative configuration file... br_conf = new BufferedReader( new FileReader(alt_conf) ); return read_config_info( br_conf ); } catch( IOException ioe ) { if( debug ) { System.out.println("[client] Error reading configuration information, " + "from alternate file. File not found: " + alt_conf ); System.out.println(ioe); } } } // this isn't implemented yet... if(false) { // try the environment variable... String host = java.lang.System.getProperty("NET_SCCS_HOST"); if( debug ) { System.out.println("[client] trying env var: NET_SCCS_HOST" ); } if( null != host ) { if( debug ) { System.out.println("[client] trying to get config info from: " + host ); } try { // to read the config from the url specified in the environment variable... return read_config_info( br_conf ); } catch( IOException ioe ) { System.out.println("[client] error retreiving config file."); if( debug ) { System.out.println(ioe); } return false; } } } // try for a file in their home dir... if( debug ) { System.out.println("[client] trying file: " + conf_home ); } try { br_conf = new BufferedReader( new FileReader(conf_home) ); conf_use = conf_home; return read_config_info(br_conf); } catch( IOException ioe ) { if( debug ) { System.out.println("[client] Error reading configuration information, " + "from file. File not found: " + conf_pwd ); System.out.println(ioe); } } // try for a file in the current dir... if( debug ) { System.out.println("[client] trying file: " + conf_pwd ); } try { br_conf = new BufferedReader( new FileReader(conf_pwd) ); conf_use = conf_pwd; return read_config_info(br_conf); } catch( IOException ioe ) { if( debug ) { System.out.println("[client] Error reading configuration information, " + "from file. File not found: " + conf_pwd ); System.out.println(ioe); } } // try for a file at the specified URL... if( debug ) { System.out.println("[client] trying file: " + conf_win ); } try { br_conf = new BufferedReader( new FileReader(conf_win) ); return read_config_info(br_conf); } catch( IOException ioe ) { if( debug ) { System.out.println("[client] Error reading configuration information, " + "from file. File not found: " + conf_pwd ); System.out.println(ioe); } } // not found...no config file present return false; } public boolean read_config_info( BufferedReader br_conf ) throws IOException { net_msgs conf = new net_msgs(); if( !conf.init(br_conf) ) { System.out.println("[client] Error reading from config file."); return false; } if( conf.containsKey("language") ) language = conf.get("language"); if( conf.containsKey("net_sccs_host") ) net_sccs_host = conf.get("net_sccs_host"); if( conf.containsKey("default_user") ) uname = conf.get("default_user"); if( conf.containsKey("default_password") ) password = conf.get("default_password"); try { msgs = new net_msgs("http://" + net_sccs_host + "/net_sccs/msgs/" + language + "net_sccs.strings.txt"); if( debug ) { System.out.println("[client] read message file."); } } catch( MalformedURLException mue ) { System.out.println("[client] Error retreiving message file."); if( debug ) { System.out.println(mue); } System.out.println("[client] Unable to continue."); System.exit(1); } catch( IOException ioe ) { System.out.println("[client] Error retreiving message file."); if( debug ) { System.out.println(ioe); } System.out.println("[client] Unable to continue."); System.exit(1); } return true; } }