[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[mhc:02170] Re: 形式エラーのチェックについて質問



From: NOGUCHI Yusuke <nogunogu@xxxxxxxxxxxxxxxxx> さん曰く
Subject: [mhc:02168] Re: 形式エラーのチェックについて質問
Message-ID: <20051004.151123.86422591.nogunogu@xxxxxxxxxxxxxxxxx>
Date: Tue, 04 Oct 2005 15:11:23 +0900 (JST)

> > これは、あくまで、以前野口さんが間違って登録したときの残骸ですよね。
> 
> おぉ…。大変失礼しました。白井さんの御指摘を受けて、schedule/ 以下をよ
> くよく確認してみたところ、今回のパッチを作成するにあたり、実験のために
> 作成していたダミー(不正形式)のスケジュールが残ったままでした…。(全て
> 消したものと思いこんでおりましたので、検討外れの対応をしていました)
> 
> % grep X-SC-Time schedule/**/*
> :
> X-SC-Time: 10:00 12:00
> X-SC-Time: 9:00
> : 

今までのスケジュールを見るときにエラーしちゃうと困るけど、登録時
のパースチェックを厳密に行うのは良い考えだと思うので、こんなパッ
チをつくってみました。

現在の MHC CVS にあたります。本当は mhc-parse/strict という変数
をもっとちゃんと(^^;)扱った方が良いと思うのですが手抜きです。

## 手を抜きすぎてなにか間違っているかも。あまり動作確認していな
## いし。

-- 
白井秀行 (mailto:shirai@xxxxxxxxxxxxx)
Index: mhc-draft.el
===================================================================
RCS file: /cvsroot/mhc/emacs/mhc-draft.el,v
retrieving revision 1.7
diff -u -r1.7 mhc-draft.el
--- mhc-draft.el	4 May 2004 13:48:31 -0000	1.7
+++ mhc-draft.el	4 Oct 2005 06:00:30 -0000
@@ -167,7 +167,8 @@
   "Add current draft as a schedule."
   (interactive)
   (let ((record
-	 (mhc-parse-buffer (mhc-record-new mhc-draft-buffer-file-name))))
+	 (mhc-parse-buffer (mhc-record-new mhc-draft-buffer-file-name)
+			   'strict)))
     (mhc-calendar-input-exit)
     (if (mhc-db-add-record-from-buffer record (current-buffer)
 				       (not (interactive-p)))
Index: mhc-parse.el
===================================================================
RCS file: /cvsroot/mhc/emacs/mhc-parse.el,v
retrieving revision 1.11
diff -u -r1.11 mhc-parse.el
--- mhc-parse.el	3 Oct 2005 14:58:02 -0000	1.11
+++ mhc-parse.el	4 Oct 2005 06:00:30 -0000
@@ -18,6 +18,7 @@
 (require 'mhc-record)
 (require 'mhc-header)
 
+(defvar mhc-parse/strict nil)
 
 (defun mhc-parse/continuous-lines ()
   "ヘッダの継続行を処理して、内容のみを取り出す関数"
@@ -78,24 +79,26 @@
    (mhc-eword-decode-string (mhc-parse/continuous-lines)))
   schedule)
 
-(defconst mhc-parse/time-regexp "\\([012][0-9]\\):\\([0-5][0-9]\\)")
+(defconst mhc-parse/time-regexp "\\([012]?[0-9]\\):\\([0-5][0-9]\\)")
 
 (defun mhc-parse/time (record schedule)
-  (let ((time (mhc-parse/continuous-lines)))
-    (let (begin end)
-      (cond
-       ((string-match (concat "^" mhc-parse/time-regexp "-" mhc-parse/time-regexp "$") time)
-	(setq begin (+ (* 60 (string-to-number (match-string 1 time)))
-		       (string-to-number (match-string 2 time)))
-	      end (+ (* 60 (string-to-number (match-string 3 time)))
-		     (string-to-number (match-string 4 time)))))
-       ((string-match (concat "^" mhc-parse/time-regexp "-?$") time)
-	(setq begin (+ (* 60 (string-to-number (match-string 1 time)))
-		       (string-to-number (match-string 2 time)))))
-       ((string-match (concat "^-" mhc-parse/time-regexp "$") time)
-	(setq end (+ (* 60 (string-to-number (match-string 1 time)))
-		     (string-to-number (match-string 2 time))))))
-      (mhc-schedule/set-time schedule begin end)))
+  (let ((time (mhc-parse/continuous-lines))
+	begin end)
+    (cond
+     ((string-match (concat "^" mhc-parse/time-regexp "-" mhc-parse/time-regexp "$") time)
+      (setq begin (+ (* 60 (string-to-number (match-string 1 time)))
+		     (string-to-number (match-string 2 time)))
+	    end (+ (* 60 (string-to-number (match-string 3 time)))
+		   (string-to-number (match-string 4 time)))))
+     ((string-match (concat "^" mhc-parse/time-regexp "-?$") time)
+      (setq begin (+ (* 60 (string-to-number (match-string 1 time)))
+		     (string-to-number (match-string 2 time)))))
+     ((string-match (concat "^-" mhc-parse/time-regexp "$") time)
+      (setq end (+ (* 60 (string-to-number (match-string 1 time)))
+		   (string-to-number (match-string 2 time)))))
+     ((and mhc-parse/strict (not (string= "" time)))
+      (error "Parse ERROR!!!(at X-SC-Time:)")))
+    (mhc-schedule/set-time schedule begin end))
   schedule)
 
 ;; For backward compatibility.
@@ -103,8 +106,15 @@
   (mhc-logic-parse-old-style-date (mhc-schedule-condition schedule))
   (mhc-parse/time record schedule))
 
+(defconst mhc-parse/alarm-regexp "^[0-9]+ \\(minute\\|hour\\|day\\)$")
+
 (defun mhc-parse/alarm (record schedule)
-  (mhc-schedule/set-alarm schedule (mhc-parse/continuous-lines))
+  (let ((alarm (mhc-parse/continuous-lines)))
+    (unless (or (not mhc-parse/strict)
+		(string-match mhc-parse/alarm-regexp alarm)
+		(string= "" alarm))
+      (error "Parse ERROR!!! (at X-SC-Alarm:)"))
+    (mhc-schedule/set-alarm schedule alarm))
   schedule)
 
 (defun mhc-parse/category (record schedule)
@@ -165,9 +175,11 @@
 
 ;; FIXME: top level とそれ以外の場所で許される header が異なるので、
 ;; multi pass parser に組み替えるべきかも知れない。
-(defun mhc-parse/internal-parser (record &optional schedule)
+(defun mhc-parse/internal-parser (record &optional schedule strict)
   "Internal parseser of schedule headers in this narrowed buffer."
-  (let (func (case-fold-search t))
+  (let ((mhc-parse/strict strict)
+	(case-fold-search t)
+	func)
     (while (not (eobp))
       (if (looking-at "\\([^ \t:]+\\):")
 	  (progn
@@ -190,12 +202,12 @@
 	(forward-line 1))))
   schedule)
 
-(defun mhc-parse-buffer (&optional record)
+(defun mhc-parse-buffer (&optional record strict)
   "Parse schedule headers in this buffer."
   (unless record
     (setq record (mhc-record-new (buffer-file-name))))
   (mhc-header-narrowing
-    (let ((schedule (mhc-parse/internal-parser record)))
+    (let ((schedule (mhc-parse/internal-parser record nil strict)))
       (if schedule (mhc-schedule/set-region-end schedule (point)))))
   ;; 得られた構造を整理する
   (let (schedules sexp)