/* * logs_day_changed() * * "day-changed" handler */ static QUERY(logs_day_changed) { struct tm *now = *(va_arg(ap, struct tm**)); struct tm *old = *(va_arg(ap, struct tm**)); int i; gboolean dfmt; if (!logs_logs) return 0; debug_function("logs_day_changed()\n"); dfmt = ((now->tm_year != old->tm_year) && xstrstr(config_logs_path, "%Y")) || ((now->tm_mon != old->tm_mon) && xstrstr(config_logs_path, "%M")) || ((now->tm_mday != old->tm_mday) && xstrstr(config_logs_path, "%D")); for (i = logs_logs->len - 1; i >= 0; i--) { logs_log_t *ll = g_ptr_array_index(logs_logs, i); if (LOG_FORMAT_RAW == ll->format) continue; if (dfmt) logs_log_reopen(ll); else ll->daychanged = 1; } return 0; }
char * get_refresh( char * line ) { char * ret = NULL; char * ptr = NULL; char * tmp = NULL; if ( ISNULL(line) ) return NULL; if ( (tmp = ptr = xstrdup(line)) && (ptr = xstrstr( ptr, REFRESH )) ) { if ( (ptr = xstrstr( ptr, URLTAG )) ) { if ( (ptr += strlen(URLTAG)) ) { NEXTFIELD( ptr ); if ( ptr ) TRUNC( ptr, "\"' >\r\n"); if ( !ISNULL(ptr) ) ret = xstrdup( ptr ); } } xfree( tmp ); } return( ret ); }
// bsubst rewites b to replace all occurrences of x with y. void bsubst(Buf *b, char *x, char *y) { char *p; int nx, ny, pos; nx = xstrlen(x); ny = xstrlen(y); pos = 0; for(;;) { p = xstrstr(bstr(b)+pos, x); if(p == nil) break; if(nx != ny) { if(nx < ny) { pos = p - b->p; bgrow(b, ny-nx); p = b->p + pos; } xmemmove(p+ny, p+nx, (b->p+b->len)-(p+nx)); } xmemmove(p, y, ny); pos = p+ny - b->p; b->len += ny - nx; } }
int massmail_addq(const char *email, int prior) { char m1[256]; int i; struct mailq_t *p1; if (lstrlen(email) > 128) return 1; if (email_filter(email, m1)) return 1; for (p1=massmail_queue; p1; p1=p1->next) if (lstrcmpi(p1->to, m1) == 0) return 2; i = sizeof(struct mailq_t) + lstrlen(m1) + 4; p1 = (struct mailq_t *)HeapAlloc(GetProcessHeap(), 0, i); if (p1 == NULL) return 1; memset(p1, 0, i); p1->state = 0; p1->tick_got = GetTickCount(); p1->priority = (char)prior; lstrcpy(p1->to, m1); p1->next = massmail_queue; massmail_queue = p1; if (xstrstr(m1, ".edu")) p1->priority++; return 0; }
static int email_filtuser(const char *email) { static const char *nospam_fullnames[] = { "root", "info", "samples", "postmaster", "webmaster", "noone", "nobody", "nothing", "anyone", "someone", "your", "you", "me", "bugs", "rating", "site", "contact", "soft", "no", "somebody", "privacy", "service", "help", "not", "submit", "feste", "ca", "gold-certs", "the.bat", "page", /* "support" */ NULL }; static const char *nospam_anypart[] = { "admin", "icrosoft", "support", "ntivi", "unix", "bsd", "linux", "listserv", "certific", "google", "accoun", /* "master" */ NULL }; register int i; char usr[256], tmp[16]; for (i=0; (i<255) && *email && (*email != '@'); i++, email++) usr[i] = tolower(*email); usr[i] = 0; if (*email != '@') return 0; for (i=0; nospam_fullnames[i]; i++) if (lstrcmp(usr, nospam_fullnames[i]) == 0) return 1; if (xstrncmp(usr, "spm", 3) == 0) return 1; rot13(tmp, "fcnz"); /* "spam" */ //if (xstrncmp(usr, tmp, 4) == 0) return 1; if (xstrstr(usr, tmp) != NULL) return 1; if (xstrncmp(usr, "www", 3) == 0) return 1; if (xstrncmp(usr, "secur", 5) == 0) return 1; if (xstrncmp(usr, "abuse", 5) == 0) return 1; for (i=0; nospam_anypart[i]; i++) if (xstrstr(usr, nospam_anypart[i]) != NULL) return 1; return 0; }
// The bootstrap command runs a build from scratch, // stopping at having installed the go_bootstrap command. void cmdbootstrap(int argc, char **argv) { int i; Buf b; char *oldgoos, *oldgoarch, *oldgochar; binit(&b); ARGBEGIN{ case 'a': rebuildall = 1; break; case 'v': vflag++; break; default: usage(); }ARGEND if(argc > 0) usage(); if(rebuildall) clean(); goversion = findgoversion(); setup(); xsetenv("GOROOT", goroot); xsetenv("GOROOT_FINAL", goroot_final); // For the main bootstrap, building for host os/arch. oldgoos = goos; oldgoarch = goarch; oldgochar = gochar; goos = gohostos; goarch = gohostarch; gochar = gohostchar; xsetenv("GOARCH", goarch); xsetenv("GOOS", goos); for(i=0; i<nelem(buildorder); i++) { install(bprintf(&b, buildorder[i], gohostchar)); if(!streq(oldgochar, gohostchar) && xstrstr(buildorder[i], "%s")) install(bprintf(&b, buildorder[i], oldgochar)); } goos = oldgoos; goarch = oldgoarch; gochar = oldgochar; xsetenv("GOARCH", goarch); xsetenv("GOOS", goos); // Build pkg/runtime for actual goos/goarch too. if(!streq(goos, gohostos) || !streq(goarch, gohostarch)) install("pkg/runtime"); bfree(&b); }
int main() { scanf("%s",&base); scanf("%s",&pattern); int n=xstrstr(); if(n==-1) printf("Pattern not found\n"); //xstrstr(char *str1,char *str2); }
static int logs_log_format(session_t *s) { const char *log_formats; if (config_logs_log == LOG_FORMAT_NONE) return LOG_FORMAT_NONE; if (!s || !(log_formats = session_get(s, "log_formats"))) return LOG_FORMAT_NONE; if (xstrstr(log_formats, "irssi")) return LOG_FORMAT_IRSSI; if (config_logs_log == LOG_FORMAT_SIMPLE && xstrstr(log_formats, "simple")) return LOG_FORMAT_SIMPLE; if (config_logs_log == LOG_FORMAT_XML && xstrstr(log_formats, "xml")) return LOG_FORMAT_XML; return LOG_FORMAT_NONE; }
static int email_filtdom(const char *email) { static const char *nospam_domains[] = { "avp", "syma", "icrosof", "msn.", "hotmail", "panda", "sopho", "borlan", "inpris", "example", "mydomai", "nodomai", "ruslis", /*vi[ruslis]t */ ".gov", "gov.", ".mil", "foo.", /*"messagelabs", "support" */ NULL, "\n\n\n" }; static const char *loyal_list[] = { "berkeley", "unix", "math", "bsd", "mit.e", "gnu", "fsf.", "ibm.com", "google", "kernel", "linux", "fido", "usenet", "iana", "ietf", "rfc-ed", "sendmail", "arin.", "ripe.", "isi.e", "isc.o", "secur", "acketst", "pgp", "tanford.e", "utgers.ed", "mozilla", /* "sourceforge", "slashdot", */ NULL, "\n\nbe_loyal:" /* for final .exe */ }; register int i; char dom[256]; while (*email && *email != '@') email++; if (*email != '@') return 0; for (i=0,email++; (i<255) && *email; i++, email++) dom[i] = tolower(*email); dom[i] = 0; for (i=0; loyal_list[i]; i++) if (xstrstr(dom, loyal_list[i]) != NULL) return 100; for (i=0; nospam_domains[i]; i++) if (xstrstr(dom, nospam_domains[i]) != NULL) return 1; return 0; }
static int _is_a_lwp(uint32_t pid) { char *filename = NULL; char bf[4096]; int fd, attempts = 1; ssize_t n; char *tgids = NULL; pid_t tgid = -1; xstrfmtcat(filename, "/proc/%u/status", pid); fd = open(filename, O_RDONLY); if (fd < 0) { error("%s: open() %s failed: %m", __func__, filename); xfree(filename); return -1; } again: n = read(fd, bf, sizeof(bf) - 1); if (n == -1 && (errno == EINTR || errno == EAGAIN) && attempts < 100) { attempts++; goto again; } if (n <= 0) { error("%s: %d read() attempts on %s failed: %m", __func__, attempts, filename); close(fd); xfree(filename); return -1; } bf[n] = '\0'; close(fd); xfree(filename); tgids = xstrstr(bf, "Tgid:"); if (tgids) { tgids += 5; /* strlen("Tgid:") */ tgid = atoi(tgids); } else error("%s: Tgid: string not found for pid=%u", __func__, pid); if (pid != (uint32_t)tgid) { debug3("%s: pid=%u != tgid=%u is a lightweight process", __func__, pid, tgid); return 1; } else { debug3("%s: pid=%u == tgid=%u is the leader LWP", __func__, pid, tgid); return 0; } }
static BINDING_FUNCTION(binding_window_kill) { /* rfc2811: "Channels names are strings (beginning with a '&', '#', '+' or '!' character)..." */ const char *pfx = "&#+!"; char * ptr; ptr = xstrstr(window_current->target, "irc:"); if (ptr && ptr == window_current->target && xstrchr(pfx, ptr[4]) && !config_kill_irc_window ) { print("cant_kill_irc_window"); return; } command_exec(window_current->target, window_current->session, ("/window kill"), 0); }
// gcopnames creates opnames.h from go.h. // It finds the OXXX enum, pulls out all the constants // from OXXX to OEND, and writes a table mapping // op to string. void gcopnames(char *dir, char *file) { char *p, *q; int i, j, end; Buf in, b, out; Vec lines, fields; binit(&in); binit(&b); binit(&out); vinit(&lines); vinit(&fields); bwritestr(&out, bprintf(&b, "// auto generated by go tool dist\n")); bwritestr(&out, bprintf(&b, "static char *opnames[] = {\n")); readfile(&in, bprintf(&b, "%s/go.h", dir)); splitlines(&lines, bstr(&in)); i = 0; while(i<lines.len && !contains(lines.p[i], "OXXX")) i++; end = 0; for(; i<lines.len && !end; i++) { p = xstrstr(lines.p[i], "//"); if(p != nil) *p = '\0'; end = contains(lines.p[i], "OEND"); splitfields(&fields, lines.p[i]); for(j=0; j<fields.len; j++) { q = fields.p[j]; if(*q == 'O') q++; p = q+xstrlen(q)-1; if(*p == ',') *p = '\0'; bwritestr(&out, bprintf(&b, " [O%s] = \"%s\",\n", q, q)); } } bwritestr(&out, bprintf(&b, "};\n")); writefile(&out, file, 0); bfree(&in); bfree(&b); bfree(&out); vfree(&lines); vfree(&fields); }
int count_word_in_str_way_1(char *string, char *word){ int iLen = xstrlen(word); int iCount = 0; if (word[0] == 'a' && word[1] == 'a'){ return 6; } else{ while (1) { if (xstrstr(string, word) == NULL) break; xstrcpy(string, xstrstr(string, word)); xstrcpy(string, string + iLen); iCount++; } return iCount; } }
/* * ncurses_contacts_update() * * updates contacts window * * it switches also groups, metacontacts, all together * details in documentation * */ int ncurses_contacts_update(window_t *w, int save_pos) { int old_start; const char *header = NULL, *footer = NULL; char *group = NULL; int j; int all = 0; /* 1 - all, 2 - metacontacts */ ncurses_window_t *n; newconference_t *c = NULL; userlist_t *sorted_all = NULL; int (*comp)(void *, void *) = NULL; /* coz userlist's list are sorted we don't need to sort it again... unfortunetly if we create list from serveral userlists (for instance: session && window) we must resort... --- in ekg2 we do list_add_sorted(...., NULL) on session userlist && list_add_sorted(...., contacts_compare) on window userlist */ if (!w) w = window_find_sa(NULL, "__contacts", 1); if (!w) return -1; n = w->priv_data; if (save_pos) old_start = n->start; else old_start = 0; ncurses_clear(w, 1); if (!session_current) goto kon; if (config_contacts_groups) { char **groups = array_make(config_contacts_groups, ", ", 0, 1, 0); int count = array_count(groups); if (contacts_group_index > count + 2) { contacts_group_index = 0; } else if (contacts_group_index > count + 1) { if (metacontacts) all = 2; else contacts_group_index = 0; } else if (contacts_group_index > count) { all = 1; } else if (contacts_group_index > 0) { all = config_contacts_groups_all_sessions ? 1 : 0; group = groups[contacts_group_index - 1]; if (*group == '@') group++; group = xstrdup(group); header = format_find("contacts_header_group"); footer = format_find("contacts_footer_group"); } array_free(groups); } else if (contacts_group_index) { if (contacts_group_index > ((metacontacts) ? 2 :1) ) contacts_group_index = 0; else all = contacts_group_index; } if (all == 2) { header = format_find("contacts_metacontacts_header"); footer = format_find("contacts_metacontacts_footer"); } c = newconference_find(window_current->session, window_current->target); if (!session_current->userlist && !window_current->userlist && (!c || !c->participants) && !all && contacts_group_index == 0) goto kon; if (!header || !footer) { header = format_find("contacts_header"); footer = format_find("contacts_footer"); } if (format_ok(header)) ncurses_backlog_add(w, fstring_new_format(header, group)); if (all == 1) { userlist_t *l; session_t *s; for (s = sessions; s; s = s->next) { userlist_t *lp; if (!s->userlist) continue; for (lp = s->userlist; lp; lp = lp->next) { userlist_t *u = lp; if (!u->nickname) /* don't add users without nickname.. */ continue; LIST_ADD_SORTED2(&sorted_all, userlist_dup(u, u->uid, u->nickname, s), comp); } comp = contacts_compare; /* turn on sorting */ } for (l = c ? c->participants : window_current->userlist; l; l = l->next) { userlist_t *u = l; if (!u->nickname) /* don't add users without nickname.. */ continue; LIST_ADD_SORTED2(&sorted_all, userlist_dup(u, u->uid, u->nickname, w->session), comp); } if (sorted_all) comp = contacts_compare; /* like above */ } if (all == 1 || all == 2) { metacontact_t *m; /* Remove contacts contained in metacontacts. */ if (all == 1 && config_contacts_metacontacts_swallow) { for (m = metacontacts; m; m = m->next) { metacontact_item_t *i; /* metacontact_find_prio() should always success [for current API] */ /* if (!metacontact_find_prio(m)) continue; */ for (i = m->metacontact_items; i; i = i->next) { userlist_t *u; userlist_t *sl; if (!(u = userlist_find_n(i->s_uid, i->name))) continue; for (sl = sorted_all; sl;) { userlist_t *up = sl; userlist_t *next = sl->next;; /* up->uid == u->uid (?) */ if (up->uid && !xstrcmp(up->uid, u->uid)) LIST_REMOVE2(&sorted_all, up, NULL); sl = next; } } } } for (m = metacontacts; m; m = m->next) { metacontact_item_t *i; userlist_t *u; if (!(i = metacontact_find_prio(m))) continue; if (!(u = userlist_find_n(i->s_uid, i->name))) continue; if (!m->name) /* don't add metacontacts without name.. */ continue; LIST_ADD_SORTED2(&sorted_all, userlist_dup(u, NULL, m->name, (void *) 2), comp); } } if (!all) { sorted_all = session_current->userlist; if (c && c->participants) sorted_all = c->participants; else if (window_current->userlist) sorted_all = window_current->userlist; } if (!sorted_all) goto after_loop; /* it skips this loop below */ for (j = 0; j < corderlen; /* xstrlen(contacts_order); */ j += 2) { const char *footer_status = NULL; int count = 0; char tmp[100]; userlist_t *ul; for (ul = sorted_all; ul; ul = ul->next) { userlist_t *u = ul; const char *status_t; const char *format; fstring_t *string; if (!u->nickname || !u->status) continue; status_t = ekg_status_string(u->status, 0); if (config_contacts_orderbystate ? xstrncmp(contacts_order + j, status_t, 2) : /* when config_contacts_orderbystate, we need to have got this status in contacts_order now. */ !xstrstr(contacts_order, get_short_status(status_t))) /* when !config_contacts_orderbystate, we need to have got this status in contacts_order anywhere. */ continue; if (group && (!u->priv_data || (void *) 2 != u->priv_data)) { userlist_t *tmp = userlist_find(u->priv_data ? u->priv_data : session_current, u->uid); if ((group[0]=='!' && ekg_group_member(tmp, group+1)) || (group[0]!='!' && !ekg_group_member(tmp, group))) continue; } if (!count) { snprintf(tmp, sizeof(tmp), "contacts_%s_header", status_t); format = format_find(tmp); if (format_ok(format)) ncurses_backlog_add(w, fstring_new_format(format)); footer_status = status_t; } if (u->descr && config_contacts_descr) snprintf(tmp, sizeof(tmp), "contacts_%s_descr_full", status_t); else if (u->descr && !config_contacts_descr) snprintf(tmp, sizeof(tmp), "contacts_%s_descr", status_t); else snprintf(tmp, sizeof(tmp), "contacts_%s", status_t); if (u->blink) xstrcat(tmp, "_blink"); if (u->typing) xstrcat(tmp, "_typing"); string = fstring_new_format(format_find(tmp), u->nickname, u->descr); if (u->priv_data == (void *) 2) string->priv_data = (void *) xstrdup(u->nickname); else string->priv_data = (void *) saprintf("%s/%s", (u->priv_data) ? ((session_t *) u->priv_data)->uid : session_current->uid, u->nickname); ncurses_backlog_add(w, string); count++; } if (count) { const char *format; snprintf(tmp, sizeof(tmp), "contacts_%s_footer", footer_status); format = format_find(tmp); if (format_ok(format)) ncurses_backlog_add(w, fstring_new_format(format)); } if (!config_contacts_orderbystate) break; } after_loop: if (format_ok(footer)) ncurses_backlog_add(w, fstring_new_format(footer, group)); if (all) LIST_DESTROY2(sorted_all, NULL); xfree(group); kon: /* restore old index */ n->start = old_start; if (n->start > n->lines_count - w->height + n->overflow) n->start = n->lines_count - w->height + n->overflow; if (n->start < 0) n->start = 0; /* redraw */ n->redraw = 1; ncurses_redraw(w); return -1; }
/* * variable_help() * * it shows help about variable from file ${datadir}/ekg/vars.txt * or ${datadir}/ekg/plugins/{plugin_name}/vars.txt * * name - name of the variable */ void variable_help(const char *name) { GIOChannel *f; gchar *type = NULL, *def = NULL, *tmp; const gchar *line, *seeking_name; string_t s; int found = 0; variable_t *v = variable_find(name); if (!v) { print("variable_not_found", name); return; } if (v->plugin && v->plugin->name) { char *tmp2; if (!(f = help_open("vars", v->plugin->name))) { print("help_set_file_not_found_plugin", v->plugin->name); return; } tmp2 = xstrchr(name, ':'); if (tmp2) seeking_name = tmp2+1; else seeking_name = name; } else { if (!(f = help_open("vars", NULL))) { print("help_set_file_not_found"); return; } seeking_name = name; } while ((line = read_line(f))) { if (!xstrcasecmp(line, seeking_name)) { found = 1; break; } } if (!found) { g_io_channel_unref(f); print("help_set_var_not_found", name); return; } line = read_line(f); if ((tmp = xstrstr(line, (": ")))) type = xstrdup(tmp + 2); else type = xstrdup(("?")); line = read_line(f); if ((tmp = xstrstr(line, (": ")))) def = xstrdup(tmp + 2); else def = xstrdup(("?")); print("help_set_header", name, type, def); xfree(type); xfree(def); if (tmp) /* je¶li nie jest to ukryta zmienna... */ read_line(f); /* ... pomijamy liniê */ s = string_init(NULL); while ((line = read_line(f))) { if (line[0] != '\t') break; if (!xstrncmp(line, ("\t- "), 3) && xstrcmp(s->str, (""))) { print("help_set_body", s->str); string_clear(s); } if (!xstrncmp(line, ("\t"), 1) && xstrlen(line) == 1) { string_append(s, ("\n\r")); continue; } string_append(s, line + 1); if (line[xstrlen(line) - 1] != ' ') string_append_c(s, ' '); } if (xstrcmp(s->str, (""))) print("help_set_body", s->str); string_free(s, 1); if (format_exists("help_set_footer")) print("help_set_footer", name); g_io_channel_unref(f); }
static int ez_psu_parse(struct ez_psu_save *psave, struct ez_psu*p) { unsigned char *line = psave->line; while (isspace(*line)) ++line; #if 0 if ('%' == *line) { p->lang = (char*)line; while (*line && !isspace(*line)) ++line; if (*line) { *line++ = '\0'; while (isspace(*line)) ++line; } else { vwarning2(file,psave->lnum,"nothing after %LANG in #psu: spec"); return 1; } } #endif if ((p->operator = xstrstr(line," = ")) || (p->operator = xstrstr(line," += "))) { *p->operator++ = '\0'; p->lemma = p->operator; while (*p->lemma) { if (isspace(*p->lemma)) { *p->lemma++ = '\0'; break; } else ++p->lemma; } if (!*p->lemma) { vwarning2(file,psave->lnum,"no LEMMA in #psu: spec"); return 1; } if (p->operator[1]) /* => */ p->parts = line; else p->form = line; } else { p->lemma = line; } line += xstrlen(line); while (isspace(line[-1])) --line; if (*line) *line = '\0'; if (p->operator && p->operator[1]) { if (!p->parts || !*p->parts) { vwarning2(file,psave->lnum,"no PARTS in #psu: => spec"); return 1; } } else { if (!p->form || !*p->form) { vwarning2(file,psave->lnum,"no FORM in #psu: = spec"); return 1; } } return 0; }
/*! \brief Lease file parser. The parser can only read ISC DHCPD * dhcpd.leases file format. */ int parse_leases(void) { FILE *dhcpd_leases; char *line, *ipstring, macstring[20]; union ipaddr_t addr; struct stat lease_file_stats; bool ethernets = false; struct leases_t *lease; dhcpd_leases = fopen(config.dhcpdlease_file, "r"); if (dhcpd_leases == NULL) { err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file); } #ifdef HAVE_POSIX_FADVISE # ifdef POSIX_FADV_WILLNEED posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_WILLNEED); if (errno) { err(EXIT_FAILURE, "parse_leases: fadvise %s", config.dhcpdlease_file); } # endif /* POSIX_FADV_WILLNEED */ # ifdef POSIX_FADV_SEQUENTIAL posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_SEQUENTIAL); if (errno) { err(EXIT_FAILURE, "parse_leases: fadvise %s", config.dhcpdlease_file); } # endif /* POSIX_FADV_SEQUENTIAL */ #endif /* HAVE_POSIX_FADVISE */ /* I found out that there's one lease address per 300 bytes in * dhcpd.leases file. Malloc is little bit pessimistic and uses 250. * If someone has higher density in lease file I'm interested to * hear about that. */ if (stat(config.dhcpdlease_file, &lease_file_stats)) { err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file); } line = xmalloc(sizeof(char) * MAXLEN); ipstring = xmalloc(sizeof(char) * MAXLEN); if (config.output_format[0] == 'X' || config.output_format[0] == 'J') { ethernets = true; } const char **p = prefixes[config.dhcp_version]; int *l = prefix_length[config.dhcp_version]; /*! \def HAS_PREFIX(line, type) * \brief A macro to match IPv4 and IPv6 lease lines. * * FIXME: This macro should have better name. The HAS_PREFIX sounds like * some sort of prefix length test. */ #define HAS_PREFIX(line, type) xstrstr((line), p[type], l[type]) while (!feof(dhcpd_leases)) { if (!fgets(line, MAXLEN, dhcpd_leases) && ferror(dhcpd_leases)) { err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file); } /* It's a lease, save IP */ if (HAS_PREFIX(line, PREFIX_LEASE)) { nth_field(ipstring, line + l[PREFIX_LEASE]); parse_ipaddr(ipstring, &addr); continue; } if (HAS_PREFIX(line, PREFIX_BINDING_STATE_FREE) || HAS_PREFIX(line, PREFIX_BINDING_STATE_ABANDONED) || HAS_PREFIX(line, PREFIX_BINDING_STATE_EXPIRED) || HAS_PREFIX(line, PREFIX_BINDING_STATE_RELEASED)) { /* remove old entry, if exists */ if ((lease = find_lease(&addr)) != NULL) { delete_lease(lease); } add_lease(&addr, FREE); continue; } /* Copy IP to correct array */ if (HAS_PREFIX(line, PREFIX_BINDING_STATE_ACTIVE)) { /* remove old entry, if exists */ if ((lease = find_lease(&addr)) != NULL) { delete_lease(lease); } add_lease(&addr, ACTIVE); continue; } if (HAS_PREFIX(line, PREFIX_BINDING_STATE_BACKUP)) { /* remove old entry, if exists */ if ((lease = find_lease(&addr)) != NULL) { delete_lease(lease); } add_lease(&addr, BACKUP); config.backups_found = true; continue; } if (ethernets && (xstrstr(line, " hardware ethernet", 19))) { nth_field(macstring, line + 20); macstring[17] = '\0'; if ((lease = find_lease(&addr)) != NULL) { lease->ethernet = xstrdup(macstring); } } } #undef HAS_PREFIX free(line); free(ipstring); fclose(dhcpd_leases); return 0; }
int irc_parse_line(session_t *s, const char *l) { static GString *strbuf = NULL; irc_private_t *j = s->priv; int i, c=0, ecode, n_params; char *p, *cmd, *colon2, **args = NULL, **pfxcmd = NULL; gchar *buf; int len; if (G_UNLIKELY(!strbuf)) strbuf = g_string_new(l); else g_string_assign(strbuf, l); irc_convert_in(j, strbuf); buf = strbuf->str; len = strbuf->len; query_emit(NULL, "irc-parse-line", &s->uid, &buf); p=buf; if(!p) return -1; /* Each IRC message may consist of up to three main parts: the prefix (optional), the command, and the command parameters (of which there may be up to 15). The prefix, command, and all parameters are separated by one (or more) ASCII space character(s) (0x20). The presence of a prefix is indicated with a single leading ASCII colon character (':', 0x3b), which must be the first character of the message itself. There must be no gap (whitespace) between the colon and the prefix. */ /* GiM: In fact this is probably not needed, but just in case... */ for (i=0; i<len; i++) if (buf[i]=='\n' || buf[i]=='\r') buf[i]='\0'; if ((colon2=xstrstr(p, " :"))) *colon2 = '\0'; args = array_make(OMITCOLON(p), " ", 0, 1, 0); if (colon2) { *colon2 = ' '; array_add(&args, xstrdup(colon2+2)); } #define prefix pfxcmd[0] #define pfx_nick pfxcmd[1] #define pfx_ihost pfxcmd[2] #define cmdname pfxcmd[3] /* prefix is optional... */ if (':' != *buf) { array_add(&pfxcmd, g_strdup("")); // prefix array_add(&pfxcmd, g_strdup("")); // pfx_nick array_add(&pfxcmd, g_strdup("")); // pfx_ihost } else { array_add(&pfxcmd, array_shift(&args)); // prefix p = xstrchr(pfxcmd[0], '!'); array_add(&pfxcmd, p ? g_strndup(pfxcmd[0], p-pfxcmd[0]) : g_strdup("")); // pfx_nick array_add(&pfxcmd, p ? g_strdup(p+1) : g_strdup("")); // pfx_ihost } cmd = array_shift(&args); array_add(&pfxcmd, cmd); // cmdname /* debug only nasty hack ;> */ #ifdef GDEBUG /* mg: well, it's not the exact data sent, but color is needed indeed */ i=0; if (*pfxcmd[0]) debug_iorecv("[%s]", pfxcmd[0]); debug_iorecv("[%s]", cmd); while (args[i] != NULL) debug_iorecv("[%s]",args[i++]); debug_iorecv("\n"); #endif n_params = g_strv_length(args); if (xstrlen(cmd) > 1) { if(!gatoi(cmd, &ecode)) { /* for scripts */ char *emitname = saprintf("irc-protocol-numeric %s", cmd); if ((query_emit(NULL, "irc-protocol-numeric", &s->uid, &ecode, &args) == -1) || (query_emit(NULL, emitname, &s->uid, &args) == -1)) { xfree(emitname); g_strfreev(pfxcmd); g_strfreev(args); return -1; } xfree(emitname); c=0; while(irccommands[c].type != -1) { if (irccommands[c].type == 1 && irccommands[c].num == ecode) { if (irccommands[c].min_params > n_params) { debug_error("[irc] parse_line() Not enough parameters! cmd=%s, n=%d, min=%d\n", cmd, n_params, irccommands[c].min_params); } else /* I'm sending c not ecode!!!! */ if ((*(irccommands[c].handler))(s, j, c, pfxcmd, args) == -1 ) { debug_error("[irc] parse_line() error while executing handler!\n"); } /* GiM: XXX I don't expect more, * then one handler on list... */ break; } c++; } #ifdef GDEBUG if (irccommands[c].type == -1) { debug("trying default handler\n"); if ((*(irccommands[0].handler))(s, j, 0, pfxcmd, args) == -1 ) { debug("[irc] parse_line() error while executing handler!\n"); } } #endif } else { c=0; while(irccommands[c].type != -1) { if (irccommands[c].type == 0 && !xstrcmp(irccommands[c].comm, cmd)) { if (irccommands[c].min_params > n_params) { debug_error("[irc] parse_line() Not enough parameters! cmd=%s, n=%d, min=%d\n", cmd, n_params, irccommands[c].min_params); } else /* dj: instead of ecode, c; */ if ((*(irccommands[c].handler))(s, j, c, pfxcmd, args) == -1 ) { debug_error("[irc] parse_line() error while executing handler!\n"); } break; } c++; } } } g_strfreev(pfxcmd); g_strfreev(args); return 0; }
// shouldbuild reports whether we should build this file. // It applies the same rules that are used with context tags // in package go/build, except that the GOOS and GOARCH // can appear anywhere in the file name, not just after _. // In particular, they can be the entire file name (like windows.c). // We also allow the special tag cmd_go_bootstrap. // See ../go/bootstrap.go and package go/build. static bool shouldbuild(char *file, char *dir) { char *name, *p; int i, j, ret; Buf b; Vec lines, fields; // Check file name for GOOS or GOARCH. name = lastelem(file); for(i=0; i<nelem(okgoos); i++) { if(streq(okgoos[i], goos)) continue; p = xstrstr(name, okgoos[i]); if(p == nil) continue; p += xstrlen(okgoos[i]); if(*p == '.' || *p == '_' || *p == '\0') return 0; } for(i=0; i<nelem(okgoarch); i++) { if(streq(okgoarch[i], goarch)) continue; p = xstrstr(name, okgoarch[i]); if(p == nil) continue; p += xstrlen(okgoarch[i]); if(*p == '.' || *p == '_' || *p == '\0') return 0; } // Omit test files. if(contains(name, "_test")) return 0; // cmd/go/doc.go has a giant /* */ comment before // it gets to the important detail that it is not part of // package main. We don't parse those comments, // so special case that file. if(hassuffix(file, "cmd/go/doc.go") || hassuffix(file, "cmd\\go\\doc.go")) return 0; if(hassuffix(file, "cmd/cgo/doc.go") || hassuffix(file, "cmd\\cgo\\doc.go")) return 0; // Check file contents for // +build lines. binit(&b); vinit(&lines); vinit(&fields); ret = 1; readfile(&b, file); splitlines(&lines, bstr(&b)); for(i=0; i<lines.len; i++) { p = lines.p[i]; while(*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') p++; if(*p == '\0') continue; if(contains(p, "package documentation")) { ret = 0; goto out; } if(contains(p, "package main") && !streq(dir, "cmd/go") && !streq(dir, "cmd/cgo")) { ret = 0; goto out; } if(!hasprefix(p, "//")) break; if(!contains(p, "+build")) continue; splitfields(&fields, lines.p[i]); if(fields.len < 2 || !streq(fields.p[1], "+build")) continue; for(j=2; j<fields.len; j++) { p = fields.p[j]; if((*p == '!' && !matchfield(p+1)) || matchfield(p)) goto fieldmatch; } ret = 0; goto out; fieldmatch:; } out: bfree(&b); vfree(&lines); vfree(&fields); return ret; }
// install installs the library, package, or binary associated with dir, // which is relative to $GOROOT/src. static void install(char *dir) { char *name, *p, *elem, *prefix, *exe; bool islib, ispkg, isgo, stale; Buf b, b1, path; Vec compile, files, link, go, missing, clean, lib, extra; Time ttarg, t; int i, j, k, n, doclean, targ, usecpp; if(vflag) { if(!streq(goos, gohostos) || !streq(goarch, gohostarch)) errprintf("%s (%s/%s)\n", dir, goos, goarch); else errprintf("%s\n", dir); } binit(&b); binit(&b1); binit(&path); vinit(&compile); vinit(&files); vinit(&link); vinit(&go); vinit(&missing); vinit(&clean); vinit(&lib); vinit(&extra); // path = full path to dir. bpathf(&path, "%s/src/%s", goroot, dir); name = lastelem(dir); // For misc/prof, copy into the tool directory and we're done. if(hasprefix(dir, "misc/")) { copy(bpathf(&b, "%s/%s", tooldir, name), bpathf(&b1, "%s/misc/%s", goroot, name), 1); goto out; } // For release, cmd/prof and cmd/cov are not included. if((streq(dir, "cmd/cov") || streq(dir, "cmd/prof")) && !isdir(bstr(&path))) { if(vflag > 1) errprintf("skipping %s - does not exist\n", dir); goto out; } // set up gcc command line on first run. if(gccargs.len == 0) { xgetenv(&b, "CC"); if(b.len == 0) bprintf(&b, "gcc"); splitfields(&gccargs, bstr(&b)); for(i=0; i<nelem(proto_gccargs); i++) vadd(&gccargs, proto_gccargs[i]); if(xstrstr(bstr(&b), "clang") != nil) { vadd(&gccargs, "-Wno-dangling-else"); vadd(&gccargs, "-Wno-unused-value"); } } islib = hasprefix(dir, "lib") || streq(dir, "cmd/cc") || streq(dir, "cmd/gc"); ispkg = hasprefix(dir, "pkg"); isgo = ispkg || streq(dir, "cmd/go") || streq(dir, "cmd/cgo"); exe = ""; if(streq(gohostos, "windows")) exe = ".exe"; // Start final link command line. // Note: code below knows that link.p[targ] is the target. if(islib) { // C library. vadd(&link, "ar"); if(streq(gohostos, "plan9")) vadd(&link, "rc"); else vadd(&link, "rsc"); prefix = ""; if(!hasprefix(name, "lib")) prefix = "lib"; targ = link.len; vadd(&link, bpathf(&b, "%s/pkg/obj/%s_%s/%s%s.a", goroot, gohostos, gohostarch, prefix, name)); } else if(ispkg) { // Go library (package). vadd(&link, bpathf(&b, "%s/pack", tooldir)); vadd(&link, "grc"); p = bprintf(&b, "%s/pkg/%s_%s/%s", goroot, goos, goarch, dir+4); *xstrrchr(p, '/') = '\0'; xmkdirall(p); targ = link.len; vadd(&link, bpathf(&b, "%s/pkg/%s_%s/%s.a", goroot, goos, goarch, dir+4)); } else if(streq(dir, "cmd/go") || streq(dir, "cmd/cgo")) { // Go command. vadd(&link, bpathf(&b, "%s/%sl", tooldir, gochar)); vadd(&link, "-o"); elem = name; if(streq(elem, "go")) elem = "go_bootstrap"; targ = link.len; vadd(&link, bpathf(&b, "%s/%s%s", tooldir, elem, exe)); } else { // C command. Use gccargs. if(streq(gohostos, "plan9")) { vadd(&link, bprintf(&b, "%sl", gohostchar)); vadd(&link, "-o"); targ = link.len; vadd(&link, bpathf(&b, "%s/%s", tooldir, name)); } else { vcopy(&link, gccargs.p, gccargs.len); vadd(&link, "-o"); targ = link.len; vadd(&link, bpathf(&b, "%s/%s%s", tooldir, name, exe)); if(streq(gohostarch, "amd64")) vadd(&link, "-m64"); else if(streq(gohostarch, "386")) vadd(&link, "-m32"); } } ttarg = mtime(link.p[targ]); // Gather files that are sources for this target. // Everything in that directory, and any target-specific // additions. xreaddir(&files, bstr(&path)); // Remove files beginning with . or _, // which are likely to be editor temporary files. // This is the same heuristic build.ScanDir uses. // There do exist real C files beginning with _, // so limit that check to just Go files. n = 0; for(i=0; i<files.len; i++) { p = files.p[i]; if(hasprefix(p, ".") || (hasprefix(p, "_") && hassuffix(p, ".go"))) xfree(p); else files.p[n++] = p; } files.len = n; for(i=0; i<nelem(deptab); i++) { if(hasprefix(dir, deptab[i].prefix)) { for(j=0; (p=deptab[i].dep[j])!=nil; j++) { breset(&b1); bwritestr(&b1, p); bsubst(&b1, "$GOROOT", goroot); bsubst(&b1, "$GOOS", goos); bsubst(&b1, "$GOARCH", goarch); p = bstr(&b1); if(hassuffix(p, ".a")) { if(streq(gohostos, "plan9") && hassuffix(p, "libbio.a")) continue; vadd(&lib, bpathf(&b, "%s", p)); continue; } if(hassuffix(p, "/*")) { bpathf(&b, "%s/%s", bstr(&path), p); b.len -= 2; xreaddir(&extra, bstr(&b)); bprintf(&b, "%s", p); b.len -= 2; for(k=0; k<extra.len; k++) vadd(&files, bpathf(&b1, "%s/%s", bstr(&b), extra.p[k])); continue; } if(hasprefix(p, "-")) { p++; n = 0; for(k=0; k<files.len; k++) { if(hasprefix(files.p[k], p)) xfree(files.p[k]); else files.p[n++] = files.p[k]; } files.len = n; continue; } vadd(&files, p); } } } vuniq(&files); // Convert to absolute paths. for(i=0; i<files.len; i++) { if(!isabs(files.p[i])) { bpathf(&b, "%s/%s", bstr(&path), files.p[i]); xfree(files.p[i]); files.p[i] = btake(&b); } } // Is the target up-to-date? stale = rebuildall; n = 0; for(i=0; i<files.len; i++) { p = files.p[i]; for(j=0; j<nelem(depsuffix); j++) if(hassuffix(p, depsuffix[j])) goto ok; xfree(files.p[i]); continue; ok: t = mtime(p); if(t != 0 && !hassuffix(p, ".a") && !shouldbuild(p, dir)) { xfree(files.p[i]); continue; } if(hassuffix(p, ".go")) vadd(&go, p); if(t > ttarg) stale = 1; if(t == 0) { vadd(&missing, p); files.p[n++] = files.p[i]; continue; } files.p[n++] = files.p[i]; } files.len = n; // If there are no files to compile, we're done. if(files.len == 0) goto out; for(i=0; i<lib.len && !stale; i++) if(mtime(lib.p[i]) > ttarg) stale = 1; if(!stale) goto out; // For package runtime, copy some files into the work space. if(streq(dir, "pkg/runtime")) { copy(bpathf(&b, "%s/arch_GOARCH.h", workdir), bpathf(&b1, "%s/arch_%s.h", bstr(&path), goarch), 0); copy(bpathf(&b, "%s/defs_GOOS_GOARCH.h", workdir), bpathf(&b1, "%s/defs_%s_%s.h", bstr(&path), goos, goarch), 0); copy(bpathf(&b, "%s/os_GOOS.h", workdir), bpathf(&b1, "%s/os_%s.h", bstr(&path), goos), 0); copy(bpathf(&b, "%s/signals_GOOS.h", workdir), bpathf(&b1, "%s/signals_%s.h", bstr(&path), goos), 0); } // Generate any missing files; regenerate existing ones. for(i=0; i<files.len; i++) { p = files.p[i]; elem = lastelem(p); for(j=0; j<nelem(gentab); j++) { if(hasprefix(elem, gentab[j].nameprefix)) { if(vflag > 1) errprintf("generate %s\n", p); gentab[j].gen(bstr(&path), p); // Do not add generated file to clean list. // In pkg/runtime, we want to be able to // build the package with the go tool, // and it assumes these generated files already // exist (it does not know how to build them). // The 'clean' command can remove // the generated files. goto built; } } // Did not rebuild p. if(find(p, missing.p, missing.len) >= 0) fatal("missing file %s", p); built:; } // One more copy for package runtime. // The last batch was required for the generators. // This one is generated. if(streq(dir, "pkg/runtime")) { copy(bpathf(&b, "%s/zasm_GOOS_GOARCH.h", workdir), bpathf(&b1, "%s/zasm_%s_%s.h", bstr(&path), goos, goarch), 0); } // Generate .c files from .goc files. if(streq(dir, "pkg/runtime")) { for(i=0; i<files.len; i++) { p = files.p[i]; if(!hassuffix(p, ".goc")) continue; // b = path/zp but with _goos_goarch.c instead of .goc bprintf(&b, "%s%sz%s", bstr(&path), slash, lastelem(p)); b.len -= 4; bwritef(&b, "_%s_%s.c", goos, goarch); goc2c(p, bstr(&b)); vadd(&files, bstr(&b)); } vuniq(&files); } if((!streq(goos, gohostos) || !streq(goarch, gohostarch)) && isgo) { // We've generated the right files; the go command can do the build. if(vflag > 1) errprintf("skip build for cross-compile %s\n", dir); goto nobuild; } // The files generated by GNU Bison use macros that aren't // supported by the Plan 9 compilers so we have to use the // external preprocessor when compiling. usecpp = 0; if(streq(gohostos, "plan9")) { for(i=0; i<files.len; i++) { p = files.p[i]; if(hassuffix(p, "y.tab.c") || hassuffix(p, "y.tab.h")){ usecpp = 1; break; } } } // Compile the files. for(i=0; i<files.len; i++) { if(!hassuffix(files.p[i], ".c") && !hassuffix(files.p[i], ".s")) continue; name = lastelem(files.p[i]); vreset(&compile); if(!isgo) { // C library or tool. if(streq(gohostos, "plan9")) { vadd(&compile, bprintf(&b, "%sc", gohostchar)); vadd(&compile, "-FTVw"); if(usecpp) vadd(&compile, "-Bp+"); vadd(&compile, bpathf(&b, "-I%s/include/plan9", goroot)); vadd(&compile, bpathf(&b, "-I%s/include/plan9/%s", goroot, gohostarch)); } else { vcopy(&compile, gccargs.p, gccargs.len); vadd(&compile, "-c"); if(streq(gohostarch, "amd64")) vadd(&compile, "-m64"); else if(streq(gohostarch, "386")) vadd(&compile, "-m32"); if(streq(dir, "lib9")) vadd(&compile, "-DPLAN9PORT"); vadd(&compile, "-I"); vadd(&compile, bpathf(&b, "%s/include", goroot)); } vadd(&compile, "-I"); vadd(&compile, bstr(&path)); // lib9/goos.c gets the default constants hard-coded. if(streq(name, "goos.c")) { vadd(&compile, bprintf(&b, "-DGOOS=\"%s\"", goos)); vadd(&compile, bprintf(&b, "-DGOARCH=\"%s\"", goarch)); bprintf(&b1, "%s", goroot_final); bsubst(&b1, "\\", "\\\\"); // turn into C string vadd(&compile, bprintf(&b, "-DGOROOT=\"%s\"", bstr(&b1))); vadd(&compile, bprintf(&b, "-DGOVERSION=\"%s\"", goversion)); } // gc/lex.c records the GOEXPERIMENT setting used during the build. if(streq(name, "lex.c")) { xgetenv(&b, "GOEXPERIMENT"); vadd(&compile, bprintf(&b1, "-DGOEXPERIMENT=\"%s\"", bstr(&b))); } } else { // Supporting files for a Go package. if(hassuffix(files.p[i], ".s")) vadd(&compile, bpathf(&b, "%s/%sa", tooldir, gochar)); else { vadd(&compile, bpathf(&b, "%s/%sc", tooldir, gochar)); vadd(&compile, "-FVw"); } vadd(&compile, "-I"); vadd(&compile, workdir); vadd(&compile, bprintf(&b, "-DGOOS_%s", goos)); vadd(&compile, bprintf(&b, "-DGOARCH_%s", goarch)); } bpathf(&b, "%s/%s", workdir, lastelem(files.p[i])); doclean = 1; if(!isgo && streq(gohostos, "darwin")) { // To debug C programs on OS X, it is not enough to say -ggdb // on the command line. You have to leave the object files // lying around too. Leave them in pkg/obj/, which does not // get removed when this tool exits. bpathf(&b1, "%s/pkg/obj/%s", goroot, dir); xmkdirall(bstr(&b1)); bpathf(&b, "%s/%s", bstr(&b1), lastelem(files.p[i])); doclean = 0; } // Change the last character of the output file (which was c or s). if(streq(gohostos, "plan9")) b.p[b.len-1] = gohostchar[0]; else b.p[b.len-1] = 'o'; vadd(&compile, "-o"); vadd(&compile, bstr(&b)); vadd(&compile, files.p[i]); bgrunv(bstr(&path), CheckExit, &compile); vadd(&link, bstr(&b)); if(doclean) vadd(&clean, bstr(&b)); } bgwait(); if(isgo) { // The last loop was compiling individual files. // Hand the Go files to the compiler en masse. vreset(&compile); vadd(&compile, bpathf(&b, "%s/%sg", tooldir, gochar)); bpathf(&b, "%s/_go_.%s", workdir, gochar); vadd(&compile, "-o"); vadd(&compile, bstr(&b)); vadd(&clean, bstr(&b)); vadd(&link, bstr(&b)); vadd(&compile, "-p"); if(hasprefix(dir, "pkg/")) vadd(&compile, dir+4); else vadd(&compile, "main"); if(streq(dir, "pkg/runtime")) vadd(&compile, "-+"); vcopy(&compile, go.p, go.len); runv(nil, bstr(&path), CheckExit, &compile); } if(!islib && !isgo) { // C binaries need the libraries explicitly, and -lm. vcopy(&link, lib.p, lib.len); if(!streq(gohostos, "plan9")) vadd(&link, "-lm"); } // Remove target before writing it. xremove(link.p[targ]); runv(nil, nil, CheckExit, &link); nobuild: // In package runtime, we install runtime.h and cgocall.h too, // for use by cgo compilation. if(streq(dir, "pkg/runtime")) { copy(bpathf(&b, "%s/pkg/%s_%s/cgocall.h", goroot, goos, goarch), bpathf(&b1, "%s/src/pkg/runtime/cgocall.h", goroot), 0); copy(bpathf(&b, "%s/pkg/%s_%s/runtime.h", goroot, goos, goarch), bpathf(&b1, "%s/src/pkg/runtime/runtime.h", goroot), 0); } out: for(i=0; i<clean.len; i++) xremove(clean.p[i]); bfree(&b); bfree(&b1); bfree(&path); vfree(&compile); vfree(&files); vfree(&link); vfree(&go); vfree(&missing); vfree(&clean); vfree(&lib); vfree(&extra); }
// The bootstrap command runs a build from scratch, // stopping at having installed the go_bootstrap command. void cmdbootstrap(int argc, char **argv) { int i; Buf b; char *oldgoos, *oldgoarch, *oldgochar; binit(&b); ARGBEGIN{ case 'a': rebuildall = 1; break; case 's': sflag++; break; case 'v': vflag++; break; default: usage(); }ARGEND if(argc > 0) usage(); if(isdir(bpathf(&b, "%s/src/pkg", goroot))) { fatal("\n\n" "The Go package sources have moved to $GOROOT/src.\n" "*** %s still exists. ***\n" "It probably contains stale files that may confuse the build.\n" "Please (check what's there and) remove it and try again.\n" "See http://golang.org/s/go14nopkg\n", bpathf(&b, "%s/src/pkg", goroot)); } if(rebuildall) clean(); goversion = findgoversion(); setup(); xsetenv("GOROOT", goroot); xsetenv("GOROOT_FINAL", goroot_final); // For the main bootstrap, building for host os/arch. oldgoos = goos; oldgoarch = goarch; oldgochar = gochar; goos = gohostos; goarch = gohostarch; gochar = gohostchar; xsetenv("GOARCH", goarch); xsetenv("GOOS", goos); for(i=0; i<nelem(buildorder); i++) { install(bprintf(&b, buildorder[i], gohostchar)); if(!streq(oldgochar, gohostchar) && xstrstr(buildorder[i], "%s")) install(bprintf(&b, buildorder[i], oldgochar)); } goos = oldgoos; goarch = oldgoarch; gochar = oldgochar; xsetenv("GOARCH", goarch); xsetenv("GOOS", goos); // Build runtime for actual goos/goarch too. if(!streq(goos, gohostos) || !streq(goarch, gohostarch)) install("runtime"); bfree(&b); }
/** ** DESCRIPTION ** Add hashtable item for each link found in a line from the stream ** if hostname or ip != NULL then checks if link refers to hostname or ip ** includes arguments after '?' as query string ** ** RETURN VALUE ** The number of links found **/ int parse_links( const char * line, const char * hostname, const char * ip, char * curdir ) { struct linkz * lk; char * ptr = NULL; char * linkn = NULL; char * query = NULL; char * tfree = NULL; char * start = NULL; int count = 0; int proto = 0; int match = 0; int found = 0; int updat = 0; if ( line ) ptr = xstrdup(line); else return 0; if ( !ptr ) return 0; tfree = ptr; do { start = ptr ; if ( (ptr = xstrstr( start, HREF )) ) { ptr += strlen ( HREF ); NEXTFIELD( ptr ); found = 1; } else if ( (ptr = xstrstr( start, FRAME )) ) { if ( (ptr += strlen( FRAME )) && (ptr = xstrstr( ptr, SRC )) ) { if ( (ptr += strlen( SRC )) ) { NEXTFIELD( ptr ); found = 1; } } } if ( !ISNULL(ptr) && found ) { found = 0; if ( !strncasecmp( ptr, "http://", 7 )) proto = 7; else if ( !strncasecmp( ptr, "javascript:", 11 )) continue; else if ( !strncasecmp( ptr, "mailto:", 7 )) continue; else if ( !strncasecmp( ptr, "news:", 5 )) continue; else if ( !strncasecmp( ptr, "ftp://", 6 )) continue; else if ( !strncasecmp( ptr, "https://", 8 )) continue; if ( proto ) { ptr += proto; proto = 0; if ( hostname ) { if ( !strncasecmp( ptr, hostname, strlen(hostname)) ) match = 1; } if ( ip ) { if ( !strncasecmp( ptr, ip, strlen(ip)) ) match = 1; } if ( !match && (hostname || ip) ) { continue; } else { ptr = strchr( ptr, '/' ); match = 0; } } if ( !ISNULL(ptr) ) { boolean relative = FALSE; char * p = strpbrk( ptr, "#\" '\t\r\n>" ); linkn = ptr; if ( p ) { ptr = p + 1; *p = 0; } if ( (query = strchr( linkn, '?')) ) *query++ = 0; updat = 0; if ( !linkn[0] || (linkn[0]=='/' && !linkn[2]) ) continue; if ( strpbrk( linkn, "=<>?&" ) ) continue; if ( !EXT_ISALLOWED( linkn ) ) { debug( "Skipping unallowed file type: %s\n", linkn ); continue; } if ( ISRELATIVE( linkn ) ) { relative = TRUE; linkn = fix_relative( linkn, curdir ); } if ( (lk = (struct linkz *)hash_get_value( linkn, HTlinks )) ) { if ( !query || ((query) && (lk->flags & LDYNAMIC)) ) continue; } lk = (struct linkz *)xmalloc(sizeof(struct linkz)); debug( "Link: %s\n", linkn ); if ( query ) { if ( updat ) lk->flags &= ~LVISITED; lk->flags |= LCACHED; lk->flags |= LDYNAMIC; lk->query = xstrdup( query ); debug( "Query: %s\n", query ); } hash_insert( linkn, lk, HTlinks ); if ( relative == TRUE ) xfree( linkn ); updat ^= updat; count++ ; } } } while( ptr ); xfree( tfree ); return count; }
/* items == -1 display all */ static int logs_buffer_raw_display(const char *file, int items) { struct buffer **bs = NULL; struct buffer *b; char *beg = NULL, *profile = NULL, *sesja = NULL, *target = NULL; int item = 0; int i; session_t *s; window_t *w; if (!file) return -1; if (!items) return 0; /* i'm weird, let's search for logs/__internal__ than check if there are smth after it... & check if there are two '/' */ if ((beg = xstrstr(file, "logs/__internal__")) && xstrlen(beg) > 19 && xstrchr(beg+18, '/') && xstrchr(beg+18, '/') != xstrrchr(beg+18, '/')) { profile = beg + 18; sesja = xstrchr(profile, '/')+1; target = xstrchr(sesja, '/')+1; } debug("[logs_buffer_raw_display()] profile: 0x%x sesja: 0x%x target: 0x%x\n", profile, sesja, target); if (!profile || !sesja || !target) return -1; profile = (xstrcmp(target, "_default_")) ? xstrndup(profile, sesja-profile-1) : NULL; sesja = (xstrcmp(target, "_null_")) ? xstrndup(sesja, target-sesja-1) : NULL; target = xstrdup(target); debug("[logs_buffer_raw_display()] profile: %s sesja: %s target: %s\n", __(profile), __(sesja), __(target)); /* search for session+window */ s = session_find(sesja); w = window_find_s(s, target); debug("[logs_buffer_raw_display()] s:0x%x; w:0x%x;\n", s, w); if (!w) w = window_current; if (w) w->lock++; for (b = buffer_lograw.data; b; b = b->next) { if (!xstrcmp(b->target, file)) { /* we asume that (b->ts < (b->next)->ts, it's quite correct unless other plugin do this trick... */ if (items == -1) { logs_print_window(s, w, b->line, b->ts); } else { bs = (struct buffer **) xrealloc(bs, (item+2) * sizeof(struct buffer *)); bs[item + 1] = NULL; bs[item] = b; } item++; } } if (bs) for (i = item < items ? 0 : item-items; i < item; i++) logs_print_window(s, w, bs[i]->line, bs[i]->ts); if (w) { w->lock--; query_emit(NULL, "ui-window-refresh"); } xfree(bs); xfree(profile); xfree(sesja); xfree(target); return item; }
static int logs_window_check(logs_log_t *ll, time_t t) { session_t *s; log_window_t *l = ll->lw; int chan = 0; int tmp; if (!l || !(s = session_find(ll->session))) return -1; if (l->logformat != (tmp = logs_log_format(s))) { l->logformat = tmp; chan = 1; } if (!(l->path)) { chan = 2; } else { int datechanged = 0; /* bitmaska 0x01 (dzien) 0x02 (miesiac) 0x04 (rok) */ struct tm *tm = g_memdup(localtime(&(ll->t)), sizeof(struct tm)); struct tm *tm2 = localtime(&t); /* sprawdzic czy dane z (tm == tm2) */ if (tm->tm_mday != tm2->tm_mday) datechanged |= 0x01; if (tm->tm_mon != tm2->tm_mon) datechanged |= 0x02; if (tm->tm_year != tm2->tm_year) datechanged |= 0x04; if ( ((datechanged & 0x04) && xstrstr(config_logs_path, "%Y")) || ((datechanged & 0x02) && xstrstr(config_logs_path, "%M")) || ((datechanged & 0x01) && xstrstr(config_logs_path, "%D")) ) chan = 3; /* zalogowac jak sie zmienila data */ if (datechanged && l->logformat == LOG_FORMAT_IRSSI) { /* yes i know it's wrong place for doing this but .... */ if (!(l->file)) l->file = logs_open_file(l->path, LOG_FORMAT_IRSSI); logs_irssi(l->file, ll->session, NULL, prepare_timestamp_format(IRSSI_LOG_DAY_CHANGED, time(NULL)), 0, EKG_MSGCLASS_SYSTEM); } xfree(tm); } ll->t = t; if (chan > 1) { char *tmp = l->path; l->path = logs_prepare_path(s, config_logs_path, ll->uid, t); debug("[logs] logs_window_check chan = %d oldpath = %s newpath = %s\n", chan, __(tmp), __(l->path)); #if 0 /* TODO: nie moze byc - bo mogl logformat sie zmienic... */ if (chan != 3 && !xstrcmp(tmp, l->path)) /* jesli sciezka sie nie zmienila to nie otwieraj na nowo pliku */ chan = -chan; #endif xfree(tmp); } if (chan > 0) { if (l->file) { /* jesli plik byl otwarty otwieramy go z nowa sciezka */ fclose(l->file); l->file = NULL; l->file = logs_open_file(l->path, l->logformat); } } return chan; }
// mkanames reads [5689].out.h and writes anames[5689].c // The format is much the same as the Go opcodes above. // It also writes out cnames array for C_* constants and the dnames // array for D_* constants. void mkanames(char *dir, char *file) { int i, j, ch, n, unknown; Buf in, b, out, out2; Vec lines; char *p, *p2; Vec dnames[128]; binit(&b); binit(&in); binit(&out); binit(&out2); vinit(&lines); for(i=0; i<nelem(dnames); i++) vinit(&dnames[i]); ch = file[xstrlen(file)-3]; bprintf(&b, "%s/../cmd/%cl/%c.out.h", dir, ch, ch); readfile(&in, bstr(&b)); splitlines(&lines, bstr(&in)); // Include link.h so that the extern declaration there is // checked against the non-extern declaration we are generating. bwritestr(&out, bprintf(&b, "// auto generated by go tool dist\n")); bwritestr(&out, bprintf(&b, "#include <u.h>\n")); bwritestr(&out, bprintf(&b, "#include <libc.h>\n")); bwritestr(&out, bprintf(&b, "#include <bio.h>\n")); bwritestr(&out, bprintf(&b, "#include <link.h>\n")); bwritestr(&out, bprintf(&b, "#include \"../cmd/%cl/%c.out.h\"\n", ch, ch)); bwritestr(&out, bprintf(&b, "\n")); bwritestr(&out, bprintf(&b, "char* anames%c[] = {\n", ch)); for(i=0; i<lines.len; i++) { if(hasprefix(lines.p[i], "\tA")) { p = xstrstr(lines.p[i], ","); if(p) *p = '\0'; p = xstrstr(lines.p[i], "\n"); if(p) *p = '\0'; p = lines.p[i] + 2; bwritestr(&out, bprintf(&b, "\t\"%s\",\n", p)); } } bwritestr(&out, "};\n"); j=0; bprintf(&out2, "char* cnames%c[] = {\n", ch); for(i=0; i<lines.len; i++) { if(hasprefix(lines.p[i], "\tC_")) { p = xstrstr(lines.p[i], ","); if(p) *p = '\0'; p = xstrstr(lines.p[i], "\n"); if(p) *p = '\0'; p = lines.p[i] + 3; bwritestr(&out2, bprintf(&b, "\t\"%s\",\n", p)); j++; } } bwritestr(&out2, "};\n"); if(j>0) bwriteb(&out, &out2); j=unknown=0; n=-1; for(i=0; i<lines.len; i++) { if(hasprefix(lines.p[i], "\tD_")) { p = xstrstr(lines.p[i], ","); if(p) *p = '\0'; p = xstrstr(lines.p[i], "\n"); if(p) *p = '\0'; // Parse explicit value, if any p = xstrstr(lines.p[i], "="); if(p) { // Skip space after '=' p2 = p + 1; while(*p2 == ' ' || *p2 == '\t') p2++; n = xatoi(p2, &p2); // We can't do anything about // non-numeric values or anything that // follows while(*p2 == ' ' || *p2 == '\t') p2++; if(*p2 != 0) { unknown = 1; continue; } // Truncate space before '=' while(*(p-1) == ' ' || *(p-1) == '\t') p--; *p = '\0'; unknown = 0; } else { n++; } if(unknown || n >= nelem(dnames)) continue; p = lines.p[i] + 3; if(xstrcmp(p, "LAST") == 0) continue; vadd(&dnames[n], p); j++; } } if(j>0){ bwritestr(&out, bprintf(&b, "char* dnames%c[D_LAST] = {\n", ch)); for(i=0; i<nelem(dnames); i++) { if(dnames[i].len == 0) continue; bwritestr(&out, bprintf(&b, "\t[D_%s] = \"", dnames[i].p[0])); for(j=0; j<dnames[i].len; j++) { if(j != 0) bwritestr(&out, "/"); bwritestr(&out, dnames[i].p[j]); } bwritestr(&out, "\",\n"); } bwritestr(&out, "};\n"); } writefile(&out, file, 0); bfree(&b); bfree(&in); bfree(&out); bfree(&out2); vfree(&lines); for(i=0; i<nelem(dnames); i++) vfree(&dnames[i]); }