示例#1
0
文件: bf.c 项目: talshorer/linux-mod
static int startloop(struct bf_ctx *ctx)
{
	if (ctx->sp == BRAINFUCK_MAX_LOOP_NEST_DEPTH) {
		printf("stack overflow error in cmd %lu\n", lastcmd(ctx));
		return 1;
	}
	ctx->stack[ctx->sp] = lastcmd(ctx);
	if (ctx->data[ctx->dp]) {
		ctx->sp++;
	} else {
		unsigned int count = 1;
		char cmd;

		while (1) {
			if (readone(ctx, &cmd) != sizeof(cmd)) {
				puts("unbalanced loops");
				return 1;
			}
			switch (cmd) {
			case '[':
				count++;
				break;
			case ']':
				if (!--count)
					return 0;
				break;
			}
		}
	}
	return 0;
}
示例#2
0
文件: bf.c 项目: talshorer/linux-mod
static int decptr(struct bf_ctx *ctx)
{
	if (ctx->dp == 0) {
		printf("data underflow error in cmd %lu\n", lastcmd(ctx));
		return 1;
	}
	ctx->dp--;
	return 0;
}
示例#3
0
文件: bf.c 项目: talshorer/linux-mod
static int incptr(struct bf_ctx *ctx)
{
	if (ctx->dp == BRAINFUCK_ARRSIZE - 1) {
		printf("data overflow error in cmd %lu\n", lastcmd(ctx));
		return 1;
	}
	ctx->dp++;
	return 0;
}
示例#4
0
文件: bf.c 项目: talshorer/linux-mod
static int endloop(struct bf_ctx *ctx)
{
	if (ctx->sp == 0) {
		printf("stack underflow error in cmd %lu\n", lastcmd(ctx));
		return 1;
	}
	ctx->sp--;
	if (ctx->data[ctx->dp])
		lseek(ctx->fd, ctx->stack[ctx->sp], SEEK_SET);
	return 0;
}
示例#5
0
文件: wx.cpp 项目: donzelot/openBCM
void wx::mbwxsave (char *taskname)
//*************************************************************************
// this task reads wx-data from serial line, saves it in intervals
// of 5min and puts current + recent (-1h) wx-data into global
// variable.
//*************************************************************************
{
  char name[20];
  strcpy(name, "wx:mbwxsave");
  time_t nextsavet = ad_time();
  int fd;
  char rawdata[80];
  long total_num = 0; // total number of measurements
  long comerr = 0; // total number of communication errors
  char lastcmd_s[50];
  int orgok;

  lastcmd("init");
  // configure tty device
  fd = configure_tty(m.wxtty);
  if (fd < 0)
    return;
  trace(replog, name, "WX logging started");
  // initialize wx-structure
  memset(&m.wx, 0, sizeof(m.wx));
  m.wx.data_valid = 0;
  // wait for data
  while (! runterfahren)
  {
    rawdata[0]=0;
    do
    {
      wdelay(50);
      while (! read_tty(fd, rawdata+strlen(rawdata), 79-strlen(rawdata)))
      {
        wdelay(50);
        // if no data received for >1min, old data is no longer valid
        if (m.wx.data_valid && ad_time()-m.wx.t > 600)
        {
          m.wx.data_valid = 0;
          lastcmd("no data");
          trace(report, name, "no data received");
        }
      }
      if (m.wxstnname[0] == '_')  //simple hack to get debug output
        trace(report, "wx", "rawdata: \"%s\"", rawdata);
      //correct string starts with !!...
      if (strlen(rawdata) > 2 && rawdata[0] != '!')
      {
        char *a = strstr(rawdata, "!!");
        if (a)
          memmove(rawdata, a, strlen(a)+1);
        else
        {
          rawdata[0]=0;
          comerr++;
        }
      }
    }
    // wait until line is complete (or buffer full)
    while(strlen(rawdata) < 70 && ! strstr(rawdata, "\n") &&
                                  ! strstr(rawdata, "\r"));
    rm_crlf(rawdata);
    //calculate data, and if correct test if data should be saved
    orgok = orgdata(&m.wx, rawdata);
    if (orgok)
      comerr++;
    else
      total_num++;
    sprintf(lastcmd_s, "data %ld/%ld", total_num, comerr);
    lastcmd(lastcmd_s);
    if (orgok && nextsavet <= m.wx.t)
    {
      if (nextsavet < ad_time())
        nextsavet = ad_time(); // if time is changed
      nextsavet += 300; //next time to save data is in 5min
      savedata(rawdata, &m.wx);
    }
  }
  close_tty(fd);
}
示例#6
0
/*
  Run all the commands in a command group and wait on
  them if 'wait' is True.
*/
void runcmdgroup(struct CmdGroup *cmdgroup){
	int p0[2];
	int p1[2];
	int *pipes[] = {p0, p1};
	int *writepipe;
	int *readpipe;

	pid_t pid;
	pid_t *pids = malloc(cmdgroup->length * sizeof(pid_t));

	struct CmdNode *node;
	int count;
	for(count = 0, node = cmdgroup->head; node != NULL; ++count, node = node->next){
		/* Swap read and write pipes every iteration */
		writepipe = pipes[GETWPIPE(count)];
		readpipe  = pipes[GETRPIPE(count)];

		/* First command */
		if(!node->prev){
			/* Followed by more commands */
			if(node->next){
				pid = firstcmd(cmdgroup, writepipe);
			}
			/* Only command */
			else{
				pid = singlecmd(cmdgroup);
			}
		}
		/* Middle command */
		else if(node->next){
			pid = middlecmd(cmdgroup, writepipe, readpipe);
		}
		/* Last command */
		else{
			pid = lastcmd(cmdgroup, readpipe);
		}
		/* ERR: couldn't fork or pipe didn't work */
		if(pid < 0){
			generalerror(NONE, "Could not fork process for '%s'\n", node->cmd);
			free(pids);
			close(readpipe[REND]);
			close(readpipe[WEND]);
			close(writepipe[WEND]);
			close(writepipe[REND]);
		}
		/* In child - run command */
		else if(pid == 0){
			pusharg(node, NULL);
			runcmd(node->cmd, node->fullpath, node->args->elements);
		}
		/* In parent - add child's pid to list */
		else{
			if(node->prev){
				close(readpipe[REND]);
				close(readpipe[WEND]);
			}
			pids[count] = pid;
			if(!cmdgroup->wait){
				printf("[%d] Sent to background\n", pid);
			}
		}
	}
	/* Started all commands successfully; now wait on them */
	if(cmdgroup->wait){
		waitforcmds(pids, count);
	}
	free(pids);
}
示例#7
0
void cfgflex (char * befbuf)
//*************************************************************************
//
// cfgflex
//
//*************************************************************************
{
  int a, i;
  int promptz = 0; // Zaehler der Nodeprompts
  char fn[20]; // Dateiname des Configfiles
  char fo[20]; // Dateiname des Antwortfiles
  char ziel[30]; // Nodecall
  char line[255]; // Noderueckgabe

  strcpy(fn, "cfgflex.bcm");
  strcpy(fo, "cfgflex.out");
  strncpy(ziel, befbuf, 29);
  ziel[29] = 0;
  trace(report, "cfgflex", "connect %s", ziel);
  timeout(1);
  lastcmd("Connect");
  if (makeconnect(m.mycall[1], ziel))
  {
    setsession();
    *b->logincall = 0;
    if (filetime(fn))
    {
      trace(report, "cfgflex", "sending %s", fn);
      FILE *f = s_fopen(fn, "lrt");
      lastcmd("TX cfg_file");
      while ((a = fgetc(f)) != EOF)
      {
        getclear_tnc();
        timeout(1);
        waitfor(e_ticsfull);
        putv(a);
        if (a == LF) promptz++; // zaehlt prompts vorwaerts
      }
      putv(LF);
      putv(LF);
      s_fclose(f);
      trace(report, "cfgflex", "receive %s", fo); // speichert Rueckgaben
      FILE *g = s_fopen(fo, "lwt");
      lastcmd("RX cfg_result");
      if (g) // JJ
      {
        while (promptz > 0)
        {
          getline(line, BUFLEN - 1, 1);
          if (! strncmp(line, "=>", 2))
            promptz--; // zaehlt prompts rueckwaerts
          if (! strncmp(line, "***", 3))
            promptz = 0; // link failure
          fprintf(g, "%s", line);
          fputc(LF, g);
        }
      }
      s_fclose(g);
    }
    lastcmd("Delay");
    for (i = 0; i < 100; i++)
    {
      wdelay(226);
      getclear_tnc();
    }
    mblogout(0);
    mbdisconnect(1); // wait for data to be sent
  }
}