/* Store a resource at specified address. * If we are in pass 2, do a lookup of the * resource. */ void store_res(LEX *lc, RES_ITEM *item, int index, int pass) { RES *res; lex_get_token(lc, T_NAME); if (pass == 2) { res = GetResWithName(item->code, lc->str); if (res == NULL) { scan_err3(lc, _("Could not find config Resource %s referenced on line %d : %s\n"), lc->str, lc->line_no, lc->line); return; } if (*(item->value)) { scan_err3(lc, _("Attempt to redefine resource \"%s\" referenced on line %d : %s\n"), item->name, lc->line_no, lc->line); return; } *(item->value) = (char *)res; } scan_to_eol(lc); set_bit(index, res_all.hdr.item_present); }
/* * Store a resource pointer in an alist. default_value indicates how many * times this routine can be called -- i.e. how many alists * there are. * * If we are in pass 2, do a lookup of the resource. */ static void store_alist_res(LEX *lc, RES_ITEM *item, int index, int pass) { RES *res; int i = 0; alist *list; URES *res_all = (URES *)my_config->m_res_all; int count = str_to_int32(item->default_value); if (pass == 2) { if (count == 0) { /* always store in item->value */ i = 0; if ((item->value)[i] == NULL) { list = New(alist(10, not_owned_by_alist)); } else { list = (alist *)(item->value)[i]; } } else { /* * Find empty place to store this directive */ while ((item->value)[i] != NULL && i++ < count) { } if (i >= count) { scan_err4(lc, _("Too many %s directives. Max. is %d. line %d: %s\n"), lc->str, count, lc->line_no, lc->line); return; } list = New(alist(10, not_owned_by_alist)); } for (;;) { lex_get_token(lc, T_NAME); /* scan next item */ res = GetResWithName(item->code, lc->str); if (res == NULL) { scan_err3(lc, _("Could not find config Resource \"%s\" referenced on line %d : %s\n"), item->name, lc->line_no, lc->line); return; } Dmsg5(900, "Append %p to alist %p size=%d i=%d %s\n", res, list, list->size(), i, item->name); list->append(res); (item->value)[i] = (char *)list; if (lc->ch != ',') { /* if no other item follows */ break; /* get out */ } lex_get_token(lc, T_ALL); /* eat comma */ } } scan_to_eol(lc); set_bit(index, res_all->hdr.item_present); }
/* * Store default values for Resource from xxxDefs * If we are in pass 2, do a lookup of the * resource and store everything not explicitly set * in main resource. * * Note, here item points to the main resource (e.g. Job, not * the jobdefs, which we look up). */ void store_defs(LEX *lc, RES_ITEM *item, int index, int pass) { RES *res; lex_get_token(lc, T_NAME); if (pass == 2) { Dmsg2(900, "Code=%d name=%s\n", item->code, lc->str); res = GetResWithName(item->code, lc->str); if (res == NULL) { scan_err3(lc, _("Missing config Resource \"%s\" referenced on line %d : %s\n"), lc->str, lc->line_no, lc->line); return; } } scan_to_eol(lc); }
/* * * Get the next token from the input * */ int lex_get_token(LEX *lf, int expect) { int ch; int token = T_NONE; bool esc_next = false; /* Unicode files, especially on Win32, may begin with a "Byte Order Mark" to indicate which transmission format the file is in. The codepoint for this mark is U+FEFF and is represented as the octets EF-BB-BF in UTF-8 and as FF-FE in UTF-16le(little endian) and FE-FF in UTF-16(big endian). We use a distinct state for UTF-8 and UTF-16le, and use bom_bytes_seen to tell which byte we are expecting. */ int bom_bytes_seen = 0; Dmsg0(dbglvl, "enter lex_get_token\n"); while (token == T_NONE) { ch = lex_get_char(lf); switch (lf->state) { case lex_none: Dmsg2(dbglvl, "Lex state lex_none ch=%d,%x\n", ch, ch); if (B_ISSPACE(ch)) break; if (B_ISALPHA(ch)) { if (lf->options & LOPT_NO_IDENT || lf->options & LOPT_STRING) { lf->state = lex_string; } else { lf->state = lex_identifier; } begin_str(lf, ch); break; } if (B_ISDIGIT(ch)) { if (lf->options & LOPT_STRING) { lf->state = lex_string; } else { lf->state = lex_number; } begin_str(lf, ch); break; } Dmsg0(dbglvl, "Enter lex_none switch\n"); switch (ch) { case L_EOF: token = T_EOF; Dmsg0(dbglvl, "got L_EOF set token=T_EOF\n"); break; case '#': lf->state = lex_comment; break; case '{': token = T_BOB; begin_str(lf, ch); break; case '}': token = T_EOB; begin_str(lf, ch); break; case '"': lf->state = lex_quoted_string; begin_str(lf, 0); break; case '=': token = T_EQUALS; begin_str(lf, ch); break; case ',': token = T_COMMA; begin_str(lf, ch); break; case ';': if (expect != T_SKIP_EOL) { token = T_EOL; /* treat ; like EOL */ } break; case L_EOL: Dmsg0(dbglvl, "got L_EOL set token=T_EOL\n"); if (expect != T_SKIP_EOL) { token = T_EOL; } break; case '@': /* In NO_EXTERN mode, @ is part of a string */ if (lf->options & LOPT_NO_EXTERN) { lf->state = lex_string; begin_str(lf, ch); } else { lf->state = lex_include; begin_str(lf, 0); } break; case 0xEF: /* probably a UTF-8 BOM */ case 0xFF: /* probably a UTF-16le BOM */ case 0xFE: /* probably a UTF-16be BOM (error)*/ if (lf->line_no != 1 || lf->col_no != 1) { lf->state = lex_string; begin_str(lf, ch); } else { bom_bytes_seen = 1; if (ch == 0xEF) { lf->state = lex_utf8_bom; } else if (ch == 0xFF) { lf->state = lex_utf16_le_bom; } else { scan_err0(lf, _("This config file appears to be in an " "unsupported Unicode format (UTF-16be). Please resave as UTF-8\n")); return T_ERROR; } } break; default: lf->state = lex_string; begin_str(lf, ch); break; } break; case lex_comment: Dmsg1(dbglvl, "Lex state lex_comment ch=%x\n", ch); if (ch == L_EOL) { lf->state = lex_none; if (expect != T_SKIP_EOL) { token = T_EOL; } } else if (ch == L_EOF) { token = T_ERROR; } break; case lex_number: Dmsg2(dbglvl, "Lex state lex_number ch=%x %c\n", ch, ch); if (ch == L_EOF) { token = T_ERROR; break; } /* Might want to allow trailing specifications here */ if (B_ISDIGIT(ch)) { add_str(lf, ch); break; } /* A valid number can be terminated by the following */ if (B_ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') { token = T_NUMBER; lf->state = lex_none; } else { lf->state = lex_string; } lex_unget_char(lf); break; case lex_ip_addr: if (ch == L_EOF) { token = T_ERROR; break; } Dmsg1(dbglvl, "Lex state lex_ip_addr ch=%x\n", ch); break; case lex_string: Dmsg1(dbglvl, "Lex state lex_string ch=%x\n", ch); if (ch == L_EOF) { token = T_ERROR; break; } if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' || ch == '\r' || ch == ';' || ch == ',' || ch == '#' || (B_ISSPACE(ch)) ) { lex_unget_char(lf); token = T_UNQUOTED_STRING; lf->state = lex_none; break; } add_str(lf, ch); break; case lex_identifier: Dmsg2(dbglvl, "Lex state lex_identifier ch=%x %c\n", ch, ch); if (B_ISALPHA(ch)) { add_str(lf, ch); break; } else if (B_ISSPACE(ch)) { break; } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' || ch == '\r' || ch == ';' || ch == ',' || ch == '"' || ch == '#') { lex_unget_char(lf); token = T_IDENTIFIER; lf->state = lex_none; break; } else if (ch == L_EOF) { token = T_ERROR; lf->state = lex_none; begin_str(lf, ch); break; } /* Some non-alpha character => string */ lf->state = lex_string; add_str(lf, ch); break; case lex_quoted_string: Dmsg2(dbglvl, "Lex state lex_quoted_string ch=%x %c\n", ch, ch); if (ch == L_EOF) { token = T_ERROR; break; } if (ch == L_EOL) { esc_next = false; break; } if (esc_next) { add_str(lf, ch); esc_next = false; break; } if (ch == '\\') { esc_next = true; break; } if (ch == '"') { token = T_QUOTED_STRING; /* * Since we may be scanning a quoted list of names, * we get the next character (a comma indicates another * one), then we put it back for rescanning. */ lex_get_char(lf); lex_unget_char(lf); lf->state = lex_none; break; } add_str(lf, ch); break; case lex_include_quoted_string: if (ch == L_EOF) { token = T_ERROR; break; } if (esc_next) { add_str(lf, ch); esc_next = false; break; } if (ch == '\\') { esc_next = true; break; } if (ch == '"') { /* Keep the original LEX so we can print an error if the included file can't be opened. */ LEX* lfori = lf; /* Skip the double quote when restarting parsing */ lex_get_char(lf); lf->state = lex_none; lf = lex_open_file(lf, lf->str, lf->scan_error, lf->scan_warning); if (lf == NULL) { berrno be; scan_err2(lfori, _("Cannot open included config file %s: %s\n"), lfori->str, be.bstrerror()); return T_ERROR; } break; } add_str(lf, ch); break; case lex_include: /* scanning a filename */ if (ch == L_EOF) { token = T_ERROR; break; } if (ch == '"') { lf->state = lex_include_quoted_string; break; } if (B_ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' || ch == ';' || ch == ',' || ch == '"' || ch == '#') { /* Keep the original LEX so we can print an error if the included file can't be opened. */ LEX* lfori = lf; lf->state = lex_none; lf = lex_open_file(lf, lf->str, lf->scan_error, lf->scan_warning); if (lf == NULL) { berrno be; scan_err2(lfori, _("Cannot open included config file %s: %s\n"), lfori->str, be.bstrerror()); return T_ERROR; } break; } add_str(lf, ch); break; case lex_utf8_bom: /* we only end up in this state if we have read an 0xEF as the first byte of the file, indicating we are probably reading a UTF-8 file */ if (ch == 0xBB && bom_bytes_seen == 1) { bom_bytes_seen++; } else if (ch == 0xBF && bom_bytes_seen == 2) { token = T_UTF8_BOM; lf->state = lex_none; } else { token = T_ERROR; } break; case lex_utf16_le_bom: /* we only end up in this state if we have read an 0xFF as the first byte of the file -- indicating that we are probably dealing with an Intel based (little endian) UTF-16 file*/ if (ch == 0xFE) { token = T_UTF16_BOM; lf->state = lex_none; } else { token = T_ERROR; } break; } Dmsg4(dbglvl, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state), lex_tok_to_str(token), ch); } Dmsg2(dbglvl, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token)); lf->token = token; /* * Here is where we check to see if the user has set certain * expectations (e.g. 32 bit integer). If so, we do type checking * and possible additional scanning (e.g. for range). */ switch (expect) { case T_PINT16: lf->u.pint16_val = (scan_pint(lf, lf->str) & 0xffff); lf->u2.pint16_val = lf->u.pint16_val; token = T_PINT16; break; case T_PINT32: lf->u.pint32_val = scan_pint(lf, lf->str); lf->u2.pint32_val = lf->u.pint32_val; token = T_PINT32; break; case T_PINT32_RANGE: if (token == T_NUMBER) { lf->u.pint32_val = scan_pint(lf, lf->str); lf->u2.pint32_val = lf->u.pint32_val; token = T_PINT32; } else { char *p = strchr(lf->str, '-'); if (!p) { scan_err2(lf, _("expected an integer or a range, got %s: %s"), lex_tok_to_str(token), lf->str); token = T_ERROR; break; } *p++ = 0; /* terminate first half of range */ lf->u.pint32_val = scan_pint(lf, lf->str); lf->u2.pint32_val = scan_pint(lf, p); token = T_PINT32_RANGE; } break; case T_INT16: if (token != T_NUMBER || !is_a_number(lf->str)) { scan_err2(lf, _("expected an integer number, got %s: %s"), lex_tok_to_str(token), lf->str); token = T_ERROR; break; } errno = 0; lf->u.int16_val = (int16_t)str_to_int64(lf->str); if (errno != 0) { scan_err2(lf, _("expected an integer number, got %s: %s"), lex_tok_to_str(token), lf->str); token = T_ERROR; } else { token = T_INT16; } break; case T_INT32: if (token != T_NUMBER || !is_a_number(lf->str)) { scan_err2(lf, _("expected an integer number, got %s: %s"), lex_tok_to_str(token), lf->str); token = T_ERROR; break; } errno = 0; lf->u.int32_val = (int32_t)str_to_int64(lf->str); if (errno != 0) { scan_err2(lf, _("expected an integer number, got %s: %s"), lex_tok_to_str(token), lf->str); token = T_ERROR; } else { token = T_INT32; } break; case T_INT64: Dmsg2(dbglvl, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL)); if (token != T_NUMBER || !is_a_number(lf->str)) { scan_err2(lf, _("expected an integer number, got %s: %s"), lex_tok_to_str(token), lf->str); token = T_ERROR; break; } errno = 0; lf->u.int64_val = str_to_int64(lf->str); if (errno != 0) { scan_err2(lf, _("expected an integer number, got %s: %s"), lex_tok_to_str(token), lf->str); token = T_ERROR; } else { token = T_INT64; } break; case T_PINT64_RANGE: if (token == T_NUMBER) { lf->u.pint64_val = scan_pint64(lf, lf->str); lf->u2.pint64_val = lf->u.pint64_val; token = T_PINT64; } else { char *p = strchr(lf->str, '-'); if (!p) { scan_err2(lf, _("expected an integer or a range, got %s: %s"), lex_tok_to_str(token), lf->str); token = T_ERROR; break; } *p++ = 0; /* terminate first half of range */ lf->u.pint64_val = scan_pint64(lf, lf->str); lf->u2.pint64_val = scan_pint64(lf, p); token = T_PINT64_RANGE; } break; case T_NAME: if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) { scan_err2(lf, _("expected a name, got %s: %s"), lex_tok_to_str(token), lf->str); token = T_ERROR; } else if (lf->str_len > MAX_RES_NAME_LENGTH) { scan_err3(lf, _("name %s length %d too long, max is %d\n"), lf->str, lf->str_len, MAX_RES_NAME_LENGTH); token = T_ERROR; } break; case T_STRING: if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) { scan_err2(lf, _("expected a string, got %s: %s"), lex_tok_to_str(token), lf->str); token = T_ERROR; } else { token = T_STRING; } break; default: break; /* no expectation given */ } lf->token = token; /* set possible new token */ return token; }
/* * Store network addresses. * * my tests * positiv * = { ip = { addr = 1.2.3.4; port = 1205; } ipv4 = { addr = 1.2.3.4; port = http; } } * = { ip = { * addr = 1.2.3.4; port = 1205; } * ipv4 = { * addr = 1.2.3.4; port = http; } * ipv6 = { * addr = 1.2.3.4; * port = 1205; * } * ip = { * addr = 1.2.3.4 * port = 1205 * } * ip = { * addr = 1.2.3.4 * } * ip = { * addr = 2001:220:222::2 * } * ip = { * addr = bluedot.thun.net ( } * } * negativ * = { ip = { } } * = { ipv4 { addr = doof.nowaytoheavenxyz.uhu; } } * = { ipv4 { port = 4711 } } */ void store_addresses(LEX * lc, RES_ITEM * item, int index, int pass) { int token; int exist; int family = 0; char errmsg[1024]; char port_str[128]; char hostname_str[1024]; enum { EMPTYLINE = 0x0, PORTLINE = 0x1, ADDRLINE = 0x2 } next_line = EMPTYLINE; int port = str_to_int32(item->default_value); token = lex_get_token(lc, T_SKIP_EOL); if (token != T_BOB) { scan_err1(lc, _("Expected a block begin { , got: %s"), lc->str); } token = lex_get_token(lc, T_SKIP_EOL); if (token == T_EOB) { scan_err0(lc, _("Empty addr block is not allowed")); } do { if (!(token == T_UNQUOTED_STRING || token == T_IDENTIFIER)) { scan_err1(lc, _("Expected a string, got: %s"), lc->str); } if (bstrcasecmp("ip", lc->str) || bstrcasecmp("ipv4", lc->str)) { family = AF_INET; #ifdef HAVE_IPV6 } else if (bstrcasecmp("ipv6", lc->str)) { family = AF_INET6; } else { scan_err1(lc, _("Expected a string [ip|ipv4|ipv6], got: %s"), lc->str); } #else } else { scan_err1(lc, _("Expected a string [ip|ipv4], got: %s"), lc->str); } #endif token = lex_get_token(lc, T_SKIP_EOL); if (token != T_EQUALS) { scan_err1(lc, _("Expected a equal =, got: %s"), lc->str); } token = lex_get_token(lc, T_SKIP_EOL); if (token != T_BOB) { scan_err1(lc, _("Expected a block begin { , got: %s"), lc->str); } token = lex_get_token(lc, T_SKIP_EOL); exist = EMPTYLINE; port_str[0] = hostname_str[0] = '\0'; do { if (token != T_IDENTIFIER) { scan_err1(lc, _("Expected a identifier [addr|port], got: %s"), lc->str); } if (bstrcasecmp("port", lc->str)) { next_line = PORTLINE; if (exist & PORTLINE) { scan_err0(lc, _("Only one port per address block")); } exist |= PORTLINE; } else if (bstrcasecmp("addr", lc->str)) { next_line = ADDRLINE; if (exist & ADDRLINE) { scan_err0(lc, _("Only one addr per address block")); } exist |= ADDRLINE; } else { scan_err1(lc, _("Expected a identifier [addr|port], got: %s"), lc->str); } token = lex_get_token(lc, T_SKIP_EOL); if (token != T_EQUALS) { scan_err1(lc, _("Expected a equal =, got: %s"), lc->str); } token = lex_get_token(lc, T_SKIP_EOL); switch (next_line) { case PORTLINE: if (!(token == T_UNQUOTED_STRING || token == T_NUMBER || token == T_IDENTIFIER)) { scan_err1(lc, _("Expected a number or a string, got: %s"), lc->str); } bstrncpy(port_str, lc->str, sizeof(port_str)); break; case ADDRLINE: if (!(token == T_UNQUOTED_STRING || token == T_IDENTIFIER)) { scan_err1(lc, _("Expected an IP number or a hostname, got: %s"), lc->str); } bstrncpy(hostname_str, lc->str, sizeof(hostname_str)); break; case EMPTYLINE: scan_err0(lc, _("State machine missmatch")); break; } token = lex_get_token(lc, T_SKIP_EOL); } while (token == T_IDENTIFIER); if (token != T_EOB) { scan_err1(lc, _("Expected a end of block }, got: %s"), lc->str); } if (pass == 1 && !add_address((dlist **)(item->value), IPADDR::R_MULTIPLE, htons(port), family, hostname_str, port_str, errmsg, sizeof(errmsg))) { scan_err3(lc, _("Can't add hostname(%s) and port(%s) to addrlist (%s)"), hostname_str, port_str, errmsg); } token = scan_to_next_not_eol(lc); } while ((token == T_IDENTIFIER || token == T_UNQUOTED_STRING));