From 38a6904a948661802d2c744e0c2b9759e11fbd70 Mon Sep 17 00:00:00 2001 From: Berni Moses Date: Wed, 1 Apr 2020 17:57:43 +0200 Subject: Support remote passwd providers like SSSD. --- src/utils.nim | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/utils.nim b/src/utils.nim index 8ca68d4..c329b68 100644 --- a/src/utils.nim +++ b/src/utils.nim @@ -215,22 +215,18 @@ proc setgroups*(size: csize, groups: ptr cint): cint {.importc, header: "".} proc getUser(uid: int): User = - while true: - var pw = getpwent() - if pw == nil: - endpwent() - raise newException(CatchableError, "") - if pw.pw_uid.int == uid: - var groups: array[100, cint] - var ngroups: cint = 100 - if getgrouplist(pw.pw_name, pw.pw_gid, addr(groups[0]), ngroups) < 0: - raise newException(CatchableError, "") - else: - let groupsSeq = groups[0 .. ngroups - 1].map(x => x.int) - let res = ($pw.pw_name, pw.pw_uid.int, pw.pw_gid.int, groupsSeq, - $pw.pw_dir, $pw.pw_shell) - endpwent() - return res + var pw = getpwuid(Uid(uid)) + if pw == nil: + raise newException(CatchableError, "") + var groups: array[100, cint] + var ngroups: cint = 100 + if getgrouplist(pw.pw_name, pw.pw_gid, addr(groups[0]), ngroups) < 0: + raise newException(CatchableError, "") + else: + let groupsSeq = groups[0 .. ngroups - 1].map(x => x.int) + let res = ($pw.pw_name, pw.pw_uid.int, pw.pw_gid.int, groupsSeq, + $pw.pw_dir, $pw.pw_shell) + return res let currentUser* = getUser(getuid().int) -- cgit v1.2.3-70-g09d2 From 086784e56e57c61742718803478cc29d0331414b Mon Sep 17 00:00:00 2001 From: zqqw Date: Fri, 1 May 2020 02:31:13 +0100 Subject: Add lc module --- src/args.nim | 2 +- src/aur.nim | 2 +- src/common.nim | 2 +- src/feature/syncinfo.nim | 2 +- src/feature/syncinstall.nim | 2 +- src/format.nim | 2 +- src/listcomp.nim | 69 +++++++++++++++++++++++++++++++++++++++++++++ src/main.nim | 2 +- src/package.nim | 2 +- src/pacman.nim | 2 +- 10 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 src/listcomp.nim diff --git a/src/args.nim b/src/args.nim index fd03ee0..9aca7aa 100644 --- a/src/args.nim +++ b/src/args.nim @@ -1,6 +1,6 @@ import options, os, posix, sequtils, sets, strutils, sugar, - utils + utils, "listcomp" type ArgumentType* {.pure.} = enum diff --git a/src/aur.nim b/src/aur.nim index 399183c..9b0628f 100644 --- a/src/aur.nim +++ b/src/aur.nim @@ -1,7 +1,7 @@ import json, lists, options, re, sequtils, sets, strutils, sugar, tables, package, utils, - "wrapper/curl" + "wrapper/curl", "listcomp" type AurComment* = tuple[ diff --git a/src/common.nim b/src/common.nim index 8db3a79..055474e 100644 --- a/src/common.nim +++ b/src/common.nim @@ -1,7 +1,7 @@ import options, os, osproc, posix, sequtils, sets, strutils, sugar, tables, args, config, format, lists, package, pacman, utils, - "wrapper/alpm" + "wrapper/alpm", "listcomp" type CacheKind* {.pure.} = enum diff --git a/src/feature/syncinfo.nim b/src/feature/syncinfo.nim index 4f758fe..a06c88d 100644 --- a/src/feature/syncinfo.nim +++ b/src/feature/syncinfo.nim @@ -2,7 +2,7 @@ import options, posix, sequtils, strutils, sugar, tables, "../args", "../aur", "../common", "../config", "../format", "../package", "../pacman", "../utils", - "../wrapper/alpm" + "../wrapper/alpm", "../listcomp" const pacmanInfoStrings = [ diff --git a/src/feature/syncinstall.nim b/src/feature/syncinstall.nim index 7aba043..bb97178 100644 --- a/src/feature/syncinstall.nim +++ b/src/feature/syncinstall.nim @@ -2,7 +2,7 @@ import algorithm, options, os, posix, sequtils, sets, strutils, sugar, tables, "../args", "../aur", "../config", "../common", "../format", "../lists", "../package", "../pacman", "../utils", - "../wrapper/alpm" + "../wrapper/alpm", "../listcomp" type Installed = tuple[ diff --git a/src/format.nim b/src/format.nim index 2ac39dc..5c8e336 100644 --- a/src/format.nim +++ b/src/format.nim @@ -1,6 +1,6 @@ import macros, options, posix, sequtils, strutils, sugar, times, unicode, - utils + utils, "listcomp" type PackageLineFormat* = tuple[ diff --git a/src/listcomp.nim b/src/listcomp.nim new file mode 100644 index 0000000..c9f8dc0 --- /dev/null +++ b/src/listcomp.nim @@ -0,0 +1,69 @@ +import macros + +type ListComprehension = object +var lc* : ListComprehension + +template `|`*(lc: ListComprehension, comp: untyped): untyped = lc + +macro `[]`*(lc: ListComprehension, comp, typ: untyped): untyped = + ## List comprehension, returns a sequence. `comp` is the actual list + ## comprehension, for example ``x | (x <- 1..10, x mod 2 == 0)``. `typ` is + ## the type that will be stored inside the result seq. + ## + ## .. code-block:: nim + ## + ## echo lc[x | (x <- 1..10, x mod 2 == 0), int] + ## + ## const n = 20 + ## echo lc[(x,y,z) | (x <- 1..n, y <- x..n, z <- y..n, x*x + y*y == z*z), + ## tuple[a,b,c: int]] + + expectLen(comp, 3) + expectKind(comp, nnkInfix) + assert($comp[0] == "|") + + result = newCall( + newDotExpr( + newIdentNode("result"), + newIdentNode("add")), + comp[1]) + + for i in countdown(comp[2].len-1, 0): + let x = comp[2][i] + expectMinLen(x, 1) + if x[0].kind == nnkIdent and x[0].strVal == "<-": + expectLen(x, 3) + result = newNimNode(nnkForStmt).add(x[1], x[2], result) + else: + result = newIfStmt((x, result)) + + result = newNimNode(nnkCall).add( + newNimNode(nnkPar).add( + newNimNode(nnkLambda).add( + newEmptyNode(), + newEmptyNode(), + newEmptyNode(), + newNimNode(nnkFormalParams).add( + newNimNode(nnkBracketExpr).add( + newIdentNode("seq"), + typ)), + newEmptyNode(), + newEmptyNode(), + newStmtList( + newAssignment( + newIdentNode("result"), + newNimNode(nnkPrefix).add( + newIdentNode("@"), + newNimNode(nnkBracket))), + result)))) + + +when isMainModule: + var a = lc[x | (x <- 1..10, x mod 2 == 0), int] + assert a == @[2, 4, 6, 8, 10] + + const n = 20 + var b = lc[(x,y,z) | (x <- 1..n, y <- x..n, z <- y..n, x*x + y*y == z*z), + tuple[a,b,c: int]] + assert b == @[(a: 3, b: 4, c: 5), (a: 5, b: 12, c: 13), (a: 6, b: 8, c: 10), +(a: 8, b: 15, c: 17), (a: 9, b: 12, c: 15), (a: 12, b: 16, c: 20)] diff --git a/src/main.nim b/src/main.nim index 92e8887..3de4a65 100644 --- a/src/main.nim +++ b/src/main.nim @@ -1,6 +1,6 @@ import options, os, posix, re, sequtils, strutils, sugar, - args, config, format, pacman, utils + args, config, format, pacman, utils, "listcomp" import "feature/localquery", diff --git a/src/package.nim b/src/package.nim index 5b87184..612f98f 100644 --- a/src/package.nim +++ b/src/package.nim @@ -1,6 +1,6 @@ import options, os, re, sequtils, sets, strutils, sugar, tables, utils, - "wrapper/alpm" + "wrapper/alpm", "listcomp" type ConstraintOperation* {.pure.} = enum diff --git a/src/pacman.nim b/src/pacman.nim index 857581f..520a67e 100644 --- a/src/pacman.nim +++ b/src/pacman.nim @@ -1,6 +1,6 @@ import macros, options, posix, sequtils, sets, strutils, sugar, tables, - args, config, utils + args, config, utils, "listcomp" type OpGroup* {.pure.} = enum -- cgit v1.2.3-70-g09d2 From 85382cae3cd98f3fc4a0925482da182681aabc8d Mon Sep 17 00:00:00 2001 From: zqqw Date: Sat, 2 May 2020 02:22:59 +0100 Subject: Use Nim version 1.0 compatibility --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 04ce4cb..2af010c 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,7 @@ NIM_OPTIMIZE = size NIM_CACHE_DIR = nimcache NIM_OPTIONS = \ + --useVersion:'1.0' \ --putenv:'PROG_VERSION'="${RVERSION}" \ --putenv:'PROG_COPYRIGHT'="${COPYRIGHT}" \ --putenv:'PROG_PKGLIBDIR'="${PKGLIBDIR}" \ -- cgit v1.2.3-70-g09d2 From 36e59748983034d5d39293e5d2415e92c14e2a22 Mon Sep 17 00:00:00 2001 From: zqqw Date: Wed, 27 May 2020 02:29:29 +0100 Subject: Fix -Ss index out of bounds, the container is empty [IndexError] (runeOffset: empty description field) --- src/format.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/format.nim b/src/format.nim index 5c8e336..b8061f9 100644 --- a/src/format.nim +++ b/src/format.nim @@ -83,7 +83,9 @@ proc splitLines(text: string, lineSize: int, lines: seq[string] = @[]): seq[stri if not addBreaks: lines & text else: - let offset = text.runeOffset(lineSize) + var offset : int = -1 + if text.len() != 0: + offset = text.runeOffset(lineSize) if offset < 0: lines & text else: -- cgit v1.2.3-70-g09d2 From b2321b0ded6f4a9803daf9dbcbd88d56321a9305 Mon Sep 17 00:00:00 2001 From: zqqw Date: Sun, 19 Jul 2020 15:44:34 +0100 Subject: Fix map error & some warnings --- src/args.nim | 2 +- src/utils.nim | 6 +++--- src/wrapper/curl.nim | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/args.nim b/src/args.nim index 9aca7aa..6da84fd 100644 --- a/src/args.nim +++ b/src/args.nim @@ -132,7 +132,7 @@ proc splitArgs*(params: seq[string], else: input - let cycle: ParseCycle = (params.map(some) & none(string)) + let cycle: ParseCycle = (params.mapIt(some(it)) & none(string)) .foldl(buildArgs(a, b), (newSeq[ParseArgument](), false, false)) if cycle.stdinConsumed: diff --git a/src/utils.nim b/src/utils.nim index c329b68..25d1b83 100644 --- a/src/utils.nim +++ b/src/utils.nim @@ -2,10 +2,10 @@ import hashes, options, os, posix, sequtils, strutils, sugar, tables type - HaltError* = object of Exception + HaltError* = object of CatchableError code*: int - CommandError* = object of Exception + CommandError* = object of CatchableError color*: Option[bool] error*: bool @@ -211,7 +211,7 @@ proc forkWaitRedirect*(call: () -> int): tuple[output: seq[string], code: int] = proc getgrouplist*(user: cstring, group: Gid, groups: ptr cint, ngroups: var cint): cint {.importc, header: "".} -proc setgroups*(size: csize, groups: ptr cint): cint +proc setgroups*(size: int, groups: ptr cint): cint {.importc, header: "".} proc getUser(uid: int): User = diff --git a/src/wrapper/curl.nim b/src/wrapper/curl.nim index 98d6880..5597ef1 100644 --- a/src/wrapper/curl.nim +++ b/src/wrapper/curl.nim @@ -18,7 +18,7 @@ type url = 10002, writeFunction = 20011 - CurlError* = object of Exception + CurlError* = object of CatchableError {.passL: "-lcurl".} @@ -58,8 +58,8 @@ proc escape*(instance: ref CurlInstance, s: string): string = else: "" -proc curlWriteMemory(mem: array[csize.high, char], size: csize, nmemb: csize, - userdata: ref CurlInstance): csize {.cdecl.} = +proc curlWriteMemory(mem: array[int.high, char], size: int, nmemb: int, + userdata: ref CurlInstance): int {.cdecl.} = let total = size * nmemb if total > 0: userData.data &= mem[0 .. total - 1] -- cgit v1.2.3-70-g09d2