mardi 19 août 2014

Code Style

This tools allows you to reformat your code in order to respect some particular coding rules.
This is for example what you want to use as a trigger in your svn, git or clearcase repo ;-)

http://astyle.sourceforge.net/


vendredi 8 août 2014

KSH eval / variable subtitution / variable test

from :http://b62.tripod.com/doc/docksh.htm

OTHER FUNCTIONALITIES


cmd1 || cmd2    exec cmd2 if cmd1 fail
cmd1 && cmd2    exec cmd2 if cmd1 is OK

V1=${V2:=V3}    Set V1 with the value of V2 if this is set else set the
                variable V1 with value of V3 (V3 could be a number).
                sh replacement:  if [ $V2 ] ; then
                                                V1=$V2
                                 else
                                                V1=$V3
                Example: DisplaySize=${LINES:24} ; Command=${Command:"cat"}


${V1:?word}     if V1 set  & V1!=null   ret $V1 else print word and exit
                  : ${V1:?"variable V1 not set on null"}
${V1:=word}     if V1 !set | V1==null   set V1=$word
${V1:-word}     if V1 set  & V1!=null   ret $V1 else ret word
${V1:+word}     if V1 set  & V1!=null   ret word else ret nothing
${V1##patt}
${V1#patt}      if patt are found at the begin of V1 return V1 whitout the patt
                else return V1
                V1="lspwd" ; ${V1#"ls"}  # exec pwd
${V1%%patt}
${V1%patt}      if patt are found at the end of V1 return V1 whitout the patt
                else return V1
                V1="lspwd" ; ${V1%"pwd"}  # exec ls 
 
 
from : http://www.well.ox.ac.uk/~johnb/comp/unix/ksh.html 
Variable Manipulations

Removing something from a variable

Variables that contain a path can very easily be stripped of it: ${name##*/} gives you just the filename.
Or if one wants the path: ${name%/*}. % takes it away from the left and # from the right.
%% and ## take the longest possibility while % and # just take the shortest one.

Replacing a variable if it does not yet exits

If we wanted $foo or if not set 4 then: ${foo:-4} but it still remains unset. To change that we use:
${foo:=4}

Exiting and stating something if variable is not set

This is very important if our program relays on a certain vaiable: ${foo:?"foo not set!"}

Just check for the variable

${foo:+1} gives one if $foo is set, otherwise nothing.
 

KSH : $- and Flags

Under Solaris and using the Korn Shell KSH, the $- allows access to the flags setted.
Example of how it changes when setting the debug mode '-x' :

# echo $-
ismC
# set -x
# echo $-
+ echo isxmC
isxmC
# set +x

man ksh informs us that (cf. http://www.lehman.cuny.edu/cgi-bin/man-cgi?ksh+1 for example)

  "   -  "         Correspond to the "Flags supplied to the shell on invocation  or  by the set command." and that "The current set of flags can be found in $- ":



  Parameters Set by Shell
     -          Flags supplied to the shell on invocation  or  by
                the set command.

   
     set [ _abCefhkmnopstuvx ] [ _o option ]... [ _A name ] [ arg
     ... ]

         The flags for this command have meaning as follows:

         -A          Array assignment. Unsets the  variable  name
                     and  assigns  values  sequentially  from the
                     list arg. If +A is used, the  variable  name
                     is not unset first.

         -a          All subsequent variables  that  are  defined
                     are automatically exported.

         -b          Causes the shell to notify  the  user  asyn-
                     chronously  of  background  job completions.
                     The following message is written to standard
                     error:

                       "[%d]%c %s%s\n", , , , \
                           

                     where the fields are as follows:

                            The character  +  identifies
                                     the  job  that would be used
                                     as a default for the  fg  or
                                     bg  utilities.  This job can
                                     also be specified using  the
                                     job_id %+ or %%. The charac-
                                     ter  -  identifies  the  job
                                     that    would   become   the
                                     default   if   the   current
                                     default  job  were  to exit;
                                     this job can also be  speci-
                                     fied  using  the  job_id %-.
                                     For other jobs,  this  field
                                     is  a  space  character.  At
                                     most one job can be  identi-
                                     fied  with + and at most one
                                     job can be  identified  with
                                     -. If there is any suspended
                                     job, then the current job is
                                     a  suspended  job.  If there
                                     are at least  two  suspended
                                     jobs,  then the previous job
                                     is also a suspended job.

                         A number that can be used to
                                     identify  the  process group
                                     to the  wait,  fg,  bg,  and
                                     kill  utilities. Using these
                                     utilities, the  job  can  be
                                     identified  by prefixing the
                                     job number with %.

                             Unspecified.

                           Unspecified.

                     When the shell notifies the user a  job  has
                     been completed, it can remove the job's pro-
                     cess ID from the list of those known in  the
                     current  shell  execution environment. Asyn-
                     chronous  notification  is  not  enabled  by
                     default.

         -C          Prevents existing files from being overwrit-
                     ten  by  the shell's > redirection operator.
                     The >| redirection operator  overrides  this
                     noclobber option for an individual file.

         -e          If a command has  a  non-zero  exit  status,
                     executes  the  ERR  trap,  if set, and exit.
                     This mode is  disabled  while  reading  pro-
                     files.
         -f          Disables file name generation.

         -h          Each command becomes a  tracked  alias  when
                     first encountered.

         -k          All variable assignment arguments are placed
                     in  the  environment for a command, not just
                     those that precede the command name.

         -m          Background jobs runs in a  separate  process
                     group and a line prints upon completion. The
                     exit status of background jobs  is  reported
                     in a completion message. On systems with job
                     control, this flag is  turned  on  automati-
                     cally for interactive shells.

         -n          Reads commands and  check  them  for  syntax
                     errors, but do not execute them. Ignored for
                     interactive shells.

         -o          Writes the current option settings to  stan-
                     dard output in a format that is suitable for
                     reinput  to  the  shell  as  commands   that
                     achieve the same option settings.

         -o          The following argument can  be  one  of  the
                     following option names:

                     allexport     Same as -a.

                     errexit       Same as -e.

                     bgnice        All background jobs are run at
                                   a  lower priority. This is the
                                   default mode.

                     emacs         Puts you  in  an  emacs  style
                                   in-line   editor  for  command
                                   entry.

                     gmacs         Puts you in a gmacs style  in-
                                   line editor for command entry.                     
                     ignoreeof     The shell does not exit onEOF.
                                   The command exit must be used.

                     keyword       Same as -k.

                     markdirs      All directory names  resulting
                                   from file name generation have
                                   a trailing / appended.

                     monitor       Same as -m.

                     noclobber     Prevents  redirection  >  from
                                   truncating   existing   files.
                                   Require >| to truncate a  file
                                   when  turned on. Equivalent to
                                   -C.

                     noexec        Same as -n.

                     noglob        Same as -f.

                     nolog         Do not save  function  defini-
                                   tions in history file.

                     notify        Equivalent to -b.

                     nounset       Same as -u.

                     privileged    Same as -p.

                     verbose       Same as -v.

                     trackall      Same as -h.

                     vi            Puts you in insert mode  of  a
                                   vi  style in-line editor until
                                   you hit escape character  033.
                                   This puts you in control mode.
                                   A return sends the line.
                     viraw         Each character is processed as
                                   it is typed in vi mode.

                     xtrace        Same as -x.

                     If no option name is supplied,  the  current
                     option settings are printed.

         -p          Disables processing  of  the  $HOME/.profile
                     file  and  uses  the  file /etc/suid_profile
                     instead of the ENV file.  This  mode  is  on
                     whenever  the  effective uid is not equal to
                     the real uid, or when the effective  gid  is
                     not  equal to the real gid. Turning this off
                     causes the effective uid and gid to  be  set
                     to the real uid and gid.

         -s          Sorts the positional parameters lexicograph-
                     ically.

         -t          Exits after reading and executing  one  com-
                     mand.

         -u          Treats unset parameters  as  an  error  when
                     substituting.

         -v          Prints shell input lines as they are read.

         -x          Prints commands and their arguments as  they
                     are executed.

         -           Turns off -x and -v flags and stops  examin-
                     ing arguments for flags.

         --          Does not change any of the flags. Useful  in
                     setting  $1  to a value beginning with -. If
                     no arguments follow this flag then the posi-
                     tional parameters are unset.

                     Using + rather than - causes these flags  to
                     be  turned off. These flags can also be used
                     upon invocation of the  shell.  The  current
                     set  of  flags can be found in $-. Unless -A
                     is specified, the  remaining  arguments  are
                     positional  parameters  and are assigned, in
                     order, to $1 $2 ....  If  no  arguments  are
                     given, the names and values of all variables
                     are printed on the standard output.                                  

mercredi 23 juillet 2014

find + mv


find ./ -name "*.logfile" | xargs -i mv '{}' /my/really/great/implementation_scripts/log_directory/


(cf : http://bturnip.com/weblog/?p=248 )

or : 
find ./ -name "*.logfile" -exec  mv '{}' /my/really/great/implementation_scripts/log_directory/ \;

Test dir var (eval shell)



 Test if the given variable contains a valid directory. If not, exit. (This uses the eval function of the shell)
testdirvar () {
    tmp=$1
    if [ -d "${!tmp}" ] ; then
        echo "${tmp}=${!tmp} (variable ${tmp}: directory exists)"
    else
        echo "!!! ${tmp}=${!tmp} is not a directory (variable ${tmp}) !!!"
        exit
    fi
}

dimanche 20 octobre 2013

Disque dur externe NTFS sous Mac OS X

 Utiliser un disque dur externe NTFS sous Mac OS X

Il existe plusieurs solution. Ayant déjà configuré les macports puisque je développe un peu, j'ai choisi celle ci, qui consiste, une fois installé fuxe4x et ntfs-3g

sudo port update
sudo port install fuse4x ntfs-3g

Fuse4x + NTFS-3G

sauver le fichier :
sudo mv /sbin/mount_ntfs /sbin/mount_ntfs.orig

changer les droits d'exécution sur le script que l'on va créer :
sudo touch /sbin/mount_ntfs
sudo chmod 0755 /sbin/mount_ntfs
sudo chown 0:0 /sbin/mount_ntfs
sudo emacs /sbin/mount_ntfs
  
créer le nouveau script ainsi :
#!/bin/bash
VOLUME_NAME="${@:$#}"
VOLUME_NAME=${VOLUME_NAME#/Volumes/}
USER_ID=501
GROUP_ID=20
TIMEOUT=20
if [ `/usr/bin/stat -f "%u" /dev/console` -eq 0 ]; then
        USERNAME=`/usr/bin/defaults read /library/preferences/com.apple.loginwindow | /usr/bin/grep autoLoginUser | /usr/bin/awk '{ print $3 }' | /usr/bin/sed 's/;//'`
        if [ "$USERNAME" = "" ]; then
                until [ `stat -f "%u" /dev/console` -ne 0 ] || [ $TIMEOUT -eq 0 ]; do
                        sleep 1
                        let TIMEOUT--
                done
                if [ $TIMEOUT -ne 0 ]; then
                        USER_ID=`/usr/bin/stat -f "%u" /dev/console`
                        GROUP_ID=`/usr/bin/stat -f "%g" /dev/console`
                fi
        else
                USER_ID=`/usr/bin/id -u $USERNAME`
                GROUP_ID=`/usr/bin/id -g $USERNAME`
        fi
else
        USER_ID=`/usr/bin/stat -f "%u" /dev/console`
        GROUP_ID=`/usr/bin/stat -f "%g" /dev/console`
fi

/opt/local/bin/ntfs-3g \
         -o volname="${VOLUME_NAME}" \
         -o local \
         -o negative_vncache \
         -o auto_xattr \
         -o auto_cache \
         -o noatime \
         -o windows_names \
         -o user_xattr \
         -o inherit \
         -o uid=$USER_ID \
         -o gid=$GROUP_ID \
         -o allow_other \
         "$@" &> /var/log/ntfsmnt.log

exit $?;
 
 
 
Ca fonctionne plutôt bien. Mais vous noterez en particulier le problème posé par ce script en cas de multi-users sur la mac.
 Je n'ai pas encore eu le temps de voir si un `id -u` et `id -g` résolvait la question, mais il y a des chances que ce soit exécuté par root donc ça ne changerait rien ... :-/



Source : Description des autres solutions & original de celle-ci : http://fernandofig.wordpress.com/2011/08/08/ntfs-write-support-on-osx-lion-with-ntfs-3g-f/


Etape d'après : faire la même chose pour mes disques ext4 & ext3.


samedi 11 mai 2013

mac scanner



Perso, j'utilise "xsane" installé via macports.

Il y a une version MacOSX précompilée ici :
http://www.sane-project.org/source.html
Il semble que tu puisses également en installer une version alternative :
http://www.macupdate.com/app/mac/27505/twain-sane

Perso, j'avais déjà installé les macports :
http://www.macports.org/
donc j'ai simplement utlisé la procédure d'installation standard pour ces adaptations de paquets linux pour mac:
ports install xsane

samedi 23 mars 2013

.emacs

My .emacs file is usually :

;; .emacs

;;; uncomment this line to disable loading of "default.el" at startup
(setq inhibit-default-init 1)

;; Set your term type to vt100
; (load "term/vt100") 
;; To change the font size under X.
; (set-default-font "9x15")


(setq inhibit-splash-screen t)

;; Remove toobar
(tool-bar-mode 0)
;(menu-bar-mode nil)

;; Color feedback
(global-font-lock-mode t)

;; Visual feedback on selections
(setq transient-mark-mode t)

;; I always want a newline at the end of files
(setq require-final-newline t)

;; And please, put me the cursor where I was
(load-library "saveplace")
(setq-default save-place t)

;; When I'm wrinting text, cut my lines (default : 70)
(add-hook 'text-mode-hook 'turn-on-auto-fill)
; (setq fill-column 80)

;; Disable anoying audio-beep
(setq visible-bell t)

;; Support Wheel Mouse Scrolling
(mouse-wheel-mode t)

;; Kill the whole line (including '\n') when the cursor in col 0
;(setq kill-whole-line t)

;; Window title
(setq frame-title-format '(buffer-file-name "%b [%f]" "%b"))

;; Show line-number and column-number in the mode line
(line-number-mode t)
(column-number-mode t)

;; See current buffer's path in the mode line
(set-default 'mode-line-buffer-identification
             '(buffer-file-name ("%f") ("%b")))


;; Aliases
;(define-key global-map "\C-Menu" 'mouse-buffer-menu)
(define-key global-map "\M-g"   'goto-line)
(define-key global-map "\M-r"   're-search-forward)


;; Modes d'accentuation é
;(setq iso-accents-mode t)
;(iso-accents-mode t)
;(iso-accents-customize "french")
;(iso-accents-customize)


;; Licence 3
(setq auto-mode-alist
      (cons '("\\.hbk\\'" .tex-mode)
        (cons '("\\.bk\\'" .tex-mode)
          (cons '("\\.a[d1-9][bs]\\'" ada-mode)
            auto-mode-alist))))



(setq load-path (append load-path (list "~/.emacs.d/")))

;; Promela mode (M2)
(autoload 'promela-mode "promela-mode" "PROMELA mode" nil t)
(setq auto-mode-alist
      (append
       (list (cons "\\.promela$"  'promela-mode)
         (cons "\\.spin$"     'promela-mode)
         (cons "\\.pml$"      'promela-mode)
         ;; (cons "\\.other-extensions$"     'promela-mode)
         (cons "\\.php5$"     'php-mode)
         )
       auto-mode-alist))

;; Lua editing mode
(setq auto-mode-alist (cons '("\\.lua$" . lua-mode) auto-mode-alist))
(autoload 'lua-mode "lua-mode" "Lua editing mode." t)


(setq load-path (append load-path (list "~/.emacs.d/")))


;; ;; MetaScribe Files : projet RAAR,
;; (load "/home/cm/.emacs.d/metascribe-msf-mode.el")
;; (load "/home/cm/.emacs.d/metascribe-msm-mode.el")
;; (load "/home/cm/.emacs.d/metascribe-mssm-mode.el")
;; (load "/home/cm/.emacs.d/metascribe-msst-mode.el")


(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(case-fold-search t)
 '(current-language-environment "UTF-8")
 '(default-input-method "rfc1345")
 '(global-font-lock-mode t nil (font-lock))
 '(inhibit-startup-screen t)
 '(save-place t nil (saveplace))
 '(text-mode-hook (quote (turn-on-auto-fill text-mode-hook-identify)))
 '(transient-mark-mode t))


(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(default ((t (:inherit nil :stipple nil :background "white" :foreground "black" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 90 :width normal :foundry "unknown" :family "DejaVu Sans Mono")))))

;;(add-to-list 'load-path "/path/to/highlight-symbol")




;; Raccourcis claviers
(global-set-key [f4] 'goto-line)
(global-set-key [f5] 'compile)
(global-set-key [f6] 'comment-region)
(global-set-key [f7] 'uncomment-region)
(global-set-key [f8] 'flyspell-mode)

;; (require 'highlight-symbol)
;; (global-set-key [(control f3)] 'highlight-symbol-at-point)
;; (global-set-key [f3] 'highlight-symbol-next)
;; (global-set-key [(shift f3)] 'highlight-symbol-prev)
;; (global-set-key [(meta f3)] 'highlight-symbol-prev)

3D printing

I recently had to test a 3D printer out of the box.

After a few hours looking for the right open-source tools, I found a proper process to make things !
  1. Draw something with Sketchup (easier, .skt files) or OpenSCAD (funnier, .scad files), or find a model on Thingiverse.com (complete models and very useful scad libraries can be found there).
  2. Export to STL format.
  3. Open ReplicatorG
  4. Generate gcode
  5. send to the printer (via memory card or USB)

NB 0 : Inkscape, DXF files,
I had no luck with Inkscape and the dxf file format so far. Converting it to a stl file seems complicated (and the inkscape plugin supposed to help doing so did not install here).

I had some troubles trying to import dxf files in an openscad code, and since I could find a proper tool to edit dxf files so far, this is still in standby.


Sketchup

Be careful : Sketchup NEEDS the STL export plugin.
Using a MacOSX system, I had to  chmod the plugin directory, or it wouldn't install
sudo chmod 777 /Library/Application\ Support/Google\ SketchUp\ 8/SketchUp/plugins/
Sketchup is pretty easy to use. Great GUI compared to the other tools I could find. Great tool for creating and visualising 3D models. A lot of tutorials are available.

sketchup http://www.sketchup.com/  
sketchup STL plugin https://github.com/SketchUp/sketchup-stl
sketchup developer tools (not tested yet) https://github.com/SketchUp/sketchup-developer-tools


OpenSCAD

It can import (but doesn't seem to be able to convert) your STL files (and DXF ? cf. NB 0), but it's easy to code in SCAD. OpenSCAD editor allows you to view and export to various formats.
BE CAREFUL : you need to compile the file before converting it to STL. The rendered image is not necesarily coherent with what's in the cache of OpenSCAD, you might have suprises once printing (I was about to print something twice the size I needed it for example, the replicatorg visualisation shown me at the next step was somewhat too big.)
http://en.wikibooks.org/wiki/OpenSCAD_User_Manual


ReplicatorG

Be sure to have the right file description for your printer. 

Useful to connect to the printer, or to write to the memory card. You first have to convert the STL file to GCODE, and then you can either print the commands to a memory card (S3G files), or send the commands directly to the printer.
There seems to be a command line interface for replicatorg, but I didn't had time to check it for now.
replicatorg --nogui
 
replicatorG https://github.com/haldean/ReplicatorG


Thingiverse.com

Nowadays, it seems to be the best platform around for sharing 3D models. A lot of them are freely available, let's hope it stays like that ! The interface even allows you to modify some customizable models, but I found it faster to just download them and recalculate them on my machine.
I suggest you to create an account. At least, it helps me keeping track of the good and interesting stuff (I have to admit there is also plenty of junk in the models... )