Skip to content

Commit

Permalink
Modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Edd12321 committed Aug 15, 2023
1 parent 2b8a7bf commit 6aaad5c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 58 deletions.
22 changes: 11 additions & 11 deletions .zrc
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ fn extract {
let s {
set s = $argv(1)
switch $s {
reg {.\.tar.bz2$} { tar xjf $s }
reg {.\.tar.gz$} { tar xzf $s }
reg {.\.bz2$} { bunzip2 $s }
reg {.\.rar$} { unrar e $s }
reg {.\.gz$} { gunzip $s }
reg {.\.tar$} { tar xf $s }
reg {.\.tbz2$} { tar xjf $s }
reg {.\.tgz$} { tar xzf $s }
reg {.\.zip$} { unzip $s }
reg {.\.Z$} { uncompress $s }
reg {.\.7z$} { 7z x $s }
reg {.*\.tar.bz2$} { tar xjf $s }
reg {.*\.tar.gz$} { tar xzf $s }
reg {.*\.bz2$} { bunzip2 $s }
reg {.*\.rar$} { unrar e $s }
reg {.*\.gz$} { gunzip $s }
reg {.*\.tar$} { tar xf $s }
reg {.*\.tbz2$} { tar xjf $s }
reg {.*\.tgz$} { tar xzf $s }
reg {.*\.zip$} { unzip $s }
reg {.*\.Z$} { uncompress $s }
reg {.*\.7z$} { 7z x $s }
default {
echo '$s could not be extracted!' >(1=2)
return 1
Expand Down
6 changes: 4 additions & 2 deletions doc/man1/read.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.SH NAME
read \- Read text from standard input
.SH SYNOPSIS
.BI "read [" "-d <delim>" | "-n <nchars>" "] [" "-p <prompt>" "] [" "<var1> <var2>..." ]
.BI "read [" "-d <delim>" | "-n <nchars>" "] [" "-p <prompt>" "] [" "-f <fd>" "] [" "<var1> <var2>..." ]
.SH DESCRIPTION
.I Read
is a command used to do various methods of text input. It can be used in conjunction with
Expand All @@ -16,8 +16,10 @@ Choose which delimiter to use (IFS)
.B n <nchars>
Read n characters
.TP
.B d <prompt>
.B p <prompt>
Display a prompt to the user
.B f <fd>
Read from a file descriptor
.SH EXAMPLES
.EX
read str
Expand Down
92 changes: 51 additions & 41 deletions src/dispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,38 +279,44 @@ Command(do) {
/** Switch statement implementation **/
Command(switch) {
std::string def_cmd;
std::string se = "<value> {<case <c> cmd|reg <r>|default <block>...}";
WordList args;

if (argc != 3)
syntax_error("<value> {<case <c> cmd|reg <r>|default <block>...}");

/* empty */ {
NullFin;
args = tokenize(argv[2], fin);
}
syntax_error(se);
{ NullFin; args = tokenize(argv[2], fin); }
std::for_each(args.wl.begin(), args.wl.end(), &str_subst);
for (int i = 0, argc = args.size(); i < argc; i += 3) {

for (int i = 0; i < args.size(); ) {
if (args.wl[i] == "case") {
if (i >= argc-2)
syntax_error("`case` ends too early");
if (args.wl[i+1] == argv[1]) {
if (i >= args.size()-2)
syntax_error(se);
if (argv[1] == args.wl[i+1]) {
eval(args.wl[i+2]);
NoReturn;
}
} else if (args.wl[i] == "reg") {
if (i >= argc-2)
syntax_error("`reg` ends too early");
std::regex sr(args.wl[i+1]);
if (std::regex_search(argv[1], sr)) {
i += 3;
}

else if (args.wl[i] == "reg") {
if (i >= args.size()-2)
syntax_error(se);
std::regex sr{args.wl[i+1]};
if (std::regex_match(argv[1], sr)) {
eval(args.wl[i+2]);
NoReturn;
}
} else if (args.wl[i] == "default") {
if (i >= argc-1)
syntax_error("`default` ends too early");
def_cmd = args.wl[1+i--];
} else {
syntax_error("Expected `case`/`default`");
i += 3;
}

else if (args.wl[i] == "default") {
if (i >= args.size()-1)
syntax_error(se);
def_cmd = args.wl[i+1];
i += 2;
}

else {
syntax_error(se);
}
}
eval(def_cmd);
Expand Down Expand Up @@ -459,13 +465,14 @@ Command(echo) {

/** Reads from stdin **/
Command(read) {
const char *se = "[-d <delim>|-n <nchars>] [-p <prompt>] [<var1> <var2>...]";
const char *se = "[-d <delim>|-n <nchars>] [-p <prompt>] [-f <fd>] [<var1> <var2>...]";
std::string buf;
long n = -1, i;
char d = '\n', b;
char d = '\n';
optind = 0;
int opt;
while ((opt = getopt(argc, argv, "d:n:p:")) != -1) {
int fd = STDIN_FILENO;
while ((opt = getopt(argc, argv, "d:n:p:f:")) != -1) {
switch (opt) {
case 'd':
if (n != -1)
Expand All @@ -477,30 +484,37 @@ Command(read) {
syntax_error(se);
n = atoi(optarg);
break;
case 'f':
fd = atoi(optarg);
break;
case 'p':
std::cout << optarg << std::flush;
break;
case '?':
syntax_error(se);
}
}
// buffers
char b[(n > 0) ? (n+1) : 1];
char c;
#define GET_INPUT \
buf.clear(); \
if (n == -1) { \
int ok; \
ok = read(0, &b, 1); \
ok = read(fd, &c, 1); \
if (ok != 1) \
return "1"; \
buf += b; \
buf += c; \
for ever { \
ok = read(0, &b, 1); \
if (ok != 1 || b == d) \
ok = read(fd, &c, 1); \
if (ok != 1 || c == d) \
break; \
buf += b; \
buf += c; \
} \
} else for (i = 0; i < n; ++i) {\
if (read(0, &b, 1) != 1) \
} else { \
if (read(fd, b, n) != n) \
return "1"; \
b[n] = '\0'; \
buf += b; \
}

Expand All @@ -525,7 +539,7 @@ Command(inc) {
if (argc >= 3)
val = expr(combine(argc, argv, 2));
var = getvar(argv[1]);
ret_val = expr(S("(")+var+")+("+val+")");
ret_val = expr(zrc_fmt("(%s)+(%s)", var.data(), val.data()));
setvar(argv[1], ret_val);
NoReturn;
}
Expand All @@ -547,14 +561,10 @@ Command(set) {
size_t len = strlen(argv[i]);
if (argv[i][len-1] == '=') {
argv[i][len-1] = '\0';
setvar(argv[i-1], expr(
S("(")
+ getvar(argv[i-1])
+ ")"
+ argv[i]
+ "("
+ argv[i+1]
+ ")"));
setvar(argv[i-1], expr(zrc_fmt("(%s)%s(%s)",
getvar(argv[i-1]).data(),
argv[i],
argv[i+1])));
} else syntax_error(se);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ expr(std::string e, ExprType mode)
str_subst(e);
if (!is_expr(e))
INVALIDSYN;

//=====functions===== =====operators=====
REP("log10", LOG10); REP("&&" , AND);
REP("log2" , LOG2 ); REP("||" , OR );
Expand Down
26 changes: 23 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
#include <limits.h>
#include <math.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include <algorithm>
#include <filesystem>
#include <format>
#include <fstream>
#include <functional>
#include <iomanip>
Expand Down Expand Up @@ -92,7 +94,7 @@
*/
#define CHK_FD \
/*if (std::regex_match(*it,m,e)){ */ \
if ((*it).length()>3&&(*it)[0]=='>'&&(*it)[1]=='('&&(*it).back()==')') [[unlikely]] {\
if ((*it).length()>3 && (*it)[0]=='>' && (*it)[1]=='(' && (*it).back()==')') [[unlikely]] {\
sword = 1; \
size_t i = 2; \
int fd1 = 0, fd2 = 0; \
Expand Down Expand Up @@ -124,7 +126,7 @@
} \
} \
baks[fd1] = dup(fd1); \
dup2(dup(fd2), fd1); \
dup2(dup(fd2), fd1); \
} \
} else if (i == (*it).length()-1) { \
baks[fd1] = dup(fd1); \
Expand All @@ -133,7 +135,7 @@
std::cerr << errmsg << ">(..?)"; \
} \
continue; \
}
}

/**
* Check if a word is an interp alias
Expand Down Expand Up @@ -192,6 +194,7 @@ typedef int Jid;
static bool die (std::string_view );
static inline bool is_number (std::string_view );
WordList glob (std::string_view );
template<typename... Var> std::string zrc_fmt(const char *fmt, Var... args);
static std::string eval_stream (std::istream& );
template<typename T> std::string eval(T const& );
// MAIN.CPP
Expand Down Expand Up @@ -312,6 +315,23 @@ glob(std::string_view s)
return wl;
}

/** Formatted string support
*
* @param {const char*}fmt,...
* @return string
*/
template<typename... Var> std::string
zrc_fmt(const char *fmt, Var... args)
{
size_t len = snprintf(nullptr, 0, fmt, args...)+1;
char *ret = new char[len];
snprintf(ret, len, fmt, args...);

std::string ret_str{ret};
delete [] ret;
return ret_str;
}

/** Evaluate a text stream.
*
* @param {istream&}in
Expand Down
2 changes: 1 addition & 1 deletion src/sighandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ bg_fg(int argc, char *argv[])
{
Job *j;
Jid index = -1;
if (argc != 2 && (!isdigit(*argv[1]) && *argv[1] != '%'))
if (argc != 2 || (!isdigit(*argv[1]) && *argv[1] != '%'))
syntax_error("<pid>|<%jid>");
if (*argv[1] == '%') {
int num = atoi(&argv[1][1]);
Expand Down

0 comments on commit 6aaad5c

Please sign in to comment.