Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ count below and mark it as done in this README.md. Thanks!

Commit message should follow [conventional commits](https://www.conventionalcommits.org/)

## Completed (6/109)
## Completed (8/109)

| Done | Cmd | Descripton |
| :-----: | --------- | ------------------------------------------------ |
Expand All @@ -43,7 +43,7 @@ Commit message should follow [conventional commits](https://www.conventionalcomm
| | base64 | Transform data into printable data |
| | basename | Strip directory and suffix from a file name |
| | basenc | Transform data into printable data |
| | cat | Concatenate and write files |
| ✓ | cat | Concatenate and write files |
| | chcon | Change SELinux context of file |
| | chgrp | Change group ownership |
| | chmod | Change access permissions |
Expand Down Expand Up @@ -141,7 +141,7 @@ Commit message should follow [conventional commits](https://www.conventionalcomm
| | uptime | Print system uptime and load |
| | users | Print login names of users currently logged in |
| | vdir | Verbosely list directory contents |
| | wc | Print newline, word, and byte counts |
| ✓ | wc | Print newline, word, and byte counts |
| | who | Print who is currently logged in |
| | whoami | Print effective user ID |
| ✓ | yes | Print a string until interrupted |
25 changes: 25 additions & 0 deletions src/cat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# cat

## specification

The Open Group Base Specifications Issue 8
IEEE Std 1003.1-2024
Copyright © 2001-2024 The IEEE and The Open Group

<https://pubs.opengroup.org/onlinepubs/9799919799/utilities/cat.html>

```txt
cat [-u] [file...]

The cat utility shall conform to XBD 12.2 Utility Syntax Guidelines .

The following option shall be supported:

-u
Write bytes from the input file to the standard output without delay as each is read.

file
A pathname of an input file. If no file operands are specified, the standard input shall be used. If a file is '-', the cat utility shall read from the standard input at that point in the sequence. The cat utility shall not close and reopen standard input when it is referenced in this way, but shall accept multiple occurrences of '-' as a file operand.

The standard input shall be used only if no file operands are specified, or if a file operand is '-'. See the INPUT FILES section.
```
61 changes: 61 additions & 0 deletions src/cat/main.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
struct CatOption {
unbuffered: Bool
files: Array[String]
}
// Parse command line arguments to extract -u flag and files
fn parse_args(args: Array[String]) -> CatOption {
let mut unbuffered = false
let files: Array[String] = []
let mut i = 0 // Skip program name

while i < args.length() {
let arg = args[i]
if arg == "-u" {
unbuffered = true
} else {
files.push(arg)
}
i = i + 1
}

return {unbuffered, files}
}

// Read from file and write to stdout
fn cat_file!(path~: String) -> Unit {
let file_content = @fs.read_file_to_string!(path = path)
let file_lines = file_content.split("\n").to_array()
for line in file_lines{
println(line)
}
}

fn main {
let args = @sys.get_cli_args()

let catOption = parse_args(args)

// Set unbuffered mode if requested
if catOption.unbuffered {
println("Unbuffered not supported yet")
return
}

// If no files specified, read from stdin
if catOption.files.length() == 0 {
println("Reading from stdin not supported yet")
return
}

// Process each file
for file in catOption.files {
match @fs.is_file?(path=file){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The document stats that

If a file is '-', the cat utility shall read from the standard input

I think we raise same error "Reading from stdin not supported yet"

Ok(true) => {
let _ = cat_file?(path=file)
}
_ => {
println("File not found: " + file)
}
}
}
}
7 changes: 7 additions & 0 deletions src/cat/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"is-main": true,
"import": [
"moonbitlang/x/sys",
"moonbitlang/x/fs"
]
}
5 changes: 5 additions & 0 deletions tests/cat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -e

cat README.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moon run src/cat? then grep "coreutils" for baseline.

Loading