static int64_t NGS_StringToI64( const NGS_String * str, ctx_t ctx ) { /* have to guarantee NUL-termination for strtoi64/strtod */ char buf[4096]; if ( sizeof(buf) > NGS_StringSize ( str, ctx ) ) { char* end; int64_t value; string_copy ( buf, sizeof(buf), NGS_StringData ( str, ctx ), NGS_StringSize ( str, ctx ) ); errno = 0; value = strtoi64 ( buf, &end, 10 ); if ( *end == 0 ) { if ( errno == 0 ) { return value; } } else { /* attempt to parse as a double */ double dbl; errno = 0; dbl = strtod ( buf, &end ); if ( *end == 0 && errno == 0 && dbl >= LLONG_MIN && dbl <= LLONG_MAX ) { return ( int64_t ) xtrunc ( dbl ); } } } INTERNAL_ERROR ( xcUnexpected, "cannot convert dictionary value '%.*s' from string to int64", NGS_StringSize ( str, ctx ), NGS_StringData ( str, ctx ) ); return 0; }
void sfsconst_init (bool lite_mode) { if (const_set) return; const_set = true; { char *p = safegetenv ("SFS_RELEASE"); if (!p || !convertint (p, &sfs_release)) { str rel (strbuf () << "SFS_RELEASE=" << sfs_release); xputenv (const_cast<char*>(rel.cstr())); } } #ifdef MAINTAINER if (char *p = safegetenv ("SFS_RUNINPLACE")) { runinplace = true; builddir = p; buildtmpdir = builddir << "/runinplace"; } if (char *p = safegetenv ("SFS_ROOT")) if (*p == '/') sfsroot = p; #endif /* MAINTAINER */ sfsdevdb = strbuf ("%s/.devdb", sfsroot); #ifdef MAINTAINER if (runinplace) { sfsdir = buildtmpdir; sfssockdir = sfsdir; etc3dir = etc1dir; etc1dir = sfsdir.cstr(); etc2dir = xstrdup (str (builddir << "/etc")); } #endif /* MAINTAINER */ if (char *ps = safegetenv ("SFS_PORT")) if (int pv = atoi (ps)) sfs_defport = pv; str sfs_config = safegetenv ("SFS_CONFIG"); if (sfs_config && sfs_config[0] == '/') { if (!parseconfig (NULL, sfs_config)) fatal << sfs_config << ": " << strerror (errno) << "\n"; } else { if (!parseconfig (etc3dir, sfs_config)) { parseconfig (etc3dir, "sfs_config"); if (!parseconfig (etc2dir, sfs_config)) { parseconfig (etc2dir, "sfs_config"); if (!parseconfig (etc1dir, sfs_config)) parseconfig (etc1dir, "sfs_config"); } } } if (!lite_mode) { if (!sfs_uid) idlookup (NULL, NULL); } if (char *p = getenv ("SFS_HASHCOST")) { sfs_hashcost = strtoi64 (p); if (sfs_hashcost > sfs_maxhashcost) sfs_hashcost = sfs_maxhashcost; } if (!getuid () && !runinplace) { mksfsdir (sfsdir, 0755); mksfsdir (sfssockdir, 0750); } else if (runinplace && access (sfsdir, 0) < 0) { struct stat sb; if (!stat (builddir, &sb)) { mode_t m = umask (0); if (!getuid ()) { if (pid_t pid = fork ()) waitpid (pid, NULL, 0); else { umask (0); setgid (sfs_gid); setuid (sb.st_uid); if (mkdir (sfsdir, 02770) >= 0) rc_ignore (chown (sfsdir, (uid_t) -1, sfs_gid)); _exit (0); } } else mkdir (sfsdir, 0777); umask (m); } } }
static void parseconfig () { str cf = configfile; parseargs pa (cf); bool errors = false; str hostname; rpc_ptr<sfs_hash> hostid; server *s = NULL; release *r = NULL; extension *e = NULL; char *c; int line; vec<str> av; while (pa.getline (&av, &line)) { if (!strcasecmp (av[0], "BindAddr")) { in_addr addr; u_int16_t port = 0; if (av.size () < 2 || av.size () > 3 || !inet_aton (av[1], &addr) || (av.size () == 3 && !convertint (av[2], &port))) { warn << cf << ":" << line << ": usage: BindAddr addr [port]\n"; errors = true; continue; } if (!port) port = sfs_defport ? sfs_defport : SFS_PORT; sockaddr_in *sinp = static_cast<sockaddr_in *> (xmalloc (sizeof (*sinp))); bzero (sinp, sizeof (*sinp)); sinp->sin_family = AF_INET; sinp->sin_port = htons (port); sinp->sin_addr = addr; #ifdef HAVE_SA_LEN sinp->sin_len = sizeof (*sinp); #endif /* HAVE_SA_LEN */ listenaddrs.push_back (reinterpret_cast<sockaddr *> (sinp)); } else if (!strcasecmp (av[0], "Server")) { if (av.size () != 2) { warn << cf << ":" << line << ": usage: Server {hostname|*}[:hostid]\n"; errors = true; continue; } if (strchr (av[1], ':') || ((c = strchr (av[1], '@')) && strchr (c, ','))) { hostid.alloc (); if (!sfs_parsepath (av[1], &hostname, hostid)) { warn << cf << ":" << line << ": bad hostname/hostid\n"; errors = true; continue; } } else { hostid.clear (); if (av[1] == "*") hostname = sfshostname (); else hostname = av[1]; } for (s = serverlist.first; s; s = serverlist.next (s)) if (hostname == s->host && ((hostid && s->hostid && *hostid == *s->hostid) || (!hostid && !s->hostid))) break; if (!s) s = New server (hostname, hostid); r = NULL; e = NULL; } else if (!strcasecmp (av[0], "Release")) { static rxx relrx ("^(\\d+)\\.(\\d\\d?)$"); if (av.size () != 2 || (!relrx.search (av[1]) && av[1] != "*")) { warn << cf << ":" << line << ": usage Release { N.NN | * }\n"; errors = true; r = NULL; continue; } if (!s) { warn << cf << ":" << line << ": Release must follow Server\n"; errors = true; r = NULL; continue; } u_int32_t rel; if (av[1] == "*") rel = 0xffffffff; else rel = strtoi64 (relrx[1]) * 100 + strtoi64 (relrx[2]); r = s->reltab[rel]; if (!r) s->reltab.insert ((r = New release (rel))); for (e = r->extlist.first; r->extlist.next (e); e = r->extlist.next (e)) ; } else if (!strcasecmp (av[0], "Extensions")) { av.pop_front (); e = r->getext (av); } else if (!strcasecmp (av[0], "Service")) { if (!parse_service (av, e, cf << ":" << line)) errors = true; } else if (!strcasecmp (av[0], "HashCost")) { if (av.size () != 2 || !convertint (av[1], &sfs_hashcost)) { warn << cf << ":" << line << ": usage: HashCost <nbits>\n"; errors = true; } else { if (sfs_hashcost > sfs_maxhashcost) sfs_hashcost = sfs_maxhashcost; str s (strbuf ("SFS_HASHCOST=%d", sfs_hashcost)); xputenv (const_cast<char*>(s.cstr())); } } else if (!strcasecmp (av[0], "RevocationDir")) { if (av.size () != 2) { warn << cf << ":" << line << ": usage: RevocationDir <directory>\n"; errors = true; } else { revocationdir = av[1]; } } else { errors = true; warn << cf << ":" << line << ": unknown directive '" << av[0] << "'\n"; } } if (errors) fatal ("parse errors in configuration file\n"); }
void ShortcutManager::Load() { unsigned char *key; unsigned char *command; ClearShortcuts(); int count = 0; while (command = config.GetConfigStringAndKey((unsigned char *)"Shortcuts", key, count)) { count++; if (command || key) { if (key && command && srealloc(shortcuts, sizeof(Shortcut) * (numShortcuts+1))) { unsigned int modifiers = 0; unsigned int vkey = 0; int i = 0; while (1) { if (key[i] == '@') modifiers |= MOD_WIN; else if (key[i] == '!') modifiers |= MOD_ALT; else if (key[i] == '^') modifiers |= MOD_CONTROL; else if (key[i] == '+') modifiers |= MOD_SHIFT; else break; i++; } if (key[i+1] == 0) { short code = VkKeyScan(key[i]); if (code != 0xFFFF) { vkey = code & 0xFF; } /*if ((UCASE(key[i] >= 'A') && UCASE(key[i] <= 'Z')) || (key[i] >= '0' && key[i] <= '9')) vkey = UCASE(key[i]); if ((UCASE(key[i] >= 'A') && UCASE(key[i] <= 'Z')) || (key[i] >= '0' && key[i] <= '9')) //*/ } else if (CompareSubstringNoCase(key+i, (unsigned char*)"num") == 0) { if (CompareSubstringNoCase(key+i, (unsigned char*)"pad") == 0) i+=3; if (key[i+1] == 0 && '0' <= key[i] && key[i] <= '9') { vkey = VK_NUMPAD0 + key[i] - '0'; } else if (stricmp(key+i, (unsigned char*) "*") == 0 || stricmp(key+i, (unsigned char*) "multiply") == 0 || stricmp(key+i, (unsigned char*) "mul") == 0 || stricmp(key+i, (unsigned char*) "times") == 0 || stricmp(key+i, (unsigned char*) "star") == 0) vkey = VK_ADD; else if (stricmp(key+i, (unsigned char*) "+") == 0 || stricmp(key+i, (unsigned char*) "plus") == 0 || stricmp(key+i, (unsigned char*) "add") == 0) vkey = VK_ADD; else if (stricmp(key+i, (unsigned char*) "-") == 0 || stricmp(key+i, (unsigned char*) "minus") == 0 || stricmp(key+i, (unsigned char*) "sub") == 0 || stricmp(key+i, (unsigned char*) "subtract") == 0) vkey = VK_SUBTRACT; else if (stricmp(key+i, (unsigned char*) "/") == 0 || stricmp(key+i, (unsigned char*) "divide") == 0 || stricmp(key+i, (unsigned char*) "div") == 0 || stricmp(key+i, (unsigned char*) "slash") == 0) vkey = VK_DIVIDE; else if (stricmp(key+i, (unsigned char*) ".") == 0 || stricmp(key+i, (unsigned char*) "dec") == 0 || stricmp(key+i, (unsigned char*) "decimal") == 0) vkey = VK_DECIMAL; else if (stricmp(key+i, (unsigned char*) ".") == 0 || stricmp(key+i, (unsigned char*) "dec") == 0 || stricmp(key+i, (unsigned char*) "decimal") == 0) vkey = VK_DECIMAL; else if (stricmp(key+i, (unsigned char*) "lock") == 0 || key[i] == 0) vkey = VK_NUMLOCK; } else if (key[i] == 'f' || key[i] == 'F') { i++; if ('0' <= key[i] && key[i] <= '9' && (key[i+1] == 0 || (key[i+2] == 0 && '0' <= key[i+1] && key[i+1] <= '9'))) { int w = (int) strtoi64((char*)key+i, 0, 0); if (w > 0 && w <=24) { // 24 f keys? There are key codes for them...apparently. vkey = VK_F1 + w; } } } else if (stricmp(key+i, (unsigned char*) "space") == 0) vkey = VK_SPACE; else if (stricmp(key+i, (unsigned char*) "esc") == 0 || stricmp(key+i, (unsigned char*) "escape") == 0) vkey = VK_ESCAPE; else if (stricmp(key+i, (unsigned char*) "pgup") == 0 || stricmp(key+i, (unsigned char*) "pageup") == 0) vkey = VK_PRIOR; else if (stricmp(key+i, (unsigned char*) "pgdn") == 0 || stricmp(key+i, (unsigned char*) "pagedn") == 0 || stricmp(key+i, (unsigned char*) "pagedown") == 0) vkey = VK_NEXT; else if (stricmp(key+i, (unsigned char*) "end") == 0) vkey = VK_END; else if (stricmp(key+i, (unsigned char*) "home") == 0) vkey = VK_HOME; else if (stricmp(key+i, (unsigned char*) "left") == 0) vkey = VK_LEFT; else if (stricmp(key+i, (unsigned char*) "right") == 0) vkey = VK_RIGHT; else if (stricmp(key+i, (unsigned char*) "down") == 0) vkey = VK_DOWN; else if (stricmp(key+i, (unsigned char*) "up") == 0) vkey = VK_UP; else if (stricmp(key+i, (unsigned char*) "print") == 0 || stricmp(key+i, (unsigned char*) "printscreen") == 0) vkey = VK_SNAPSHOT; else if (stricmp(key+i, (unsigned char*) "ins") == 0 || stricmp(key+i, (unsigned char*) "insert") == 0) vkey = VK_INSERT; else if (stricmp(key+i, (unsigned char*) "del") == 0 || stricmp(key+i, (unsigned char*) "delete") == 0) vkey = VK_DELETE; else if (stricmp(key+i, (unsigned char*) "help") == 0) vkey = VK_HELP; else if (stricmp(key+i, (unsigned char*) "pause") == 0) vkey = VK_PAUSE; else if (stricmp(key+i, (unsigned char*) "caps") == 0 || stricmp(key+i, (unsigned char*) "capslock") == 0) vkey = VK_CAPITAL; else if (stricmp(key+i, (unsigned char*) "tab") == 0) vkey = VK_TAB; else if (stricmp(key+i, (unsigned char*) "scroll") == 0 || stricmp(key+i, (unsigned char*) "scrolllock") == 0) vkey = VK_SCROLL; else if (stricmp(key+i, (unsigned char*) "back") == 0 || stricmp(key+i, (unsigned char*) "backspace") == 0) vkey = VK_BACK; else if (stricmp(key+i, (unsigned char*) "clear") == 0 || stricmp(key+i, (unsigned char*) "cls") == 0 || stricmp(key+i, (unsigned char*) "clearscreen") == 0) vkey = VK_CLEAR; else if (stricmp(key+i, (unsigned char*) "enter") == 0 || stricmp(key+i, (unsigned char*) "resturn") == 0) vkey = VK_RETURN; else if (stricmp(key+i, (unsigned char*) "shift") == 0) vkey = VK_SHIFT; else if (stricmp(key+i, (unsigned char*) "control") == 0 || stricmp(key+i, (unsigned char*) "ctrl") == 0) vkey = VK_MENU; if (vkey) { if (RegisterHotKey(ghWnd, numShortcuts, modifiers, vkey)) { shortcuts[numShortcuts].flags = modifiers; shortcuts[numShortcuts].vkey = vkey; shortcuts[numShortcuts++].command = command; LeftJustify(command); continue; } } } if (command) free(command); continue; } break; } }
int JSONdecodeSub(ScriptValue &s, unsigned char * &b) { while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++; CreateNullValue(s); if (b[0] == '"') { return JSONParseString(s, b); } else if (b[0] == '[') { if (!CreateListValue(s, 4)) return 0; b++; while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++; if (*b == ']') { b++; return 1; } while (1) { ScriptValue sv; int res = JSONdecodeSub(sv, b); int res2; if (sv.type != SCRIPT_NULL) { res2 = s.listVal->PushBack(sv); } if (!res || !res2) return 0; while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++; if (*b == ']') { b++; return 1; } // ??? if (*b != ',') return 0; b++; } } else if (b[0] == '{') { if (!CreateDictValue(s, 4)) return 0; b++; while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++; if (*b == '}') { b++; return 1; } while (1) { ScriptValue sv, sv2; if (!JSONParseString(sv, b)) { sv.Release(); return 0; } while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++; if (*b != ':') { sv.Release(); return 0; } b++; int res = JSONdecodeSub(sv2, b); int res2 = s.dictVal->Add(sv, sv2); if (!res || !res2) return 0; while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++; if (*b == '}') { b++; return 1; } // ??? if (*b != ',') return 0; b++; } } else if ((b[0] >= '0' && b[0] <= '9') || b[0] == '-' || b[0] == '.') { unsigned char *t1, *t2; __int64 i = strtoi64((char*)b, (char**)&t1, 0); double d = strtod((char*)b, (char**)&t2); if (t1 >= t2) { if (t1) { b = t1; CreateIntValue(s, i); return 1; } } else { b = t2; CreateDoubleValue(s, d); return 1; } } else { if (LCASE(b[0]) == 'n' && LCASE(b[1]) == 'u' && LCASE(b[2]) == 'l' && LCASE(b[3]) == 'l') { b+=4; return 1; } if (LCASE(b[0]) == 'f' && LCASE(b[1]) == 'a' && LCASE(b[2]) == 'l' && LCASE(b[3]) == 's' && LCASE(b[4]) == 'e') { b+=5; return 1; } if (LCASE(b[0]) == 't' && LCASE(b[1]) == 'r' && LCASE(b[2]) == 'u' && LCASE(b[3]) == 'e') { CreateIntValue(s, 1); b+=4; return 1; } } return 0; }
int options_parse(int argc, char *argv[], options_t *options) { int result = PQ_SUCCESS; int c, option_index; char *options_string = "hVvi:o:bp:zrmtn:"; static struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"print-every", required_argument, 0, 'p'}, {"file-in", required_argument, 0, 'i'}, {"file-out", required_argument, 0, 'o'}, {"binary-out", no_argument, 0, 'b'}, {"resolution-only", no_argument, 0, 'z'}, {"header-only", no_argument, 0, 'r'}, {"mode-only", no_argument, 0, 'm'}, {"to-t2", no_argument, 0, 't'}, {"number", required_argument, 0, 'n'}, {0, 0, 0, 0}}; while ( (c = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1 ) { switch (c) { case 'h': usage(); description(); result = PQ_USAGE; break; case 'v': verbose = 1; break; case 'V': version(); result = PQ_VERSION; break; case 'p': options->print_every = strtoi32(optarg, NULL, 10); break; case 'i': options->filename_in = strdup(optarg); break; case 'o': options->filename_out = strdup(optarg); break; case 'b': options->binary_out = 1; break; case 'z': options->print_resolution = 1; break; case 'r': options->print_header = 1; break; case 'm': options->print_mode = 1; break; case 't': options->to_t2 = 1; break; case 'n': options->number = strtoi64(optarg, NULL, 10); break; case '?': default: usage(); description(); result = PQ_ERROR_OPTIONS; } } return(result); }