From ed93f80f4a004fdd230be9305a8a9f1b8795b017 Mon Sep 17 00:00:00 2001 From: Oleg Bakharev Date: Sun, 17 Apr 2022 14:29:14 +0300 Subject: [PATCH] Added function to transform 9P/Styx messages to nice output form --- source/styx2000/extrautil/mischelpers.d | 229 ++++++++++++++++++++++++ 1 file changed, 229 insertions(+) diff --git a/source/styx2000/extrautil/mischelpers.d b/source/styx2000/extrautil/mischelpers.d index 94edde7..3782227 100644 --- a/source/styx2000/extrautil/mischelpers.d +++ b/source/styx2000/extrautil/mischelpers.d @@ -180,6 +180,235 @@ auto toPlan9Stat(Stat stat) } +/// Translate Plan 9 Message to standard output form in Plan 9 +auto toPlan9Message(StyxMessage msg) +{ + string representation; + + auto tag = msg[2].toTag.getTag; + + final switch (msg[1].toType.getType) with (STYX_MESSAGE_TYPE) + { + /// T-Messages + + case T_VERSION: + representation = format( + `Tversion tag %d msize %d version '%s'`, + tag, + msg[3].toMsize.getMsize, + msg[4].toVersion.getVersion + ); + break; + case T_AUTH: + uint afid = msg[3].toAfid.getAfid; + representation = format( + `Tauth tag %d afid uname '%s' aname '%s'`, + tag, + cast(int) ((afid == uint.max) ? -1 : afid), + msg[4].toUname.getUname, + msg[5].toAname.getAname + ); + break; + case T_FLUSH: + representation = format( + `Tflush oldtag %d`, + msg[3].toOldTag.getTag + ); + break; + case T_ATTACH: + uint afid = msg[4].toAfid.getAfid; + representation = format( + `Tattach tag %d fid %d afid %d uname '%s' aname '%s'`, + tag, + msg[3].toFid.getFid, + cast(int) ((afid == uint.max) ? -1 : afid), + msg[5].toUname.getUname, + msg[6].toAname.getAname + ); + break; + case T_WALK: + representation = format( + `Twalk tag %d fid %d newfid %d %s`, + tag, + msg[3].toFid.getFid, + msg[4].toNewFid.getFid, + msg[5].toNwname.toPlan9Nwname, + ); + break; + case T_OPEN: + representation = format( + `Topen tag %d fid %d mode %d`, + tag, + msg[3].toFid.getFid, + cast(uint) msg[4].toMode.getMode + ); + break; + case T_CREATE: + representation = format( + `Tcreate tag %d fid %d '%s' m %s mode %d`, + tag, + msg[3].toFid.getFid, + msg[4].toName.getName, + msg[5].toPerm.toPlan9Permissions, + cast(uint) msg[6].toMode.getMode + ); + break; + case T_READ: + representation = format( + `Tread tag %d fid %d offset %d count %d`, + tag, + msg[3].toFid.getFid, + msg[4].toOffset.getOffset, + msg[5].toCount.getCount, + ); + break; + case T_WRITE: + uint count = msg[5].toCount.getCount; + representation = format( + `Twrite tag %d fid %d offset %d count %d '%s'`, + tag, + msg[3].toFid.getFid, + msg[4].toOffset.getOffset, + count, + msg[6].toData.toPlan9Chunk(count) + ); + break; + case T_CLUNK: + representation = format( + `Tstat tag %d fid %d`, + tag, + msg[3].toFid.getFid, + ); + break; + case T_REMOVE: + representation = format( + `Tremove tag %d fid %d`, + tag, + msg[3].toFid.getFid, + ); + break; + case T_STAT: + representation = format( + `Tclunk tag %d fid %d`, + tag, + msg[3].toFid.getFid, + ); + break; + case T_WSTAT: + representation = format( + `Twstat tag %d fid %d stat %s`, + tag, + msg[3].toFid.getFid, + msg[4].toStat.toPlan9Stat + ); + break; + + /// R-Messages + + case R_VERSION: + representation = format( + `Rversion tag %d msize %d version '%s'`, + tag, + msg[3].toMsize.getMsize, + msg[4].toVersion.getVersion + ); + break; + case R_AUTH: + representation = format( + `Rauth tag %d aqid %s`, + tag, + msg[3].toAqid.toPlan9Qid + ); + break; + case R_FLUSH: + representation = format( + `Rflush tag %d`, + tag, + ); + break; + case R_ERROR: + representation = format( + `Rerror tag %d ename %s`, + tag, + msg[3].toEname.getName + ); + break; + case R_ATTACH: + representation = format( + `Rattach tag %d qid %s`, + tag, + msg[3].toQid.toPlan9Qid + ); + break; + case R_WALK: + representation = format( + `Rwalk tag %d %s`, + tag, + msg[3].toNwqid.toPlan9Nwqid + ); + break; + case R_OPEN: + representation = format( + `Ropen tag %d qid %s iounit %d`, + tag, + msg[3].toQid.toPlan9Qid, + msg[4].toIounit.getUnit, + ); + break; + case R_CREATE: + representation = format( + `Rcreate tag %d qid %s iounit %d`, + tag, + msg[3].toQid.toPlan9Qid, + msg[4].toIounit.getUnit, + ); + break; + case R_READ: + uint count = msg[3].toCount.getCount; + representation = format( + `Rread tag %d count %d '%s'`, + tag, + count, + msg[4].toData.toPlan9Chunk(count) + ); + break; + case R_WRITE: + representation = format( + `Rwrite tag %d count %d`, + tag, + msg[3].toCount.getCount, + ); + break; + case R_CLUNK: + representation = format( + `Rclunk tag %d`, + tag, + ); + break; + case R_REMOVE: + representation = format( + `Rremove tag %d`, + tag + ); + break; + case R_STAT: + representation = format( + `Rstat tag %d stat %s`, + tag, + msg[3].toStat.toPlan9Stat, + ); + break; + case R_WSTAT: + representation = format( + `Rwstat tag %d`, + tag + ); + break; + } + return representation; +} + + /// Translate data chunks to their string representation (string are the same as in Plan 9) auto toPlan9Chunk(Data data, uint count) {