3 Commits 1bbdadc04f ... e03c93bc99

Author SHA1 Message Date
  Joshua Drake e03c93bc99 Fix to allow arbitrary ints passed to py_interrupt_event 9 years ago
  Joshua Drake 3716661e5e Modified MODULES2 9 years ago
  Joshua Drake da81f8505c cat and cd implemented 9 years ago
3 changed files with 120 additions and 32 deletions
  1. 1 1
      src/Makefile
  2. 107 21
      src/modules/threads/threads.c
  3. 12 10
      src/src/pyevent.c

+ 1 - 1
src/Makefile

@@ -24,7 +24,7 @@ PROJECT := $(shell echo $(BINARY) | tr A-Z a-z)
 MODULE_DIR := modules
 
 # Directories of modules using the 2.0 system
-MODULES2 := dice party stats entropy shf
+MODULES2 := dice party stats threads shf
 
 # Path to python config utility
 PYTHON_CONFIG := python2.7-config

+ 107 - 21
src/modules/threads/threads.c

@@ -447,10 +447,39 @@ bool newTask
 }
 
 #ifdef DEBUG
+void dumpOut(char* header, char* arr[], uint8_t len, uint8_t max) {
+  uint8_t i=1;
+  uint8_t i2=1;
+  uint8_t hlen=strlen(header);
+  char buf[64];
+
+  sprintf(buf, "%s: %%-%ds ", header, max);
+  printf(buf, arr[0]);
+  
+  while (arr[i2]) {
+    if (i == 4) {
+      i=0;
+      putc('\n', stdout);
+      sprintf(buf, "%%-%ds", hlen+2);
+      printf(buf, "");
+    }
+    
+    sprintf(buf, "%%-%ds ", max);
+    printf(buf, arr[i2]);
+    i++;
+    i2++;
+  }
+  
+  putc('\n', stdout);
+  fflush(stdout);
+}
+
+//Move into SleeperBox, remove heap and destructor from task struct
 int _ls(char* args) {
   uint8_t len;
-  uint8_t i=0; uint8_t i2=0;
-  char* res[128]; char buf[64]; char cwd[1024];
+  uint8_t i=0; uint8_t i2=0; uint8_t fi=0; uint8_t di=0; uint8_t oi=0;
+  char* files[64]; char* dirs[64]; char* others[64];
+  char buf[64]; char cwd[1024];
   if (!args || !strlen(args)) {
     if (!getcwd(cwd, 1024)) {
       log_string("ls: couldn't get current working directory: %s", strerror(errno));
@@ -472,33 +501,71 @@ int _ls(char* args) {
   struct dirent *e;
 
   while ((e = readdir(d))) {
-    if (i2 > 126) { break; }
+    if (fi > 62 || di > 62 || oi > 62) {
+      log_string("ls: output truncated");
+      break;
+    }
     if (e->d_name[0] != '.') {
-      res[i2] = e->d_name; 
       len = strlen(e->d_name);
       if (len > max) { max = len; }
-      i2++;
+      switch (e->d_type) {
+      case DT_REG:
+        files[fi]  = e->d_name;
+        fi++;
+        break;
+      case DT_DIR:
+        dirs[di]   = e->d_name;
+        di++;
+        break;
+      default:
+        others[oi] = e->d_name;
+        oi++;
+      }
     }
   }
-  res[(i2++)] = NULL;
-  i2 = 0;
+  files[(fi++)] = NULL; dirs[(di++)] = NULL; others[(oi++)] = NULL;
 
-  while (res[i2]) {
-    if (i == 4) {
-      i=0;
-      putc('\n', stdout);
-    }
+  if (fi > 1) { dumpOut("Files:       ", files,  fi, max); }
+  if (di > 1) { dumpOut("Directories: ", dirs,   di, max); }
+  if (oi > 1) { dumpOut("Other:       ", others, oi, max); }
+  
+  closedir(d);
+  return 0;
+}
 
-    sprintf(buf, "%%-%ds ", max);
-    printf(buf, res[i2]);
-    i++;
-    i2++;
+int _cat(char* args) {
+  int8_t r;
+  uint8_t buf[4096];
+  uint8_t fd = open(args, O_RDONLY);
+  if (fd < 0) {
+    log_string("cat: couldn't open file '%s': %s", args, strerror(errno));
+    return 0;
   }
 
-  putc('\n', stdout);
+  while (1) {
+    r = read(fd, buf, 4096);
+    if (r == 0)  { break; }
+    if (r == -1) {
+      log_string("cat: couldn't read: %s", strerror(errno));
+      break;
+    }
+    
+    if (write(1, buf, r) != r) {
+      log_string("cat: couldn't write: %s", strerror(errno));
+    }
+  }
   fflush(stdout);
-  
-  closedir(d);
+  if (close(fd) < 0) {
+    log_string("cat: couldn't close FD %d: %s", fd, strerror(errno));
+    return 1;
+  }
+  return 0;
+}
+
+int _cd(char* args) {
+  if (chdir(args) < 0) {
+    log_string("cd: couldn't change directory to '%s'", args);
+  }
   return 0;
 }
 
@@ -515,16 +582,33 @@ int process_command(char* buf, size_t len) {
   }
   else { memcpy(cmd, buf, len); }
 
-  if (!strncmp(cmd, "ls", len)) { return _ls(args); }
-  
+  if      (!strncmp(cmd, "ls",  len)) { return _ls(args);  }
+  else if (!strncmp(cmd, "cd",  len)) { return _cd(args); }
+  else if (!strncmp(cmd, "cat", len)) { return _cat(args); }
   return 0;
 }
 
 //Return 0 for error, otherwise length of command
 size_t get_command(char* buf, size_t len) {
+  char hostname[HOST_NAME_MAX+1];
+  char cwd[64];
   uint8_t i;
   uint8_t r;
   char* err;
+
+  if (getcwd(cwd, 64) < 0) {
+    log_string("command_loop: couldn't get current working directory: %s", strerror(errno));
+    return 1;
+  }
+  
+  if (gethostname(hostname, HOST_NAME_MAX+1) < 0) {
+    log_string("command_loop: couldn't get hostname: %s", strerror(errno));
+    return 1;
+  }
+  
+  //possibly move setids to after chroot to have own user file
+  printf("%s:%s$ ", hostname, cwd);
+  fflush(stdout);
   
   for (i=0; i<len; i++) {
     if ((r = read(0, &(buf[i]), 1) < 1)) {
@@ -542,6 +626,7 @@ size_t get_command(char* buf, size_t len) {
 }
 
 int command_loop() {
+
   char buf[32]; 
   size_t len;
 
@@ -608,6 +693,7 @@ int  main() {
     ALLOW_SYSCALL(getdents),     ALLOW_SYSCALL(open),
     ALLOW_SYSCALL(close),        ALLOW_SYSCALL(openat),
     ALLOW_SYSCALL(getcwd),       ALLOW_SYSCALL(chdir),
+    ALLOW_SYSCALL(uname),
     /*ALLOW_SYSCALL(fsync),
 #ifdef __amd64__
     ALLOW_SYSCALL(pwrite64),

+ 12 - 10
src/src/pyevent.c

@@ -90,18 +90,22 @@ PyObject *PyEvent_start(PyObject *self, PyObject *args, void *func) {
     owner = NULL;
     sprintf(otype, "none");
   }
-  else if(PyChar_Check(PyOwner)) {
+  else if (PyChar_Check(PyOwner)) {
     owner = PyChar_AsChar(PyOwner);
     sprintf(otype, "char");
   }
-  else if(PyRoom_Check(PyOwner)) {
+  else if (PyRoom_Check(PyOwner)) {
     owner = PyRoom_AsRoom(PyOwner);
     sprintf(otype, "room");
   }
-  else if(PyObj_Check(PyOwner)) {
+  else if (PyObj_Check(PyOwner)) {
     owner = PyObj_AsObj(PyOwner);
     sprintf(otype, "obj");
   }
+  else if (PyInt_Check(PyOwner)) {
+    owner = (void*)PyOwner;
+    sprintf(otype, "int");
+  }
   // invalid type
   else {
     PyErr_Format(PyExc_TypeError,
@@ -154,13 +158,11 @@ PyObject *PyEvent_interrupt_event(PyObject *self, PyObject *args) {
   // check the possibilities
   if(PyOwner == Py_None)
     interrupt_events_involving(NULL);
-  else if(PyChar_Check(PyOwner))
-    interrupt_events_involving(PyChar_AsChar(PyOwner));
-  else if(PyRoom_Check(PyOwner))
-    interrupt_events_involving(PyRoom_AsRoom(PyOwner));
-  else if(PyObj_Check(PyOwner))
-    interrupt_events_involving(PyObj_AsObj(PyOwner));
-
+  else if (PyChar_Check(PyOwner)) { interrupt_events_involving(PyChar_AsChar(PyOwner)); }
+  else if (PyRoom_Check(PyOwner)) { interrupt_events_involving(PyRoom_AsRoom(PyOwner)); }
+  else if (PyObj_Check(PyOwner))  { interrupt_events_involving(PyObj_AsObj(PyOwner));   }
+  else if (PyInt_Check(PyOwner))  { interrupt_events_involving(PyOwner);                }
+    
   // everything ok
   return Py_BuildValue("i", 1);
 }