aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-05-17 16:19:17 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-05-17 16:19:17 -0700
commit9ec5bed6a7d715ffa69851569485a685dd69db2e (patch)
treec25eaef1000f52caacf25ee398e54f63067b8feb /include
parentc92c89280d036240a75ff122084dd58cde473394 (diff)
prototype of front end cli
Diffstat (limited to 'include')
-rw-r--r--include/libn.h107
1 files changed, 72 insertions, 35 deletions
diff --git a/include/libn.h b/include/libn.h
index c57868c..87c993c 100644
--- a/include/libn.h
+++ b/include/libn.h
@@ -105,21 +105,37 @@ error coro·free(Coro *c);
// -----------------------------------------------------------------------------
// Strings
-// TODO(nnoll): Move here?
typedef byte* string;
-typedef struct str·Hdr
-{
- vlong len;
- vlong cap;
- byte buf[];
-} str·Hdr;
-
-// -------------------------------------------------------------------------
-// UTF-8 functions.
-// Perhaps break into own unit
-// TODO: Add to(upper|lower|title)
+/* string helpers */
+string str·makecap(const byte *s, vlong len, vlong cap);
+string str·makelen(const byte *s, vlong len);
+string str·make(const byte *s);
+string str·makef(const byte *fmt, ...);
+void str·free(string s);
+int str·len(const string s);
+int str·cap(const string s);
+void str·clear(string *s);
+void str·grow(string *s, vlong delta);
+void str·fit(string *s);
+void str·appendlen(string *s, vlong len, const byte *b);
+void str·append(string *s, const byte* b);
+void str·appendf(string *s, const byte* fmt, ...);
+void str·appendbyte(string *s, const byte b);
+bool str·equals(const string s, const string t);
+int str·find(string s, const byte* substr);
+void str·lower(string s);
+void str·upper(string s);
+int str·read(string s, int size, int n, void *buf);
+void str·replace(string s, const byte* from, const byte* to);
+string* str·split(string s, const byte* tok);
+string str·join(vlong len, byte** fields, const byte* sep);
+ /*
+ * UTF-8 functions.
+ * Perhaps break into own unit
+ * TODO: Add to(upper|lower|title)
+ */
typedef uint32 rune;
enum
@@ -134,6 +150,7 @@ enum
/* utf8 helpers */
int utf8·fullrune(byte *s, int n);
byte *utf8·findrune(byte *s, long i);
+byte *utf8·findrrune(byte* s, long c);
int utf8·chartorune(rune *r, byte *s);
int utf8·runetochar(byte *s, rune *r);
int utf8·len(byte *s);
@@ -143,29 +160,6 @@ int utf8·isdigit(rune r);
int utf8·isspace(rune r);
int utf8·istitle(rune r);
-/* string helpers */
-string str·makecap(const byte *s, vlong len, vlong cap);
-string str·makelen(const byte *s, vlong len);
-string str·make(const byte *s);
-string str·makef(const byte *fmt, ...);
-void str·free(string s);
-int str·len(const string s);
-int str·cap(const string s);
-string str·clear(string s);
-string str·grow(string s, vlong delta);
-string str·fit(string s);
-string str·appendcount(string s, vlong len, const byte *b);
-string str·append(string s, const byte* b);
-string str·appendf(string s, const byte* fmt, ...);
-string str·appendbyte(string s, const byte b);
-bool str·equals(const string s, const string t);
-int str·find(string s, const byte* substr);
-void str·lower(string s);
-void str·upper(string s);
-void str·replace(string s, const byte* from, const byte* to);
-string* str·split(string s, const byte* tok);
-string str·join(vlong len, byte** fields, const byte* sep);
-
// -----------------------------------------------------------------------------
// I/O
@@ -179,8 +173,16 @@ enum SeekPos
seek·end = SEEK_END
};
+enum
+{
+ ReadOK = R_OK,
+ WriteOK = W_OK,
+ ExecOK = X_OK,
+};
+
Stream *io·open(byte *name, byte *mode);
int io·fd(Stream *s);
+int io·exists(byte *path, int flag);
error io·stat(Stream *s, io·Stat *buf);
error io·close(Stream *s);
byte io·getbyte(Stream *s);
@@ -361,3 +363,38 @@ error rng·init(uint64 seed);
double rng·random();
bool rng·bernoulli(double f);
uint64 rng·randi(int max);
+
+// -----------------------------------------------------------------------------
+// variable arguments
+
+/* from plan9 libc */
+
+#define SET(x) ((x)=0)
+#define USED(x) if(x){}else{}
+#ifdef __GNUC__
+# if __GNUC__ >= 3
+# undef USED
+# define USED(x) ((void)(x))
+# endif
+#endif
+
+extern char *argv0;
+#define ARGBEGIN for((argv0?0:(argv0=*argv)),argv++,argc--;\
+ argv[0] && argv[0][0]=='-' && argv[0][1];\
+ argc--, argv++) {\
+ byte *_args, *_argt;\
+ rune _argc;\
+ _args = &argv[0][1];\
+ if(_args[0]=='-' && _args[1]==0){\
+ argc--; argv++; break;\
+ }\
+ _argc = 0;\
+ while(*_args && (_args += utf8·chartorune(&_argc, _args)))\
+ switch(_argc)
+#define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
+#define ARGF() (_argt=_args, _args="",\
+ (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
+#define EARGF(x) (_argt=_args, _args="",\
+ (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
+
+#define ARGC() _argc