mirror of
https://github.com/XaverKlemenschits/jupyter-c-kernel.git
synced 2025-04-13 01:46:10 +00:00
Static server (#2)
* Wrap input and file output functions for a read only static server. * Added -y to docker apt-get in docker file. * wError/wAll as options in Kernel, defaults only to wAll.
This commit is contained in:
parent
8b69d8354d
commit
bddb7ca6b0
@ -3,6 +3,10 @@ MAINTAINER Xaver Klemenschits <klemenschits@iue.tuwien.ac.at>
|
||||
|
||||
USER root
|
||||
|
||||
# Install vim and ssh
|
||||
RUN apt-get update
|
||||
RUN apt-get install -y vim openssh-client
|
||||
|
||||
WORKDIR /tmp
|
||||
|
||||
COPY ./ jupyter_c_kernel/
|
||||
|
@ -102,6 +102,9 @@ class CKernel(Kernel):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(CKernel, self).__init__(*args, **kwargs)
|
||||
self._allow_stdin = True
|
||||
self.readOnlyFileSystem = False
|
||||
self.wAll = True # show all warnings by default
|
||||
self.wError = False # but keep comipiling for warnings
|
||||
self.files = []
|
||||
mastertemp = tempfile.mkstemp(suffix='.out')
|
||||
os.close(mastertemp[0])
|
||||
@ -145,6 +148,12 @@ class CKernel(Kernel):
|
||||
|
||||
def compile_with_gcc(self, source_filename, binary_filename, cflags=None, ldflags=None):
|
||||
cflags = ['-std=c11', '-fPIC', '-shared', '-rdynamic'] + cflags
|
||||
if self.wError:
|
||||
cflags = cflags + ['-Werror']
|
||||
if self.wAll:
|
||||
cflags = cflags + ['-Wall']
|
||||
if self.readOnlyFileSystem:
|
||||
cflags = ['-DREAD_ONLY_FILE_SYSTEM'] + cflags
|
||||
args = ['gcc', source_filename] + cflags + ['-o', binary_filename] + ldflags
|
||||
return self.create_jupyter_subprocess(args)
|
||||
|
||||
@ -178,8 +187,13 @@ class CKernel(Kernel):
|
||||
# check whether int main() is specified, if not add it around the code
|
||||
# also add common magics like -lm
|
||||
def _add_main(self, magics, code):
|
||||
x = re.search("int\s+main\s*\(", code)
|
||||
if x is None:
|
||||
# remove comments
|
||||
tmpCode = re.sub("//.*", "", code)
|
||||
tmpCode = re.sub("/\*.*?\*/", "", tmpCode, flags=re.M|re.S)
|
||||
|
||||
x = re.search("int\s+main\s*\(", tmpCode)
|
||||
|
||||
if not x:
|
||||
code = self.main_head + code + self.main_foot
|
||||
magics['cflags'] += ['-lm']
|
||||
|
||||
|
@ -1,10 +1,63 @@
|
||||
#ifndef STDIO_WRAP_H
|
||||
#define STDIO_WRAP_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
/* Define the functions to overload the old ones */
|
||||
int scanf_wrap(const char *format, ...) {
|
||||
printf("<inputRequest>");
|
||||
fflush(stdout);
|
||||
va_list arglist;
|
||||
va_start( arglist, format );
|
||||
int result = vscanf( format, arglist );
|
||||
va_end( arglist );
|
||||
return result;
|
||||
}
|
||||
|
||||
int getchar_wrap(){
|
||||
printf("<inputRequest>");
|
||||
fflush(stdout);
|
||||
return getchar();
|
||||
}
|
||||
|
||||
/* Replace all the necessary input functions
|
||||
Need double hashes in case there are no __VA_ARGS__*/
|
||||
#define scanf(format, ...) scanf_wrap(format, ##__VA_ARGS__)
|
||||
|
||||
#define getchar() getchar_wrap()
|
||||
|
||||
/* Replace FILE write operations for read-only systems */
|
||||
#ifdef READ_ONLY_FILE_SYSTEM
|
||||
|
||||
/* Define wrapping functions */
|
||||
/* Output that the fopen succeeded and return some valid pointer */
|
||||
FILE *fopen_wrap(const char *filename, const char *modes) {
|
||||
static long stream = 0x1FFFF0000;
|
||||
#ifdef SHOW_FILE_IO_VERBOSE
|
||||
printf("\x01b[42m");
|
||||
printf("\"%s\" opened in mode \"%s\"\n", filename, modes);
|
||||
printf("\x01b[0m");
|
||||
#endif /* SHOW_FILE_IO_VERBOSE */
|
||||
return (FILE*)stream++;
|
||||
}
|
||||
|
||||
int fprintf_wrap(FILE* stream, const char* format, ...) {
|
||||
printf("\x01b[42m");
|
||||
printf("%p:", stream);
|
||||
printf("\x01b[0m");
|
||||
va_list arglist;
|
||||
va_start( arglist, format );
|
||||
int result = vprintf(format, arglist);
|
||||
va_end( arglist );
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Replace all the necessary input functions */
|
||||
#define scanf(...) printf("<inputRequest>");\
|
||||
fflush(stdout);\
|
||||
scanf(__VA_ARGS__);
|
||||
#define fopen(file, mode) fopen_wrap(file, mode)
|
||||
|
||||
#define getchar() printf("<inputRequest>");\
|
||||
fflush(stdout);\
|
||||
getchar();
|
||||
#define fprintf(stream, format, ...) fprintf_wrap(stream, format, ##__VA_ARGS__)
|
||||
|
||||
#endif /* READ_ONLY_FILE_SYSTEM */
|
||||
|
||||
#endif /* STDIO_WRAP_H */
|
||||
|
Loading…
x
Reference in New Issue
Block a user