added -v functionality

This commit is contained in:
rexy712 2019-05-18 12:09:43 -07:00
parent df44c75bdb
commit ae77265efc
3 changed files with 50 additions and 6 deletions

View File

@ -67,6 +67,7 @@ struct cmd_args{
cmd_args(void)noexcept;
void clear_gnu_options(void);
void set_nonprinting(bool = true);
};
cmd_args process_cmd_args(int argc, char** argv);

View File

@ -30,6 +30,9 @@ void cmd_args::clear_gnu_options(void){
number = PRINT_LINE_NEVER;
ends = squeeze = tabs = nonprinting = 0;
}
void cmd_args::set_nonprinting(bool i){
nonprinting = treatbinary = i;
}
static constexpr size_t constexpr_strlen(const char* str){
size_t i = 0;
@ -129,8 +132,9 @@ static constexpr const char SQUEEZE_BLANKS_DESC[] = "suppress repeated
static constexpr const char* VT_DESC = concat_many_strs<EQUIV_DESC_BASE,NONPRINTING_SHORT_OPTION,SHOW_TABS_SHORT_OPTION>::str;
static constexpr const char SHOW_TABS_DESC[] = "display TAB characters as ^I";
static constexpr const char* U_IGNORED_DESC = IGNORED_DESC;
static constexpr const char NONPRINTING_DESC[] = "should use ^ and M- notation, except for LFD and TAB (not supported as of now)";
static constexpr const char BINARY_DESC[] = "treat FILE as a binary file";
static constexpr const char NONPRINTING_DESC_BASE[] = "should use ^ and M- notation, except for LFD and TAB. Implies -";
static constexpr const char* NONPRINTING_DESC = concat_many_strs<NONPRINTING_DESC_BASE,BINARY_SHORT_OPTION>::str;
static constexpr const char USAGE_DESC[] = "display this help and exit";
static constexpr const char VERSION_DESC[] = "output version information and exit";
static constexpr const char SEED_DESC[] = "Rainbow seed, 0 = random (default: 0)";
@ -237,13 +241,15 @@ cmd_args process_cmd_args(int argc, char** argv){
for(size_t j = 1;j < arg_len;++j){
switch(argv[i][j]){
case SHORT_OPT(SHOW_ALL):
ret.nonprinting = ret.ends = ret.tabs = 1;
ret.set_nonprinting();
ret.ends = ret.tabs = 1;
break;
case SHORT_OPT(NUMBER_NONBLANK):
ret.number = PRINT_LINE_NONBLANK;
break;
case SHORT_OPT(VE):
ret.nonprinting = ret.ends = 1;
ret.set_nonprinting();
ret.ends = 1;
break;
case SHORT_OPT(SHOW_ENDS):
ret.ends = 1;
@ -256,6 +262,7 @@ cmd_args process_cmd_args(int argc, char** argv){
ret.squeeze = 1;
break;
case SHORT_OPT(VT):
ret.set_nonprinting();
ret.nonprinting = ret.tabs = 1;
break;
case SHORT_OPT(SHOW_TABS):
@ -264,7 +271,7 @@ cmd_args process_cmd_args(int argc, char** argv){
case SHORT_OPT(U_IGNORED):
break;
case SHORT_OPT(NONPRINTING):
ret.nonprinting = 1;
ret.set_nonprinting();
break;
case SHORT_OPT(BINARY):
ret.treatbinary = 1;
@ -330,7 +337,7 @@ cmd_args process_cmd_args(int argc, char** argv){
}else if(CHECK_LONG_OPTION(SHOW_TABS, arg)){
ret.tabs = 1;
}else if(CHECK_LONG_OPTION(NONPRINTING, arg)){
ret.nonprinting = 1;
ret.set_nonprinting();
}else if(CHECK_LONG_OPTION(BINARY, arg)){
ret.treatbinary = 1;
}else if(CHECK_LONG_OPTION(USAGE, arg)){

View File

@ -103,6 +103,37 @@ constexpr void print_version(Printer&& p){
p.reset();
}
void to_caret_notation(int in, char* dest){
dest[0] = '^';
dest[1] = in ^ 0x40;
dest[2] = 0;
}
void to_mdash_notation(int in, char* dest){
int corin = in & 0x7F;
dest[0] = 'M';
dest[1] = '-';
if(corin > 32 && corin < 127){
dest[2] = corin;
dest[3] = 0;
}else{
char tmp[3];
to_caret_notation(corin, tmp);
dest[2] = tmp[0];
dest[3] = tmp[1];
dest[4] = 0;
}
}
void nonprinting_notation(int in, char* dest){
if(in & 0x80){
to_mdash_notation(in, dest);
}else if(in == '\n' || in == '\t' || (in > 31 && in < 127)){
dest[0] = in;
dest[1] = 0;
}else{
to_caret_notation(in, dest);
}
}
//Use the given type of printer to output the contents of files and/or stdin
template<class Printer>
int print_normal_files(cmd_args& args){
@ -161,6 +192,11 @@ int print_binary_files(cmd_args& args){
continue;
}
for(int in;(in = fgetc(fp)) != WEOF;)
if(args.nonprinting){
char tmp[5];
nonprinting_notation(in, tmp);
p.print(tmp);
}else
p.print(static_cast<char>(in));
p.reset();
fclose(fp);