home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / crontab.el < prev    next >
Encoding:
Text File  |  1990-03-21  |  5.9 KB  |  167 lines

  1. ;From: chris@lxn.eds.com (Christopher D. Orr)
  2. ;Newsgroups: comp.emacs
  3. ;Subject: crontab.el (Functions to allow editing of crontab)
  4. ;Message-ID: <429@lxn.eds.com>
  5. ;Date: 18 Aug 89 16:19:47 GMT
  6. ;Organization: Electronic Data Systems (EDS), Center Valley, PA
  7. ;Lines: 157
  8. ;Keywords: crontab system administrators cron
  9. ;
  10. ;
  11. ;Among the wealth of Emacs lisp code, I expected to find somebody who
  12. ;had written a function for editing a user's crontab entry.  But alas,
  13. ;I could not find one.  Enclosed is such a function.
  14. ;
  15. ;Please report and bug reports to me and any enhancements, refinements,
  16. ;and/or rewrites that are made.
  17. ;
  18. ;------------------------------ CUT HERE ------------------------------
  19. ;
  20. ;; #(@) crontab.el - An Emacs function to assist in editing crontab entries
  21. ;; Last edited: Fri Aug 18 12:19:22 1989 by chris (Christopher D. Orr) on lxn
  22. ;;
  23. ;; Version: 1.00 - Initial Creation of mode
  24. ;;          1.01 - Added crontab-use-local-file variable
  25. ;;          1.02 - Reworked most of the library to be cleaner.
  26. ;;          1.03 - Now deletes blank lines in crontab entry
  27.  
  28. ;; Copyright (C) 1989 Christopher D. Orr (chris@lxn.eds.com)
  29.  
  30. ;; This file NOT YET is part of GNU Emacs.
  31.  
  32. ;; GNU Emacs is distributed in the hope that it will be useful,
  33. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  34. ;; accepts responsibility to anyone for the consequences of using it
  35. ;; or for whether it serves any particular purpose or works at all,
  36. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  37. ;; License for full details.
  38.  
  39. ;; Everyone is granted permission to copy, modify and redistribute
  40. ;; GNU Emacs, but only under the conditions described in the
  41. ;; GNU Emacs General Public License.   A copy of this license is
  42. ;; supposed to have been given to you along with GNU Emacs so you
  43. ;; can know your rights and responsibilities.  It should be in a
  44. ;; file named COPYING.  Among other things, the copyright notice
  45. ;; and this notice must be preserved on all copies.
  46. ;;
  47. ;;
  48. ;; TODO:
  49. ;;
  50.  
  51. ;; Place the following line in your ~/.emacs file:
  52. ;;    (autoload 'crontab-edit "crontab"
  53. ;;              "Function to allow the easy editing of crontab files." t)
  54. ;;
  55.  
  56. (provide 'crontab-edit)
  57.  
  58. ;;; Local Variables.  Define these to your liking.
  59.  
  60. (defvar crontab-filename "~/.crontab"
  61.   "*The name of the file to store the User's Crontab.")
  62.  
  63. (defvar crontab-directory "/usr/spool/cron/crontabs"
  64.   "*The name of the directory in which crontab stores it's entries.")
  65.  
  66. (defvar crontab-use-local-file nil
  67.   "*Non-nil means use file stored in User's Home directory, if it exists.
  68. Otherwise, always ask crontab for the current entry (maybe).")
  69.  
  70.  
  71. ;;; Interactive Function called to edit a Crontab Entry.  It is called
  72. ;;; instead of crontab-edit to allow for future automatic entries.
  73.  
  74. (defun crontab-edit ()
  75.   "Function to allow the easy editing of crontab files."
  76.  
  77.   (interactive)
  78.   (crontab-get))
  79.  
  80.  
  81. ;;; Function to retrieve the crontab entry.  The Function will
  82. ;;; retrieve the file (crontab-filename) first.  If the file does not
  83. ;;; exists, a crontab -l command will be executed. 
  84.  
  85. (defun crontab-get ()
  86.    "Retrieve a crontab file either using crontab -l or from the variable
  87. crontab-filename"
  88.    (message "Retrieving Crontab ... ")
  89.    (switch-to-buffer (create-file-buffer crontab-filename))
  90.    (erase-buffer)
  91.  
  92.    (if (file-exists-p crontab-filename)
  93.        (if (file-newer-than-file-p (concat crontab-directory "/" (user-login-name)) (expand-file-name crontab-filename))
  94.        (if (yes-or-no-p "Cron has a more recent copy of your crontab.  Use it ? ")
  95.            (call-process "crontab" nil t t "-l")
  96.          (insert-file crontab-filename))
  97.      (if crontab-use-local-file
  98.          (insert-file crontab-filename)
  99.        (call-process "crontab" nil t t "-l")))
  100.      (if crontab-use-local-file
  101.      (insert-file crontab-filename)
  102.        (call-process "crontab" nil t t "-l")))
  103.  
  104. ;; What if crontab returns a fatal ??????  Can't we check the errorlevel ????
  105.    (goto-char (point-min))
  106.    (if (search-forward "crontab:" nil t nil)
  107.        (erase-buffer))
  108.    (if (eobp)
  109.        (crontab-initialize))
  110.    (goto-line 6)
  111.    (setq buffer-file-name crontab-filename)
  112.    (set-buffer-modified-p nil)
  113.    (make-variable-buffer-local 'write-file-hooks)
  114.    (or (memq 'crontab-save write-file-hooks)
  115.        (setq write-file-hooks 
  116.          (reverse (cons 'crontab-save (reverse write-file-hooks)))))
  117.    (message "Save file normally when finished to update cron."))
  118.  
  119.  
  120. ;;; This function is called whenever a save-file operation is
  121. ;;; performed in the crontab buffer.  It saves the crontab to the file
  122. ;;; name (crontab-filename) and then removes the crontab buffer.
  123.  
  124. (defun crontab-save ()
  125.   "Submit the edited crontab to the cron deamon for processing"
  126.  
  127.   (goto-char (point-min))
  128.   (while (not (eobp))
  129.     (delete-blank-lines)
  130.     (forward-line 1))
  131.   (redraw-display)
  132.  
  133.   (setq write-file-hooks nil)
  134.   (let ((crontab-buffer (buffer-name)))
  135.     (basic-save-buffer)
  136.  
  137. ;; What if the call-process to crontab fails ???  Can we check for a fatal ???
  138. ;;  (call-process "crontab" nil nil nil (expand-file-name crontab-filename))
  139.     (shell-command (concat "crontab " (expand-file-name crontab-filename)))
  140.  
  141.     (switch-to-buffer (other-buffer))
  142.     (kill-buffer crontab-buffer))
  143.   (message (concat "Crontab saved as " crontab-filename " and submitted to cron."))
  144.   nil)
  145.  
  146. (defun crontab-initialize ()
  147.   "Create a default Crontab file if one does not exist or is empty.
  148. If the function (time-stamp) is available, the last modification time will
  149. be stamped to the file."
  150.  
  151.    (insert "# Cron Table Entry for ")
  152.    (insert (user-login-name))
  153.    (insert " (")
  154.    (insert (user-full-name))
  155.    (insert ")\n# Last Edited: \n")
  156.    (insert "#\n")
  157.    (insert "# min    hr     day   mon    wday(0=sun)  cmd\n")
  158.    (insert "#\n"))
  159.  
  160. ;;; Watch out for the signature  :-)
  161.  
  162. ;-- 
  163. ;Christopher D. Orr                  | US MAIL: Electronic Data Systems (EDS)
  164. ;UUCP: vu-vlsi!lxn!chris             |          Lanark Building
  165. ; or   chris%lxn.uucp@rutgers.edu    |          Center Valley, PA  18034
  166. ; or   lehi3b15!lxn!chris            | Voice:   (215) 282-1213
  167.