svnno****@sourc*****
svnno****@sourc*****
2010年 1月 29日 (金) 10:54:00 JST
Revision: 107 http://sourceforge.jp/projects/ngms/svn/view?view=rev&revision=107 Author: osiire Date: 2010-01-29 10:53:59 +0900 (Fri, 29 Jan 2010) Log Message: ----------- [NMShell] refactor some functions. Modified Paths: -------------- trunk/source/NMShell/src/info/ngms/commands/border.scala trunk/source/NMShell/src/info/ngms/commands/cd.scala trunk/source/NMShell/src/info/ngms/commands/echo.scala trunk/source/NMShell/src/info/ngms/commands/ls.scala trunk/source/NMShell/src/info/ngms/commands/mkdir.scala trunk/source/NMShell/src/info/ngms/commands/rm.scala trunk/source/NMShell/src/info/ngms/commands/touch.scala trunk/source/NMShell/src/info/ngms/nmshell/NMCommand.scala trunk/source/NMShell/src/info/ngms/nmshell/NMCommandParameterInfo.scala trunk/source/NMTree/src/info/ngms/nmtree/NMPath.scala Removed Paths: ------------- trunk/source/NMShell/src/info/ngms/commands/exit.scala Modified: trunk/source/NMShell/src/info/ngms/commands/border.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/commands/border.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMShell/src/info/ngms/commands/border.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -21,11 +21,12 @@ def parseOption( args : Array[String] ) : Unit = { } - def parameters : List[ NMCommandParameterInfo ] = { + lazy val parameters : List[ NMCommandParameterInfo ] = { Nil } - def doWork( env : NMCommandContext ) : Unit = { + override def doWork( env : NMCommandContext ) : Unit = { + super.doWork(env) (env.stdin,env.stdout) match { case (NMRawStream(in), NMRawStream(out)) => { for(c <- "***\n") { out.write(c) } @@ -40,7 +41,7 @@ out.close } case _ => - println("not support stream type") + errPrintln("not support stream type") } () } Modified: trunk/source/NMShell/src/info/ngms/commands/cd.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/commands/cd.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMShell/src/info/ngms/commands/cd.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -29,17 +29,18 @@ } } - def parameters : List[ NMCommandParameterInfo ] = { + lazy val parameters : List[ NMCommandParameterInfo ] = { Nil } - def doWork( env : NMCommandContext ) : Unit = { + override def doWork( env : NMCommandContext ) : Unit = { + super.doWork(env) options.dst match { case Some(path) => { if(path.exists) NMShellEnvironment.currentPath = path else - print(env, "no such file or directory\n") + outPrintln("no such file or directory") } case _ => () Modified: trunk/source/NMShell/src/info/ngms/commands/echo.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/commands/echo.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMShell/src/info/ngms/commands/echo.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -23,13 +23,14 @@ arguments = args } - def parameters : List[ NMCommandParameterInfo ] = { + lazy val parameters : List[ NMCommandParameterInfo ] = { Nil } - def doWork( env : NMCommandContext ) : Unit = { + override def doWork( env : NMCommandContext ) : Unit = { + super.doWork(env) arguments foreach { arg => print(env, arg + " ") } - print(env, "\n") + outPrintln("") } def inputStreamKind = RawStream() Deleted: trunk/source/NMShell/src/info/ngms/commands/exit.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/commands/exit.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMShell/src/info/ngms/commands/exit.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -1,33 +0,0 @@ -/* - * Next Generation Management System Project - * Copyright(c) 2009, NGMS Project Team All Rights Reserved. - */ -package info.ngms.commands - -import info.ngms.nmshell.NMCommand -import info.ngms.nmshell.NMCommandContext -import info.ngms.nmshell.NMCommandStream -import info.ngms.nmshell.NMCommandStreamKind -import info.ngms.nmshell.NMCommandParameterInfo -import info.ngms.nmshell.NMRawStream -import info.ngms.nmshell.RawStream -import info.ngms.nmtree.NMTreeElements - -class exit extends NMCommand { - val name = "exit" - - def parseOption( args : Array[String] ) : Unit = { - } - - def parameters : List[ NMCommandParameterInfo ] = { - Nil - } - - def doWork( env : NMCommandContext ) : Unit = { - // terminate - System.exit(0) - } - - def inputStreamKind = RawStream() - def outputStreamKind = RawStream() -} Modified: trunk/source/NMShell/src/info/ngms/commands/ls.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/commands/ls.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMShell/src/info/ngms/commands/ls.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -20,13 +20,14 @@ def parseOption( args : Array[String] ) : Unit = { } - def parameters : List[ NMCommandParameterInfo ] = { + lazy val parameters : List[ NMCommandParameterInfo ] = { Nil } - def doWork( env : NMCommandContext ) : Unit = { + override def doWork( env : NMCommandContext ) : Unit = { + super.doWork(env) val current = NMShellEnvironment.currentPath - current.children.foreach( p => print(env, p.basename + "\n")) + current.children.foreach( p => outPrintln(p.basename)) } def inputStreamKind = RawStream() Modified: trunk/source/NMShell/src/info/ngms/commands/mkdir.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/commands/mkdir.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMShell/src/info/ngms/commands/mkdir.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -4,9 +4,6 @@ */ package info.ngms.commands -//import scala.util.parsing.combinator.RegexParsers -//import scala.util.parsing.combinator.Parsers -//import scala.util.parsing.combinator._ import info.ngms.nmshell.NMCommand import info.ngms.nmshell.NMCommandContext import info.ngms.nmshell.NMCommandParameterInfo @@ -59,75 +56,61 @@ */ val name : String = commandName() - /** - * 与えられたコマンドライン引数をもとに、オプション引数を解析します。 - * - * @args コマンドライン引数の配列 - */ - def parseOption( args : Array[String] ) : Unit = { - commandLine = createCommandLine(args) + private[this] val mode = new NMCommandParameterInfo { + val shortName : String = "m" + val longName : String = "mode" + val selection : List[String] = Nil + val need : Boolean = false + val default : Option[String] = None + val explain : String = "set file mode" + val hasArg : Boolean = true } - private[this] var commandLine : CommandLine = null + private[this] val parents = new NMCommandParameterInfo { + val shortName : String = "p" + val longName : String = "parents" + val selection : List[String] = Nil + val need : Boolean = false + val default : Option[String] = None + val explain : String = "no error if existing, make parent directories as needed" + val hasArg : Boolean = false + } - private[this] val commandLineOptions : Options = new Options() - - private[this] def setupCommandLineOptions() : Unit = { - commandLineOptions. - addOption("m", "mode", true, "set file mode"). - addOption("p", "parents", false, "no error if existing, make parent directories as needed"). - addOption("v", "verbose", false, "print a message for each created directory") + private[this] val verbose = new NMCommandParameterInfo { + val shortName : String = "v" + val longName : String = "verbose" + val selection : List[String] = Nil + val need : Boolean = false + val default : Option[String] = None + val explain : String = "print a message for each created directory" + val hasArg : Boolean = false } -// private object ModeParsers extends RegexParsers { -// -// def modes : Parser[List[UsersAndOperation]] = {repsep(mode, ",")} -// -// def mode : Parser[UsersAndOperation] = {(opt(users)) ~ operatorAndOperands ^^ { -// case Some(users) ~ operatorAndOperands => new UsersAndOperation(users, operatorAndOperands) -// case None ~ operatorAndOperands => new UsersAndOperation(null, operatorAndOperands) -// } -// } -// -//// def users : Parser[List[String]] = {rep("u" | "a")} -// def users : Parser[List[String]] = {rep("u" | "g" | "o" | "a")} -// -// def operatorAndOperands : Parser[OperatorAndOperands] = { -// operator ~ rep(operand) ^^ { -// case operator ~ operands => new OperatorAndOperands(operator, operands) -// } -// } -// -// def operator : Parser[String] = {"+" | "-" | "="} -// -// def operand : Parser[String] = {"r" | "w" | "x"} -// -// class UsersAndOperation (val users : List[String], val operation : OperatorAndOperands) { -// override def toString : String = { -// return "UsersAndOperation(" + users + ", " + operation + ")" -// } -// } -// -// class OperatorAndOperands (val operator : String, val operands : List[String]) { -// override def toString : String = { -// return "OperatorAndOperands(" + operator + ", " + operands + ")" -// } -// } -// -// } - - private[commands] def createCommandLine( args : Array[String] ) : CommandLine = { -//println("args.length: " + args.length) -//args.foreach((arg: String) => {println("arg: '" + arg + "'")}) - setupCommandLineOptions() - return new PosixParser().parse(commandLineOptions, args) + /** + * パラメーター情報 + */ + lazy val parameters : List[ NMCommandParameterInfo ] = { + List(mode, parents, verbose) } + + private[this] lazy val options = { + def param2opt( op : Options, param : NMCommandParameterInfo ) : Options = { + op.addOption(param.shortName, param.longName, param.hasArg, param.explain) + } + parameters.foldLeft (new Options()) ( (op, param) => param2opt(op, param) ) + } + private[this] var commandLine : CommandLine = null + + def createCommandLine( args : Array[String]) = new PosixParser().parse(options, args) + /** + * 与えられたコマンドライン引数をもとに、オプション引数を解析します。 * + * @args コマンドライン引数の配列 */ - def parameters : List[ NMCommandParameterInfo ] = { - Nil + def parseOption( args : Array[String] ) : Unit = { + commandLine = createCommandLine(args) } /** @@ -144,8 +127,6 @@ */ def outputStreamKind : NMCommandStreamKind = new RawStream() - private[this] var context : NMCommandContext = null - // private[this] var modeSetting : ModeSetting = null // // private[this] class ModeSetting (modes : List[ModeParsers.UsersAndOperation]) { @@ -277,13 +258,13 @@ * * @param givenContext プロセスの現在の状態 */ - def doWork ( givenContext : NMCommandContext ) : Unit = { + override def doWork ( context : NMCommandContext ) : Unit = { + super.doWork(context) //println("NMShellEnvironment.currentPath.canonical: " + NMShellEnvironment.currentPath.canonical) - context = givenContext val specifiedNewDirs : Array[String] = commandLine.getArgs() if (specifiedNewDirs.length == 0) { errPrintln(name + ": missing operand") - printUsage(context) + printUsage() return } if (commandLine.hasOption("m")) { @@ -298,7 +279,7 @@ // } // modeSetting = new ModeSetting(parseResult.get) } - specifiedNewDirs.foreach((dir: String) => {processArg(context, dir)}) + specifiedNewDirs.foreach( (dir: String) => { processArg(context, dir) } ) } // private[this] def setFileMode(path : NMPath) : Boolean = { @@ -307,7 +288,7 @@ // } private[this] def processArg(context : NMCommandContext, dir : String) : Unit = { - val newDirPath : NMPath = if(pathNameIsAbsolute(dir)) { + val newDirPath : NMPath = if( NMPath.isAbsolute(dir) ) { //println("creating directory with the given absolute path: " + dir) new NMPath(dir) } else { @@ -317,10 +298,6 @@ createDir(newDirPath) } - private[this] def pathNameIsAbsolute(pathName : String) : Boolean = { - return pathName.startsWith(NMPath.root) - } - private[this] def createDir(newDirPath : NMPath) : Unit = { //print(context, name + ": creating directory `" + newDirPath + "' ...\n") if(newDirPath.exists) { @@ -352,22 +329,12 @@ } private[this] def errorMessage(path : NMPath) : String = { - return name + ": cannot create directory `" + path.canonical + "'" + name + ": cannot create directory `" + path.canonical + "'" } - private[this] def printUsage(context : NMCommandContext) : Unit = { + private[this] def printUsage() : Unit = { new HelpFormatter().printHelp(name + " [OPTION]... DIRECTORY...\n", - "Create the DIRECTORY(ies), if they do not already exist.", commandLineOptions, null) + "Create the DIRECTORY(ies), if they do not already exist.", options, null) } - private[this] val lineSeparator = "\n" - - private[this] def outPrintln(text : String) : Unit = { - print(context, text + lineSeparator) - } - - private[this] def errPrintln(text : String) : Unit = { - print_err(context, text + lineSeparator) - } - } Modified: trunk/source/NMShell/src/info/ngms/commands/rm.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/commands/rm.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMShell/src/info/ngms/commands/rm.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -36,14 +36,15 @@ rmArgs = rmArgs.reverse } - def parameters : List[ NMCommandParameterInfo ] = { + lazy val parameters : List[ NMCommandParameterInfo ] = { Nil } - def doWork( env : NMCommandContext ) : Unit = { + override def doWork( env : NMCommandContext ) : Unit = { + super.doWork(env) val current = NMShellEnvironment.currentPath if (rmArgs == Nil) { - print_err(env, "not enouch arguments\n") + errPrintln("not enouch arguments") } else { val ops = rmArgs map { rmArg => Delete(current.concat(rmArg.name), rmArg.recursive) Modified: trunk/source/NMShell/src/info/ngms/commands/touch.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/commands/touch.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMShell/src/info/ngms/commands/touch.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -28,18 +28,19 @@ if (args.length > 0) fileNames = Some(args) } - def parameters : List[ NMCommandParameterInfo ] = { + lazy val parameters : List[ NMCommandParameterInfo ] = { Nil } - def doWork( env : NMCommandContext ) : Unit = { + override def doWork( env : NMCommandContext ) : Unit = { + super.doWork(env) val current = NMShellEnvironment.currentPath fileNames match { case Some(names) => names foreach { name => NMTree.createStream(current.concat(name)) } - case None => print_err(env, "missing file operand\n") + case None => errPrintln("missing file operand") } } Modified: trunk/source/NMShell/src/info/ngms/nmshell/NMCommand.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/nmshell/NMCommand.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMShell/src/info/ngms/nmshell/NMCommand.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -8,9 +8,12 @@ trait NMCommand { val name : String + val parameters : List[ NMCommandParameterInfo ] + var context : NMCommandContext = null def parseOption( args : Array[String] ) : Unit - def parameters : List[ NMCommandParameterInfo ] - def doWork( env : NMCommandContext ) : Unit + def doWork( env : NMCommandContext ) : Unit = { + context = env + } /** * コマンドの受けつけるストリームの種類を取得する @@ -26,7 +29,7 @@ */ def outputStreamKind : NMCommandStreamKind - def print_stream( stream : NMCommandStream, msg : String ) : Unit = { + def printStream( stream : NMCommandStream, msg : String ) : Unit = { stream match { case NMRawStream(stream) => { for(c <- msg) { @@ -39,11 +42,22 @@ } } - def print( env : NMCommandContext, msg : String ) : Unit = { - print_stream(env.stdout, msg) + def outPrint( msg : String ) : Unit = { + printStream(context.stdout, msg) } - def print_err( env : NMCommandContext, msg : String ) : Unit = { - print_stream(env.stderr, msg) + def errPrint( msg : String ) : Unit = { + printStream(context.stderr, msg) } + + private[this] val lineSeparator = "\n" + + def outPrintln(msg : String) : Unit = { + outPrint(msg + lineSeparator) + } + + def errPrintln(msg : String) : Unit = { + errPrint(msg + lineSeparator) + } + } Modified: trunk/source/NMShell/src/info/ngms/nmshell/NMCommandParameterInfo.scala =================================================================== --- trunk/source/NMShell/src/info/ngms/nmshell/NMCommandParameterInfo.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMShell/src/info/ngms/nmshell/NMCommandParameterInfo.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -11,4 +11,5 @@ val need : Boolean val default : Option[String] val explain : String + val hasArg : Boolean } Modified: trunk/source/NMTree/src/info/ngms/nmtree/NMPath.scala =================================================================== --- trunk/source/NMTree/src/info/ngms/nmtree/NMPath.scala 2010-01-28 12:24:13 UTC (rev 106) +++ trunk/source/NMTree/src/info/ngms/nmtree/NMPath.scala 2010-01-29 01:53:59 UTC (rev 107) @@ -182,6 +182,10 @@ makeInnerPath(path).diff(makeInnerPath(base)) } + def isAbsolute( path : String ) : Boolean = { + path.startsWith(root) + } + } /**