static void list_load_from_data (GSList ** list, char *ibuf, int size) { char cmd[384]; char name[128]; char *buf; int pnt = 0; cmd[0] = 0; name[0] = 0; while (buf_get_line (ibuf, &buf, &pnt, size)) { if (*buf != '#') { if (!g_ascii_strncasecmp (buf, "NAME ", 5)) { safe_strcpy (name, buf + 5, sizeof (name)); } else if (!g_ascii_strncasecmp (buf, "CMD ", 4)) { safe_strcpy (cmd, buf + 4, sizeof (cmd)); if (*name) { list_addentry (list, cmd, name); cmd[0] = 0; name[0] = 0; } } } } }
static void test_proto_line(void *arg) { (void)arg; char tmp[60]; buf_t *buf = buf_new(); #define S(str) str, sizeof(str)-1 const struct { const char *input; size_t input_len; size_t line_len; const char *output; int returnval; } cases[] = { { S("Hello world"), 0, NULL, 0 }, { S("Hello world\n"), 12, "Hello world\n", 1 }, { S("Hello world\nMore"), 12, "Hello world\n", 1 }, { S("\n oh hello world\nMore"), 1, "\n", 1 }, { S("Hello worpd\n\nMore"), 12, "Hello worpd\n", 1 }, { S("------------------------------------------------------------\n"), 0, NULL, -1 }, }; unsigned i; for (i = 0; i < ARRAY_LENGTH(cases); ++i) { buf_add(buf, cases[i].input, cases[i].input_len); memset(tmp, 0xfe, sizeof(tmp)); size_t sz = sizeof(tmp); int rv = buf_get_line(buf, tmp, &sz); tt_int_op(rv, OP_EQ, cases[i].returnval); if (rv == 1) { tt_int_op(sz, OP_LT, sizeof(tmp)); tt_mem_op(cases[i].output, OP_EQ, tmp, sz+1); tt_int_op(buf_datalen(buf), OP_EQ, cases[i].input_len - strlen(tmp)); tt_int_op(sz, OP_EQ, cases[i].line_len); } else { tt_int_op(buf_datalen(buf), OP_EQ, cases[i].input_len); // tt_int_op(sz, OP_EQ, sizeof(tmp)); } buf_clear(buf); } done: buf_free(buf); }
static int key_load_kbs (void) { char *buf, *ibuf; struct stat st; struct key_binding *kb = NULL; int fd, len, state = 0, pnt = 0; guint keyval; GdkModifierType mod = 0; off_t size; fd = hexchat_open_file ("keybindings.conf", O_RDONLY, 0, 0); if (fd < 0) { ibuf = g_strdup (default_kb_cfg); size = strlen (default_kb_cfg); } else { if (fstat (fd, &st) != 0) { close (fd); return 1; } ibuf = g_malloc(st.st_size); read (fd, ibuf, st.st_size); size = st.st_size; close (fd); } if (keybind_list) { g_slist_free_full (keybind_list, key_free); keybind_list = NULL; } while (buf_get_line (ibuf, &buf, &pnt, size)) { if (buf[0] == '#') continue; if (strlen (buf) == 0) continue; switch (state) { case KBSTATE_MOD: kb = g_new0 (struct key_binding, 1); /* New format */ if (strncmp (buf, "ACCEL=", 6) == 0) { buf += 6; gtk_accelerator_parse (buf, &keyval, &mod); kb->keyval = keyval; kb->mod = key_modifier_get_valid (mod); state = KBSTATE_ACT; continue; } if (key_load_kbs_helper_mod (buf, &mod)) goto corrupt_file; kb->mod = mod; state = KBSTATE_KEY; continue; case KBSTATE_KEY: STRIP_WHITESPACE keyval = gdk_keyval_from_name (buf); if (keyval == 0) { g_free (ibuf); return 2; } kb->keyval = keyval; state = KBSTATE_ACT; continue; case KBSTATE_ACT: STRIP_WHITESPACE kb->action = key_get_action_from_string (buf); if (kb->action == KEY_MAX_ACTIONS + 1) { g_free (ibuf); return 3; } state = KBSTATE_DT1; continue; case KBSTATE_DT1: case KBSTATE_DT2: if (state == KBSTATE_DT1) kb->data1 = kb->data2 = NULL; while (buf[0] == ' ' || buf[0] == '\t') buf++; if (buf[0] != 'D') { g_free (ibuf); return 4; } switch (buf[1]) { case '1': if (state != KBSTATE_DT1) goto corrupt_file; break; case '2': if (state != KBSTATE_DT2) goto corrupt_file; break; default: goto corrupt_file; } if (buf[2] == ':') { len = strlen (buf); /* Add one for the NULL, subtract 3 for the "Dx:" */ len++; len -= 3; if (state == KBSTATE_DT1) { kb->data1 = g_strndup (&buf[3], len); } else { kb->data2 = g_strndup (&buf[3], len); } } else if (buf[2] == '!') { if (state == KBSTATE_DT1) kb->data1 = NULL; else kb->data2 = NULL; } if (state == KBSTATE_DT1) { state = KBSTATE_DT2; continue; } else { keybind_list = g_slist_append (keybind_list, kb); state = KBSTATE_MOD; } continue; } } g_free (ibuf); return 0; corrupt_file: g_free (ibuf); g_free (kb); return 5; }
static int key_load_kbs (char *filename) { char *buf, *ibuf; struct stat st; struct key_binding *kb = NULL, *last = NULL; int fd, len, pnt = 0, state = 0, n; if (filename == NULL) fd = xchat_open_file ("keybindings.conf", O_RDONLY, 0, 0); else fd = xchat_open_file (filename, O_RDONLY, 0, XOF_FULLPATH); if (fd < 0) return 1; if (fstat (fd, &st) != 0) return 1; ibuf = malloc (st.st_size); read (fd, ibuf, st.st_size); close (fd); while (buf_get_line (ibuf, &buf, &pnt, st.st_size)) { if (buf[0] == '#') continue; if (strlen (buf) == 0) continue; switch (state) { case KBSTATE_MOD: kb = (struct key_binding *) malloc (sizeof (struct key_binding)); if (key_load_kbs_helper_mod (buf, &kb->mod)) goto corrupt_file; state = KBSTATE_KEY; continue; case KBSTATE_KEY: /* First strip off the fluff */ while (buf[0] == ' ' || buf[0] == '\t') buf++; len = strlen (buf); while (buf[len] == ' ' || buf[len] == '\t') { buf[len] = 0; len--; } n = gdk_keyval_from_name (buf); if (n == 0) { /* Unknown keyname, abort */ if (last) last->next = NULL; free (ibuf); ibuf = malloc (1024); snprintf (ibuf, 1024, _("Unknown keyname %s in key bindings config file\nLoad aborted, please fix %s/keybindings.conf\n"), buf, get_xdir_utf8 ()); fe_message (ibuf, FE_MSG_ERROR); free (ibuf); return 2; } kb->keyname = gdk_keyval_name (n); kb->keyval = n; state = KBSTATE_ACT; continue; case KBSTATE_ACT: /* First strip off the fluff */ while (buf[0] == ' ' || buf[0] == '\t') buf++; len = strlen (buf); while (buf[len] == ' ' || buf[len] == '\t') { buf[len] = 0; len--; } for (n = 0; n < KEY_MAX_ACTIONS + 1; n++) { if (strcmp (key_actions[n].name, buf) == 0) { kb->action = n; break; } } if (n == KEY_MAX_ACTIONS + 1) { if (last) last->next = NULL; free (ibuf); ibuf = malloc (1024); snprintf (ibuf, 1024, _("Unknown action %s in key bindings config file\nLoad aborted, Please fix %s/keybindings\n"), buf, get_xdir_utf8 ()); fe_message (ibuf, FE_MSG_ERROR); free (ibuf); return 3; } state = KBSTATE_DT1; continue; case KBSTATE_DT1: case KBSTATE_DT2: if (state == KBSTATE_DT1) kb->data1 = kb->data2 = NULL; while (buf[0] == ' ' || buf[0] == '\t') buf++; if (buf[0] != 'D') { free (ibuf); ibuf = malloc (1024); snprintf (ibuf, 1024, _("Expecting Data line (beginning Dx{:|!}) but got:\n%s\n\nLoad aborted, Please fix %s/keybindings\n"), buf, get_xdir_utf8 ()); fe_message (ibuf, FE_MSG_ERROR); free (ibuf); return 4; } switch (buf[1]) { case '1': if (state != KBSTATE_DT1) goto corrupt_file; break; case '2': if (state != KBSTATE_DT2) goto corrupt_file; break; default: goto corrupt_file; } if (buf[2] == ':') { len = strlen (buf); /* Add one for the NULL, subtract 3 for the "Dx:" */ len++; len -= 3; if (state == KBSTATE_DT1) { kb->data1 = malloc (len); memcpy (kb->data1, &buf[3], len); } else { kb->data2 = malloc (len); memcpy (kb->data2, &buf[3], len); } } else if (buf[2] == '!') { if (state == KBSTATE_DT1) kb->data1 = NULL; else kb->data2 = NULL; } if (state == KBSTATE_DT1) { state = KBSTATE_DT2; continue; } else { if (last) last->next = kb; else keys_root = kb; last = kb; state = KBSTATE_MOD; } continue; } } if (last) last->next = NULL; free (ibuf); return 0; corrupt_file: /*if (getenv ("XCHAT_DEBUG")) abort ();*/ snprintf (ibuf, 1024, _("Key bindings config file is corrupt, load aborted\n" "Please fix %s/keybindings.conf\n"), get_xdir_utf8 ()); fe_message (ibuf, FE_MSG_ERROR); free (ibuf); return 5; }
int pevent_load (char *filename) { /* AGL, I've changed this file and pevent_save, could you please take a look at * the changes and possibly modify them to suit you * //David H */ char *buf, *ibuf; int fd, i = 0, pnt = 0; struct stat st; char *text = NULL, *snd = NULL; int penum = 0; char *ofs; if (filename == NULL) fd = hexchat_open_file ("pevents.conf", O_RDONLY, 0, 0); else fd = hexchat_open_file (filename, O_RDONLY, 0, XOF_FULLPATH); if (fd == -1) return 1; if (fstat (fd, &st) != 0) { close (fd); return 1; } ibuf = g_malloc (st.st_size); read (fd, ibuf, st.st_size); close (fd); while (buf_get_line (ibuf, &buf, &pnt, st.st_size)) { if (buf[0] == '#') continue; if (strlen (buf) == 0) continue; ofs = strchr (buf, '='); if (!ofs) continue; *ofs = 0; ofs++; if (strcmp (buf, "event_name") == 0) { if (penum >= 0) pevent_trigger_load (&penum, &text, &snd); penum = pevent_find (ofs, &i); continue; } else if (strcmp (buf, "event_text") == 0) { g_free (text); text = g_strdup (ofs); continue; } continue; } pevent_trigger_load (&penum, &text, &snd); g_free (ibuf); return 0; }
int udev_db_lookup_name(const char *name, char *devpath, size_t len) { char dbpath[PATH_MAX]; DIR *dir; int found = 0; strlcpy(dbpath, udev_root, sizeof(dbpath)); strlcat(dbpath, "/"DB_DIR, sizeof(dbpath)); dir = opendir(dbpath); if (dir == NULL) { info("no udev_db available '%s': %s", dbpath, strerror(errno)); return -1; } while (!found) { struct dirent *ent; char filename[PATH_SIZE]; char nodename[PATH_SIZE]; struct stat stats; char *bufline; char *buf; size_t bufsize; size_t cur; size_t count; ent = readdir(dir); if (ent == NULL || ent->d_name[0] == '\0') break; if (ent->d_name[0] == '.') continue; snprintf(filename, sizeof(filename), "%s/%s", dbpath, ent->d_name); filename[sizeof(filename)-1] = '\0'; dbg("looking at '%s'", filename); if (lstat(filename, &stats) != 0) { info("unable to read %s: %s", filename, strerror(errno)); continue; } if ((stats.st_mode & S_IFMT) == S_IFLNK) { char target[NAME_SIZE]; int target_len; info("found a symlink as db file"); target_len = readlink(filename, target, sizeof(target)); if (target_len > 0) target[target_len] = '\0'; else { info("error reading db link %s: %s", filename, strerror(errno)); return -1; } dbg("db link points to '%s'", target); if (strcmp(name, target) == 0) { db_file_to_devpath(ent->d_name, devpath, len); found =1; } continue; } if (file_map(filename, &buf, &bufsize) != 0) { info("unable to read db file '%s': %s", filename, strerror(errno)); continue; } cur = 0; while (cur < bufsize && !found) { count = buf_get_line(buf, bufsize, cur); bufline = &buf[cur]; cur += count+1; switch(bufline[0]) { case 'N': case 'S': if (count > sizeof(nodename)) count = sizeof(nodename); memcpy(nodename, &bufline[2], count-2); nodename[count-2] = '\0'; dbg("compare '%s' '%s'", nodename, name); if (strcmp(nodename, name) == 0) { db_file_to_devpath(ent->d_name, devpath, len); found = 1; } break; default: continue; } } file_unmap(buf, bufsize); } closedir(dir); if (found) return 0; else return -1; }
int udev_db_get_device(struct udevice *udev, const char *devpath) { struct stat stats; char filename[PATH_SIZE]; char line[PATH_SIZE]; unsigned int maj, min; char *bufline; char *buf; size_t bufsize; size_t cur; size_t count; strlcpy(udev->dev->devpath, devpath, sizeof(udev->dev->devpath)); devpath_to_db_path(devpath, filename, sizeof(filename)); if (lstat(filename, &stats) != 0) { info("no db file to read %s: %s", filename, strerror(errno)); return -1; } if ((stats.st_mode & S_IFMT) == S_IFLNK) { char target[NAME_SIZE]; int target_len; info("found a symlink as db file"); target_len = readlink(filename, target, sizeof(target)); if (target_len > 0) target[target_len] = '\0'; else { info("error reading db link %s: %s", filename, strerror(errno)); return -1; } dbg("db link points to '%s'", target); strlcpy(udev->name, target, sizeof(udev->name)); return 0; } if (file_map(filename, &buf, &bufsize) != 0) { info("error reading db file %s: %s", filename, strerror(errno)); return -1; } cur = 0; while (cur < bufsize) { count = buf_get_line(buf, bufsize, cur); bufline = &buf[cur]; cur += count+1; switch(bufline[0]) { case 'N': if (count > sizeof(udev->name)) count = sizeof(udev->name); memcpy(udev->name, &bufline[2], count-2); udev->name[count-2] = '\0'; break; case 'M': if (count > sizeof(line)) count = sizeof(line); memcpy(line, &bufline[2], count-2); line[count-2] = '\0'; sscanf(line, "%u:%u", &maj, &min); udev->devt = makedev(maj, min); break; case 'S': if (count > sizeof(line)) count = sizeof(line); memcpy(line, &bufline[2], count-2); line[count-2] = '\0'; name_list_add(&udev->symlink_list, line, 0); break; case 'A': if (count > sizeof(line)) count = sizeof(line); memcpy(line, &bufline[2], count-2); line[count-2] = '\0'; udev->partitions = atoi(line); break; case 'R': if (count > sizeof(line)) count = sizeof(line); memcpy(line, &bufline[2], count-2); line[count-2] = '\0'; udev->ignore_remove = atoi(line); break; case 'E': if (count > sizeof(line)) count = sizeof(line); memcpy(line, &bufline[2], count-2); line[count-2] = '\0'; name_list_add(&udev->env_list, line, 0); break; } } file_unmap(buf, bufsize); if (udev->name[0] == '\0') return -1; return 0; }
static int parse_config_file(void) { char line[LINE_SIZE]; char* bufline; char* linepos; char* variable; char* value; char* buf; size_t bufsize; size_t cur; size_t count; int lineno; int retval = 0; if (file_map(udev_config_filename, &buf, &bufsize) != 0) { err("can't open '%s' as config file: %s", udev_config_filename, strerror(errno)); return -ENODEV; } /* loop through the whole file */ lineno = 0; cur = 0; while (cur < bufsize) { count = buf_get_line(buf, bufsize, cur); bufline = &buf[cur]; cur += count + 1; lineno++; if (count >= sizeof(line)) { err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno); continue; } /* eat the whitespace */ while ((count > 0) && isspace(bufline[0])) { bufline++; count--; } if (count == 0) { continue; } /* see if this is a comment */ if (bufline[0] == COMMENT_CHARACTER) { continue; } memcpy(line, bufline, count); line[count] = '\0'; linepos = line; retval = get_key(&linepos, &variable, &value); if (retval != 0) { err("error parsing %s, line %d:%d", udev_config_filename, lineno, (int)(linepos - line)); continue; } if (strcasecmp(variable, "udev_root") == 0) { strlcpy(udev_root, value, sizeof(udev_root)); remove_trailing_chars(udev_root, '/'); continue; } if (strcasecmp(variable, "udev_rules") == 0) { strlcpy(udev_rules_dir, value, sizeof(udev_rules_dir)); remove_trailing_chars(udev_rules_dir, '/'); continue; } if (strcasecmp(variable, "udev_log") == 0) { udev_log_priority = log_priority(value); continue; } } file_unmap(buf, bufsize); return retval; }