From 1e26047c3b38349402858ce0a1a5bf747b9f2914 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Sat, 7 Dec 2013 20:30:44 +0200 Subject: adding some provisions for writing lower-level unit tests targeting specific sub-systems of the compiler see sigmatch as an example. tests are compiled only when the compiler is compiled with -d:selftest to execute them, just run the resulting binary without arguments --- lib/pure/unittest.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim index 71f4d498b..20f5c610e 100644 --- a/lib/pure/unittest.nim +++ b/lib/pure/unittest.nim @@ -98,8 +98,12 @@ template fail* = when not defined(ECMAScript): if AbortOnError: quit(1) - - TestStatusIMPL = FAILED + + when defined(TestStatusIMPL): + TestStatusIMPL = FAILED + else: + program_result += 1 + checkpoints = @[] macro check*(conditions: stmt): stmt {.immediate.} = -- cgit 1.4.1-2-gfad0 From e7e8c7706240c4bef17533da63df988d228ba127 Mon Sep 17 00:00:00 2001 From: Michał Zieliński Date: Mon, 2 Dec 2013 18:34:32 +0100 Subject: Make quoteIfContainsWhite quote argument, so it can be safely passed to shell. On Windows put it in double quotes and escape double quotes using backslash. On Posix put it in single quotes and escape single quotes using '"'"'. This commit changes what quoteIfContainsWhite does, but before that change it was used incorrectly all over standard library, which caused security issues. --- lib/pure/strutils.nim | 51 +++++++++++++++++++++++++++++++++++++++++++-------- web/news.txt | 2 ++ 2 files changed, 45 insertions(+), 8 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index a4aa81578..0299f5cd2 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -709,14 +709,6 @@ proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} = if result != -1: return return -1 -proc quoteIfContainsWhite*(s: string): string = - ## returns ``'"' & s & '"'`` if `s` contains a space and does not - ## start with a quote, else returns `s` - if find(s, {' ', '\t'}) >= 0 and s[0] != '"': - result = '"' & s & '"' - else: - result = s - proc contains*(s: string, c: char): bool {.noSideEffect.} = ## Same as ``find(s, c) >= 0``. return find(s, c) >= 0 @@ -780,6 +772,49 @@ proc replaceWord*(s, sub: string, by = ""): string {.noSideEffect, # copy the rest: add result, substr(s, i) +proc quoteIfContainsWhite*(s: string): string {.noSideEffect.} = + ## Quote s, so it can be safely passed to shell. + when defined(Windows): + # based on Python's subprocess.list2cmdline + # see http://msdn.microsoft.com/en-us/library/17w5ykft.aspx + let needQuote = {' ', '\t'} in s or s.len == 0 + + result = "" + var backslashBuff = "" + if needQuote: + result.add("\"") + + for c in s: + if c == '\\': + backslashBuff.add(c) + elif c == '\"': + result.add(backslashBuff) + result.add(backslashBuff) + backslashBuff.setLen(0) + result.add("\\\"") + else: + if backslashBuff.len != 0: + result.add(backslashBuff) + backslashBuff.setLen(0) + result.add(c) + + if needQuote: + result.add("\"") + + else: + # based on Python's pipes.quote + const safeUnixChars = {'%', '+', '-', '.', '/', '_', ':', '=', '@', + '0'..'9', 'A'..'Z', 'a'..'z'} + if s.len == 0: + return "''" + + let safe = s.allCharsInSet(safeUnixChars) + + if safe: + return s + else: + return "'" & s.replace("'", "'\"'\"'") & "'" + proc delete*(s: var string, first, last: int) {.noSideEffect, rtl, extern: "nsuDelete".} = ## Deletes in `s` the characters at position `first` .. `last`. This modifies diff --git a/web/news.txt b/web/news.txt index 1b492fa97..f919089a5 100644 --- a/web/news.txt +++ b/web/news.txt @@ -28,6 +28,8 @@ Changes affecting backwards compatibility require an error code to be passed to them. This error code can be retrieved using the new ``OSLastError`` proc. - ``os.parentDir`` now returns "" if there is no parent dir. +- ``quoteIfContainsWhite`` now escapes argument in such way that it can be safely + passed to shell, instead of just adding double quotes. Compiler Additions -- cgit 1.4.1-2-gfad0 From de3b7cd413e02498c0ee5554f55f9c92d2baa9ab Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Mon, 9 Dec 2013 00:09:03 +0200 Subject: progress towards adding negative type classes [unittest bugfixes] the block form of check now allows comments errors when inspecting the arguments of var-accepting procs --- compiler/ast.nim | 11 ++- compiler/ccgutils.nim | 2 +- compiler/jsgen.nim | 2 +- compiler/semexprs.nim | 2 +- compiler/seminst.nim | 5 +- compiler/semtypes.nim | 9 +- compiler/sigmatch.nim | 251 +++++++++++++++++++++++++++++++++++++++++++++----- compiler/types.nim | 25 ++--- lib/pure/unittest.nim | 6 +- 9 files changed, 267 insertions(+), 46 deletions(-) (limited to 'lib/pure') diff --git a/compiler/ast.nim b/compiler/ast.nim index 5a5d87d06..1e5276d68 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -335,12 +335,19 @@ type tyConst, tyMutable, tyVarargs, tyIter, # unused tyProxy # used as errornous type (for idetools) - tyTypeClass, + tyTypeClass + tyAnd + tyOr + tyNot + tyAnything + tyParametricTypeClass # structured similarly to tyGenericInst + # lastSon is the body of the type class const tyPureObject* = tyTuple GcTypeKinds* = {tyRef, tySequence, tyString} tyError* = tyProxy # as an errornous node should match everything + tyTypeClasses* = {tyTypeClass, tyParametricTypeClass, tyAnd, tyOr, tyNot, tyAnything} type TTypeKinds* = set[TTypeKind] @@ -377,6 +384,7 @@ type # used as return types for return type inference) tfAll, # type class requires all constraints to be met (default) tfAny, # type class requires any constraint to be met + tfNot, # type class with a negative check tfCapturesEnv, # whether proc really captures some environment tfByCopy, # pass object/tuple by copy (C backend) tfByRef, # pass object/tuple by reference (C backend) @@ -1416,3 +1424,4 @@ proc isAtom*(n: PNode): bool {.inline.} = proc isEmptyType*(t: PType): bool {.inline.} = ## 'void' and 'stmt' types are often equivalent to 'nil' these days: result = t == nil or t.kind in {tyEmpty, tyStmt} + diff --git a/compiler/ccgutils.nim b/compiler/ccgutils.nim index c37754511..310f7204a 100644 --- a/compiler/ccgutils.nim +++ b/compiler/ccgutils.nim @@ -86,7 +86,7 @@ proc GetUniqueType*(key: PType): PType = if result == nil: gCanonicalTypes[k] = key result = key - of tyTypeDesc, tyTypeClass: + of tyTypeDesc, tyTypeClasses: InternalError("value expected, but got a type") of tyGenericParam: InternalError("GetUniqueType") diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 54ee43069..a3c88824d 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -130,7 +130,7 @@ proc mapType(typ: PType): TJSTypeKind = result = etyObject of tyNil: result = etyNull of tyGenericInst, tyGenericParam, tyGenericBody, tyGenericInvokation, tyNone, - tyForward, tyEmpty, tyExpr, tyStmt, tyTypeDesc, tyTypeClass: + tyForward, tyEmpty, tyExpr, tyStmt, tyTypeDesc, tyTypeClasses: result = etyNone of tyProc: result = etyProc of tyCString: result = etyString diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 337224aef..5abe8da83 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -322,7 +322,7 @@ proc isOpImpl(c: PContext, n: PNode): PNode = var match: bool let t2 = n[2].typ case t2.kind - of tyTypeClass: + of tyTypeClasses: var m: TCandidate InitCandidate(m, t2) match = matchUserTypeClass(c, m, emptyNode, t2, t1) != nil diff --git a/compiler/seminst.nim b/compiler/seminst.nim index 0cf5086a8..d7d64fd54 100644 --- a/compiler/seminst.nim +++ b/compiler/seminst.nim @@ -20,7 +20,8 @@ proc instantiateGenericParamList(c: PContext, n: PNode, pt: TIdTable, if a.kind != nkSym: InternalError(a.info, "instantiateGenericParamList; no symbol") var q = a.sym - if q.typ.kind notin {tyTypeDesc, tyGenericParam, tyTypeClass, tyExpr}: continue + if q.typ.kind notin {tyTypeDesc, tyGenericParam, tyExpr}+tyTypeClasses: + continue var s = newSym(skType, q.name, getCurrOwner(), q.info) s.flags = s.flags + {sfUsed, sfFromGeneric} var t = PType(IdTableGet(pt, q.typ)) @@ -193,7 +194,7 @@ proc fixupProcType(c: PContext, genericType: PType, if result == nil: return case genericType.kind - of tyGenericParam, tyTypeClass: + of tyGenericParam, tyTypeClasses: result = inst.concreteTypes[genericType.sym.position] of tyTypeDesc: result = inst.concreteTypes[genericType.sym.position] diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 92f47f585..6c9c476d9 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -676,8 +676,13 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode, if lifted != nil: paramType.sons[i] = lifted result = paramType - - if result != nil: + + if paramType.lastSon.kind == tyTypeClass: + result = paramType + result.kind = tyParametricTypeClass + result = addImplicitGeneric(copyType(result, + getCurrOwner(), false)) + elif result != nil: result.kind = tyGenericInvokation result.sons.setLen(result.sons.len - 1) of tyTypeClass: diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 12a5708b6..cacf4782e 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -203,7 +203,7 @@ proc describeArgs*(c: PContext, n: PNode, startIdx = 1): string = add(result, argTypeToString(arg)) if i != sonsLen(n) - 1: add(result, ", ") -proc typeRel*(c: var TCandidate, f, a: PType): TTypeRelation +proc typeRel*(c: var TCandidate, f, a: PType, doBind = true): TTypeRelation proc concreteType(c: TCandidate, t: PType): PType = case t.kind of tyArrayConstr: @@ -213,7 +213,7 @@ proc concreteType(c: TCandidate, t: PType): PType = addSonSkipIntLit(result, t.sons[1]) # XXX: semantic checking for the type? of tyNil: result = nil # what should it be? - of tyGenericParam: + of tyGenericParam, tyAnything: result = t while true: result = PType(idTableGet(c.bindings, t)) @@ -385,8 +385,23 @@ proc typeRangeRel(f, a: PType): TTypeRelation {.noinline.} = else: result = isNone -proc typeRel(c: var TCandidate, f, a: PType): TTypeRelation = - # is a subtype of f? +proc typeRel(c: var TCandidate, f, a: PType, doBind = true): TTypeRelation = + # typeRel can be used to establish various relationships between types: + # + # 1) When used with concrete types, it will check for type equivalence + # or a subtype relationship. + # + # 2) When used with a concrete type against a type class (such as generic + # signature of a proc), it will check whether the concrete type is a member + # of the designated type class. + # + # 3) When used with two type classes, it will check whether the types + # matching the first type class are a strict subset of the types matching + # the other. This allows us to compare the signatures of generic procs in + # order to give preferrence to the most specific one: + # + # seq[seq[any]] is a strict subset of seq[any] and hence more specific. + result = isNone assert(f != nil) assert(a != nil) @@ -397,6 +412,50 @@ proc typeRel(c: var TCandidate, f, a: PType): TTypeRelation = return typeRel(c, f, lastSon(a)) if a.kind == tyVar and f.kind != tyVar: return typeRel(c, f, a.sons[0]) + + template bindingRet(res) = + when res == isGeneric: put(c.bindings, f, a) + return res + + case a.kind + of tyOr: + # seq[int|string] vs seq[number] + # both int and string must match against number + for branch in a.sons: + if typeRel(c, f, branch, false) == isNone: + return isNone + + return isGeneric + + of tyAnd: + # seq[Sortable and Iterable] vs seq[Sortable] + # only one match is enough + for branch in a.sons: + if typeRel(c, f, branch, false) != isNone: + return isGeneric + + return isNone + + of tyNot: + case f.kind + of tyNot: + # seq[!int] vs seq[!number] + # seq[float] matches the first, but not the second + # we must turn the problem around: + # is number a subset of int? + return typeRel(c, a.lastSon, f.lastSon) + + else: + # negative type classes are essentially infinite, + # so only the `any` type class is their superset + return if f.kind == tyAnything: isGeneric + else: isNone + + of tyAnything: + return if f.kind == tyAnything: isGeneric + else: isNone + else: nil + case f.kind of tyEnum: if a.kind == f.kind and sameEnumTypes(f, a): result = isEqual @@ -485,9 +544,12 @@ proc typeRel(c: var TCandidate, f, a: PType): TTypeRelation = of tyOrdinal: if isOrdinalType(a): var x = if a.kind == tyOrdinal: a.sons[0] else: a - - result = typeRel(c, f.sons[0], x) - if result < isGeneric: result = isNone + + if f.sonsLen == 0: + result = isGeneric + else: + result = typeRel(c, f.sons[0], x) + if result < isGeneric: result = isNone elif a.kind == tyGenericParam: result = isGeneric of tyForward: InternalError("forward type in typeRel()") @@ -574,13 +636,17 @@ proc typeRel(c: var TCandidate, f, a: PType): TTypeRelation = (a.sons[1].kind == tyChar): result = isConvertible else: nil - of tyEmpty: + + of tyEmpty: if a.kind == tyEmpty: result = isEqual - of tyGenericInst: + + of tyGenericInst: result = typeRel(c, lastSon(f), a) - of tyGenericBody: + + of tyGenericBody: let ff = lastSon(f) if ff != nil: result = typeRel(c, ff, a) + of tyGenericInvokation: var x = a.skipGenericAlias if x.kind == tyGenericInvokation or f.sons[0].kind != tyGenericBody: @@ -604,6 +670,38 @@ proc typeRel(c: var TCandidate, f, a: PType): TTypeRelation = if x == nil or x.kind in {tyGenericInvokation, tyGenericParam}: InternalError("wrong instantiated type!") put(c.bindings, f.sons[i], x) + + of tyAnd: + for branch in f.sons: + if typeRel(c, branch, a) == isNone: + return isNone + + bindingRet isGeneric + + of tyOr: + for branch in f.sons: + if typeRel(c, branch, a) != isNone: + bindingRet isGeneric + + return isNone + + of tyNot: + for branch in f.sons: + if typeRel(c, branch, a) != isNone: + return isNone + + bindingRet isGeneric + + of tyAnything: + var prev = PType(idTableGet(c.bindings, f)) + if prev == nil: + var concrete = concreteType(c, a) + if concrete != nil and doBind: + put(c.bindings, f, concrete) + return isGeneric + else: + return typeRel(c, prev, a) + of tyGenericParam, tyTypeClass: var x = PType(idTableGet(c.bindings, f)) if x == nil: @@ -634,7 +732,7 @@ proc typeRel(c: var TCandidate, f, a: PType): TTypeRelation = if concrete == nil: result = isNone else: - put(c.bindings, f, concrete) + if doBind: put(c.bindings, f, concrete) elif a.kind == tyEmpty: result = isGeneric elif x.kind == tyGenericParam: @@ -809,8 +907,8 @@ proc ParamTypesMatchAux(c: PContext, m: var TCandidate, f, argType: PType, InternalAssert a.len > 0 r = typeRel(m, f.lastSon, a.lastSon) else: - let match = matchTypeClass(m, fMaybeExpr, a) - if match != isGeneric: r = isNone + let match = matchTypeClass(m.bindings, fMaybeExpr, a) + if not match: r = isNone else: # XXX: Ideally, this should happen much earlier somewhere near # semOpAux, but to do that, we need to be able to query the @@ -827,7 +925,7 @@ proc ParamTypesMatchAux(c: PContext, m: var TCandidate, f, argType: PType, if r == isGeneric: put(m.bindings, f, arg.typ) - of tyTypeClass: + of tyTypeClass, tyParametricTypeClass: if fMaybeExpr.n != nil: let match = matchUserTypeClass(c, m, arg, fMaybeExpr, a) if match != nil: @@ -1156,15 +1254,120 @@ proc argtypeMatches*(c: PContext, f, a: PType): bool = include suggest +tests: + var dummyOwner = newSym(skModule, getIdent("test_module"), nil, UnknownLineInfo()) + + proc `|` (t1, t2: PType): PType = + result = newType(tyOr, dummyOwner) + result.rawAddSon(t1) + result.rawAddSon(t2) + + proc `&` (t1, t2: PType): PType = + result = newType(tyAnd, dummyOwner) + result.rawAddSon(t1) + result.rawAddSon(t2) + + proc `!` (t: PType): PType = + result = newType(tyNot, dummyOwner) + result.rawAddSon(t) + + proc seq(t: PType): PType = + result = newType(tySequence, dummyOwner) + result.rawAddSon(t) + + proc array(x: int, t: PType): PType = + result = newType(tyArray, dummyOwner) + + var n = newNodeI(nkRange, UnknownLineInfo()) + addSon(n, newIntNode(nkIntLit, 0)) + addSon(n, newIntNode(nkIntLit, x)) + let range = newType(tyRange, dummyOwner) + + result.rawAddSon(range) + result.rawAddSon(t) + + suite "type classes": + let + int = newType(tyInt, dummyOwner) + float = newType(tyFloat, dummyOwner) + string = newType(tyString, dummyOwner) + ordinal = newType(tyOrdinal, dummyOwner) + any = newType(tyAnything, dummyOwner) + number = int | float + + var TFoo = newType(tyObject, dummyOwner) + TFoo.sym = newSym(skType, getIdent"TFoo", dummyOwner, UnknownLineInfo()) + + var T1 = newType(tyGenericParam, dummyOwner) + T1.sym = newSym(skType, getIdent"T1", dummyOwner, UnknownLineInfo()) + T1.sym.position = 0 + + var T2 = newType(tyGenericParam, dummyOwner) + T2.sym = newSym(skType, getIdent"T2", dummyOwner, UnknownLineInfo()) + T2.sym.position = 1 + + setup: + var c: TCandidate + InitCandidate(c, nil) + + template yes(x, y) = + test astToStr(x) & " is " & astToStr(y): + check typeRel(c, y, x) == isGeneric + + template no(x, y) = + test astToStr(x) & " is not " & astToStr(y): + check typeRel(c, y, x) == isNone + + yes seq(any), array(10, int) | seq(any) + # Sure, seq[any] is directly included + yes seq(int), seq(any) + yes seq(int), seq(number) + # Sure, the int sequence is certainly + # part of the number sequences (and all sequences) + + no seq(any), seq(float) + # Nope, seq[any] includes types that are not seq[float] (e.g. seq[int]) + + yes seq(int|string), seq(any) + # Sure + + yes seq(int&string), seq(any) + # Again + + yes seq(int&string), seq(int) + # A bit more complicated + # seq[int&string] is not a real type, but it's analogous to + # seq[Sortable and Iterable], which is certainly a subset of seq[Sortable] + + no seq(int|string), seq(int|float) + # Nope, seq[string] is not included in not included in + # the seq[int|float] set + + no seq(!(int|string)), seq(string) + # A sequence that is neither seq[int] or seq[string] + # is obviously not seq[string] + + no seq(!int), seq(number) + # Now your head should start to hurt a bit + # A sequence that is not seq[int] is not necessarily a number sequence + # it could well be seq[string] for example + + yes seq(!(int|string)), seq(!string) + # all sequnece types besides seq[int] and seq[string] + # are subset of all sequence types that are not seq[string] + + no seq(!(int|string)), seq(!(string|TFoo)) + # Nope, seq[TFoo] is included in the first set, but not in the second + + no seq(!string), seq(!number) + # Nope, seq[int] in included in the first set, but not in the second + + yes seq(!number), seq(any) + yes seq(!int), seq(any) + no seq(any), seq(!any) + no seq(!int), seq(!any) + + yes int, ordinal + no string, ordinal -tests: - suite "typerel": - test "ordinals": - # var owner = newSym(skModule, getIdent("dummy"), nil, UnknownLineInfo()) - var m: TCandidate - InitCandidate(m, f) - - # let f = newType(tyOrdinal, owner) - # let a = getSysType(tyInt) - # check typerel(m, f, a) == isGeneric diff --git a/compiler/types.nim b/compiler/types.nim index 4dec9ea2f..7e07a0667 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -382,33 +382,34 @@ proc mutateTypeAux(marker: var TIntSet, t: PType, iter: TTypeMutator, if t.n != nil: result.n = mutateNode(marker, t.n, iter, closure) assert(result != nil) -proc mutateType(t: PType, iter: TTypeMutator, closure: PObject): PType = +proc mutateType(t: PType, iter: TTypeMutator, closure: PObject): PType = var marker = InitIntSet() result = mutateTypeAux(marker, t, iter, closure) -proc ValueToString(a: PNode): string = +proc ValueToString(a: PNode): string = case a.kind of nkCharLit..nkUInt64Lit: result = $(a.intVal) of nkFloatLit..nkFloat128Lit: result = $(a.floatVal) of nkStrLit..nkTripleStrLit: result = a.strVal else: result = "" -proc rangeToStr(n: PNode): string = +proc rangeToStr(n: PNode): string = assert(n.kind == nkRange) result = ValueToString(n.sons[0]) & ".." & ValueToString(n.sons[1]) const - typeToStr: array[TTypeKind, string] = ["None", "bool", "Char", "empty", - "Array Constructor [$1]", "nil", "expr", "stmt", "typeDesc", - "GenericInvokation", "GenericBody", "GenericInst", "GenericParam", - "distinct $1", "enum", "ordinal[$1]", "array[$1, $2]", "object", "tuple", - "set[$1]", "range[$1]", "ptr ", "ref ", "var ", "seq[$1]", "proc", + typeToStr: array[TTypeKind, string] = ["None", "bool", "Char", "empty", + "Array Constructor [$1]", "nil", "expr", "stmt", "typeDesc", + "GenericInvokation", "GenericBody", "GenericInst", "GenericParam", + "distinct $1", "enum", "ordinal[$1]", "array[$1, $2]", "object", "tuple", + "set[$1]", "range[$1]", "ptr ", "ref ", "var ", "seq[$1]", "proc", "pointer", "OpenArray[$1]", "string", "CString", "Forward", "int", "int8", "int16", "int32", "int64", "float", "float32", "float64", "float128", "uint", "uint8", "uint16", "uint32", "uint64", "bignum", "const ", - "!", "varargs[$1]", "iter[$1]", "Error Type", "TypeClass"] + "!", "varargs[$1]", "iter[$1]", "Error Type", "TypeClass", + "ParametricTypeClass", "and", "or", "not", "any"] proc consToStr(t: PType): string = if t.len > 0: result = t.typeToString @@ -421,7 +422,7 @@ proc constraintsToStr(t: PType): string = if i > 0: result.add(sep) result.add(t.sons[i].consToStr) -proc TypeToString(typ: PType, prefer: TPreferedDesc = preferName): string = +proc TypeToString(typ: PType, prefer: TPreferedDesc = preferName): string = var t = typ result = "" if t == nil: return @@ -861,7 +862,7 @@ proc SameTypeAux(x, y: PType, c: var TSameTypeClosure): bool = of tyGenericParam, tyGenericInvokation, tyGenericBody, tySequence, tyOpenArray, tySet, tyRef, tyPtr, tyVar, tyArrayConstr, tyArray, tyProc, tyConst, tyMutable, tyVarargs, tyIter, - tyOrdinal, tyTypeClass: + tyOrdinal, tyTypeClasses: CycleCheck() result = sameChildrenAux(a, b, c) and sameFlags(a, b) if result and (a.kind == tyProc): @@ -1042,7 +1043,7 @@ proc typeAllowedAux(marker: var TIntSet, typ: PType, kind: TSymKind, # XXX er ... no? these should not be allowed! of tyEmpty: result = taField in flags - of tyTypeClass: + of tyTypeClasses: result = true of tyGenericBody, tyGenericParam, tyForward, tyNone, tyGenericInvokation: result = false diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim index 20f5c610e..f847d24f4 100644 --- a/lib/pure/unittest.nim +++ b/lib/pure/unittest.nim @@ -115,7 +115,8 @@ macro check*(conditions: stmt): stmt {.immediate.} = counter = 0 template asgn(a, value: expr): stmt = - let a = value + var a = value # XXX: we need "var: var" here in order to + # preserve the semantics of var params template print(name, value: expr): stmt = when compiles(string($value)): @@ -150,7 +151,8 @@ macro check*(conditions: stmt): stmt {.immediate.} = of nnkStmtList: result = newNimNode(nnkStmtList) for i in countup(0, checked.len - 1): - result.add(newCall(!"check", checked[i])) + if checked[i].kind != nnkCommentStmt: + result.add(newCall(!"check", checked[i])) else: template rewrite(Exp, lineInfoLit: expr, expLit: string): stmt = -- cgit 1.4.1-2-gfad0 From 642a0d556390ab520fc81d1e54712df20ffd2ae9 Mon Sep 17 00:00:00 2001 From: Michał Zieliński Date: Mon, 9 Dec 2013 18:03:36 +0100 Subject: Normalize whitespace in os.nim. I can't edit it - trick with asking git to ignore whitespace doesn't work if I need to change indention level. --- lib/pure/os.nim | 127 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 63 insertions(+), 64 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/os.nim b/lib/pure/os.nim index a39ca7b83..7d6821848 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -21,7 +21,7 @@ import when defined(windows): import winlean -elif defined(posix): +elif defined(posix): import posix else: {.error: "OS module not ported to your operating system!".} @@ -33,7 +33,7 @@ type ## from an environment variable FWriteEnv* = object of FWriteIO ## effect that denotes a write ## to an environment variable - + FReadDir* = object of FReadIO ## effect that denotes a write operation to ## the directory structure FWriteDir* = object of FWriteIO ## effect that denotes a write operation to @@ -179,7 +179,7 @@ proc OSErrorMsg*(): string {.rtl, extern: "nos$1", deprecated.} = ## Returns "" if no error occured. ## ## **Deprecated since version 0.9.4**: use the other ``OSErrorMsg`` proc. - + result = "" when defined(Windows): var err = GetLastError() @@ -276,7 +276,7 @@ proc OSLastError*(): TOSErrorCode = ## On Windows some OS calls can reset the error code to ``0`` causing this ## procedure to return ``0``. It is therefore advised to call this procedure ## immediately after an OS call fails. On POSIX systems this is not a problem. - + when defined(windows): result = TOSErrorCode(GetLastError()) else: @@ -355,8 +355,8 @@ when defined(windows): result = f.cFilename[0] == '.' template getFilename(f: expr): expr = $f.cFilename - -proc existsFile*(filename: string): bool {.rtl, extern: "nos$1", + +proc existsFile*(filename: string): bool {.rtl, extern: "nos$1", tags: [FReadDir].} = ## Returns true if the file exists, false otherwise. when defined(windows): @@ -410,7 +410,7 @@ proc getLastAccessTime*(file: string): TTime {.rtl, extern: "nos$1".} = result = winTimeToUnixTime(rdFileTime(f.ftLastAccessTime)) findclose(h) -proc getCreationTime*(file: string): TTime {.rtl, extern: "nos$1".} = +proc getCreationTime*(file: string): TTime {.rtl, extern: "nos$1".} = ## Returns the `file`'s creation time. when defined(posix): var res: TStat @@ -524,7 +524,7 @@ proc SplitPath*(path: string): tuple[head, tail: string] {. ## Splits a directory into (head, tail), so that ## ``JoinPath(head, tail) == path``. ## - ## Examples: + ## Examples: ## ## .. code-block:: nimrod ## SplitPath("usr/local/bin") -> ("usr/local", "bin") @@ -567,7 +567,7 @@ proc parentDir*(path: string): string {. proc isRootDir*(path: string): bool {. noSideEffect, rtl, extern: "nos$1".} = - ## Checks whether a given `path` is a root directory + ## Checks whether a given `path` is a root directory result = parentDirPos(path) < 0 iterator parentDirs*(path: string, fromRoot=false, inclusive=true): string = @@ -589,7 +589,7 @@ iterator parentDirs*(path: string, fromRoot=false, inclusive=true): string = else: for i in countup(0, path.len - 2): # ignore the last / # deal with non-normalized paths such as /foo//bar//baz - if path[i] in {dirsep, altsep} and + if path[i] in {dirsep, altsep} and (i == 0 or path[i-1] notin {dirsep, altsep}): yield path.substr(0, i) @@ -642,7 +642,7 @@ proc splitFile*(path: string): tuple[dir, name, ext: string] {. var dotPos = path.len for i in countdown(len(path)-1, 0): if path[i] == ExtSep: - if dotPos == path.len and i > 0 and + if dotPos == path.len and i > 0 and path[i-1] notin {dirsep, altsep}: dotPos = i elif path[i] in {dirsep, altsep}: sepPos = i @@ -653,7 +653,7 @@ proc splitFile*(path: string): tuple[dir, name, ext: string] {. proc extractFilename*(path: string): string {. noSideEffect, rtl, extern: "nos$1".} = - ## Extracts the filename of a given `path`. This is the same as + ## Extracts the filename of a given `path`. This is the same as ## ``name & ext`` from ``splitFile(path)``. if path.len == 0 or path[path.len-1] in {dirSep, altSep}: result = "" @@ -669,7 +669,7 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", var unused: widecstring var res = newWideCString("", bufsize div 2) var L = GetFullPathNameW(newWideCString(filename), bufsize, res, unused) - if L <= 0'i32 or L >= bufsize: + if L <= 0'i32 or L >= bufsize: OSError(OSLastError()) result = res$L else: @@ -684,7 +684,7 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", var r = realpath(filename, result) if r.isNil: OSError(OSLastError()) setlen(result, c_strlen(result)) - + proc ChangeFileExt*(filename, ext: string): string {. noSideEffect, rtl, extern: "nos$1".} = ## Changes the file extension to `ext`. @@ -740,12 +740,12 @@ proc isAbsolute*(path: string): bool {.rtl, noSideEffect, extern: "nos$1".} = elif defined(posix): result = path[0] == '/' -proc sameFile*(path1, path2: string): bool {.rtl, extern: "nos$1", +proc sameFile*(path1, path2: string): bool {.rtl, extern: "nos$1", tags: [FReadDir].} = - ## Returns True if both pathname arguments refer to the same physical + ## Returns True if both pathname arguments refer to the same physical ## file or directory. Raises an exception if any of the files does not ## exist or information about it can not be obtained. - ## + ## ## This proc will return true if given two alternative hard-linked or ## sym-linked paths to the same file or directory. when defined(Windows): @@ -761,7 +761,7 @@ proc sameFile*(path1, path2: string): bool {.rtl, extern: "nos$1", var f1 = OpenHandle(p1) var f2 = OpenHandle(p2) - + else: template OpenHandle(path: expr): expr = CreateFileA(path, 0'i32, FILE_SHARE_DELETE or FILE_SHARE_READ or @@ -829,7 +829,7 @@ proc sameFileContent*(path1, path2: string): bool {.rtl, extern: "nos$1", close(a) close(b) -proc copyFile*(source, dest: string) {.rtl, extern: "nos$1", +proc copyFile*(source, dest: string) {.rtl, extern: "nos$1", tags: [FReadIO, FWriteIO].} = ## Copies a file from `source` to `dest`. ## @@ -870,7 +870,7 @@ proc copyFile*(source, dest: string) {.rtl, extern: "nos$1", close(s) close(d) -proc moveFile*(source, dest: string) {.rtl, extern: "nos$1", +proc moveFile*(source, dest: string) {.rtl, extern: "nos$1", tags: [FReadIO, FWriteIO].} = ## Moves a file from `source` to `dest`. If this fails, `EOS` is raised. if crename(source, dest) != 0'i32: @@ -885,7 +885,7 @@ proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} = if cremove(file) != 0'i32 and errno != ENOENT: raise newException(EOS, $strerror(errno)) -proc execShellCmd*(command: string): int {.rtl, extern: "nos$1", +proc execShellCmd*(command: string): int {.rtl, extern: "nos$1", tags: [FExecIO].} = ## Executes a `shell command`:idx:. ## @@ -897,7 +897,7 @@ proc execShellCmd*(command: string): int {.rtl, extern: "nos$1", ## module. result = csystem(command) -# Environment handling cannot be put into RTL, because the ``envPairs`` +# Environment handling cannot be put into RTL, because the ``envPairs`` # iterator depends on ``environment``. var @@ -943,11 +943,11 @@ when defined(windows): else: const - useNSGetEnviron = defined(macosx) and + useNSGetEnviron = defined(macosx) and (defined(createNimRtl) or defined(useNimRtl)) when useNSGetEnviron: # From the manual: - # Shared libraries and bundles don't have direct access to environ, + # Shared libraries and bundles don't have direct access to environ, # which is only available to the loader ld(1) when a complete program # is being linked. # The environment routines can still be used, but if direct access to @@ -1025,13 +1025,13 @@ proc putEnv*(key, val: string) {.tags: [FWriteEnv].} = if SetEnvironmentVariableA(key, val) == 0'i32: OSError(OSLastError()) iterator envPairs*(): tuple[key, value: TaintedString] {.tags: [FReadEnv].} = - ## Iterate over all `environments variables`:idx:. In the first component + ## Iterate over all `environments variables`:idx:. In the first component ## of the tuple is the name of the current variable stored, in the second ## its value. getEnvVarsC() for i in 0..high(environment): var p = find(environment[i], '=') - yield (TaintedString(substr(environment[i], 0, p-1)), + yield (TaintedString(substr(environment[i], 0, p-1)), TaintedString(substr(environment[i], p+1))) iterator walkFiles*(pattern: string): string {.tags: [FReadDir].} = @@ -1125,7 +1125,7 @@ iterator walkDir*(dir: string): tuple[kind: TPathComponent, path: string] {. iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {. tags: [FReadDir].} = - ## walks over the directory `dir` and yields for each file in `dir`. The + ## walks over the directory `dir` and yields for each file in `dir`. The ## full path for each file is returned. ## Walking is recursive. `filter` controls the behaviour of the iterator: ## @@ -1137,7 +1137,7 @@ iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {. ## ``pcDir`` follow real directories ## ``pcLinkToDir`` follow symbolic links to directories ## --------------------- --------------------------------------------- - ## + ## var stack = @[dir] while stack.len > 0: for k,p in walkDir(stack.pop()): @@ -1146,14 +1146,14 @@ iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {. of pcFile, pcLinkToFile: yield p of pcDir, pcLinkToDir: stack.add(p) -proc rawRemoveDir(dir: string) = +proc rawRemoveDir(dir: string) = when defined(windows): when useWinUnicode: wrapUnary(res, RemoveDirectoryW, dir) else: var res = RemoveDirectoryA(dir) let lastError = OSLastError() - if res == 0'i32 and lastError.int32 != 3'i32 and + if res == 0'i32 and lastError.int32 != 3'i32 and lastError.int32 != 18'i32 and lastError.int32 != 2'i32: OSError(lastError) else: @@ -1166,7 +1166,7 @@ proc removeDir*(dir: string) {.rtl, extern: "nos$1", tags: [ ## ## If this fails, `EOS` is raised. This does not fail if the directory never ## existed in the first place. - for kind, path in walkDir(dir): + for kind, path in walkDir(dir): case kind of pcFile, pcLinkToFile, pcLinkToDir: removeFile(path) of pcDir: removeDir(path) @@ -1192,7 +1192,7 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} = ## ## The directory may contain several subdirectories that do not exist yet. ## The full path is created. If this fails, `EOS` is raised. It does **not** - ## fail if the path already exists because for most usages this does not + ## fail if the path already exists because for most usages this does not ## indicate an error. var omitNext = false when defined(doslike): @@ -1205,7 +1205,7 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} = rawCreateDir(substr(dir, 0, i-1)) rawCreateDir(dir) -proc copyDir*(source, dest: string) {.rtl, extern: "nos$1", +proc copyDir*(source, dest: string) {.rtl, extern: "nos$1", tags: [FWriteIO, FReadIO].} = ## Copies a directory from `source` to `dest`. If this fails, `EOS` is raised. createDir(dest) @@ -1220,7 +1220,7 @@ proc copyDir*(source, dest: string) {.rtl, extern: "nos$1", proc parseCmdLine*(c: string): seq[string] {. noSideEffect, rtl, extern: "nos$1".} = - ## Splits a command line into several components; + ## Splits a command line into several components; ## This proc is only occassionally useful, better use the `parseopt` module. ## ## On Windows, it uses the following parsing rules @@ -1247,7 +1247,7 @@ proc parseCmdLine*(c: string): seq[string] {. ## causing a literal double quotation mark (") to be placed in argv. ## ## On Posix systems, it uses the following parsing rules: - ## Components are separated by whitespace unless the whitespace + ## Components are separated by whitespace unless the whitespace ## occurs within ``"`` or ``'`` quotes. result = @[] var i = 0 @@ -1260,31 +1260,31 @@ proc parseCmdLine*(c: string): seq[string] {. if c[i] == '\0': break var inQuote = false while true: - case c[i] + case c[i] of '\0': break of '\\': var j = i while c[j] == '\\': inc(j) - if c[j] == '"': + if c[j] == '"': for k in 1..(j-i) div 2: a.add('\\') - if (j-i) mod 2 == 0: + if (j-i) mod 2 == 0: i = j - else: + else: a.add('"') i = j+1 - else: + else: a.add(c[i]) inc(i) of '"': inc(i) if not inQuote: inQuote = true - elif c[i] == '"': + elif c[i] == '"': a.add(c[i]) inc(i) else: inQuote = false break - of ' ', '\t': + of ' ', '\t': if not inQuote: break a.add(c[i]) inc(i) @@ -1346,11 +1346,11 @@ proc getFilePermissions*(filename: string): set[TFilePermission] {. var res = GetFileAttributesA(filename) if res == -1'i32: OSError(OSLastError()) if (res and FILE_ATTRIBUTE_READONLY) != 0'i32: - result = {fpUserExec, fpUserRead, fpGroupExec, fpGroupRead, + result = {fpUserExec, fpUserRead, fpGroupExec, fpGroupRead, fpOthersExec, fpOthersRead} else: result = {fpUserExec..fpOthersRead} - + proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {. rtl, extern: "nos$1", tags: [FWriteDir].} = ## sets the file permissions for `filename`. `OSError` is raised in case of @@ -1361,15 +1361,15 @@ proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {. if fpUserRead in permissions: p = p or S_IRUSR if fpUserWrite in permissions: p = p or S_IWUSR if fpUserExec in permissions: p = p or S_IXUSR - + if fpGroupRead in permissions: p = p or S_IRGRP if fpGroupWrite in permissions: p = p or S_IWGRP if fpGroupExec in permissions: p = p or S_IXGRP - + if fpOthersRead in permissions: p = p or S_IROTH if fpOthersWrite in permissions: p = p or S_IWOTH if fpOthersExec in permissions: p = p or S_IXOTH - + if chmod(filename, p) != 0: OSError(OSLastError()) else: when useWinUnicode: @@ -1377,7 +1377,7 @@ proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {. else: var res = GetFileAttributesA(filename) if res == -1'i32: OSError(OSLastError()) - if fpUserWrite in permissions: + if fpUserWrite in permissions: res = res and not FILE_ATTRIBUTE_READONLY else: res = res or FILE_ATTRIBUTE_READONLY @@ -1386,7 +1386,7 @@ proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {. else: var res2 = SetFileAttributesA(filename, res) if res2 == - 1'i32: OSError(OSLastError()) - + proc copyFileWithPermissions*(source, dest: string, ignorePermissionErrors = true) = ## Copies a file from `source` to `dest` preserving file permissions. @@ -1407,19 +1407,19 @@ proc copyFileWithPermissions*(source, dest: string, if not ignorePermissionErrors: raise -proc inclFilePermissions*(filename: string, +proc inclFilePermissions*(filename: string, permissions: set[TFilePermission]) {. rtl, extern: "nos$1", tags: [FReadDir, FWriteDir].} = - ## a convenience procedure for: + ## a convenience procedure for: ## ## .. code-block:: nimrod ## setFilePermissions(filename, getFilePermissions(filename)+permissions) setFilePermissions(filename, getFilePermissions(filename)+permissions) -proc exclFilePermissions*(filename: string, +proc exclFilePermissions*(filename: string, permissions: set[TFilePermission]) {. rtl, extern: "nos$1", tags: [FReadDir, FWriteDir].} = - ## a convenience procedure for: + ## a convenience procedure for: ## ## .. code-block:: nimrod ## setFilePermissions(filename, getFilePermissions(filename)-permissions) @@ -1459,7 +1459,7 @@ when defined(windows): if isNil(ownArgv): ownArgv = parseCmdLine($getCommandLine()) result = ownArgv.len-1 - proc paramStr*(i: int): TaintedString {.rtl, extern: "nos$1", + proc paramStr*(i: int): TaintedString {.rtl, extern: "nos$1", tags: [FReadIO].} = ## Returns the `i`-th `command line argument`:idx: given to the ## application. @@ -1494,7 +1494,7 @@ when defined(macosx): type cuint32* {.importc: "unsigned int", nodecl.} = int ## This is the same as the type ``uint32_t`` in *C*. - + # a really hacky solution: since we like to include 2 headers we have to # define two procs which in reality are the same proc getExecPath1(c: cstring, size: var cuint32) {. @@ -1553,13 +1553,13 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [FReadIO].} = proc getApplicationFilename*(): string {.rtl, extern: "nos$1", deprecated.} = ## Returns the filename of the application's executable. - ## **Deprecated since version 0.8.12**: use ``getAppFilename`` + ## **Deprecated since version 0.8.12**: use ``getAppFilename`` ## instead. result = getAppFilename() proc getApplicationDir*(): string {.rtl, extern: "nos$1", deprecated.} = ## Returns the directory of the application's executable. - ## **Deprecated since version 0.8.12**: use ``getAppDir`` + ## **Deprecated since version 0.8.12**: use ``getAppDir`` ## instead. result = splitFile(getAppFilename()).dir @@ -1580,7 +1580,7 @@ proc sleep*(milsecs: int) {.rtl, extern: "nos$1", tags: [FTime].} = proc getFileSize*(file: string): biggestInt {.rtl, extern: "nos$1", tags: [FReadIO].} = - ## returns the file size of `file`. Can raise ``EOS``. + ## returns the file size of `file`. Can raise ``EOS``. when defined(windows): var a: TWin32FindData var resA = findfirstFile(file, a) @@ -1589,20 +1589,20 @@ proc getFileSize*(file: string): biggestInt {.rtl, extern: "nos$1", findclose(resA) else: var f: TFile - if open(f, file): + if open(f, file): result = getFileSize(f) close(f) else: OSError(OSLastError()) -proc findExe*(exe: string): string {.tags: [FReadDir, FReadEnv].} = +proc findExe*(exe: string): string {.tags: [FReadDir, FReadEnv].} = ## Searches for `exe` in the current working directory and then - ## in directories listed in the ``PATH`` environment variable. - ## Returns "" if the `exe` cannot be found. On DOS-like platforms, `exe` + ## in directories listed in the ``PATH`` environment variable. + ## Returns "" if the `exe` cannot be found. On DOS-like platforms, `exe` ## is added an ``.exe`` file extension if it has no extension. result = addFileExt(exe, os.exeExt) if ExistsFile(result): return var path = string(os.getEnv("PATH")) - for candidate in split(path, pathSep): + for candidate in split(path, pathSep): var x = candidate / result if ExistsFile(x): return x result = "" @@ -1629,4 +1629,3 @@ proc expandTilde*(path: string): string = result = path {.pop.} - -- cgit 1.4.1-2-gfad0 From 8dae66415966e2cba50fd5295c514a97ff187fc2 Mon Sep 17 00:00:00 2001 From: Michał Zieliński Date: Mon, 9 Dec 2013 21:45:42 +0100 Subject: Add commandLineParams to os.nim. commandLineParams returns seq of arguments given to program on command line. --- lib/pure/os.nim | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/pure') diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 7d6821848..117e06299 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -1481,6 +1481,12 @@ elif not defined(createNimRtl): proc paramCount*(): int {.tags: [FReadIO].} = return cmdCount-1 +when defined(paramCount): + proc commandLineParams*(): seq[TaintedString] = + result = @[] + for i in 1..paramCount(): + result.add(paramStr(i)) + when defined(linux) or defined(solaris) or defined(bsd) or defined(aix): proc getApplAux(procPath: string): string = result = newString(256) -- cgit 1.4.1-2-gfad0 From d1f3512aba83814146583538c46b7f0c84175423 Mon Sep 17 00:00:00 2001 From: Michał Zieliński Date: Mon, 9 Dec 2013 22:22:06 +0100 Subject: Reimplement parseopt which parses arguments given as a sequence of strings, not single string. --- .gitignore | 1 + lib/pure/parseopt.nim | 184 +++++++++++++++++++++++------------------------- tests/system/params.nim | 17 +++++ 3 files changed, 107 insertions(+), 95 deletions(-) create mode 100644 tests/system/params.nim (limited to 'lib/pure') diff --git a/.gitignore b/.gitignore index 536ec9d24..d67d9f2cf 100644 --- a/.gitignore +++ b/.gitignore @@ -180,3 +180,4 @@ examples/cross_calculator/android/tags /tests/caas/main /tests/caasdriver /tools/nimgrep +/tests/system/params diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim index 6d9d16bc9..81a1fd1f0 100644 --- a/lib/pure/parseopt.nim +++ b/lib/pure/parseopt.nim @@ -10,123 +10,117 @@ ## This module provides the standard Nimrod command line parser. ## It supports one convenience iterator over all command line options and some ## lower-level features. +## +## Supported syntax: +## +## 1. short options - ``-abcd``, where a, b, c, d are names +## 2. long option - ``--foo:bar``, ``--foo=bar`` or ``--foo`` +## 3. argument - everything else {.push debugger: off.} include "system/inclrtl" -import +import os, strutils -type +type TCmdLineKind* = enum ## the detected command line token cmdEnd, ## end of command line reached cmdArgument, ## argument detected - cmdLongoption, ## a long option ``--option`` detected + cmdLongOption, ## a long option ``--option`` detected cmdShortOption ## a short option ``-c`` detected - TOptParser* = - object of TObject ## this object implements the command line parser - cmd: string + TOptParser* = + object of TObject ## this object implements the command line parser + cmd: seq[string] pos: int - inShortState: bool + remainingShortOptions: string kind*: TCmdLineKind ## the dected command line token key*, val*: TaintedString ## key and value pair; ``key`` is the option ## or the argument, ``value`` is not "" if ## the option was given a value -when defined(os.ParamCount): - # we cannot provide this for NimRtl creation on Posix, because we can't - # access the command line arguments then! - - proc initOptParser*(cmdline = ""): TOptParser = - ## inits the option parser. If ``cmdline == ""``, the real command line - ## (as provided by the ``OS`` module) is taken. - result.pos = 0 - result.inShortState = false - if cmdline != "": - result.cmd = cmdline - else: - result.cmd = "" - for i in countup(1, ParamCount()): - result.cmd = result.cmd & quoteIfContainsWhite(paramStr(i).string) & ' ' - result.kind = cmdEnd - result.key = TaintedString"" - result.val = TaintedString"" - -proc parseWord(s: string, i: int, w: var string, - delim: TCharSet = {'\x09', ' ', '\0'}): int = - result = i - if s[result] == '\"': - inc(result) - while not (s[result] in {'\0', '\"'}): - add(w, s[result]) - inc(result) - if s[result] == '\"': inc(result) - else: - while not (s[result] in delim): - add(w, s[result]) - inc(result) - -proc handleShortOption(p: var TOptParser) = - var i = p.pos - p.kind = cmdShortOption - add(p.key.string, p.cmd[i]) - inc(i) - p.inShortState = true - while p.cmd[i] in {'\x09', ' '}: - inc(i) - p.inShortState = false - if p.cmd[i] in {':', '='}: - inc(i) - p.inShortState = false - while p.cmd[i] in {'\x09', ' '}: inc(i) - i = parseWord(p.cmd, i, p.val.string) - if p.cmd[i] == '\0': p.inShortState = false - p.pos = i - -proc next*(p: var TOptParser) {. - rtl, extern: "npo$1".} = - ## parses the first or next option; ``p.kind`` describes what token has been - ## parsed. ``p.key`` and ``p.val`` are set accordingly. - var i = p.pos - while p.cmd[i] in {'\x09', ' '}: inc(i) - p.pos = i - setlen(p.key.string, 0) - setlen(p.val.string, 0) - if p.inShortState: - handleShortOption(p) - return - case p.cmd[i] - of '\0': +proc initOptParser*(cmdline: seq[string]): TOptParser {.rtl.} = + ## Initalizes option parses with cmdline. cmdline should not contain + ## argument 0 - program name. + ## If cmdline == nil default to current command line arguments. + result.remainingShortOptions = "" + when not defined(createNimRtl): + if cmdline == nil: + result.cmd = commandLineParams() + return + else: + assert cmdline != nil, "Cannot determine command line arguments." + + result.cmd = @cmdline + +proc initOptParser*(cmdline: string): TOptParser {.rtl, deprecated.} = + ## Initalizes option parses with cmdline. Splits cmdline in on spaces + ## and calls initOptParser(openarray[string]) + ## Do not use. + if cmdline == "": # backward compatibilty + return initOptParser(seq[string](nil)) + else: + return initOptParser(cmdline.split) + +when not defined(createNimRtl): + proc initOptParser*(): TOptParser = + ## Initializes option parser from current command line arguments. + return initOptParser(commandLineParams()) + +proc next*(p: var TOptParser) {.rtl, extern: "npo$1".} + +proc nextOption(p: var TOptParser, token: string, allowEmpty: bool) = + for splitchar in ['=', ':']: + if splitchar in token: + let pos = token.find(splitchar) + p.key = token[0..pos-1] + p.val = token[pos+1..token.len-1] + return + + p.key = token + if allowEmpty: + p.val = "" + else: + p.remainingShortOptions = token[0..token.len-1] + p.next() + +proc next(p: var TOptParser) = + if p.remainingShortOptions.len != 0: + p.kind = cmdShortOption + p.key = TaintedString(p.remainingShortOptions[0..0]) + p.val = "" + p.remainingShortOptions = p.remainingShortOptions[1..p.remainingShortOptions.len-1] + return + + if p.pos >= p.cmd.len: p.kind = cmdEnd - of '-': - inc(i) - if p.cmd[i] == '-': - p.kind = cmdLongOption - inc(i) - i = parseWord(p.cmd, i, p.key.string, {'\0', ' ', '\x09', ':', '='}) - while p.cmd[i] in {'\x09', ' '}: inc(i) - if p.cmd[i] in {':', '='}: - inc(i) - while p.cmd[i] in {'\x09', ' '}: inc(i) - p.pos = parseWord(p.cmd, i, p.val.string) - else: - p.pos = i - else: - p.pos = i - handleShortOption(p) + return + + let token = p.cmd[p.pos] + p.pos += 1 + + if token.startswith("--"): + p.kind = cmdLongOption + nextOption(p, token[2..token.len-1], allowEmpty=true) + elif token.startswith("-"): + p.kind = cmdShortOption + nextOption(p, token[1..token.len-1], allowEmpty=true) else: p.kind = cmdArgument - p.pos = parseWord(p.cmd, i, p.key.string) + p.key = token + p.val = "" -proc cmdLineRest*(p: TOptParser): TaintedString {. - rtl, extern: "npo$1".} = - ## retrieves the rest of the command line that has not been parsed yet. - result = strip(substr(p.cmd, p.pos, len(p.cmd) - 1)).TaintedString +proc cmdLineRest*(p: TOptParser): TaintedString {.rtl, extern: "npo$1", deprecated.} = + ## Returns part of command line string that has not been parsed yet. + ## Do not use - does not correctly handle whitespace. + return p.cmd[p.pos..p.cmd.len-1].join(" ") -when defined(initOptParser): +type + TGetoptResult* = tuple[kind: TCmdLineKind, key, val: TaintedString] - iterator getopt*(): tuple[kind: TCmdLineKind, key, val: TaintedString] = +when defined(paramCount): + iterator getopt*(): TGetoptResult = ## This is an convenience iterator for iterating over the command line. ## This uses the TOptParser object. Example: ## @@ -135,7 +129,7 @@ when defined(initOptParser): ## filename = "" ## for kind, key, val in getopt(): ## case kind - ## of cmdArgument: + ## of cmdArgument: ## filename = key ## of cmdLongOption, cmdShortOption: ## case key diff --git a/tests/system/params.nim b/tests/system/params.nim new file mode 100644 index 000000000..1e8385dc8 --- /dev/null +++ b/tests/system/params.nim @@ -0,0 +1,17 @@ +import os +import osproc +import parseopt +import sequtils + +let argv = commandLineParams() + +if argv == @[]: + # this won't work with spaces + assert execShellCmd(getAppFilename() & " \"foo bar\" --aa:bar --ab -c --a[baz]:doo") == 0 +else: + let f = toSeq(getopt()) + echo f.repr + assert f[0].kind == cmdArgument and f[0].key == "foo bar" and f[0].val == "" + assert f[1].kind == cmdLongOption and f[1].key == "aa" and f[1].val == "bar" + assert f[2].kind == cmdLongOption and f[2].key == "ab" and f[2].val == "" + assert f[3].kind == cmdShortOption and f[3].key == "c" and f[3].val == "" -- cgit 1.4.1-2-gfad0 From da05cf51def9eb619c406fd521c3662b03da6f20 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Tue, 10 Dec 2013 17:41:34 +0100 Subject: Reverts "Make quoteIfContainsWhite quote…". Refs #702. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit e7e8c7706240c4bef17533da63df988d228ba127 to avoid tool breakage. A different approach is being worked on #730. --- lib/pure/strutils.nim | 51 ++++++++------------------------------------------- web/news.txt | 2 -- 2 files changed, 8 insertions(+), 45 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 0299f5cd2..a4aa81578 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -709,6 +709,14 @@ proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} = if result != -1: return return -1 +proc quoteIfContainsWhite*(s: string): string = + ## returns ``'"' & s & '"'`` if `s` contains a space and does not + ## start with a quote, else returns `s` + if find(s, {' ', '\t'}) >= 0 and s[0] != '"': + result = '"' & s & '"' + else: + result = s + proc contains*(s: string, c: char): bool {.noSideEffect.} = ## Same as ``find(s, c) >= 0``. return find(s, c) >= 0 @@ -772,49 +780,6 @@ proc replaceWord*(s, sub: string, by = ""): string {.noSideEffect, # copy the rest: add result, substr(s, i) -proc quoteIfContainsWhite*(s: string): string {.noSideEffect.} = - ## Quote s, so it can be safely passed to shell. - when defined(Windows): - # based on Python's subprocess.list2cmdline - # see http://msdn.microsoft.com/en-us/library/17w5ykft.aspx - let needQuote = {' ', '\t'} in s or s.len == 0 - - result = "" - var backslashBuff = "" - if needQuote: - result.add("\"") - - for c in s: - if c == '\\': - backslashBuff.add(c) - elif c == '\"': - result.add(backslashBuff) - result.add(backslashBuff) - backslashBuff.setLen(0) - result.add("\\\"") - else: - if backslashBuff.len != 0: - result.add(backslashBuff) - backslashBuff.setLen(0) - result.add(c) - - if needQuote: - result.add("\"") - - else: - # based on Python's pipes.quote - const safeUnixChars = {'%', '+', '-', '.', '/', '_', ':', '=', '@', - '0'..'9', 'A'..'Z', 'a'..'z'} - if s.len == 0: - return "''" - - let safe = s.allCharsInSet(safeUnixChars) - - if safe: - return s - else: - return "'" & s.replace("'", "'\"'\"'") & "'" - proc delete*(s: var string, first, last: int) {.noSideEffect, rtl, extern: "nsuDelete".} = ## Deletes in `s` the characters at position `first` .. `last`. This modifies diff --git a/web/news.txt b/web/news.txt index f919089a5..1b492fa97 100644 --- a/web/news.txt +++ b/web/news.txt @@ -28,8 +28,6 @@ Changes affecting backwards compatibility require an error code to be passed to them. This error code can be retrieved using the new ``OSLastError`` proc. - ``os.parentDir`` now returns "" if there is no parent dir. -- ``quoteIfContainsWhite`` now escapes argument in such way that it can be safely - passed to shell, instead of just adding double quotes. Compiler Additions -- cgit 1.4.1-2-gfad0 From 896766ae2c395a1fac7de75a61f09f3db5822985 Mon Sep 17 00:00:00 2001 From: Michał Zieliński Date: Tue, 10 Dec 2013 19:10:06 +0100 Subject: Fix : and = precedence in parseopt. --- lib/pure/parseopt.nim | 2 +- tests/system/params.nim | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim index 81a1fd1f0..5e79d8a18 100644 --- a/lib/pure/parseopt.nim +++ b/lib/pure/parseopt.nim @@ -71,7 +71,7 @@ when not defined(createNimRtl): proc next*(p: var TOptParser) {.rtl, extern: "npo$1".} proc nextOption(p: var TOptParser, token: string, allowEmpty: bool) = - for splitchar in ['=', ':']: + for splitchar in [':', '=']: if splitchar in token: let pos = token.find(splitchar) p.key = token[0..pos-1] diff --git a/tests/system/params.nim b/tests/system/params.nim index 1e8385dc8..0ea099daa 100644 --- a/tests/system/params.nim +++ b/tests/system/params.nim @@ -7,11 +7,12 @@ let argv = commandLineParams() if argv == @[]: # this won't work with spaces - assert execShellCmd(getAppFilename() & " \"foo bar\" --aa:bar --ab -c --a[baz]:doo") == 0 + assert execShellCmd(getAppFilename() & " \"foo bar\" --aa:bar=a --a=c:d --ab -c --a[baz]:doo") == 0 else: let f = toSeq(getopt()) echo f.repr assert f[0].kind == cmdArgument and f[0].key == "foo bar" and f[0].val == "" - assert f[1].kind == cmdLongOption and f[1].key == "aa" and f[1].val == "bar" - assert f[2].kind == cmdLongOption and f[2].key == "ab" and f[2].val == "" - assert f[3].kind == cmdShortOption and f[3].key == "c" and f[3].val == "" + assert f[1].kind == cmdLongOption and f[1].key == "aa" and f[1].val == "bar=a" + assert f[2].kind == cmdLongOption and f[2].key == "a=c" and f[2].val == "d" + assert f[3].kind == cmdLongOption and f[3].key == "ab" and f[3].val == "" + assert f[4].kind == cmdShortOption and f[4].key == "c" and f[4].val == "" -- cgit 1.4.1-2-gfad0 From 088d4726624a4b644ce63ce3cd8898b7c79c63fc Mon Sep 17 00:00:00 2001 From: Michał Zieliński Date: Tue, 10 Dec 2013 20:33:51 +0100 Subject: rename new parseopt to parseopt2 --- lib/pure/parseopt.nim | 148 ------------------------------------------------- lib/pure/parseopt2.nim | 148 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 148 deletions(-) delete mode 100644 lib/pure/parseopt.nim create mode 100644 lib/pure/parseopt2.nim (limited to 'lib/pure') diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim deleted file mode 100644 index 5e79d8a18..000000000 --- a/lib/pure/parseopt.nim +++ /dev/null @@ -1,148 +0,0 @@ -# -# -# Nimrod's Runtime Library -# (c) Copyright 2012 Andreas Rumpf -# -# See the file "copying.txt", included in this -# distribution, for details about the copyright. -# - -## This module provides the standard Nimrod command line parser. -## It supports one convenience iterator over all command line options and some -## lower-level features. -## -## Supported syntax: -## -## 1. short options - ``-abcd``, where a, b, c, d are names -## 2. long option - ``--foo:bar``, ``--foo=bar`` or ``--foo`` -## 3. argument - everything else - -{.push debugger: off.} - -include "system/inclrtl" - -import - os, strutils - -type - TCmdLineKind* = enum ## the detected command line token - cmdEnd, ## end of command line reached - cmdArgument, ## argument detected - cmdLongOption, ## a long option ``--option`` detected - cmdShortOption ## a short option ``-c`` detected - TOptParser* = - object of TObject ## this object implements the command line parser - cmd: seq[string] - pos: int - remainingShortOptions: string - kind*: TCmdLineKind ## the dected command line token - key*, val*: TaintedString ## key and value pair; ``key`` is the option - ## or the argument, ``value`` is not "" if - ## the option was given a value - -proc initOptParser*(cmdline: seq[string]): TOptParser {.rtl.} = - ## Initalizes option parses with cmdline. cmdline should not contain - ## argument 0 - program name. - ## If cmdline == nil default to current command line arguments. - result.remainingShortOptions = "" - when not defined(createNimRtl): - if cmdline == nil: - result.cmd = commandLineParams() - return - else: - assert cmdline != nil, "Cannot determine command line arguments." - - result.cmd = @cmdline - -proc initOptParser*(cmdline: string): TOptParser {.rtl, deprecated.} = - ## Initalizes option parses with cmdline. Splits cmdline in on spaces - ## and calls initOptParser(openarray[string]) - ## Do not use. - if cmdline == "": # backward compatibilty - return initOptParser(seq[string](nil)) - else: - return initOptParser(cmdline.split) - -when not defined(createNimRtl): - proc initOptParser*(): TOptParser = - ## Initializes option parser from current command line arguments. - return initOptParser(commandLineParams()) - -proc next*(p: var TOptParser) {.rtl, extern: "npo$1".} - -proc nextOption(p: var TOptParser, token: string, allowEmpty: bool) = - for splitchar in [':', '=']: - if splitchar in token: - let pos = token.find(splitchar) - p.key = token[0..pos-1] - p.val = token[pos+1..token.len-1] - return - - p.key = token - if allowEmpty: - p.val = "" - else: - p.remainingShortOptions = token[0..token.len-1] - p.next() - -proc next(p: var TOptParser) = - if p.remainingShortOptions.len != 0: - p.kind = cmdShortOption - p.key = TaintedString(p.remainingShortOptions[0..0]) - p.val = "" - p.remainingShortOptions = p.remainingShortOptions[1..p.remainingShortOptions.len-1] - return - - if p.pos >= p.cmd.len: - p.kind = cmdEnd - return - - let token = p.cmd[p.pos] - p.pos += 1 - - if token.startswith("--"): - p.kind = cmdLongOption - nextOption(p, token[2..token.len-1], allowEmpty=true) - elif token.startswith("-"): - p.kind = cmdShortOption - nextOption(p, token[1..token.len-1], allowEmpty=true) - else: - p.kind = cmdArgument - p.key = token - p.val = "" - -proc cmdLineRest*(p: TOptParser): TaintedString {.rtl, extern: "npo$1", deprecated.} = - ## Returns part of command line string that has not been parsed yet. - ## Do not use - does not correctly handle whitespace. - return p.cmd[p.pos..p.cmd.len-1].join(" ") - -type - TGetoptResult* = tuple[kind: TCmdLineKind, key, val: TaintedString] - -when defined(paramCount): - iterator getopt*(): TGetoptResult = - ## This is an convenience iterator for iterating over the command line. - ## This uses the TOptParser object. Example: - ## - ## .. code-block:: nimrod - ## var - ## filename = "" - ## for kind, key, val in getopt(): - ## case kind - ## of cmdArgument: - ## filename = key - ## of cmdLongOption, cmdShortOption: - ## case key - ## of "help", "h": writeHelp() - ## of "version", "v": writeVersion() - ## of cmdEnd: assert(false) # cannot happen - ## if filename == "": - ## # no filename has been given, so we show the help: - ## writeHelp() - var p = initOptParser() - while true: - next(p) - if p.kind == cmdEnd: break - yield (p.kind, p.key, p.val) - -{.pop.} diff --git a/lib/pure/parseopt2.nim b/lib/pure/parseopt2.nim new file mode 100644 index 000000000..5e79d8a18 --- /dev/null +++ b/lib/pure/parseopt2.nim @@ -0,0 +1,148 @@ +# +# +# Nimrod's Runtime Library +# (c) Copyright 2012 Andreas Rumpf +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## This module provides the standard Nimrod command line parser. +## It supports one convenience iterator over all command line options and some +## lower-level features. +## +## Supported syntax: +## +## 1. short options - ``-abcd``, where a, b, c, d are names +## 2. long option - ``--foo:bar``, ``--foo=bar`` or ``--foo`` +## 3. argument - everything else + +{.push debugger: off.} + +include "system/inclrtl" + +import + os, strutils + +type + TCmdLineKind* = enum ## the detected command line token + cmdEnd, ## end of command line reached + cmdArgument, ## argument detected + cmdLongOption, ## a long option ``--option`` detected + cmdShortOption ## a short option ``-c`` detected + TOptParser* = + object of TObject ## this object implements the command line parser + cmd: seq[string] + pos: int + remainingShortOptions: string + kind*: TCmdLineKind ## the dected command line token + key*, val*: TaintedString ## key and value pair; ``key`` is the option + ## or the argument, ``value`` is not "" if + ## the option was given a value + +proc initOptParser*(cmdline: seq[string]): TOptParser {.rtl.} = + ## Initalizes option parses with cmdline. cmdline should not contain + ## argument 0 - program name. + ## If cmdline == nil default to current command line arguments. + result.remainingShortOptions = "" + when not defined(createNimRtl): + if cmdline == nil: + result.cmd = commandLineParams() + return + else: + assert cmdline != nil, "Cannot determine command line arguments." + + result.cmd = @cmdline + +proc initOptParser*(cmdline: string): TOptParser {.rtl, deprecated.} = + ## Initalizes option parses with cmdline. Splits cmdline in on spaces + ## and calls initOptParser(openarray[string]) + ## Do not use. + if cmdline == "": # backward compatibilty + return initOptParser(seq[string](nil)) + else: + return initOptParser(cmdline.split) + +when not defined(createNimRtl): + proc initOptParser*(): TOptParser = + ## Initializes option parser from current command line arguments. + return initOptParser(commandLineParams()) + +proc next*(p: var TOptParser) {.rtl, extern: "npo$1".} + +proc nextOption(p: var TOptParser, token: string, allowEmpty: bool) = + for splitchar in [':', '=']: + if splitchar in token: + let pos = token.find(splitchar) + p.key = token[0..pos-1] + p.val = token[pos+1..token.len-1] + return + + p.key = token + if allowEmpty: + p.val = "" + else: + p.remainingShortOptions = token[0..token.len-1] + p.next() + +proc next(p: var TOptParser) = + if p.remainingShortOptions.len != 0: + p.kind = cmdShortOption + p.key = TaintedString(p.remainingShortOptions[0..0]) + p.val = "" + p.remainingShortOptions = p.remainingShortOptions[1..p.remainingShortOptions.len-1] + return + + if p.pos >= p.cmd.len: + p.kind = cmdEnd + return + + let token = p.cmd[p.pos] + p.pos += 1 + + if token.startswith("--"): + p.kind = cmdLongOption + nextOption(p, token[2..token.len-1], allowEmpty=true) + elif token.startswith("-"): + p.kind = cmdShortOption + nextOption(p, token[1..token.len-1], allowEmpty=true) + else: + p.kind = cmdArgument + p.key = token + p.val = "" + +proc cmdLineRest*(p: TOptParser): TaintedString {.rtl, extern: "npo$1", deprecated.} = + ## Returns part of command line string that has not been parsed yet. + ## Do not use - does not correctly handle whitespace. + return p.cmd[p.pos..p.cmd.len-1].join(" ") + +type + TGetoptResult* = tuple[kind: TCmdLineKind, key, val: TaintedString] + +when defined(paramCount): + iterator getopt*(): TGetoptResult = + ## This is an convenience iterator for iterating over the command line. + ## This uses the TOptParser object. Example: + ## + ## .. code-block:: nimrod + ## var + ## filename = "" + ## for kind, key, val in getopt(): + ## case kind + ## of cmdArgument: + ## filename = key + ## of cmdLongOption, cmdShortOption: + ## case key + ## of "help", "h": writeHelp() + ## of "version", "v": writeVersion() + ## of cmdEnd: assert(false) # cannot happen + ## if filename == "": + ## # no filename has been given, so we show the help: + ## writeHelp() + var p = initOptParser() + while true: + next(p) + if p.kind == cmdEnd: break + yield (p.kind, p.key, p.val) + +{.pop.} -- cgit 1.4.1-2-gfad0 From 2e0da6a5c6f0440b63a0c1138f3e631cfdfc6ab8 Mon Sep 17 00:00:00 2001 From: Michał Zieliński Date: Tue, 10 Dec 2013 20:41:52 +0100 Subject: Make old parseopt as deprecated. --- lib/pure/parseopt.nim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/pure') diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim index 6d9d16bc9..fa704bbce 100644 --- a/lib/pure/parseopt.nim +++ b/lib/pure/parseopt.nim @@ -10,7 +10,10 @@ ## This module provides the standard Nimrod command line parser. ## It supports one convenience iterator over all command line options and some ## lower-level features. - +## +## DEPRECATED. Use parseopt2 instead as this version has issues with spaces +## in arguments. +{.deprecated.} {.push debugger: off.} include "system/inclrtl" -- cgit 1.4.1-2-gfad0 From 33ffe6086456384a06efba7fbb30a2c4850367d4 Mon Sep 17 00:00:00 2001 From: Clay Sweetser Date: Wed, 11 Dec 2013 16:16:14 -0500 Subject: Fixed skipFile proc to not skip files that merely start with '.' Modified removeFile to reset the read only attribute on files before trying to delete. --- lib/pure/os.nim | 183 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 100 insertions(+), 83 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 117e06299..71639d821 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -342,7 +342,14 @@ when defined(windows): template getCommandLine(): expr = getCommandLineW() proc skipFindData(f: TWIN32_FIND_DATA): bool {.inline.} = - result = f.cFilename[0].int == ord('.') + let + nul = 0 + dot = ord('.') + result = (f.cFilename[0].int == dot) + if result: + result = (f.cFilename[1].int in {dot, nul}) + if result: + result = (f.cFilename[2].int == nul) template getFilename(f: expr): expr = $cast[WideCString](addr(f.cFilename[0])) @@ -352,7 +359,14 @@ when defined(windows): template getCommandLine(): expr = getCommandLineA() proc skipFindData(f: TWIN32_FIND_DATA): bool {.inline.} = - result = f.cFilename[0] == '.' + let + nul = '\0' + dot = '.' + result = (f.cFilename[0] == dot) + if result: + result = (f.cFilename[1] in {dot, nul}) + if result: + result = (f.cFilename[2] == nul) template getFilename(f: expr): expr = $f.cFilename @@ -829,6 +843,86 @@ proc sameFileContent*(path1, path2: string): bool {.rtl, extern: "nos$1", close(a) close(b) +type + TFilePermission* = enum ## file access permission; modelled after UNIX + fpUserExec, ## execute access for the file owner + fpUserWrite, ## write access for the file owner + fpUserRead, ## read access for the file owner + fpGroupExec, ## execute access for the group + fpGroupWrite, ## write access for the group + fpGroupRead, ## read access for the group + fpOthersExec, ## execute access for others + fpOthersWrite, ## write access for others + fpOthersRead ## read access for others + +proc getFilePermissions*(filename: string): set[TFilePermission] {. + rtl, extern: "nos$1", tags: [FReadDir].} = + ## retrieves file permissions for `filename`. `OSError` is raised in case of + ## an error. On Windows, only the ``readonly`` flag is checked, every other + ## permission is available in any case. + when defined(posix): + var a: TStat + if stat(filename, a) < 0'i32: OSError(OSLastError()) + result = {} + if (a.st_mode and S_IRUSR) != 0'i32: result.incl(fpUserRead) + if (a.st_mode and S_IWUSR) != 0'i32: result.incl(fpUserWrite) + if (a.st_mode and S_IXUSR) != 0'i32: result.incl(fpUserExec) + + if (a.st_mode and S_IRGRP) != 0'i32: result.incl(fpGroupRead) + if (a.st_mode and S_IWGRP) != 0'i32: result.incl(fpGroupWrite) + if (a.st_mode and S_IXGRP) != 0'i32: result.incl(fpGroupExec) + + if (a.st_mode and S_IROTH) != 0'i32: result.incl(fpOthersRead) + if (a.st_mode and S_IWOTH) != 0'i32: result.incl(fpOthersWrite) + if (a.st_mode and S_IXOTH) != 0'i32: result.incl(fpOthersExec) + else: + when useWinUnicode: + wrapUnary(res, GetFileAttributesW, filename) + else: + var res = GetFileAttributesA(filename) + if res == -1'i32: OSError(OSLastError()) + if (res and FILE_ATTRIBUTE_READONLY) != 0'i32: + result = {fpUserExec, fpUserRead, fpGroupExec, fpGroupRead, + fpOthersExec, fpOthersRead} + else: + result = {fpUserExec..fpOthersRead} + +proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {. + rtl, extern: "nos$1", tags: [FWriteDir].} = + ## sets the file permissions for `filename`. `OSError` is raised in case of + ## an error. On Windows, only the ``readonly`` flag is changed, depending on + ## ``fpUserWrite``. + when defined(posix): + var p = 0'i32 + if fpUserRead in permissions: p = p or S_IRUSR + if fpUserWrite in permissions: p = p or S_IWUSR + if fpUserExec in permissions: p = p or S_IXUSR + + if fpGroupRead in permissions: p = p or S_IRGRP + if fpGroupWrite in permissions: p = p or S_IWGRP + if fpGroupExec in permissions: p = p or S_IXGRP + + if fpOthersRead in permissions: p = p or S_IROTH + if fpOthersWrite in permissions: p = p or S_IWOTH + if fpOthersExec in permissions: p = p or S_IXOTH + + if chmod(filename, p) != 0: OSError(OSLastError()) + else: + when useWinUnicode: + wrapUnary(res, GetFileAttributesW, filename) + else: + var res = GetFileAttributesA(filename) + if res == -1'i32: OSError(OSLastError()) + if fpUserWrite in permissions: + res = res and not FILE_ATTRIBUTE_READONLY + else: + res = res or FILE_ATTRIBUTE_READONLY + when useWinUnicode: + wrapBinary(res2, SetFileAttributesW, filename, res) + else: + var res2 = SetFileAttributesA(filename, res) + if res2 == - 1'i32: OSError(OSLastError()) + proc copyFile*(source, dest: string) {.rtl, extern: "nos$1", tags: [FReadIO, FWriteIO].} = ## Copies a file from `source` to `dest`. @@ -882,6 +976,9 @@ when not defined(ENOENT): proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} = ## Removes the `file`. If this fails, `EOS` is raised. This does not fail ## if the file never existed in the first place. + ## On Windows, ignores the read-only attribute. + when defined(Windows): + setFilePermissions(file, {fpUserWrite}) if cremove(file) != 0'i32 and errno != ENOENT: raise newException(EOS, $strerror(errno)) @@ -1306,87 +1403,7 @@ proc parseCmdLine*(c: string): seq[string] {. add(a, c[i]) inc(i) add(result, a) - -type - TFilePermission* = enum ## file access permission; modelled after UNIX - fpUserExec, ## execute access for the file owner - fpUserWrite, ## write access for the file owner - fpUserRead, ## read access for the file owner - fpGroupExec, ## execute access for the group - fpGroupWrite, ## write access for the group - fpGroupRead, ## read access for the group - fpOthersExec, ## execute access for others - fpOthersWrite, ## write access for others - fpOthersRead ## read access for others - -proc getFilePermissions*(filename: string): set[TFilePermission] {. - rtl, extern: "nos$1", tags: [FReadDir].} = - ## retrieves file permissions for `filename`. `OSError` is raised in case of - ## an error. On Windows, only the ``readonly`` flag is checked, every other - ## permission is available in any case. - when defined(posix): - var a: TStat - if stat(filename, a) < 0'i32: OSError(OSLastError()) - result = {} - if (a.st_mode and S_IRUSR) != 0'i32: result.incl(fpUserRead) - if (a.st_mode and S_IWUSR) != 0'i32: result.incl(fpUserWrite) - if (a.st_mode and S_IXUSR) != 0'i32: result.incl(fpUserExec) - - if (a.st_mode and S_IRGRP) != 0'i32: result.incl(fpGroupRead) - if (a.st_mode and S_IWGRP) != 0'i32: result.incl(fpGroupWrite) - if (a.st_mode and S_IXGRP) != 0'i32: result.incl(fpGroupExec) - - if (a.st_mode and S_IROTH) != 0'i32: result.incl(fpOthersRead) - if (a.st_mode and S_IWOTH) != 0'i32: result.incl(fpOthersWrite) - if (a.st_mode and S_IXOTH) != 0'i32: result.incl(fpOthersExec) - else: - when useWinUnicode: - wrapUnary(res, GetFileAttributesW, filename) - else: - var res = GetFileAttributesA(filename) - if res == -1'i32: OSError(OSLastError()) - if (res and FILE_ATTRIBUTE_READONLY) != 0'i32: - result = {fpUserExec, fpUserRead, fpGroupExec, fpGroupRead, - fpOthersExec, fpOthersRead} - else: - result = {fpUserExec..fpOthersRead} - -proc setFilePermissions*(filename: string, permissions: set[TFilePermission]) {. - rtl, extern: "nos$1", tags: [FWriteDir].} = - ## sets the file permissions for `filename`. `OSError` is raised in case of - ## an error. On Windows, only the ``readonly`` flag is changed, depending on - ## ``fpUserWrite``. - when defined(posix): - var p = 0'i32 - if fpUserRead in permissions: p = p or S_IRUSR - if fpUserWrite in permissions: p = p or S_IWUSR - if fpUserExec in permissions: p = p or S_IXUSR - - if fpGroupRead in permissions: p = p or S_IRGRP - if fpGroupWrite in permissions: p = p or S_IWGRP - if fpGroupExec in permissions: p = p or S_IXGRP - - if fpOthersRead in permissions: p = p or S_IROTH - if fpOthersWrite in permissions: p = p or S_IWOTH - if fpOthersExec in permissions: p = p or S_IXOTH - - if chmod(filename, p) != 0: OSError(OSLastError()) - else: - when useWinUnicode: - wrapUnary(res, GetFileAttributesW, filename) - else: - var res = GetFileAttributesA(filename) - if res == -1'i32: OSError(OSLastError()) - if fpUserWrite in permissions: - res = res and not FILE_ATTRIBUTE_READONLY - else: - res = res or FILE_ATTRIBUTE_READONLY - when useWinUnicode: - wrapBinary(res2, SetFileAttributesW, filename, res) - else: - var res2 = SetFileAttributesA(filename, res) - if res2 == - 1'i32: OSError(OSLastError()) - + proc copyFileWithPermissions*(source, dest: string, ignorePermissionErrors = true) = ## Copies a file from `source` to `dest` preserving file permissions. -- cgit 1.4.1-2-gfad0