Esempio n. 1
0
File: C.c Progetto: mattn/C-win32
void setup_dir(void)
{
#ifdef _WIN32
  char* dirname;
  int old_umask = -1;
  
  char tmppath[MAX_PATH];
  if (GetTempPath(sizeof(tmppath), tmppath) == 0) {
    cmd_error("failed to stat tmpdir\n");
  }
  tmppath[strlen(tmppath)-1] = 0;
  root_dir = str_dup(tmppath);
#else
  char* dirname, buf[sizeof("/LARGE_C-01234567")];
  uid_t euid = geteuid();
  int old_umask = -1;
  
  /* use TMPDIR, or if not set, create a directory of my own under P_tmpdir */
  if ((root_dir = getenv("TMPDIR")) != NULL) {
    root_dir = str_dup(root_dir);
  } else {
    old_umask = umask(077);
    sprintf(buf, "/LARGE_C-%u", (unsigned)euid);
    root_dir = str_concat(str_dup(P_tmpdir), buf);
    if (mkdir(root_dir, 0700) != 0) {
      struct stat st;
      if (lstat(root_dir, &st) != 0) {
	cmd_error("failed to stat: %s : %s\n", root_dir, strerror(errno));
      }
      if (st.st_uid != euid) {
	cmd_error("%s owned by somebody else\n", root_dir);
      }
    }
  }
#endif

  /* make subdirs */
  dirname = str_concat(str_dup(root_dir), "/cache");
  mkdir(dirname, 0777);
  free(dirname);
  dirname = str_concat(str_dup(root_dir), "/tmp");
  mkdir(dirname, 0777);
  free(dirname);
  
  /* restore */
  if (old_umask != -1) {
    umask(old_umask);
  }
}
Esempio n. 2
0
File: C.c Progetto: mattn/C-win32
static int call_proc(char** argv, char* errmsg)
{
#ifdef _WIN32
  int status, ret;
  ret = spawn_w32(argv, &status);
  if (status) {
    LPVOID buf = "";
    FormatMessage(
      FORMAT_MESSAGE_ALLOCATE_BUFFER |
      FORMAT_MESSAGE_FROM_SYSTEM,
      NULL,
      GetLastError(),
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
      (LPTSTR) &buf,
      0,
      NULL);
    cmd_error("%s: %s : %s\n", errmsg, argv[0], buf);
    LocalFree(buf);
  }
  return status;
#else
  pid_t pid;
  int status;

  /* fork */
  pid = fork();
  switch (pid) {
  case 0: /* child process */
    execvp(argv[0], argv);
    cmd_error("%s: %s : %s\n", errmsg, argv[0], strerror(errno));
    break;
  case -1: /* fork failed */
    cmd_error("%s: %s : %s\n", errmsg, argv[0], strerror(errno));
    break;
  }
  
  /* only the parent process reaches here, and only if fork succeeds */
 WAIT:
  if (waitpid(pid, &status, 0) < 0) {
    if (errno == EINTR) goto WAIT;
    cmd_error("unexpected response from waitpid : %s\n", strerror(errno));
  }
  if (! WIFEXITED(status)) {
    cmd_error(NULL); /* silently exit(255) */
  }
  return WEXITSTATUS(status);
#endif
}
Esempio n. 3
0
static bool proceed_response(int srv, struct message *response)
{
  switch(response->command) {
  case(CMD_QUIT):
  case(CMD_END):
    return false;
  case(CMD_PUSH):
  case(CMD_POP):
  case(CMD_POPF):
  case(CMD_CLEAN):
  case(CMD_GET):
  case(CMD_GETF):
  case(CMD_GETALL):
  case(CMD_SIZE):
    warnx("received invalid command %d from server", response->command);
    send_error(srv, E_INVAL);
    return true;
  case(CMD_RESPS):
    return cmd_resps(srv, response);
  case(CMD_RESPI):
    return cmd_respi(srv, response);
  case(CMD_ERROR):
    return cmd_error(srv, response);
  default:
    assert(false); /* unknown command */
  }

  return false;
}
Esempio n. 4
0
File: C.c Progetto: mattn/C-win32
static char* get_line(FILE* fp)
{
  static char* buf = NULL;
  static int len = 0;
  
  int i = 0, ch;
  
  while ((ch = fgetc(fp)) != EOF) {
    if (i == len) {
      len += 4096;
      if ((buf = realloc(buf, len)) == NULL) {
	cmd_error("out of memory\n");
      }
    }
    buf[i++] = ch;
    if (ch == '\n') {
      break;
    }
  }
  if (i == 0) {
    return NULL;
  }
  buf[i] = '\0';
  
  return buf;
}
Esempio n. 5
0
File: C.c Progetto: mattn/C-win32
static char* str_concat(char* base, char* add)
{
  if ((base = realloc(base, strlen(base) + strlen(add) + 1)) == NULL) {
    cmd_error("out of memory\n");
  }
  strcat(base, add);
  return base;
}
Esempio n. 6
0
File: C.c Progetto: mattn/C-win32
static char* str_dup(char* str)
{
  char* p;
  if ((p = malloc(strlen(str) + 1)) == NULL) {
    cmd_error("out of memory\n");
  }
  strcpy(p, str);
  return p;
}
Esempio n. 7
0
static int
process_command(int serial, int cid, char *cmd)
{
  /*char *opt, *opt2; */
  char *p, *opt;
  int ret;

  if ((p = strchr(cmd, '\n')))
	*p = '\0';
  else
	return -1;

  if ((opt = strchr(cmd, ' '))) {
	*opt = '\0';
	opt ++;
  } else {
	opt = NULL;
  }

  debug_printf(DEBUG_NOTE, "cmd: %s\n", cmd);

  if (strcmp(cmd, "RELEASE") == 0)
	ret = cmd_release(cid);
  else if (strcmp(cmd, "UNFOCUSED") == 0)
	ret = cmd_unfocused(cid);
  else if (strcmp(cmd, "FOCUSED") == 0)
	ret = cmd_focused(cid);
  else if (strcmp(cmd, "HIDE") == 0)
	ret = cmd_hide(cid);
  else if (strcmp(cmd, "SHOW") == 0)
	ret = cmd_show(cid);
  else if (strcmp(cmd, "NEW") == 0)
  	ret = cmd_new(cid, opt);
  else if (strcmp(cmd, "RESET") == 0)
	ret = cmd_reset(cid);
  else if (strcmp(cmd, "CHANGE") == 0)
  	ret = cmd_change(cid, opt);
  else if (strcmp(cmd, "PROP") == 0)
  	ret = cmd_prop(cid, opt);
  else if (strcmp(cmd, "LABEL") == 0)
  	ret = cmd_label(cid);
  else if (strcmp(cmd, "HELPER") == 0)
	ret = cmd_helper(cid, opt);
  else if (strcmp(cmd, "NOP") == 0)
  	ret = cmd_nop(cid);
  else if (strcmp(cmd, "LIST") == 0)
  	ret = cmd_list();
  else if (strcmp(cmd, "SETENC") == 0)
  	ret = cmd_setenc(opt);
  else if (strcmp(cmd, "GETENC") == 0)
  	ret = cmd_getenc(opt); /* for debug */
  else
	ret = cmd_error();

  return ret;
}
Esempio n. 8
0
File: C.c Progetto: mattn/C-win32
static void save_specs(void)
{
  char* filename;
  FILE* fp;
  
  filename = str_concat(str_dup(temp_dir), "/SPECS");
  if ((fp = fopen(filename, "wb")) == NULL) {
    cmd_error("failed to write file: %s : %s\n", filename, strerror(errno));
  }
  fwrite(spec, 1, spec_size, fp);
  fclose(fp);
  free(filename);
}
Esempio n. 9
0
bool run_command(command_t c)
{
  if (c == NULL)
  {
    return false;
  }
  // Check IO
  // Set contents of file to be an argument
  if (c->input != NULL)
  {
    size_t i = 0;
    size_t in_size = strlen(c->input);
    // Find end of command list
    while (c->u.word[i] != NULL)
    {
      i++; 
    }
    c->u.word[i] = (char*)malloc(in_size *sizeof(char));
    c->u.word[i] = c->input;
    c->u.word[i+1] = (char*)malloc(sizeof(char));
    c->u.word[i+1] = NULL;
  } 

  // Print stdout to file
  if (c->output != NULL)
  {
    freopen(c->output, "w", stdout);
  }

  // Testing Code
  printf("\n\nCommand: %s \nWith Arguments: %s \n", c->u.word[0], c->u.word[1] );
  
  if (execvp(c->u.word[0], c->u.word) == -1)
  {
    c->status = 1;
    cmd_error(c->u.word[0]);
  }

  // Close output file if set
  if (c->output != NULL)
  {
    fclose(stdout);
  }
  return true;
}
Esempio n. 10
0
File: C.c Progetto: mattn/C-win32
static void make_temp_dir(void)
{
#define MAX_TRIES 1000
  
  int rep;
  
  srand((int)time(NULL) ^ (int)getpid());
  
  for (rep = 0; rep < MAX_TRIES; rep++) {
    char randbuf[sizeof("/tmp/01234567")];
    sprintf(randbuf, "/tmp/%08x", rand());
    temp_dir = str_concat(str_dup(root_dir), randbuf);
    if (mkdir(temp_dir, 0777) == 0) {
      return;
    }
    free(temp_dir);
  }
  cmd_error("failed to create temporary directory.\n");
  
#undef MAX_TRIES
}
Esempio n. 11
0
// eventcat_id;
ExecWindow::ExecWindow(watchCond *wc, int pid, QString cmd)
{
    setupUi(this);
    setWindowTitle( tr( "Qps Watchdog" ) );

    wcond = wc;

    QString str;

    if (wc->cond == WATCH_PROCESS_START)
    {
        textEdit->append(cmd + "(" + QString::number(pid) + ")" + " start");
    }
    if (wc->cond == WATCH_PROCESS_FINISH)
        textEdit->append(cmd + "(" + QString::number(pid) + ")" + " finished");

    flag_started = false;

    pr = new QProcess;          // leak?
    if (!wc->command.isEmpty()) // conflict pid's command
    {
        pr->start(wc->command); // thread run, if null then segfault occurs. ?
    }

    connect(okButton, SIGNAL(clicked()), this, SLOT(cmd_ok()));

    connect(pr, SIGNAL(started()), this, SLOT(cmd_started()));
    connect(pr, SIGNAL(finished(int, QProcess::ExitStatus)), this,
            SLOT(cmd_finished(int, QProcess::ExitStatus)));
    connect(pr, SIGNAL(error(QProcess::ProcessError)), this,
            SLOT(cmd_error(QProcess::ProcessError)));

    show();

    execlist.append(this);
}
Esempio n. 12
0
int main (int argc, char *argv[]) {
    spawn_term((argc >= 2) ? argv[1] : NULL);

    char cmd[51];               // stores the command that was entered
    char* token[MAX_TOKENS];    // points to parts of the command
    unsigned i;
    int pwm, motor_num, target_setting;
    
    for (;;) {
        printf (LINE_START);
        cmd_ok = 0;
        assert(fgets (cmd, 50, stdin));
        
        // use strtok to grab each part of the command 
        token[0] = strtok (cmd, " \n");
        for (i = 1; i < MAX_TOKENS; i++)
            token[i] = strtok (NULL, " \n");
        
        if (token[0] == NULL) 
            help ();
        else if (!strcmp(token[0], "q") || !strcmp(token[0], "exit")) 
            exit_safe();
        else if (!strcmp(token[0], ":q"))
            printf ("this isnt vim lol\n");
        else if (!strcmp(token[0], "help")) {
            if (token[1] == NULL) 
                help ();
            else if (!strcmp(token[1],"motor") || !strcmp(token[1],"m"))
                help_motor();
            else if (!strcmp(token[1],"dyn") || !strcmp(token[1],"d"))
                help_dyn();
            else if (!strcmp(token[1],"power") || !strcmp(token[1],"p"))
                help_power();
        }
        else if (!strcmp(token[0], "motor") || !strcmp(token[0], "m")) {
            if (token[1] == NULL || !strcmp (token[1], "status"))
                motor_status();
            else if (!strcmp (token[1], "all")) {
                pwm = atoi_safe(token[2]); // note an invalid token will cause pwm = 0, which is ok
                motor_set (pwm, H_ALL);                    
            }
            else if (!strcmp (token[1], "fwd")) {
                pwm = atoi_safe(token[2]); // note an invalid token will cause pwm = 0, which is ok
                motor_set (pwm, H_FWD);                    
            }
            else if (!strcmp (token[1], "rise")) {
                pwm = atoi_safe(token[2]); // note an invalid token will cause pwm = 0, which is ok
                motor_set (pwm, H_RISE);                    
            }
            else {
                motor_num = atoi_safe (token[1]); // 1-indexed
		motor_num--; // Internally, this is 0-indexed
                pwm = atoi_safe (token[2]);
                switch (motor_num) {
                    case M_FRONT_LEFT:  motor_set (pwm, H_FRONT_LEFT); break;
                    case M_FRONT_RIGHT: motor_set (pwm, H_FRONT_RIGHT); break;
                    case M_FWD_LEFT:    motor_set (pwm, H_FWD_LEFT); break;
                    case M_FWD_RIGHT:   motor_set (pwm, H_FWD_RIGHT); break;
                    case M_REAR:        motor_set (pwm, H_REAR); break;
                    default: printf ("**** Invalid motor number.\n");
                }
            }
        }
        else if (!strcmp(token[0], "power") || !strcmp(token[0], "p")) {
            if (token[1] == NULL || !strcmp (token[1], "status"))
                power_status();
            else if (!strcmp (token[1], "on") || !strcmp (token[1], "1")) {
                power_on();
            }
            else if (!strcmp (token[1], "start") || !strcmp (token[1], "2")) {
                startup_sequence();
            }
            else if (!strcmp (token[1], "off") || !strcmp (token[1], "0")) {
                power_off();
            }
        }
        else if (!strcmp(token[0], "dyn") || !strcmp(token[0], "d")) {
            if (token[1] == NULL)
                dyn_status();
            else if (!strcmp (token[1], "depth")) {
                target_setting = atoi_safe(token[2]); // Note an invalid token will cause target_setting = 0, which is ok
                dyn_set_target_depth(target_setting);
            }
        }
        
        if (cmd_ok != 1) 
            cmd_error();
    }
}
Esempio n. 13
0
File: C.c Progetto: mattn/C-win32
int main(int argc, char** argv)
{
  int ret;
  
  setup_dir();
  
  /* init globals */
  gcc = sa_concat(gcc, "gcc"); /* replaced laterwards if c++ */
  gcc = sa_concat(gcc, "-I.");
  
  src_lines =
    sa_concat(src_lines,
	      "#define __LARGE_C__ " VERSION_INT_STR "\n"
	      "#ifdef __cplusplus\n"
	      "extern \"C\" {\n"
	      "#endif\n"
	      "#include <stdio.h>\n"
	      "#include <stdlib.h>\n"
	      "#ifdef __cplusplus\n"
	      "}\n"
	      "#include <iostream>\n"
	      "using namespace std;\n"
	      "#endif\n"
	      "\n"
	      "__LARGE_C_PREFIX__\n");
  
  argv++;
  { /* parse args, determine cache dir */
    char** new_argv = parse_args(argv, NULL, 0);
    for (; argv != new_argv; argv++) {
      add_spec(*argv, strlen(*argv) + 1);
    }
    if (! keep_files && (oneliner || *argv != NULL)) {
      struct stat st;
      if (oneliner) {
	build_store_dir();
      } else if (stat(*argv, &st) == 0) {
	add_spec(*argv, strlen(*argv) + 1);
	add_spec(&st.st_size, sizeof(st.st_size));
	add_spec(&st.st_mtime, sizeof(st.st_mtime));
	build_store_dir();
      }
    }
  }
  
  /* use cache if possible */
  if (store_dir != NULL && check_specs()) {
    char** child_argv = NULL;
#ifdef _WIN32
    _utime(store_dir, NULL); /* update mtime of the directory */
#else
    utimes(store_dir, NULL); /* update mtime of the directory */
#endif
    exec_file = str_concat(str_dup(store_dir), "/"A_OUT);
    child_argv = sa_concat(child_argv, exec_file);
#ifdef _WIN32
    {
      int status;
	  ret = spawn_w32(child_argv, &status);
      if (status == 0) exit(ret);
    }
#else
    execv(exec_file, child_argv);
#endif
    // if execv failed, we compile
    free(exec_file);
    remove_dir(store_dir);
  }
  
  /* prepare files */
  make_temp_dir();
  exec_file = str_concat(str_dup(temp_dir), "/"A_OUT);
  c_file = str_concat(str_dup(temp_dir), "/source.c");
  if ((src_fp = fopen(c_file, "wt")) == NULL) {
    cmd_error("failed to create temporary file: %s : %s\n", c_file,
	      strerror(errno));
  }
  while (src_lines != NULL && *src_lines != NULL) {
    fputs(*src_lines++, src_fp);
  }
  
  /* write source with adjustments */
  if (! oneliner) {
    FILE* fp;
    char* file;
    char* line;
    int line_no = 0;
    if (argv[0] == NULL) {
      fp = stdin;
      file = "stdin";
    } else if (strcmp(argv[0], "-") == 0) {
      fp = stdin;
      argv++;
      file = "stdin";
    } else {
      file = *argv++;
      if ((fp = fopen(file, "rt")) == NULL) {
	cmd_error("cannot open file: %s : %s\n", file, strerror(errno));
      }
      fprintf(src_fp, "# 1 \"%s\" 1\n", file);
    }
    while ((line = get_line(fp)) != NULL) {
      int comment_out = 0;
      line_no++;
      if (line_no == 1 && strncmp(line, "#!", 2) == 0) {
	comment_out = 1;
      } else if (line[0] == '#') {
	char* buf = str_dup(line + 1);
	char** tokens = split_tokens(buf);
	if (*tokens != NULL) {
	  if (strcmp(tokens[0], "option") == 0) {
	    parse_args(tokens + 1, file, line_no);
	    comment_out = 1;
	  }
	}
	free(buf);
	free(tokens);
      }
      if (comment_out == 1) {
	fprintf(src_fp, "// ");
      }
      fputs(line, src_fp);
    }
    fputs("\n", src_fp);
    if (fp != stdin) {
      fclose(fp);
    }
  }
  
  /* close source file */
  fputs("__LARGE_C_SUFFIX__\n", src_fp);
  fclose(src_fp);
  src_fp = NULL;
  
  /* compile */
  if (use_plusplus) {
    gcc[0] = "g++";
  }
  if (use_main) {
    gcc = sa_concat(gcc, "-D__LARGE_C_PREFIX__=");
    gcc = sa_concat(gcc, "-D__LARGE_C_SUFFIX__=");
  } else {
    gcc =
      sa_concat(gcc, "-D__LARGE_C_PREFIX__=int main(int argc, char** argv) {");
    gcc = sa_concat(gcc, "-D__LARGE_C_SUFFIX__=; return 0; }");
  }
  gcc = sa_concat(gcc, "-o");
  gcc = sa_concat(gcc, show_disassembly ? "-" : exec_file);
  gcc = sa_concat(gcc, c_file);
  gcc = sa_merge(gcc, lopts);
  if ((ret = call_proc(gcc, "could not execute compiler")) != 0) {
    cleanup();
    exit(ret);
  }
  
  if (show_disassembly) {
    cleanup();
    exit(0);
  }
  
  { /* execute */
    char** child_argv = NULL;
    if (use_debugger) {
      child_argv = sa_concat(child_argv, "gdb");
    }
    child_argv = sa_concat(child_argv, exec_file);
    child_argv = sa_merge(child_argv, argv);
    ret = call_proc(child_argv, "could not spawn child process");
  }
  
  /* move temp_dir to store_dir, if possible.
   * or, remove work_dir
   */
  if (store_dir == NULL) {
    cleanup();
  } else {
    save_specs();
    update_cache();
    if (rename(temp_dir, store_dir) != 0) {
      cleanup();
    }
  }
  
  return ret;
}
Esempio n. 14
0
File: C.c Progetto: mattn/C-win32
static void assert_cmdline(char* option, char* file, int line)
{
  if (file != NULL) {
    cmd_error("%s:%s: %s cannot be used in file\n", file, line, option);
  }
}