Skip to content

Commit 6047016

Browse files
committed
Added missing insert/replace from standard input.
1 parent f106ac7 commit 6047016

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/text-block.1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
.\" -------------------------------------------------------------------------
3737
.\"
3838
.\" ###### Setup ############################################################
39-
.Dd April 6, 2025
39+
.Dd May 9, 2025
4040
.Dt text-block 1
4141
.Os text-block
4242
.\" ###### Name #############################################################
@@ -169,10 +169,16 @@ Run in Highlight mode.
169169
Run in Delete mode.
170170
.It Fl F Ar insert_file | Fl Fl insert-front Ar insert_file
171171
Run in Insert-Front mode.
172+
"-" is placeholder for inserting from standard input.
173+
In this case, an input file is required.
172174
.It Fl B Ar insert_file | Fl Fl insert-back Ar insert_file
173175
Run in Insert-Back mode.
176+
"-" is placeholder for inserting from standard input.
177+
In this case, an input file is required.
174178
.It Fl R Ar insert_file | Fl Fl replace Ar insert_file
175179
Run in Replace mode.
180+
"-" is placeholder for replacing from standard input.
181+
In this case, an input file is required.
176182
.It Fl h | Fl Fl help
177183
Prints command-line parameters.
178184
.It Fl v | Fl Fl version
@@ -196,8 +202,10 @@ Sets a combined begin/end tag, i.e. the tag marks begin and end.
196202
Tags are mutually exclusive with line selection (--select|-s).
197203
.It Fl i | Fl Fl input Ar input_file
198204
Sets the input file.
205+
"-" is placeholder for reading from standard input (default).
199206
.It Fl o | Fl Fl output Ar output_file
200207
Sets the output file. By default, an existing output file will be overwritten.
208+
"-" is placeholder for writing to standard output (default)
201209
.It Fl a | Fl Fl append
202210
Opens the output file in append mode, appending new output instead of overwriting an existing file.
203211
.Op Fl m | Fl Fl min-actions

src/text-block.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ static const char* Pointer;
110110
[[ noreturn ]] static void cleanUp(int exitCode)
111111
{
112112
if(InsertFile) {
113-
fclose(InsertFile);
113+
if(InsertFile != stdin) {
114+
fclose(InsertFile);
115+
}
114116
InsertFile = nullptr;
115117
}
116118

@@ -162,7 +164,9 @@ static long long countLines(FILE* inputFile)
162164
lines++;
163165
line = Buffer;
164166
}
165-
rewind(inputFile);
167+
if(inputFile != stdin) {
168+
rewind(inputFile);
169+
}
166170

167171
return lines;
168172
}
@@ -171,8 +175,6 @@ static long long countLines(FILE* inputFile)
171175
// ###### Write contents of insert file #####################################
172176
static void copyInsertFileIntoOutputFile()
173177
{
174-
rewind(InsertFile);
175-
176178
char buffer[65536];
177179
ssize_t bytesRead;
178180
while( (bytesRead = fread((char*)&buffer, 1, sizeof(buffer), InsertFile)) > 0 ) {
@@ -184,6 +186,9 @@ static void copyInsertFileIntoOutputFile()
184186
fputs("\n", stderr);
185187
cleanUp(1);
186188
}
189+
if(InsertFile != stdin) {
190+
rewind(InsertFile);
191+
}
187192
}
188193

189194

@@ -594,6 +599,9 @@ int main (int argc, char** argv)
594599

595600
// ====== Open input file ================================================
596601
InputFile = stdin;
602+
if( (InputFileName != nullptr) && (strcmp(InputFileName, "-") == 0) ) {
603+
InputFileName = nullptr; // Special case: - => stdin
604+
}
597605
if(InputFileName != nullptr) {
598606
InputFile = fopen(InputFileName, "r");
599607
if(InputFile == nullptr) {
@@ -633,6 +641,9 @@ int main (int argc, char** argv)
633641

634642
// ====== Open output file ===============================================
635643
OutputFile = stdout;
644+
if( (OutputFileName != nullptr) && (strcmp(OutputFileName, "-") == 0) ) {
645+
OutputFileName = nullptr; // Special case: - => stdout
646+
}
636647
if(OutputFileName != nullptr) {
637648
OutputFile = fopen(OutputFileName, (OpenOutputAppend == false) ? "w" : "a");
638649
if(OutputFile == nullptr) {
@@ -648,6 +659,15 @@ int main (int argc, char** argv)
648659
}
649660

650661
// ====== Open insert file ===============================================
662+
if( (InsertFileName != nullptr) && (strcmp(InsertFileName, "-") == 0) ) {
663+
InsertFileName = nullptr; // Special case: - => stdin
664+
if(InputFile == stdin) {
665+
fputs(gettext("ERROR: Insert from stdin requires an input file!"), stderr);
666+
fputs("\n", stderr);
667+
cleanUp(1);
668+
}
669+
InsertFile = stdin;
670+
}
651671
if(InsertFileName != nullptr) {
652672
InsertFile = fopen(InsertFileName, "r");
653673
if(InsertFile == nullptr) {
@@ -660,7 +680,7 @@ int main (int argc, char** argv)
660680
posix_fadvise(fileno(InputFile), 0, 0, POSIX_FADV_SEQUENTIAL|POSIX_FADV_WILLNEED);
661681
#endif
662682
}
663-
else {
683+
if(InputFile == nullptr) {
664684
if( (Mode == InsertFront) || (Mode == InsertBack) || (Mode == Replace) ) {
665685
fputs(gettext("ERROR: No insert file provided!"), stderr);
666686
fputs("\n", stderr);

0 commit comments

Comments
 (0)