nstr NSys::stripPath(const nstr& path){ size_t offset = 0; for(size_t i = path.length() - 1; i > 0; --i){ if(path[i] == '/'){ ++offset; } else{ break; } } return path.substr(0, path.length() - offset); }
void write(const nstr& in){ if(ifd_ < 0){ NERROR("command was not started with input mode"); } int n = ::write(ifd_, in.c_str(), in.length()); if(n < 0){ NERROR("error while writing"); } }
nstr NSys::basename(const nstr& path){ nstr ret; for(int i = path.length() - 1; i >= 0; --i){ if(path[i] == '/'){ break; } ret.insert(0, 1, path[i]); } return ret; }
void NSys::replaceEnvs(nstr& s){ size_t len = s.length(); for(size_t i = 0; i < len; ++i){ if(s[i] == '$' && (i == 0 || s[i - 1] != '\\') && (i < len && s[i + 1] == '(')){ size_t begin = i; i += 2; nstr env; bool fin = false; for(; i < len; ++i){ if(s[i] == ')'){ fin = true; break; } env += s[i]; } if(!fin){ NERROR("unterminated environment variable"); } nstr v; if(!getEnv(env, v)){ NERROR("invalid environment variable: " + env); } s.replace(begin, i - begin + 1, v); i = begin + v.length(); len = s.length(); } } }