static value ex(const string &s, size_t curr) { value ret, id; bool running = true; while (running) { vector<value> tok; for (;;) { const auto next = s.find_first_of("; \r\t\n[(", curr); const auto last = next == string::npos ? s.size() : next; const auto len = last-curr; const auto c = s[last]; if (len!=0) tok.push_back(stov(s.substr(curr,len))); if (c == '(' || c == '[') { const auto v = expr(s, c, curr); tok.push_back(v.first); curr = v.second; } else if (c == ';' || c == '\n') {curr=last+1; break;} else if (c == '\0') {curr=last; running=false; break;} else curr=last+1; } if (tok.size() == 0) return btov(false); auto const it = unique<builtinmap>()->find(vtos(tok[0])); if (it!=unique<builtinmap>()->end()) // try to call a builtin ret = it->second(tok); else if (tok.size() == 1 && vtos(tok[0]) == s) return stov(s); // literals else { // function call scope frame; for (size_t i = 1; i < tok.size(); ++i) new_local(to_string(i-1),tok[i]); ret = ex(vtos(tok[0])); } } return ret; }
void NexuizColorpickerString_configureNexuizColorpickerString(entity me, string theCvar, string theDefaultCvar) { me.cvarName = theCvar; me.configureImage(me, me.image); if(cvar_string(theCvar) != "") me.prevcoords = color_hslimage(stov(cvar_string(theCvar)), me.imagemargin); else // use default me.prevcoords = color_hslimage(stov(cvar_string(theDefaultCvar)), me.imagemargin); }
void XonoticCrosshairButton_draw(entity me) { vector sz, rgb; float a; rgb = stov(cvar_string("crosshair_color")); a = cvar("crosshair_alpha"); if(!me.checked && !me.focused && me.cvarValueFloat != -1) { a *= me.disabledAlpha; rgb = '1 1 1'; } if(me.cvarValueFloat == -1) // update the preview if this is the preview button { if(me.src3) strunzone(me.src3); me.src3 = strzone(strcat("/gfx/crosshair", cvar_string("crosshair"))); me.focused = 1; me.checked = 0; } SUPER(XonoticCrosshairButton).draw(me); sz = draw_PictureSize(me.src3); sz = globalToBoxSize(sz, draw_scale); if(me.cvarValueFloat == -1) { sz = (6 * '1 1 0' + sz * cvar("crosshair_size")) * 0.08; // (6 * '1 1 0' + ...) * 0.08 here to make visible size changes happen also at bigger sizes if(sz_x > 0.95) sz = sz * (0.95 / sz_x); if(sz_y > 0.95) sz = sz * (0.95 / sz_y); } else // show the crosshair picker at full size sz = '0.95 0.95 0'; draw_Picture('0.5 0.5 0' - 0.5 * sz, me.src3, sz, rgb, a); if(cvar("crosshair_dot")) { if(cvar_string("crosshair_dot_color") != "0") rgb = stov(cvar_string("crosshair_dot_color")); draw_Picture('0.5 0.5 0' - 0.5 * sz * cvar("crosshair_dot_size"), me.src4, sz * cvar("crosshair_dot_size"), rgb, a * cvar("crosshair_dot_alpha")); } }
static value loop_builtin(args arg) { value last; auto const n = int(vtod(get(arg,2))); for (int i = 0; i < n; ++i) { try { new_local(vtos(get(arg,1)), stov(to_string(i))); last = ex(vtos(get(arg,3))); } catch (bool b) { if (b) break; else continue; } } return last; }
static pair<value,size_t> expr(const string &s, char c, size_t curr) { const char *match = c=='['?"[]@":"()"; stringstream ss; size_t opened = 1, next = curr; while (opened) { if ((next = s.find_first_of(match,next+1)) == string::npos) throw boodew_exception(format("missing %c", c=='['?']':')')); if (c == '[' && s[next] == '@') { ss << s.substr(curr+1, next-curr-1); if (s[next+1] == '(') { const auto v = expr(s, '(', next+1); ss << vtos(v.first); if (s[v.second]!=']'||opened!=1) ss << s[v.second]; curr = v.second; next = curr > 0 ? curr-1 : 0; } else do ss << s[curr = ++next]; while (s[next] == '@'); } else opened += s[next] == c ? +1 : -1; } if (next>curr) ss << s.substr(curr+1, next-curr-1); return make_pair((c=='[' ? stov(ss.str()) : ex(ss.str())), next+1); }
last = ex(vtos(get(arg,3))); } catch (bool b) { if (b) break; else continue; } } return last; } static value if_builtin(args arg) { return vtob(get(arg,1)) ? ex(vtos(get(arg,2))): (arg.size()>3?ex(vtos(get(arg,3))):btov(false)); } #define O(S)CMDL(#S,[](args arg){return dtov(vtod(get(arg,1)) S vtod(get(arg,2)));}) O(+) O(-) O(/) O(*) #undef O #define O(S)CMDL(#S,[](args arg){return btov(vtod(get(arg,1)) S vtod(get(arg,2)));}) O(==) O(!=) O(<) O(>) O(<=) O(>=) #undef O CMDL("int",[](args arg){return stov(to_string(int(vtod(get(arg,1)))));}) CMDL("var",[](args arg){return new_local(vtos(get(arg,1)),arg.size()<3?btov(false):get(arg,2));}) CMDL("#", [](args){return btov(false);}) CMDL("..", [](args arg){return stov(vtos(get(arg,1))+vtos(get(arg,2)));}) CMDL("echo", [](args arg){cout<<vtos(get(arg,1));return get(arg,1);}) CMDL("^", [](args arg){return get(arg,1);}) CMDL("return", [](args arg)->value {throw get(arg,1);}) CMDL("do", [](args arg){try {return ex(vtos(get(arg,1)));} catch (value v) {return v;}}) CMDL("break", [](args arg)->value {throw true;}) CMDL("continue", [](args arg)->value {throw false;}) CMDN("while", while_builtin) CMDN("loop", loop_builtin) CMDN("?", if_builtin) CMDN("$", getvar) pair<string,bool> exec(const string &s) { try {ex(s); return make_pair("",true);}
int main (int argc, char const *argv []) { extern char const *host_name; extern char const *domain_name; static char const *optv [] = { "a:df:" #ifdef SYSLOGD_INETAF "hi:l:" #endif "m:no:p:" #ifdef SYSLOGD_INETAF "rs:" #endif "xvw:", "System Logging Daemon", "a s\tadd unix socket path s", "d\trun as daemon (background)", "f s\tuse configuration file s [" _PATH_SYSLOG_CONF "]", #ifdef SYSLOGD_INETAF "h\tforward messages to other hosts", "i n\tadd inet socket on port n", "l s\tlist of hostnames separated by ':'", #endif "m nn\tmark internal is nn[s|S|m|M|h|H]", "n\trun as normal program (foreground)", "o s\toutput file is s", "p s\tlocal log device path is s [" _PATH_SYSLOG "]", #ifdef SYSLOGD_UNIXAF "r\treceive messages from other hosts", "s s\tdomain list is s", #endif "v\tdisplay version and exit", "w n\twait n seconds for klogd to start", "x\tnot implemented", (char const *) (0) }; #ifdef SYSLOGD_INETAF struct servent *servent; struct sockaddr_in *sockaddr_inet; #endif #ifdef SYSLOGD_UNIXAF struct socket * socket; struct sockaddr_un *sockaddr_unix; #endif struct hostent *hostent; char fqdn [HOSTNAME_MAX]; time_t delay = SYSLOGD_DELAY_TIME; file_t fd; int c; chdir ("/"); /*--------------------------------------------------------------------* * point host_name and domain_name to constant strings; technically, * the fqdn is the name returned by gethostbyname when given the name * returned by gethostname; store the fqdn then break it at the first * '.', if present; *--------------------------------------------------------------------*/ gethostname (fqdn, sizeof (fqdn)); if ((hostent = gethostbyname (fqdn)) != (struct hostent *) (0)) { strncpy (fqdn, hostent->h_name, sizeof (fqdn)); } strlwr (fqdn); for (host_name = domain_name = fqdn; *domain_name != (char) (0); domain_name++) { if (*domain_name == '.') { fqdn [domain_name++ - host_name] = (char) (0); break; } } #ifdef SYSLOGD_INETAF /*--------------------------------------------------------------------* * confirm that the service is registered; the name is "syslog", the * type is "udp" and it should appear in "/etc/services"; record the * port number in struct sockaddr_in for global reference; *--------------------------------------------------------------------*/ servent = getservbyname (SYSLOGD_SERVICE_NAME, SYSLOGD_SERVICE_TYPE); if (servent == (struct servent *) (0)) { error (1, ENOTSUP, "The %s %s service is not registered on %s", SYSLOGD_SERVICE_NAME, SYSLOGD_SERVICE_TYPE, host_name); } if (servent != (struct servent *) (0)) { inetsock_addr.sin_port = servent->s_port; } #endif optind = 1; opterr = 1; while ((c = getoptv (argc, argv, optv)) != -1) { switch ((char) (c)) { #ifdef SYSLOGD_UNIXAF /*--------------------------------------------------------------------* * create additional unix socket with specified pathname and append * it to the sockets list; *--------------------------------------------------------------------*/ case 'a': sockaddr_unix = NEW (struct sockaddr_un); if (sockaddr_unix == (struct sockaddr_un *)(0)) { break; } memset (sockaddr_unix, 0, sizeof (struct sockaddr_un)); sockaddr_unix->sun_family = AF_UNIX; memcpy (&sockaddr_unix->sun_path, optarg, sizeof (sockaddr_unix->sun_path)); socket = NEW (struct socket); if (socket == (struct socket *)(0)) { error (0, errno, "Can't add socket %s", optarg); free (sockaddr_unix); break; } memset (socket, 0, sizeof (struct socket)); socket->desc = -1; socket->socksize = sizeof (struct sockaddr_un); socket->sockaddr = (struct sockaddr *)(sockaddr_unix); socket->next = &unixsock; socket->prev = unixsock.prev; unixsock.prev->next = socket; unixsock.prev = socket; break; #endif #ifdef SYSLOGD_INETAF case 'i': sockaddr_inet = NEW (struct sockaddr_in); if (sockaddr_inet == (struct sockaddr_in *)(0)) { break; } sockaddr_inet->sin_family = AF_INET; sockaddr_inet->sin_port = (unsigned short)(uintspec (optarg, IPPORT_RESERVED, IPPORT_USERRESERVED)); socket = NEW (struct socket); if (socket == (struct socket *)(0)) { error (0, errno, "Can't add socket %s", optarg); free (sockaddr_inet); break; } memset (socket, 0, sizeof (struct socket)); socket->desc = -1; socket->socksize = sizeof (struct sockaddr_in); socket->sockaddr = (struct sockaddr *)(sockaddr_inet); socket->next = &inetsock; socket->prev = inetsock.prev; inetsock.prev->next = socket; inetsock.prev = socket; break; #endif case 'd': _setbits (state, SYSLOGD_STATE_DAEMON); break; case 'f': cfgfile = optarg; break; #ifdef SYSLOGD_INETAF case 'h': _setbits (state, SYSLOGD_STATE_FORWARD); break; #endif #ifdef SYSLOGD_INETAF case 'l': if (ourhosts != (char const **) (0)) { error (0, EINVAL, "discarding '%s': only one host list allowed", optarg); break; } ourhosts = stov (optarg, ':'); break; #endif case 'm': timer = waitspec (optarg); break; case 'n': _clrbits (state, SYSLOGD_STATE_DAEMON); break; case 'o': mapfile = optarg; break; #ifdef SYSLOGD_UNIXAF case 'p': strncpy (unixsock_addr.sun_path, optarg, sizeof (unixsock_addr.sun_path)); break; #endif #ifdef SYSLOGD_INETAF case 'r': _setbits (state, SYSLOGD_STATE_RECEIVE); break; #endif #ifdef SYSLOGD_INETAF case 's': if (ourdomains != (char const **) (0)) { error (0, 0, "ignoring '%s': only one -s argument allowed", optarg); break; } ourdomains = stov (optarg, ':'); break; #endif case 't': delay = uintspec (optarg, 0, 3600); break; case 'v': version (); exit (0); case 'w': delay = waitspec (optarg); break; case 'x': break; default: break; } } argc -= optind; argv += optind; if (getuid ()) { error (1, EACCES, NOTROOT); } if (checkpf (pidfile)) { error (1, 0, "service is already running"); } #ifdef SYSLOGD_INETAF if (_allset (state, (SYSLOGD_STATE_RECEIVE|SYSLOGD_STATE_FORWARD))) { error (1, EPERM, "Can't receive and forward messages in same session"); } #endif memset (&sigquit, 0, sizeof (struct sigaction)); sigquit.sa_handler = syslogd_sigquit; sigquit.sa_flags = SA_ONESHOT; sigemptyset (&sigquit.sa_mask); memset (&sigterm, 0, sizeof (struct sigaction)); sigterm.sa_handler = syslogd_sigterm; sigterm.sa_flags = SA_ONESHOT; sigemptyset (&sigterm.sa_mask); memset (&sigchld, 0, sizeof (struct sigaction)); sigchld.sa_handler = syslogd_sigchld; sigchld.sa_flags = 0; sigemptyset (&sigchld.sa_mask); memset (&sigalrm, 0, sizeof (struct sigaction)); sigalrm.sa_handler = syslogd_sigalrm; sigalrm.sa_flags = 0; sigemptyset (&sigalrm.sa_mask); memset (&sigusr1, 0, sizeof (struct sigaction)); sigusr1.sa_handler = syslogd_sigusr1; sigusr1.sa_flags = 0; sigemptyset (&sigusr1.sa_mask); memset (&sighup, 0, sizeof (struct sigaction)); sighup.sa_handler = syslogd_sighup; sighup.sa_flags = 0; sigemptyset (&sighup.sa_mask); sigaction (SIGINT, &sigquit, (struct sigaction *) (0)); sigaction (SIGTERM, &sigquit, (struct sigaction *) (0)); sigaction (SIGQUIT, &sigquit, (struct sigaction *) (0)); if (_allset (state, SYSLOGD_STATE_DAEMON)) { if (fork ()) { sleep (delay); error (1, 0, "daemon did not start"); } setsid (); for (fd = getdtablesize (); fd-- > 0; close (fd)); kill (getppid (), SIGTERM); sigquit.sa_handler = SIG_IGN; } sigaction (SIGTERM, &sigterm, (struct sigaction *) (0)); sigaction (SIGCHLD, &sigchld, (struct sigaction *) (0)); sigaction (SIGALRM, &sigalrm, (struct sigaction *) (0)); sigaction (SIGHUP, &sighup, (struct sigaction *) (0)); setlinebuf (stdout); if (!writepf (pidfile)) { error (1, errno, "can't create pidfile %s", pidfile); } #ifdef SYSLOGD_INETAF /*--------------------------------------------------------------------* * the next two checks require some double-think; the forward and * receive flags cannot occur together because that is eliminated * above; thus only one or the other or neither flag is set here; * * we disable receiving and forwarding at the same time to avoid * accidental message loops; this means that each syslog host is * either a divinity (source) or a black hole (sink); * * when forwardng messages we do not add inet sockets to the socket * list but we use the first inet socket (inetsock) as the template * for host-type syslogs; consequently, we allocate the descriptor * calling syslogd_parse() where it is used in * that way; * * when receiving messages then we add inet sockets to the socket * list and let syslogd_open_sockets() allocate their descriptors * since syslog_parse() does not use inetsock in receive mode; *--------------------------------------------------------------------*/ if (_anyset (state, SYSLOGD_STATE_RECEIVE)) { socket = unixsock.next; unixsock.next = inetsock.next; inetsock.next = socket; socket = unixsock.prev; unixsock.prev = inetsock.prev; inetsock.prev = socket; } if (_anyset (state, SYSLOGD_STATE_FORWARD)) { inetsock.desc = syslogd_inet_socket ((struct sockaddr_in *)(inetsock.sockaddr)); if (inetsock.desc == -1) { error (1, errno, "Can't start %s %s service", SYSLOGD_SERVICE_NAME, SYSLOGD_SERVICE_TYPE); } } #endif /*--------------------------------------------------------------------* * start mark message timer so that it activates on even multiples of * clock time relative to minute 0; timer must be an integral divisor * of 3600 and greater than 60 for this to work as intended; function * waitspec() filters input to ensure this; *--------------------------------------------------------------------*/ if (timer) { time_t clock = time (&clock); alarm (timer - clock%timer); } /*--------------------------------------------------------------------* * read configuration, create syslogs and open them; write the syslog * map file, if enabled and requested; open sockets; read messages and * dispatch them until terminated; close sockets then close syslogs; *--------------------------------------------------------------------*/ syslogd_start (&syslogs, state, cfgfile); #ifdef SYSLOGD_REPORT syslogd_admin (&syslogs, state, mapfile); #endif syslogd_open_sockets (&unixsock); while (loop) { syslogd_read_sockets (&unixsock); } syslogd_close_sockets (&unixsock); syslogd_stop (&syslogs, state); removepf (pidfile); exit (0); }