aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Brendan Taylor <whateley@gmail.com>2011-02-14 22:51:46 -0700
committerGravatar Brendan Taylor <whateley@gmail.com>2011-02-14 22:54:54 -0700
commitdb3984b609153ff213233f1a78c8895fdb525350 (patch)
tree24e6b2959d08b8cadb93853cb3620681f6f05e64
parent59af2a8499160a369aceed1bd5cb32109fb5e77c (diff)
parent700332fac498169927ced24731ce1278369b1425 (diff)
Merge branch 'experimental' into cleaning-commenting
Conflicts: src/callbacks.c src/uzbl-core.c src/uzbl-core.h
-rw-r--r--Makefile23
-rw-r--r--README9
-rw-r--r--docs/README.cookies63
-rw-r--r--docs/README.uzbl-event-manager39
-rw-r--r--examples/config/config16
-rw-r--r--examples/data/plugins/bind.py4
-rw-r--r--examples/data/plugins/cookies.py56
-rw-r--r--examples/data/plugins/history.py129
-rw-r--r--examples/data/plugins/keycmd.py5
-rwxr-xr-xexamples/data/scripts/follow.sh2
-rwxr-xr-xexamples/data/scripts/uzbl-cookie-daemon677
-rw-r--r--examples/uzbl-cookie-manager.c381
-rw-r--r--extras/vim/syntax/uzbl.vim2
-rw-r--r--src/callbacks.c79
-rw-r--r--src/callbacks.h3
-rw-r--r--src/cookie-jar.c286
-rw-r--r--src/cookie-jar.h12
-rw-r--r--src/io.c20
-rw-r--r--src/util.c8
-rw-r--r--src/util.h1
-rwxr-xr-xsrc/uzbl-browser7
-rw-r--r--src/uzbl-core.c511
-rw-r--r--src/uzbl-core.h26
-rw-r--r--tests/test-command.c40
24 files changed, 538 insertions, 1861 deletions
diff --git a/Makefile b/Makefile
index f33a626..62bc9b5 100644
--- a/Makefile
+++ b/Makefile
@@ -41,7 +41,7 @@ HEAD = $(wildcard src/*.h)
OBJ = $(foreach obj, $(SRC:.c=.o), $(notdir $(obj)))
LOBJ = $(foreach obj, $(SRC:.c=.lo), $(notdir $(obj)))
-all: uzbl-browser uzbl-cookie-manager
+all: uzbl-browser
VPATH:=src
@@ -49,11 +49,7 @@ ${OBJ}: ${HEAD}
uzbl-core: ${OBJ}
-uzbl-cookie-manager: examples/uzbl-cookie-manager.o util.o
- @echo -e "\n${CC} -o $@ examples/uzbl-cookie-manager.o util.o ${shell pkg-config --libs glib-2.0 libsoup-2.4}"
- @${CC} -o $@ examples/uzbl-cookie-manager.o util.o $(shell pkg-config --libs glib-2.0 libsoup-2.4)
-
-uzbl-browser: uzbl-core uzbl-cookie-manager
+uzbl-browser: uzbl-core
# the 'tests' target can never be up to date
.PHONY: tests
@@ -87,10 +83,8 @@ test-uzbl-browser-sandbox: uzbl-browser
make DESTDIR=./sandbox RUN_PREFIX=`pwd`/sandbox/usr/local install-uzbl-browser
make DESTDIR=./sandbox RUN_PREFIX=`pwd`/sandbox/usr/local install-example-data
cp -np ./misc/env.sh ./sandbox/env.sh
- -source ./sandbox/env.sh && uzbl-cookie-manager -v
-source ./sandbox/env.sh && uzbl-event-manager restart -avv
source ./sandbox/env.sh && uzbl-browser --uri http://www.uzbl.org --verbose
- kill `cat ./sandbox/home/.cache/uzbl/cookie_daemon_socket.pid`
source ./sandbox/env.sh && uzbl-event-manager stop -ivv
make DESTDIR=./sandbox uninstall
rm -rf ./sandbox/usr
@@ -100,18 +94,20 @@ test-uzbl-tabbed-sandbox: uzbl-browser
make DESTDIR=./sandbox RUN_PREFIX=`pwd`/sandbox/usr/local install-uzbl-browser
make DESTDIR=./sandbox RUN_PREFIX=`pwd`/sandbox/usr/local install-example-data
cp -np ./misc/env.sh ./sandbox/env.sh
- -source ./sandbox/env.sh && uzbl-cookie-manager -v
-source ./sandbox/env.sh && uzbl-event-manager restart -avv
source ./sandbox/env.sh && ./sandbox/home/.local/share/uzbl/scripts/uzbl-tabbed
- kill `cat ./sandbox/home/.cache/uzbl/cookie_daemon_socket.pid`
source ./sandbox/env.sh && uzbl-event-manager stop -ivv
make DESTDIR=./sandbox uninstall
rm -rf ./sandbox/usr
clean:
rm -f uzbl-core
- rm -f uzbl-cookie-manager
- rm -f *.o *.lo
+ rm -f uzbl-core.o
+ rm -f events.o
+ rm -f callbacks.o
+ rm -f inspector.o
+ rm -f cookie-jar.o
+ rm -f util.o
find ./examples/ -name "*.pyc" -delete
cd ./tests/; $(MAKE) clean
rm -rf ./sandbox/
@@ -137,9 +133,8 @@ install-uzbl-core: all install-dirs
chmod 755 $(INSTALLDIR)/share/uzbl/examples/data/scripts/*
install -m755 uzbl-core $(INSTALLDIR)/bin/uzbl-core
-install-uzbl-browser: uzbl-cookie-manager install-dirs
+install-uzbl-browser: install-dirs
install -m755 src/uzbl-browser $(INSTALLDIR)/bin/uzbl-browser
- install -m755 uzbl-cookie-manager $(INSTALLDIR)/bin/uzbl-cookie-manager
install -m755 examples/data/scripts/uzbl-event-manager $(INSTALLDIR)/bin/uzbl-event-manager
mv $(INSTALLDIR)/bin/uzbl-browser $(INSTALLDIR)/bin/uzbl-browser.bak
sed 's#^PREFIX=.*#PREFIX=$(RUN_PREFIX)#' < $(INSTALLDIR)/bin/uzbl-browser.bak > $(INSTALLDIR)/bin/uzbl-browser
diff --git a/README b/README
index d1f2357..d20cbd2 100644
--- a/README
+++ b/README
@@ -183,11 +183,6 @@ The following commands are recognized:
the end, so the argument numbers will be higher.
* `sync_sh <command>`
- Synchronous version of `sh`, See `sync_spawn`.
-* `talk_to_socket <socketfile> <tokens>`
- - Send a message to `<socketfile>` and wait for a response. `<tokens>` are
- concatenated and separated by ASCII NUL bytes.
- - Expects the socket type to be `SOCK_SEQPACKET` (see `connect(2)`).
- - Waits for 500ms for a response.
* `exit`
- Closes `uzbl`.
* `search <string>`
@@ -775,12 +770,12 @@ Events/requests which the EM and its plugins listens for
move the cursor back by one character.
* `START_COMPLETION`: TODO explain completion
* `BLACKLIST_COOKIE`: add a rule for blacklisting cookies
- - `request BLACKLIST_COOKIE <component> <regexp>`: Blacklist cookies where
+ - `request BLACKLIST_COOKIE [<component> <regexp>]*`: Blacklist cookies where
`<component>` matches `<regexp>`. `<component>` is one of `domain`,
`path`, `name`, `value`, `scheme` or `expires`.
* `WHITELIST_COOKIE`: add a rule for whitelisting cookies (if any whitelist is
set then only cookies that are whitelisted cookies will be used)
- - `request WHITELIST_COOKIE <component> <regexp>`: Whitelist cookies where
+ - `request WHITELIST_COOKIE [<component> <regexp>]*`: Whitelist cookies where
`<component>` matches `<regexp>`. `<component>` is one of `domain`,
`path`, `name`, `value`, `scheme` or `expires`.
diff --git a/docs/README.cookies b/docs/README.cookies
deleted file mode 100644
index 148603f..0000000
--- a/docs/README.cookies
+++ /dev/null
@@ -1,63 +0,0 @@
-# Cookies and Uzbl #
-
-The speed of cookie lookups is important, since a single page load can involve
-dozens of HTTP requests, each of which needs a separate cookie lookup (since
-another instance of uzbl may have obtained new cookies for a site).
-
-It is possible handle cookie lookup (and storage) using a `spawn_async` cookie
-handler, but spawning new processes is inherently slow so a `talk_to_socket`
-cookie daemon (like the default uzbl-cookie-manager) is recommended.
-
-## uzbl-cookie-manager ##
-
-uzbl-cookie-manager is a cookie daemon based on libsoup's SoupCookieJar. Cookies
-are stored in a file in the Mozilla cookies.txt format (default location
-$XDG_DATA_HOME/.local/share/cookies.txt).
-
-### uzbl-cookie-manager Whitelist ###
-
-If a whitelist file is present (default location
-$XDG_CONFIG_HOME/uzbl/cookie_whitelist), then website attempts to set cookies
-will be ignored unless the site's domain is present in the whitelist.
-
-The whitelist can contain comment lines beginning with `#`, and domain lines. A
-domain line beginning with . will whitelist the given domain name and any
-subdomain of it. Otherwise only exact matches of the domain are whitelisted.
-
-For instance, given this whitelist file:
-
- example.com
- .uzbl.org
-
-uzbl-cookie-manager would accept cookies for example.com, uzbl.org and
-www.uzbl.org, but ignore cookies set for www.example.com (and any other
-domain that is not a subdomain of uzbl.org).
-
-## uzbl-cookie-daemon ##
-
-uzbl-cookie-daemon is a Python cookie daemon based on Python's cookielib.
-Cookielib's lookup algorithm isn't very efficient for our needs, so
-uzbl-cookie-daemon is noticeably slow.
-
-## Cookie Daemon Protocol ##
-
-When uzbl's `cookie_handler` variable is set to `talk_to_socket path`, uzbl
-connects to the Unix domain socket located at `path`. uzbl will send a cookie
-lookup request on this socket every time it makes an HTTP request. The format of
-this lookup request is:
-
- GET\0scheme\0host\0path\0
-
-where `\0` is the null character, `scheme` is the URL scheme (http or https),
-`host` is the hostname from the URL and `path` is the requested path. The cookie
-daemon should respond with the names and values of cookies that match the
-request, in the format used by the `Cookie` header, terminated with a `\0`.
-
-When a website adds, deletes or changes a cookie, uzbl notifies the cookie
-daemon with a request in the format:
-
- PUT\0scheme\0host\0path\0name=value\0
-
-where `scheme`, `host` and `path` are (approximately) as above, and `name=value`
-is the cookie name-value pair to store. The cookie daemon should respond by
-writing `\0` to the socket.
diff --git a/docs/README.uzbl-event-manager b/docs/README.uzbl-event-manager
index 074811e..23e185c 100644
--- a/docs/README.uzbl-event-manager
+++ b/docs/README.uzbl-event-manager
@@ -83,17 +83,36 @@ events. If any whitelist is set, then any cookie that is not whitelisted will
be rejected. Otherwise, only cookies that have been blacklisted will be
rejected.
-BLACKLIST_COOKIE <part> <re>
- Adds a new blacklist filter. cookies where the component specified by
- `part` matches the regular expression `re` will be filtered. part can be
- either 0-5 or any of the symbolic names domain, path, name, value, scheme,
- expires
+BLACKLIST_COOKIE [<component> <re>]*
+ Adds a new blacklist filter. cookies where the components specified by
+ `component` matches the regular expression `re` will be filtered. component
+ may be either 0-5 or any of the symbolic names domain, path, name, value,
+ scheme, expires
- for example to block all cookies which name is "__utm" followed by a single
+ for example to block all cookies which name is "__utm" followed by a single
character (google analytics cookies) do.
request BLACKLIST_COOKIE name '^__utm.$'
-WHITELIST_COOKIE <part> <re>
- Adds a new whitelist filter. cookies where the component specified by
- `part` matches the regular expression `re` will be allowed. part can be any
- of the parts allowed for the BLACKLIST_COOKIE event
+WHITELIST_COOKIE [<component> <re>]*
+ Adds a new whitelist filter. cookies where the components specified by
+ `component` matches the regular expression `re` will be allowed. component
+ may be any of the components allowed for the BLACKLIST_COOKIE event
+
+### history.py ###
+- Status bar command history
+- Connects To: (KEYCMD_EXEC, HISTORY_PREV, HISTORY_NEXT, HISTORY_SEARCH)
+
+Records commands that are typed into the status bar so that they can be
+recalled. The same history is shared by all uzbl instances connected to the
+same event manager.
+
+HISTORY_PREV
+ Iterates backwards through commands that have been issued (filtered by the
+ last HISTORY_SEARCH if applicable).
+
+HISTORY_NEXT
+ Iterates forwards through commands that have been issued (filtered by the
+ last HISTORY_SEARCH if applicable).
+
+HISTORY_SEARCH <string>
+ Searches backwards through command history for an exact string.
diff --git a/examples/config/config b/examples/config/config
index a2ecdb8..bc07b0c 100644
--- a/examples/config/config
+++ b/examples/config/config
@@ -129,7 +129,11 @@ set progress.pending =
set useragent = Uzbl (Webkit @{WEBKIT_MAJOR}.@{WEBKIT_MINOR}) (@(+uname -sm)@ [@ARCH_UZBL])
# === Configure cookie blacklist ========================================================
-# Drop google analytics tracking cookies
+
+# Accept 'session cookies' from uzbl.org (when you have a whitelist all other cookies are dropped)
+#request WHITELIST_COOKIE domain 'uzbl.org$' expires '^$'
+
+# Drop google analytics tracking cookies (applied after whitelists if any)
#request BLACKLIST_COOKIE name '^__utm.$'
# === Key binding configuration ==============================================
@@ -172,6 +176,7 @@ set ebind = @mode_bind global,-insert
# Resets keycmd and returns to default mode.
@on_event ESCAPE @set_mode
+@on_event ESCAPE event KEYCMD_CLEAR
@bind <Escape> = event ESCAPE
@bind <Ctrl>[ = event ESCAPE
@@ -190,6 +195,15 @@ set ebind = @mode_bind global,-insert
@ebind <Ctrl>a = event SET_CURSOR_POS 0
@ebind <Ctrl>e = event SET_CURSOR_POS -1
+@ebind <Up> = event HISTORY_PREV
+@ebind <Down> = event HISTORY_NEXT
+@ebind <Ctrl>r<search:>_ = event HISTORY_SEARCH %s
+# Keycmd injection/append examples.
+#@ebind <Ctrl>su = event INJECT_KEYCMD \@uri
+#@ebind <Ctrl>st = event INJECT_KEYCMD \@title
+#@ebind <Ctrl>du = event APPEND_KEYCMD \@uri
+#@ebind <Ctrl>dt = event APPEND_KEYCMD \@title
+
# --- Mouse bindings ---------------------------------------------------------
# Middle click open in new window
diff --git a/examples/data/plugins/bind.py b/examples/data/plugins/bind.py
index 5b13476..41f96c5 100644
--- a/examples/data/plugins/bind.py
+++ b/examples/data/plugins/bind.py
@@ -379,7 +379,6 @@ def mode_changed(uzbl, mode):
if mode != 'stack':
uzbl.bindlet.reset()
- uzbl.clear_keycmd()
def match_and_exec(uzbl, bind, depth, keylet, bindlet):
@@ -421,7 +420,6 @@ def match_and_exec(uzbl, bind, depth, keylet, bindlet):
if not has_args or on_exec:
del uzbl.config['mode']
bindlet.reset()
- uzbl.clear_current()
return True
@@ -469,3 +467,5 @@ def init(uzbl):
'mode_bind': mode_bind,
'bindlet': Bindlet(uzbl),
})
+
+# vi: set et ts=4:
diff --git a/examples/data/plugins/cookies.py b/examples/data/plugins/cookies.py
index c9fe2c3..6ee8798 100644
--- a/examples/data/plugins/cookies.py
+++ b/examples/data/plugins/cookies.py
@@ -38,24 +38,38 @@ class TextStore(object):
self.filename = filename
def as_event(self, cookie):
+ """Convert cookie.txt row to uzbls cookie event format"""
+ scheme = {
+ 'TRUE' : 'https',
+ 'FALSE' : 'http'
+ }
if cookie[0].startswith("#HttpOnly_"):
domain = cookie[0][len("#HttpOnly_"):]
elif cookie[0].startswith('#'):
return None
else:
domain = cookie[0]
- return (domain,
- cookie[2],
- cookie[5],
- cookie[6],
- 'https' if cookie[3] == 'TRUE' else 'http',
- cookie[4])
+ try:
+ return (domain,
+ cookie[2],
+ cookie[5],
+ cookie[6],
+ scheme[cookie[3]],
+ cookie[4])
+ except (KeyError,IndexError):
+ # Let malformed rows pass through like comments
+ return None
def as_file(self, cookie):
+ """Convert cookie event to cookie.txt row"""
+ secure = {
+ 'https' : 'TRUE',
+ 'http' : 'FALSE'
+ }
return (cookie[0],
'TRUE' if cookie[0].startswith('.') else 'FALSE',
cookie[1],
- 'TRUE' if cookie[4] == 'https' else 'FALSE',
+ secure[cookie[4]],
cookie[5],
cookie[2],
cookie[3])
@@ -92,8 +106,11 @@ DefaultStore = TextStore(os.path.join(xdg_data_home, 'uzbl/cookies.txt'))
SessionStore = TextStore(os.path.join(xdg_data_home, 'uzbl/session-cookies.txt'))
def match_list(_list, cookie):
- for component, match in _list:
- if match(cookie[component]) is not None:
+ for matcher in _list:
+ for component, match in matcher:
+ if match(cookie[component]) is None:
+ break
+ else:
return True
return False
@@ -144,18 +161,21 @@ def delete_cookie(uzbl, cookie):
store.delete_cookie(cookie, splitted)
# add a cookie matcher to a whitelist or a blacklist.
-# a matcher is a (component, re) tuple that matches a cookie when the
+# a matcher is a list of (component, re) tuples that matches a cookie when the
# "component" part of the cookie matches the regular expression "re".
# "component" is one of the keys defined in the variable "symbolic" above,
# or the index of a component of a cookie tuple.
def add_cookie_matcher(_list, arg):
- component, regexp = splitquoted(arg)
- try:
- component = symbolic[component]
- except KeyError:
- component = int(component)
- assert component <= 5
- _list.append((component, re.compile(regexp).search))
+ args = splitquoted(arg)
+ mlist = []
+ for (component, regexp) in zip(args[0::2], args[1::2]):
+ try:
+ component = symbolic[component]
+ except KeyError:
+ component = int(component)
+ assert component <= 5
+ mlist.append((component, re.compile(regexp).search))
+ _list.append(mlist)
def blacklist(uzbl, arg):
add_cookie_matcher(uzbl.cookie_blacklist, arg)
@@ -174,3 +194,5 @@ def init(uzbl):
'cookie_blacklist' : [],
'cookie_whitelist' : []
})
+
+# vi: set et ts=4:
diff --git a/examples/data/plugins/history.py b/examples/data/plugins/history.py
new file mode 100644
index 0000000..5e9e4e1
--- /dev/null
+++ b/examples/data/plugins/history.py
@@ -0,0 +1,129 @@
+import random
+
+shared_history = {'':[]}
+
+class History(object):
+ def __init__(self, uzbl):
+ self.uzbl = uzbl
+ self._temporary = []
+ self.prompt = ''
+ self.cursor = None
+ self.__temp_tail = False
+ self.search_key = None
+
+ def prev(self):
+ if self.cursor is None:
+ self.cursor = len(self) - 1
+ else:
+ self.cursor -= 1
+
+ if self.search_key:
+ while self.cursor >= 0 and self.search_key not in self[self.cursor]:
+ self.cursor -= 1
+
+ if self.cursor < 0 or len(self) == 0:
+ self.cursor = -1
+ return random.choice(end_messages)
+
+ return self[self.cursor]
+
+ def next(self):
+ if self.cursor is None:
+ return ''
+
+ self.cursor += 1
+
+ if self.search_key:
+ while self.cursor < len(self) and self.search_key not in self[self.cursor]:
+ self.cursor += 1
+
+ if self.cursor >= len(shared_history[self.prompt]):
+ self.cursor = None
+ self.search_key = None
+
+ if self._temporary:
+ return self._temporary.pop()
+ return ''
+
+ return self[self.cursor]
+
+ def change_prompt(self, prompt):
+ self.prompt = prompt
+ self._temporary = []
+ self.__temp_tail = False
+ if prompt not in shared_history:
+ shared_history[prompt] = []
+
+ def search(self, key):
+ self.search_key = key
+ self.cursor = None
+
+ def add(self, cmd):
+ if self._temporary:
+ self._temporary.pop()
+
+ shared_history[self.prompt].append(cmd)
+ self.cursor = None
+ self.search_key = None
+
+ def add_temporary(self, cmd):
+ assert not self._temporary
+
+ self._temporary.append(cmd)
+ self.cursor = len(self) - 1
+
+ def __getitem__(self, i):
+ if i < len(shared_history[self.prompt]):
+ return shared_history[self.prompt][i]
+ return self._temporary[i-len(shared_history)+1]
+
+ def __len__(self):
+ return len(shared_history[self.prompt]) + len(self._temporary)
+
+ def __str__(self):
+ return "(History %s, %s)" % (self.cursor, self.prompt)
+
+def keycmd_exec(uzbl, keylet):
+ cmd = keylet.get_keycmd()
+ if cmd:
+ uzbl.history.add(cmd)
+
+def history_prev(uzbl, _x):
+ cmd = uzbl.keylet.get_keycmd()
+ if uzbl.history.cursor is None and cmd:
+ uzbl.history.add_temporary(cmd)
+
+ uzbl.set_keycmd(uzbl.history.prev())
+ uzbl.logger.debug('PREV %s' % uzbl.history)
+
+def history_next(uzbl, _x):
+ cmd = uzbl.keylet.get_keycmd()
+
+ uzbl.set_keycmd(uzbl.history.next())
+ uzbl.logger.debug('NEXT %s' % uzbl.history)
+
+def history_search(uzbl, key):
+ uzbl.history.search(key)
+ uzbl.send('event HISTORY_PREV')
+ uzbl.logger.debug('SEARCH %s %s' % (key, uzbl.history))
+
+end_messages = ('Look behind you, A three-headed monkey!', 'error #4: static from nylon underwear.', 'error #5: static from plastic slide rules.', 'error #6: global warming.', 'error #9: doppler effect.', 'error #16: somebody was calculating pi on the server.', 'error #19: floating point processor overflow.', 'error #21: POSIX compliance problem.', 'error #25: Decreasing electron flux.', 'error #26: first Saturday after first full moon in Winter.', 'error #64: CPU needs recalibration.', 'error #116: the real ttys became pseudo ttys and vice-versa.', 'error #229: wrong polarity of neutron flow.', 'error #330: quantum decoherence.', 'error #388: Bad user karma.', 'error #407: Route flapping at the NAP.', 'error #435: Internet shut down due to maintenance.')
+
+# plugin init hook
+def init(uzbl):
+ connect_dict(uzbl, {
+ 'KEYCMD_EXEC': keycmd_exec,
+ 'HISTORY_PREV': history_prev,
+ 'HISTORY_NEXT': history_next,
+ 'HISTORY_SEARCH': history_search
+ })
+
+ export_dict(uzbl, {
+ 'history' : History(uzbl)
+ })
+
+# plugin after hook
+def after(uzbl):
+ uzbl.on_set('keycmd_prompt', lambda uzbl, k, v: uzbl.history.change_prompt(v))
+
+# vi: set et ts=4:
diff --git a/examples/data/plugins/keycmd.py b/examples/data/plugins/keycmd.py
index 2fb2283..928c597 100644
--- a/examples/data/plugins/keycmd.py
+++ b/examples/data/plugins/keycmd.py
@@ -210,7 +210,7 @@ def modkey_addition_parse(uzbl, modkeys):
add_modkey_addition(uzbl, keys[:-1], keys[-1])
-def clear_keycmd(uzbl):
+def clear_keycmd(uzbl, *args):
'''Clear the keycmd for this uzbl instance.'''
k = uzbl.keylet
@@ -493,6 +493,7 @@ def init(uzbl):
'KEYCMD_DELETE': keycmd_delete,
'KEYCMD_EXEC_CURRENT': keycmd_exec_current,
'KEYCMD_STRIP_WORD': keycmd_strip_word,
+ 'KEYCMD_CLEAR': clear_keycmd,
'KEY_PRESS': key_press,
'KEY_RELEASE': key_release,
'MODKEY_ADDITION': modkey_addition_parse,
@@ -514,3 +515,5 @@ def init(uzbl):
'set_cursor_pos': set_cursor_pos,
'set_keycmd': set_keycmd,
})
+
+# vi: set et ts=4:
diff --git a/examples/data/scripts/follow.sh b/examples/data/scripts/follow.sh
index d1560bf..d7fe117 100755
--- a/examples/data/scripts/follow.sh
+++ b/examples/data/scripts/follow.sh
@@ -17,5 +17,5 @@ case $result in
echo 'event FORM_ACTIVE' > "$UZBL_FIFO" ;;
*XXXRESET_MODEXXX*)
# a link was selected, reset uzbl's input mode
- echo 'set mode=' > "$UZBL_FIFO" ;;
+ printf 'set mode=\nevent KEYCMD_CLEAR\n' > "$UZBL_FIFO" ;;
esac
diff --git a/examples/data/scripts/uzbl-cookie-daemon b/examples/data/scripts/uzbl-cookie-daemon
deleted file mode 100755
index 0b9bef9..0000000
--- a/examples/data/scripts/uzbl-cookie-daemon
+++ /dev/null
@@ -1,677 +0,0 @@
-#!/usr/bin/env python
-
-# The Python Cookie Daemon for Uzbl.
-# Copyright (c) 2009, Tom Adams <tom@holizz.com>
-# Copyright (c) 2009, Dieter Plaetinck <dieter@plaetinck.be>
-# Copyright (c) 2009, Mason Larobina <mason.larobina@gmail.com>
-# Copyright (c) 2009, Michael Fiano <axionix@gmail.com>
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-'''
-The Python Cookie Daemon
-========================
-
-This daemon is a re-write of the original cookies.py script found in uzbl's
-master branch. This script provides more functionality than the original
-cookies.py by adding numerous command line options to specify different cookie
-jar locations, socket locations, verbose output, etc. This functionality is
-very useful as it allows you to run multiple daemons at once serving cookies
-to different groups of uzbl instances as required.
-
-Keeping up to date
-==================
-
-Check the cookie daemon uzbl-wiki page for more information on where to
-find the latest version of the cookie_daemon.py
-
- http://www.uzbl.org/wiki/cookie_daemon.py
-
-Command line options
-====================
-
-Use the following command to get a full list of the cookie_daemon.py command
-line options:
-
- ./cookie_daemon.py --help
-
-Talking with uzbl
-=================
-
-In order to get uzbl to talk to a running cookie daemon you add the following
-to your uzbl config:
-
- set cookie_handler = talk_to_socket $XDG_CACHE_HOME/uzbl/cookie_daemon_socket
-
-Or if you prefer using the $HOME variable:
-
- set cookie_handler = talk_to_socket $HOME/.cache/uzbl/cookie_daemon_socket
-
-Todo list
-=========
-
- - Use a pid file to make force killing a running daemon possible.
-
-Reporting bugs / getting help
-=============================
-
-The best way to report bugs and or get help with the cookie daemon is to
-contact the maintainers it the #uzbl irc channel found on the Freenode IRC
-network (irc.freenode.org).
-'''
-
-import cookielib
-import os
-import sys
-import urllib2
-import select
-import socket
-import time
-import atexit
-from traceback import print_exc
-from signal import signal, SIGTERM
-from optparse import OptionParser
-from os.path import join
-
-try:
- import cStringIO as StringIO
-
-except ImportError:
- import StringIO
-
-
-# ============================================================================
-# ::: Default configuration section ::::::::::::::::::::::::::::::::::::::::::
-# ============================================================================
-
-def xdghome(key, default):
- '''Attempts to use the environ XDG_*_HOME paths if they exist otherwise
- use $HOME and the default path.'''
-
- xdgkey = "XDG_%s_HOME" % key
- if xdgkey in os.environ.keys() and os.environ[xdgkey]:
- return os.environ[xdgkey]
-
- return join(os.environ['HOME'], default)
-
-# Setup xdg paths.
-CACHE_DIR = join(xdghome('CACHE', '.cache/'), 'uzbl/')
-DATA_DIR = join(xdghome('DATA', '.local/share/'), 'uzbl/')
-CONFIG_DIR = join(xdghome('CONFIG', '.config/'), 'uzbl/')
-
-# Ensure data paths exist.
-for path in [CACHE_DIR, DATA_DIR, CONFIG_DIR]:
- if not os.path.exists(path):
- os.makedirs(path)
-
-# Default config
-config = {
-
- # Default cookie jar, whitelist, and daemon socket locations.
- 'cookie_jar': join(DATA_DIR, 'cookies.txt'),
- 'cookie_whitelist': join(CONFIG_DIR, 'cookie_whitelist'),
- 'cookie_socket': join(CACHE_DIR, 'cookie_daemon_socket'),
-
- # Don't use a cookie whitelist policy by default.
- 'use_whitelist': False,
-
- # Time out after x seconds of inactivity (set to 0 for never time out).
- # WARNING: Do not use this option if you are manually launching the daemon.
- 'daemon_timeout': 0,
-
- # Daemonise by default.
- 'daemon_mode': True,
-
- # Optionally print helpful debugging messages to the terminal.
- 'verbose': False,
-
-} # End of config dictionary.
-
-
-# ============================================================================
-# ::: End of configuration section :::::::::::::::::::::::::::::::::::::::::::
-# ============================================================================
-
-
-_SCRIPTNAME = os.path.basename(sys.argv[0])
-def echo(msg):
- '''Prints only if the verbose flag has been set.'''
-
- if config['verbose']:
- sys.stderr.write("%s: %s\n" % (_SCRIPTNAME, msg))
-
-
-def error(msg):
- '''Prints error message and exits.'''
-
- sys.stderr.write("%s: error: %s\n" % (_SCRIPTNAME, msg))
- sys.exit(1)
-
-
-def mkbasedir(filepath):
- '''Create the base directories of the file in the file-path if the dirs
- don't exist.'''
-
- dirname = os.path.dirname(filepath)
- if not os.path.exists(dirname):
- echo("creating dirs: %r" % dirname)
- os.makedirs(dirname)
-
-
-def daemon_running(cookie_socket):
- '''Check if another process (hopefully a cookie_daemon.py) is listening
- on the cookie daemon socket. If another process is found to be
- listening on the socket exit the daemon immediately and leave the
- socket alone. If the connect fails assume the socket has been abandoned
- and delete it (to be re-created in the create socket function).'''
-
- if not os.path.exists(cookie_socket):
- return False
-
- if os.path.isfile(cookie_socket):
- raise Exception("regular file at %r is not a socket" % cookie_socket)
-
-
- if os.path.isdir(cookie_socket):
- raise Exception("directory at %r is not a socket" % cookie_socket)
-
- try:
- sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
- sock.connect(cookie_socket)
- sock.close()
- echo("detected daemon listening on %r" % cookie_socket)
- return True
-
- except socket.error:
- # Failed to connect to cookie_socket so assume it has been
- # abandoned by another cookie daemon process.
- if os.path.exists(cookie_socket):
- echo("deleting abandoned socket at %r" % cookie_socket)
- os.remove(cookie_socket)
-
- return False
-
-
-def send_command(cookie_socket, cmd):
- '''Send a command to a running cookie daemon.'''
-
- if not daemon_running(cookie_socket):
- return False
-
- try:
- sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
- sock.connect(cookie_socket)
- sock.send(cmd)
- sock.close()
- echo("sent command %r to %r" % (cmd, cookie_socket))
- return True
-
- except socket.error:
- print_exc()
- error("failed to send message %r to %r" % (cmd, cookie_socket))
- return False
-
-
-def kill_daemon(cookie_socket):
- '''Send the "EXIT" command to running cookie_daemon.'''
-
- if send_command(cookie_socket, "EXIT"):
- # Now ensure the cookie_socket is cleaned up.
- start = time.time()
- while os.path.exists(cookie_socket):
- time.sleep(0.1)
- if (time.time() - start) > 5:
- error("force deleting socket %r" % cookie_socket)
- os.remove(cookie_socket)
- return
-
- echo("stopped daemon listening on %r"% cookie_socket)
-
- else:
- if os.path.exists(cookie_socket):
- os.remove(cookie_socket)
- echo("removed abandoned/broken socket %r" % cookie_socket)
-
-
-def daemonize():
- '''Daemonize the process using the Stevens' double-fork magic.'''
-
- try:
- if os.fork():
- os._exit(0)
-
- except OSError:
- print_exc()
- sys.stderr.write("fork #1 failed")
- sys.exit(1)
-
- os.chdir('/')
- os.setsid()
- os.umask(0)
-
- try:
- if os.fork():
- os._exit(0)
-
- except OSError:
- print_exc()
- sys.stderr.write("fork #2 failed")
- sys.exit(1)
-
- if sys.stdout.isatty():
- sys.stdout.flush()
- sys.stderr.flush()
-
- devnull = '/dev/null'
- stdin = file(devnull, 'r')
- stdout = file(devnull, 'a+')
- stderr = file(devnull, 'a+', 0)
-
- os.dup2(stdin.fileno(), sys.stdin.fileno())
- os.dup2(stdout.fileno(), sys.stdout.fileno())
- os.dup2(stderr.fileno(), sys.stderr.fileno())
-
-
-class CookieMonster:
- '''The uzbl cookie daemon class.'''
-
- def __init__(self):
- '''Initialise class variables.'''
-
- self.server_socket = None
- self.jar = None
- self.last_request = time.time()
- self._running = False
-
-
- def run(self):
- '''Start the daemon.'''
-
- # The check healthy function will exit if another daemon is detected
- # listening on the cookie socket and remove the abandoned socket if
- # there isnt.
- if os.path.exists(config['cookie_socket']):
- if daemon_running(config['cookie_socket']):
- sys.exit(1)
-
- # Create cookie daemon socket.
- self.create_socket()
-
- # Daemonize process.
- if config['daemon_mode']:
- echo("entering daemon mode")
- daemonize()
-
- # Register a function to cleanup on exit.
- atexit.register(self.quit)
-
- # Make SIGTERM act orderly.
- signal(SIGTERM, lambda signum, stack_frame: sys.exit(1))
-
- # Create cookie jar object from file.
- self.open_cookie_jar()
-
- # Create a way to exit nested loops by setting a running flag.
- self._running = True
-
- while self._running:
- try:
- # Enter main listen loop.
- self.listen()
-
- except KeyboardInterrupt:
- self._running = False
- print
-
- except socket.error:
- print_exc()
-
- except:
- # Clean up
- self.del_socket()
-
- # Raise exception
- raise
-
- # Always delete the socket before calling create again.
- self.del_socket()
- # Create cookie daemon socket.
- self.create_socket()
-
-
- def load_whitelist(self):
- '''Load the cookie jar whitelist policy.'''
-
- cookie_whitelist = config['cookie_whitelist']
-
- if cookie_whitelist:
- mkbasedir(cookie_whitelist)
-
- # Create cookie whitelist file if it does not exist.
- if not os.path.exists(cookie_whitelist):
- open(cookie_whitelist, 'w').close()
-
- # Read cookie whitelist file into list.
- file = open(cookie_whitelist,'r')
- domain_list = [line.rstrip('\n') for line in file]
- file.close()
-
- # Define policy of allowed domains
- policy = cookielib.DefaultCookiePolicy(allowed_domains=domain_list)
- self.jar.set_policy(policy)
-
- # Save the last modified time of the whitelist.
- self._whitelistmtime = os.stat(cookie_whitelist).st_mtime
-
-
- def open_cookie_jar(self):
- '''Open the cookie jar.'''
-
- cookie_jar = config['cookie_jar']
- cookie_whitelist = config['cookie_whitelist']
-
- if cookie_jar:
- mkbasedir(cookie_jar)
-
- # Create cookie jar object from file.
- self.jar = cookielib.MozillaCookieJar(cookie_jar)
-
- # Load cookie whitelist policy.
- if config['use_whitelist']:
- self.load_whitelist()
-
- if cookie_jar:
- try:
- # Attempt to load cookies from the cookie jar.
- self.jar.load(ignore_discard=True)
-
- # Ensure restrictive permissions are set on the cookie jar
- # to prevent other users on the system from hi-jacking your
- # authenticated sessions simply by copying your cookie jar.
- os.chmod(cookie_jar, 0600)
-
- except:
- pass
-
-
- def reload_whitelist(self):
- '''Reload the cookie whitelist.'''
-
- cookie_whitelist = config['cookie_whitelist']
- if os.path.exists(cookie_whitelist):
- echo("reloading whitelist %r" % cookie_whitelist)
- self.open_cookie_jar()
-
-
- def create_socket(self):
- '''Create AF_UNIX socket for communication with uzbl instances.'''
-
- cookie_socket = config['cookie_socket']
- mkbasedir(cookie_socket)
-
- self.server_socket = socket.socket(socket.AF_UNIX,
- socket.SOCK_SEQPACKET)
-
- self.server_socket.bind(cookie_socket)
-
- # Set restrictive permissions on the cookie socket to prevent other
- # users on the system from data-mining your cookies.
- os.chmod(cookie_socket, 0600)
-
-
- def listen(self):
- '''Listen for incoming cookie PUT and GET requests.'''
-
- daemon_timeout = config['daemon_timeout']
- echo("listening on %r" % config['cookie_socket'])
-
- connections = []
-
- while self._running:
- # This line tells the socket how many pending incoming connections
- # to enqueue at once. Raising this number may or may not increase
- # performance.
- self.server_socket.listen(1)
-
- r, w, x = select.select([self.server_socket]+connections, [], [], 1)
-
- for socket in r:
- if self.server_socket == socket:
- client_socket, _ = socket.accept()
- connections.append(client_socket)
- else:
- if not self.handle_request(socket):
- # connection was closed, forget about the client socket
- connections.remove(socket)
-
- self.last_request = time.time()
-
- if daemon_timeout:
- # Checks if the daemon has been idling for too long.
- idle = time.time() - self.last_request
- if idle > daemon_timeout:
- self._running = False
-
-
- def handle_request(self, client_socket):
- '''Connection made, now to serve a cookie PUT or GET request.'''
-
- # Receive cookie request from client.
- data = client_socket.recv(8192)
- if not data:
- return False
-
- # Cookie argument list in packet is null separated.
- argv = data.split("\0")
- action = argv[0].upper().strip()
-
- # Catch the EXIT command sent to kill running daemons.
- if action == "EXIT":
- self._running = False
- return False
-
- # Catch whitelist RELOAD command.
- elif action == "RELOAD":
- self.reload_whitelist()
- return True
-
- # Return if command unknown.
- elif action not in ['GET', 'PUT']:
- error("unknown command %r." % argv)
- return True
-
- # Determine whether or not to print cookie data to terminal.
- print_cookie = (config['verbose'] and not config['daemon_mode'])
- if print_cookie:
- print ' '.join(argv[:4])
-
- uri = urllib2.urlparse.ParseResult(
- scheme=argv[1],
- netloc=argv[2],
- path=argv[3],
- params='',
- query='',
- fragment='').geturl()
-
- req = urllib2.Request(uri)
-
- if action == "GET":
- self.jar._policy._now = self._now = int(time.time())
- cookies = self.jar._cookies_for_request(req)
- attrs = self.jar._cookie_attrs(cookies)
- if attrs:
- cookie = "; ".join(attrs)
- client_socket.send(cookie)
- if print_cookie:
- print cookie
- else:
- client_socket.send("\0")
-
- elif action == "PUT":
- cookie = argv[4] if len(argv) > 3 else None
- if print_cookie:
- print cookie
-
- self.put_cookie(req, cookie)
- client_socket.send("\0")
-
- if print_cookie:
- print
-
- return True
-
-
- def put_cookie(self, req, cookie=None):
- '''Put a cookie in the cookie jar.'''
-
- hdr = urllib2.httplib.HTTPMessage(\
- StringIO.StringIO('Set-Cookie: %s' % cookie))
- res = urllib2.addinfourl(StringIO.StringIO(), hdr,
- req.get_full_url())
- self.jar.extract_cookies(res, req)
- if config['cookie_jar']:
- self.jar.save(ignore_discard=True)
-
-
- def del_socket(self):
- '''Remove the cookie_socket file on exit. In a way the cookie_socket
- is the daemons pid file equivalent.'''
-
- if self.server_socket:
- try:
- self.server_socket.close()
-
- except:
- pass
-
- self.server_socket = None
-
- cookie_socket = config['cookie_socket']
- if os.path.exists(cookie_socket):
- echo("deleting socket %r" % cookie_socket)
- os.remove(cookie_socket)
-
-
- def quit(self):
- '''Called on exit to make sure all loose ends are tied up.'''
-
- self.del_socket()
- sys.exit(0)
-
-
-def main():
- '''Main function.'''
-
- # Define command line parameters.
- usage = "usage: %prog [options] {start|stop|restart|reload}"
- parser = OptionParser(usage=usage)
- parser.add_option('-n', '--no-daemon', dest='no_daemon',
- action='store_true', help="don't daemonise the process.")
-
- parser.add_option('-v', '--verbose', dest="verbose",
- action='store_true', help="print verbose output.")
-
- parser.add_option('-t', '--daemon-timeout', dest='daemon_timeout',
- action="store", metavar="SECONDS", help="shutdown the daemon after x "\
- "seconds inactivity. WARNING: Do not use this when launching the "\
- "cookie daemon manually.")
-
- parser.add_option('-s', '--cookie-socket', dest="cookie_socket",
- metavar="SOCKET", help="manually specify the socket location.")
-
- parser.add_option('-j', '--cookie-jar', dest='cookie_jar',
- metavar="FILE", help="manually specify the cookie jar location.")
-
- parser.add_option('-m', '--memory', dest='memory', action='store_true',
- help="store cookies in memory only - do not write to disk")
-
- parser.add_option('-u', '--use-whitelist', dest='usewhitelist',
- action='store_true', help="use cookie whitelist policy")
-
- parser.add_option('-w', '--cookie-whitelist', dest='whitelist',
- action='store', help="manually specify whitelist location",
- metavar='FILE')
-
- # Parse the command line arguments.
- (options, args) = parser.parse_args()
-
- expand = lambda p: os.path.realpath(os.path.expandvars(p))
-
- initcommands = ['start', 'stop', 'restart', 'reload']
- for arg in args:
- if arg not in initcommands:
- error("unknown argument %r" % args[0])
- sys.exit(1)
-
- if len(args) > 1:
- error("the daemon only accepts one {%s} action at a time."
- % '|'.join(initcommands))
- sys.exit(1)
-
- if len(args):
- action = args[0]
-
- else:
- action = "start"
-
- if options.no_daemon:
- config['daemon_mode'] = False
-
- if options.cookie_socket:
- config['cookie_socket'] = expand(options.cookie_socket)
-
- if options.cookie_jar:
- config['cookie_jar'] = expand(options.cookie_jar)
-
- if options.memory:
- config['cookie_jar'] = None
-
- if options.whitelist:
- config['cookie_whitelist'] = expand(options.whitelist)
-
- if options.whitelist or options.usewhitelist:
- config['use_whitelist'] = True
-
- if options.daemon_timeout:
- try:
- config['daemon_timeout'] = int(options.daemon_timeout)
-
- except ValueError:
- error("expected int argument for -t, --daemon-timeout")
-
- # Expand $VAR's in config keys that relate to paths.
- for key in ['cookie_socket', 'cookie_jar', 'cookie_whitelist']:
- if config[key]:
- config[key] = os.path.expandvars(config[key])
-
- if options.verbose:
- config['verbose'] = True
- import pprint
- sys.stderr.write("%s\n" % pprint.pformat(config))
-
- # It would be better if we didn't need to start this python process just
- # to send a command to the socket, but unfortunately socat doesn't seem
- # to support SEQPACKET.
- if action == "reload":
- send_command(config['cookie_socket'], "RELOAD")
-
- if action in ['stop', 'restart']:
- kill_daemon(config['cookie_socket'])
-
- if action in ['start', 'restart']:
- CookieMonster().run()
-
-
-if __name__ == "__main__":
- main()
diff --git a/examples/uzbl-cookie-manager.c b/examples/uzbl-cookie-manager.c
deleted file mode 100644
index 70addf3..0000000
--- a/examples/uzbl-cookie-manager.c
+++ /dev/null
@@ -1,381 +0,0 @@
-#define _POSIX_SOURCE
-
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <ctype.h>
-#include <signal.h>
-
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <sys/select.h>
-#include <sys/unistd.h>
-
-#include <sys/stat.h>
-#include <sys/file.h>
-#include <fcntl.h>
-#include <stdlib.h>
-
-#include <libsoup/soup-cookie.h>
-#include <libsoup/soup-cookie-jar-text.h>
-#include <libsoup/soup-uri.h>
-
-#include "../src/util.h"
-
-extern const XDG_Var XDG[];
-
-int verbose = 0;
-
-#define SOCK_BACKLOG 10
-#define MAX_COOKIE_LENGTH 4096
-
-char cookie_buffer[MAX_COOKIE_LENGTH];
-
-int setup_socket(const char *cookied_socket_path) {
- /* delete the cookie socket if it was left behind on a previous run */
- unlink(cookied_socket_path);
-
- int socket_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
-
- if(socket_fd < 0) {
- fprintf(stderr, "socket failed (%s)\n", strerror(errno));
- return -1;
- }
-
- struct sockaddr_un sa;
- sa.sun_family = AF_UNIX;
- strcpy(sa.sun_path, cookied_socket_path);
-
- if(bind(socket_fd, (struct sockaddr*)&sa, sizeof(sa)) < 0) {
- fprintf(stderr, "bind failed (%s)\n", strerror(errno));
- return -1;
- }
-
- if(listen(socket_fd, SOCK_BACKLOG) < 0) {
- fprintf(stderr, "listen failed (%s)\n", strerror(errno));
- return -1;
- }
-
- return socket_fd;
-}
-
-const char *whitelist_path = NULL;
-GPtrArray *whitelisted_hosts = NULL;
-time_t whitelist_update_time = 0;
-
-void whitelist_line_cb(const gchar* line, void *user_data) {
- (void) user_data;
-
- gchar *norm_host;
-
- const gchar *p = line;
- while(isspace(*p))
- p++;
-
- if(p[0] == '#' || !p[0]) /* ignore comments and blank lines */
- return;
-
- if(p[0] == '.')
- norm_host = g_strdup(p);
- else
- norm_host = g_strconcat(".", p, NULL);
-
- g_ptr_array_add(whitelisted_hosts, g_strchomp(norm_host));
-}
-
-gboolean load_whitelist(const char *whitelist_path) {
- if(!file_exists(whitelist_path))
- return FALSE;
-
- /* check if the whitelist file was updated */
- struct stat f;
- if(stat(whitelist_path, &f) < 0)
- return FALSE;
-
- if(whitelisted_hosts == NULL)
- whitelisted_hosts = g_ptr_array_new();
-
- if(f.st_mtime > whitelist_update_time) {
- /* the file was updated, reload the whitelist */
- if(verbose) puts("reloading whitelist");
- while(whitelisted_hosts->len > 0) {
- g_free(g_ptr_array_index(whitelisted_hosts, 0));
- g_ptr_array_remove_index_fast(whitelisted_hosts, 0);
- }
- for_each_line_in_file(whitelist_path, whitelist_line_cb, NULL);
- whitelist_update_time = f.st_mtime;
- }
-
- return TRUE;
-}
-
-gboolean should_save_cookie(const char *host) {
- if(!load_whitelist(whitelist_path))
- return TRUE; /* some error with the file, assume no whitelist */
-
- /* we normalize the hostname so it has a . in front like the whitelist entries */
- gchar *test_host = (host[0] == '.') ? g_strdup(host) : g_strconcat(".", host, NULL);
- int hl = strlen(test_host);
-
- /* test against each entry in the whitelist */
- gboolean result = FALSE;
- guint i;
- for(i = 0; i < whitelisted_hosts->len; i++) {
- /* a match means the host ends with (or is equal to) the whitelist entry */
- const gchar *entry = g_ptr_array_index(whitelisted_hosts, i);
- int el = strlen(entry);
- result = (el <= hl) && !strcmp(test_host + (hl - el), entry);
-
- if(result)
- break;
- }
-
- g_free(test_host);
-
- return result;
-}
-
-void handle_request(SoupCookieJar *j, const char *buff, int len, int fd) {
- const char *command = buff;
-
- const char *scheme = command + strlen(command) + 1;
- if((scheme - buff) > len) {
- fprintf(stderr, "got malformed or partial request\n");
- return;
- }
-
- const char *host = scheme + strlen(scheme) + 1;
- if((host - buff) > len) {
- fprintf(stderr, "got malformed or partial request\n");
- return;
- }
-
- const char *path = host + strlen(host) + 1;
- if((path - buff) > len) {
- fprintf(stderr, "got malformed or partial request\n");
- return;
- }
-
- /* glue the parts back together into a SoupURI */
- char *u = g_strconcat(scheme, "://", host, path, NULL);
- if(verbose) printf("%s %s\n", command, u);
- SoupURI *uri = soup_uri_new(u);
- g_free(u);
-
- if(!strcmp(command, "GET")) {
- char *result = soup_cookie_jar_get_cookies(j, uri, TRUE);
- if(result) {
- if(verbose) puts(result);
- if(write(fd, result, strlen(result)+1) < 0)
- fprintf(stderr, "write failed (%s)", strerror(errno));
-
- g_free(result);
- } else {
- if(verbose) puts("-");
- if(write(fd, "", 1) < 0)
- fprintf(stderr, "write failed (%s)", strerror(errno));
- }
- } else if(!strcmp(command, "PUT")) {
- const char *name_and_val = path + strlen(path) + 1;
- if((name_and_val - buff) > len) {
- fprintf(stderr, "got malformed or partial request\n");
- return;
- }
-
- if(verbose) puts(name_and_val);
-
- if(should_save_cookie(host)) {
- char *eql = strchr(name_and_val, '=');
- eql[0] = 0;
-
- const char *name = name_and_val;
- const char *value = eql + 1;
-
- SoupCookie *cookie = soup_cookie_new(name, value, host, path, SOUP_COOKIE_MAX_AGE_ONE_YEAR);
-
- soup_cookie_jar_add_cookie(j, cookie);
- } else if(verbose)
- puts("no, blacklisted.");
-
- if(write(fd, "", 1) < 0)
- fprintf(stderr, "write failed (%s)", strerror(errno));
- }
-
- soup_uri_free(uri);
-}
-
-void
-wait_for_things_to_happen_and_then_do_things(SoupCookieJar* j, int cookie_socket) {
- GArray *connections = g_array_new (FALSE, FALSE, sizeof (int));
-
- while(1) {
- unsigned int i;
- int r;
- fd_set fs;
-
- int maxfd = cookie_socket;
- FD_ZERO(&fs);
- FD_SET(maxfd, &fs);
-
- for(i = 0; i < connections->len; i++) {
- int fd = g_array_index(connections, int, i);
- if(fd > maxfd) maxfd = fd;
- FD_SET(fd, &fs);
- }
-
- r = select(maxfd+1, &fs, NULL, NULL, NULL);
- if(r < 0) {
- fprintf(stderr, "select failed (%s)\n", strerror(errno));
- continue;
- }
-
- if(FD_ISSET(cookie_socket, &fs)) {
- /* handle new connection */
- int fd = accept(cookie_socket, NULL, NULL);
- g_array_append_val(connections, fd);
- if(verbose) puts("got connection.");
- }
-
- for(i = 0; i < connections->len; i++) {
- /* handle activity on a connection */
- int fd = g_array_index(connections, int, i);
- if(FD_ISSET(fd, &fs)) {
- r = read(fd, cookie_buffer, MAX_COOKIE_LENGTH);
- if(r < 0) {
- fprintf(stderr, "read failed (%s)\n", strerror(errno));
- continue;
- } else if(r == 0) {
- if(verbose) puts("client hung up.");
- g_array_remove_index(connections, i);
- i--; /* other elements in the array are moved down to fill the gap */
- continue;
- }
- cookie_buffer[r] = 0;
-
- handle_request(j, cookie_buffer, r, fd);
- }
- }
- }
-}
-
-void usage(const char *progname) {
- printf("%s [-s socket-path] [-f cookies.txt] [-w whitelist-file] [-n] [-v]\n", progname);
- puts("\t-n\tdon't daemonise the process");
- puts("\t-v\tbe verbose");
-}
-
-void daemonise() {
- int r = fork();
-
- if(r < 0) {
- fprintf(stderr, "fork failed (%s)", strerror(errno));
- exit(1);
- } else if (r > 0) {
- /* this is the parent, which has done its job */
- exit(0);
- }
-
- if(setsid() < 0) {
- fprintf(stderr, "setsid failed (%s)", strerror(errno));
- exit(1);
- }
-}
-
-const char *pid_file_path = NULL;
-const char *cookied_socket_path = NULL;
-
-void cleanup_after_signal(int signal) {
- (void) signal;
- unlink(pid_file_path);
- unlink(cookied_socket_path);
- exit(0);
-}
-
-int main(int argc, char *argv[]) {
- int i;
-
- const char *cookies_txt_path = NULL;
- gboolean foreground = FALSE;
-
- for(i = 1; i < argc && argv[i][0] == '-'; i++) {
- switch(argv[i][1]) {
- case 's':
- cookied_socket_path = argv[++i];
- break;
- case 'f':
- cookies_txt_path = argv[++i];
- break;
- case 'w':
- whitelist_path = argv[++i];
- break;
- case 'n':
- foreground = TRUE;
- break;
- case 'v':
- verbose = 1;
- break;
- default:
- usage(argv[0]);
- return 1;
- }
- }
-
- if(!foreground)
- daemonise();
-
- if(!cookies_txt_path)
- cookies_txt_path = g_strconcat(get_xdg_var(XDG[1]), "/uzbl/cookies.txt", NULL);
-
- if(!cookied_socket_path)
- cookied_socket_path = g_strconcat(get_xdg_var(XDG[2]), "/uzbl/cookie_daemon_socket", NULL);
-
- if(!whitelist_path)
- whitelist_path = g_strconcat(get_xdg_var(XDG[0]), "/uzbl/cookie_whitelist", NULL);
-
- /* write out and lock the pid file.
- * this ensures that only one uzbl-cookie-manager is running per-socket.
- * (we should probably also lock the cookies.txt to prevent accidents...) */
- pid_file_path = g_strconcat(cookied_socket_path, ".pid", NULL);
- int lockfd = open(pid_file_path, O_RDWR|O_CREAT, 0600);
- if(lockfd < 0) {
- fprintf(stderr, "couldn't open pid file %s (%s)\n", pid_file_path, strerror(errno));
- return 1;
- }
-
- if(flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
- fprintf(stderr, "couldn't lock pid file %s (%s)\n", pid_file_path, strerror(errno));
- fprintf(stderr, "uzbl-cookie-manager is probably already running\n");
- return 1;
- }
-
- gchar* pids = g_strdup_printf("%d\n", getpid());
- write(lockfd, pids, strlen(pids));
- g_free(pids);
-
- struct sigaction sa;
- sa.sa_handler = cleanup_after_signal;
- if(sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL)) {
- fprintf(stderr, "sigaction failed (%s)\n", strerror(errno));
- return 1;
- }
-
- if(!foreground) {
- /* close STDIO */
- close(0);
- close(1);
- close(2);
- }
-
- g_type_init();
-
- SoupCookieJar *j = soup_cookie_jar_text_new(cookies_txt_path, FALSE);
-
- int cookie_socket = setup_socket(cookied_socket_path);
- if(cookie_socket < 0)
- return 1;
-
- wait_for_things_to_happen_and_then_do_things(j, cookie_socket);
-
- return 0;
-}
diff --git a/extras/vim/syntax/uzbl.vim b/extras/vim/syntax/uzbl.vim
index b8572c9..bf7108c 100644
--- a/extras/vim/syntax/uzbl.vim
+++ b/extras/vim/syntax/uzbl.vim
@@ -30,7 +30,7 @@ setl iskeyword=!-~,192-255
syn keyword uzblKeyword back forward scroll reload reload_ign_cache stop
syn keyword uzblKeyword zoom_in zoom_out toggle_zoom_type uri script
-syn keyword uzblKeyword toggle_status spawn sync_spawn sync_sh talk_to_socket
+syn keyword uzblKeyword toggle_status spawn sync_spawn sync_sh sync_spawn_exec
syn keyword uzblKeyword exit search search_reverse search_clear dehilight set
syn keyword uzblKeyword dump_config dump_config_as_events chain print event
syn keyword uzblKeyword request menu_add menu_link_add menu_image_add
diff --git a/src/callbacks.c b/src/callbacks.c
index 2ba4e6c..c261234 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -52,16 +52,20 @@ set_authentication_handler() {
void
set_status_background() {
- GdkColor color;
- gdk_color_parse (uzbl.behave.status_background, &color);
-
/* labels and hboxes do not draw their own background. applying this
* on the vbox/main_window is ok as the statusbar is the only affected
* widget. (if not, we could also use GtkEventBox) */
- if (uzbl.gui.main_window)
- gtk_widget_modify_bg (uzbl.gui.main_window, GTK_STATE_NORMAL, &color);
- else if (uzbl.gui.plug)
- gtk_widget_modify_bg (GTK_WIDGET(uzbl.gui.plug), GTK_STATE_NORMAL, &color);
+ GtkWidget* widget = uzbl.gui.main_window ? uzbl.gui.main_window : GTK_WIDGET (uzbl.gui.plug);
+
+#if GTK_CHECK_VERSION(2,91,0)
+ GdkRGBA color;
+ gdk_rgba_parse (&color, uzbl.behave.status_background);
+ gtk_widget_override_background_color (widget, GTK_STATE_NORMAL, &color);
+#else
+ GdkColor color;
+ gdk_color_parse (uzbl.behave.status_background, &color);
+ gtk_widget_modify_bg (widget, GTK_STATE_NORMAL, &color);
+#endif
}
@@ -648,23 +652,26 @@ navigation_decision_cb (WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNe
printf("Navigation requested -> %s\n", uri);
if (uzbl.behave.scheme_handler) {
- GString *s = g_string_new ("");
- g_string_printf(s, "'%s'", uri);
+ GString *result = g_string_new ("");
+ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+ const CommandInfo *c = parse_command_parts(uzbl.behave.scheme_handler, a);
- run_handler(uzbl.behave.scheme_handler, s->str);
+ if(c) {
+ g_array_append_val(a, uri);
+ run_parsed_command(c, a, result);
+ }
+ g_array_free(a, TRUE);
- if(uzbl.comm.sync_stdout && strcmp (uzbl.comm.sync_stdout, "") != 0) {
- char *p = strchr(uzbl.comm.sync_stdout, '\n' );
+ if(result->len > 0) {
+ char *p = strchr(result->str, '\n' );
if ( p != NULL ) *p = '\0';
- if (!strcmp(uzbl.comm.sync_stdout, "USED")) {
+ if (!strcmp(result->str, "USED")) {
webkit_web_policy_decision_ignore(policy_decision);
decision_made = TRUE;
}
}
- if (uzbl.comm.sync_stdout)
- uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout);
- g_string_free(s, TRUE);
+ g_string_free(result, TRUE);
}
if (!decision_made)
webkit_web_policy_decision_use(policy_decision);
@@ -851,27 +858,35 @@ download_cb(WebKitWebView *web_view, WebKitDownload *download, gpointer user_dat
(this may be inaccurate, there's nothing we can do about that.) */
unsigned int total_size = webkit_download_get_total_size(download);
- gchar *ev = g_strdup_printf("'%s' '%s' '%s' %d", uri, suggested_filename,
- content_type, total_size);
- run_handler(uzbl.behave.download_handler, ev);
- g_free(ev);
-
- /* no response, cancel the download */
- if(!uzbl.comm.sync_stdout) {
+ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+ const CommandInfo *c = parse_command_parts(uzbl.behave.download_handler, a);
+ if(!c) {
webkit_download_cancel(download);
+ g_array_free(a, TRUE);
return FALSE;
}
+ g_array_append_val(a, uri);
+ g_array_append_val(a, suggested_filename);
+ g_array_append_val(a, content_type);
+ gchar *total_size_s = g_strdup_printf("%d", total_size);
+ g_array_append_val(a, total_size_s);
+
+ GString *result = g_string_new ("");
+ run_parsed_command(c, a, result);
+
+ g_free(total_size_s);
+ g_array_free(a, TRUE);
+
/* no response, cancel the download */
- if(uzbl.comm.sync_stdout[0] == 0) {
+ if(result->len == 0) {
webkit_download_cancel(download);
- uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout);
return FALSE;
}
/* we got a response, it's the path we should download the file to */
- gchar *destination_path = uzbl.comm.sync_stdout;
- uzbl.comm.sync_stdout = NULL;
+ gchar *destination_path = result->str;
+ g_string_free(result, FALSE);
/* presumably people don't need newlines in their filenames. */
char *p = strchr(destination_path, '\n');
@@ -1005,13 +1020,3 @@ populate_popup_cb(WebKitWebView *v, GtkMenu *m, void *c) {
}
}
}
-
-void
-cmd_set_cookie_handler() {
- if(uzbl.behave.cookie_handler[0] == 0) {
- g_free(uzbl.behave.cookie_handler);
- uzbl.behave.cookie_handler = NULL;
- }
-
- uzbl_cookie_jar_set_handler(uzbl.net.soup_cookie_jar, uzbl.behave.cookie_handler);
-}
diff --git a/src/callbacks.h b/src/callbacks.h
index 899e959..13cb83d 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -216,6 +216,3 @@ scroll_vert_cb(GtkAdjustment *adjust, void *w);
gboolean
scroll_horiz_cb(GtkAdjustment *adjust, void *w);
-
-void
-cmd_set_cookie_handler();
diff --git a/src/cookie-jar.c b/src/cookie-jar.c
index 626e454..82a5269 100644
--- a/src/cookie-jar.c
+++ b/src/cookie-jar.c
@@ -1,51 +1,20 @@
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <poll.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-#include <string.h>
-#include <errno.h>
-
-#include <libsoup/soup-uri.h>
#include <libsoup/soup-cookie.h>
#include "cookie-jar.h"
#include "uzbl-core.h"
#include "events.h"
-static void
-uzbl_cookie_jar_session_feature_init(SoupSessionFeatureInterface *iface, gpointer user_data);
-
-G_DEFINE_TYPE_WITH_CODE (UzblCookieJar, soup_cookie_jar_socket, SOUP_TYPE_COOKIE_JAR,
- G_IMPLEMENT_INTERFACE (SOUP_TYPE_SESSION_FEATURE, uzbl_cookie_jar_session_feature_init))
+G_DEFINE_TYPE (UzblCookieJar, soup_cookie_jar_socket, SOUP_TYPE_COOKIE_JAR)
-static void request_started (SoupSessionFeature *feature, SoupSession *session, SoupMessage *msg, SoupSocket *socket);
static void changed(SoupCookieJar *jar, SoupCookie *old_cookie, SoupCookie *new_cookie);
-static void setup_handler(UzblCookieJar *jar);
-
-static void connect_cookie_socket(UzblCookieJar *jar);
-static void disconnect_cookie_socket(UzblCookieJar *jar);
-
-static gchar *do_socket_request(UzblCookieJar *jar, gchar *request, int request_len);
-
-static bool has_socket_handler(UzblCookieJar *jar) {
- return jar->socket_path != NULL;
-}
-
static void
soup_cookie_jar_socket_init(UzblCookieJar *jar) {
- jar->handler = NULL;
- jar->socket_path = NULL;
- jar->connection_fd = -1;
- jar->in_get_callback = 0;
jar->in_manual_add = 0;
}
static void
finalize(GObject *object) {
- disconnect_cookie_socket(UZBL_COOKIE_JAR(object));
G_OBJECT_CLASS(soup_cookie_jar_socket_parent_class)->finalize(object);
}
@@ -55,120 +24,30 @@ soup_cookie_jar_socket_class_init(UzblCookieJarClass *socket_class) {
SOUP_COOKIE_JAR_CLASS(socket_class)->changed = changed;
}
-/* override SoupCookieJar's request_started handler */
-static void
-uzbl_cookie_jar_session_feature_init(SoupSessionFeatureInterface *iface, gpointer user_data) {
- (void) user_data;
- iface->request_started = request_started;
-}
-
UzblCookieJar *uzbl_cookie_jar_new() {
return g_object_new(UZBL_TYPE_COOKIE_JAR, NULL);
}
-void
-uzbl_cookie_jar_set_handler(UzblCookieJar *jar, const gchar* handler) {
- jar->handler = handler;
- setup_handler(jar);
-}
-
-char *get_cookies(UzblCookieJar *jar, SoupURI *uri) {
- gchar *result, *path;
- GString *s = g_string_new ("GET");
-
- path = uri->path[0] ? uri->path : "/";
-
- if(has_socket_handler(jar)) {
- g_string_append_c(s, 0); /* null-terminate the GET */
- g_string_append_len(s, uri->scheme, strlen(uri->scheme)+1);
- g_string_append_len(s, uri->host, strlen(uri->host)+1 );
- g_string_append_len(s, path, strlen(path)+1 );
-
- result = do_socket_request(jar, s->str, s->len);
- /* try it again; older cookie daemons closed the connection after each request */
- if(result == NULL)
- result = do_socket_request(jar, s->str, s->len);
- } else {
- g_string_append_printf(s, " '%s' '%s' '%s'", uri->scheme, uri->host, uri->path);
-
- run_handler(jar->handler, s->str);
- result = g_strdup(uzbl.comm.sync_stdout);
- }
- g_string_free(s, TRUE);
- return result;
-}
-
-/* this is a duplicate of SoupCookieJar's request_started that uses our get_cookies instead */
-static void
-request_started(SoupSessionFeature *feature, SoupSession *session,
- SoupMessage *msg, SoupSocket *socket) {
- (void) session; (void) socket;
- gchar *cookies;
-
- UzblCookieJar *jar = UZBL_COOKIE_JAR (feature);
- SoupURI *uri = soup_message_get_uri(msg);
- gboolean add_to_internal_jar = false;
-
- if(jar->handler) {
- cookies = get_cookies(jar, uri);
- } else {
- /* no handler is set, fall back to the internal soup cookie jar */
- cookies = soup_cookie_jar_get_cookies(SOUP_COOKIE_JAR(jar), soup_message_get_uri (msg), TRUE);
- }
-
- if (cookies && cookies[0] != 0) {
- const gchar *next_cookie_start = cookies;
-
- if (add_to_internal_jar) {
- /* add the cookie data that we just obtained from the cookie handler
- to the cookie jar so that javascript has access to them.
- we set this flag so that we don't trigger the PUT handler. */
- jar->in_get_callback = true;
- do {
- SoupCookie *soup_cookie = soup_cookie_parse(next_cookie_start, uri);
- if(soup_cookie)
- soup_cookie_jar_add_cookie(SOUP_COOKIE_JAR(uzbl.net.soup_cookie_jar), soup_cookie);
- next_cookie_start = strchr(next_cookie_start, ';');
- } while(next_cookie_start++ != NULL);
- jar->in_get_callback = false;
- }
-
- soup_message_headers_replace (msg->request_headers, "Cookie", cookies);
- } else {
- soup_message_headers_remove (msg->request_headers, "Cookie");
- }
-
- if(cookies)
- g_free (cookies);
-}
-
static void
changed(SoupCookieJar *jar, SoupCookie *old_cookie, SoupCookie *new_cookie) {
- SoupCookie * cookie = new_cookie ? new_cookie : old_cookie;
+ SoupCookie *cookie = new_cookie ? new_cookie : old_cookie;
UzblCookieJar *uzbl_jar = UZBL_COOKIE_JAR(jar);
- /* when Uzbl begins an HTTP request, it GETs cookies from the handler
- and then adds them to the cookie jar so that javascript can access
- these cookies. this causes a 'changed' callback, which we don't want
- to do anything, so we just return.
-
- (if SoupCookieJar let us override soup_cookie_jar_get_cookies we
- wouldn't have to do this.) */
- if(uzbl_jar->in_get_callback)
- return;
-
- gchar *scheme = cookie->secure ? "https" : "http";
-
- /* send a ADD or DELETE -_COOKIE event depending on what have changed */
+ /* send a ADD or DELETE -_COOKIE event depending on what has changed. these
+ * events aren't sent when a cookie changes due to an add/delete_cookie
+ * command because otherwise a loop would occur when a cookie change is
+ * propagated to other uzbl instances using add/delete_cookie. */
if(!uzbl_jar->in_manual_add) {
+ gchar *scheme = cookie->secure ? "https" : "http";
+
gchar *expires = NULL;
if(cookie->expires)
expires = g_strdup_printf ("%d", soup_date_to_time_t (cookie->expires));
gchar * eventstr = g_strdup_printf ("'%s' '%s' '%s' '%s' '%s' '%s'",
cookie->domain, cookie->path, cookie->name, cookie->value, scheme, expires?expires:"");
- if(new_cookie)
+ if(new_cookie)
send_event(ADD_COOKIE, eventstr, NULL);
else
send_event(DELETE_COOKIE, eventstr, NULL);
@@ -176,151 +55,4 @@ changed(SoupCookieJar *jar, SoupCookie *old_cookie, SoupCookie *new_cookie) {
if(expires)
g_free(expires);
}
-
- /* the cookie daemon is only interested in new cookies and changed
- ones, it can take care of deleting expired cookies on its own. */
- if(!new_cookie)
- return;
-
- GString *s = g_string_new ("PUT");
-
- if(has_socket_handler(uzbl_jar)) {
- g_string_append_c(s, 0); /* null-terminate the PUT */
- g_string_append_len(s, scheme, strlen(scheme)+1);
- g_string_append_len(s, new_cookie->domain, strlen(new_cookie->domain)+1 );
- g_string_append_len(s, new_cookie->path, strlen(new_cookie->path)+1 );
- g_string_append_printf(s, "%s=%s", new_cookie->name, new_cookie->value);
-
- gchar *result = do_socket_request(uzbl_jar, s->str, s->len+1);
- /* try it again; older cookie daemons closed the connection after each request */
- if(!result)
- result = do_socket_request(uzbl_jar, s->str, s->len+1);
-
- g_free(result);
- } else {
- g_string_append_printf(s, " '%s' '%s' '%s' '%s=%s'", scheme, new_cookie->domain, new_cookie->path, new_cookie->name, new_cookie->value);
-
- run_handler(uzbl_jar->handler, s->str);
- }
-
- g_string_free(s, TRUE);
-}
-
-static void
-setup_handler(UzblCookieJar *jar) {
- if(jar->handler && strncmp(jar->handler, "talk_to_socket", strlen("talk_to_socket")) == 0) {
- /* extract the socket path from the handler. */
- jar->socket_path = jar->handler + strlen("talk_to_socket");
- while(isspace(*jar->socket_path))
- jar->socket_path++;
- if(*jar->socket_path == 0)
- return; /* there was no path specified. */
- disconnect_cookie_socket(jar);
- connect_cookie_socket(jar);
- } else {
- jar->socket_path = NULL;
- }
-}
-
-static void
-connect_cookie_socket(UzblCookieJar *jar) {
- struct sockaddr_un sa;
- int fd;
-
- g_strlcpy(sa.sun_path, jar->socket_path, sizeof(sa.sun_path));
- sa.sun_family = AF_UNIX;
-
- /* create socket file descriptor and connect it to path */
- fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
- if(fd == -1) {
- g_printerr("connect_cookie_socket: creating socket failed (%s)\n", strerror(errno));
- return;
- }
-
- if(connect(fd, (struct sockaddr*)&sa, sizeof(sa))) {
- g_printerr("connect_cookie_socket: connect failed (%s)\n", strerror(errno));
- close(fd);
- return;
- }
-
- /* successful connection! */
- jar->connection_fd = fd;
-}
-
-static void
-disconnect_cookie_socket(UzblCookieJar *jar) {
- if(jar->connection_fd > 0) {
- close(jar->connection_fd);
- jar->connection_fd = -1;
- }
-}
-
-static gchar *do_socket_request(UzblCookieJar *jar, gchar *request, int request_length) {
- int len;
- ssize_t ret;
- struct pollfd pfd;
- gchar *result = NULL;
-
- if(jar->connection_fd < 0)
- connect_cookie_socket(jar); /* connection was lost, reconnect */
-
- /* write request */
- ret = write(jar->connection_fd, request, request_length);
- if(ret == -1) {
- g_printerr("talk_to_socket: write failed (%s)\n", strerror(errno));
- disconnect_cookie_socket(jar);
- return NULL;
- }
-
- /* wait for a response, with a 500ms timeout */
- pfd.fd = jar->connection_fd;
- pfd.events = POLLIN;
- while(1) {
- ret = poll(&pfd, 1, 500);
- if(ret == 1) break;
- if(ret == 0) errno = ETIMEDOUT;
- if(errno == EINTR) continue;
- g_printerr("talk_to_socket: poll failed while waiting for input (%s)\n",
- strerror(errno));
- if(errno != ETIMEDOUT)
- disconnect_cookie_socket(jar);
- return NULL;
- }
-
- /* get length of response */
- if(ioctl(jar->connection_fd, FIONREAD, &len) == -1) {
- g_printerr("talk_to_socket: cannot find daemon response length, "
- "ioctl failed (%s)\n", strerror(errno));
- disconnect_cookie_socket(jar);
- return NULL;
- }
-
- /* there was an empty response. */
- if(len == 0)
- return g_strdup("");
-
- /* there is a response, read it */
- result = g_malloc(len + 1);
- if(!result) {
- g_printerr("talk_to_socket: failed to allocate %d bytes\n", len);
- return NULL;
- }
- result[len] = 0; /* ensure result is null terminated */
-
- gchar *p = result;
- while(len > 0) {
- ret = read(jar->connection_fd, p, len);
- if(ret == -1) {
- g_printerr("talk_to_socket: failed to read from socket (%s)\n",
- strerror(errno));
- disconnect_cookie_socket(jar);
- g_free(result);
- return NULL;
- } else {
- len -= ret;
- p += ret;
- }
- }
-
- return result;
}
diff --git a/src/cookie-jar.h b/src/cookie-jar.h
index f3e3733..05f4a6f 100644
--- a/src/cookie-jar.h
+++ b/src/cookie-jar.h
@@ -10,12 +10,6 @@
typedef struct {
SoupCookieJar parent;
- const gchar *handler;
-
- const gchar *socket_path;
- int connection_fd;
-
- gboolean in_get_callback;
gboolean in_manual_add;
} UzblCookieJar;
@@ -25,10 +19,4 @@ typedef struct {
UzblCookieJar *uzbl_cookie_jar_new();
-void
-uzbl_cookie_jar_set_handler(UzblCookieJar *jar, const gchar *handler);
-
-char
-*get_cookies(UzblCookieJar *jar, SoupURI *uri);
-
#endif
diff --git a/src/io.c b/src/io.c
index 1c463db..1e85237 100644
--- a/src/io.c
+++ b/src/io.c
@@ -129,27 +129,20 @@ control_stdin(GIOChannel *gio, GIOCondition condition) {
return TRUE;
}
-
void
-create_stdin () {
- GIOChannel *chan = NULL;
- GError *error = NULL;
-
- chan = g_io_channel_unix_new(fileno(stdin));
+create_stdin() {
+ GIOChannel *chan = g_io_channel_unix_new(fileno(stdin));
if (chan) {
if (!g_io_add_watch(chan, G_IO_IN|G_IO_HUP, (GIOFunc) control_stdin, NULL)) {
- g_error ("Stdin: could not add watch\n");
- } else {
- if (uzbl.state.verbose)
- printf ("Stdin: watch added successfully\n");
+ g_error ("create_stdin: could not add watch\n");
+ } else if (uzbl.state.verbose) {
+ printf ("create_stdin: watch added successfully\n");
}
} else {
- g_error ("Stdin: Error while opening: %s\n", error->message);
+ g_error ("create_stdin: error while opening stdin\n");
}
- if (error) g_error_free (error);
}
-
gboolean
remove_socket_from_array(GIOChannel *chan) {
gboolean ret = 0;
@@ -163,7 +156,6 @@ remove_socket_from_array(GIOChannel *chan) {
return ret;
}
-
gboolean
control_socket(GIOChannel *chan) {
struct sockaddr_un remote;
diff --git a/src/util.c b/src/util.c
index 3cf6416..f56eb57 100644
--- a/src/util.c
+++ b/src/util.c
@@ -172,7 +172,7 @@ find_existing_file(gchar* path_list) {
i++;
}
-g_free(executable);
+ g_free(executable);
g_strfreev(split);
return NULL;
}
@@ -187,12 +187,6 @@ itos(int val) {
}
gchar*
-strfree(gchar *str) {
- g_free(str);
- return NULL;
-}
-
-gchar*
argv_idx(const GArray *a, const guint idx) {
return g_array_index(a, gchar*, idx);
}
diff --git a/src/util.h b/src/util.h
index a0efe85..388c202 100644
--- a/src/util.h
+++ b/src/util.h
@@ -17,5 +17,4 @@ gboolean for_each_line_in_file(const gchar *path, void (*callback)(const gcha
enum exp_type get_exp_type(const gchar*);
gchar* find_existing_file(gchar*);
char* itos(int val);
-gchar* strfree(gchar *str);
gchar* argv_idx(const GArray*, const guint);
diff --git a/src/uzbl-browser b/src/uzbl-browser
index faa2829..81645ca 100755
--- a/src/uzbl-browser
+++ b/src/uzbl-browser
@@ -64,13 +64,6 @@ then
export UZBL_UTIL_DIR
fi
-# uzbl-cookie-manager will exit if another instance is already running.
-# we could also check if its pid file exists to avoid having to spawn it.
-#if [ ! -f "$XDG_CACHE_HOME"/uzbl/cookie_daemon_socket.pid ]
-#then
-# ${UZBL_COOKIE_DAEMON:-uzbl-cookie-manager}
-#fi
-
# uzbl-event-manager will exit if one is already running.
# we could also check if its pid file exists to avoid having to spawn it.
DAEMON_SOCKET="$XDG_CACHE_HOME"/uzbl/event_daemon
diff --git a/src/uzbl-core.c b/src/uzbl-core.c
index 4ee0129..a3a03b0 100644
--- a/src/uzbl-core.c
+++ b/src/uzbl-core.c
@@ -92,7 +92,6 @@ const struct var_name_to_ptr_t {
{ "title_format_short", PTR_V_STR(uzbl.behave.title_format_short, 1, NULL)},
{ "icon", PTR_V_STR(uzbl.gui.icon, 1, set_icon)},
{ "forward_keys", PTR_V_INT(uzbl.behave.forward_keys, 1, NULL)},
- { "cookie_handler", PTR_V_STR(uzbl.behave.cookie_handler, 1, cmd_set_cookie_handler)},
{ "authentication_handler", PTR_V_STR(uzbl.behave.authentication_handler, 1, set_authentication_handler)},
{ "scheme_handler", PTR_V_STR(uzbl.behave.scheme_handler, 1, NULL)},
{ "download_handler", PTR_V_STR(uzbl.behave.download_handler, 1, NULL)},
@@ -146,6 +145,7 @@ const struct var_name_to_ptr_t {
{ "SELECTED_URI", PTR_C_STR(uzbl.state.selected_url, NULL)},
{ "NAME", PTR_C_STR(uzbl.state.instance_name, NULL)},
{ "PID", PTR_C_STR(uzbl.info.pid_str, NULL)},
+ { "_", PTR_C_STR(uzbl.state.last_result, NULL)},
{ NULL, {.ptr.s = NULL, .type = TYPE_INT, .dump = 0, .writeable = 0, .func = NULL}}
};
@@ -334,7 +334,6 @@ expand(const char* s, guint recurse) {
return g_string_free(buf, FALSE);
}
-
void
clean_up(void) {
if (uzbl.info.pid_str) {
@@ -489,55 +488,55 @@ VIEWFUNC(go_forward)
#undef VIEWFUNC
/* -- command to callback/function map for things we cannot attach to any signals */
-struct {const char *key; CommandInfo value;} cmdlist[] =
+CommandInfo cmdlist[] =
{ /* key function no_split */
- { "back", {view_go_back, 0} },
- { "forward", {view_go_forward, 0} },
- { "scroll", {scroll_cmd, 0} },
- { "reload", {view_reload, 0}, },
- { "reload_ign_cache", {view_reload_bypass_cache, 0} },
- { "stop", {view_stop_loading, 0}, },
- { "zoom_in", {view_zoom_in, 0}, }, //Can crash (when max zoom reached?).
- { "zoom_out", {view_zoom_out, 0}, },
- { "toggle_zoom_type", {toggle_zoom_type, 0}, },
- { "uri", {load_uri, TRUE} },
- { "js", {run_js, TRUE} },
- { "script", {run_external_js, 0} },
- { "toggle_status", {toggle_status_cb, 0} },
- { "spawn", {spawn_async, 0} },
- { "sync_spawn", {spawn_sync, 0} }, // needed for cookie handler
- { "sync_spawn_exec", {spawn_sync_exec, 0} }, // needed for load_cookies.sh :(
- { "sh", {spawn_sh_async, 0} },
- { "sync_sh", {spawn_sh_sync, 0} }, // needed for cookie handler
- { "exit", {close_uzbl, 0} },
- { "search", {search_forward_text, TRUE} },
- { "search_reverse", {search_reverse_text, TRUE} },
- { "search_clear", {search_clear, TRUE} },
- { "dehilight", {dehilight, 0} },
- { "set", {set_var, TRUE} },
- { "dump_config", {act_dump_config, 0} },
- { "dump_config_as_events", {act_dump_config_as_events, 0} },
- { "chain", {chain, 0} },
- { "print", {print, TRUE} },
- { "event", {event, TRUE} },
- { "request", {event, TRUE} },
- { "menu_add", {menu_add, TRUE} },
- { "menu_link_add", {menu_add_link, TRUE} },
- { "menu_image_add", {menu_add_image, TRUE} },
- { "menu_editable_add", {menu_add_edit, TRUE} },
- { "menu_separator", {menu_add_separator, TRUE} },
- { "menu_link_separator", {menu_add_separator_link, TRUE} },
- { "menu_image_separator", {menu_add_separator_image, TRUE}},
- { "menu_editable_separator", {menu_add_separator_edit, TRUE} },
- { "menu_remove", {menu_remove, TRUE} },
- { "menu_link_remove", {menu_remove_link, TRUE} },
- { "menu_image_remove", {menu_remove_image, TRUE} },
- { "menu_editable_remove", {menu_remove_edit, TRUE} },
- { "hardcopy", {hardcopy, TRUE} },
- { "include", {include, TRUE} },
- { "show_inspector", {show_inspector, 0} },
- { "add_cookie", {add_cookie, 0} },
- { "delete_cookie", {delete_cookie, 0} }
+ { "back", view_go_back, 0 },
+ { "forward", view_go_forward, 0 },
+ { "scroll", scroll_cmd, 0 },
+ { "reload", view_reload, 0 },
+ { "reload_ign_cache", view_reload_bypass_cache, 0 },
+ { "stop", view_stop_loading, 0 },
+ { "zoom_in", view_zoom_in, 0 }, //Can crash (when max zoom reached?).
+ { "zoom_out", view_zoom_out, 0 },
+ { "toggle_zoom_type", toggle_zoom_type, 0 },
+ { "uri", load_uri, TRUE },
+ { "js", run_js, TRUE },
+ { "script", run_external_js, 0 },
+ { "toggle_status", toggle_status_cb, 0 },
+ { "spawn", spawn_async, 0 },
+ { "sync_spawn", spawn_sync, 0 },
+ { "sync_spawn_exec", spawn_sync_exec, 0 }, // needed for load_cookies.sh :(
+ { "sh", spawn_sh_async, 0 },
+ { "sync_sh", spawn_sh_sync, 0 },
+ { "exit", close_uzbl, 0 },
+ { "search", search_forward_text, TRUE },
+ { "search_reverse", search_reverse_text, TRUE },
+ { "search_clear", search_clear, TRUE },
+ { "dehilight", dehilight, 0 },
+ { "set", set_var, TRUE },
+ { "dump_config", act_dump_config, 0 },
+ { "dump_config_as_events", act_dump_config_as_events, 0 },
+ { "chain", chain, 0 },
+ { "print", print, TRUE },
+ { "event", event, TRUE },
+ { "request", event, TRUE },
+ { "menu_add", menu_add, TRUE },
+ { "menu_link_add", menu_add_link, TRUE },
+ { "menu_image_add", menu_add_image, TRUE },
+ { "menu_editable_add", menu_add_edit, TRUE },
+ { "menu_separator", menu_add_separator, TRUE },
+ { "menu_link_separator", menu_add_separator_link, TRUE },
+ { "menu_image_separator", menu_add_separator_image, TRUE },
+ { "menu_editable_separator", menu_add_separator_edit, TRUE },
+ { "menu_remove", menu_remove, TRUE },
+ { "menu_link_remove", menu_remove_link, TRUE },
+ { "menu_image_remove", menu_remove_image, TRUE },
+ { "menu_editable_remove", menu_remove_edit, TRUE },
+ { "hardcopy", hardcopy, TRUE },
+ { "include", include, TRUE },
+ { "show_inspector", show_inspector, 0 },
+ { "add_cookie", add_cookie, 0 },
+ { "delete_cookie", delete_cookie, 0 }
};
void
@@ -546,7 +545,7 @@ commands_hash(void) {
uzbl.behave.commands = g_hash_table_new(g_str_hash, g_str_equal);
for (i = 0; i < LENGTH(cmdlist); i++)
- g_hash_table_insert(uzbl.behave.commands, (gpointer) cmdlist[i].key, &cmdlist[i].value);
+ g_hash_table_insert(uzbl.behave.commands, (gpointer) cmdlist[i].key, &cmdlist[i]);
}
@@ -864,7 +863,8 @@ search_clear(WebKitWebView *page, GArray *argv, GString *result) {
(void) result;
webkit_web_view_unmark_text_matches (page);
- uzbl.state.searchtx = strfree (uzbl.state.searchtx);
+ g_free(uzbl.state.searchtx);
+ uzbl.state.searchtx = NULL;
}
void
@@ -887,16 +887,21 @@ dehilight(WebKitWebView *page, GArray *argv, GString *result) {
void
chain(WebKitWebView *page, GArray *argv, GString *result) {
- (void) page; (void) result;
- gchar *a = NULL;
- gchar **parts = NULL;
+ (void) page;
guint i = 0;
- while ((a = argv_idx(argv, i++))) {
- parts = g_strsplit (a, " ", 2);
- if (parts[0])
- parse_command(parts[0], parts[1], result);
- g_strfreev (parts);
+ const gchar *cmd;
+ GString *r = g_string_new ("");
+ while ((cmd = argv_idx(argv, i++))) {
+ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+ const CommandInfo *c = parse_command_parts(cmd, a);
+ if (c)
+ run_parsed_command(c, a, r);
+ g_array_free (a, TRUE);
}
+ if(result)
+ g_string_assign (result, r->str);
+
+ g_string_free(r, TRUE);
}
void
@@ -930,7 +935,8 @@ run_command (const gchar *command, const gchar **args, const gboolean sync,
gboolean result;
if (sync) {
- if (*output_stdout) *output_stdout = strfree(*output_stdout);
+ if (*output_stdout)
+ g_free(*output_stdout);
result = g_spawn_sync(NULL, (gchar **)a->data, NULL, G_SPAWN_SEARCH_PATH,
NULL, NULL, output_stdout, NULL, NULL, &err);
@@ -998,25 +1004,28 @@ split_quoted(const gchar* src, const gboolean unquote) {
}
void
-spawn(GArray *argv, gboolean sync, gboolean exec) {
+spawn(GArray *argv, GString *result, gboolean exec) {
gchar *path = NULL;
gchar *arg_car = argv_idx(argv, 0);
const gchar **arg_cdr = &g_array_index(argv, const gchar *, 1);
if (arg_car && (path = find_existing_file(arg_car))) {
- if (uzbl.comm.sync_stdout)
- uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout);
- run_command(path, arg_cdr, sync, sync?&uzbl.comm.sync_stdout:NULL);
- // run each line of output from the program as a command
- if (sync && exec && uzbl.comm.sync_stdout) {
- gchar *head = uzbl.comm.sync_stdout;
- gchar *tail;
- while ((tail = strchr (head, '\n'))) {
- *tail = '\0';
- parse_cmd_line(head, NULL);
- head = tail + 1;
+ gchar *r = NULL;
+ run_command(path, arg_cdr, result != NULL, result ? &r : NULL);
+ if(result) {
+ g_string_assign(result, r);
+ // run each line of output from the program as a command
+ if (exec && r) {
+ gchar *head = r;
+ gchar *tail;
+ while ((tail = strchr (head, '\n'))) {
+ *tail = '\0';
+ parse_cmd_line(head, NULL);
+ head = tail + 1;
+ }
}
}
+ g_free(r);
g_free(path);
}
}
@@ -1024,23 +1033,28 @@ spawn(GArray *argv, gboolean sync, gboolean exec) {
void
spawn_async(WebKitWebView *web_view, GArray *argv, GString *result) {
(void)web_view; (void)result;
- spawn(argv, FALSE, FALSE);
+ spawn(argv, NULL, FALSE);
}
void
spawn_sync(WebKitWebView *web_view, GArray *argv, GString *result) {
- (void)web_view; (void)result;
- spawn(argv, TRUE, FALSE);
+ (void)web_view;
+ spawn(argv, result, FALSE);
}
void
spawn_sync_exec(WebKitWebView *web_view, GArray *argv, GString *result) {
- (void)web_view; (void)result;
- spawn(argv, TRUE, TRUE);
+ (void)web_view;
+ if(!result) {
+ GString *force_result = g_string_new("");
+ spawn(argv, force_result, TRUE);
+ g_string_free (force_result, TRUE);
+ } else
+ spawn(argv, result, TRUE);
}
void
-spawn_sh(GArray *argv, gboolean sync) {
+spawn_sh(GArray *argv, GString *result) {
if (!uzbl.behave.shell_cmd) {
g_printerr ("spawn_sh: shell_cmd is not set!\n");
return;
@@ -1048,19 +1062,23 @@ spawn_sh(GArray *argv, gboolean sync) {
guint i;
gchar **cmd = split_quoted(uzbl.behave.shell_cmd, TRUE);
+ if(!cmd)
+ return;
+
gchar *cmdname = g_strdup(cmd[0]);
g_array_insert_val(argv, 1, cmdname);
for (i = 1; i < g_strv_length(cmd); i++)
g_array_prepend_val(argv, cmd[i]);
- if (cmd) {
- if (uzbl.comm.sync_stdout)
- uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout);
+ if (result) {
+ gchar *r = NULL;
+ run_command(cmd[0], (const gchar **) argv->data, TRUE, &r);
+ g_string_assign(result, r);
+ g_free(r);
+ } else
+ run_command(cmd[0], (const gchar **) argv->data, FALSE, NULL);
- run_command(cmd[0], (const gchar **) argv->data,
- sync, sync?&uzbl.comm.sync_stdout:NULL);
- }
g_free (cmdname);
g_strfreev (cmd);
}
@@ -1068,58 +1086,101 @@ spawn_sh(GArray *argv, gboolean sync) {
void
spawn_sh_async(WebKitWebView *web_view, GArray *argv, GString *result) {
(void)web_view; (void)result;
- spawn_sh(argv, FALSE);
+ spawn_sh(argv, NULL);
}
void
spawn_sh_sync(WebKitWebView *web_view, GArray *argv, GString *result) {
(void)web_view; (void)result;
- spawn_sh(argv, TRUE);
+ spawn_sh(argv, result);
}
void
-parse_command(const char *cmd, const char *param, GString *result) {
- CommandInfo *c;
- GString *tmp = g_string_new("");
+run_parsed_command(const CommandInfo *c, GArray *a, GString *result) {
+ c->function(uzbl.gui.web_view, a, result);
+
+ /* send the COMMAND_EXECUTED event, except for set and event/request commands */
+ if(strcmp("set", c->key) &&
+ strcmp("event", c->key) &&
+ strcmp("request", c->key)) {
+ GString *tmp = g_string_new(c->key);
+ const gchar *p;
+ guint i = 0;
+ while ((p = argv_idx(a, i++)))
+ g_string_append_printf(tmp, " '%s'", p);
+ send_event(COMMAND_EXECUTED, tmp->str, NULL);
+ g_string_free(tmp, TRUE);
+ }
+
+ if(result) {
+ g_free(uzbl.state.last_result);
+ uzbl.state.last_result = g_strdup(result->str);
+ }
+}
- if ((c = g_hash_table_lookup(uzbl.behave.commands, cmd))) {
+void
+parse_command_arguments(const gchar *p, GArray *a, gboolean no_split) {
+ if (no_split && p) { /* pass the parameters through in one chunk */
+ sharg_append(a, g_strdup(p));
+ return;
+ }
+
+ gchar **par = split_quoted(p, TRUE);
+ if (par) {
guint i;
- gchar **par = split_quoted(param, TRUE);
- GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+ for (i = 0; i < g_strv_length(par); i++)
+ sharg_append(a, g_strdup(par[i]));
+ g_strfreev (par);
+ }
+}
- if (c->no_split) { /* don't split */
- sharg_append(a, param);
- } else if (par) {
- for (i = 0; i < g_strv_length(par); i++)
- sharg_append(a, par[i]);
- }
+const CommandInfo *
+parse_command_parts(const gchar *line, GArray *a) {
+ CommandInfo *c = NULL;
- if (result == NULL) {
- GString *result_print = g_string_new("");
+ gchar *exp_line = expand(line, 0);
+ if(exp_line[0] == '\0')
+ return NULL;
- c->function(uzbl.gui.web_view, a, result_print);
- if (result_print->len)
- printf("%*s\n", (int)result_print->len, result_print->str);
+ /* separate the line into the command and its parameters */
+ gchar **tokens = g_strsplit(exp_line, " ", 2);
- g_string_free(result_print, TRUE);
- } else {
- c->function(uzbl.gui.web_view, a, result);
- }
- g_strfreev (par);
- g_array_free (a, TRUE);
+ /* look up the command */
+ c = g_hash_table_lookup(uzbl.behave.commands, tokens[0]);
- if(strcmp("set", cmd) &&
- strcmp("event", cmd) &&
- strcmp("request", cmd)) {
- g_string_printf(tmp, "%s %s", cmd, param?param:"");
- send_event(COMMAND_EXECUTED, tmp->str, NULL);
- }
+ if(!c) {
+ send_event(COMMAND_ERROR, exp_line, NULL);
+ g_free(exp_line);
+ g_strfreev(tokens);
+ return NULL;
}
- else {
- g_string_printf (tmp, "%s %s", cmd, param?param:"");
- send_event(COMMAND_ERROR, tmp->str, NULL);
+
+ gchar *p = g_strdup(tokens[1]);
+ g_free(exp_line);
+ g_strfreev(tokens);
+
+ /* parse the arguments */
+ parse_command_arguments(p, a, c->no_split);
+ g_free(p);
+
+ return c;
+}
+
+void
+parse_command(const char *cmd, const char *params, GString *result) {
+ CommandInfo *c = g_hash_table_lookup(uzbl.behave.commands, cmd);
+ if(c) {
+ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+
+ parse_command_arguments(params, a, c->no_split);
+ run_parsed_command(c, a, result);
+
+ g_array_free (a, TRUE);
+ } else {
+ gchar *tmp = g_strconcat(cmd, " ", params, NULL);
+ send_event(COMMAND_ERROR, tmp, NULL);
+ g_free(tmp);
}
- g_string_free(tmp, TRUE);
}
@@ -1212,44 +1273,24 @@ set_var_value(const gchar *name, gchar *val) {
}
void
-parse_cmd_line(const char* ctl_line, GString* result) {
- size_t len = 0;
- gchar* ctlstrip = NULL;
- gchar* exp_line = NULL;
- gchar* work_string = NULL;
-
- work_string = g_strdup(ctl_line);
-
- /* Strip trailing newline */
- len = strlen(ctl_line);
- if (work_string[len - 1] == '\n')
- ctlstrip = g_strndup(work_string, len - 1);
- else
- ctlstrip = g_strdup(work_string);
- g_free(work_string);
-
- if( strcmp(g_strchug(ctlstrip), "") &&
- strcmp(exp_line = expand(ctlstrip, 0), "")
- ) {
- /* ignore comments */
- if((exp_line[0] == '#'))
- ;
-
- /* parse a command */
- else {
- gchar **tokens = NULL;
-
- tokens = g_strsplit(exp_line, " ", 2);
- parse_command(tokens[0], tokens[1], result);
- g_strfreev(tokens);
- }
- g_free(exp_line);
+parse_cmd_line(const char *ctl_line, GString *result) {
+ gchar *work_string = g_strdup(ctl_line);
+
+ /* strip trailing newline, and any other whitespace in front */
+ g_strstrip(work_string);
+
+ if( strcmp(work_string, "") ) {
+ if((work_string[0] != '#')) { /* ignore comments */
+ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+ const CommandInfo *c = parse_command_parts(work_string, a);
+ run_parsed_command(c, a, result);
+ g_array_free (a, TRUE);
+ }
}
- g_free(ctlstrip);
+ g_free(work_string);
}
-
void
update_title(void) {
Behaviour *b = &uzbl.behave;
@@ -1397,126 +1438,6 @@ create_plug() {
return plug;
}
-
-gchar**
-inject_handler_args(const gchar *actname, const gchar *origargs, const gchar *newargs) {
- /*
- If actname is one that calls an external command, this function will inject
- newargs in front of the user-provided args in that command line. They will
- come become after the body of the script (in sh) or after the name of
- the command to execute (in spawn).
- i.e. sh <body> <userargs> becomes sh <body> <ARGS> <userargs> and
- spawn <command> <userargs> becomes spawn <command> <ARGS> <userargs>.
-
- The return value consist of two strings: the action (sh, ...) and its args.
-
- If act is not one that calls an external command, then the given action merely
- gets duplicated.
- */
- GArray *rets = g_array_new(TRUE, FALSE, sizeof(gchar*));
- /* Arrr! Here be memory leaks */
- gchar *actdup = g_strdup(actname);
- g_array_append_val(rets, actdup);
-
- if ((g_strcmp0(actname, "spawn") == 0) ||
- (g_strcmp0(actname, "sh") == 0) ||
- (g_strcmp0(actname, "sync_spawn") == 0) ||
- (g_strcmp0(actname, "sync_sh") == 0)) {
- guint i;
- GString *a = g_string_new("");
- gchar **spawnparts = split_quoted(origargs, FALSE);
- g_string_append_printf(a, "%s", spawnparts[0]); /* sh body or script name */
- if (newargs) g_string_append_printf(a, " %s", newargs); /* handler args */
-
- for (i = 1; i < g_strv_length(spawnparts); i++) /* user args */
- if (spawnparts[i]) g_string_append_printf(a, " %s", spawnparts[i]);
-
- g_array_append_val(rets, a->str);
- g_string_free(a, FALSE);
- g_strfreev(spawnparts);
- } else {
- gchar *origdup = g_strdup(origargs);
- g_array_append_val(rets, origdup);
- }
- return (gchar**)g_array_free(rets, FALSE);
-}
-
-void
-run_handler (const gchar *act, const gchar *args) {
- /* Consider this code a temporary hack to make the handlers usable.
- In practice, all this splicing, injection, and reconstruction is
- inefficient, annoying and hard to manage. Potential pitfalls arise
- when the handler specific args 1) are not quoted (the handler
- callbacks should take care of this) 2) are quoted but interfere
- with the users' own quotation. A more ideal solution is
- to refactor parse_command so that it doesn't just take a string
- and execute it; rather than that, we should have a function which
- returns the argument vector parsed from the string. This vector
- could be modified (e.g. insert additional args into it) before
- passing it to the next function that actually executes it. Though
- it still isn't perfect for chain actions.. will reconsider & re-
- factor when I have the time. -duc */
-
- if (!act) return;
- char **parts = g_strsplit(act, " ", 2);
- if (!parts || !parts[0]) return;
- if (g_strcmp0(parts[0], "chain") == 0) {
- GString *newargs = g_string_new("");
- gchar **chainparts = split_quoted(parts[1], FALSE);
-
- /* for every argument in the chain, inject the handler args
- and make sure the new parts are wrapped in quotes */
- gchar **cp = chainparts;
- gchar quot = '\'';
- gchar *quotless = NULL;
- gchar **spliced_quotless = NULL; // sigh -_-;
- gchar **inpart = NULL;
-
- while (*cp) {
- if ((**cp == '\'') || (**cp == '\"')) { /* strip old quotes */
- quot = **cp;
- quotless = g_strndup(&(*cp)[1], strlen(*cp) - 2);
- } else quotless = g_strdup(*cp);
-
- spliced_quotless = g_strsplit(quotless, " ", 2);
- inpart = inject_handler_args(spliced_quotless[0], spliced_quotless[1], args);
- g_strfreev(spliced_quotless);
-
- g_string_append_printf(newargs, " %c%s %s%c", quot, inpart[0], inpart[1], quot);
- g_free(quotless);
- g_strfreev(inpart);
- cp++;
- }
-
- parse_command(parts[0], &(newargs->str[1]), NULL);
- g_string_free(newargs, TRUE);
- g_strfreev(chainparts);
-
- } else {
- gchar **inparts;
- gchar *inparts_[2];
- if (parts[1]) {
- /* expand the user-specified arguments */
- gchar* expanded = expand(parts[1], 0);
- inparts = inject_handler_args(parts[0], expanded, args);
- g_free(expanded);
- } else {
- inparts_[0] = parts[0];
- inparts_[1] = g_strdup(args);
- inparts = inparts_;
- }
-
- parse_command(inparts[0], inparts[1], NULL);
-
- if (inparts != inparts_) {
- g_free(inparts[0]);
- g_free(inparts[1]);
- } else
- g_free(inparts[1]);
- }
- g_strfreev(parts);
-}
-
void
settings_init () {
State* s = &uzbl.state;
@@ -1559,32 +1480,34 @@ void handle_authentication (SoupSession *session, SoupMessage *msg, SoupAuth *au
(void) user_data;
if (uzbl.behave.authentication_handler && *uzbl.behave.authentication_handler != 0) {
- gchar *info, *host, *realm;
- gchar *p;
-
soup_session_pause_message(session, msg);
- /* Sanitize strings */
- info = g_strdup(soup_auth_get_info(auth));
- host = g_strdup(soup_auth_get_host(auth));
- realm = g_strdup(soup_auth_get_realm(auth));
- for (p = info; *p; p++) if (*p == '\'') *p = '\"';
- for (p = host; *p; p++) if (*p == '\'') *p = '\"';
- for (p = realm; *p; p++) if (*p == '\'') *p = '\"';
+ GString *result = g_string_new ("");
- GString *s = g_string_new ("");
- g_string_printf(s, "'%s' '%s' '%s' '%s'",
- info, host, realm, retrying?"TRUE":"FALSE");
+ gchar *info = g_strdup(soup_auth_get_info(auth));
+ gchar *host = g_strdup(soup_auth_get_host(auth));
+ gchar *realm = g_strdup(soup_auth_get_realm(auth));
- run_handler(uzbl.behave.authentication_handler, s->str);
+ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+ const CommandInfo *c = parse_command_parts(uzbl.behave.authentication_handler, a);
+ if(c) {
+ sharg_append(a, info);
+ sharg_append(a, host);
+ sharg_append(a, realm);
+ sharg_append(a, retrying ? "TRUE" : "FALSE");
+
+ run_parsed_command(c, a, result);
+ }
+ g_array_free(a, TRUE);
- if (uzbl.comm.sync_stdout && strcmp (uzbl.comm.sync_stdout, "") != 0) {
+ if (result->len > 0) {
char *username, *password;
int number_of_endls=0;
- username = uzbl.comm.sync_stdout;
+ username = result->str;
- for (p = uzbl.comm.sync_stdout; *p; p++) {
+ gchar *p;
+ for (p = result->str; *p; p++) {
if (*p == '\n') {
*p = '\0';
if (++number_of_endls == 1)
@@ -1598,12 +1521,9 @@ void handle_authentication (SoupSession *session, SoupMessage *msg, SoupAuth *au
soup_auth_authenticate(auth, username, password);
}
- if (uzbl.comm.sync_stdout)
- uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout);
-
soup_session_unpause_message(session, msg);
- g_string_free(s, TRUE);
+ g_string_free(result, TRUE);
g_free(info);
g_free(host);
g_free(realm);
@@ -1714,6 +1634,7 @@ initialize(int argc, char** argv) {
uzbl.info.arch = ARCH;
uzbl.info.commit = COMMIT;
+ uzbl.state.last_result = NULL;
/* Parse commandline arguments */
GOptionContext* context = g_option_context_new ("[ uri ] - load a uri by default");
diff --git a/src/uzbl-core.h b/src/uzbl-core.h
index 76421d2..a10c0ce 100644
--- a/src/uzbl-core.h
+++ b/src/uzbl-core.h
@@ -43,6 +43,10 @@
#include <sys/ioctl.h>
#include <assert.h>
+#if GTK_CHECK_VERSION(2,91,0)
+ #include <gtk/gtkx.h>
+#endif
+
#include "cookie-jar.h"
#define LENGTH(x) (sizeof x / sizeof x[0])
@@ -90,7 +94,6 @@ typedef struct {
gchar *socket_path;
GHashTable *proto_var; /* stores (key)"variable name" -> (value)"pointer to var */
- gchar *sync_stdout;
GPtrArray *connect_chan;
GPtrArray *client_chan;
} Communication;
@@ -107,6 +110,7 @@ typedef struct {
gchar* searchtx;
gboolean verbose;
GdkEventButton* last_button;
+ gchar* last_result;
gboolean plug_mode;
/* Events */
@@ -148,7 +152,6 @@ typedef struct {
gchar* socket_dir;
/* Handlers */
- gchar* cookie_handler;
gchar* authentication_handler;
gchar* scheme_handler;
gchar* download_handler;
@@ -251,9 +254,9 @@ typedef struct {
/*@null@*/ void (*func)(void);
} uzbl_cmdprop;
-
/* Functions */
void clean_up(void);
+void update_title(void);
/* Signal management functions */
void catch_sigterm(int s);
@@ -278,9 +281,6 @@ void spawn_sync_exec(WebKitWebView *web_view, GArray *argv, GString *resu
void parse_command(const char *cmd, const char *param, GString *result);
void parse_cmd_line(const char *ctl_line, GString *result);
-
-void update_title(void);
-
/* Keyboard events functions */
gboolean key_press_cb(GtkWidget* window, GdkEventKey* event);
gboolean key_release_cb(GtkWidget* window, GdkEventKey* event);
@@ -340,10 +340,20 @@ void builtins();
typedef void (*Command)(WebKitWebView*, GArray *argv, GString *result);
typedef struct {
- Command function;
- gboolean no_split;
+ const gchar *key;
+ Command function;
+ gboolean no_split;
} CommandInfo;
+const CommandInfo *
+parse_command_parts(const gchar *line, GArray *a);
+
+void
+parse_command_arguments(const gchar *p, GArray *a, gboolean no_split);
+
+void
+run_parsed_command(const CommandInfo *c, GArray *a, GString *result);
+
typedef struct {
gchar* name;
gchar* cmd;
diff --git a/tests/test-command.c b/tests/test-command.c
index 7b33405..d07bf79 100644
--- a/tests/test-command.c
+++ b/tests/test-command.c
@@ -263,11 +263,12 @@ test_toggle_status (void) {
void
test_sync_sh (void) {
- parse_cmd_line("sync_sh 'echo Test echo.'", NULL);
- g_assert_cmpstr("Test echo.\n", ==, uzbl.comm.sync_stdout);
+ GString *result = g_string_new("");
+
+ parse_cmd_line("sync_sh 'echo Test echo.'", result);
+ g_assert_cmpstr("Test echo.\n", ==, result->str);
- /* clean up after ourselves */
- uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout);
+ g_string_free(result, TRUE);
}
void
@@ -282,26 +283,18 @@ test_js (void) {
}
void
-test_run_handler_arg_order (void) {
- run_handler("sync_spawn echo uvw xyz", "abc def");
-
- assert(uzbl.comm.sync_stdout);
-
- /* the rest of the result should be the arguments passed to run_handler. */
- /* the arguments in the second argument to run_handler should be placed before any
- * included in the first argument to run handler. */
- g_assert_cmpstr("abc def uvw xyz\n", ==, uzbl.comm.sync_stdout);
-}
+test_last_result (void) {
+ GString *result = g_string_new("");
-void
-test_run_handler_expand (void) {
- uzbl.net.useragent = "Test uzbl uzr agent";
- run_handler("sync_spawn echo @useragent", "result:");
+ /* the last result gets set */
+ parse_cmd_line("js -1", result);
+ g_assert_cmpstr("-1", ==, uzbl.state.last_result);
- assert(uzbl.comm.sync_stdout);
+ /* the last result can be used in a chain */
+ parse_cmd_line("chain 'js 1' 'js \\@_ + 1'", result);
+ g_assert_cmpstr("2", ==, uzbl.state.last_result);
- /* the user-specified arguments to the handler should have been expanded */
- g_assert_cmpstr("result: Test uzbl uzr agent\n", ==, uzbl.comm.sync_stdout);
+ g_string_free(result, TRUE);
}
int
@@ -320,10 +313,7 @@ main (int argc, char *argv[]) {
g_test_add_func("/test-command/js", test_js);
- /* the following aren't really "command" tests, but they're not worth
- * splitting into a separate file yet */
- g_test_add_func("/test-command/run_handler/arg-order", test_run_handler_arg_order);
- g_test_add_func("/test-command/run_handler/expand", test_run_handler_expand);
+ g_test_add_func("/test-command/last-result", test_last_result);
/* set up uzbl */
initialize(argc, argv);