all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@IRO.UMontreal.CA>
To: Douglas Lewan <d_lewan2000@yahoo.com>
Cc: "emacs-devel@gnu.org" <emacs-devel@gnu.org>
Subject: Re: Proposal: cpio-mode
Date: Thu, 16 Apr 2015 16:42:45 -0400	[thread overview]
Message-ID: <jwvh9sfzu7p.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <1061117555.4572808.1429211310562.JavaMail.yahoo@mail.yahoo.com> (Douglas Lewan's message of "Thu, 16 Apr 2015 19:08:30 +0000 (UTC)")

> A cpio mode has been in the emacs TODO file for as long as I've looked there.
> I'd like to write that. Is there really still interest?

It'd fit very nicely in GNU ELPA, I think, yes.  Of course, the fact
that noone wrote one up might indicate that the demand is not very high,
but I for one would enjoy easier access to the initramfs files, for example.


        Stefan


PS: For what it's worth, here's my earlier attempt at such a thing.
Obviously I stopped before getting to the meat of it.
PPS: Also, there are various possible options in terms of UI: you could
integrate cpio support into arc-mode.el, or you could "do it by hand"
along the lines of tar-mode.el, or you make a magic file-name handler
(and rely on dired for the UI).


;;; cpio-mode.el --- View/edit cpio archives

;; Copyright (C) 2012  Stefan Monnier

;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
;; Keywords: 

;; 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/>.

;;; Commentary:

;; 

;;; Code:

;; The format supported is the "New ASCII Format" as documented in
;; http://people.freebsd.org/~kientzle/libarchive/man/cpio.5.txt.

(eval-when-compile (require 'cl))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.cpio\\'" . cpio-mode))

;;;###autoload
(when (boundp 'inhibit-local-variables-regexps)
  (add-to-list 'inhibit-local-variables-regexps "\\.cpio\\'"))

;;;###autoload
(define-derived-mode cpio-mode special-mode "CPIO"
  "View/edit cpio archives."
  (add-hook 'change-major-mode-hook
            (lambda () (with-silent-modifications
                    (save-restriction
                      (widen)
                      (remove-text-properties (point-min) (point-max)
                                              '(display nil invisible nil))))))
  (cpio--parse-buffer))

(defun cpio--parse-mode (mode)
  (string
   (case (logand #o170000 mode)
     (#o140000 ?s)
     (#o120000 ?l)
     (#o100000 ?-)
     (#o060000 ?b)
     (#o040000 ?d)
     (#o020000 ?c)
     (#o010000 ?p)
     (t (error "Unknown mode %S" mode)))
   (if (zerop (logand #o400 mode)) ?- ?r)
   (if (zerop (logand #o200 mode)) ?- ?w)
   (if (zerop (logand #o100 mode))
       (if (zerop (logand #o4000 mode)) ?- ?S)
     (if (zerop (logand #o4000 mode)) ?x ?s))
   (if (zerop (logand #o040 mode)) ?- ?r)
   (if (zerop (logand #o020 mode)) ?- ?w)
   (if (zerop (logand #o010 mode))
       (if (zerop (logand #o2000 mode)) ?- ?S)
     (if (zerop (logand #o2000 mode)) ?x ?s))
   (if (zerop (logand #o004 mode)) ?- ?r)
   (if (zerop (logand #o002 mode)) ?- ?w)
   (if (zerop (logand #o001 mode)) ?- ?x)))
     

(defun cpio--parse-buffer ()
  (save-excursion
    (save-restriction
      (widen)
      (goto-char (point-min))
      (with-silent-modifications
        (while (not (eobp))
          (assert (zerop (mod (- (point) (point-min)) 4)))
          (cond
           ((not (looking-at "07070[12]\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)[[:xdigit:]]\\{8\\}\\|\0+\\'"))
            (error "Unrecognized cpio header format"))
           ((not (match-beginning 1))
            ;; Reached the trailing padding, just skip it.
            (put-text-property (point) (point-max) 'invisible t)
            (goto-char (match-end 0)))
           (t
            (let* ((ino (string-to-number (match-string 1) 16))
                   (mode (string-to-number (match-string 2) 16))
                   (uid (string-to-number (match-string 3) 16))
                   (gid (string-to-number (match-string 4) 16))
                   (nlink (string-to-number (match-string 5) 16))
                   (mtime (string-to-number (match-string 6) 16))
                   (filesize (string-to-number (match-string 7) 16))
                   (devmajor (string-to-number (match-string 8) 16))
                   (devminor (string-to-number (match-string 9) 16))
                   (rdevmajor (string-to-number (match-string 10) 16))
                   (rdevminor (string-to-number (match-string 11) 16))
                   (namesize (string-to-number (match-string 12) 16))
                   (namebeg (match-end 0))
                   (name (buffer-substring namebeg (+ namebeg namesize -1)))
                   (filebeg (+ (match-end 0) 2 (* (/ (+ namesize 1) 4) 4)))
                   (next (+ filebeg (* (/ (+ filesize 3) 4) 4))))
              (if (and (zerop ino) (zerop mode) (zerop filesize)
                       (equal name "TRAILER!!!"))
                  (put-text-property (match-beginning 0) next
                                     'display "\n<trailer>")
                (if (bobp)
                    (put-text-property (point) (match-end 1) 'invisible t)
                  (put-text-property (point) (match-end 1) 'display "\n"))
                (put-text-property (match-beginning 2) (match-end 2)
                                   'display (cpio--parse-mode mode))
                (put-text-property (match-beginning 3) (match-end 3)
                                   'display (format "%10d" uid))
                (put-text-property (match-beginning 4) (match-end 4)
                                   'display (format "/%-10d" gid))
                (put-text-property (match-beginning 5) ;; (match-end 6)
                ;;                    'invisible t)
                ;; (put-text-property (match-beginning 7)
                                   (match-end 7)
                                   'display (format "%10d" filesize))
                (put-text-property (match-beginning 8) namebeg
                                   'display " ")
                (if (= (logand #o170000 mode) #o120000) ;Symlink
                    (progn
                      (put-text-property (+ namebeg namesize -1) filebeg
                                         'display " -> ")
                      (put-text-property (+ filebeg filesize) next
                                         'invisible t))
                  (put-text-property (+ namebeg namesize -1) next
                                     'invisible t))
                (message "Parsed %S of size %d" name filesize))
              (goto-char next)))))))))

(provide 'cpio-mode)
;;; cpio-mode.el ends here



  reply	other threads:[~2015-04-16 20:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-16 19:08 Proposal: cpio-mode Douglas Lewan
2015-04-16 20:42 ` Stefan Monnier [this message]
2016-10-20 11:02   ` Lars Brinkhoff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvh9sfzu7p.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=d_lewan2000@yahoo.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.