2014-05-14 22:08:42 -04:00
|
|
|
/*
|
|
|
|
* str.c
|
2014-06-27 01:24:40 -04:00
|
|
|
* Copyright (C) 2014 c9s <yoanlin93@gmail.com>
|
2014-05-14 22:08:42 -04:00
|
|
|
*
|
|
|
|
* Distributed under terms of the MIT license.
|
|
|
|
*/
|
2014-06-01 18:58:02 -04:00
|
|
|
#include "config.h"
|
2014-05-14 22:08:42 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2014-05-20 11:54:57 -04:00
|
|
|
#include "r3.h"
|
2015-11-17 08:17:18 -05:00
|
|
|
#include "r3_slug.h"
|
2015-11-17 08:11:12 -05:00
|
|
|
#include "str.h"
|
2014-06-02 03:47:02 -04:00
|
|
|
#include "slug.h"
|
2014-05-20 13:50:15 -04:00
|
|
|
#include "zmalloc.h"
|
2014-05-14 22:08:42 -04:00
|
|
|
|
2018-02-20 12:18:43 -05:00
|
|
|
static const char * strnchr(const char* str, unsigned int len, int ch) {
|
2016-03-21 22:23:37 -04:00
|
|
|
for (unsigned int i = 0; i < len; i++) {
|
|
|
|
if (str[i] == ch) return str + i;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r3_pattern_to_opcode(const char * pattern, unsigned int len) {
|
2014-05-23 01:49:18 -04:00
|
|
|
if ( strncmp(pattern, "\\w+",len) == 0 ) {
|
2014-05-23 10:58:20 -04:00
|
|
|
return OP_EXPECT_MORE_WORDS;
|
|
|
|
}
|
|
|
|
if ( strncmp(pattern, "[0-9a-z]+",len) == 0 || strncmp(pattern, "[a-z0-9]+",len) == 0 ) {
|
|
|
|
return OP_EXPECT_MORE_WORDS;
|
|
|
|
}
|
|
|
|
if ( strncmp(pattern, "[a-z]+",len) == 0 ) {
|
|
|
|
return OP_EXPECT_MORE_ALPHA;
|
2014-05-22 23:25:25 -04:00
|
|
|
}
|
2014-05-23 01:49:18 -04:00
|
|
|
if ( strncmp(pattern, "\\d+", len) == 0 ) {
|
2014-05-23 10:58:20 -04:00
|
|
|
return OP_EXPECT_MORE_DIGITS;
|
|
|
|
}
|
|
|
|
if ( strncmp(pattern, "[0-9]+", len) == 0 ) {
|
|
|
|
return OP_EXPECT_MORE_DIGITS;
|
2014-05-22 23:25:25 -04:00
|
|
|
}
|
2014-05-23 01:49:18 -04:00
|
|
|
if ( strncmp(pattern, "[^/]+", len) == 0 ) {
|
2014-05-22 23:25:25 -04:00
|
|
|
return OP_EXPECT_NOSLASH;
|
|
|
|
}
|
2014-05-23 01:49:18 -04:00
|
|
|
if ( strncmp(pattern, "[^-]+", len) == 0 ) {
|
2014-05-22 23:28:19 -04:00
|
|
|
return OP_EXPECT_NODASH;
|
|
|
|
}
|
2014-05-22 23:25:25 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-22 23:18:15 -04:00
|
|
|
|
2014-05-15 10:57:13 -04:00
|
|
|
|
2014-06-03 08:50:19 -04:00
|
|
|
char * r3_inside_slug(const char * needle, int needle_len, char *offset, char **errstr) {
|
2014-05-18 06:58:31 -04:00
|
|
|
char * s1 = offset;
|
|
|
|
char * s2 = offset;
|
|
|
|
|
2014-05-21 10:05:10 -04:00
|
|
|
short found_s1 = 0;
|
|
|
|
short found_s2 = 0;
|
|
|
|
|
2014-05-21 08:30:35 -04:00
|
|
|
while( s1 >= needle && (s1 - needle < needle_len) ) {
|
2014-05-18 06:58:31 -04:00
|
|
|
if ( *s1 == '{' ) {
|
2014-05-21 10:05:10 -04:00
|
|
|
found_s1 = 1;
|
2014-05-18 06:58:31 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
s1--;
|
|
|
|
}
|
|
|
|
|
2014-05-31 14:56:29 -04:00
|
|
|
const char * end = needle + needle_len;
|
2014-05-21 08:30:35 -04:00
|
|
|
while( (s2 + 1) < end ) {
|
2014-05-18 06:58:31 -04:00
|
|
|
if ( *s2 == '}' ) {
|
2014-05-21 10:05:10 -04:00
|
|
|
found_s2 = 1;
|
2014-05-18 06:58:31 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
s2++;
|
|
|
|
}
|
2014-05-21 10:05:10 -04:00
|
|
|
if (found_s1 && found_s2) {
|
2014-05-18 06:58:31 -04:00
|
|
|
return s1;
|
|
|
|
}
|
2014-06-01 08:51:47 -04:00
|
|
|
if (found_s1 || found_s2) {
|
|
|
|
// wrong slug pattern
|
|
|
|
if(errstr) {
|
2014-06-01 18:48:00 -04:00
|
|
|
asprintf(errstr, "Incomplete slug pattern");
|
2014-06-01 08:51:47 -04:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-18 06:58:31 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-20 12:18:43 -05:00
|
|
|
const char * r3_slug_find_placeholder(const char *s1, unsigned int str_len, unsigned int *len) {
|
|
|
|
const char *c;
|
|
|
|
const char *s2;
|
2014-05-18 06:58:31 -04:00
|
|
|
int cnt = 0;
|
2018-02-21 03:11:53 -05:00
|
|
|
if ((c = strnchr(s1, str_len, '{'))) {
|
2014-05-18 06:58:31 -04:00
|
|
|
// find closing '}'
|
|
|
|
s2 = c;
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int j = str_len - (c - s1);
|
|
|
|
for (unsigned int i = 0; i < j; i++) {
|
2014-05-18 06:58:31 -04:00
|
|
|
if (*s2 == '{' )
|
|
|
|
cnt++;
|
|
|
|
else if (*s2 == '}' )
|
|
|
|
cnt--;
|
|
|
|
if (cnt == 0)
|
|
|
|
break;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (cnt!=0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(len) {
|
|
|
|
*len = s2 - c + 1;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* given a slug string, duplicate the pattern string of the slug
|
|
|
|
*/
|
2018-02-20 12:18:43 -05:00
|
|
|
const char * r3_slug_find_pattern(const char *s1, unsigned int str_len, unsigned int *len) {
|
|
|
|
const char *c;
|
|
|
|
const char *s2;
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int cnt = 1;
|
|
|
|
if ( (c = strnchr(s1, str_len, ':')) ) {
|
2014-05-18 06:58:31 -04:00
|
|
|
c++;
|
|
|
|
// find closing '}'
|
|
|
|
s2 = c;
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int j = str_len - (c - s1);
|
|
|
|
for (unsigned int i = 0; i < j; i++) {
|
2014-05-18 06:58:31 -04:00
|
|
|
if (*s2 == '{' )
|
|
|
|
cnt++;
|
|
|
|
else if (*s2 == '}' )
|
|
|
|
cnt--;
|
|
|
|
if (cnt == 0)
|
|
|
|
break;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
if (cnt!=0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-18 08:08:43 -04:00
|
|
|
*len = s2 - c;
|
|
|
|
return c;
|
2014-05-18 06:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-26 10:00:23 -04:00
|
|
|
/**
|
|
|
|
* given a slug string, duplicate the parameter name string of the slug
|
|
|
|
*/
|
2018-02-20 12:18:43 -05:00
|
|
|
const char * r3_slug_find_name(const char *s1, unsigned int str_len, unsigned int *len) {
|
|
|
|
const char * c;
|
|
|
|
const char * s2;
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int plholder;
|
2018-02-21 03:11:53 -05:00
|
|
|
if ((c = r3_slug_find_placeholder(s1, str_len, &plholder))) {
|
2016-03-21 22:23:37 -04:00
|
|
|
c++;
|
2018-02-21 03:11:53 -05:00
|
|
|
if (( s2 = strnchr(c, plholder, ':') )) {
|
2016-03-21 22:23:37 -04:00
|
|
|
*len = s2 - c;
|
|
|
|
return c;
|
|
|
|
} else {
|
|
|
|
*len = plholder - 2;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return NULL;
|
2015-08-26 10:00:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-15 08:38:07 -04:00
|
|
|
/**
|
|
|
|
* @param char * sep separator
|
|
|
|
*/
|
2016-03-21 22:23:37 -04:00
|
|
|
char * r3_slug_compile(const char * str, unsigned int len)
|
2014-05-15 08:38:07 -04:00
|
|
|
{
|
2018-02-20 12:18:43 -05:00
|
|
|
const char *s1 = NULL;
|
|
|
|
char *o = NULL;
|
|
|
|
const char *pat = NULL;
|
2014-05-15 11:46:49 -04:00
|
|
|
char sep = '/';
|
|
|
|
|
2014-05-18 08:08:43 -04:00
|
|
|
|
|
|
|
// append prefix
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int s1_len;
|
|
|
|
s1 = r3_slug_find_placeholder(str, len, &s1_len);
|
2014-05-15 11:46:49 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
if ( !s1 ) {
|
|
|
|
return zstrndup(str,len);
|
2014-05-15 11:46:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char * out = NULL;
|
2016-03-21 22:23:37 -04:00
|
|
|
if (!(out = zcalloc(sizeof(char) * 200))) {
|
2014-05-15 11:46:49 -04:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
o = out;
|
2014-06-01 13:21:02 -04:00
|
|
|
strncat(o, "^", 1);
|
|
|
|
o++;
|
|
|
|
|
2014-05-18 08:08:43 -04:00
|
|
|
strncat(o, str, s1 - str); // string before slug
|
2014-05-15 11:46:49 -04:00
|
|
|
o += (s1 - str);
|
|
|
|
|
2014-05-15 08:38:07 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int pat_len;
|
|
|
|
pat = r3_slug_find_pattern(s1, s1_len, &pat_len);
|
2014-05-15 11:46:49 -04:00
|
|
|
|
2014-05-18 08:08:43 -04:00
|
|
|
if (pat) {
|
2014-05-15 11:46:49 -04:00
|
|
|
*o = '(';
|
|
|
|
o++;
|
2014-05-18 08:08:43 -04:00
|
|
|
strncat(o, pat, pat_len );
|
|
|
|
o += pat_len;
|
2014-05-15 11:46:49 -04:00
|
|
|
*o = ')';
|
|
|
|
o++;
|
2014-05-15 08:38:07 -04:00
|
|
|
} else {
|
2014-05-15 11:46:49 -04:00
|
|
|
sprintf(o, "([^%c]+)", sep);
|
2014-05-18 08:08:43 -04:00
|
|
|
o+= strlen("([^*]+)");
|
2014-05-15 08:38:07 -04:00
|
|
|
}
|
2014-05-18 08:08:43 -04:00
|
|
|
s1 += s1_len;
|
2016-03-21 22:23:37 -04:00
|
|
|
strncat(o, s1, len - (s1 - str)); // string after slug
|
2014-05-15 11:46:49 -04:00
|
|
|
return out;
|
2014-05-15 08:38:07 -04:00
|
|
|
}
|
2014-05-14 22:08:42 -04:00
|
|
|
|
|
|
|
|
2014-05-15 00:53:48 -04:00
|
|
|
char * ltrim_slash(char* str)
|
|
|
|
{
|
|
|
|
char * p = str;
|
|
|
|
while (*p == '/') p++;
|
2014-05-20 13:50:15 -04:00
|
|
|
return zstrdup(p);
|
2014-05-15 00:53:48 -04:00
|
|
|
}
|
|
|
|
|
2014-05-15 06:02:10 -04:00
|
|
|
void print_indent(int level) {
|
|
|
|
int len = level * 2;
|
|
|
|
while(len--) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
2014-05-16 12:35:56 -04:00
|
|
|
|
2014-06-18 08:47:06 -04:00
|
|
|
|
|
|
|
|
2014-05-16 19:26:47 -04:00
|
|
|
#ifndef HAVE_STRDUP
|
2014-05-20 13:50:15 -04:00
|
|
|
char *zstrdup(const char *s) {
|
2014-05-16 12:35:56 -04:00
|
|
|
char *out;
|
2014-05-16 15:17:51 -04:00
|
|
|
int count = 0;
|
2014-05-16 12:35:56 -04:00
|
|
|
while( s[count] )
|
|
|
|
++count;
|
|
|
|
++count;
|
2014-05-20 13:50:15 -04:00
|
|
|
out = zmalloc(sizeof(char) * count);
|
2014-05-16 12:35:56 -04:00
|
|
|
out[--count] = 0;
|
|
|
|
while( --count >= 0 )
|
|
|
|
out[count] = s[count];
|
|
|
|
return out;
|
|
|
|
}
|
2014-05-16 19:26:47 -04:00
|
|
|
#endif
|
2014-05-16 12:35:56 -04:00
|
|
|
|
2014-06-18 08:47:06 -04:00
|
|
|
|
|
|
|
|
2014-05-16 19:26:47 -04:00
|
|
|
#ifndef HAVE_STRNDUP
|
2014-05-21 04:36:50 -04:00
|
|
|
char *zstrndup(const char *s, int n) {
|
2014-05-16 12:35:56 -04:00
|
|
|
char *out;
|
2014-05-16 15:17:51 -04:00
|
|
|
int count = 0;
|
2014-05-16 12:35:56 -04:00
|
|
|
while( count < n && s[count] )
|
|
|
|
++count;
|
|
|
|
++count;
|
2014-05-20 13:50:15 -04:00
|
|
|
out = zmalloc(sizeof(char) * count);
|
2014-05-16 12:35:56 -04:00
|
|
|
out[--count] = 0;
|
|
|
|
while( --count >= 0 )
|
|
|
|
out[count] = s[count];
|
|
|
|
return out;
|
|
|
|
}
|
2014-05-16 19:26:47 -04:00
|
|
|
#endif
|