From 6f4eec14247760680cf2509f3fb8e49f4006c3f3 Mon Sep 17 00:00:00 2001 From: Krzysztof Drewniak Date: Sat, 5 Mar 2011 14:56:14 -0600 Subject: [PATCH 1/4] Aded code to parse command-line options. --- options.lisp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ stumpwm.asd | 1 + stumpwm.lisp | 19 +++++++++++++++++-- 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 options.lisp diff --git a/options.lisp b/options.lisp new file mode 100644 index 0000000..961e37b --- /dev/null +++ b/options.lisp @@ -0,0 +1,53 @@ +;; Copyright (C) 2003-2008 Shawn Betts +;; +;; This file is part of stumpwm. +;; +;; stumpwm 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 2, or (at your option) +;; any later version. + +;; stumpwm 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 software; see the file COPYING. If not, write to +;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330, +;; Boston, MA 02111-1307 USA + +;; Commentary: +;; +;; Options and option parsing code +;; +;; Code: + +(in-package :stumpwm) + +(defvar *options* nil "The structure that holds the results of the parsing of the command-line options") + +(defclass options () + ((help :initform nil) (version :initform nil) (replace :initform nil))) + +(let ((options-list + ;; Each element of this list is of the form + ;; (option-symbol help-text &rest text-to-match) + '(('help "Show this help text and exit." "--help" "-h") + ('version "Print the version of the program and exit." "--version" "-v") + ('replace "Replace any window manager already running." "--replace")))) + + (defun parse-options (args) + (let ((struct (make-instance 'options))) + (loop for i in args do + (loop for (symbol help &rest matches) in options-list do + (when (member i matches :test #'string=) + (setf (slot-value struct symbol) t)))) + struct)) + + (defun show-help () + (loop for (symbol help &rest matches) in options-list do + (format t "~{~a~^, ~}~20,1T~a" matches help)))) + +(defun get-option (option &optional (struct *options*)) + (slot-value option struct)) diff --git a/stumpwm.asd b/stumpwm.asd index 9bb3cf8..75870ad 100644 --- a/stumpwm.asd +++ b/stumpwm.asd @@ -47,6 +47,7 @@ (:file "bindings") (:file "events") (:file "help") + (:file "options") (:file "fdump") (:file "time") (:file "mode-line") diff --git a/stumpwm.lisp b/stumpwm.lisp index a356e12..c19f214 100644 --- a/stumpwm.lisp +++ b/stumpwm.lisp @@ -58,8 +58,11 @@ loaded. When CATCH-ERRORS is nil, errors are left to be handled further up. " (find error-key '(xlib:window-error xlib:drawable-error xlib:match-error))) (dformat 4 "Ignoring error: ~s~%" error-key)) ((eq error-key 'xlib:access-error) - (write-line "Another window manager is running.") - (throw :top-level :quit)) + (if (get-option 'replace) + (replace-wm) + (progn + (write-line "Another window manager is running.") + (throw :top-level :quit)))) ;; all other asynchronous errors are printed. (asynchronous (message "Caught Asynchronous X Error: ~s ~s" error-key key-vals)) @@ -196,6 +199,16 @@ of those expired." :local) (t :internet))))) +(defun handle-options () + (setf *options* (parse-options (argv))) + (let ((option (or (and (get-option 'help) 'help) + (and (get-option 'version) 'version)))) + (when option + (case option + ('help (show-help)) + ('version (format t *version*))) + (throw :top-level :quit)))) + (defun stumpwm-internal (display-str) (multiple-value-bind (host display screen protocol) (parse-display-string display-str) (declare (ignore screen)) @@ -219,6 +232,8 @@ of those expired." (if success (and *startup-message* (message *startup-message* (print-key *escape-key*))) (message "^B^1*Error loading ^b~A^B: ^n~A" rc err)))) + ;; Options that must be acted on here are acted on + (handle-options) (when *last-unhandled-error* (message-no-timeout "^B^1*StumpWM Crashed With An Unhandled Error!~%Copy the error to the clipboard with the 'copy-unhandled-error' command.~%^b~a^B^n~%~%~a" (first *last-unhandled-error*) (second *last-unhandled-error*))) -- 1.7.1