示例#1
0
文件: osutil.c 项目: doniexun/OrangeC
void dumperrs(FILE *file)
{
#ifndef CPREPROCESSOR
    if (cparams.prm_listfile)
    {
        fprintf(listFile,"******** Global Symbols ********\n");
        list_table(globalNameSpace->syms,0);
        fprintf(listFile,"******** Global Tags ********\n");
        list_table(globalNameSpace->tags,0);
    }
        if (diagcount && !total_errors)
            fprintf(file, "%d Diagnostics\n", diagcount);
#endif
    if (total_errors)
        fprintf(file, "%d Errors\n", total_errors);
}
示例#2
0
int main(int argc, char** argv)
{
  AppArgs::Init();

  if (!(  AppArgs::AddOption("about",   '?', AAT_NO_VALUE, false)
      &&  AppArgs::AddOption("help",    'h', AAT_NO_VALUE, false)
      &&  AppArgs::AddOption("list",    'l', AAT_NO_VALUE, false)
      &&  AppArgs::AddOption("remove",  'r', AAT_NO_VALUE, false)
      &&  AppArgs::AddOption("edit",    'e', AAT_NO_VALUE, false)
      &&  AppArgs::AddOption("types",   't', AAT_NO_VALUE, false)
      &&  AppArgs::AddOption("reload",  'd', AAT_NO_VALUE, false)
      &&  AppArgs::AddOption("user",    'u', AAT_MANDATORY_VALUE, false)
      &&  AppArgs::AddOption("config",  'f', AAT_MANDATORY_VALUE, false)
      &&  AppArgs::AddOption("version", 'V', AAT_NO_VALUE, false)))
  {
    fprintf(stderr, "error while initializing application");
    return 1;
  }

  AppArgs::Parse(argc, argv);

  if (AppArgs::ExistsOption("help")) {
    fprintf(stderr, "%s\n", INCRONTAB_HELP);
    return 0;
  }

  if (AppArgs::ExistsOption("about")) {
    fprintf(stderr, "%s\n", INCRONTAB_DESCRIPTION);
    return 0;
  }

  if (AppArgs::ExistsOption("version")) {
    fprintf(stderr, "%s\n", INCRONTAB_VERSION);
    return 0;
  }

  bool oper = AppArgs::ExistsOption("list")
          ||  AppArgs::ExistsOption("remove")
          ||  AppArgs::ExistsOption("edit")
          ||  AppArgs::ExistsOption("types")
          ||  AppArgs::ExistsOption("reload");

  size_t vals = AppArgs::GetValueCount();

  if (!oper && vals == 0) {
    fprintf(stderr, "invalid arguments - specify operation or source file\n");
    return 1;
  }

  if (oper && vals > 0) {
    fprintf(stderr, "invalid arguments - operation and source file cannot be combined\n");
    return 1;
  }

  uid_t uid = getuid();

  std::string user;
  bool chuser = AppArgs::GetOption("user", user);

  if (uid != 0 && chuser) {
    fprintf(stderr, "cannot override user to '%s': insufficient privileges\n", user.c_str());
    return 1;
  }

  struct passwd* ppwd = NULL;

  if (chuser) {
	if ((ppwd = getpwnam(user.c_str())) != NULL) {
      if (    setenv("LOGNAME",   ppwd->pw_name,   1) != 0
		  ||  setenv("USER",      ppwd->pw_name,   1) != 0
		  ||  setenv("USERNAME",  ppwd->pw_name,   1) != 0
		  ||  setenv("HOME",      ppwd->pw_dir,    1) != 0
		  ||  setenv("SHELL",     ppwd->pw_shell,  1) != 0)
      {
		perror("cannot set environment variables");
		return 1;
	  }
	} else {
	  fprintf(stderr, "user '%s' not found\n", user.c_str());
	  return 1;
	}
  } else {
    ppwd = getpwuid(uid);
    if (ppwd == NULL) {
      fprintf(stderr, "cannot determine current user\n");
      return 1;
    }
    user = ppwd->pw_name;
  }

  try {

    IncronCfg::Init();

    std::string cfg(INCRON_CONFIG);
    if (AppArgs::GetOption("config", cfg)) {
      if (uid != 0) {
        fprintf(stderr, "insufficient privileges to use custom configuration (will use default)\n");
      }
      else if (euidaccess(cfg.c_str(), R_OK) != 0) {
        perror("cannot read configuration file (will use default)");
      }
    }

    IncronCfg::Load(cfg);

    if (!IncronTab::CheckUser(user)) {
      fprintf(stderr, "user '%s' is not allowed to use incron\n", user.c_str());
      return 1;
    }

    if (!oper) {
      std::string file;
      if (!AppArgs::GetValue(0, file)
          || !copy_from_file(file, user))
      {
        return 1;
      }
    }
    else {
      if (AppArgs::ExistsOption("list")) {
        if (!list_table(user))
          return 1;
      }
      else if (AppArgs::ExistsOption("remove")) {
        if (!remove_table(user))
          return 1;
      }
      else if (AppArgs::ExistsOption("edit")) {
        if (!edit_table(user))
          return 1;
      }
      else if (AppArgs::ExistsOption("types")) {
        list_types();
      }
      else if (AppArgs::ExistsOption("reload")) {
        if (!reload_table(user))
          return 1;
      }
      else {
        fprintf(stderr, "invalid usage\n");
        return 1;
      }
    }

    return 0;

  } catch (InotifyException e) {
    fprintf(stderr, "*** unhandled exception occurred ***\n");
    fprintf(stderr, "%s\n", e.GetMessage().c_str());
    fprintf(stderr, "error: (%i) %s\n", e.GetErrorNumber(), strerror(e.GetErrorNumber()));

    return 1;
  }
}
示例#3
0
int main(int argc, char** argv)
{
  struct arguments arguments;
  
  arguments.user = NULL;
  arguments.oper = OPER_NONE;
  arguments.file = NULL;
  
  argp_parse (&argp, argc, argv, 0, 0, &arguments);
  
  if (arguments.file != NULL && arguments.oper != OPER_NONE) {
    fprintf(stderr, "invalid arguments - specify source file or operation\n");
    return 1;
  }
  if (arguments.file == NULL && arguments.oper == OPER_NONE) {
    fprintf(stderr, "invalid arguments - specify source file or operation\n");
    return 1;
  }
  
  uid_t uid = getuid();
  
  if (uid != 0 && arguments.user != NULL) {
    fprintf(stderr, "cannot access table for user %s: permission denied\n", arguments.user);
    return 1;
  }
  
  struct passwd pwd;
  
  if (arguments.user == NULL) {
    struct passwd* ppwd = getpwuid(uid);
    if (ppwd == NULL) {
      fprintf(stderr, "cannot determine current user\n");
      return 1;
    }
    memcpy(&pwd, ppwd, sizeof(pwd));
    arguments.user = pwd.pw_name;
  }
  else if (getpwnam(arguments.user) == NULL) {
    fprintf(stderr, "user %s not found\n", arguments.user);
    return 1;
  }
  
  if (!InCronTab::CheckUser(arguments.user)) {
    fprintf(stderr, "user %s is not allowed to use incron\n", arguments.user);
    return 1;
  }
  
  switch (arguments.oper) {
    case OPER_NONE:
      fprintf(stderr, "copying table from file: %s\n", arguments.file);
      if (!copy_from_file(arguments.file, arguments.user))
        return 1;
      break;
    case OPER_LIST:
      if (!list_table(arguments.user))
        return 1;
      break;
    case OPER_REMOVE:
      fprintf(stderr, "removing table for user %s\n", arguments.user);
      if (!remove_table(arguments.user))
        return 1;
      break;
    case OPER_EDIT:
      if (!edit_table(arguments.user))
        return 1;
      break;
    default:
      fprintf(stderr, "invalid usage\n");
      return 1;
  }
  
  return 0;
}
示例#4
0
/* List a variable */
void list_var(SYM *sp, int i)
{
    int j;
    long val;
    if (!prm_listfile)
        return ;
    if (sp->dontlist)
        return;
    if (sp->tp->type == bt_defunc)
    {
        sp = sp->tp->lst.head;
        while (sp)
        {
            list_var(sp, 0);
            sp = sp->next;
        }
        return ;
    }
    for (j = i; j; --j)
        fprintf(listFile, "    ");
    if ((sp->storage_class == sc_auto || sp->storage_class == sc_autoreg) &&
        !sp->inreg)
        val = (long)getautoval(sp->value.i);
    else if (sp->storage_class == sc_static || sp->storage_class == sc_global)
        val = sp->offset;
    else
        val = sp->value.u;
    fprintf(listFile,"Identifier:   %s\n    ", unmangledname(sp->name));
    for (j = i; j; --j)
        fprintf(listFile, "    ");
    if (sp->inreg) {
        fprintf(listFile,"Register: %-3s      ",
            registers[( - val) & 255]);
    }
    else
        fprintf(listFile,"Offset:   %08X ", val);
    fprintf(listFile,"Storage: ");
    if (sp->tp->type == bt_ifunc)
        if (sp->value.classdata.cppflags &PF_INLINE)
            fprintf(listFile,"%-7s","inline");
        else
            fprintf(listFile,"%-7s","code");
    else if (sp->storage_class == sc_auto)
        if (sp->inreg)
            fprintf(listFile,"%-7s","reg");
        else
            fprintf(listFile,"%-7s","stack");
    else if (sp->storage_class == sc_global || sp->storage_class == sc_static)
        if ((sp->tp->cflags &DF_CONST) && !(sp->tp->cflags &DF_VOL))
            fprintf(listFile,"%-7s","const");
        else if (sp->init)
            fprintf(listFile,"%-7s","data");
        else
            fprintf(listFile,"%-7s","bss");
    else if (sp->storage_class == sc_const)
        fprintf(listFile,"%-7s","inline");
    else
        fprintf(listFile,"%-7s","none");
    put_sc(sp->storage_class);
    put_ty(sp->tp);
    fprintf(listFile, "\n");
    if (sp->tp == 0)
        return ;
    if (isstructured(sp->tp) && sp->storage_class == sc_type)
        list_table(&(sp->tp->lst), i + 1);
}
示例#5
0
// store information about Lua value present at the 'index' inside 'v' struct
void lua_details::capture_value(lua_State* L, Value& v, int index, int recursive, size_t table_size_limit)
{
	int i= index;
	char buf[100];	// temp output for fast number/pointer formatting

	int t= lua_type(L, i);

	switch (t)
	{
	case LUA_TSTRING:
		v.type = String;
		v.value = lua_tostring(L, i);
		break;

	case LUA_TBOOLEAN:
		v.type = Bool;
		v.value = lua_toboolean(L, i) ? "true" : "false";
		break;

	case LUA_TNUMBER:
		v.type = Number;
		sprintf(buf, "%g", static_cast<double>(lua_tonumber(L, i)));
		v.value = buf;
		break;

	case LUA_TLIGHTUSERDATA:
		v.type = LightUserData;
		v.value = to_pointer(buf, lua_topointer(L, i));
		break;

	case LUA_TUSERDATA:
		v.type = UserData;
		v.value = to_pointer(buf, lua_topointer(L, i));
		break;

	case LUA_TTABLE:
		v.type = Table;
		if (recursive > 0)
		{
			TableInfo t;
			list_table(L, i, t, recursive - 1);
			v.value = table_as_string(t, table_size_limit);
		}
		else
			v.value = to_pointer(buf, lua_topointer(L, i));
		break;

	case LUA_TFUNCTION:
		v.type = Function;
		v.value = to_pointer(buf, lua_topointer(L, i));
		break;

	case LUA_TTHREAD:
		v.type = Thread;
		v.value = to_pointer(buf, lua_topointer(L, i));
		break;

	case LUA_TNIL:
		v.type = Nil;
		v.value.clear();
		break;

	default:
		v.type = None;
		v.value.clear();
		break;
	}

	v.type_name = lua_typename(L, t);
}
示例#6
0
/* List a variable */
void list_var(SYMBOL *sp, int i)
{
    int j;
    long val;
    if (!cparams.prm_listfile)
        return ;
    if (sp->dontlist)
        return;
    if (sp->tp->type == bt_aggregate)
    {
        HASHREC *hr = sp->tp->syms->table[0];
        while (hr)
        {
            sp = (SYMBOL *)hr->p;
            list_var(sp, 0);
            hr = hr->next;
        }
        return ;
    }
    for (j = i; j; --j)
        fprintf(listFile, "    ");
    if (sp->storage_class == sc_auto && !sp->regmode)
        val = (long)getautoval(sp->offset);
    else
        val = sp->value.u;
    fprintf(listFile,"Identifier:   %s\n    ", unmangledname(sp->name));
    for (j = i; j; --j)
        fprintf(listFile, "    ");
    if (sp->regmode == 1) {
        fprintf(listFile,"Register: %-3s&     ",lookupRegName((-sp->offset) & 255));
    }
    else if (sp->regmode == 2) {
        fprintf(listFile,"Register: %-3s      ",lookupRegName((-sp->offset) & 255));
    }
    else
        fprintf(listFile,"Offset:   %08lX ", val);
    fprintf(listFile,"Storage: ");
    if (sp->tp->type == bt_ifunc)
        if (sp->isInline && !sp->noinline)
            fprintf(listFile,"%-7s","inline");
        else
            fprintf(listFile,"%-7s","code");
    else if (sp->storage_class == sc_auto)
        if (sp->regmode)
            fprintf(listFile,"%-7s","reg");
        else
            fprintf(listFile,"%-7s","stack");
    else if (sp->storage_class == sc_global || sp->storage_class == sc_static 
             || sp->storage_class == sc_localstatic)
        if (isconst(sp->tp))
            fprintf(listFile,"%-7s","const");
        else if (sp->init)
            fprintf(listFile,"%-7s","data");
        else
            fprintf(listFile,"%-7s","bss");
    else if (sp->storage_class == sc_constant || sp->storage_class == sc_enumconstant)
        fprintf(listFile,"%-7s","constant");
    else
        fprintf(listFile,"%-7s","none");
    put_sc(sp->storage_class);
    put_ty(sp->tp);
    fprintf(listFile, "\n");
    if (sp->tp == 0)
        return ;
    if (isstructured(sp->tp) && sp->storage_class == sc_type)
        list_table(sp->tp->syms, i + 1);
}