github.com/gotranspile/cxgo@v0.3.7/libs/stdio.go (about) 1 package libs 2 3 import ( 4 "github.com/gotranspile/cxgo/runtime/stdio" 5 "github.com/gotranspile/cxgo/types" 6 ) 7 8 // TODO: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdio.h.html 9 10 const ( 11 StdioH = "stdio.h" 12 ) 13 14 func init() { 15 // TODO: must #include <stdarg.h> 16 // TODO: must #include <sys/types.h> 17 // TODO: off_t and ssize_t must be in <sys/types.h> 18 RegisterLibrary(StdioH, func(c *Env) *Library { 19 gintT := c.Go().Int() 20 gstrT := c.Go().String() 21 intT := types.IntT(4) 22 longT := types.IntT(8) 23 cstrT := c.C().String() 24 cbytesT := c.C().String() 25 bytesT := c.C().String() 26 filePtr := c.PtrT(nil) 27 valistT := c.GetLibraryType(BuiltinH, "__builtin_va_list") 28 fileT := types.NamedTGo("FILE", "stdio.File", c.MethStructT(map[string]*types.FuncType{ 29 "PutC": c.FuncTT(intT, gintT), 30 "PutS": c.FuncTT(intT, cstrT), 31 "WriteN": c.FuncTT(intT, cbytesT, gintT, gintT), 32 "Write": c.FuncTT(intT, cbytesT, gintT), 33 "ReadN": c.FuncTT(intT, bytesT, gintT, gintT), 34 "Read": c.FuncTT(intT, bytesT, gintT), 35 "Tell": c.FuncTT(longT), 36 "Seek": c.FuncTT(intT, longT, intT), 37 "GetC": c.FuncTT(gintT), 38 "UnGetC": c.FuncTT(intT, gintT), 39 "GetS": c.FuncTT(cstrT, cstrT, intT), 40 "IsEOF": c.FuncTT(intT), 41 "FileNo": c.FuncTT(c.Go().Uintptr()), 42 "Flush": c.FuncTT(intT), 43 "Close": c.FuncTT(intT), 44 })) 45 filePtr.SetElem(fileT) 46 l := &Library{ 47 Imports: map[string]string{ 48 "stdio": RuntimePrefix + "stdio", 49 }, 50 Types: map[string]types.Type{ 51 "FILE": fileT, 52 }, 53 Idents: map[string]*types.Ident{ 54 "SEEK_SET": c.NewIdent("SEEK_SET", "stdio.SEEK_SET", stdio.SEEK_SET, intT), 55 "SEEK_CUR": c.NewIdent("SEEK_CUR", "stdio.SEEK_CUR", stdio.SEEK_CUR, intT), 56 "SEEK_END": c.NewIdent("SEEK_END", "stdio.SEEK_END", stdio.SEEK_END, intT), 57 58 "_cxgo_EOF": c.NewIdent("_cxgo_EOF", "stdio.EOF", stdio.EOF, types.UntypedIntT(1)), 59 }, 60 Header: ` 61 #include <` + BuiltinH + `> 62 #include <` + stddefH + `> 63 #include <` + StdargH + `> 64 #include <` + StdlibH + `> 65 #include <` + sysTypesH + `> 66 67 typedef struct FILE FILE; 68 69 typedef struct FILE { 70 _cxgo_sint32 (*PutC)(_cxgo_go_int); 71 _cxgo_sint32 (*PutS)(const char*); 72 _cxgo_sint32 (*WriteN)(const void*, _cxgo_go_int, _cxgo_go_int); 73 _cxgo_sint32 (*Write)(const void*, _cxgo_go_int); 74 _cxgo_sint32 (*ReadN)(void*, _cxgo_go_int, _cxgo_go_int); 75 _cxgo_sint32 (*Read)(void*, _cxgo_go_int); 76 _cxgo_sint64 (*Tell)(void); 77 _cxgo_sint32 (*Seek)(_cxgo_sint64, _cxgo_sint32); 78 _cxgo_go_int (*GetC)(void); 79 _cxgo_sint32 (*UnGetC)(_cxgo_go_int); 80 char* (*GetS)(char*, _cxgo_sint32); 81 _cxgo_sint32 (*IsEOF)(void); 82 _cxgo_go_uintptr (*FileNo)(void); 83 _cxgo_sint32 (*Flush)(void); 84 _cxgo_sint32 (*Close)(void); 85 } FILE; 86 87 #define fpos_t _cxgo_uint64 88 #define int _cxgo_int64 89 90 #define FILENAME_MAX 255 91 92 const _cxgo_sint32 SEEK_SET = 0; 93 const _cxgo_sint32 SEEK_CUR = 1; 94 const _cxgo_sint32 SEEK_END = 2; 95 96 const int _cxgo_EOF = -1; 97 #define EOF _cxgo_EOF 98 99 `, 100 } 101 102 l.Declare( 103 c.NewIdent("_cxgo_getStdout", "stdio.Stdout", stdio.Stdout, c.FuncTT(filePtr)), 104 c.NewIdent("_cxgo_getStderr", "stdio.Stderr", stdio.Stderr, c.FuncTT(filePtr)), 105 c.NewIdent("_cxgo_getStdin", "stdio.Stdin", stdio.Stdin, c.FuncTT(filePtr)), 106 c.NewIdent("_cxgo_fileByFD", "stdio.ByFD", stdio.ByFD, c.FuncTT(filePtr, c.Go().Uintptr())), 107 c.NewIdent("fopen", "stdio.FOpen", stdio.FOpen, c.FuncTT(filePtr, gstrT, gstrT)), 108 c.NewIdent("fdopen", "stdio.FDOpen", stdio.FDOpen, c.FuncTT(filePtr, c.Go().Uintptr(), gstrT)), 109 // note: printf itself is considered a builtin 110 c.NewIdent("vprintf", "stdio.Vprintf", stdio.Vprintf, c.FuncTT(gintT, gstrT, valistT)), 111 c.NewIdent("sprintf", "stdio.Sprintf", stdio.Sprintf, c.VarFuncTT(gintT, cstrT, gstrT)), 112 c.NewIdent("vsprintf", "stdio.Vsprintf", stdio.Vsprintf, c.FuncTT(gintT, cstrT, gstrT, valistT)), 113 c.NewIdent("snprintf", "stdio.Snprintf", stdio.Snprintf, c.VarFuncTT(gintT, cstrT, gintT, gstrT)), 114 c.NewIdent("vsnprintf", "stdio.Vsnprintf", stdio.Vsnprintf, c.FuncTT(gintT, cstrT, gintT, gstrT, valistT)), 115 c.NewIdent("fprintf", "stdio.Fprintf", stdio.Fprintf, c.VarFuncTT(gintT, filePtr, gstrT)), 116 c.NewIdent("vfprintf", "stdio.Vfprintf", stdio.Vfprintf, c.FuncTT(gintT, filePtr, gstrT, valistT)), 117 c.NewIdent("scanf", "stdio.Scanf", stdio.Scanf, c.VarFuncTT(gintT, gstrT)), 118 c.NewIdent("vscanf", "stdio.Vscanf", stdio.Vscanf, c.FuncTT(gintT, gstrT, valistT)), 119 c.NewIdent("sscanf", "stdio.Sscanf", stdio.Sscanf, c.VarFuncTT(gintT, cstrT, gstrT)), 120 c.NewIdent("vsscanf", "stdio.Vsscanf", stdio.Vsscanf, c.FuncTT(gintT, cstrT, gstrT, valistT)), 121 c.NewIdent("fscanf", "stdio.Fscanf", stdio.Fscanf, c.VarFuncTT(gintT, filePtr, gstrT)), 122 c.NewIdent("vfscanf", "stdio.Vfscanf", stdio.Vfscanf, c.FuncTT(gintT, filePtr, gstrT, valistT)), 123 c.NewIdent("remove", "stdio.Remove", stdio.Remove, c.VarFuncTT(gintT, gstrT)), 124 c.NewIdent("rename", "stdio.Rename", stdio.Rename, c.VarFuncTT(gintT, gstrT, gstrT)), 125 ) 126 127 l.Header += ` 128 #define stdout _cxgo_getStdout() 129 #define stderr _cxgo_getStderr() 130 #define stdin _cxgo_getStdin() 131 132 #define fclose(f) ((FILE*)f)->Close() 133 #define feof(f) ((FILE*)f)->IsEOF() 134 #define fflush(f) ((FILE*)f)->Flush() 135 #define fgetc(f) ((FILE*)f)->GetC() 136 #define fgets(buf, sz, f) ((FILE*)f)->GetS(buf, sz) 137 #define fileno(f) ((FILE*)f)->FileNo() 138 #define fputc(v, f) ((FILE*)f)->PutC(v) 139 #define fputs(v, f) ((FILE*)f)->PutS(v) 140 #define fread(p, sz, cnt, f) ((FILE*)f)->ReadN(p, sz, cnt) 141 #define fwrite(p, sz, cnt, f) ((FILE*)f)->WriteN(p, sz, cnt) 142 #define fseek(f, a1, a2) ((FILE*)f)->Seek(a1, a2) 143 #define ftell(f) ((FILE*)f)->Tell() 144 #define getc(f) ((FILE*)f)->GetC() 145 #define ungetc(c, f) ((FILE*)f)->UnGetC(c) 146 147 void clearerr(FILE *); 148 char *ctermid(char *); 149 _cxgo_go_int dprintf(_cxgo_go_int, const char *restrict, ...); 150 int ferror(FILE *); 151 int fgetpos(FILE *restrict, fpos_t *restrict); 152 void flockfile(FILE *); 153 FILE *fmemopen(void *restrict, size_t, const char *restrict); 154 FILE *freopen(const char *restrict, const char *restrict, FILE *restrict); 155 int fseeko(FILE *, off_t, int); 156 int fsetpos(FILE *, const fpos_t *); 157 off_t ftello(FILE *); 158 int ftrylockfile(FILE *); 159 void funlockfile(FILE *); 160 int getchar(void); 161 int getc_unlocked(FILE *); 162 int getchar_unlocked(void); 163 ssize_t getdelim(char **restrict, size_t *restrict, int, FILE *restrict); 164 ssize_t getline(char **restrict, size_t *restrict, FILE *restrict); 165 char *gets(char *); 166 FILE *open_memstream(char **, size_t *); 167 int pclose(FILE *); 168 void perror(const char *); 169 FILE *popen(const char *, const char *); 170 int putc(int, FILE *); 171 int putchar(int); 172 int putc_unlocked(int, FILE *); 173 int putchar_unlocked(int); 174 int puts(const char *); 175 int renameat(int, const char *, int, const char *); 176 void rewind(FILE *); 177 void setbuf(FILE *restrict, char *restrict); 178 int setvbuf(FILE *restrict, char *restrict, int, size_t); 179 char *tempnam(const char *, const char *); 180 FILE *tmpfile(void); 181 char *tmpnam(char *); 182 int vdprintf(int, const char *restrict, va_list); 183 184 #undef int 185 ` 186 return l 187 }) 188 }